1315a1350SMichael S. Tsirkin /* 2315a1350SMichael S. Tsirkin * QEMU PCI bus manager 3315a1350SMichael S. Tsirkin * 4315a1350SMichael S. Tsirkin * Copyright (c) 2004 Fabrice Bellard 5315a1350SMichael S. Tsirkin * 6315a1350SMichael S. Tsirkin * Permission is hereby granted, free of charge, to any person obtaining a copy 7315a1350SMichael S. Tsirkin * of this software and associated documentation files (the "Software"), to deal 8315a1350SMichael S. Tsirkin * in the Software without restriction, including without limitation the rights 9315a1350SMichael S. Tsirkin * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10315a1350SMichael S. Tsirkin * copies of the Software, and to permit persons to whom the Software is 11315a1350SMichael S. Tsirkin * furnished to do so, subject to the following conditions: 12315a1350SMichael S. Tsirkin * 13315a1350SMichael S. Tsirkin * The above copyright notice and this permission notice shall be included in 14315a1350SMichael S. Tsirkin * all copies or substantial portions of the Software. 15315a1350SMichael S. Tsirkin * 16315a1350SMichael S. Tsirkin * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17315a1350SMichael S. Tsirkin * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18315a1350SMichael S. Tsirkin * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19315a1350SMichael S. Tsirkin * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20315a1350SMichael S. Tsirkin * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21315a1350SMichael S. Tsirkin * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22315a1350SMichael S. Tsirkin * THE SOFTWARE. 23315a1350SMichael S. Tsirkin */ 24e688df6bSMarkus Armbruster 2597d5408fSPeter Maydell #include "qemu/osdep.h" 262c65db5eSPaolo Bonzini #include "qemu/datadir.h" 277c16b5bbSPaolo Bonzini #include "qemu/units.h" 2864552b6bSMarkus Armbruster #include "hw/irq.h" 29c759b24fSMichael S. Tsirkin #include "hw/pci/pci.h" 30c759b24fSMichael S. Tsirkin #include "hw/pci/pci_bridge.h" 3106aac7bdSMichael S. Tsirkin #include "hw/pci/pci_bus.h" 32568f0690SDavid Gibson #include "hw/pci/pci_host.h" 33a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h" 34ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h" 35ca77ee28SMarkus Armbruster #include "migration/qemu-file-types.h" 36d6454270SMarkus Armbruster #include "migration/vmstate.h" 371422e32dSPaolo Bonzini #include "net/net.h" 38b58c5c2dSMarkus Armbruster #include "sysemu/numa.h" 3946517dd4SMarkus Armbruster #include "sysemu/sysemu.h" 40c759b24fSMichael S. Tsirkin #include "hw/loader.h" 41d49b6836SMarkus Armbruster #include "qemu/error-report.h" 421de7afc9SPaolo Bonzini #include "qemu/range.h" 437828d750SDon Koch #include "trace.h" 44c759b24fSMichael S. Tsirkin #include "hw/pci/msi.h" 45c759b24fSMichael S. Tsirkin #include "hw/pci/msix.h" 465e954943SIgor Mammedov #include "hw/hotplug.h" 47e4024630SLaurent Vivier #include "hw/boards.h" 48e688df6bSMarkus Armbruster #include "qapi/error.h" 49f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 50987b73b3SMarkus Armbruster #include "pci-internal.h" 51315a1350SMichael S. Tsirkin 52315a1350SMichael S. Tsirkin //#define DEBUG_PCI 53315a1350SMichael S. Tsirkin #ifdef DEBUG_PCI 54315a1350SMichael S. Tsirkin # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) 55315a1350SMichael S. Tsirkin #else 56315a1350SMichael S. Tsirkin # define PCI_DPRINTF(format, ...) do { } while (0) 57315a1350SMichael S. Tsirkin #endif 58315a1350SMichael S. Tsirkin 5988c725c7SCornelia Huck bool pci_available = true; 6088c725c7SCornelia Huck 61315a1350SMichael S. Tsirkin static char *pcibus_get_dev_path(DeviceState *dev); 62315a1350SMichael S. Tsirkin static char *pcibus_get_fw_dev_path(DeviceState *dev); 63dcc20931SPaolo Bonzini static void pcibus_reset(BusState *qbus); 64315a1350SMichael S. Tsirkin 65315a1350SMichael S. Tsirkin static Property pci_props[] = { 66315a1350SMichael S. Tsirkin DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1), 67315a1350SMichael S. Tsirkin DEFINE_PROP_STRING("romfile", PCIDevice, romfile), 6808b1df8fSPaolo Bonzini DEFINE_PROP_UINT32("romsize", PCIDevice, romsize, -1), 69315a1350SMichael S. Tsirkin DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1), 70315a1350SMichael S. Tsirkin DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present, 71315a1350SMichael S. Tsirkin QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false), 726b449540SMichael S. Tsirkin DEFINE_PROP_BIT("x-pcie-lnksta-dllla", PCIDevice, cap_present, 736b449540SMichael S. Tsirkin QEMU_PCIE_LNKSTA_DLLLA_BITNR, true), 74f03d8ea3SMarcel Apfelbaum DEFINE_PROP_BIT("x-pcie-extcap-init", PCIDevice, cap_present, 75f03d8ea3SMarcel Apfelbaum QEMU_PCIE_EXTCAP_INIT_BITNR, true), 764f5b6a05SJens Freimann DEFINE_PROP_STRING("failover_pair_id", PCIDevice, 774f5b6a05SJens Freimann failover_pair_id), 78b32bd763SIgor Mammedov DEFINE_PROP_UINT32("acpi-index", PCIDevice, acpi_index, 0), 79315a1350SMichael S. Tsirkin DEFINE_PROP_END_OF_LIST() 80315a1350SMichael S. Tsirkin }; 81315a1350SMichael S. Tsirkin 82d2f69df7SBandan Das static const VMStateDescription vmstate_pcibus = { 83d2f69df7SBandan Das .name = "PCIBUS", 84d2f69df7SBandan Das .version_id = 1, 85d2f69df7SBandan Das .minimum_version_id = 1, 86d2f69df7SBandan Das .fields = (VMStateField[]) { 87d2164ad3SHalil Pasic VMSTATE_INT32_EQUAL(nirq, PCIBus, NULL), 88d2f69df7SBandan Das VMSTATE_VARRAY_INT32(irq_count, PCIBus, 89d2f69df7SBandan Das nirq, 0, vmstate_info_int32, 90d2f69df7SBandan Das int32_t), 91d2f69df7SBandan Das VMSTATE_END_OF_LIST() 92d2f69df7SBandan Das } 93d2f69df7SBandan Das }; 94d2f69df7SBandan Das 95b86eacb8SMarcel Apfelbaum static void pci_init_bus_master(PCIDevice *pci_dev) 96b86eacb8SMarcel Apfelbaum { 97b86eacb8SMarcel Apfelbaum AddressSpace *dma_as = pci_device_iommu_address_space(pci_dev); 98b86eacb8SMarcel Apfelbaum 99b86eacb8SMarcel Apfelbaum memory_region_init_alias(&pci_dev->bus_master_enable_region, 100b86eacb8SMarcel Apfelbaum OBJECT(pci_dev), "bus master", 101b86eacb8SMarcel Apfelbaum dma_as->root, 0, memory_region_size(dma_as->root)); 102b86eacb8SMarcel Apfelbaum memory_region_set_enabled(&pci_dev->bus_master_enable_region, false); 1033716d590SJason Wang memory_region_add_subregion(&pci_dev->bus_master_container_region, 0, 1043716d590SJason Wang &pci_dev->bus_master_enable_region); 105b86eacb8SMarcel Apfelbaum } 106b86eacb8SMarcel Apfelbaum 107b86eacb8SMarcel Apfelbaum static void pcibus_machine_done(Notifier *notifier, void *data) 108b86eacb8SMarcel Apfelbaum { 109b86eacb8SMarcel Apfelbaum PCIBus *bus = container_of(notifier, PCIBus, machine_done); 110b86eacb8SMarcel Apfelbaum int i; 111b86eacb8SMarcel Apfelbaum 112b86eacb8SMarcel Apfelbaum for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { 113b86eacb8SMarcel Apfelbaum if (bus->devices[i]) { 114b86eacb8SMarcel Apfelbaum pci_init_bus_master(bus->devices[i]); 115b86eacb8SMarcel Apfelbaum } 116b86eacb8SMarcel Apfelbaum } 117b86eacb8SMarcel Apfelbaum } 118b86eacb8SMarcel Apfelbaum 119d2f69df7SBandan Das static void pci_bus_realize(BusState *qbus, Error **errp) 120d2f69df7SBandan Das { 121d2f69df7SBandan Das PCIBus *bus = PCI_BUS(qbus); 122d2f69df7SBandan Das 123b86eacb8SMarcel Apfelbaum bus->machine_done.notify = pcibus_machine_done; 124b86eacb8SMarcel Apfelbaum qemu_add_machine_init_done_notifier(&bus->machine_done); 125b86eacb8SMarcel Apfelbaum 1261df2c9a2SPeter Xu vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, &vmstate_pcibus, bus); 127d2f69df7SBandan Das } 128d2f69df7SBandan Das 1292f57db8aSDavid Gibson static void pcie_bus_realize(BusState *qbus, Error **errp) 1302f57db8aSDavid Gibson { 1312f57db8aSDavid Gibson PCIBus *bus = PCI_BUS(qbus); 132b52fa0eaSPhilippe Mathieu-Daudé Error *local_err = NULL; 1332f57db8aSDavid Gibson 134b52fa0eaSPhilippe Mathieu-Daudé pci_bus_realize(qbus, &local_err); 135b52fa0eaSPhilippe Mathieu-Daudé if (local_err) { 136b52fa0eaSPhilippe Mathieu-Daudé error_propagate(errp, local_err); 137b52fa0eaSPhilippe Mathieu-Daudé return; 138b52fa0eaSPhilippe Mathieu-Daudé } 1392f57db8aSDavid Gibson 1402f57db8aSDavid Gibson /* 1412f57db8aSDavid Gibson * A PCI-E bus can support extended config space if it's the root 1422f57db8aSDavid Gibson * bus, or if the bus/bridge above it does as well 1432f57db8aSDavid Gibson */ 1442f57db8aSDavid Gibson if (pci_bus_is_root(bus)) { 1452f57db8aSDavid Gibson bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE; 1462f57db8aSDavid Gibson } else { 1472f57db8aSDavid Gibson PCIBus *parent_bus = pci_get_bus(bus->parent_dev); 1482f57db8aSDavid Gibson 1492f57db8aSDavid Gibson if (pci_bus_allows_extended_config_space(parent_bus)) { 1502f57db8aSDavid Gibson bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE; 1512f57db8aSDavid Gibson } 1522f57db8aSDavid Gibson } 1532f57db8aSDavid Gibson } 1542f57db8aSDavid Gibson 155b69c3c21SMarkus Armbruster static void pci_bus_unrealize(BusState *qbus) 156d2f69df7SBandan Das { 157d2f69df7SBandan Das PCIBus *bus = PCI_BUS(qbus); 158d2f69df7SBandan Das 159b86eacb8SMarcel Apfelbaum qemu_remove_machine_init_done_notifier(&bus->machine_done); 160b86eacb8SMarcel Apfelbaum 161d2f69df7SBandan Das vmstate_unregister(NULL, &vmstate_pcibus, bus); 162d2f69df7SBandan Das } 163d2f69df7SBandan Das 164602141d9SMarcel Apfelbaum static int pcibus_num(PCIBus *bus) 165602141d9SMarcel Apfelbaum { 166b0e5196aSDavid Gibson if (pci_bus_is_root(bus)) { 167602141d9SMarcel Apfelbaum return 0; /* pci host bridge */ 168602141d9SMarcel Apfelbaum } 169602141d9SMarcel Apfelbaum return bus->parent_dev->config[PCI_SECONDARY_BUS]; 170602141d9SMarcel Apfelbaum } 171602141d9SMarcel Apfelbaum 1726a3042b2SMarcel Apfelbaum static uint16_t pcibus_numa_node(PCIBus *bus) 1736a3042b2SMarcel Apfelbaum { 1746a3042b2SMarcel Apfelbaum return NUMA_NODE_UNASSIGNED; 1756a3042b2SMarcel Apfelbaum } 1766a3042b2SMarcel Apfelbaum 177315a1350SMichael S. Tsirkin static void pci_bus_class_init(ObjectClass *klass, void *data) 178315a1350SMichael S. Tsirkin { 179315a1350SMichael S. Tsirkin BusClass *k = BUS_CLASS(klass); 180ce6a28eeSMarcel Apfelbaum PCIBusClass *pbc = PCI_BUS_CLASS(klass); 181315a1350SMichael S. Tsirkin 182315a1350SMichael S. Tsirkin k->print_dev = pcibus_dev_print; 183315a1350SMichael S. Tsirkin k->get_dev_path = pcibus_get_dev_path; 184315a1350SMichael S. Tsirkin k->get_fw_dev_path = pcibus_get_fw_dev_path; 185d2f69df7SBandan Das k->realize = pci_bus_realize; 186d2f69df7SBandan Das k->unrealize = pci_bus_unrealize; 187315a1350SMichael S. Tsirkin k->reset = pcibus_reset; 188ce6a28eeSMarcel Apfelbaum 189602141d9SMarcel Apfelbaum pbc->bus_num = pcibus_num; 1906a3042b2SMarcel Apfelbaum pbc->numa_node = pcibus_numa_node; 191315a1350SMichael S. Tsirkin } 192315a1350SMichael S. Tsirkin 193315a1350SMichael S. Tsirkin static const TypeInfo pci_bus_info = { 194315a1350SMichael S. Tsirkin .name = TYPE_PCI_BUS, 195315a1350SMichael S. Tsirkin .parent = TYPE_BUS, 196315a1350SMichael S. Tsirkin .instance_size = sizeof(PCIBus), 197ce6a28eeSMarcel Apfelbaum .class_size = sizeof(PCIBusClass), 198315a1350SMichael S. Tsirkin .class_init = pci_bus_class_init, 199315a1350SMichael S. Tsirkin }; 200315a1350SMichael S. Tsirkin 201cf04aba2SBen Widawsky static const TypeInfo cxl_interface_info = { 202cf04aba2SBen Widawsky .name = INTERFACE_CXL_DEVICE, 203cf04aba2SBen Widawsky .parent = TYPE_INTERFACE, 204cf04aba2SBen Widawsky }; 205cf04aba2SBen Widawsky 206619f02aeSEduardo Habkost static const TypeInfo pcie_interface_info = { 207619f02aeSEduardo Habkost .name = INTERFACE_PCIE_DEVICE, 208619f02aeSEduardo Habkost .parent = TYPE_INTERFACE, 209619f02aeSEduardo Habkost }; 210619f02aeSEduardo Habkost 211619f02aeSEduardo Habkost static const TypeInfo conventional_pci_interface_info = { 212619f02aeSEduardo Habkost .name = INTERFACE_CONVENTIONAL_PCI_DEVICE, 213619f02aeSEduardo Habkost .parent = TYPE_INTERFACE, 214619f02aeSEduardo Habkost }; 215619f02aeSEduardo Habkost 2161c685a90SGreg Kurz static void pcie_bus_class_init(ObjectClass *klass, void *data) 2171c685a90SGreg Kurz { 2182f57db8aSDavid Gibson BusClass *k = BUS_CLASS(klass); 2191c685a90SGreg Kurz 2202f57db8aSDavid Gibson k->realize = pcie_bus_realize; 2211c685a90SGreg Kurz } 2221c685a90SGreg Kurz 2233a861c46SAlex Williamson static const TypeInfo pcie_bus_info = { 2243a861c46SAlex Williamson .name = TYPE_PCIE_BUS, 2253a861c46SAlex Williamson .parent = TYPE_PCI_BUS, 2261c685a90SGreg Kurz .class_init = pcie_bus_class_init, 2273a861c46SAlex Williamson }; 2283a861c46SAlex Williamson 2294f8db871SBen Widawsky static const TypeInfo cxl_bus_info = { 2304f8db871SBen Widawsky .name = TYPE_CXL_BUS, 2314f8db871SBen Widawsky .parent = TYPE_PCIE_BUS, 2324f8db871SBen Widawsky .class_init = pcie_bus_class_init, 2334f8db871SBen Widawsky }; 2344f8db871SBen Widawsky 235315a1350SMichael S. Tsirkin static void pci_update_mappings(PCIDevice *d); 236d98f08f5SMarcel Apfelbaum static void pci_irq_handler(void *opaque, int irq_num, int level); 237133e9b22SMarkus Armbruster static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, Error **); 238315a1350SMichael S. Tsirkin static void pci_del_option_rom(PCIDevice *pdev); 239315a1350SMichael S. Tsirkin 240315a1350SMichael S. Tsirkin static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET; 241315a1350SMichael S. Tsirkin static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU; 242315a1350SMichael S. Tsirkin 243987b73b3SMarkus Armbruster PCIHostStateList pci_host_bridges; 244315a1350SMichael S. Tsirkin 245cf8c704dSMichael Roth int pci_bar(PCIDevice *d, int reg) 246315a1350SMichael S. Tsirkin { 247315a1350SMichael S. Tsirkin uint8_t type; 248315a1350SMichael S. Tsirkin 2497c0fa8dfSKnut Omang /* PCIe virtual functions do not have their own BARs */ 2507c0fa8dfSKnut Omang assert(!pci_is_vf(d)); 2517c0fa8dfSKnut Omang 252315a1350SMichael S. Tsirkin if (reg != PCI_ROM_SLOT) 253315a1350SMichael S. Tsirkin return PCI_BASE_ADDRESS_0 + reg * 4; 254315a1350SMichael S. Tsirkin 255315a1350SMichael S. Tsirkin type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; 256315a1350SMichael S. Tsirkin return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS; 257315a1350SMichael S. Tsirkin } 258315a1350SMichael S. Tsirkin 259315a1350SMichael S. Tsirkin static inline int pci_irq_state(PCIDevice *d, int irq_num) 260315a1350SMichael S. Tsirkin { 261315a1350SMichael S. Tsirkin return (d->irq_state >> irq_num) & 0x1; 262315a1350SMichael S. Tsirkin } 263315a1350SMichael S. Tsirkin 264315a1350SMichael S. Tsirkin static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level) 265315a1350SMichael S. Tsirkin { 266315a1350SMichael S. Tsirkin d->irq_state &= ~(0x1 << irq_num); 267315a1350SMichael S. Tsirkin d->irq_state |= level << irq_num; 268315a1350SMichael S. Tsirkin } 269315a1350SMichael S. Tsirkin 270b06fe3e7SPhilippe Mathieu-Daudé static void pci_bus_change_irq_level(PCIBus *bus, int irq_num, int change) 271b06fe3e7SPhilippe Mathieu-Daudé { 272459ca8bfSMark Cave-Ayland assert(irq_num >= 0); 273459ca8bfSMark Cave-Ayland assert(irq_num < bus->nirq); 274b06fe3e7SPhilippe Mathieu-Daudé bus->irq_count[irq_num] += change; 275b06fe3e7SPhilippe Mathieu-Daudé bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0); 276b06fe3e7SPhilippe Mathieu-Daudé } 277b06fe3e7SPhilippe Mathieu-Daudé 278315a1350SMichael S. Tsirkin static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change) 279315a1350SMichael S. Tsirkin { 280315a1350SMichael S. Tsirkin PCIBus *bus; 281315a1350SMichael S. Tsirkin for (;;) { 282fd56e061SDavid Gibson bus = pci_get_bus(pci_dev); 283f021f4e9SBernhard Beschow assert(bus->map_irq); 284315a1350SMichael S. Tsirkin irq_num = bus->map_irq(pci_dev, irq_num); 285315a1350SMichael S. Tsirkin if (bus->set_irq) 286315a1350SMichael S. Tsirkin break; 287315a1350SMichael S. Tsirkin pci_dev = bus->parent_dev; 288315a1350SMichael S. Tsirkin } 289b06fe3e7SPhilippe Mathieu-Daudé pci_bus_change_irq_level(bus, irq_num, change); 290315a1350SMichael S. Tsirkin } 291315a1350SMichael S. Tsirkin 292315a1350SMichael S. Tsirkin int pci_bus_get_irq_level(PCIBus *bus, int irq_num) 293315a1350SMichael S. Tsirkin { 294315a1350SMichael S. Tsirkin assert(irq_num >= 0); 295315a1350SMichael S. Tsirkin assert(irq_num < bus->nirq); 296315a1350SMichael S. Tsirkin return !!bus->irq_count[irq_num]; 297315a1350SMichael S. Tsirkin } 298315a1350SMichael S. Tsirkin 299315a1350SMichael S. Tsirkin /* Update interrupt status bit in config space on interrupt 300315a1350SMichael S. Tsirkin * state change. */ 301315a1350SMichael S. Tsirkin static void pci_update_irq_status(PCIDevice *dev) 302315a1350SMichael S. Tsirkin { 303315a1350SMichael S. Tsirkin if (dev->irq_state) { 304315a1350SMichael S. Tsirkin dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT; 305315a1350SMichael S. Tsirkin } else { 306315a1350SMichael S. Tsirkin dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; 307315a1350SMichael S. Tsirkin } 308315a1350SMichael S. Tsirkin } 309315a1350SMichael S. Tsirkin 310315a1350SMichael S. Tsirkin void pci_device_deassert_intx(PCIDevice *dev) 311315a1350SMichael S. Tsirkin { 312315a1350SMichael S. Tsirkin int i; 313315a1350SMichael S. Tsirkin for (i = 0; i < PCI_NUM_PINS; ++i) { 314d98f08f5SMarcel Apfelbaum pci_irq_handler(dev, i, 0); 315315a1350SMichael S. Tsirkin } 316315a1350SMichael S. Tsirkin } 317315a1350SMichael S. Tsirkin 31808cf3dc6SJagannathan Raman static void pci_msi_trigger(PCIDevice *dev, MSIMessage msg) 31908cf3dc6SJagannathan Raman { 32008cf3dc6SJagannathan Raman MemTxAttrs attrs = {}; 32108cf3dc6SJagannathan Raman 32208cf3dc6SJagannathan Raman attrs.requester_id = pci_requester_id(dev); 32308cf3dc6SJagannathan Raman address_space_stl_le(&dev->bus_master_as, msg.address, msg.data, 32408cf3dc6SJagannathan Raman attrs, NULL); 32508cf3dc6SJagannathan Raman } 32608cf3dc6SJagannathan Raman 3277c0fa8dfSKnut Omang static void pci_reset_regions(PCIDevice *dev) 328315a1350SMichael S. Tsirkin { 329315a1350SMichael S. Tsirkin int r; 3307c0fa8dfSKnut Omang if (pci_is_vf(dev)) { 3317c0fa8dfSKnut Omang return; 3327c0fa8dfSKnut Omang } 333315a1350SMichael S. Tsirkin 3347c0fa8dfSKnut Omang for (r = 0; r < PCI_NUM_REGIONS; ++r) { 3357c0fa8dfSKnut Omang PCIIORegion *region = &dev->io_regions[r]; 3367c0fa8dfSKnut Omang if (!region->size) { 3377c0fa8dfSKnut Omang continue; 3387c0fa8dfSKnut Omang } 3397c0fa8dfSKnut Omang 3407c0fa8dfSKnut Omang if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) && 3417c0fa8dfSKnut Omang region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { 3427c0fa8dfSKnut Omang pci_set_quad(dev->config + pci_bar(dev, r), region->type); 3437c0fa8dfSKnut Omang } else { 3447c0fa8dfSKnut Omang pci_set_long(dev->config + pci_bar(dev, r), region->type); 3457c0fa8dfSKnut Omang } 3467c0fa8dfSKnut Omang } 3477c0fa8dfSKnut Omang } 3487c0fa8dfSKnut Omang 3497c0fa8dfSKnut Omang static void pci_do_device_reset(PCIDevice *dev) 3507c0fa8dfSKnut Omang { 351315a1350SMichael S. Tsirkin pci_device_deassert_intx(dev); 35258b59014SCole Robinson assert(dev->irq_state == 0); 35358b59014SCole Robinson 354315a1350SMichael S. Tsirkin /* Clear all writable bits */ 355315a1350SMichael S. Tsirkin pci_word_test_and_clear_mask(dev->config + PCI_COMMAND, 356315a1350SMichael S. Tsirkin pci_get_word(dev->wmask + PCI_COMMAND) | 357315a1350SMichael S. Tsirkin pci_get_word(dev->w1cmask + PCI_COMMAND)); 358315a1350SMichael S. Tsirkin pci_word_test_and_clear_mask(dev->config + PCI_STATUS, 359315a1350SMichael S. Tsirkin pci_get_word(dev->wmask + PCI_STATUS) | 360315a1350SMichael S. Tsirkin pci_get_word(dev->w1cmask + PCI_STATUS)); 3617ff81d63SBALATON Zoltan /* Some devices make bits of PCI_INTERRUPT_LINE read only */ 3627ff81d63SBALATON Zoltan pci_byte_test_and_clear_mask(dev->config + PCI_INTERRUPT_LINE, 3637ff81d63SBALATON Zoltan pci_get_word(dev->wmask + PCI_INTERRUPT_LINE) | 3647ff81d63SBALATON Zoltan pci_get_word(dev->w1cmask + PCI_INTERRUPT_LINE)); 365315a1350SMichael S. Tsirkin dev->config[PCI_CACHE_LINE_SIZE] = 0x0; 3667c0fa8dfSKnut Omang pci_reset_regions(dev); 367315a1350SMichael S. Tsirkin pci_update_mappings(dev); 368315a1350SMichael S. Tsirkin 369315a1350SMichael S. Tsirkin msi_reset(dev); 370315a1350SMichael S. Tsirkin msix_reset(dev); 371315a1350SMichael S. Tsirkin } 372315a1350SMichael S. Tsirkin 373315a1350SMichael S. Tsirkin /* 374dcc20931SPaolo Bonzini * This function is called on #RST and FLR. 375dcc20931SPaolo Bonzini * FLR if PCI_EXP_DEVCTL_BCR_FLR is set 376315a1350SMichael S. Tsirkin */ 377dcc20931SPaolo Bonzini void pci_device_reset(PCIDevice *dev) 378dcc20931SPaolo Bonzini { 37978e4d5cbSPeter Maydell device_cold_reset(&dev->qdev); 380dcc20931SPaolo Bonzini pci_do_device_reset(dev); 381dcc20931SPaolo Bonzini } 382dcc20931SPaolo Bonzini 383dcc20931SPaolo Bonzini /* 384dcc20931SPaolo Bonzini * Trigger pci bus reset under a given bus. 38578e4d5cbSPeter Maydell * Called via bus_cold_reset on RST# assert, after the devices 38678e4d5cbSPeter Maydell * have been reset device_cold_reset-ed already. 387dcc20931SPaolo Bonzini */ 388dcc20931SPaolo Bonzini static void pcibus_reset(BusState *qbus) 389315a1350SMichael S. Tsirkin { 39081e3e75bSPaolo Bonzini PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus); 391315a1350SMichael S. Tsirkin int i; 392315a1350SMichael S. Tsirkin 393315a1350SMichael S. Tsirkin for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { 394315a1350SMichael S. Tsirkin if (bus->devices[i]) { 395dcc20931SPaolo Bonzini pci_do_device_reset(bus->devices[i]); 396315a1350SMichael S. Tsirkin } 397315a1350SMichael S. Tsirkin } 398315a1350SMichael S. Tsirkin 3999bdbbfc3SPaolo Bonzini for (i = 0; i < bus->nirq; i++) { 4009bdbbfc3SPaolo Bonzini assert(bus->irq_count[i] == 0); 4019bdbbfc3SPaolo Bonzini } 402315a1350SMichael S. Tsirkin } 403315a1350SMichael S. Tsirkin 4043dbc01aeSCao jin static void pci_host_bus_register(DeviceState *host) 405315a1350SMichael S. Tsirkin { 4063dbc01aeSCao jin PCIHostState *host_bridge = PCI_HOST_BRIDGE(host); 4077588e2b0SDavid Gibson 4087588e2b0SDavid Gibson QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next); 409315a1350SMichael S. Tsirkin } 410315a1350SMichael S. Tsirkin 411c13ee169SMichael Roth static void pci_host_bus_unregister(DeviceState *host) 412c13ee169SMichael Roth { 413c13ee169SMichael Roth PCIHostState *host_bridge = PCI_HOST_BRIDGE(host); 414c13ee169SMichael Roth 415c13ee169SMichael Roth QLIST_REMOVE(host_bridge, next); 416c13ee169SMichael Roth } 417c13ee169SMichael Roth 418c473d18dSDavid Gibson PCIBus *pci_device_root_bus(const PCIDevice *d) 419315a1350SMichael S. Tsirkin { 420fd56e061SDavid Gibson PCIBus *bus = pci_get_bus(d); 421315a1350SMichael S. Tsirkin 422ce6a28eeSMarcel Apfelbaum while (!pci_bus_is_root(bus)) { 423ce6a28eeSMarcel Apfelbaum d = bus->parent_dev; 424ce6a28eeSMarcel Apfelbaum assert(d != NULL); 425ce6a28eeSMarcel Apfelbaum 426fd56e061SDavid Gibson bus = pci_get_bus(d); 427315a1350SMichael S. Tsirkin } 428315a1350SMichael S. Tsirkin 429c473d18dSDavid Gibson return bus; 430315a1350SMichael S. Tsirkin } 431315a1350SMichael S. Tsirkin 432568f0690SDavid Gibson const char *pci_root_bus_path(PCIDevice *dev) 433c473d18dSDavid Gibson { 434568f0690SDavid Gibson PCIBus *rootbus = pci_device_root_bus(dev); 435568f0690SDavid Gibson PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent); 436568f0690SDavid Gibson PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge); 437c473d18dSDavid Gibson 438568f0690SDavid Gibson assert(host_bridge->bus == rootbus); 439568f0690SDavid Gibson 440568f0690SDavid Gibson if (hc->root_bus_path) { 441568f0690SDavid Gibson return (*hc->root_bus_path)(host_bridge, rootbus); 442315a1350SMichael S. Tsirkin } 443315a1350SMichael S. Tsirkin 444568f0690SDavid Gibson return rootbus->qbus.name; 445315a1350SMichael S. Tsirkin } 446315a1350SMichael S. Tsirkin 4472d64b7bbSXingang Wang bool pci_bus_bypass_iommu(PCIBus *bus) 4482d64b7bbSXingang Wang { 4492d64b7bbSXingang Wang PCIBus *rootbus = bus; 4502d64b7bbSXingang Wang PCIHostState *host_bridge; 4512d64b7bbSXingang Wang 4522d64b7bbSXingang Wang if (!pci_bus_is_root(bus)) { 4532d64b7bbSXingang Wang rootbus = pci_device_root_bus(bus->parent_dev); 4542d64b7bbSXingang Wang } 4552d64b7bbSXingang Wang 4562d64b7bbSXingang Wang host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent); 4572d64b7bbSXingang Wang 4582d64b7bbSXingang Wang assert(host_bridge->bus == rootbus); 4592d64b7bbSXingang Wang 4602d64b7bbSXingang Wang return host_bridge->bypass_iommu; 4612d64b7bbSXingang Wang } 4622d64b7bbSXingang Wang 4638d4cdf01SPeter Maydell static void pci_root_bus_internal_init(PCIBus *bus, DeviceState *parent, 464315a1350SMichael S. Tsirkin MemoryRegion *address_space_mem, 465315a1350SMichael S. Tsirkin MemoryRegion *address_space_io, 466315a1350SMichael S. Tsirkin uint8_t devfn_min) 467315a1350SMichael S. Tsirkin { 468315a1350SMichael S. Tsirkin assert(PCI_FUNC(devfn_min) == 0); 469315a1350SMichael S. Tsirkin bus->devfn_min = devfn_min; 4708b884984SMark Cave-Ayland bus->slot_reserved_mask = 0x0; 471315a1350SMichael S. Tsirkin bus->address_space_mem = address_space_mem; 472315a1350SMichael S. Tsirkin bus->address_space_io = address_space_io; 473b0e5196aSDavid Gibson bus->flags |= PCI_BUS_IS_ROOT; 474315a1350SMichael S. Tsirkin 475315a1350SMichael S. Tsirkin /* host bridge */ 476315a1350SMichael S. Tsirkin QLIST_INIT(&bus->child); 4772b8cc89aSDavid Gibson 4783dbc01aeSCao jin pci_host_bus_register(parent); 479315a1350SMichael S. Tsirkin } 480315a1350SMichael S. Tsirkin 481c13ee169SMichael Roth static void pci_bus_uninit(PCIBus *bus) 482c13ee169SMichael Roth { 483c13ee169SMichael Roth pci_host_bus_unregister(BUS(bus)->parent); 484c13ee169SMichael Roth } 485c13ee169SMichael Roth 486c6f16471SIgor Mammedov bool pci_bus_is_express(const PCIBus *bus) 4878c0bf9e2SAlex Williamson { 4888c0bf9e2SAlex Williamson return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS); 4898c0bf9e2SAlex Williamson } 4908c0bf9e2SAlex Williamson 4918d4cdf01SPeter Maydell void pci_root_bus_init(PCIBus *bus, size_t bus_size, DeviceState *parent, 4924fec6404SPaolo Bonzini const char *name, 4934fec6404SPaolo Bonzini MemoryRegion *address_space_mem, 4944fec6404SPaolo Bonzini MemoryRegion *address_space_io, 49560a0e443SAlex Williamson uint8_t devfn_min, const char *typename) 4964fec6404SPaolo Bonzini { 497d637e1dcSPeter Maydell qbus_init(bus, bus_size, typename, parent, name); 4988d4cdf01SPeter Maydell pci_root_bus_internal_init(bus, parent, address_space_mem, 4998d4cdf01SPeter Maydell address_space_io, devfn_min); 5004fec6404SPaolo Bonzini } 5014fec6404SPaolo Bonzini 5021115ff6dSDavid Gibson PCIBus *pci_root_bus_new(DeviceState *parent, const char *name, 503315a1350SMichael S. Tsirkin MemoryRegion *address_space_mem, 504315a1350SMichael S. Tsirkin MemoryRegion *address_space_io, 50560a0e443SAlex Williamson uint8_t devfn_min, const char *typename) 506315a1350SMichael S. Tsirkin { 507315a1350SMichael S. Tsirkin PCIBus *bus; 508315a1350SMichael S. Tsirkin 5099388d170SPeter Maydell bus = PCI_BUS(qbus_new(typename, parent, name)); 5108d4cdf01SPeter Maydell pci_root_bus_internal_init(bus, parent, address_space_mem, 5118d4cdf01SPeter Maydell address_space_io, devfn_min); 512315a1350SMichael S. Tsirkin return bus; 513315a1350SMichael S. Tsirkin } 514315a1350SMichael S. Tsirkin 515c13ee169SMichael Roth void pci_root_bus_cleanup(PCIBus *bus) 516c13ee169SMichael Roth { 517c13ee169SMichael Roth pci_bus_uninit(bus); 51807578b0aSDavid Hildenbrand /* the caller of the unplug hotplug handler will delete this device */ 519f1483b46SMarkus Armbruster qbus_unrealize(BUS(bus)); 520c13ee169SMichael Roth } 521c13ee169SMichael Roth 522f021f4e9SBernhard Beschow void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, 523315a1350SMichael S. Tsirkin void *irq_opaque, int nirq) 524315a1350SMichael S. Tsirkin { 525315a1350SMichael S. Tsirkin bus->set_irq = set_irq; 526315a1350SMichael S. Tsirkin bus->irq_opaque = irq_opaque; 527315a1350SMichael S. Tsirkin bus->nirq = nirq; 528315a1350SMichael S. Tsirkin bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0])); 529315a1350SMichael S. Tsirkin } 530315a1350SMichael S. Tsirkin 531f021f4e9SBernhard Beschow void pci_bus_map_irqs(PCIBus *bus, pci_map_irq_fn map_irq) 532f021f4e9SBernhard Beschow { 533f021f4e9SBernhard Beschow bus->map_irq = map_irq; 534f021f4e9SBernhard Beschow } 535f021f4e9SBernhard Beschow 536c13ee169SMichael Roth void pci_bus_irqs_cleanup(PCIBus *bus) 537c13ee169SMichael Roth { 538c13ee169SMichael Roth bus->set_irq = NULL; 539c13ee169SMichael Roth bus->map_irq = NULL; 540c13ee169SMichael Roth bus->irq_opaque = NULL; 541c13ee169SMichael Roth bus->nirq = 0; 542c13ee169SMichael Roth g_free(bus->irq_count); 543c13ee169SMichael Roth } 544c13ee169SMichael Roth 5451115ff6dSDavid Gibson PCIBus *pci_register_root_bus(DeviceState *parent, const char *name, 546315a1350SMichael S. Tsirkin pci_set_irq_fn set_irq, pci_map_irq_fn map_irq, 547315a1350SMichael S. Tsirkin void *irq_opaque, 548315a1350SMichael S. Tsirkin MemoryRegion *address_space_mem, 549315a1350SMichael S. Tsirkin MemoryRegion *address_space_io, 5501115ff6dSDavid Gibson uint8_t devfn_min, int nirq, 5511115ff6dSDavid Gibson const char *typename) 552315a1350SMichael S. Tsirkin { 553315a1350SMichael S. Tsirkin PCIBus *bus; 554315a1350SMichael S. Tsirkin 5551115ff6dSDavid Gibson bus = pci_root_bus_new(parent, name, address_space_mem, 55660a0e443SAlex Williamson address_space_io, devfn_min, typename); 557f021f4e9SBernhard Beschow pci_bus_irqs(bus, set_irq, irq_opaque, nirq); 558f021f4e9SBernhard Beschow pci_bus_map_irqs(bus, map_irq); 559315a1350SMichael S. Tsirkin return bus; 560315a1350SMichael S. Tsirkin } 561315a1350SMichael S. Tsirkin 562c13ee169SMichael Roth void pci_unregister_root_bus(PCIBus *bus) 563c13ee169SMichael Roth { 564c13ee169SMichael Roth pci_bus_irqs_cleanup(bus); 565c13ee169SMichael Roth pci_root_bus_cleanup(bus); 566c13ee169SMichael Roth } 567c13ee169SMichael Roth 568315a1350SMichael S. Tsirkin int pci_bus_num(PCIBus *s) 569315a1350SMichael S. Tsirkin { 570602141d9SMarcel Apfelbaum return PCI_BUS_GET_CLASS(s)->bus_num(s); 571315a1350SMichael S. Tsirkin } 572315a1350SMichael S. Tsirkin 573500db1daSXingang Wang /* Returns the min and max bus numbers of a PCI bus hierarchy */ 574500db1daSXingang Wang void pci_bus_range(PCIBus *bus, int *min_bus, int *max_bus) 575500db1daSXingang Wang { 576500db1daSXingang Wang int i; 577500db1daSXingang Wang *min_bus = *max_bus = pci_bus_num(bus); 578500db1daSXingang Wang 579500db1daSXingang Wang for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { 580500db1daSXingang Wang PCIDevice *dev = bus->devices[i]; 581500db1daSXingang Wang 582ad494274SIgor Mammedov if (dev && IS_PCI_BRIDGE(dev)) { 583500db1daSXingang Wang *min_bus = MIN(*min_bus, dev->config[PCI_SECONDARY_BUS]); 584500db1daSXingang Wang *max_bus = MAX(*max_bus, dev->config[PCI_SUBORDINATE_BUS]); 585500db1daSXingang Wang } 586500db1daSXingang Wang } 587500db1daSXingang Wang } 588500db1daSXingang Wang 5896a3042b2SMarcel Apfelbaum int pci_bus_numa_node(PCIBus *bus) 5906a3042b2SMarcel Apfelbaum { 5916a3042b2SMarcel Apfelbaum return PCI_BUS_GET_CLASS(bus)->numa_node(bus); 592315a1350SMichael S. Tsirkin } 593315a1350SMichael S. Tsirkin 5942c21ee76SJianjun Duan static int get_pci_config_device(QEMUFile *f, void *pv, size_t size, 59503fee66fSMarc-André Lureau const VMStateField *field) 596315a1350SMichael S. Tsirkin { 597315a1350SMichael S. Tsirkin PCIDevice *s = container_of(pv, PCIDevice, config); 598315a1350SMichael S. Tsirkin uint8_t *config; 599315a1350SMichael S. Tsirkin int i; 600315a1350SMichael S. Tsirkin 601315a1350SMichael S. Tsirkin assert(size == pci_config_size(s)); 602315a1350SMichael S. Tsirkin config = g_malloc(size); 603315a1350SMichael S. Tsirkin 604315a1350SMichael S. Tsirkin qemu_get_buffer(f, config, size); 605315a1350SMichael S. Tsirkin for (i = 0; i < size; ++i) { 606315a1350SMichael S. Tsirkin if ((config[i] ^ s->config[i]) & 607315a1350SMichael S. Tsirkin s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) { 6087c59364dSDr. David Alan Gilbert error_report("%s: Bad config data: i=0x%x read: %x device: %x " 6097c59364dSDr. David Alan Gilbert "cmask: %x wmask: %x w1cmask:%x", __func__, 6107c59364dSDr. David Alan Gilbert i, config[i], s->config[i], 6117c59364dSDr. David Alan Gilbert s->cmask[i], s->wmask[i], s->w1cmask[i]); 612315a1350SMichael S. Tsirkin g_free(config); 613315a1350SMichael S. Tsirkin return -EINVAL; 614315a1350SMichael S. Tsirkin } 615315a1350SMichael S. Tsirkin } 616315a1350SMichael S. Tsirkin memcpy(s->config, config, size); 617315a1350SMichael S. Tsirkin 618315a1350SMichael S. Tsirkin pci_update_mappings(s); 619ad494274SIgor Mammedov if (IS_PCI_BRIDGE(s)) { 620ad494274SIgor Mammedov pci_bridge_update_mappings(PCI_BRIDGE(s)); 621e78e9ae4SDon Koch } 622315a1350SMichael S. Tsirkin 623315a1350SMichael S. Tsirkin memory_region_set_enabled(&s->bus_master_enable_region, 624315a1350SMichael S. Tsirkin pci_get_word(s->config + PCI_COMMAND) 625315a1350SMichael S. Tsirkin & PCI_COMMAND_MASTER); 626315a1350SMichael S. Tsirkin 627315a1350SMichael S. Tsirkin g_free(config); 628315a1350SMichael S. Tsirkin return 0; 629315a1350SMichael S. Tsirkin } 630315a1350SMichael S. Tsirkin 631315a1350SMichael S. Tsirkin /* just put buffer */ 6322c21ee76SJianjun Duan static int put_pci_config_device(QEMUFile *f, void *pv, size_t size, 6333ddba9a9SMarkus Armbruster const VMStateField *field, JSONWriter *vmdesc) 634315a1350SMichael S. Tsirkin { 635315a1350SMichael S. Tsirkin const uint8_t **v = pv; 636315a1350SMichael S. Tsirkin assert(size == pci_config_size(container_of(pv, PCIDevice, config))); 637315a1350SMichael S. Tsirkin qemu_put_buffer(f, *v, size); 6382c21ee76SJianjun Duan 6392c21ee76SJianjun Duan return 0; 640315a1350SMichael S. Tsirkin } 641315a1350SMichael S. Tsirkin 642315a1350SMichael S. Tsirkin static VMStateInfo vmstate_info_pci_config = { 643315a1350SMichael S. Tsirkin .name = "pci config", 644315a1350SMichael S. Tsirkin .get = get_pci_config_device, 645315a1350SMichael S. Tsirkin .put = put_pci_config_device, 646315a1350SMichael S. Tsirkin }; 647315a1350SMichael S. Tsirkin 6482c21ee76SJianjun Duan static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size, 64903fee66fSMarc-André Lureau const VMStateField *field) 650315a1350SMichael S. Tsirkin { 651315a1350SMichael S. Tsirkin PCIDevice *s = container_of(pv, PCIDevice, irq_state); 652315a1350SMichael S. Tsirkin uint32_t irq_state[PCI_NUM_PINS]; 653315a1350SMichael S. Tsirkin int i; 654315a1350SMichael S. Tsirkin for (i = 0; i < PCI_NUM_PINS; ++i) { 655315a1350SMichael S. Tsirkin irq_state[i] = qemu_get_be32(f); 656315a1350SMichael S. Tsirkin if (irq_state[i] != 0x1 && irq_state[i] != 0) { 657315a1350SMichael S. Tsirkin fprintf(stderr, "irq state %d: must be 0 or 1.\n", 658315a1350SMichael S. Tsirkin irq_state[i]); 659315a1350SMichael S. Tsirkin return -EINVAL; 660315a1350SMichael S. Tsirkin } 661315a1350SMichael S. Tsirkin } 662315a1350SMichael S. Tsirkin 663315a1350SMichael S. Tsirkin for (i = 0; i < PCI_NUM_PINS; ++i) { 664315a1350SMichael S. Tsirkin pci_set_irq_state(s, i, irq_state[i]); 665315a1350SMichael S. Tsirkin } 666315a1350SMichael S. Tsirkin 667315a1350SMichael S. Tsirkin return 0; 668315a1350SMichael S. Tsirkin } 669315a1350SMichael S. Tsirkin 6702c21ee76SJianjun Duan static int put_pci_irq_state(QEMUFile *f, void *pv, size_t size, 6713ddba9a9SMarkus Armbruster const VMStateField *field, JSONWriter *vmdesc) 672315a1350SMichael S. Tsirkin { 673315a1350SMichael S. Tsirkin int i; 674315a1350SMichael S. Tsirkin PCIDevice *s = container_of(pv, PCIDevice, irq_state); 675315a1350SMichael S. Tsirkin 676315a1350SMichael S. Tsirkin for (i = 0; i < PCI_NUM_PINS; ++i) { 677315a1350SMichael S. Tsirkin qemu_put_be32(f, pci_irq_state(s, i)); 678315a1350SMichael S. Tsirkin } 6792c21ee76SJianjun Duan 6802c21ee76SJianjun Duan return 0; 681315a1350SMichael S. Tsirkin } 682315a1350SMichael S. Tsirkin 683315a1350SMichael S. Tsirkin static VMStateInfo vmstate_info_pci_irq_state = { 684315a1350SMichael S. Tsirkin .name = "pci irq state", 685315a1350SMichael S. Tsirkin .get = get_pci_irq_state, 686315a1350SMichael S. Tsirkin .put = put_pci_irq_state, 687315a1350SMichael S. Tsirkin }; 688315a1350SMichael S. Tsirkin 68920daa90aSDr. David Alan Gilbert static bool migrate_is_pcie(void *opaque, int version_id) 69020daa90aSDr. David Alan Gilbert { 69120daa90aSDr. David Alan Gilbert return pci_is_express((PCIDevice *)opaque); 69220daa90aSDr. David Alan Gilbert } 69320daa90aSDr. David Alan Gilbert 69420daa90aSDr. David Alan Gilbert static bool migrate_is_not_pcie(void *opaque, int version_id) 69520daa90aSDr. David Alan Gilbert { 69620daa90aSDr. David Alan Gilbert return !pci_is_express((PCIDevice *)opaque); 69720daa90aSDr. David Alan Gilbert } 69820daa90aSDr. David Alan Gilbert 699315a1350SMichael S. Tsirkin const VMStateDescription vmstate_pci_device = { 700315a1350SMichael S. Tsirkin .name = "PCIDevice", 701315a1350SMichael S. Tsirkin .version_id = 2, 702315a1350SMichael S. Tsirkin .minimum_version_id = 1, 703315a1350SMichael S. Tsirkin .fields = (VMStateField[]) { 7043476436aSMichael S. Tsirkin VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice), 70520daa90aSDr. David Alan Gilbert VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice, 70620daa90aSDr. David Alan Gilbert migrate_is_not_pcie, 70720daa90aSDr. David Alan Gilbert 0, vmstate_info_pci_config, 708315a1350SMichael S. Tsirkin PCI_CONFIG_SPACE_SIZE), 70920daa90aSDr. David Alan Gilbert VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice, 71020daa90aSDr. David Alan Gilbert migrate_is_pcie, 71120daa90aSDr. David Alan Gilbert 0, vmstate_info_pci_config, 712315a1350SMichael S. Tsirkin PCIE_CONFIG_SPACE_SIZE), 713315a1350SMichael S. Tsirkin VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2, 714315a1350SMichael S. Tsirkin vmstate_info_pci_irq_state, 715315a1350SMichael S. Tsirkin PCI_NUM_PINS * sizeof(int32_t)), 716315a1350SMichael S. Tsirkin VMSTATE_END_OF_LIST() 717315a1350SMichael S. Tsirkin } 718315a1350SMichael S. Tsirkin }; 719315a1350SMichael S. Tsirkin 720315a1350SMichael S. Tsirkin 721315a1350SMichael S. Tsirkin void pci_device_save(PCIDevice *s, QEMUFile *f) 722315a1350SMichael S. Tsirkin { 723315a1350SMichael S. Tsirkin /* Clear interrupt status bit: it is implicit 724315a1350SMichael S. Tsirkin * in irq_state which we are saving. 725315a1350SMichael S. Tsirkin * This makes us compatible with old devices 726315a1350SMichael S. Tsirkin * which never set or clear this bit. */ 727315a1350SMichael S. Tsirkin s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT; 72820daa90aSDr. David Alan Gilbert vmstate_save_state(f, &vmstate_pci_device, s, NULL); 729315a1350SMichael S. Tsirkin /* Restore the interrupt status bit. */ 730315a1350SMichael S. Tsirkin pci_update_irq_status(s); 731315a1350SMichael S. Tsirkin } 732315a1350SMichael S. Tsirkin 733315a1350SMichael S. Tsirkin int pci_device_load(PCIDevice *s, QEMUFile *f) 734315a1350SMichael S. Tsirkin { 735315a1350SMichael S. Tsirkin int ret; 73620daa90aSDr. David Alan Gilbert ret = vmstate_load_state(f, &vmstate_pci_device, s, s->version_id); 737315a1350SMichael S. Tsirkin /* Restore the interrupt status bit. */ 738315a1350SMichael S. Tsirkin pci_update_irq_status(s); 739315a1350SMichael S. Tsirkin return ret; 740315a1350SMichael S. Tsirkin } 741315a1350SMichael S. Tsirkin 742315a1350SMichael S. Tsirkin static void pci_set_default_subsystem_id(PCIDevice *pci_dev) 743315a1350SMichael S. Tsirkin { 744315a1350SMichael S. Tsirkin pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, 745315a1350SMichael S. Tsirkin pci_default_sub_vendor_id); 746315a1350SMichael S. Tsirkin pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, 747315a1350SMichael S. Tsirkin pci_default_sub_device_id); 748315a1350SMichael S. Tsirkin } 749315a1350SMichael S. Tsirkin 750315a1350SMichael S. Tsirkin /* 751315a1350SMichael S. Tsirkin * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL 752315a1350SMichael S. Tsirkin * [[<domain>:]<bus>:]<slot>.<func>, return -1 on error 753315a1350SMichael S. Tsirkin */ 7546dbcb819SMarkus Armbruster static int pci_parse_devaddr(const char *addr, int *domp, int *busp, 755315a1350SMichael S. Tsirkin unsigned int *slotp, unsigned int *funcp) 756315a1350SMichael S. Tsirkin { 757315a1350SMichael S. Tsirkin const char *p; 758315a1350SMichael S. Tsirkin char *e; 759315a1350SMichael S. Tsirkin unsigned long val; 760315a1350SMichael S. Tsirkin unsigned long dom = 0, bus = 0; 761315a1350SMichael S. Tsirkin unsigned int slot = 0; 762315a1350SMichael S. Tsirkin unsigned int func = 0; 763315a1350SMichael S. Tsirkin 764315a1350SMichael S. Tsirkin p = addr; 765315a1350SMichael S. Tsirkin val = strtoul(p, &e, 16); 766315a1350SMichael S. Tsirkin if (e == p) 767315a1350SMichael S. Tsirkin return -1; 768315a1350SMichael S. Tsirkin if (*e == ':') { 769315a1350SMichael S. Tsirkin bus = val; 770315a1350SMichael S. Tsirkin p = e + 1; 771315a1350SMichael S. Tsirkin val = strtoul(p, &e, 16); 772315a1350SMichael S. Tsirkin if (e == p) 773315a1350SMichael S. Tsirkin return -1; 774315a1350SMichael S. Tsirkin if (*e == ':') { 775315a1350SMichael S. Tsirkin dom = bus; 776315a1350SMichael S. Tsirkin bus = val; 777315a1350SMichael S. Tsirkin p = e + 1; 778315a1350SMichael S. Tsirkin val = strtoul(p, &e, 16); 779315a1350SMichael S. Tsirkin if (e == p) 780315a1350SMichael S. Tsirkin return -1; 781315a1350SMichael S. Tsirkin } 782315a1350SMichael S. Tsirkin } 783315a1350SMichael S. Tsirkin 784315a1350SMichael S. Tsirkin slot = val; 785315a1350SMichael S. Tsirkin 786315a1350SMichael S. Tsirkin if (funcp != NULL) { 787315a1350SMichael S. Tsirkin if (*e != '.') 788315a1350SMichael S. Tsirkin return -1; 789315a1350SMichael S. Tsirkin 790315a1350SMichael S. Tsirkin p = e + 1; 791315a1350SMichael S. Tsirkin val = strtoul(p, &e, 16); 792315a1350SMichael S. Tsirkin if (e == p) 793315a1350SMichael S. Tsirkin return -1; 794315a1350SMichael S. Tsirkin 795315a1350SMichael S. Tsirkin func = val; 796315a1350SMichael S. Tsirkin } 797315a1350SMichael S. Tsirkin 798315a1350SMichael S. Tsirkin /* if funcp == NULL func is 0 */ 799315a1350SMichael S. Tsirkin if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7) 800315a1350SMichael S. Tsirkin return -1; 801315a1350SMichael S. Tsirkin 802315a1350SMichael S. Tsirkin if (*e) 803315a1350SMichael S. Tsirkin return -1; 804315a1350SMichael S. Tsirkin 805315a1350SMichael S. Tsirkin *domp = dom; 806315a1350SMichael S. Tsirkin *busp = bus; 807315a1350SMichael S. Tsirkin *slotp = slot; 808315a1350SMichael S. Tsirkin if (funcp != NULL) 809315a1350SMichael S. Tsirkin *funcp = func; 810315a1350SMichael S. Tsirkin return 0; 811315a1350SMichael S. Tsirkin } 812315a1350SMichael S. Tsirkin 813315a1350SMichael S. Tsirkin static void pci_init_cmask(PCIDevice *dev) 814315a1350SMichael S. Tsirkin { 815315a1350SMichael S. Tsirkin pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff); 816315a1350SMichael S. Tsirkin pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff); 817315a1350SMichael S. Tsirkin dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST; 818315a1350SMichael S. Tsirkin dev->cmask[PCI_REVISION_ID] = 0xff; 819315a1350SMichael S. Tsirkin dev->cmask[PCI_CLASS_PROG] = 0xff; 820315a1350SMichael S. Tsirkin pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff); 821315a1350SMichael S. Tsirkin dev->cmask[PCI_HEADER_TYPE] = 0xff; 822315a1350SMichael S. Tsirkin dev->cmask[PCI_CAPABILITY_LIST] = 0xff; 823315a1350SMichael S. Tsirkin } 824315a1350SMichael S. Tsirkin 825315a1350SMichael S. Tsirkin static void pci_init_wmask(PCIDevice *dev) 826315a1350SMichael S. Tsirkin { 827315a1350SMichael S. Tsirkin int config_size = pci_config_size(dev); 828315a1350SMichael S. Tsirkin 829315a1350SMichael S. Tsirkin dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff; 830315a1350SMichael S. Tsirkin dev->wmask[PCI_INTERRUPT_LINE] = 0xff; 831315a1350SMichael S. Tsirkin pci_set_word(dev->wmask + PCI_COMMAND, 832315a1350SMichael S. Tsirkin PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER | 833315a1350SMichael S. Tsirkin PCI_COMMAND_INTX_DISABLE); 834315a1350SMichael S. Tsirkin pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR); 835315a1350SMichael S. Tsirkin 836315a1350SMichael S. Tsirkin memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff, 837315a1350SMichael S. Tsirkin config_size - PCI_CONFIG_HEADER_SIZE); 838315a1350SMichael S. Tsirkin } 839315a1350SMichael S. Tsirkin 840315a1350SMichael S. Tsirkin static void pci_init_w1cmask(PCIDevice *dev) 841315a1350SMichael S. Tsirkin { 842315a1350SMichael S. Tsirkin /* 843315a1350SMichael S. Tsirkin * Note: It's okay to set w1cmask even for readonly bits as 844315a1350SMichael S. Tsirkin * long as their value is hardwired to 0. 845315a1350SMichael S. Tsirkin */ 846315a1350SMichael S. Tsirkin pci_set_word(dev->w1cmask + PCI_STATUS, 847315a1350SMichael S. Tsirkin PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT | 848315a1350SMichael S. Tsirkin PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT | 849315a1350SMichael S. Tsirkin PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY); 850315a1350SMichael S. Tsirkin } 851315a1350SMichael S. Tsirkin 852315a1350SMichael S. Tsirkin static void pci_init_mask_bridge(PCIDevice *d) 853315a1350SMichael S. Tsirkin { 854315a1350SMichael S. Tsirkin /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and 855315a1350SMichael S. Tsirkin PCI_SEC_LETENCY_TIMER */ 856315a1350SMichael S. Tsirkin memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4); 857315a1350SMichael S. Tsirkin 858315a1350SMichael S. Tsirkin /* base and limit */ 859315a1350SMichael S. Tsirkin d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff; 860315a1350SMichael S. Tsirkin d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff; 861315a1350SMichael S. Tsirkin pci_set_word(d->wmask + PCI_MEMORY_BASE, 862315a1350SMichael S. Tsirkin PCI_MEMORY_RANGE_MASK & 0xffff); 863315a1350SMichael S. Tsirkin pci_set_word(d->wmask + PCI_MEMORY_LIMIT, 864315a1350SMichael S. Tsirkin PCI_MEMORY_RANGE_MASK & 0xffff); 865315a1350SMichael S. Tsirkin pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE, 866315a1350SMichael S. Tsirkin PCI_PREF_RANGE_MASK & 0xffff); 867315a1350SMichael S. Tsirkin pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT, 868315a1350SMichael S. Tsirkin PCI_PREF_RANGE_MASK & 0xffff); 869315a1350SMichael S. Tsirkin 870315a1350SMichael S. Tsirkin /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */ 871315a1350SMichael S. Tsirkin memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8); 872315a1350SMichael S. Tsirkin 873315a1350SMichael S. Tsirkin /* Supported memory and i/o types */ 874315a1350SMichael S. Tsirkin d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16; 875315a1350SMichael S. Tsirkin d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16; 876315a1350SMichael S. Tsirkin pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE, 877315a1350SMichael S. Tsirkin PCI_PREF_RANGE_TYPE_64); 878315a1350SMichael S. Tsirkin pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT, 879315a1350SMichael S. Tsirkin PCI_PREF_RANGE_TYPE_64); 880315a1350SMichael S. Tsirkin 881ba7d8515SAlex Williamson /* 882ba7d8515SAlex Williamson * TODO: Bridges default to 10-bit VGA decoding but we currently only 883ba7d8515SAlex Williamson * implement 16-bit decoding (no alias support). 884ba7d8515SAlex Williamson */ 885315a1350SMichael S. Tsirkin pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 886315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_PARITY | 887315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_SERR | 888315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_ISA | 889315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_VGA | 890315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_VGA_16BIT | 891315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_MASTER_ABORT | 892315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_BUS_RESET | 893315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_FAST_BACK | 894315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_DISCARD | 895315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_SEC_DISCARD | 896315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_DISCARD_SERR); 897315a1350SMichael S. Tsirkin /* Below does not do anything as we never set this bit, put here for 898315a1350SMichael S. Tsirkin * completeness. */ 899315a1350SMichael S. Tsirkin pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL, 900315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_DISCARD_STATUS); 901315a1350SMichael S. Tsirkin d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK; 902315a1350SMichael S. Tsirkin d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK; 903315a1350SMichael S. Tsirkin pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE, 904315a1350SMichael S. Tsirkin PCI_PREF_RANGE_TYPE_MASK); 905315a1350SMichael S. Tsirkin pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT, 906315a1350SMichael S. Tsirkin PCI_PREF_RANGE_TYPE_MASK); 907315a1350SMichael S. Tsirkin } 908315a1350SMichael S. Tsirkin 909133e9b22SMarkus Armbruster static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp) 910315a1350SMichael S. Tsirkin { 911315a1350SMichael S. Tsirkin uint8_t slot = PCI_SLOT(dev->devfn); 912315a1350SMichael S. Tsirkin uint8_t func; 913315a1350SMichael S. Tsirkin 914315a1350SMichael S. Tsirkin if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) { 915315a1350SMichael S. Tsirkin dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION; 916315a1350SMichael S. Tsirkin } 917315a1350SMichael S. Tsirkin 918315a1350SMichael S. Tsirkin /* 9197c0fa8dfSKnut Omang * With SR/IOV and ARI, a device at function 0 need not be a multifunction 9207c0fa8dfSKnut Omang * device, as it may just be a VF that ended up with function 0 in 9217c0fa8dfSKnut Omang * the legacy PCI interpretation. Avoid failing in such cases: 9227c0fa8dfSKnut Omang */ 9237c0fa8dfSKnut Omang if (pci_is_vf(dev) && 9247c0fa8dfSKnut Omang dev->exp.sriov_vf.pf->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) { 9257c0fa8dfSKnut Omang return; 9267c0fa8dfSKnut Omang } 9277c0fa8dfSKnut Omang 9287c0fa8dfSKnut Omang /* 929315a1350SMichael S. Tsirkin * multifunction bit is interpreted in two ways as follows. 930315a1350SMichael S. Tsirkin * - all functions must set the bit to 1. 931315a1350SMichael S. Tsirkin * Example: Intel X53 932315a1350SMichael S. Tsirkin * - function 0 must set the bit, but the rest function (> 0) 933315a1350SMichael S. Tsirkin * is allowed to leave the bit to 0. 934315a1350SMichael S. Tsirkin * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10, 935315a1350SMichael S. Tsirkin * 936315a1350SMichael S. Tsirkin * So OS (at least Linux) checks the bit of only function 0, 937315a1350SMichael S. Tsirkin * and doesn't see the bit of function > 0. 938315a1350SMichael S. Tsirkin * 939315a1350SMichael S. Tsirkin * The below check allows both interpretation. 940315a1350SMichael S. Tsirkin */ 941315a1350SMichael S. Tsirkin if (PCI_FUNC(dev->devfn)) { 942315a1350SMichael S. Tsirkin PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)]; 943315a1350SMichael S. Tsirkin if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) { 944315a1350SMichael S. Tsirkin /* function 0 should set multifunction bit */ 945133e9b22SMarkus Armbruster error_setg(errp, "PCI: single function device can't be populated " 946315a1350SMichael S. Tsirkin "in function %x.%x", slot, PCI_FUNC(dev->devfn)); 947133e9b22SMarkus Armbruster return; 948315a1350SMichael S. Tsirkin } 949133e9b22SMarkus Armbruster return; 950315a1350SMichael S. Tsirkin } 951315a1350SMichael S. Tsirkin 952315a1350SMichael S. Tsirkin if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) { 953133e9b22SMarkus Armbruster return; 954315a1350SMichael S. Tsirkin } 955315a1350SMichael S. Tsirkin /* function 0 indicates single function, so function > 0 must be NULL */ 956315a1350SMichael S. Tsirkin for (func = 1; func < PCI_FUNC_MAX; ++func) { 957315a1350SMichael S. Tsirkin if (bus->devices[PCI_DEVFN(slot, func)]) { 958133e9b22SMarkus Armbruster error_setg(errp, "PCI: %x.0 indicates single function, " 959315a1350SMichael S. Tsirkin "but %x.%x is already populated.", 960315a1350SMichael S. Tsirkin slot, slot, func); 961133e9b22SMarkus Armbruster return; 962315a1350SMichael S. Tsirkin } 963315a1350SMichael S. Tsirkin } 964315a1350SMichael S. Tsirkin } 965315a1350SMichael S. Tsirkin 966315a1350SMichael S. Tsirkin static void pci_config_alloc(PCIDevice *pci_dev) 967315a1350SMichael S. Tsirkin { 968315a1350SMichael S. Tsirkin int config_size = pci_config_size(pci_dev); 969315a1350SMichael S. Tsirkin 970315a1350SMichael S. Tsirkin pci_dev->config = g_malloc0(config_size); 971315a1350SMichael S. Tsirkin pci_dev->cmask = g_malloc0(config_size); 972315a1350SMichael S. Tsirkin pci_dev->wmask = g_malloc0(config_size); 973315a1350SMichael S. Tsirkin pci_dev->w1cmask = g_malloc0(config_size); 974315a1350SMichael S. Tsirkin pci_dev->used = g_malloc0(config_size); 975315a1350SMichael S. Tsirkin } 976315a1350SMichael S. Tsirkin 977315a1350SMichael S. Tsirkin static void pci_config_free(PCIDevice *pci_dev) 978315a1350SMichael S. Tsirkin { 979315a1350SMichael S. Tsirkin g_free(pci_dev->config); 980315a1350SMichael S. Tsirkin g_free(pci_dev->cmask); 981315a1350SMichael S. Tsirkin g_free(pci_dev->wmask); 982315a1350SMichael S. Tsirkin g_free(pci_dev->w1cmask); 983315a1350SMichael S. Tsirkin g_free(pci_dev->used); 984315a1350SMichael S. Tsirkin } 985315a1350SMichael S. Tsirkin 98630607764SMarcel Apfelbaum static void do_pci_unregister_device(PCIDevice *pci_dev) 98730607764SMarcel Apfelbaum { 988fd56e061SDavid Gibson pci_get_bus(pci_dev)->devices[pci_dev->devfn] = NULL; 98930607764SMarcel Apfelbaum pci_config_free(pci_dev); 99030607764SMarcel Apfelbaum 991193982c6SAlexey Kardashevskiy if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) { 992c53598edSAlexey Kardashevskiy memory_region_del_subregion(&pci_dev->bus_master_container_region, 993c53598edSAlexey Kardashevskiy &pci_dev->bus_master_enable_region); 994193982c6SAlexey Kardashevskiy } 99530607764SMarcel Apfelbaum address_space_destroy(&pci_dev->bus_master_as); 99630607764SMarcel Apfelbaum } 99730607764SMarcel Apfelbaum 9984a94b3aaSPeter Xu /* Extract PCIReqIDCache into BDF format */ 9994a94b3aaSPeter Xu static uint16_t pci_req_id_cache_extract(PCIReqIDCache *cache) 10004a94b3aaSPeter Xu { 10014a94b3aaSPeter Xu uint8_t bus_n; 10024a94b3aaSPeter Xu uint16_t result; 10034a94b3aaSPeter Xu 10044a94b3aaSPeter Xu switch (cache->type) { 10054a94b3aaSPeter Xu case PCI_REQ_ID_BDF: 10064a94b3aaSPeter Xu result = pci_get_bdf(cache->dev); 10074a94b3aaSPeter Xu break; 10084a94b3aaSPeter Xu case PCI_REQ_ID_SECONDARY_BUS: 1009fd56e061SDavid Gibson bus_n = pci_dev_bus_num(cache->dev); 10104a94b3aaSPeter Xu result = PCI_BUILD_BDF(bus_n, 0); 10114a94b3aaSPeter Xu break; 10124a94b3aaSPeter Xu default: 1013eaf27fabSMarkus Armbruster error_report("Invalid PCI requester ID cache type: %d", 10144a94b3aaSPeter Xu cache->type); 10154a94b3aaSPeter Xu exit(1); 10164a94b3aaSPeter Xu break; 10174a94b3aaSPeter Xu } 10184a94b3aaSPeter Xu 10194a94b3aaSPeter Xu return result; 10204a94b3aaSPeter Xu } 10214a94b3aaSPeter Xu 10224a94b3aaSPeter Xu /* Parse bridges up to the root complex and return requester ID 10234a94b3aaSPeter Xu * cache for specific device. For full PCIe topology, the cache 10244a94b3aaSPeter Xu * result would be exactly the same as getting BDF of the device. 10254a94b3aaSPeter Xu * However, several tricks are required when system mixed up with 10264a94b3aaSPeter Xu * legacy PCI devices and PCIe-to-PCI bridges. 10274a94b3aaSPeter Xu * 10284a94b3aaSPeter Xu * Here we cache the proxy device (and type) not requester ID since 10294a94b3aaSPeter Xu * bus number might change from time to time. 10304a94b3aaSPeter Xu */ 10314a94b3aaSPeter Xu static PCIReqIDCache pci_req_id_cache_get(PCIDevice *dev) 10324a94b3aaSPeter Xu { 10334a94b3aaSPeter Xu PCIDevice *parent; 10344a94b3aaSPeter Xu PCIReqIDCache cache = { 10354a94b3aaSPeter Xu .dev = dev, 10364a94b3aaSPeter Xu .type = PCI_REQ_ID_BDF, 10374a94b3aaSPeter Xu }; 10384a94b3aaSPeter Xu 1039fd56e061SDavid Gibson while (!pci_bus_is_root(pci_get_bus(dev))) { 10404a94b3aaSPeter Xu /* We are under PCI/PCIe bridges */ 1041fd56e061SDavid Gibson parent = pci_get_bus(dev)->parent_dev; 10424a94b3aaSPeter Xu if (pci_is_express(parent)) { 10434a94b3aaSPeter Xu if (pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) { 10444a94b3aaSPeter Xu /* When we pass through PCIe-to-PCI/PCIX bridges, we 10454a94b3aaSPeter Xu * override the requester ID using secondary bus 10464a94b3aaSPeter Xu * number of parent bridge with zeroed devfn 10474a94b3aaSPeter Xu * (pcie-to-pci bridge spec chap 2.3). */ 10484a94b3aaSPeter Xu cache.type = PCI_REQ_ID_SECONDARY_BUS; 10494a94b3aaSPeter Xu cache.dev = dev; 10504a94b3aaSPeter Xu } 10514a94b3aaSPeter Xu } else { 10524a94b3aaSPeter Xu /* Legacy PCI, override requester ID with the bridge's 10534a94b3aaSPeter Xu * BDF upstream. When the root complex connects to 10544a94b3aaSPeter Xu * legacy PCI devices (including buses), it can only 10554a94b3aaSPeter Xu * obtain requester ID info from directly attached 10564a94b3aaSPeter Xu * devices. If devices are attached under bridges, only 10574a94b3aaSPeter Xu * the requester ID of the bridge that is directly 10584a94b3aaSPeter Xu * attached to the root complex can be recognized. */ 10594a94b3aaSPeter Xu cache.type = PCI_REQ_ID_BDF; 10604a94b3aaSPeter Xu cache.dev = parent; 10614a94b3aaSPeter Xu } 10624a94b3aaSPeter Xu dev = parent; 10634a94b3aaSPeter Xu } 10644a94b3aaSPeter Xu 10654a94b3aaSPeter Xu return cache; 10664a94b3aaSPeter Xu } 10674a94b3aaSPeter Xu 10684a94b3aaSPeter Xu uint16_t pci_requester_id(PCIDevice *dev) 10694a94b3aaSPeter Xu { 10704a94b3aaSPeter Xu return pci_req_id_cache_extract(&dev->requester_id_cache); 10714a94b3aaSPeter Xu } 10724a94b3aaSPeter Xu 10739b717a3aSMark Cave-Ayland static bool pci_bus_devfn_available(PCIBus *bus, int devfn) 10749b717a3aSMark Cave-Ayland { 10759b717a3aSMark Cave-Ayland return !(bus->devices[devfn]); 10769b717a3aSMark Cave-Ayland } 10779b717a3aSMark Cave-Ayland 10788b884984SMark Cave-Ayland static bool pci_bus_devfn_reserved(PCIBus *bus, int devfn) 10798b884984SMark Cave-Ayland { 10808b884984SMark Cave-Ayland return bus->slot_reserved_mask & (1UL << PCI_SLOT(devfn)); 10818b884984SMark Cave-Ayland } 10828b884984SMark Cave-Ayland 1083315a1350SMichael S. Tsirkin /* -1 for devfn means auto assign */ 1084fd56e061SDavid Gibson static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, 1085133e9b22SMarkus Armbruster const char *name, int devfn, 1086133e9b22SMarkus Armbruster Error **errp) 1087315a1350SMichael S. Tsirkin { 1088315a1350SMichael S. Tsirkin PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); 1089315a1350SMichael S. Tsirkin PCIConfigReadFunc *config_read = pc->config_read; 1090315a1350SMichael S. Tsirkin PCIConfigWriteFunc *config_write = pc->config_write; 1091133e9b22SMarkus Armbruster Error *local_err = NULL; 10923f1e1478SCao jin DeviceState *dev = DEVICE(pci_dev); 1093fd56e061SDavid Gibson PCIBus *bus = pci_get_bus(pci_dev); 1094ad494274SIgor Mammedov bool is_bridge = IS_PCI_BRIDGE(pci_dev); 10953f1e1478SCao jin 10960144f6f1SMarcel Apfelbaum /* Only pci bridges can be attached to extra PCI root buses */ 1097ad494274SIgor Mammedov if (pci_bus_is_root(bus) && bus->parent_dev && !is_bridge) { 10980144f6f1SMarcel Apfelbaum error_setg(errp, 10990144f6f1SMarcel Apfelbaum "PCI: Only PCI/PCIe bridges can be plugged into %s", 11000144f6f1SMarcel Apfelbaum bus->parent_dev->name); 11010144f6f1SMarcel Apfelbaum return NULL; 11020144f6f1SMarcel Apfelbaum } 1103315a1350SMichael S. Tsirkin 1104315a1350SMichael S. Tsirkin if (devfn < 0) { 1105315a1350SMichael S. Tsirkin for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices); 1106315a1350SMichael S. Tsirkin devfn += PCI_FUNC_MAX) { 11078b884984SMark Cave-Ayland if (pci_bus_devfn_available(bus, devfn) && 11088b884984SMark Cave-Ayland !pci_bus_devfn_reserved(bus, devfn)) { 1109315a1350SMichael S. Tsirkin goto found; 1110315a1350SMichael S. Tsirkin } 11119b717a3aSMark Cave-Ayland } 11128b884984SMark Cave-Ayland error_setg(errp, "PCI: no slot/function available for %s, all in use " 11138b884984SMark Cave-Ayland "or reserved", name); 1114315a1350SMichael S. Tsirkin return NULL; 1115315a1350SMichael S. Tsirkin found: ; 11168b884984SMark Cave-Ayland } else if (pci_bus_devfn_reserved(bus, devfn)) { 11178b884984SMark Cave-Ayland error_setg(errp, "PCI: slot %d function %d not available for %s," 11188b884984SMark Cave-Ayland " reserved", 11198b884984SMark Cave-Ayland PCI_SLOT(devfn), PCI_FUNC(devfn), name); 11208b884984SMark Cave-Ayland return NULL; 11219b717a3aSMark Cave-Ayland } else if (!pci_bus_devfn_available(bus, devfn)) { 1122133e9b22SMarkus Armbruster error_setg(errp, "PCI: slot %d function %d not available for %s," 1123ad003b9eSZhenzhong Duan " in use by %s,id=%s", 1124133e9b22SMarkus Armbruster PCI_SLOT(devfn), PCI_FUNC(devfn), name, 1125ad003b9eSZhenzhong Duan bus->devices[devfn]->name, bus->devices[devfn]->qdev.id); 1126315a1350SMichael S. Tsirkin return NULL; 11273f1e1478SCao jin } else if (dev->hotplugged && 11287c0fa8dfSKnut Omang !pci_is_vf(pci_dev) && 11293f1e1478SCao jin pci_get_function_0(pci_dev)) { 11303298bbceSJulia Suvorova error_setg(errp, "PCI: slot %d function 0 already occupied by %s," 11313f1e1478SCao jin " new func %s cannot be exposed to guest.", 1132d93ddfb1SMichael S. Tsirkin PCI_SLOT(pci_get_function_0(pci_dev)->devfn), 1133d93ddfb1SMichael S. Tsirkin pci_get_function_0(pci_dev)->name, 11343f1e1478SCao jin name); 11353f1e1478SCao jin 11363f1e1478SCao jin return NULL; 1137315a1350SMichael S. Tsirkin } 1138e00387d5SAvi Kivity 1139efc8188eSLe Tan pci_dev->devfn = devfn; 11404a94b3aaSPeter Xu pci_dev->requester_id_cache = pci_req_id_cache_get(pci_dev); 1141d06bce95SAlexey Kardashevskiy pstrcpy(pci_dev->name, sizeof(pci_dev->name), name); 1142e00387d5SAvi Kivity 11433716d590SJason Wang memory_region_init(&pci_dev->bus_master_container_region, OBJECT(pci_dev), 11443716d590SJason Wang "bus master container", UINT64_MAX); 11453716d590SJason Wang address_space_init(&pci_dev->bus_master_as, 11463716d590SJason Wang &pci_dev->bus_master_container_region, pci_dev->name); 11473716d590SJason Wang 11482f181fbdSPaolo Bonzini if (phase_check(PHASE_MACHINE_READY)) { 1149b86eacb8SMarcel Apfelbaum pci_init_bus_master(pci_dev); 1150b86eacb8SMarcel Apfelbaum } 1151315a1350SMichael S. Tsirkin pci_dev->irq_state = 0; 1152315a1350SMichael S. Tsirkin pci_config_alloc(pci_dev); 1153315a1350SMichael S. Tsirkin 1154315a1350SMichael S. Tsirkin pci_config_set_vendor_id(pci_dev->config, pc->vendor_id); 1155315a1350SMichael S. Tsirkin pci_config_set_device_id(pci_dev->config, pc->device_id); 1156315a1350SMichael S. Tsirkin pci_config_set_revision(pci_dev->config, pc->revision); 1157315a1350SMichael S. Tsirkin pci_config_set_class(pci_dev->config, pc->class_id); 1158315a1350SMichael S. Tsirkin 1159ad494274SIgor Mammedov if (!is_bridge) { 1160315a1350SMichael S. Tsirkin if (pc->subsystem_vendor_id || pc->subsystem_id) { 1161315a1350SMichael S. Tsirkin pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID, 1162315a1350SMichael S. Tsirkin pc->subsystem_vendor_id); 1163315a1350SMichael S. Tsirkin pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID, 1164315a1350SMichael S. Tsirkin pc->subsystem_id); 1165315a1350SMichael S. Tsirkin } else { 1166315a1350SMichael S. Tsirkin pci_set_default_subsystem_id(pci_dev); 1167315a1350SMichael S. Tsirkin } 1168315a1350SMichael S. Tsirkin } else { 1169315a1350SMichael S. Tsirkin /* subsystem_vendor_id/subsystem_id are only for header type 0 */ 1170315a1350SMichael S. Tsirkin assert(!pc->subsystem_vendor_id); 1171315a1350SMichael S. Tsirkin assert(!pc->subsystem_id); 1172315a1350SMichael S. Tsirkin } 1173315a1350SMichael S. Tsirkin pci_init_cmask(pci_dev); 1174315a1350SMichael S. Tsirkin pci_init_wmask(pci_dev); 1175315a1350SMichael S. Tsirkin pci_init_w1cmask(pci_dev); 1176ad494274SIgor Mammedov if (is_bridge) { 1177315a1350SMichael S. Tsirkin pci_init_mask_bridge(pci_dev); 1178315a1350SMichael S. Tsirkin } 1179133e9b22SMarkus Armbruster pci_init_multifunction(bus, pci_dev, &local_err); 1180133e9b22SMarkus Armbruster if (local_err) { 1181133e9b22SMarkus Armbruster error_propagate(errp, local_err); 118230607764SMarcel Apfelbaum do_pci_unregister_device(pci_dev); 1183315a1350SMichael S. Tsirkin return NULL; 1184315a1350SMichael S. Tsirkin } 1185315a1350SMichael S. Tsirkin 1186315a1350SMichael S. Tsirkin if (!config_read) 1187315a1350SMichael S. Tsirkin config_read = pci_default_read_config; 1188315a1350SMichael S. Tsirkin if (!config_write) 1189315a1350SMichael S. Tsirkin config_write = pci_default_write_config; 1190315a1350SMichael S. Tsirkin pci_dev->config_read = config_read; 1191315a1350SMichael S. Tsirkin pci_dev->config_write = config_write; 1192315a1350SMichael S. Tsirkin bus->devices[devfn] = pci_dev; 1193315a1350SMichael S. Tsirkin pci_dev->version_id = 2; /* Current pci device vmstate version */ 1194315a1350SMichael S. Tsirkin return pci_dev; 1195315a1350SMichael S. Tsirkin } 1196315a1350SMichael S. Tsirkin 1197315a1350SMichael S. Tsirkin static void pci_unregister_io_regions(PCIDevice *pci_dev) 1198315a1350SMichael S. Tsirkin { 1199315a1350SMichael S. Tsirkin PCIIORegion *r; 1200315a1350SMichael S. Tsirkin int i; 1201315a1350SMichael S. Tsirkin 1202315a1350SMichael S. Tsirkin for(i = 0; i < PCI_NUM_REGIONS; i++) { 1203315a1350SMichael S. Tsirkin r = &pci_dev->io_regions[i]; 1204315a1350SMichael S. Tsirkin if (!r->size || r->addr == PCI_BAR_UNMAPPED) 1205315a1350SMichael S. Tsirkin continue; 1206315a1350SMichael S. Tsirkin memory_region_del_subregion(r->address_space, r->memory); 1207315a1350SMichael S. Tsirkin } 1208e01fd687SAlex Williamson 1209e01fd687SAlex Williamson pci_unregister_vga(pci_dev); 1210315a1350SMichael S. Tsirkin } 1211315a1350SMichael S. Tsirkin 1212b69c3c21SMarkus Armbruster static void pci_qdev_unrealize(DeviceState *dev) 1213315a1350SMichael S. Tsirkin { 1214315a1350SMichael S. Tsirkin PCIDevice *pci_dev = PCI_DEVICE(dev); 1215315a1350SMichael S. Tsirkin PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); 1216315a1350SMichael S. Tsirkin 1217315a1350SMichael S. Tsirkin pci_unregister_io_regions(pci_dev); 1218315a1350SMichael S. Tsirkin pci_del_option_rom(pci_dev); 1219315a1350SMichael S. Tsirkin 1220315a1350SMichael S. Tsirkin if (pc->exit) { 1221315a1350SMichael S. Tsirkin pc->exit(pci_dev); 1222315a1350SMichael S. Tsirkin } 1223315a1350SMichael S. Tsirkin 12243936161fSHerongguang (Stephen) pci_device_deassert_intx(pci_dev); 1225315a1350SMichael S. Tsirkin do_pci_unregister_device(pci_dev); 122608cf3dc6SJagannathan Raman 122708cf3dc6SJagannathan Raman pci_dev->msi_trigger = NULL; 1228315a1350SMichael S. Tsirkin } 1229315a1350SMichael S. Tsirkin 1230315a1350SMichael S. Tsirkin void pci_register_bar(PCIDevice *pci_dev, int region_num, 1231315a1350SMichael S. Tsirkin uint8_t type, MemoryRegion *memory) 1232315a1350SMichael S. Tsirkin { 1233315a1350SMichael S. Tsirkin PCIIORegion *r; 12345178ecd8SCao jin uint32_t addr; /* offset in pci config space */ 1235315a1350SMichael S. Tsirkin uint64_t wmask; 1236315a1350SMichael S. Tsirkin pcibus_t size = memory_region_size(memory); 12376a5b19caSBen Widawsky uint8_t hdr_type; 1238315a1350SMichael S. Tsirkin 12397c0fa8dfSKnut Omang assert(!pci_is_vf(pci_dev)); /* VFs must use pcie_sriov_vf_register_bar */ 1240315a1350SMichael S. Tsirkin assert(region_num >= 0); 1241315a1350SMichael S. Tsirkin assert(region_num < PCI_NUM_REGIONS); 12422c729dc8SBen Widawsky assert(is_power_of_2(size)); 1243315a1350SMichael S. Tsirkin 12446a5b19caSBen Widawsky /* A PCI bridge device (with Type 1 header) may only have at most 2 BARs */ 12456a5b19caSBen Widawsky hdr_type = 12466a5b19caSBen Widawsky pci_dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION; 12476a5b19caSBen Widawsky assert(hdr_type != PCI_HEADER_TYPE_BRIDGE || region_num < 2); 12486a5b19caSBen Widawsky 1249315a1350SMichael S. Tsirkin r = &pci_dev->io_regions[region_num]; 1250315a1350SMichael S. Tsirkin r->addr = PCI_BAR_UNMAPPED; 1251315a1350SMichael S. Tsirkin r->size = size; 1252315a1350SMichael S. Tsirkin r->type = type; 12535178ecd8SCao jin r->memory = memory; 12545178ecd8SCao jin r->address_space = type & PCI_BASE_ADDRESS_SPACE_IO 1255fd56e061SDavid Gibson ? pci_get_bus(pci_dev)->address_space_io 1256fd56e061SDavid Gibson : pci_get_bus(pci_dev)->address_space_mem; 1257315a1350SMichael S. Tsirkin 1258315a1350SMichael S. Tsirkin wmask = ~(size - 1); 1259315a1350SMichael S. Tsirkin if (region_num == PCI_ROM_SLOT) { 1260315a1350SMichael S. Tsirkin /* ROM enable bit is writable */ 1261315a1350SMichael S. Tsirkin wmask |= PCI_ROM_ADDRESS_ENABLE; 1262315a1350SMichael S. Tsirkin } 12635178ecd8SCao jin 12645178ecd8SCao jin addr = pci_bar(pci_dev, region_num); 1265315a1350SMichael S. Tsirkin pci_set_long(pci_dev->config + addr, type); 12665178ecd8SCao jin 1267315a1350SMichael S. Tsirkin if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) && 1268315a1350SMichael S. Tsirkin r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) { 1269315a1350SMichael S. Tsirkin pci_set_quad(pci_dev->wmask + addr, wmask); 1270315a1350SMichael S. Tsirkin pci_set_quad(pci_dev->cmask + addr, ~0ULL); 1271315a1350SMichael S. Tsirkin } else { 1272315a1350SMichael S. Tsirkin pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff); 1273315a1350SMichael S. Tsirkin pci_set_long(pci_dev->cmask + addr, 0xffffffff); 1274315a1350SMichael S. Tsirkin } 1275315a1350SMichael S. Tsirkin } 1276315a1350SMichael S. Tsirkin 1277e01fd687SAlex Williamson static void pci_update_vga(PCIDevice *pci_dev) 1278e01fd687SAlex Williamson { 1279e01fd687SAlex Williamson uint16_t cmd; 1280e01fd687SAlex Williamson 1281e01fd687SAlex Williamson if (!pci_dev->has_vga) { 1282e01fd687SAlex Williamson return; 1283e01fd687SAlex Williamson } 1284e01fd687SAlex Williamson 1285e01fd687SAlex Williamson cmd = pci_get_word(pci_dev->config + PCI_COMMAND); 1286e01fd687SAlex Williamson 1287e01fd687SAlex Williamson memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM], 1288e01fd687SAlex Williamson cmd & PCI_COMMAND_MEMORY); 1289e01fd687SAlex Williamson memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO], 1290e01fd687SAlex Williamson cmd & PCI_COMMAND_IO); 1291e01fd687SAlex Williamson memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI], 1292e01fd687SAlex Williamson cmd & PCI_COMMAND_IO); 1293e01fd687SAlex Williamson } 1294e01fd687SAlex Williamson 1295e01fd687SAlex Williamson void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem, 1296e01fd687SAlex Williamson MemoryRegion *io_lo, MemoryRegion *io_hi) 1297e01fd687SAlex Williamson { 1298fd56e061SDavid Gibson PCIBus *bus = pci_get_bus(pci_dev); 1299fd56e061SDavid Gibson 1300e01fd687SAlex Williamson assert(!pci_dev->has_vga); 1301e01fd687SAlex Williamson 1302e01fd687SAlex Williamson assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE); 1303e01fd687SAlex Williamson pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem; 1304fd56e061SDavid Gibson memory_region_add_subregion_overlap(bus->address_space_mem, 1305e01fd687SAlex Williamson QEMU_PCI_VGA_MEM_BASE, mem, 1); 1306e01fd687SAlex Williamson 1307e01fd687SAlex Williamson assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE); 1308e01fd687SAlex Williamson pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo; 1309fd56e061SDavid Gibson memory_region_add_subregion_overlap(bus->address_space_io, 1310e01fd687SAlex Williamson QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1); 1311e01fd687SAlex Williamson 1312e01fd687SAlex Williamson assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE); 1313e01fd687SAlex Williamson pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi; 1314fd56e061SDavid Gibson memory_region_add_subregion_overlap(bus->address_space_io, 1315e01fd687SAlex Williamson QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1); 1316e01fd687SAlex Williamson pci_dev->has_vga = true; 1317e01fd687SAlex Williamson 1318e01fd687SAlex Williamson pci_update_vga(pci_dev); 1319e01fd687SAlex Williamson } 1320e01fd687SAlex Williamson 1321e01fd687SAlex Williamson void pci_unregister_vga(PCIDevice *pci_dev) 1322e01fd687SAlex Williamson { 1323fd56e061SDavid Gibson PCIBus *bus = pci_get_bus(pci_dev); 1324fd56e061SDavid Gibson 1325e01fd687SAlex Williamson if (!pci_dev->has_vga) { 1326e01fd687SAlex Williamson return; 1327e01fd687SAlex Williamson } 1328e01fd687SAlex Williamson 1329fd56e061SDavid Gibson memory_region_del_subregion(bus->address_space_mem, 1330e01fd687SAlex Williamson pci_dev->vga_regions[QEMU_PCI_VGA_MEM]); 1331fd56e061SDavid Gibson memory_region_del_subregion(bus->address_space_io, 1332e01fd687SAlex Williamson pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]); 1333fd56e061SDavid Gibson memory_region_del_subregion(bus->address_space_io, 1334e01fd687SAlex Williamson pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]); 1335e01fd687SAlex Williamson pci_dev->has_vga = false; 1336e01fd687SAlex Williamson } 1337e01fd687SAlex Williamson 1338315a1350SMichael S. Tsirkin pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num) 1339315a1350SMichael S. Tsirkin { 1340315a1350SMichael S. Tsirkin return pci_dev->io_regions[region_num].addr; 1341315a1350SMichael S. Tsirkin } 1342315a1350SMichael S. Tsirkin 13437c0fa8dfSKnut Omang static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg, 13447c0fa8dfSKnut Omang uint8_t type, pcibus_t size) 13457c0fa8dfSKnut Omang { 13467c0fa8dfSKnut Omang pcibus_t new_addr; 13477c0fa8dfSKnut Omang if (!pci_is_vf(d)) { 13487c0fa8dfSKnut Omang int bar = pci_bar(d, reg); 13497c0fa8dfSKnut Omang if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) { 13507c0fa8dfSKnut Omang new_addr = pci_get_quad(d->config + bar); 13517c0fa8dfSKnut Omang } else { 13527c0fa8dfSKnut Omang new_addr = pci_get_long(d->config + bar); 13537c0fa8dfSKnut Omang } 13547c0fa8dfSKnut Omang } else { 13557c0fa8dfSKnut Omang PCIDevice *pf = d->exp.sriov_vf.pf; 13567c0fa8dfSKnut Omang uint16_t sriov_cap = pf->exp.sriov_cap; 13577c0fa8dfSKnut Omang int bar = sriov_cap + PCI_SRIOV_BAR + reg * 4; 13587c0fa8dfSKnut Omang uint16_t vf_offset = 13597c0fa8dfSKnut Omang pci_get_word(pf->config + sriov_cap + PCI_SRIOV_VF_OFFSET); 13607c0fa8dfSKnut Omang uint16_t vf_stride = 13617c0fa8dfSKnut Omang pci_get_word(pf->config + sriov_cap + PCI_SRIOV_VF_STRIDE); 13627c0fa8dfSKnut Omang uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride; 13637c0fa8dfSKnut Omang 13647c0fa8dfSKnut Omang if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) { 13657c0fa8dfSKnut Omang new_addr = pci_get_quad(pf->config + bar); 13667c0fa8dfSKnut Omang } else { 13677c0fa8dfSKnut Omang new_addr = pci_get_long(pf->config + bar); 13687c0fa8dfSKnut Omang } 13697c0fa8dfSKnut Omang new_addr += vf_num * size; 13707c0fa8dfSKnut Omang } 13717c0fa8dfSKnut Omang /* The ROM slot has a specific enable bit, keep it intact */ 13727c0fa8dfSKnut Omang if (reg != PCI_ROM_SLOT) { 13737c0fa8dfSKnut Omang new_addr &= ~(size - 1); 13747c0fa8dfSKnut Omang } 13757c0fa8dfSKnut Omang return new_addr; 13767c0fa8dfSKnut Omang } 13777c0fa8dfSKnut Omang 13787c0fa8dfSKnut Omang pcibus_t pci_bar_address(PCIDevice *d, 1379315a1350SMichael S. Tsirkin int reg, uint8_t type, pcibus_t size) 1380315a1350SMichael S. Tsirkin { 1381315a1350SMichael S. Tsirkin pcibus_t new_addr, last_addr; 1382315a1350SMichael S. Tsirkin uint16_t cmd = pci_get_word(d->config + PCI_COMMAND); 1383e4024630SLaurent Vivier Object *machine = qdev_get_machine(); 1384e4024630SLaurent Vivier ObjectClass *oc = object_get_class(machine); 1385e4024630SLaurent Vivier MachineClass *mc = MACHINE_CLASS(oc); 1386e4024630SLaurent Vivier bool allow_0_address = mc->pci_allow_0_address; 1387315a1350SMichael S. Tsirkin 1388315a1350SMichael S. Tsirkin if (type & PCI_BASE_ADDRESS_SPACE_IO) { 1389315a1350SMichael S. Tsirkin if (!(cmd & PCI_COMMAND_IO)) { 1390315a1350SMichael S. Tsirkin return PCI_BAR_UNMAPPED; 1391315a1350SMichael S. Tsirkin } 13927c0fa8dfSKnut Omang new_addr = pci_config_get_bar_addr(d, reg, type, size); 1393315a1350SMichael S. Tsirkin last_addr = new_addr + size - 1; 13949f1a029aSHervé Poussineau /* Check if 32 bit BAR wraps around explicitly. 13959f1a029aSHervé Poussineau * TODO: make priorities correct and remove this work around. 13969f1a029aSHervé Poussineau */ 1397e4024630SLaurent Vivier if (last_addr <= new_addr || last_addr >= UINT32_MAX || 1398e4024630SLaurent Vivier (!allow_0_address && new_addr == 0)) { 1399315a1350SMichael S. Tsirkin return PCI_BAR_UNMAPPED; 1400315a1350SMichael S. Tsirkin } 1401315a1350SMichael S. Tsirkin return new_addr; 1402315a1350SMichael S. Tsirkin } 1403315a1350SMichael S. Tsirkin 1404315a1350SMichael S. Tsirkin if (!(cmd & PCI_COMMAND_MEMORY)) { 1405315a1350SMichael S. Tsirkin return PCI_BAR_UNMAPPED; 1406315a1350SMichael S. Tsirkin } 14077c0fa8dfSKnut Omang new_addr = pci_config_get_bar_addr(d, reg, type, size); 1408315a1350SMichael S. Tsirkin /* the ROM slot has a specific enable bit */ 1409315a1350SMichael S. Tsirkin if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) { 1410315a1350SMichael S. Tsirkin return PCI_BAR_UNMAPPED; 1411315a1350SMichael S. Tsirkin } 1412315a1350SMichael S. Tsirkin new_addr &= ~(size - 1); 1413315a1350SMichael S. Tsirkin last_addr = new_addr + size - 1; 1414315a1350SMichael S. Tsirkin /* NOTE: we do not support wrapping */ 1415315a1350SMichael S. Tsirkin /* XXX: as we cannot support really dynamic 1416315a1350SMichael S. Tsirkin mappings, we handle specific values as invalid 1417315a1350SMichael S. Tsirkin mappings. */ 1418e4024630SLaurent Vivier if (last_addr <= new_addr || last_addr == PCI_BAR_UNMAPPED || 1419e4024630SLaurent Vivier (!allow_0_address && new_addr == 0)) { 1420315a1350SMichael S. Tsirkin return PCI_BAR_UNMAPPED; 1421315a1350SMichael S. Tsirkin } 1422315a1350SMichael S. Tsirkin 1423315a1350SMichael S. Tsirkin /* Now pcibus_t is 64bit. 1424315a1350SMichael S. Tsirkin * Check if 32 bit BAR wraps around explicitly. 1425315a1350SMichael S. Tsirkin * Without this, PC ide doesn't work well. 1426315a1350SMichael S. Tsirkin * TODO: remove this work around. 1427315a1350SMichael S. Tsirkin */ 1428315a1350SMichael S. Tsirkin if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) { 1429315a1350SMichael S. Tsirkin return PCI_BAR_UNMAPPED; 1430315a1350SMichael S. Tsirkin } 1431315a1350SMichael S. Tsirkin 1432315a1350SMichael S. Tsirkin /* 1433315a1350SMichael S. Tsirkin * OS is allowed to set BAR beyond its addressable 1434315a1350SMichael S. Tsirkin * bits. For example, 32 bit OS can set 64bit bar 1435315a1350SMichael S. Tsirkin * to >4G. Check it. TODO: we might need to support 1436315a1350SMichael S. Tsirkin * it in the future for e.g. PAE. 1437315a1350SMichael S. Tsirkin */ 1438315a1350SMichael S. Tsirkin if (last_addr >= HWADDR_MAX) { 1439315a1350SMichael S. Tsirkin return PCI_BAR_UNMAPPED; 1440315a1350SMichael S. Tsirkin } 1441315a1350SMichael S. Tsirkin 1442315a1350SMichael S. Tsirkin return new_addr; 1443315a1350SMichael S. Tsirkin } 1444315a1350SMichael S. Tsirkin 1445315a1350SMichael S. Tsirkin static void pci_update_mappings(PCIDevice *d) 1446315a1350SMichael S. Tsirkin { 1447315a1350SMichael S. Tsirkin PCIIORegion *r; 1448315a1350SMichael S. Tsirkin int i; 1449315a1350SMichael S. Tsirkin pcibus_t new_addr; 1450315a1350SMichael S. Tsirkin 1451315a1350SMichael S. Tsirkin for(i = 0; i < PCI_NUM_REGIONS; i++) { 1452315a1350SMichael S. Tsirkin r = &d->io_regions[i]; 1453315a1350SMichael S. Tsirkin 1454315a1350SMichael S. Tsirkin /* this region isn't registered */ 1455315a1350SMichael S. Tsirkin if (!r->size) 1456315a1350SMichael S. Tsirkin continue; 1457315a1350SMichael S. Tsirkin 1458315a1350SMichael S. Tsirkin new_addr = pci_bar_address(d, i, r->type, r->size); 145923786d13SGerd Hoffmann if (!d->has_power) { 146023786d13SGerd Hoffmann new_addr = PCI_BAR_UNMAPPED; 146123786d13SGerd Hoffmann } 1462315a1350SMichael S. Tsirkin 1463315a1350SMichael S. Tsirkin /* This bar isn't changed */ 1464315a1350SMichael S. Tsirkin if (new_addr == r->addr) 1465315a1350SMichael S. Tsirkin continue; 1466315a1350SMichael S. Tsirkin 1467315a1350SMichael S. Tsirkin /* now do the real mapping */ 1468315a1350SMichael S. Tsirkin if (r->addr != PCI_BAR_UNMAPPED) { 1469deeb956cSLaurent Vivier trace_pci_update_mappings_del(d->name, pci_dev_bus_num(d), 14707828d750SDon Koch PCI_SLOT(d->devfn), 14710f288f85SLaszlo Ersek PCI_FUNC(d->devfn), 14727828d750SDon Koch i, r->addr, r->size); 1473315a1350SMichael S. Tsirkin memory_region_del_subregion(r->address_space, r->memory); 1474315a1350SMichael S. Tsirkin } 1475315a1350SMichael S. Tsirkin r->addr = new_addr; 1476315a1350SMichael S. Tsirkin if (r->addr != PCI_BAR_UNMAPPED) { 1477deeb956cSLaurent Vivier trace_pci_update_mappings_add(d->name, pci_dev_bus_num(d), 14787828d750SDon Koch PCI_SLOT(d->devfn), 14790f288f85SLaszlo Ersek PCI_FUNC(d->devfn), 14807828d750SDon Koch i, r->addr, r->size); 1481315a1350SMichael S. Tsirkin memory_region_add_subregion_overlap(r->address_space, 1482315a1350SMichael S. Tsirkin r->addr, r->memory, 1); 1483315a1350SMichael S. Tsirkin } 1484315a1350SMichael S. Tsirkin } 1485e01fd687SAlex Williamson 1486e01fd687SAlex Williamson pci_update_vga(d); 1487315a1350SMichael S. Tsirkin } 1488315a1350SMichael S. Tsirkin 1489315a1350SMichael S. Tsirkin static inline int pci_irq_disabled(PCIDevice *d) 1490315a1350SMichael S. Tsirkin { 1491315a1350SMichael S. Tsirkin return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE; 1492315a1350SMichael S. Tsirkin } 1493315a1350SMichael S. Tsirkin 1494315a1350SMichael S. Tsirkin /* Called after interrupt disabled field update in config space, 1495315a1350SMichael S. Tsirkin * assert/deassert interrupts if necessary. 1496315a1350SMichael S. Tsirkin * Gets original interrupt disable bit value (before update). */ 1497315a1350SMichael S. Tsirkin static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled) 1498315a1350SMichael S. Tsirkin { 1499315a1350SMichael S. Tsirkin int i, disabled = pci_irq_disabled(d); 1500315a1350SMichael S. Tsirkin if (disabled == was_irq_disabled) 1501315a1350SMichael S. Tsirkin return; 1502315a1350SMichael S. Tsirkin for (i = 0; i < PCI_NUM_PINS; ++i) { 1503315a1350SMichael S. Tsirkin int state = pci_irq_state(d, i); 1504315a1350SMichael S. Tsirkin pci_change_irq_level(d, i, disabled ? -state : state); 1505315a1350SMichael S. Tsirkin } 1506315a1350SMichael S. Tsirkin } 1507315a1350SMichael S. Tsirkin 1508315a1350SMichael S. Tsirkin uint32_t pci_default_read_config(PCIDevice *d, 1509315a1350SMichael S. Tsirkin uint32_t address, int len) 1510315a1350SMichael S. Tsirkin { 1511315a1350SMichael S. Tsirkin uint32_t val = 0; 1512315a1350SMichael S. Tsirkin 1513f7d6a635SPrasad J Pandit assert(address + len <= pci_config_size(d)); 1514f7d6a635SPrasad J Pandit 1515727b4866SAlex Williamson if (pci_is_express_downstream_port(d) && 1516727b4866SAlex Williamson ranges_overlap(address, len, d->exp.exp_cap + PCI_EXP_LNKSTA, 2)) { 1517727b4866SAlex Williamson pcie_sync_bridge_lnk(d); 1518727b4866SAlex Williamson } 1519315a1350SMichael S. Tsirkin memcpy(&val, d->config + address, len); 1520315a1350SMichael S. Tsirkin return le32_to_cpu(val); 1521315a1350SMichael S. Tsirkin } 1522315a1350SMichael S. Tsirkin 1523d7efb7e0SKnut Omang void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l) 1524315a1350SMichael S. Tsirkin { 1525315a1350SMichael S. Tsirkin int i, was_irq_disabled = pci_irq_disabled(d); 1526d7efb7e0SKnut Omang uint32_t val = val_in; 1527315a1350SMichael S. Tsirkin 1528f7d6a635SPrasad J Pandit assert(addr + l <= pci_config_size(d)); 1529f7d6a635SPrasad J Pandit 1530315a1350SMichael S. Tsirkin for (i = 0; i < l; val >>= 8, ++i) { 1531315a1350SMichael S. Tsirkin uint8_t wmask = d->wmask[addr + i]; 1532315a1350SMichael S. Tsirkin uint8_t w1cmask = d->w1cmask[addr + i]; 1533315a1350SMichael S. Tsirkin assert(!(wmask & w1cmask)); 1534315a1350SMichael S. Tsirkin d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask); 1535315a1350SMichael S. Tsirkin d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */ 1536315a1350SMichael S. Tsirkin } 1537315a1350SMichael S. Tsirkin if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) || 1538315a1350SMichael S. Tsirkin ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) || 1539315a1350SMichael S. Tsirkin ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) || 1540315a1350SMichael S. Tsirkin range_covers_byte(addr, l, PCI_COMMAND)) 1541315a1350SMichael S. Tsirkin pci_update_mappings(d); 1542315a1350SMichael S. Tsirkin 1543315a1350SMichael S. Tsirkin if (range_covers_byte(addr, l, PCI_COMMAND)) { 1544315a1350SMichael S. Tsirkin pci_update_irq_disabled(d, was_irq_disabled); 1545315a1350SMichael S. Tsirkin memory_region_set_enabled(&d->bus_master_enable_region, 154623786d13SGerd Hoffmann (pci_get_word(d->config + PCI_COMMAND) 154723786d13SGerd Hoffmann & PCI_COMMAND_MASTER) && d->has_power); 1548315a1350SMichael S. Tsirkin } 1549315a1350SMichael S. Tsirkin 1550d7efb7e0SKnut Omang msi_write_config(d, addr, val_in, l); 1551d7efb7e0SKnut Omang msix_write_config(d, addr, val_in, l); 15527c0fa8dfSKnut Omang pcie_sriov_config_write(d, addr, val_in, l); 1553315a1350SMichael S. Tsirkin } 1554315a1350SMichael S. Tsirkin 1555315a1350SMichael S. Tsirkin /***********************************************************/ 1556315a1350SMichael S. Tsirkin /* generic PCI irq support */ 1557315a1350SMichael S. Tsirkin 1558315a1350SMichael S. Tsirkin /* 0 <= irq_num <= 3. level must be 0 or 1 */ 1559d98f08f5SMarcel Apfelbaum static void pci_irq_handler(void *opaque, int irq_num, int level) 1560315a1350SMichael S. Tsirkin { 1561315a1350SMichael S. Tsirkin PCIDevice *pci_dev = opaque; 1562315a1350SMichael S. Tsirkin int change; 1563315a1350SMichael S. Tsirkin 15648ddf5432SIsaku Yamahata assert(0 <= irq_num && irq_num < PCI_NUM_PINS); 15658ddf5432SIsaku Yamahata assert(level == 0 || level == 1); 1566315a1350SMichael S. Tsirkin change = level - pci_irq_state(pci_dev, irq_num); 1567315a1350SMichael S. Tsirkin if (!change) 1568315a1350SMichael S. Tsirkin return; 1569315a1350SMichael S. Tsirkin 1570315a1350SMichael S. Tsirkin pci_set_irq_state(pci_dev, irq_num, level); 1571315a1350SMichael S. Tsirkin pci_update_irq_status(pci_dev); 1572315a1350SMichael S. Tsirkin if (pci_irq_disabled(pci_dev)) 1573315a1350SMichael S. Tsirkin return; 1574315a1350SMichael S. Tsirkin pci_change_irq_level(pci_dev, irq_num, change); 1575315a1350SMichael S. Tsirkin } 1576315a1350SMichael S. Tsirkin 1577d98f08f5SMarcel Apfelbaum qemu_irq pci_allocate_irq(PCIDevice *pci_dev) 1578d98f08f5SMarcel Apfelbaum { 1579d98f08f5SMarcel Apfelbaum int intx = pci_intx(pci_dev); 15808ddf5432SIsaku Yamahata assert(0 <= intx && intx < PCI_NUM_PINS); 1581d98f08f5SMarcel Apfelbaum 1582d98f08f5SMarcel Apfelbaum return qemu_allocate_irq(pci_irq_handler, pci_dev, intx); 1583d98f08f5SMarcel Apfelbaum } 1584d98f08f5SMarcel Apfelbaum 1585d98f08f5SMarcel Apfelbaum void pci_set_irq(PCIDevice *pci_dev, int level) 1586d98f08f5SMarcel Apfelbaum { 1587d98f08f5SMarcel Apfelbaum int intx = pci_intx(pci_dev); 1588d98f08f5SMarcel Apfelbaum pci_irq_handler(pci_dev, intx, level); 1589d98f08f5SMarcel Apfelbaum } 1590d98f08f5SMarcel Apfelbaum 1591315a1350SMichael S. Tsirkin /* Special hooks used by device assignment */ 1592315a1350SMichael S. Tsirkin void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq) 1593315a1350SMichael S. Tsirkin { 15940889464aSAlex Williamson assert(pci_bus_is_root(bus)); 1595315a1350SMichael S. Tsirkin bus->route_intx_to_irq = route_intx_to_irq; 1596315a1350SMichael S. Tsirkin } 1597315a1350SMichael S. Tsirkin 1598315a1350SMichael S. Tsirkin PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin) 1599315a1350SMichael S. Tsirkin { 1600315a1350SMichael S. Tsirkin PCIBus *bus; 1601315a1350SMichael S. Tsirkin 1602315a1350SMichael S. Tsirkin do { 1603fd56e061SDavid Gibson bus = pci_get_bus(dev); 1604315a1350SMichael S. Tsirkin pin = bus->map_irq(dev, pin); 1605315a1350SMichael S. Tsirkin dev = bus->parent_dev; 1606315a1350SMichael S. Tsirkin } while (dev); 1607315a1350SMichael S. Tsirkin 1608315a1350SMichael S. Tsirkin if (!bus->route_intx_to_irq) { 1609312fd5f2SMarkus Armbruster error_report("PCI: Bug - unimplemented PCI INTx routing (%s)", 1610315a1350SMichael S. Tsirkin object_get_typename(OBJECT(bus->qbus.parent))); 1611315a1350SMichael S. Tsirkin return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 }; 1612315a1350SMichael S. Tsirkin } 1613315a1350SMichael S. Tsirkin 1614315a1350SMichael S. Tsirkin return bus->route_intx_to_irq(bus->irq_opaque, pin); 1615315a1350SMichael S. Tsirkin } 1616315a1350SMichael S. Tsirkin 1617315a1350SMichael S. Tsirkin bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new) 1618315a1350SMichael S. Tsirkin { 1619315a1350SMichael S. Tsirkin return old->mode != new->mode || old->irq != new->irq; 1620315a1350SMichael S. Tsirkin } 1621315a1350SMichael S. Tsirkin 1622315a1350SMichael S. Tsirkin void pci_bus_fire_intx_routing_notifier(PCIBus *bus) 1623315a1350SMichael S. Tsirkin { 1624315a1350SMichael S. Tsirkin PCIDevice *dev; 1625315a1350SMichael S. Tsirkin PCIBus *sec; 1626315a1350SMichael S. Tsirkin int i; 1627315a1350SMichael S. Tsirkin 1628315a1350SMichael S. Tsirkin for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { 1629315a1350SMichael S. Tsirkin dev = bus->devices[i]; 1630315a1350SMichael S. Tsirkin if (dev && dev->intx_routing_notifier) { 1631315a1350SMichael S. Tsirkin dev->intx_routing_notifier(dev); 1632315a1350SMichael S. Tsirkin } 1633e5368f0dSAlex Williamson } 1634e5368f0dSAlex Williamson 1635315a1350SMichael S. Tsirkin QLIST_FOREACH(sec, &bus->child, sibling) { 1636315a1350SMichael S. Tsirkin pci_bus_fire_intx_routing_notifier(sec); 1637315a1350SMichael S. Tsirkin } 1638315a1350SMichael S. Tsirkin } 1639315a1350SMichael S. Tsirkin 1640315a1350SMichael S. Tsirkin void pci_device_set_intx_routing_notifier(PCIDevice *dev, 1641315a1350SMichael S. Tsirkin PCIINTxRoutingNotifier notifier) 1642315a1350SMichael S. Tsirkin { 1643315a1350SMichael S. Tsirkin dev->intx_routing_notifier = notifier; 1644315a1350SMichael S. Tsirkin } 1645315a1350SMichael S. Tsirkin 1646315a1350SMichael S. Tsirkin /* 1647315a1350SMichael S. Tsirkin * PCI-to-PCI bridge specification 1648315a1350SMichael S. Tsirkin * 9.1: Interrupt routing. Table 9-1 1649315a1350SMichael S. Tsirkin * 1650315a1350SMichael S. Tsirkin * the PCI Express Base Specification, Revision 2.1 1651315a1350SMichael S. Tsirkin * 2.2.8.1: INTx interrutp signaling - Rules 1652315a1350SMichael S. Tsirkin * the Implementation Note 1653315a1350SMichael S. Tsirkin * Table 2-20 1654315a1350SMichael S. Tsirkin */ 1655315a1350SMichael S. Tsirkin /* 1656315a1350SMichael S. Tsirkin * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD 1657315a1350SMichael S. Tsirkin * 0-origin unlike PCI interrupt pin register. 1658315a1350SMichael S. Tsirkin */ 1659315a1350SMichael S. Tsirkin int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin) 1660315a1350SMichael S. Tsirkin { 1661e8ec4adfSGreg Kurz return pci_swizzle(PCI_SLOT(pci_dev->devfn), pin); 1662315a1350SMichael S. Tsirkin } 1663315a1350SMichael S. Tsirkin 1664315a1350SMichael S. Tsirkin /***********************************************************/ 1665315a1350SMichael S. Tsirkin /* monitor info on PCI */ 1666315a1350SMichael S. Tsirkin 1667315a1350SMichael S. Tsirkin static const pci_class_desc pci_class_descriptions[] = 1668315a1350SMichael S. Tsirkin { 1669315a1350SMichael S. Tsirkin { 0x0001, "VGA controller", "display"}, 1670315a1350SMichael S. Tsirkin { 0x0100, "SCSI controller", "scsi"}, 1671315a1350SMichael S. Tsirkin { 0x0101, "IDE controller", "ide"}, 1672315a1350SMichael S. Tsirkin { 0x0102, "Floppy controller", "fdc"}, 1673315a1350SMichael S. Tsirkin { 0x0103, "IPI controller", "ipi"}, 1674315a1350SMichael S. Tsirkin { 0x0104, "RAID controller", "raid"}, 1675315a1350SMichael S. Tsirkin { 0x0106, "SATA controller"}, 1676315a1350SMichael S. Tsirkin { 0x0107, "SAS controller"}, 1677315a1350SMichael S. Tsirkin { 0x0180, "Storage controller"}, 1678315a1350SMichael S. Tsirkin { 0x0200, "Ethernet controller", "ethernet"}, 1679315a1350SMichael S. Tsirkin { 0x0201, "Token Ring controller", "token-ring"}, 1680315a1350SMichael S. Tsirkin { 0x0202, "FDDI controller", "fddi"}, 1681315a1350SMichael S. Tsirkin { 0x0203, "ATM controller", "atm"}, 1682315a1350SMichael S. Tsirkin { 0x0280, "Network controller"}, 1683315a1350SMichael S. Tsirkin { 0x0300, "VGA controller", "display", 0x00ff}, 1684315a1350SMichael S. Tsirkin { 0x0301, "XGA controller"}, 1685315a1350SMichael S. Tsirkin { 0x0302, "3D controller"}, 1686315a1350SMichael S. Tsirkin { 0x0380, "Display controller"}, 1687315a1350SMichael S. Tsirkin { 0x0400, "Video controller", "video"}, 1688315a1350SMichael S. Tsirkin { 0x0401, "Audio controller", "sound"}, 1689315a1350SMichael S. Tsirkin { 0x0402, "Phone"}, 1690315a1350SMichael S. Tsirkin { 0x0403, "Audio controller", "sound"}, 1691315a1350SMichael S. Tsirkin { 0x0480, "Multimedia controller"}, 1692315a1350SMichael S. Tsirkin { 0x0500, "RAM controller", "memory"}, 1693315a1350SMichael S. Tsirkin { 0x0501, "Flash controller", "flash"}, 1694315a1350SMichael S. Tsirkin { 0x0580, "Memory controller"}, 1695315a1350SMichael S. Tsirkin { 0x0600, "Host bridge", "host"}, 1696315a1350SMichael S. Tsirkin { 0x0601, "ISA bridge", "isa"}, 1697315a1350SMichael S. Tsirkin { 0x0602, "EISA bridge", "eisa"}, 1698315a1350SMichael S. Tsirkin { 0x0603, "MC bridge", "mca"}, 16994c41425dSGerd Hoffmann { 0x0604, "PCI bridge", "pci-bridge"}, 1700315a1350SMichael S. Tsirkin { 0x0605, "PCMCIA bridge", "pcmcia"}, 1701315a1350SMichael S. Tsirkin { 0x0606, "NUBUS bridge", "nubus"}, 1702315a1350SMichael S. Tsirkin { 0x0607, "CARDBUS bridge", "cardbus"}, 1703315a1350SMichael S. Tsirkin { 0x0608, "RACEWAY bridge"}, 1704315a1350SMichael S. Tsirkin { 0x0680, "Bridge"}, 1705315a1350SMichael S. Tsirkin { 0x0700, "Serial port", "serial"}, 1706315a1350SMichael S. Tsirkin { 0x0701, "Parallel port", "parallel"}, 1707315a1350SMichael S. Tsirkin { 0x0800, "Interrupt controller", "interrupt-controller"}, 1708315a1350SMichael S. Tsirkin { 0x0801, "DMA controller", "dma-controller"}, 1709315a1350SMichael S. Tsirkin { 0x0802, "Timer", "timer"}, 1710315a1350SMichael S. Tsirkin { 0x0803, "RTC", "rtc"}, 1711315a1350SMichael S. Tsirkin { 0x0900, "Keyboard", "keyboard"}, 1712315a1350SMichael S. Tsirkin { 0x0901, "Pen", "pen"}, 1713315a1350SMichael S. Tsirkin { 0x0902, "Mouse", "mouse"}, 1714315a1350SMichael S. Tsirkin { 0x0A00, "Dock station", "dock", 0x00ff}, 1715315a1350SMichael S. Tsirkin { 0x0B00, "i386 cpu", "cpu", 0x00ff}, 1716d1e9e646SRebecca Cran { 0x0c00, "Firewire controller", "firewire"}, 1717315a1350SMichael S. Tsirkin { 0x0c01, "Access bus controller", "access-bus"}, 1718315a1350SMichael S. Tsirkin { 0x0c02, "SSA controller", "ssa"}, 1719315a1350SMichael S. Tsirkin { 0x0c03, "USB controller", "usb"}, 1720315a1350SMichael S. Tsirkin { 0x0c04, "Fibre channel controller", "fibre-channel"}, 1721315a1350SMichael S. Tsirkin { 0x0c05, "SMBus"}, 1722315a1350SMichael S. Tsirkin { 0, NULL} 1723315a1350SMichael S. Tsirkin }; 1724315a1350SMichael S. Tsirkin 17252914fc61SPeter Xu void pci_for_each_device_under_bus_reverse(PCIBus *bus, 1726b3dcf94fSPeter Xu pci_bus_dev_fn fn, 1727a8eeafdaSGreg Kurz void *opaque) 1728a8eeafdaSGreg Kurz { 1729a8eeafdaSGreg Kurz PCIDevice *d; 1730a8eeafdaSGreg Kurz int devfn; 1731a8eeafdaSGreg Kurz 1732a8eeafdaSGreg Kurz for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { 1733a8eeafdaSGreg Kurz d = bus->devices[ARRAY_SIZE(bus->devices) - 1 - devfn]; 1734a8eeafdaSGreg Kurz if (d) { 1735a8eeafdaSGreg Kurz fn(bus, d, opaque); 1736a8eeafdaSGreg Kurz } 1737a8eeafdaSGreg Kurz } 1738a8eeafdaSGreg Kurz } 1739a8eeafdaSGreg Kurz 1740a8eeafdaSGreg Kurz void pci_for_each_device_reverse(PCIBus *bus, int bus_num, 1741b3dcf94fSPeter Xu pci_bus_dev_fn fn, void *opaque) 1742a8eeafdaSGreg Kurz { 1743a8eeafdaSGreg Kurz bus = pci_find_bus_nr(bus, bus_num); 1744a8eeafdaSGreg Kurz 1745a8eeafdaSGreg Kurz if (bus) { 1746a8eeafdaSGreg Kurz pci_for_each_device_under_bus_reverse(bus, fn, opaque); 1747a8eeafdaSGreg Kurz } 1748a8eeafdaSGreg Kurz } 1749a8eeafdaSGreg Kurz 17502914fc61SPeter Xu void pci_for_each_device_under_bus(PCIBus *bus, 1751b3dcf94fSPeter Xu pci_bus_dev_fn fn, void *opaque) 1752315a1350SMichael S. Tsirkin { 1753315a1350SMichael S. Tsirkin PCIDevice *d; 1754315a1350SMichael S. Tsirkin int devfn; 1755315a1350SMichael S. Tsirkin 1756315a1350SMichael S. Tsirkin for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) { 1757315a1350SMichael S. Tsirkin d = bus->devices[devfn]; 1758315a1350SMichael S. Tsirkin if (d) { 1759315a1350SMichael S. Tsirkin fn(bus, d, opaque); 1760315a1350SMichael S. Tsirkin } 1761315a1350SMichael S. Tsirkin } 1762315a1350SMichael S. Tsirkin } 1763315a1350SMichael S. Tsirkin 1764315a1350SMichael S. Tsirkin void pci_for_each_device(PCIBus *bus, int bus_num, 1765b3dcf94fSPeter Xu pci_bus_dev_fn fn, void *opaque) 1766315a1350SMichael S. Tsirkin { 1767315a1350SMichael S. Tsirkin bus = pci_find_bus_nr(bus, bus_num); 1768315a1350SMichael S. Tsirkin 1769315a1350SMichael S. Tsirkin if (bus) { 1770315a1350SMichael S. Tsirkin pci_for_each_device_under_bus(bus, fn, opaque); 1771315a1350SMichael S. Tsirkin } 1772315a1350SMichael S. Tsirkin } 1773315a1350SMichael S. Tsirkin 1774987b73b3SMarkus Armbruster const pci_class_desc *get_class_desc(int class) 1775315a1350SMichael S. Tsirkin { 1776315a1350SMichael S. Tsirkin const pci_class_desc *desc; 1777315a1350SMichael S. Tsirkin 1778315a1350SMichael S. Tsirkin desc = pci_class_descriptions; 1779315a1350SMichael S. Tsirkin while (desc->desc && class != desc->class) { 1780315a1350SMichael S. Tsirkin desc++; 1781315a1350SMichael S. Tsirkin } 1782315a1350SMichael S. Tsirkin 1783315a1350SMichael S. Tsirkin return desc; 1784315a1350SMichael S. Tsirkin } 1785315a1350SMichael S. Tsirkin 1786315a1350SMichael S. Tsirkin /* Initialize a PCI NIC. */ 178751f7cb97SThomas Huth PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus, 178829b358f9SDavid Gibson const char *default_model, 178951f7cb97SThomas Huth const char *default_devaddr) 1790315a1350SMichael S. Tsirkin { 1791315a1350SMichael S. Tsirkin const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr; 179252310c3fSPaolo Bonzini GPtrArray *pci_nic_models; 1793315a1350SMichael S. Tsirkin PCIBus *bus; 1794315a1350SMichael S. Tsirkin PCIDevice *pci_dev; 1795315a1350SMichael S. Tsirkin DeviceState *dev; 179651f7cb97SThomas Huth int devfn; 1797315a1350SMichael S. Tsirkin int i; 17982ad778b8SDavid Gibson int dom, busnr; 17992ad778b8SDavid Gibson unsigned slot; 1800315a1350SMichael S. Tsirkin 180152310c3fSPaolo Bonzini if (nd->model && !strcmp(nd->model, "virtio")) { 180252310c3fSPaolo Bonzini g_free(nd->model); 180352310c3fSPaolo Bonzini nd->model = g_strdup("virtio-net-pci"); 180452310c3fSPaolo Bonzini } 180552310c3fSPaolo Bonzini 1806*c6941b3bSThomas Huth pci_nic_models = qemu_get_nic_models(TYPE_PCI_DEVICE); 180752310c3fSPaolo Bonzini 180852310c3fSPaolo Bonzini if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) { 180951f7cb97SThomas Huth exit(0); 181051f7cb97SThomas Huth } 181151f7cb97SThomas Huth 181252310c3fSPaolo Bonzini i = qemu_find_nic_model(nd, (const char **)pci_nic_models->pdata, 181352310c3fSPaolo Bonzini default_model); 181451f7cb97SThomas Huth if (i < 0) { 181551f7cb97SThomas Huth exit(1); 181651f7cb97SThomas Huth } 1817315a1350SMichael S. Tsirkin 18182ad778b8SDavid Gibson if (!rootbus) { 18192ad778b8SDavid Gibson error_report("No primary PCI bus"); 18202ad778b8SDavid Gibson exit(1); 18212ad778b8SDavid Gibson } 18222ad778b8SDavid Gibson 18232ad778b8SDavid Gibson assert(!rootbus->parent_dev); 18242ad778b8SDavid Gibson 18252ad778b8SDavid Gibson if (!devaddr) { 18262ad778b8SDavid Gibson devfn = -1; 18272ad778b8SDavid Gibson busnr = 0; 18282ad778b8SDavid Gibson } else { 18292ad778b8SDavid Gibson if (pci_parse_devaddr(devaddr, &dom, &busnr, &slot, NULL) < 0) { 18302ad778b8SDavid Gibson error_report("Invalid PCI device address %s for device %s", 18312ad778b8SDavid Gibson devaddr, nd->model); 18322ad778b8SDavid Gibson exit(1); 18332ad778b8SDavid Gibson } 18342ad778b8SDavid Gibson 18352ad778b8SDavid Gibson if (dom != 0) { 18362ad778b8SDavid Gibson error_report("No support for non-zero PCI domains"); 18372ad778b8SDavid Gibson exit(1); 18382ad778b8SDavid Gibson } 18392ad778b8SDavid Gibson 18402ad778b8SDavid Gibson devfn = PCI_DEVFN(slot, 0); 18412ad778b8SDavid Gibson } 18422ad778b8SDavid Gibson 18432ad778b8SDavid Gibson bus = pci_find_bus_nr(rootbus, busnr); 1844315a1350SMichael S. Tsirkin if (!bus) { 1845315a1350SMichael S. Tsirkin error_report("Invalid PCI device address %s for device %s", 184652310c3fSPaolo Bonzini devaddr, nd->model); 184751f7cb97SThomas Huth exit(1); 1848315a1350SMichael S. Tsirkin } 1849315a1350SMichael S. Tsirkin 18509307d06dSMarkus Armbruster pci_dev = pci_new(devfn, nd->model); 1851315a1350SMichael S. Tsirkin dev = &pci_dev->qdev; 1852315a1350SMichael S. Tsirkin qdev_set_nic_properties(dev, nd); 18539307d06dSMarkus Armbruster pci_realize_and_unref(pci_dev, bus, &error_fatal); 185452310c3fSPaolo Bonzini g_ptr_array_free(pci_nic_models, true); 185551f7cb97SThomas Huth return pci_dev; 1856315a1350SMichael S. Tsirkin } 1857315a1350SMichael S. Tsirkin 1858315a1350SMichael S. Tsirkin PCIDevice *pci_vga_init(PCIBus *bus) 1859315a1350SMichael S. Tsirkin { 1860f9bcb2d6SGautam Agrawal vga_interface_created = true; 1861315a1350SMichael S. Tsirkin switch (vga_interface_type) { 1862315a1350SMichael S. Tsirkin case VGA_CIRRUS: 1863315a1350SMichael S. Tsirkin return pci_create_simple(bus, -1, "cirrus-vga"); 1864315a1350SMichael S. Tsirkin case VGA_QXL: 1865315a1350SMichael S. Tsirkin return pci_create_simple(bus, -1, "qxl-vga"); 1866315a1350SMichael S. Tsirkin case VGA_STD: 1867315a1350SMichael S. Tsirkin return pci_create_simple(bus, -1, "VGA"); 1868315a1350SMichael S. Tsirkin case VGA_VMWARE: 1869315a1350SMichael S. Tsirkin return pci_create_simple(bus, -1, "vmware-svga"); 1870a94f0c5cSGerd Hoffmann case VGA_VIRTIO: 1871a94f0c5cSGerd Hoffmann return pci_create_simple(bus, -1, "virtio-vga"); 1872315a1350SMichael S. Tsirkin case VGA_NONE: 1873315a1350SMichael S. Tsirkin default: /* Other non-PCI types. Checking for unsupported types is already 1874315a1350SMichael S. Tsirkin done in vl.c. */ 1875315a1350SMichael S. Tsirkin return NULL; 1876315a1350SMichael S. Tsirkin } 1877315a1350SMichael S. Tsirkin } 1878315a1350SMichael S. Tsirkin 1879315a1350SMichael S. Tsirkin /* Whether a given bus number is in range of the secondary 1880315a1350SMichael S. Tsirkin * bus of the given bridge device. */ 1881315a1350SMichael S. Tsirkin static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num) 1882315a1350SMichael S. Tsirkin { 1883315a1350SMichael S. Tsirkin return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) & 1884315a1350SMichael S. Tsirkin PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ && 188509e5b819SMarcel Apfelbaum dev->config[PCI_SECONDARY_BUS] <= bus_num && 1886315a1350SMichael S. Tsirkin bus_num <= dev->config[PCI_SUBORDINATE_BUS]; 1887315a1350SMichael S. Tsirkin } 1888315a1350SMichael S. Tsirkin 188909e5b819SMarcel Apfelbaum /* Whether a given bus number is in a range of a root bus */ 189009e5b819SMarcel Apfelbaum static bool pci_root_bus_in_range(PCIBus *bus, int bus_num) 189109e5b819SMarcel Apfelbaum { 189209e5b819SMarcel Apfelbaum int i; 189309e5b819SMarcel Apfelbaum 189409e5b819SMarcel Apfelbaum for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) { 189509e5b819SMarcel Apfelbaum PCIDevice *dev = bus->devices[i]; 189609e5b819SMarcel Apfelbaum 1897ad494274SIgor Mammedov if (dev && IS_PCI_BRIDGE(dev)) { 189809e5b819SMarcel Apfelbaum if (pci_secondary_bus_in_range(dev, bus_num)) { 189909e5b819SMarcel Apfelbaum return true; 190009e5b819SMarcel Apfelbaum } 190109e5b819SMarcel Apfelbaum } 190209e5b819SMarcel Apfelbaum } 190309e5b819SMarcel Apfelbaum 190409e5b819SMarcel Apfelbaum return false; 190509e5b819SMarcel Apfelbaum } 190609e5b819SMarcel Apfelbaum 1907987b73b3SMarkus Armbruster PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num) 1908315a1350SMichael S. Tsirkin { 1909315a1350SMichael S. Tsirkin PCIBus *sec; 1910315a1350SMichael S. Tsirkin 1911315a1350SMichael S. Tsirkin if (!bus) { 1912315a1350SMichael S. Tsirkin return NULL; 1913315a1350SMichael S. Tsirkin } 1914315a1350SMichael S. Tsirkin 1915315a1350SMichael S. Tsirkin if (pci_bus_num(bus) == bus_num) { 1916315a1350SMichael S. Tsirkin return bus; 1917315a1350SMichael S. Tsirkin } 1918315a1350SMichael S. Tsirkin 1919315a1350SMichael S. Tsirkin /* Consider all bus numbers in range for the host pci bridge. */ 19200889464aSAlex Williamson if (!pci_bus_is_root(bus) && 1921315a1350SMichael S. Tsirkin !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) { 1922315a1350SMichael S. Tsirkin return NULL; 1923315a1350SMichael S. Tsirkin } 1924315a1350SMichael S. Tsirkin 1925315a1350SMichael S. Tsirkin /* try child bus */ 1926315a1350SMichael S. Tsirkin for (; bus; bus = sec) { 1927315a1350SMichael S. Tsirkin QLIST_FOREACH(sec, &bus->child, sibling) { 192809e5b819SMarcel Apfelbaum if (pci_bus_num(sec) == bus_num) { 1929315a1350SMichael S. Tsirkin return sec; 1930315a1350SMichael S. Tsirkin } 193109e5b819SMarcel Apfelbaum /* PXB buses assumed to be children of bus 0 */ 193209e5b819SMarcel Apfelbaum if (pci_bus_is_root(sec)) { 193309e5b819SMarcel Apfelbaum if (pci_root_bus_in_range(sec, bus_num)) { 193409e5b819SMarcel Apfelbaum break; 193509e5b819SMarcel Apfelbaum } 193609e5b819SMarcel Apfelbaum } else { 1937315a1350SMichael S. Tsirkin if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) { 1938315a1350SMichael S. Tsirkin break; 1939315a1350SMichael S. Tsirkin } 1940315a1350SMichael S. Tsirkin } 1941315a1350SMichael S. Tsirkin } 194209e5b819SMarcel Apfelbaum } 1943315a1350SMichael S. Tsirkin 1944315a1350SMichael S. Tsirkin return NULL; 1945315a1350SMichael S. Tsirkin } 1946315a1350SMichael S. Tsirkin 1947b3dcf94fSPeter Xu void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin, 1948b3dcf94fSPeter Xu pci_bus_fn end, void *parent_state) 1949eb0acfddSMichael S. Tsirkin { 1950eb0acfddSMichael S. Tsirkin PCIBus *sec; 1951eb0acfddSMichael S. Tsirkin void *state; 1952eb0acfddSMichael S. Tsirkin 1953eb0acfddSMichael S. Tsirkin if (!bus) { 1954eb0acfddSMichael S. Tsirkin return; 1955eb0acfddSMichael S. Tsirkin } 1956eb0acfddSMichael S. Tsirkin 1957eb0acfddSMichael S. Tsirkin if (begin) { 1958eb0acfddSMichael S. Tsirkin state = begin(bus, parent_state); 1959eb0acfddSMichael S. Tsirkin } else { 1960eb0acfddSMichael S. Tsirkin state = parent_state; 1961eb0acfddSMichael S. Tsirkin } 1962eb0acfddSMichael S. Tsirkin 1963eb0acfddSMichael S. Tsirkin QLIST_FOREACH(sec, &bus->child, sibling) { 1964eb0acfddSMichael S. Tsirkin pci_for_each_bus_depth_first(sec, begin, end, state); 1965eb0acfddSMichael S. Tsirkin } 1966eb0acfddSMichael S. Tsirkin 1967eb0acfddSMichael S. Tsirkin if (end) { 1968eb0acfddSMichael S. Tsirkin end(bus, state); 1969eb0acfddSMichael S. Tsirkin } 1970eb0acfddSMichael S. Tsirkin } 1971eb0acfddSMichael S. Tsirkin 1972eb0acfddSMichael S. Tsirkin 1973315a1350SMichael S. Tsirkin PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn) 1974315a1350SMichael S. Tsirkin { 1975315a1350SMichael S. Tsirkin bus = pci_find_bus_nr(bus, bus_num); 1976315a1350SMichael S. Tsirkin 1977315a1350SMichael S. Tsirkin if (!bus) 1978315a1350SMichael S. Tsirkin return NULL; 1979315a1350SMichael S. Tsirkin 1980315a1350SMichael S. Tsirkin return bus->devices[devfn]; 1981315a1350SMichael S. Tsirkin } 1982315a1350SMichael S. Tsirkin 1983133e9b22SMarkus Armbruster static void pci_qdev_realize(DeviceState *qdev, Error **errp) 1984315a1350SMichael S. Tsirkin { 1985315a1350SMichael S. Tsirkin PCIDevice *pci_dev = (PCIDevice *)qdev; 1986315a1350SMichael S. Tsirkin PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev); 1987d61a363dSYoni Bettan ObjectClass *klass = OBJECT_CLASS(pc); 1988133e9b22SMarkus Armbruster Error *local_err = NULL; 1989315a1350SMichael S. Tsirkin bool is_default_rom; 19904f5b6a05SJens Freimann uint16_t class_id; 1991315a1350SMichael S. Tsirkin 199208b1df8fSPaolo Bonzini if (pci_dev->romsize != -1 && !is_power_of_2(pci_dev->romsize)) { 199308b1df8fSPaolo Bonzini error_setg(errp, "ROM size %u is not a power of two", pci_dev->romsize); 199408b1df8fSPaolo Bonzini return; 199508b1df8fSPaolo Bonzini } 199608b1df8fSPaolo Bonzini 1997d61a363dSYoni Bettan /* initialize cap_present for pci_is_express() and pci_config_size(), 1998d61a363dSYoni Bettan * Note that hybrid PCIs are not set automatically and need to manage 1999d61a363dSYoni Bettan * QEMU_PCI_CAP_EXPRESS manually */ 2000d61a363dSYoni Bettan if (object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE) && 2001d61a363dSYoni Bettan !object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE)) { 2002315a1350SMichael S. Tsirkin pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; 2003315a1350SMichael S. Tsirkin } 2004315a1350SMichael S. Tsirkin 2005cf04aba2SBen Widawsky if (object_class_dynamic_cast(klass, INTERFACE_CXL_DEVICE)) { 2006cf04aba2SBen Widawsky pci_dev->cap_present |= QEMU_PCIE_CAP_CXL; 2007cf04aba2SBen Widawsky } 2008cf04aba2SBen Widawsky 2009fd56e061SDavid Gibson pci_dev = do_pci_register_device(pci_dev, 2010315a1350SMichael S. Tsirkin object_get_typename(OBJECT(qdev)), 2011133e9b22SMarkus Armbruster pci_dev->devfn, errp); 2012315a1350SMichael S. Tsirkin if (pci_dev == NULL) 2013133e9b22SMarkus Armbruster return; 20142897ae02SIgor Mammedov 20157ee6c1e1SMarkus Armbruster if (pc->realize) { 20167ee6c1e1SMarkus Armbruster pc->realize(pci_dev, &local_err); 20177ee6c1e1SMarkus Armbruster if (local_err) { 20187ee6c1e1SMarkus Armbruster error_propagate(errp, local_err); 2019315a1350SMichael S. Tsirkin do_pci_unregister_device(pci_dev); 2020133e9b22SMarkus Armbruster return; 2021315a1350SMichael S. Tsirkin } 2022315a1350SMichael S. Tsirkin } 2023315a1350SMichael S. Tsirkin 20244f5b6a05SJens Freimann if (pci_dev->failover_pair_id) { 20254f5b6a05SJens Freimann if (!pci_bus_is_express(pci_get_bus(pci_dev))) { 20264f5b6a05SJens Freimann error_setg(errp, "failover primary device must be on " 20274f5b6a05SJens Freimann "PCIExpress bus"); 2028b69c3c21SMarkus Armbruster pci_qdev_unrealize(DEVICE(pci_dev)); 20294f5b6a05SJens Freimann return; 20304f5b6a05SJens Freimann } 20314f5b6a05SJens Freimann class_id = pci_get_word(pci_dev->config + PCI_CLASS_DEVICE); 20324f5b6a05SJens Freimann if (class_id != PCI_CLASS_NETWORK_ETHERNET) { 20334f5b6a05SJens Freimann error_setg(errp, "failover primary device is not an " 20344f5b6a05SJens Freimann "Ethernet device"); 2035b69c3c21SMarkus Armbruster pci_qdev_unrealize(DEVICE(pci_dev)); 20364f5b6a05SJens Freimann return; 20374f5b6a05SJens Freimann } 2038b01a4901SLaurent Vivier if ((pci_dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) 2039b01a4901SLaurent Vivier || (PCI_FUNC(pci_dev->devfn) != 0)) { 20404f5b6a05SJens Freimann error_setg(errp, "failover: primary device must be in its own " 20414f5b6a05SJens Freimann "PCI slot"); 2042b69c3c21SMarkus Armbruster pci_qdev_unrealize(DEVICE(pci_dev)); 20434f5b6a05SJens Freimann return; 20444f5b6a05SJens Freimann } 2045a1190ab6SJens Freimann qdev->allow_unplug_during_migration = true; 20464f5b6a05SJens Freimann } 20474f5b6a05SJens Freimann 2048315a1350SMichael S. Tsirkin /* rom loading */ 2049315a1350SMichael S. Tsirkin is_default_rom = false; 2050315a1350SMichael S. Tsirkin if (pci_dev->romfile == NULL && pc->romfile != NULL) { 2051315a1350SMichael S. Tsirkin pci_dev->romfile = g_strdup(pc->romfile); 2052315a1350SMichael S. Tsirkin is_default_rom = true; 2053315a1350SMichael S. Tsirkin } 2054178e785fSMarcel Apfelbaum 2055133e9b22SMarkus Armbruster pci_add_option_rom(pci_dev, is_default_rom, &local_err); 2056133e9b22SMarkus Armbruster if (local_err) { 2057133e9b22SMarkus Armbruster error_propagate(errp, local_err); 2058b69c3c21SMarkus Armbruster pci_qdev_unrealize(DEVICE(pci_dev)); 2059133e9b22SMarkus Armbruster return; 2060178e785fSMarcel Apfelbaum } 206123786d13SGerd Hoffmann 206223786d13SGerd Hoffmann pci_set_power(pci_dev, true); 206308cf3dc6SJagannathan Raman 206408cf3dc6SJagannathan Raman pci_dev->msi_trigger = pci_msi_trigger; 2065315a1350SMichael S. Tsirkin } 2066315a1350SMichael S. Tsirkin 20677411aa63SMarkus Armbruster PCIDevice *pci_new_multifunction(int devfn, bool multifunction, 20687411aa63SMarkus Armbruster const char *name) 20697411aa63SMarkus Armbruster { 20707411aa63SMarkus Armbruster DeviceState *dev; 20717411aa63SMarkus Armbruster 20727411aa63SMarkus Armbruster dev = qdev_new(name); 20737411aa63SMarkus Armbruster qdev_prop_set_int32(dev, "addr", devfn); 20747411aa63SMarkus Armbruster qdev_prop_set_bit(dev, "multifunction", multifunction); 20757411aa63SMarkus Armbruster return PCI_DEVICE(dev); 20767411aa63SMarkus Armbruster } 20777411aa63SMarkus Armbruster 20787411aa63SMarkus Armbruster PCIDevice *pci_new(int devfn, const char *name) 20797411aa63SMarkus Armbruster { 20807411aa63SMarkus Armbruster return pci_new_multifunction(devfn, false, name); 20817411aa63SMarkus Armbruster } 20827411aa63SMarkus Armbruster 20837411aa63SMarkus Armbruster bool pci_realize_and_unref(PCIDevice *dev, PCIBus *bus, Error **errp) 20847411aa63SMarkus Armbruster { 20857411aa63SMarkus Armbruster return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp); 20867411aa63SMarkus Armbruster } 20877411aa63SMarkus Armbruster 2088315a1350SMichael S. Tsirkin PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn, 2089315a1350SMichael S. Tsirkin bool multifunction, 2090315a1350SMichael S. Tsirkin const char *name) 2091315a1350SMichael S. Tsirkin { 20929307d06dSMarkus Armbruster PCIDevice *dev = pci_new_multifunction(devfn, multifunction, name); 20939307d06dSMarkus Armbruster pci_realize_and_unref(dev, bus, &error_fatal); 2094315a1350SMichael S. Tsirkin return dev; 2095315a1350SMichael S. Tsirkin } 2096315a1350SMichael S. Tsirkin 2097315a1350SMichael S. Tsirkin PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name) 2098315a1350SMichael S. Tsirkin { 2099315a1350SMichael S. Tsirkin return pci_create_simple_multifunction(bus, devfn, false, name); 2100315a1350SMichael S. Tsirkin } 2101315a1350SMichael S. Tsirkin 2102315a1350SMichael S. Tsirkin static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size) 2103315a1350SMichael S. Tsirkin { 2104315a1350SMichael S. Tsirkin int offset = PCI_CONFIG_HEADER_SIZE; 2105315a1350SMichael S. Tsirkin int i; 2106315a1350SMichael S. Tsirkin for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) { 2107315a1350SMichael S. Tsirkin if (pdev->used[i]) 2108315a1350SMichael S. Tsirkin offset = i + 1; 2109315a1350SMichael S. Tsirkin else if (i - offset + 1 == size) 2110315a1350SMichael S. Tsirkin return offset; 2111315a1350SMichael S. Tsirkin } 2112315a1350SMichael S. Tsirkin return 0; 2113315a1350SMichael S. Tsirkin } 2114315a1350SMichael S. Tsirkin 2115315a1350SMichael S. Tsirkin static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id, 2116315a1350SMichael S. Tsirkin uint8_t *prev_p) 2117315a1350SMichael S. Tsirkin { 2118315a1350SMichael S. Tsirkin uint8_t next, prev; 2119315a1350SMichael S. Tsirkin 2120315a1350SMichael S. Tsirkin if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST)) 2121315a1350SMichael S. Tsirkin return 0; 2122315a1350SMichael S. Tsirkin 2123315a1350SMichael S. Tsirkin for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]); 2124315a1350SMichael S. Tsirkin prev = next + PCI_CAP_LIST_NEXT) 2125315a1350SMichael S. Tsirkin if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id) 2126315a1350SMichael S. Tsirkin break; 2127315a1350SMichael S. Tsirkin 2128315a1350SMichael S. Tsirkin if (prev_p) 2129315a1350SMichael S. Tsirkin *prev_p = prev; 2130315a1350SMichael S. Tsirkin return next; 2131315a1350SMichael S. Tsirkin } 2132315a1350SMichael S. Tsirkin 2133315a1350SMichael S. Tsirkin static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset) 2134315a1350SMichael S. Tsirkin { 2135315a1350SMichael S. Tsirkin uint8_t next, prev, found = 0; 2136315a1350SMichael S. Tsirkin 2137315a1350SMichael S. Tsirkin if (!(pdev->used[offset])) { 2138315a1350SMichael S. Tsirkin return 0; 2139315a1350SMichael S. Tsirkin } 2140315a1350SMichael S. Tsirkin 2141315a1350SMichael S. Tsirkin assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST); 2142315a1350SMichael S. Tsirkin 2143315a1350SMichael S. Tsirkin for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]); 2144315a1350SMichael S. Tsirkin prev = next + PCI_CAP_LIST_NEXT) { 2145315a1350SMichael S. Tsirkin if (next <= offset && next > found) { 2146315a1350SMichael S. Tsirkin found = next; 2147315a1350SMichael S. Tsirkin } 2148315a1350SMichael S. Tsirkin } 2149315a1350SMichael S. Tsirkin return found; 2150315a1350SMichael S. Tsirkin } 2151315a1350SMichael S. Tsirkin 2152315a1350SMichael S. Tsirkin /* Patch the PCI vendor and device ids in a PCI rom image if necessary. 2153315a1350SMichael S. Tsirkin This is needed for an option rom which is used for more than one device. */ 21547c16b5bbSPaolo Bonzini static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size) 2155315a1350SMichael S. Tsirkin { 2156315a1350SMichael S. Tsirkin uint16_t vendor_id; 2157315a1350SMichael S. Tsirkin uint16_t device_id; 2158315a1350SMichael S. Tsirkin uint16_t rom_vendor_id; 2159315a1350SMichael S. Tsirkin uint16_t rom_device_id; 2160315a1350SMichael S. Tsirkin uint16_t rom_magic; 2161315a1350SMichael S. Tsirkin uint16_t pcir_offset; 2162315a1350SMichael S. Tsirkin uint8_t checksum; 2163315a1350SMichael S. Tsirkin 2164315a1350SMichael S. Tsirkin /* Words in rom data are little endian (like in PCI configuration), 2165315a1350SMichael S. Tsirkin so they can be read / written with pci_get_word / pci_set_word. */ 2166315a1350SMichael S. Tsirkin 2167315a1350SMichael S. Tsirkin /* Only a valid rom will be patched. */ 2168315a1350SMichael S. Tsirkin rom_magic = pci_get_word(ptr); 2169315a1350SMichael S. Tsirkin if (rom_magic != 0xaa55) { 2170315a1350SMichael S. Tsirkin PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic); 2171315a1350SMichael S. Tsirkin return; 2172315a1350SMichael S. Tsirkin } 2173315a1350SMichael S. Tsirkin pcir_offset = pci_get_word(ptr + 0x18); 2174315a1350SMichael S. Tsirkin if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) { 2175315a1350SMichael S. Tsirkin PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset); 2176315a1350SMichael S. Tsirkin return; 2177315a1350SMichael S. Tsirkin } 2178315a1350SMichael S. Tsirkin 2179315a1350SMichael S. Tsirkin vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID); 2180315a1350SMichael S. Tsirkin device_id = pci_get_word(pdev->config + PCI_DEVICE_ID); 2181315a1350SMichael S. Tsirkin rom_vendor_id = pci_get_word(ptr + pcir_offset + 4); 2182315a1350SMichael S. Tsirkin rom_device_id = pci_get_word(ptr + pcir_offset + 6); 2183315a1350SMichael S. Tsirkin 2184315a1350SMichael S. Tsirkin PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile, 2185315a1350SMichael S. Tsirkin vendor_id, device_id, rom_vendor_id, rom_device_id); 2186315a1350SMichael S. Tsirkin 2187315a1350SMichael S. Tsirkin checksum = ptr[6]; 2188315a1350SMichael S. Tsirkin 2189315a1350SMichael S. Tsirkin if (vendor_id != rom_vendor_id) { 2190315a1350SMichael S. Tsirkin /* Patch vendor id and checksum (at offset 6 for etherboot roms). */ 2191315a1350SMichael S. Tsirkin checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8); 2192315a1350SMichael S. Tsirkin checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8); 2193315a1350SMichael S. Tsirkin PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); 2194315a1350SMichael S. Tsirkin ptr[6] = checksum; 2195315a1350SMichael S. Tsirkin pci_set_word(ptr + pcir_offset + 4, vendor_id); 2196315a1350SMichael S. Tsirkin } 2197315a1350SMichael S. Tsirkin 2198315a1350SMichael S. Tsirkin if (device_id != rom_device_id) { 2199315a1350SMichael S. Tsirkin /* Patch device id and checksum (at offset 6 for etherboot roms). */ 2200315a1350SMichael S. Tsirkin checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8); 2201315a1350SMichael S. Tsirkin checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8); 2202315a1350SMichael S. Tsirkin PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum); 2203315a1350SMichael S. Tsirkin ptr[6] = checksum; 2204315a1350SMichael S. Tsirkin pci_set_word(ptr + pcir_offset + 6, device_id); 2205315a1350SMichael S. Tsirkin } 2206315a1350SMichael S. Tsirkin } 2207315a1350SMichael S. Tsirkin 2208315a1350SMichael S. Tsirkin /* Add an option rom for the device */ 2209133e9b22SMarkus Armbruster static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, 2210133e9b22SMarkus Armbruster Error **errp) 2211315a1350SMichael S. Tsirkin { 22127c16b5bbSPaolo Bonzini int64_t size; 2213315a1350SMichael S. Tsirkin char *path; 2214315a1350SMichael S. Tsirkin void *ptr; 2215315a1350SMichael S. Tsirkin char name[32]; 2216315a1350SMichael S. Tsirkin const VMStateDescription *vmsd; 2217315a1350SMichael S. Tsirkin 2218315a1350SMichael S. Tsirkin if (!pdev->romfile) 2219133e9b22SMarkus Armbruster return; 2220315a1350SMichael S. Tsirkin if (strlen(pdev->romfile) == 0) 2221133e9b22SMarkus Armbruster return; 2222315a1350SMichael S. Tsirkin 2223315a1350SMichael S. Tsirkin if (!pdev->rom_bar) { 2224315a1350SMichael S. Tsirkin /* 2225315a1350SMichael S. Tsirkin * Load rom via fw_cfg instead of creating a rom bar, 2226315a1350SMichael S. Tsirkin * for 0.11 compatibility. 2227315a1350SMichael S. Tsirkin */ 2228315a1350SMichael S. Tsirkin int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE); 2229db80c7b9SMarcel Apfelbaum 2230db80c7b9SMarcel Apfelbaum /* 2231db80c7b9SMarcel Apfelbaum * Hot-plugged devices can't use the option ROM 2232db80c7b9SMarcel Apfelbaum * if the rom bar is disabled. 2233db80c7b9SMarcel Apfelbaum */ 2234db80c7b9SMarcel Apfelbaum if (DEVICE(pdev)->hotplugged) { 2235133e9b22SMarkus Armbruster error_setg(errp, "Hot-plugged device without ROM bar" 2236133e9b22SMarkus Armbruster " can't have an option ROM"); 2237133e9b22SMarkus Armbruster return; 2238db80c7b9SMarcel Apfelbaum } 2239db80c7b9SMarcel Apfelbaum 2240315a1350SMichael S. Tsirkin if (class == 0x0300) { 2241315a1350SMichael S. Tsirkin rom_add_vga(pdev->romfile); 2242315a1350SMichael S. Tsirkin } else { 2243315a1350SMichael S. Tsirkin rom_add_option(pdev->romfile, -1); 2244315a1350SMichael S. Tsirkin } 2245133e9b22SMarkus Armbruster return; 2246315a1350SMichael S. Tsirkin } 2247315a1350SMichael S. Tsirkin 2248315a1350SMichael S. Tsirkin path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile); 2249315a1350SMichael S. Tsirkin if (path == NULL) { 2250315a1350SMichael S. Tsirkin path = g_strdup(pdev->romfile); 2251315a1350SMichael S. Tsirkin } 2252315a1350SMichael S. Tsirkin 2253315a1350SMichael S. Tsirkin size = get_image_size(path); 2254315a1350SMichael S. Tsirkin if (size < 0) { 2255133e9b22SMarkus Armbruster error_setg(errp, "failed to find romfile \"%s\"", pdev->romfile); 22568c7f3dd0SStefan Hajnoczi g_free(path); 2257133e9b22SMarkus Armbruster return; 22588c7f3dd0SStefan Hajnoczi } else if (size == 0) { 2259133e9b22SMarkus Armbruster error_setg(errp, "romfile \"%s\" is empty", pdev->romfile); 2260315a1350SMichael S. Tsirkin g_free(path); 2261133e9b22SMarkus Armbruster return; 22627c16b5bbSPaolo Bonzini } else if (size > 2 * GiB) { 22637c16b5bbSPaolo Bonzini error_setg(errp, "romfile \"%s\" too large (size cannot exceed 2 GiB)", 22647c16b5bbSPaolo Bonzini pdev->romfile); 22657c16b5bbSPaolo Bonzini g_free(path); 22667c16b5bbSPaolo Bonzini return; 2267315a1350SMichael S. Tsirkin } 226808b1df8fSPaolo Bonzini if (pdev->romsize != -1) { 226908b1df8fSPaolo Bonzini if (size > pdev->romsize) { 227008b1df8fSPaolo Bonzini error_setg(errp, "romfile \"%s\" (%u bytes) is too large for ROM size %u", 227108b1df8fSPaolo Bonzini pdev->romfile, (uint32_t)size, pdev->romsize); 227208b1df8fSPaolo Bonzini g_free(path); 227308b1df8fSPaolo Bonzini return; 227408b1df8fSPaolo Bonzini } 227508b1df8fSPaolo Bonzini } else { 227608b1df8fSPaolo Bonzini pdev->romsize = pow2ceil(size); 227708b1df8fSPaolo Bonzini } 2278315a1350SMichael S. Tsirkin 2279315a1350SMichael S. Tsirkin vmsd = qdev_get_vmsd(DEVICE(pdev)); 2280315a1350SMichael S. Tsirkin 2281315a1350SMichael S. Tsirkin if (vmsd) { 2282315a1350SMichael S. Tsirkin snprintf(name, sizeof(name), "%s.rom", vmsd->name); 2283315a1350SMichael S. Tsirkin } else { 2284315a1350SMichael S. Tsirkin snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev))); 2285315a1350SMichael S. Tsirkin } 2286315a1350SMichael S. Tsirkin pdev->has_rom = true; 228708b1df8fSPaolo Bonzini memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, pdev->romsize, &error_fatal); 2288315a1350SMichael S. Tsirkin ptr = memory_region_get_ram_ptr(&pdev->rom); 228936bde091SPeter Maydell if (load_image_size(path, ptr, size) < 0) { 229036bde091SPeter Maydell error_setg(errp, "failed to load romfile \"%s\"", pdev->romfile); 229136bde091SPeter Maydell g_free(path); 229236bde091SPeter Maydell return; 229336bde091SPeter Maydell } 2294315a1350SMichael S. Tsirkin g_free(path); 2295315a1350SMichael S. Tsirkin 2296315a1350SMichael S. Tsirkin if (is_default_rom) { 2297315a1350SMichael S. Tsirkin /* Only the default rom images will be patched (if needed). */ 2298315a1350SMichael S. Tsirkin pci_patch_ids(pdev, ptr, size); 2299315a1350SMichael S. Tsirkin } 2300315a1350SMichael S. Tsirkin 2301315a1350SMichael S. Tsirkin pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom); 2302315a1350SMichael S. Tsirkin } 2303315a1350SMichael S. Tsirkin 2304315a1350SMichael S. Tsirkin static void pci_del_option_rom(PCIDevice *pdev) 2305315a1350SMichael S. Tsirkin { 2306315a1350SMichael S. Tsirkin if (!pdev->has_rom) 2307315a1350SMichael S. Tsirkin return; 2308315a1350SMichael S. Tsirkin 2309315a1350SMichael S. Tsirkin vmstate_unregister_ram(&pdev->rom, &pdev->qdev); 2310315a1350SMichael S. Tsirkin pdev->has_rom = false; 2311315a1350SMichael S. Tsirkin } 2312315a1350SMichael S. Tsirkin 2313315a1350SMichael S. Tsirkin /* 231427841278SMao Zhongyi * On success, pci_add_capability() returns a positive value 2315eacbc632SMao Zhongyi * that the offset of the pci capability. 2316eacbc632SMao Zhongyi * On failure, it sets an error and returns a negative error 2317eacbc632SMao Zhongyi * code. 2318eacbc632SMao Zhongyi */ 231927841278SMao Zhongyi int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, 2320cd9aa33eSLaszlo Ersek uint8_t offset, uint8_t size, 2321cd9aa33eSLaszlo Ersek Error **errp) 2322cd9aa33eSLaszlo Ersek { 2323315a1350SMichael S. Tsirkin uint8_t *config; 2324315a1350SMichael S. Tsirkin int i, overlapping_cap; 2325315a1350SMichael S. Tsirkin 2326315a1350SMichael S. Tsirkin if (!offset) { 2327315a1350SMichael S. Tsirkin offset = pci_find_space(pdev, size); 232897fe42f1SCao jin /* out of PCI config space is programming error */ 232997fe42f1SCao jin assert(offset); 2330315a1350SMichael S. Tsirkin } else { 2331315a1350SMichael S. Tsirkin /* Verify that capabilities don't overlap. Note: device assignment 2332315a1350SMichael S. Tsirkin * depends on this check to verify that the device is not broken. 2333315a1350SMichael S. Tsirkin * Should never trigger for emulated devices, but it's helpful 2334315a1350SMichael S. Tsirkin * for debugging these. */ 2335315a1350SMichael S. Tsirkin for (i = offset; i < offset + size; i++) { 2336315a1350SMichael S. Tsirkin overlapping_cap = pci_find_capability_at_offset(pdev, i); 2337315a1350SMichael S. Tsirkin if (overlapping_cap) { 2338cd9aa33eSLaszlo Ersek error_setg(errp, "%s:%02x:%02x.%x " 2339315a1350SMichael S. Tsirkin "Attempt to add PCI capability %x at offset " 2340cd9aa33eSLaszlo Ersek "%x overlaps existing capability %x at offset %x", 2341fd56e061SDavid Gibson pci_root_bus_path(pdev), pci_dev_bus_num(pdev), 2342315a1350SMichael S. Tsirkin PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn), 2343315a1350SMichael S. Tsirkin cap_id, offset, overlapping_cap, i); 2344315a1350SMichael S. Tsirkin return -EINVAL; 2345315a1350SMichael S. Tsirkin } 2346315a1350SMichael S. Tsirkin } 2347315a1350SMichael S. Tsirkin } 2348315a1350SMichael S. Tsirkin 2349315a1350SMichael S. Tsirkin config = pdev->config + offset; 2350315a1350SMichael S. Tsirkin config[PCI_CAP_LIST_ID] = cap_id; 2351315a1350SMichael S. Tsirkin config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST]; 2352315a1350SMichael S. Tsirkin pdev->config[PCI_CAPABILITY_LIST] = offset; 2353315a1350SMichael S. Tsirkin pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST; 2354315a1350SMichael S. Tsirkin memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4)); 2355315a1350SMichael S. Tsirkin /* Make capability read-only by default */ 2356315a1350SMichael S. Tsirkin memset(pdev->wmask + offset, 0, size); 2357315a1350SMichael S. Tsirkin /* Check capability by default */ 2358315a1350SMichael S. Tsirkin memset(pdev->cmask + offset, 0xFF, size); 2359315a1350SMichael S. Tsirkin return offset; 2360315a1350SMichael S. Tsirkin } 2361315a1350SMichael S. Tsirkin 2362315a1350SMichael S. Tsirkin /* Unlink capability from the pci config space. */ 2363315a1350SMichael S. Tsirkin void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size) 2364315a1350SMichael S. Tsirkin { 2365315a1350SMichael S. Tsirkin uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev); 2366315a1350SMichael S. Tsirkin if (!offset) 2367315a1350SMichael S. Tsirkin return; 2368315a1350SMichael S. Tsirkin pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT]; 2369315a1350SMichael S. Tsirkin /* Make capability writable again */ 2370315a1350SMichael S. Tsirkin memset(pdev->wmask + offset, 0xff, size); 2371315a1350SMichael S. Tsirkin memset(pdev->w1cmask + offset, 0, size); 2372315a1350SMichael S. Tsirkin /* Clear cmask as device-specific registers can't be checked */ 2373315a1350SMichael S. Tsirkin memset(pdev->cmask + offset, 0, size); 2374315a1350SMichael S. Tsirkin memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4)); 2375315a1350SMichael S. Tsirkin 2376315a1350SMichael S. Tsirkin if (!pdev->config[PCI_CAPABILITY_LIST]) 2377315a1350SMichael S. Tsirkin pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST; 2378315a1350SMichael S. Tsirkin } 2379315a1350SMichael S. Tsirkin 2380315a1350SMichael S. Tsirkin uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id) 2381315a1350SMichael S. Tsirkin { 2382315a1350SMichael S. Tsirkin return pci_find_capability_list(pdev, cap_id, NULL); 2383315a1350SMichael S. Tsirkin } 2384315a1350SMichael S. Tsirkin 2385315a1350SMichael S. Tsirkin static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len) 2386315a1350SMichael S. Tsirkin { 2387315a1350SMichael S. Tsirkin PCIDevice *d = (PCIDevice *)dev; 2388315a1350SMichael S. Tsirkin const char *name = NULL; 2389315a1350SMichael S. Tsirkin const pci_class_desc *desc = pci_class_descriptions; 2390315a1350SMichael S. Tsirkin int class = pci_get_word(d->config + PCI_CLASS_DEVICE); 2391315a1350SMichael S. Tsirkin 2392315a1350SMichael S. Tsirkin while (desc->desc && 2393315a1350SMichael S. Tsirkin (class & ~desc->fw_ign_bits) != 2394315a1350SMichael S. Tsirkin (desc->class & ~desc->fw_ign_bits)) { 2395315a1350SMichael S. Tsirkin desc++; 2396315a1350SMichael S. Tsirkin } 2397315a1350SMichael S. Tsirkin 2398315a1350SMichael S. Tsirkin if (desc->desc) { 2399315a1350SMichael S. Tsirkin name = desc->fw_name; 2400315a1350SMichael S. Tsirkin } 2401315a1350SMichael S. Tsirkin 2402315a1350SMichael S. Tsirkin if (name) { 2403315a1350SMichael S. Tsirkin pstrcpy(buf, len, name); 2404315a1350SMichael S. Tsirkin } else { 2405315a1350SMichael S. Tsirkin snprintf(buf, len, "pci%04x,%04x", 2406315a1350SMichael S. Tsirkin pci_get_word(d->config + PCI_VENDOR_ID), 2407315a1350SMichael S. Tsirkin pci_get_word(d->config + PCI_DEVICE_ID)); 2408315a1350SMichael S. Tsirkin } 2409315a1350SMichael S. Tsirkin 2410315a1350SMichael S. Tsirkin return buf; 2411315a1350SMichael S. Tsirkin } 2412315a1350SMichael S. Tsirkin 2413315a1350SMichael S. Tsirkin static char *pcibus_get_fw_dev_path(DeviceState *dev) 2414315a1350SMichael S. Tsirkin { 2415315a1350SMichael S. Tsirkin PCIDevice *d = (PCIDevice *)dev; 241636f18c69SClaudio Fontana char name[33]; 241736f18c69SClaudio Fontana int has_func = !!PCI_FUNC(d->devfn); 2418315a1350SMichael S. Tsirkin 241936f18c69SClaudio Fontana return g_strdup_printf("%s@%x%s%.*x", 242036f18c69SClaudio Fontana pci_dev_fw_name(dev, name, sizeof(name)), 242136f18c69SClaudio Fontana PCI_SLOT(d->devfn), 242236f18c69SClaudio Fontana has_func ? "," : "", 242336f18c69SClaudio Fontana has_func, 242436f18c69SClaudio Fontana PCI_FUNC(d->devfn)); 2425315a1350SMichael S. Tsirkin } 2426315a1350SMichael S. Tsirkin 2427315a1350SMichael S. Tsirkin static char *pcibus_get_dev_path(DeviceState *dev) 2428315a1350SMichael S. Tsirkin { 2429315a1350SMichael S. Tsirkin PCIDevice *d = container_of(dev, PCIDevice, qdev); 2430315a1350SMichael S. Tsirkin PCIDevice *t; 2431315a1350SMichael S. Tsirkin int slot_depth; 2432315a1350SMichael S. Tsirkin /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function. 2433315a1350SMichael S. Tsirkin * 00 is added here to make this format compatible with 2434315a1350SMichael S. Tsirkin * domain:Bus:Slot.Func for systems without nested PCI bridges. 2435315a1350SMichael S. Tsirkin * Slot.Function list specifies the slot and function numbers for all 2436315a1350SMichael S. Tsirkin * devices on the path from root to the specific device. */ 2437568f0690SDavid Gibson const char *root_bus_path; 2438568f0690SDavid Gibson int root_bus_len; 2439315a1350SMichael S. Tsirkin char slot[] = ":SS.F"; 2440315a1350SMichael S. Tsirkin int slot_len = sizeof slot - 1 /* For '\0' */; 2441315a1350SMichael S. Tsirkin int path_len; 2442315a1350SMichael S. Tsirkin char *path, *p; 2443315a1350SMichael S. Tsirkin int s; 2444315a1350SMichael S. Tsirkin 2445568f0690SDavid Gibson root_bus_path = pci_root_bus_path(d); 2446568f0690SDavid Gibson root_bus_len = strlen(root_bus_path); 2447568f0690SDavid Gibson 2448315a1350SMichael S. Tsirkin /* Calculate # of slots on path between device and root. */; 2449315a1350SMichael S. Tsirkin slot_depth = 0; 2450fd56e061SDavid Gibson for (t = d; t; t = pci_get_bus(t)->parent_dev) { 2451315a1350SMichael S. Tsirkin ++slot_depth; 2452315a1350SMichael S. Tsirkin } 2453315a1350SMichael S. Tsirkin 2454568f0690SDavid Gibson path_len = root_bus_len + slot_len * slot_depth; 2455315a1350SMichael S. Tsirkin 2456315a1350SMichael S. Tsirkin /* Allocate memory, fill in the terminating null byte. */ 2457315a1350SMichael S. Tsirkin path = g_malloc(path_len + 1 /* For '\0' */); 2458315a1350SMichael S. Tsirkin path[path_len] = '\0'; 2459315a1350SMichael S. Tsirkin 2460568f0690SDavid Gibson memcpy(path, root_bus_path, root_bus_len); 2461315a1350SMichael S. Tsirkin 2462315a1350SMichael S. Tsirkin /* Fill in slot numbers. We walk up from device to root, so need to print 2463315a1350SMichael S. Tsirkin * them in the reverse order, last to first. */ 2464315a1350SMichael S. Tsirkin p = path + path_len; 2465fd56e061SDavid Gibson for (t = d; t; t = pci_get_bus(t)->parent_dev) { 2466315a1350SMichael S. Tsirkin p -= slot_len; 2467315a1350SMichael S. Tsirkin s = snprintf(slot, sizeof slot, ":%02x.%x", 2468315a1350SMichael S. Tsirkin PCI_SLOT(t->devfn), PCI_FUNC(t->devfn)); 2469315a1350SMichael S. Tsirkin assert(s == slot_len); 2470315a1350SMichael S. Tsirkin memcpy(p, slot, slot_len); 2471315a1350SMichael S. Tsirkin } 2472315a1350SMichael S. Tsirkin 2473315a1350SMichael S. Tsirkin return path; 2474315a1350SMichael S. Tsirkin } 2475315a1350SMichael S. Tsirkin 2476315a1350SMichael S. Tsirkin static int pci_qdev_find_recursive(PCIBus *bus, 2477315a1350SMichael S. Tsirkin const char *id, PCIDevice **pdev) 2478315a1350SMichael S. Tsirkin { 2479315a1350SMichael S. Tsirkin DeviceState *qdev = qdev_find_recursive(&bus->qbus, id); 2480315a1350SMichael S. Tsirkin if (!qdev) { 2481315a1350SMichael S. Tsirkin return -ENODEV; 2482315a1350SMichael S. Tsirkin } 2483315a1350SMichael S. Tsirkin 2484315a1350SMichael S. Tsirkin /* roughly check if given qdev is pci device */ 2485315a1350SMichael S. Tsirkin if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) { 2486315a1350SMichael S. Tsirkin *pdev = PCI_DEVICE(qdev); 2487315a1350SMichael S. Tsirkin return 0; 2488315a1350SMichael S. Tsirkin } 2489315a1350SMichael S. Tsirkin return -EINVAL; 2490315a1350SMichael S. Tsirkin } 2491315a1350SMichael S. Tsirkin 2492315a1350SMichael S. Tsirkin int pci_qdev_find_device(const char *id, PCIDevice **pdev) 2493315a1350SMichael S. Tsirkin { 24947588e2b0SDavid Gibson PCIHostState *host_bridge; 2495315a1350SMichael S. Tsirkin int rc = -ENODEV; 2496315a1350SMichael S. Tsirkin 24977588e2b0SDavid Gibson QLIST_FOREACH(host_bridge, &pci_host_bridges, next) { 24987588e2b0SDavid Gibson int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev); 2499315a1350SMichael S. Tsirkin if (!tmp) { 2500315a1350SMichael S. Tsirkin rc = 0; 2501315a1350SMichael S. Tsirkin break; 2502315a1350SMichael S. Tsirkin } 2503315a1350SMichael S. Tsirkin if (tmp != -ENODEV) { 2504315a1350SMichael S. Tsirkin rc = tmp; 2505315a1350SMichael S. Tsirkin } 2506315a1350SMichael S. Tsirkin } 2507315a1350SMichael S. Tsirkin 2508315a1350SMichael S. Tsirkin return rc; 2509315a1350SMichael S. Tsirkin } 2510315a1350SMichael S. Tsirkin 2511315a1350SMichael S. Tsirkin MemoryRegion *pci_address_space(PCIDevice *dev) 2512315a1350SMichael S. Tsirkin { 2513fd56e061SDavid Gibson return pci_get_bus(dev)->address_space_mem; 2514315a1350SMichael S. Tsirkin } 2515315a1350SMichael S. Tsirkin 2516315a1350SMichael S. Tsirkin MemoryRegion *pci_address_space_io(PCIDevice *dev) 2517315a1350SMichael S. Tsirkin { 2518fd56e061SDavid Gibson return pci_get_bus(dev)->address_space_io; 2519315a1350SMichael S. Tsirkin } 2520315a1350SMichael S. Tsirkin 2521315a1350SMichael S. Tsirkin static void pci_device_class_init(ObjectClass *klass, void *data) 2522315a1350SMichael S. Tsirkin { 2523315a1350SMichael S. Tsirkin DeviceClass *k = DEVICE_CLASS(klass); 25247ee6c1e1SMarkus Armbruster 2525133e9b22SMarkus Armbruster k->realize = pci_qdev_realize; 2526133e9b22SMarkus Armbruster k->unrealize = pci_qdev_unrealize; 2527315a1350SMichael S. Tsirkin k->bus_type = TYPE_PCI_BUS; 25284f67d30bSMarc-André Lureau device_class_set_props(k, pci_props); 2529315a1350SMichael S. Tsirkin } 2530315a1350SMichael S. Tsirkin 25312fefa16cSEduardo Habkost static void pci_device_class_base_init(ObjectClass *klass, void *data) 25322fefa16cSEduardo Habkost { 25332fefa16cSEduardo Habkost if (!object_class_is_abstract(klass)) { 25342fefa16cSEduardo Habkost ObjectClass *conventional = 25352fefa16cSEduardo Habkost object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE); 25362fefa16cSEduardo Habkost ObjectClass *pcie = 25372fefa16cSEduardo Habkost object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE); 2538d86d3019SBen Widawsky ObjectClass *cxl = 2539d86d3019SBen Widawsky object_class_dynamic_cast(klass, INTERFACE_CXL_DEVICE); 2540d86d3019SBen Widawsky assert(conventional || pcie || cxl); 25412fefa16cSEduardo Habkost } 25422fefa16cSEduardo Habkost } 25432fefa16cSEduardo Habkost 25449eda7d37SAlexey Kardashevskiy AddressSpace *pci_device_iommu_address_space(PCIDevice *dev) 25459eda7d37SAlexey Kardashevskiy { 2546fd56e061SDavid Gibson PCIBus *bus = pci_get_bus(dev); 25475af2ae23SBenjamin Herrenschmidt PCIBus *iommu_bus = bus; 254877ef8f8dSAlex Williamson uint8_t devfn = dev->devfn; 25499eda7d37SAlexey Kardashevskiy 25505af2ae23SBenjamin Herrenschmidt while (iommu_bus && !iommu_bus->iommu_fn && iommu_bus->parent_dev) { 255177ef8f8dSAlex Williamson PCIBus *parent_bus = pci_get_bus(iommu_bus->parent_dev); 255277ef8f8dSAlex Williamson 255377ef8f8dSAlex Williamson /* 255477ef8f8dSAlex Williamson * The requester ID of the provided device may be aliased, as seen from 255577ef8f8dSAlex Williamson * the IOMMU, due to topology limitations. The IOMMU relies on a 255677ef8f8dSAlex Williamson * requester ID to provide a unique AddressSpace for devices, but 255777ef8f8dSAlex Williamson * conventional PCI buses pre-date such concepts. Instead, the PCIe- 255877ef8f8dSAlex Williamson * to-PCI bridge creates and accepts transactions on behalf of down- 255977ef8f8dSAlex Williamson * stream devices. When doing so, all downstream devices are masked 256077ef8f8dSAlex Williamson * (aliased) behind a single requester ID. The requester ID used 256177ef8f8dSAlex Williamson * depends on the format of the bridge devices. Proper PCIe-to-PCI 256277ef8f8dSAlex Williamson * bridges, with a PCIe capability indicating such, follow the 256377ef8f8dSAlex Williamson * guidelines of chapter 2.3 of the PCIe-to-PCI/X bridge specification, 256477ef8f8dSAlex Williamson * where the bridge uses the seconary bus as the bridge portion of the 256577ef8f8dSAlex Williamson * requester ID and devfn of 00.0. For other bridges, typically those 256677ef8f8dSAlex Williamson * found on the root complex such as the dmi-to-pci-bridge, we follow 256777ef8f8dSAlex Williamson * the convention of typical bare-metal hardware, which uses the 256877ef8f8dSAlex Williamson * requester ID of the bridge itself. There are device specific 256977ef8f8dSAlex Williamson * exceptions to these rules, but these are the defaults that the 257077ef8f8dSAlex Williamson * Linux kernel uses when determining DMA aliases itself and believed 257177ef8f8dSAlex Williamson * to be true for the bare metal equivalents of the devices emulated 257277ef8f8dSAlex Williamson * in QEMU. 257377ef8f8dSAlex Williamson */ 257477ef8f8dSAlex Williamson if (!pci_bus_is_express(iommu_bus)) { 257577ef8f8dSAlex Williamson PCIDevice *parent = iommu_bus->parent_dev; 257677ef8f8dSAlex Williamson 257777ef8f8dSAlex Williamson if (pci_is_express(parent) && 257877ef8f8dSAlex Williamson pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) { 257977ef8f8dSAlex Williamson devfn = PCI_DEVFN(0, 0); 258077ef8f8dSAlex Williamson bus = iommu_bus; 258177ef8f8dSAlex Williamson } else { 258277ef8f8dSAlex Williamson devfn = parent->devfn; 258377ef8f8dSAlex Williamson bus = parent_bus; 258477ef8f8dSAlex Williamson } 258577ef8f8dSAlex Williamson } 258677ef8f8dSAlex Williamson 258777ef8f8dSAlex Williamson iommu_bus = parent_bus; 25889eda7d37SAlexey Kardashevskiy } 25892d64b7bbSXingang Wang if (!pci_bus_bypass_iommu(bus) && iommu_bus && iommu_bus->iommu_fn) { 259077ef8f8dSAlex Williamson return iommu_bus->iommu_fn(bus, iommu_bus->iommu_opaque, devfn); 25919eda7d37SAlexey Kardashevskiy } 25929eda7d37SAlexey Kardashevskiy return &address_space_memory; 25939eda7d37SAlexey Kardashevskiy } 25949eda7d37SAlexey Kardashevskiy 2595e00387d5SAvi Kivity void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque) 2596315a1350SMichael S. Tsirkin { 2597e00387d5SAvi Kivity bus->iommu_fn = fn; 2598e00387d5SAvi Kivity bus->iommu_opaque = opaque; 2599315a1350SMichael S. Tsirkin } 2600315a1350SMichael S. Tsirkin 260143864069SMichael S. Tsirkin static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque) 260243864069SMichael S. Tsirkin { 260343864069SMichael S. Tsirkin Range *range = opaque; 260443864069SMichael S. Tsirkin uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND); 260577d6f4eaSMichael S. Tsirkin int i; 260643864069SMichael S. Tsirkin 260743864069SMichael S. Tsirkin if (!(cmd & PCI_COMMAND_MEMORY)) { 260843864069SMichael S. Tsirkin return; 260943864069SMichael S. Tsirkin } 261043864069SMichael S. Tsirkin 2611ad494274SIgor Mammedov if (IS_PCI_BRIDGE(dev)) { 261243864069SMichael S. Tsirkin pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); 261343864069SMichael S. Tsirkin pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH); 261443864069SMichael S. Tsirkin 261543864069SMichael S. Tsirkin base = MAX(base, 0x1ULL << 32); 261643864069SMichael S. Tsirkin 261743864069SMichael S. Tsirkin if (limit >= base) { 261843864069SMichael S. Tsirkin Range pref_range; 2619a0efbf16SMarkus Armbruster range_set_bounds(&pref_range, base, limit); 262043864069SMichael S. Tsirkin range_extend(range, &pref_range); 262143864069SMichael S. Tsirkin } 262243864069SMichael S. Tsirkin } 262377d6f4eaSMichael S. Tsirkin for (i = 0; i < PCI_NUM_REGIONS; ++i) { 262477d6f4eaSMichael S. Tsirkin PCIIORegion *r = &dev->io_regions[i]; 2625a0efbf16SMarkus Armbruster pcibus_t lob, upb; 262643864069SMichael S. Tsirkin Range region_range; 262743864069SMichael S. Tsirkin 262877d6f4eaSMichael S. Tsirkin if (!r->size || 262977d6f4eaSMichael S. Tsirkin (r->type & PCI_BASE_ADDRESS_SPACE_IO) || 263077d6f4eaSMichael S. Tsirkin !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) { 263143864069SMichael S. Tsirkin continue; 263243864069SMichael S. Tsirkin } 263377d6f4eaSMichael S. Tsirkin 2634a0efbf16SMarkus Armbruster lob = pci_bar_address(dev, i, r->type, r->size); 2635a0efbf16SMarkus Armbruster upb = lob + r->size - 1; 2636a0efbf16SMarkus Armbruster if (lob == PCI_BAR_UNMAPPED) { 263777d6f4eaSMichael S. Tsirkin continue; 263877d6f4eaSMichael S. Tsirkin } 263943864069SMichael S. Tsirkin 2640a0efbf16SMarkus Armbruster lob = MAX(lob, 0x1ULL << 32); 264143864069SMichael S. Tsirkin 2642a0efbf16SMarkus Armbruster if (upb >= lob) { 2643a0efbf16SMarkus Armbruster range_set_bounds(®ion_range, lob, upb); 264443864069SMichael S. Tsirkin range_extend(range, ®ion_range); 264543864069SMichael S. Tsirkin } 264643864069SMichael S. Tsirkin } 264743864069SMichael S. Tsirkin } 264843864069SMichael S. Tsirkin 264943864069SMichael S. Tsirkin void pci_bus_get_w64_range(PCIBus *bus, Range *range) 265043864069SMichael S. Tsirkin { 2651a0efbf16SMarkus Armbruster range_make_empty(range); 265243864069SMichael S. Tsirkin pci_for_each_device_under_bus(bus, pci_dev_get_w64, range); 265343864069SMichael S. Tsirkin } 265443864069SMichael S. Tsirkin 26553f1e1478SCao jin static bool pcie_has_upstream_port(PCIDevice *dev) 26563f1e1478SCao jin { 2657fd56e061SDavid Gibson PCIDevice *parent_dev = pci_bridge_get_device(pci_get_bus(dev)); 26583f1e1478SCao jin 26593f1e1478SCao jin /* Device associated with an upstream port. 26603f1e1478SCao jin * As there are several types of these, it's easier to check the 26613f1e1478SCao jin * parent device: upstream ports are always connected to 26623f1e1478SCao jin * root or downstream ports. 26633f1e1478SCao jin */ 26643f1e1478SCao jin return parent_dev && 26653f1e1478SCao jin pci_is_express(parent_dev) && 26663f1e1478SCao jin parent_dev->exp.exp_cap && 26673f1e1478SCao jin (pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_ROOT_PORT || 26683f1e1478SCao jin pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_DOWNSTREAM); 26693f1e1478SCao jin } 26703f1e1478SCao jin 26713f1e1478SCao jin PCIDevice *pci_get_function_0(PCIDevice *pci_dev) 26723f1e1478SCao jin { 2673fd56e061SDavid Gibson PCIBus *bus = pci_get_bus(pci_dev); 2674fd56e061SDavid Gibson 26753f1e1478SCao jin if(pcie_has_upstream_port(pci_dev)) { 26763f1e1478SCao jin /* With an upstream PCIe port, we only support 1 device at slot 0 */ 2677fd56e061SDavid Gibson return bus->devices[0]; 26783f1e1478SCao jin } else { 26793f1e1478SCao jin /* Other bus types might support multiple devices at slots 0-31 */ 2680fd56e061SDavid Gibson return bus->devices[PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 0)]; 26813f1e1478SCao jin } 26823f1e1478SCao jin } 26833f1e1478SCao jin 2684e1d4fb2dSPeter Xu MSIMessage pci_get_msi_message(PCIDevice *dev, int vector) 2685e1d4fb2dSPeter Xu { 2686e1d4fb2dSPeter Xu MSIMessage msg; 2687e1d4fb2dSPeter Xu if (msix_enabled(dev)) { 2688e1d4fb2dSPeter Xu msg = msix_get_message(dev, vector); 2689e1d4fb2dSPeter Xu } else if (msi_enabled(dev)) { 2690e1d4fb2dSPeter Xu msg = msi_get_message(dev, vector); 2691e1d4fb2dSPeter Xu } else { 2692e1d4fb2dSPeter Xu /* Should never happen */ 2693e1d4fb2dSPeter Xu error_report("%s: unknown interrupt type", __func__); 2694e1d4fb2dSPeter Xu abort(); 2695e1d4fb2dSPeter Xu } 2696e1d4fb2dSPeter Xu return msg; 2697e1d4fb2dSPeter Xu } 2698e1d4fb2dSPeter Xu 269923786d13SGerd Hoffmann void pci_set_power(PCIDevice *d, bool state) 270023786d13SGerd Hoffmann { 270123786d13SGerd Hoffmann if (d->has_power == state) { 270223786d13SGerd Hoffmann return; 270323786d13SGerd Hoffmann } 270423786d13SGerd Hoffmann 270523786d13SGerd Hoffmann d->has_power = state; 270623786d13SGerd Hoffmann pci_update_mappings(d); 270723786d13SGerd Hoffmann memory_region_set_enabled(&d->bus_master_enable_region, 270823786d13SGerd Hoffmann (pci_get_word(d->config + PCI_COMMAND) 270923786d13SGerd Hoffmann & PCI_COMMAND_MASTER) && d->has_power); 271023786d13SGerd Hoffmann if (!d->has_power) { 271123786d13SGerd Hoffmann pci_device_reset(d); 271223786d13SGerd Hoffmann } 271323786d13SGerd Hoffmann } 271423786d13SGerd Hoffmann 27158c43a6f0SAndreas Färber static const TypeInfo pci_device_type_info = { 2716315a1350SMichael S. Tsirkin .name = TYPE_PCI_DEVICE, 2717315a1350SMichael S. Tsirkin .parent = TYPE_DEVICE, 2718315a1350SMichael S. Tsirkin .instance_size = sizeof(PCIDevice), 2719315a1350SMichael S. Tsirkin .abstract = true, 2720315a1350SMichael S. Tsirkin .class_size = sizeof(PCIDeviceClass), 2721315a1350SMichael S. Tsirkin .class_init = pci_device_class_init, 27222fefa16cSEduardo Habkost .class_base_init = pci_device_class_base_init, 2723315a1350SMichael S. Tsirkin }; 2724315a1350SMichael S. Tsirkin 2725315a1350SMichael S. Tsirkin static void pci_register_types(void) 2726315a1350SMichael S. Tsirkin { 2727315a1350SMichael S. Tsirkin type_register_static(&pci_bus_info); 27283a861c46SAlex Williamson type_register_static(&pcie_bus_info); 27294f8db871SBen Widawsky type_register_static(&cxl_bus_info); 2730619f02aeSEduardo Habkost type_register_static(&conventional_pci_interface_info); 2731cf04aba2SBen Widawsky type_register_static(&cxl_interface_info); 2732619f02aeSEduardo Habkost type_register_static(&pcie_interface_info); 2733315a1350SMichael S. Tsirkin type_register_static(&pci_device_type_info); 2734315a1350SMichael S. Tsirkin } 2735315a1350SMichael S. Tsirkin 2736315a1350SMichael S. Tsirkin type_init(pci_register_types) 2737