xref: /openbmc/qemu/hw/pci/pci.c (revision 36f18c69)
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"
3783c9089eSPaolo Bonzini #include "monitor/monitor.h"
381422e32dSPaolo Bonzini #include "net/net.h"
39b58c5c2dSMarkus Armbruster #include "sysemu/numa.h"
4046517dd4SMarkus Armbruster #include "sysemu/sysemu.h"
41c759b24fSMichael S. Tsirkin #include "hw/loader.h"
42d49b6836SMarkus Armbruster #include "qemu/error-report.h"
431de7afc9SPaolo Bonzini #include "qemu/range.h"
447828d750SDon Koch #include "trace.h"
45c759b24fSMichael S. Tsirkin #include "hw/pci/msi.h"
46c759b24fSMichael S. Tsirkin #include "hw/pci/msix.h"
475e954943SIgor Mammedov #include "hw/hotplug.h"
48e4024630SLaurent Vivier #include "hw/boards.h"
49e688df6bSMarkus Armbruster #include "qapi/error.h"
5061c7f987SPhilippe Mathieu-Daudé #include "qapi/qapi-commands-pci.h"
51f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
52315a1350SMichael S. Tsirkin 
53315a1350SMichael S. Tsirkin //#define DEBUG_PCI
54315a1350SMichael S. Tsirkin #ifdef DEBUG_PCI
55315a1350SMichael S. Tsirkin # define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
56315a1350SMichael S. Tsirkin #else
57315a1350SMichael S. Tsirkin # define PCI_DPRINTF(format, ...)       do { } while (0)
58315a1350SMichael S. Tsirkin #endif
59315a1350SMichael S. Tsirkin 
6088c725c7SCornelia Huck bool pci_available = true;
6188c725c7SCornelia Huck 
62315a1350SMichael S. Tsirkin static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
63315a1350SMichael S. Tsirkin static char *pcibus_get_dev_path(DeviceState *dev);
64315a1350SMichael S. Tsirkin static char *pcibus_get_fw_dev_path(DeviceState *dev);
65dcc20931SPaolo Bonzini static void pcibus_reset(BusState *qbus);
66315a1350SMichael S. Tsirkin 
67315a1350SMichael S. Tsirkin static Property pci_props[] = {
68315a1350SMichael S. Tsirkin     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
69315a1350SMichael S. Tsirkin     DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
7008b1df8fSPaolo Bonzini     DEFINE_PROP_UINT32("romsize", PCIDevice, romsize, -1),
71315a1350SMichael S. Tsirkin     DEFINE_PROP_UINT32("rombar",  PCIDevice, rom_bar, 1),
72315a1350SMichael S. Tsirkin     DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
73315a1350SMichael S. Tsirkin                     QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
746b449540SMichael S. Tsirkin     DEFINE_PROP_BIT("x-pcie-lnksta-dllla", PCIDevice, cap_present,
756b449540SMichael S. Tsirkin                     QEMU_PCIE_LNKSTA_DLLLA_BITNR, true),
76f03d8ea3SMarcel Apfelbaum     DEFINE_PROP_BIT("x-pcie-extcap-init", PCIDevice, cap_present,
77f03d8ea3SMarcel Apfelbaum                     QEMU_PCIE_EXTCAP_INIT_BITNR, true),
784f5b6a05SJens Freimann     DEFINE_PROP_STRING("failover_pair_id", PCIDevice,
794f5b6a05SJens Freimann                        failover_pair_id),
80b32bd763SIgor Mammedov     DEFINE_PROP_UINT32("acpi-index",  PCIDevice, acpi_index, 0),
81315a1350SMichael S. Tsirkin     DEFINE_PROP_END_OF_LIST()
82315a1350SMichael S. Tsirkin };
83315a1350SMichael S. Tsirkin 
84d2f69df7SBandan Das static const VMStateDescription vmstate_pcibus = {
85d2f69df7SBandan Das     .name = "PCIBUS",
86d2f69df7SBandan Das     .version_id = 1,
87d2f69df7SBandan Das     .minimum_version_id = 1,
88d2f69df7SBandan Das     .fields = (VMStateField[]) {
89d2164ad3SHalil Pasic         VMSTATE_INT32_EQUAL(nirq, PCIBus, NULL),
90d2f69df7SBandan Das         VMSTATE_VARRAY_INT32(irq_count, PCIBus,
91d2f69df7SBandan Das                              nirq, 0, vmstate_info_int32,
92d2f69df7SBandan Das                              int32_t),
93d2f69df7SBandan Das         VMSTATE_END_OF_LIST()
94d2f69df7SBandan Das     }
95d2f69df7SBandan Das };
96d2f69df7SBandan Das 
97b86eacb8SMarcel Apfelbaum static void pci_init_bus_master(PCIDevice *pci_dev)
98b86eacb8SMarcel Apfelbaum {
99b86eacb8SMarcel Apfelbaum     AddressSpace *dma_as = pci_device_iommu_address_space(pci_dev);
100b86eacb8SMarcel Apfelbaum 
101b86eacb8SMarcel Apfelbaum     memory_region_init_alias(&pci_dev->bus_master_enable_region,
102b86eacb8SMarcel Apfelbaum                              OBJECT(pci_dev), "bus master",
103b86eacb8SMarcel Apfelbaum                              dma_as->root, 0, memory_region_size(dma_as->root));
104b86eacb8SMarcel Apfelbaum     memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
1053716d590SJason Wang     memory_region_add_subregion(&pci_dev->bus_master_container_region, 0,
1063716d590SJason Wang                                 &pci_dev->bus_master_enable_region);
107b86eacb8SMarcel Apfelbaum }
108b86eacb8SMarcel Apfelbaum 
109b86eacb8SMarcel Apfelbaum static void pcibus_machine_done(Notifier *notifier, void *data)
110b86eacb8SMarcel Apfelbaum {
111b86eacb8SMarcel Apfelbaum     PCIBus *bus = container_of(notifier, PCIBus, machine_done);
112b86eacb8SMarcel Apfelbaum     int i;
113b86eacb8SMarcel Apfelbaum 
114b86eacb8SMarcel Apfelbaum     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
115b86eacb8SMarcel Apfelbaum         if (bus->devices[i]) {
116b86eacb8SMarcel Apfelbaum             pci_init_bus_master(bus->devices[i]);
117b86eacb8SMarcel Apfelbaum         }
118b86eacb8SMarcel Apfelbaum     }
119b86eacb8SMarcel Apfelbaum }
120b86eacb8SMarcel Apfelbaum 
121d2f69df7SBandan Das static void pci_bus_realize(BusState *qbus, Error **errp)
122d2f69df7SBandan Das {
123d2f69df7SBandan Das     PCIBus *bus = PCI_BUS(qbus);
124d2f69df7SBandan Das 
125b86eacb8SMarcel Apfelbaum     bus->machine_done.notify = pcibus_machine_done;
126b86eacb8SMarcel Apfelbaum     qemu_add_machine_init_done_notifier(&bus->machine_done);
127b86eacb8SMarcel Apfelbaum 
1281df2c9a2SPeter Xu     vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, &vmstate_pcibus, bus);
129d2f69df7SBandan Das }
130d2f69df7SBandan Das 
1312f57db8aSDavid Gibson static void pcie_bus_realize(BusState *qbus, Error **errp)
1322f57db8aSDavid Gibson {
1332f57db8aSDavid Gibson     PCIBus *bus = PCI_BUS(qbus);
134b52fa0eaSPhilippe Mathieu-Daudé     Error *local_err = NULL;
1352f57db8aSDavid Gibson 
136b52fa0eaSPhilippe Mathieu-Daudé     pci_bus_realize(qbus, &local_err);
137b52fa0eaSPhilippe Mathieu-Daudé     if (local_err) {
138b52fa0eaSPhilippe Mathieu-Daudé         error_propagate(errp, local_err);
139b52fa0eaSPhilippe Mathieu-Daudé         return;
140b52fa0eaSPhilippe Mathieu-Daudé     }
1412f57db8aSDavid Gibson 
1422f57db8aSDavid Gibson     /*
1432f57db8aSDavid Gibson      * A PCI-E bus can support extended config space if it's the root
1442f57db8aSDavid Gibson      * bus, or if the bus/bridge above it does as well
1452f57db8aSDavid Gibson      */
1462f57db8aSDavid Gibson     if (pci_bus_is_root(bus)) {
1472f57db8aSDavid Gibson         bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE;
1482f57db8aSDavid Gibson     } else {
1492f57db8aSDavid Gibson         PCIBus *parent_bus = pci_get_bus(bus->parent_dev);
1502f57db8aSDavid Gibson 
1512f57db8aSDavid Gibson         if (pci_bus_allows_extended_config_space(parent_bus)) {
1522f57db8aSDavid Gibson             bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE;
1532f57db8aSDavid Gibson         }
1542f57db8aSDavid Gibson     }
1552f57db8aSDavid Gibson }
1562f57db8aSDavid Gibson 
157b69c3c21SMarkus Armbruster static void pci_bus_unrealize(BusState *qbus)
158d2f69df7SBandan Das {
159d2f69df7SBandan Das     PCIBus *bus = PCI_BUS(qbus);
160d2f69df7SBandan Das 
161b86eacb8SMarcel Apfelbaum     qemu_remove_machine_init_done_notifier(&bus->machine_done);
162b86eacb8SMarcel Apfelbaum 
163d2f69df7SBandan Das     vmstate_unregister(NULL, &vmstate_pcibus, bus);
164d2f69df7SBandan Das }
165d2f69df7SBandan Das 
166602141d9SMarcel Apfelbaum static int pcibus_num(PCIBus *bus)
167602141d9SMarcel Apfelbaum {
168b0e5196aSDavid Gibson     if (pci_bus_is_root(bus)) {
169602141d9SMarcel Apfelbaum         return 0; /* pci host bridge */
170602141d9SMarcel Apfelbaum     }
171602141d9SMarcel Apfelbaum     return bus->parent_dev->config[PCI_SECONDARY_BUS];
172602141d9SMarcel Apfelbaum }
173602141d9SMarcel Apfelbaum 
1746a3042b2SMarcel Apfelbaum static uint16_t pcibus_numa_node(PCIBus *bus)
1756a3042b2SMarcel Apfelbaum {
1766a3042b2SMarcel Apfelbaum     return NUMA_NODE_UNASSIGNED;
1776a3042b2SMarcel Apfelbaum }
1786a3042b2SMarcel Apfelbaum 
179315a1350SMichael S. Tsirkin static void pci_bus_class_init(ObjectClass *klass, void *data)
180315a1350SMichael S. Tsirkin {
181315a1350SMichael S. Tsirkin     BusClass *k = BUS_CLASS(klass);
182ce6a28eeSMarcel Apfelbaum     PCIBusClass *pbc = PCI_BUS_CLASS(klass);
183315a1350SMichael S. Tsirkin 
184315a1350SMichael S. Tsirkin     k->print_dev = pcibus_dev_print;
185315a1350SMichael S. Tsirkin     k->get_dev_path = pcibus_get_dev_path;
186315a1350SMichael S. Tsirkin     k->get_fw_dev_path = pcibus_get_fw_dev_path;
187d2f69df7SBandan Das     k->realize = pci_bus_realize;
188d2f69df7SBandan Das     k->unrealize = pci_bus_unrealize;
189315a1350SMichael S. Tsirkin     k->reset = pcibus_reset;
190ce6a28eeSMarcel Apfelbaum 
191602141d9SMarcel Apfelbaum     pbc->bus_num = pcibus_num;
1926a3042b2SMarcel Apfelbaum     pbc->numa_node = pcibus_numa_node;
193315a1350SMichael S. Tsirkin }
194315a1350SMichael S. Tsirkin 
195315a1350SMichael S. Tsirkin static const TypeInfo pci_bus_info = {
196315a1350SMichael S. Tsirkin     .name = TYPE_PCI_BUS,
197315a1350SMichael S. Tsirkin     .parent = TYPE_BUS,
198315a1350SMichael S. Tsirkin     .instance_size = sizeof(PCIBus),
199ce6a28eeSMarcel Apfelbaum     .class_size = sizeof(PCIBusClass),
200315a1350SMichael S. Tsirkin     .class_init = pci_bus_class_init,
201315a1350SMichael S. Tsirkin };
202315a1350SMichael S. Tsirkin 
203cf04aba2SBen Widawsky static const TypeInfo cxl_interface_info = {
204cf04aba2SBen Widawsky     .name          = INTERFACE_CXL_DEVICE,
205cf04aba2SBen Widawsky     .parent        = TYPE_INTERFACE,
206cf04aba2SBen Widawsky };
207cf04aba2SBen Widawsky 
208619f02aeSEduardo Habkost static const TypeInfo pcie_interface_info = {
209619f02aeSEduardo Habkost     .name          = INTERFACE_PCIE_DEVICE,
210619f02aeSEduardo Habkost     .parent        = TYPE_INTERFACE,
211619f02aeSEduardo Habkost };
212619f02aeSEduardo Habkost 
213619f02aeSEduardo Habkost static const TypeInfo conventional_pci_interface_info = {
214619f02aeSEduardo Habkost     .name          = INTERFACE_CONVENTIONAL_PCI_DEVICE,
215619f02aeSEduardo Habkost     .parent        = TYPE_INTERFACE,
216619f02aeSEduardo Habkost };
217619f02aeSEduardo Habkost 
2181c685a90SGreg Kurz static void pcie_bus_class_init(ObjectClass *klass, void *data)
2191c685a90SGreg Kurz {
2202f57db8aSDavid Gibson     BusClass *k = BUS_CLASS(klass);
2211c685a90SGreg Kurz 
2222f57db8aSDavid Gibson     k->realize = pcie_bus_realize;
2231c685a90SGreg Kurz }
2241c685a90SGreg Kurz 
2253a861c46SAlex Williamson static const TypeInfo pcie_bus_info = {
2263a861c46SAlex Williamson     .name = TYPE_PCIE_BUS,
2273a861c46SAlex Williamson     .parent = TYPE_PCI_BUS,
2281c685a90SGreg Kurz     .class_init = pcie_bus_class_init,
2293a861c46SAlex Williamson };
2303a861c46SAlex Williamson 
2314f8db871SBen Widawsky static const TypeInfo cxl_bus_info = {
2324f8db871SBen Widawsky     .name       = TYPE_CXL_BUS,
2334f8db871SBen Widawsky     .parent     = TYPE_PCIE_BUS,
2344f8db871SBen Widawsky     .class_init = pcie_bus_class_init,
2354f8db871SBen Widawsky };
2364f8db871SBen Widawsky 
237315a1350SMichael S. Tsirkin static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
238315a1350SMichael S. Tsirkin static void pci_update_mappings(PCIDevice *d);
239d98f08f5SMarcel Apfelbaum static void pci_irq_handler(void *opaque, int irq_num, int level);
240133e9b22SMarkus Armbruster static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, Error **);
241315a1350SMichael S. Tsirkin static void pci_del_option_rom(PCIDevice *pdev);
242315a1350SMichael S. Tsirkin 
243315a1350SMichael S. Tsirkin static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
244315a1350SMichael S. Tsirkin static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
245315a1350SMichael S. Tsirkin 
2467588e2b0SDavid Gibson static QLIST_HEAD(, PCIHostState) pci_host_bridges;
247315a1350SMichael S. Tsirkin 
248cf8c704dSMichael Roth int pci_bar(PCIDevice *d, int reg)
249315a1350SMichael S. Tsirkin {
250315a1350SMichael S. Tsirkin     uint8_t type;
251315a1350SMichael S. Tsirkin 
2527c0fa8dfSKnut Omang     /* PCIe virtual functions do not have their own BARs */
2537c0fa8dfSKnut Omang     assert(!pci_is_vf(d));
2547c0fa8dfSKnut Omang 
255315a1350SMichael S. Tsirkin     if (reg != PCI_ROM_SLOT)
256315a1350SMichael S. Tsirkin         return PCI_BASE_ADDRESS_0 + reg * 4;
257315a1350SMichael S. Tsirkin 
258315a1350SMichael S. Tsirkin     type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
259315a1350SMichael S. Tsirkin     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
260315a1350SMichael S. Tsirkin }
261315a1350SMichael S. Tsirkin 
262315a1350SMichael S. Tsirkin static inline int pci_irq_state(PCIDevice *d, int irq_num)
263315a1350SMichael S. Tsirkin {
264315a1350SMichael S. Tsirkin         return (d->irq_state >> irq_num) & 0x1;
265315a1350SMichael S. Tsirkin }
266315a1350SMichael S. Tsirkin 
267315a1350SMichael S. Tsirkin static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
268315a1350SMichael S. Tsirkin {
269315a1350SMichael S. Tsirkin         d->irq_state &= ~(0x1 << irq_num);
270315a1350SMichael S. Tsirkin         d->irq_state |= level << irq_num;
271315a1350SMichael S. Tsirkin }
272315a1350SMichael S. Tsirkin 
273b06fe3e7SPhilippe Mathieu-Daudé static void pci_bus_change_irq_level(PCIBus *bus, int irq_num, int change)
274b06fe3e7SPhilippe Mathieu-Daudé {
275459ca8bfSMark Cave-Ayland     assert(irq_num >= 0);
276459ca8bfSMark Cave-Ayland     assert(irq_num < bus->nirq);
277b06fe3e7SPhilippe Mathieu-Daudé     bus->irq_count[irq_num] += change;
278b06fe3e7SPhilippe Mathieu-Daudé     bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
279b06fe3e7SPhilippe Mathieu-Daudé }
280b06fe3e7SPhilippe Mathieu-Daudé 
281315a1350SMichael S. Tsirkin static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
282315a1350SMichael S. Tsirkin {
283315a1350SMichael S. Tsirkin     PCIBus *bus;
284315a1350SMichael S. Tsirkin     for (;;) {
285fd56e061SDavid Gibson         bus = pci_get_bus(pci_dev);
286315a1350SMichael S. Tsirkin         irq_num = bus->map_irq(pci_dev, irq_num);
287315a1350SMichael S. Tsirkin         if (bus->set_irq)
288315a1350SMichael S. Tsirkin             break;
289315a1350SMichael S. Tsirkin         pci_dev = bus->parent_dev;
290315a1350SMichael S. Tsirkin     }
291b06fe3e7SPhilippe Mathieu-Daudé     pci_bus_change_irq_level(bus, irq_num, change);
292315a1350SMichael S. Tsirkin }
293315a1350SMichael S. Tsirkin 
294315a1350SMichael S. Tsirkin int pci_bus_get_irq_level(PCIBus *bus, int irq_num)
295315a1350SMichael S. Tsirkin {
296315a1350SMichael S. Tsirkin     assert(irq_num >= 0);
297315a1350SMichael S. Tsirkin     assert(irq_num < bus->nirq);
298315a1350SMichael S. Tsirkin     return !!bus->irq_count[irq_num];
299315a1350SMichael S. Tsirkin }
300315a1350SMichael S. Tsirkin 
301315a1350SMichael S. Tsirkin /* Update interrupt status bit in config space on interrupt
302315a1350SMichael S. Tsirkin  * state change. */
303315a1350SMichael S. Tsirkin static void pci_update_irq_status(PCIDevice *dev)
304315a1350SMichael S. Tsirkin {
305315a1350SMichael S. Tsirkin     if (dev->irq_state) {
306315a1350SMichael S. Tsirkin         dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
307315a1350SMichael S. Tsirkin     } else {
308315a1350SMichael S. Tsirkin         dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
309315a1350SMichael S. Tsirkin     }
310315a1350SMichael S. Tsirkin }
311315a1350SMichael S. Tsirkin 
312315a1350SMichael S. Tsirkin void pci_device_deassert_intx(PCIDevice *dev)
313315a1350SMichael S. Tsirkin {
314315a1350SMichael S. Tsirkin     int i;
315315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
316d98f08f5SMarcel Apfelbaum         pci_irq_handler(dev, i, 0);
317315a1350SMichael S. Tsirkin     }
318315a1350SMichael S. Tsirkin }
319315a1350SMichael S. Tsirkin 
3207c0fa8dfSKnut Omang static void pci_reset_regions(PCIDevice *dev)
321315a1350SMichael S. Tsirkin {
322315a1350SMichael S. Tsirkin     int r;
3237c0fa8dfSKnut Omang     if (pci_is_vf(dev)) {
3247c0fa8dfSKnut Omang         return;
3257c0fa8dfSKnut Omang     }
326315a1350SMichael S. Tsirkin 
3277c0fa8dfSKnut Omang     for (r = 0; r < PCI_NUM_REGIONS; ++r) {
3287c0fa8dfSKnut Omang         PCIIORegion *region = &dev->io_regions[r];
3297c0fa8dfSKnut Omang         if (!region->size) {
3307c0fa8dfSKnut Omang             continue;
3317c0fa8dfSKnut Omang         }
3327c0fa8dfSKnut Omang 
3337c0fa8dfSKnut Omang         if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
3347c0fa8dfSKnut Omang             region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
3357c0fa8dfSKnut Omang             pci_set_quad(dev->config + pci_bar(dev, r), region->type);
3367c0fa8dfSKnut Omang         } else {
3377c0fa8dfSKnut Omang             pci_set_long(dev->config + pci_bar(dev, r), region->type);
3387c0fa8dfSKnut Omang         }
3397c0fa8dfSKnut Omang     }
3407c0fa8dfSKnut Omang }
3417c0fa8dfSKnut Omang 
3427c0fa8dfSKnut Omang static void pci_do_device_reset(PCIDevice *dev)
3437c0fa8dfSKnut Omang {
344315a1350SMichael S. Tsirkin     pci_device_deassert_intx(dev);
34558b59014SCole Robinson     assert(dev->irq_state == 0);
34658b59014SCole Robinson 
347315a1350SMichael S. Tsirkin     /* Clear all writable bits */
348315a1350SMichael S. Tsirkin     pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
349315a1350SMichael S. Tsirkin                                  pci_get_word(dev->wmask + PCI_COMMAND) |
350315a1350SMichael S. Tsirkin                                  pci_get_word(dev->w1cmask + PCI_COMMAND));
351315a1350SMichael S. Tsirkin     pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
352315a1350SMichael S. Tsirkin                                  pci_get_word(dev->wmask + PCI_STATUS) |
353315a1350SMichael S. Tsirkin                                  pci_get_word(dev->w1cmask + PCI_STATUS));
3547ff81d63SBALATON Zoltan     /* Some devices make bits of PCI_INTERRUPT_LINE read only */
3557ff81d63SBALATON Zoltan     pci_byte_test_and_clear_mask(dev->config + PCI_INTERRUPT_LINE,
3567ff81d63SBALATON Zoltan                               pci_get_word(dev->wmask + PCI_INTERRUPT_LINE) |
3577ff81d63SBALATON Zoltan                               pci_get_word(dev->w1cmask + PCI_INTERRUPT_LINE));
358315a1350SMichael S. Tsirkin     dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
3597c0fa8dfSKnut Omang     pci_reset_regions(dev);
360315a1350SMichael S. Tsirkin     pci_update_mappings(dev);
361315a1350SMichael S. Tsirkin 
362315a1350SMichael S. Tsirkin     msi_reset(dev);
363315a1350SMichael S. Tsirkin     msix_reset(dev);
364315a1350SMichael S. Tsirkin }
365315a1350SMichael S. Tsirkin 
366315a1350SMichael S. Tsirkin /*
367dcc20931SPaolo Bonzini  * This function is called on #RST and FLR.
368dcc20931SPaolo Bonzini  * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
369315a1350SMichael S. Tsirkin  */
370dcc20931SPaolo Bonzini void pci_device_reset(PCIDevice *dev)
371dcc20931SPaolo Bonzini {
372dcc20931SPaolo Bonzini     qdev_reset_all(&dev->qdev);
373dcc20931SPaolo Bonzini     pci_do_device_reset(dev);
374dcc20931SPaolo Bonzini }
375dcc20931SPaolo Bonzini 
376dcc20931SPaolo Bonzini /*
377dcc20931SPaolo Bonzini  * Trigger pci bus reset under a given bus.
378dcc20931SPaolo Bonzini  * Called via qbus_reset_all on RST# assert, after the devices
379dcc20931SPaolo Bonzini  * have been reset qdev_reset_all-ed already.
380dcc20931SPaolo Bonzini  */
381dcc20931SPaolo Bonzini static void pcibus_reset(BusState *qbus)
382315a1350SMichael S. Tsirkin {
38381e3e75bSPaolo Bonzini     PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
384315a1350SMichael S. Tsirkin     int i;
385315a1350SMichael S. Tsirkin 
386315a1350SMichael S. Tsirkin     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
387315a1350SMichael S. Tsirkin         if (bus->devices[i]) {
388dcc20931SPaolo Bonzini             pci_do_device_reset(bus->devices[i]);
389315a1350SMichael S. Tsirkin         }
390315a1350SMichael S. Tsirkin     }
391315a1350SMichael S. Tsirkin 
3929bdbbfc3SPaolo Bonzini     for (i = 0; i < bus->nirq; i++) {
3939bdbbfc3SPaolo Bonzini         assert(bus->irq_count[i] == 0);
3949bdbbfc3SPaolo Bonzini     }
395315a1350SMichael S. Tsirkin }
396315a1350SMichael S. Tsirkin 
3973dbc01aeSCao jin static void pci_host_bus_register(DeviceState *host)
398315a1350SMichael S. Tsirkin {
3993dbc01aeSCao jin     PCIHostState *host_bridge = PCI_HOST_BRIDGE(host);
4007588e2b0SDavid Gibson 
4017588e2b0SDavid Gibson     QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next);
402315a1350SMichael S. Tsirkin }
403315a1350SMichael S. Tsirkin 
404c13ee169SMichael Roth static void pci_host_bus_unregister(DeviceState *host)
405c13ee169SMichael Roth {
406c13ee169SMichael Roth     PCIHostState *host_bridge = PCI_HOST_BRIDGE(host);
407c13ee169SMichael Roth 
408c13ee169SMichael Roth     QLIST_REMOVE(host_bridge, next);
409c13ee169SMichael Roth }
410c13ee169SMichael Roth 
411c473d18dSDavid Gibson PCIBus *pci_device_root_bus(const PCIDevice *d)
412315a1350SMichael S. Tsirkin {
413fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(d);
414315a1350SMichael S. Tsirkin 
415ce6a28eeSMarcel Apfelbaum     while (!pci_bus_is_root(bus)) {
416ce6a28eeSMarcel Apfelbaum         d = bus->parent_dev;
417ce6a28eeSMarcel Apfelbaum         assert(d != NULL);
418ce6a28eeSMarcel Apfelbaum 
419fd56e061SDavid Gibson         bus = pci_get_bus(d);
420315a1350SMichael S. Tsirkin     }
421315a1350SMichael S. Tsirkin 
422c473d18dSDavid Gibson     return bus;
423315a1350SMichael S. Tsirkin }
424315a1350SMichael S. Tsirkin 
425568f0690SDavid Gibson const char *pci_root_bus_path(PCIDevice *dev)
426c473d18dSDavid Gibson {
427568f0690SDavid Gibson     PCIBus *rootbus = pci_device_root_bus(dev);
428568f0690SDavid Gibson     PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
429568f0690SDavid Gibson     PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge);
430c473d18dSDavid Gibson 
431568f0690SDavid Gibson     assert(host_bridge->bus == rootbus);
432568f0690SDavid Gibson 
433568f0690SDavid Gibson     if (hc->root_bus_path) {
434568f0690SDavid Gibson         return (*hc->root_bus_path)(host_bridge, rootbus);
435315a1350SMichael S. Tsirkin     }
436315a1350SMichael S. Tsirkin 
437568f0690SDavid Gibson     return rootbus->qbus.name;
438315a1350SMichael S. Tsirkin }
439315a1350SMichael S. Tsirkin 
4402d64b7bbSXingang Wang bool pci_bus_bypass_iommu(PCIBus *bus)
4412d64b7bbSXingang Wang {
4422d64b7bbSXingang Wang     PCIBus *rootbus = bus;
4432d64b7bbSXingang Wang     PCIHostState *host_bridge;
4442d64b7bbSXingang Wang 
4452d64b7bbSXingang Wang     if (!pci_bus_is_root(bus)) {
4462d64b7bbSXingang Wang         rootbus = pci_device_root_bus(bus->parent_dev);
4472d64b7bbSXingang Wang     }
4482d64b7bbSXingang Wang 
4492d64b7bbSXingang Wang     host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
4502d64b7bbSXingang Wang 
4512d64b7bbSXingang Wang     assert(host_bridge->bus == rootbus);
4522d64b7bbSXingang Wang 
4532d64b7bbSXingang Wang     return host_bridge->bypass_iommu;
4542d64b7bbSXingang Wang }
4552d64b7bbSXingang Wang 
4568d4cdf01SPeter Maydell static void pci_root_bus_internal_init(PCIBus *bus, DeviceState *parent,
457315a1350SMichael S. Tsirkin                                        MemoryRegion *address_space_mem,
458315a1350SMichael S. Tsirkin                                        MemoryRegion *address_space_io,
459315a1350SMichael S. Tsirkin                                        uint8_t devfn_min)
460315a1350SMichael S. Tsirkin {
461315a1350SMichael S. Tsirkin     assert(PCI_FUNC(devfn_min) == 0);
462315a1350SMichael S. Tsirkin     bus->devfn_min = devfn_min;
4638b884984SMark Cave-Ayland     bus->slot_reserved_mask = 0x0;
464315a1350SMichael S. Tsirkin     bus->address_space_mem = address_space_mem;
465315a1350SMichael S. Tsirkin     bus->address_space_io = address_space_io;
466b0e5196aSDavid Gibson     bus->flags |= PCI_BUS_IS_ROOT;
467315a1350SMichael S. Tsirkin 
468315a1350SMichael S. Tsirkin     /* host bridge */
469315a1350SMichael S. Tsirkin     QLIST_INIT(&bus->child);
4702b8cc89aSDavid Gibson 
4713dbc01aeSCao jin     pci_host_bus_register(parent);
472315a1350SMichael S. Tsirkin }
473315a1350SMichael S. Tsirkin 
474c13ee169SMichael Roth static void pci_bus_uninit(PCIBus *bus)
475c13ee169SMichael Roth {
476c13ee169SMichael Roth     pci_host_bus_unregister(BUS(bus)->parent);
477c13ee169SMichael Roth }
478c13ee169SMichael Roth 
4798c0bf9e2SAlex Williamson bool pci_bus_is_express(PCIBus *bus)
4808c0bf9e2SAlex Williamson {
4818c0bf9e2SAlex Williamson     return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
4828c0bf9e2SAlex Williamson }
4838c0bf9e2SAlex Williamson 
4848d4cdf01SPeter Maydell void pci_root_bus_init(PCIBus *bus, size_t bus_size, DeviceState *parent,
4854fec6404SPaolo Bonzini                        const char *name,
4864fec6404SPaolo Bonzini                        MemoryRegion *address_space_mem,
4874fec6404SPaolo Bonzini                        MemoryRegion *address_space_io,
48860a0e443SAlex Williamson                        uint8_t devfn_min, const char *typename)
4894fec6404SPaolo Bonzini {
490d637e1dcSPeter Maydell     qbus_init(bus, bus_size, typename, parent, name);
4918d4cdf01SPeter Maydell     pci_root_bus_internal_init(bus, parent, address_space_mem,
4928d4cdf01SPeter Maydell                                address_space_io, devfn_min);
4934fec6404SPaolo Bonzini }
4944fec6404SPaolo Bonzini 
4951115ff6dSDavid Gibson PCIBus *pci_root_bus_new(DeviceState *parent, const char *name,
496315a1350SMichael S. Tsirkin                          MemoryRegion *address_space_mem,
497315a1350SMichael S. Tsirkin                          MemoryRegion *address_space_io,
49860a0e443SAlex Williamson                          uint8_t devfn_min, const char *typename)
499315a1350SMichael S. Tsirkin {
500315a1350SMichael S. Tsirkin     PCIBus *bus;
501315a1350SMichael S. Tsirkin 
5029388d170SPeter Maydell     bus = PCI_BUS(qbus_new(typename, parent, name));
5038d4cdf01SPeter Maydell     pci_root_bus_internal_init(bus, parent, address_space_mem,
5048d4cdf01SPeter Maydell                                address_space_io, devfn_min);
505315a1350SMichael S. Tsirkin     return bus;
506315a1350SMichael S. Tsirkin }
507315a1350SMichael S. Tsirkin 
508c13ee169SMichael Roth void pci_root_bus_cleanup(PCIBus *bus)
509c13ee169SMichael Roth {
510c13ee169SMichael Roth     pci_bus_uninit(bus);
51107578b0aSDavid Hildenbrand     /* the caller of the unplug hotplug handler will delete this device */
512f1483b46SMarkus Armbruster     qbus_unrealize(BUS(bus));
513c13ee169SMichael Roth }
514c13ee169SMichael Roth 
515315a1350SMichael S. Tsirkin void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
516315a1350SMichael S. Tsirkin                   void *irq_opaque, int nirq)
517315a1350SMichael S. Tsirkin {
518315a1350SMichael S. Tsirkin     bus->set_irq = set_irq;
519315a1350SMichael S. Tsirkin     bus->map_irq = map_irq;
520315a1350SMichael S. Tsirkin     bus->irq_opaque = irq_opaque;
521315a1350SMichael S. Tsirkin     bus->nirq = nirq;
522315a1350SMichael S. Tsirkin     bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0]));
523315a1350SMichael S. Tsirkin }
524315a1350SMichael S. Tsirkin 
525c13ee169SMichael Roth void pci_bus_irqs_cleanup(PCIBus *bus)
526c13ee169SMichael Roth {
527c13ee169SMichael Roth     bus->set_irq = NULL;
528c13ee169SMichael Roth     bus->map_irq = NULL;
529c13ee169SMichael Roth     bus->irq_opaque = NULL;
530c13ee169SMichael Roth     bus->nirq = 0;
531c13ee169SMichael Roth     g_free(bus->irq_count);
532c13ee169SMichael Roth }
533c13ee169SMichael Roth 
5341115ff6dSDavid Gibson PCIBus *pci_register_root_bus(DeviceState *parent, const char *name,
535315a1350SMichael S. Tsirkin                               pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
536315a1350SMichael S. Tsirkin                               void *irq_opaque,
537315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_mem,
538315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_io,
5391115ff6dSDavid Gibson                               uint8_t devfn_min, int nirq,
5401115ff6dSDavid Gibson                               const char *typename)
541315a1350SMichael S. Tsirkin {
542315a1350SMichael S. Tsirkin     PCIBus *bus;
543315a1350SMichael S. Tsirkin 
5441115ff6dSDavid Gibson     bus = pci_root_bus_new(parent, name, address_space_mem,
54560a0e443SAlex Williamson                            address_space_io, devfn_min, typename);
546315a1350SMichael S. Tsirkin     pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
547315a1350SMichael S. Tsirkin     return bus;
548315a1350SMichael S. Tsirkin }
549315a1350SMichael S. Tsirkin 
550c13ee169SMichael Roth void pci_unregister_root_bus(PCIBus *bus)
551c13ee169SMichael Roth {
552c13ee169SMichael Roth     pci_bus_irqs_cleanup(bus);
553c13ee169SMichael Roth     pci_root_bus_cleanup(bus);
554c13ee169SMichael Roth }
555c13ee169SMichael Roth 
556315a1350SMichael S. Tsirkin int pci_bus_num(PCIBus *s)
557315a1350SMichael S. Tsirkin {
558602141d9SMarcel Apfelbaum     return PCI_BUS_GET_CLASS(s)->bus_num(s);
559315a1350SMichael S. Tsirkin }
560315a1350SMichael S. Tsirkin 
561500db1daSXingang Wang /* Returns the min and max bus numbers of a PCI bus hierarchy */
562500db1daSXingang Wang void pci_bus_range(PCIBus *bus, int *min_bus, int *max_bus)
563500db1daSXingang Wang {
564500db1daSXingang Wang     int i;
565500db1daSXingang Wang     *min_bus = *max_bus = pci_bus_num(bus);
566500db1daSXingang Wang 
567500db1daSXingang Wang     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
568500db1daSXingang Wang         PCIDevice *dev = bus->devices[i];
569500db1daSXingang Wang 
570500db1daSXingang Wang         if (dev && PCI_DEVICE_GET_CLASS(dev)->is_bridge) {
571500db1daSXingang Wang             *min_bus = MIN(*min_bus, dev->config[PCI_SECONDARY_BUS]);
572500db1daSXingang Wang             *max_bus = MAX(*max_bus, dev->config[PCI_SUBORDINATE_BUS]);
573500db1daSXingang Wang         }
574500db1daSXingang Wang     }
575500db1daSXingang Wang }
576500db1daSXingang Wang 
5776a3042b2SMarcel Apfelbaum int pci_bus_numa_node(PCIBus *bus)
5786a3042b2SMarcel Apfelbaum {
5796a3042b2SMarcel Apfelbaum     return PCI_BUS_GET_CLASS(bus)->numa_node(bus);
580315a1350SMichael S. Tsirkin }
581315a1350SMichael S. Tsirkin 
5822c21ee76SJianjun Duan static int get_pci_config_device(QEMUFile *f, void *pv, size_t size,
58303fee66fSMarc-André Lureau                                  const VMStateField *field)
584315a1350SMichael S. Tsirkin {
585315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, config);
586e78e9ae4SDon Koch     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(s);
587315a1350SMichael S. Tsirkin     uint8_t *config;
588315a1350SMichael S. Tsirkin     int i;
589315a1350SMichael S. Tsirkin 
590315a1350SMichael S. Tsirkin     assert(size == pci_config_size(s));
591315a1350SMichael S. Tsirkin     config = g_malloc(size);
592315a1350SMichael S. Tsirkin 
593315a1350SMichael S. Tsirkin     qemu_get_buffer(f, config, size);
594315a1350SMichael S. Tsirkin     for (i = 0; i < size; ++i) {
595315a1350SMichael S. Tsirkin         if ((config[i] ^ s->config[i]) &
596315a1350SMichael S. Tsirkin             s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
5977c59364dSDr. David Alan Gilbert             error_report("%s: Bad config data: i=0x%x read: %x device: %x "
5987c59364dSDr. David Alan Gilbert                          "cmask: %x wmask: %x w1cmask:%x", __func__,
5997c59364dSDr. David Alan Gilbert                          i, config[i], s->config[i],
6007c59364dSDr. David Alan Gilbert                          s->cmask[i], s->wmask[i], s->w1cmask[i]);
601315a1350SMichael S. Tsirkin             g_free(config);
602315a1350SMichael S. Tsirkin             return -EINVAL;
603315a1350SMichael S. Tsirkin         }
604315a1350SMichael S. Tsirkin     }
605315a1350SMichael S. Tsirkin     memcpy(s->config, config, size);
606315a1350SMichael S. Tsirkin 
607315a1350SMichael S. Tsirkin     pci_update_mappings(s);
608e78e9ae4SDon Koch     if (pc->is_bridge) {
609f055e96bSAndreas Färber         PCIBridge *b = PCI_BRIDGE(s);
610e78e9ae4SDon Koch         pci_bridge_update_mappings(b);
611e78e9ae4SDon Koch     }
612315a1350SMichael S. Tsirkin 
613315a1350SMichael S. Tsirkin     memory_region_set_enabled(&s->bus_master_enable_region,
614315a1350SMichael S. Tsirkin                               pci_get_word(s->config + PCI_COMMAND)
615315a1350SMichael S. Tsirkin                               & PCI_COMMAND_MASTER);
616315a1350SMichael S. Tsirkin 
617315a1350SMichael S. Tsirkin     g_free(config);
618315a1350SMichael S. Tsirkin     return 0;
619315a1350SMichael S. Tsirkin }
620315a1350SMichael S. Tsirkin 
621315a1350SMichael S. Tsirkin /* just put buffer */
6222c21ee76SJianjun Duan static int put_pci_config_device(QEMUFile *f, void *pv, size_t size,
6233ddba9a9SMarkus Armbruster                                  const VMStateField *field, JSONWriter *vmdesc)
624315a1350SMichael S. Tsirkin {
625315a1350SMichael S. Tsirkin     const uint8_t **v = pv;
626315a1350SMichael S. Tsirkin     assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
627315a1350SMichael S. Tsirkin     qemu_put_buffer(f, *v, size);
6282c21ee76SJianjun Duan 
6292c21ee76SJianjun Duan     return 0;
630315a1350SMichael S. Tsirkin }
631315a1350SMichael S. Tsirkin 
632315a1350SMichael S. Tsirkin static VMStateInfo vmstate_info_pci_config = {
633315a1350SMichael S. Tsirkin     .name = "pci config",
634315a1350SMichael S. Tsirkin     .get  = get_pci_config_device,
635315a1350SMichael S. Tsirkin     .put  = put_pci_config_device,
636315a1350SMichael S. Tsirkin };
637315a1350SMichael S. Tsirkin 
6382c21ee76SJianjun Duan static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size,
63903fee66fSMarc-André Lureau                              const VMStateField *field)
640315a1350SMichael S. Tsirkin {
641315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, irq_state);
642315a1350SMichael S. Tsirkin     uint32_t irq_state[PCI_NUM_PINS];
643315a1350SMichael S. Tsirkin     int i;
644315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
645315a1350SMichael S. Tsirkin         irq_state[i] = qemu_get_be32(f);
646315a1350SMichael S. Tsirkin         if (irq_state[i] != 0x1 && irq_state[i] != 0) {
647315a1350SMichael S. Tsirkin             fprintf(stderr, "irq state %d: must be 0 or 1.\n",
648315a1350SMichael S. Tsirkin                     irq_state[i]);
649315a1350SMichael S. Tsirkin             return -EINVAL;
650315a1350SMichael S. Tsirkin         }
651315a1350SMichael S. Tsirkin     }
652315a1350SMichael S. Tsirkin 
653315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
654315a1350SMichael S. Tsirkin         pci_set_irq_state(s, i, irq_state[i]);
655315a1350SMichael S. Tsirkin     }
656315a1350SMichael S. Tsirkin 
657315a1350SMichael S. Tsirkin     return 0;
658315a1350SMichael S. Tsirkin }
659315a1350SMichael S. Tsirkin 
6602c21ee76SJianjun Duan static int put_pci_irq_state(QEMUFile *f, void *pv, size_t size,
6613ddba9a9SMarkus Armbruster                              const VMStateField *field, JSONWriter *vmdesc)
662315a1350SMichael S. Tsirkin {
663315a1350SMichael S. Tsirkin     int i;
664315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, irq_state);
665315a1350SMichael S. Tsirkin 
666315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
667315a1350SMichael S. Tsirkin         qemu_put_be32(f, pci_irq_state(s, i));
668315a1350SMichael S. Tsirkin     }
6692c21ee76SJianjun Duan 
6702c21ee76SJianjun Duan     return 0;
671315a1350SMichael S. Tsirkin }
672315a1350SMichael S. Tsirkin 
673315a1350SMichael S. Tsirkin static VMStateInfo vmstate_info_pci_irq_state = {
674315a1350SMichael S. Tsirkin     .name = "pci irq state",
675315a1350SMichael S. Tsirkin     .get  = get_pci_irq_state,
676315a1350SMichael S. Tsirkin     .put  = put_pci_irq_state,
677315a1350SMichael S. Tsirkin };
678315a1350SMichael S. Tsirkin 
67920daa90aSDr. David Alan Gilbert static bool migrate_is_pcie(void *opaque, int version_id)
68020daa90aSDr. David Alan Gilbert {
68120daa90aSDr. David Alan Gilbert     return pci_is_express((PCIDevice *)opaque);
68220daa90aSDr. David Alan Gilbert }
68320daa90aSDr. David Alan Gilbert 
68420daa90aSDr. David Alan Gilbert static bool migrate_is_not_pcie(void *opaque, int version_id)
68520daa90aSDr. David Alan Gilbert {
68620daa90aSDr. David Alan Gilbert     return !pci_is_express((PCIDevice *)opaque);
68720daa90aSDr. David Alan Gilbert }
68820daa90aSDr. David Alan Gilbert 
689315a1350SMichael S. Tsirkin const VMStateDescription vmstate_pci_device = {
690315a1350SMichael S. Tsirkin     .name = "PCIDevice",
691315a1350SMichael S. Tsirkin     .version_id = 2,
692315a1350SMichael S. Tsirkin     .minimum_version_id = 1,
693315a1350SMichael S. Tsirkin     .fields = (VMStateField[]) {
6943476436aSMichael S. Tsirkin         VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice),
69520daa90aSDr. David Alan Gilbert         VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice,
69620daa90aSDr. David Alan Gilbert                                    migrate_is_not_pcie,
69720daa90aSDr. David Alan Gilbert                                    0, vmstate_info_pci_config,
698315a1350SMichael S. Tsirkin                                    PCI_CONFIG_SPACE_SIZE),
69920daa90aSDr. David Alan Gilbert         VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice,
70020daa90aSDr. David Alan Gilbert                                    migrate_is_pcie,
70120daa90aSDr. David Alan Gilbert                                    0, vmstate_info_pci_config,
702315a1350SMichael S. Tsirkin                                    PCIE_CONFIG_SPACE_SIZE),
703315a1350SMichael S. Tsirkin         VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
704315a1350SMichael S. Tsirkin                                    vmstate_info_pci_irq_state,
705315a1350SMichael S. Tsirkin                                    PCI_NUM_PINS * sizeof(int32_t)),
706315a1350SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
707315a1350SMichael S. Tsirkin     }
708315a1350SMichael S. Tsirkin };
709315a1350SMichael S. Tsirkin 
710315a1350SMichael S. Tsirkin 
711315a1350SMichael S. Tsirkin void pci_device_save(PCIDevice *s, QEMUFile *f)
712315a1350SMichael S. Tsirkin {
713315a1350SMichael S. Tsirkin     /* Clear interrupt status bit: it is implicit
714315a1350SMichael S. Tsirkin      * in irq_state which we are saving.
715315a1350SMichael S. Tsirkin      * This makes us compatible with old devices
716315a1350SMichael S. Tsirkin      * which never set or clear this bit. */
717315a1350SMichael S. Tsirkin     s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
71820daa90aSDr. David Alan Gilbert     vmstate_save_state(f, &vmstate_pci_device, s, NULL);
719315a1350SMichael S. Tsirkin     /* Restore the interrupt status bit. */
720315a1350SMichael S. Tsirkin     pci_update_irq_status(s);
721315a1350SMichael S. Tsirkin }
722315a1350SMichael S. Tsirkin 
723315a1350SMichael S. Tsirkin int pci_device_load(PCIDevice *s, QEMUFile *f)
724315a1350SMichael S. Tsirkin {
725315a1350SMichael S. Tsirkin     int ret;
72620daa90aSDr. David Alan Gilbert     ret = vmstate_load_state(f, &vmstate_pci_device, s, s->version_id);
727315a1350SMichael S. Tsirkin     /* Restore the interrupt status bit. */
728315a1350SMichael S. Tsirkin     pci_update_irq_status(s);
729315a1350SMichael S. Tsirkin     return ret;
730315a1350SMichael S. Tsirkin }
731315a1350SMichael S. Tsirkin 
732315a1350SMichael S. Tsirkin static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
733315a1350SMichael S. Tsirkin {
734315a1350SMichael S. Tsirkin     pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
735315a1350SMichael S. Tsirkin                  pci_default_sub_vendor_id);
736315a1350SMichael S. Tsirkin     pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
737315a1350SMichael S. Tsirkin                  pci_default_sub_device_id);
738315a1350SMichael S. Tsirkin }
739315a1350SMichael S. Tsirkin 
740315a1350SMichael S. Tsirkin /*
741315a1350SMichael S. Tsirkin  * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
742315a1350SMichael S. Tsirkin  *       [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
743315a1350SMichael S. Tsirkin  */
7446dbcb819SMarkus Armbruster static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
745315a1350SMichael S. Tsirkin                              unsigned int *slotp, unsigned int *funcp)
746315a1350SMichael S. Tsirkin {
747315a1350SMichael S. Tsirkin     const char *p;
748315a1350SMichael S. Tsirkin     char *e;
749315a1350SMichael S. Tsirkin     unsigned long val;
750315a1350SMichael S. Tsirkin     unsigned long dom = 0, bus = 0;
751315a1350SMichael S. Tsirkin     unsigned int slot = 0;
752315a1350SMichael S. Tsirkin     unsigned int func = 0;
753315a1350SMichael S. Tsirkin 
754315a1350SMichael S. Tsirkin     p = addr;
755315a1350SMichael S. Tsirkin     val = strtoul(p, &e, 16);
756315a1350SMichael S. Tsirkin     if (e == p)
757315a1350SMichael S. Tsirkin         return -1;
758315a1350SMichael S. Tsirkin     if (*e == ':') {
759315a1350SMichael S. Tsirkin         bus = val;
760315a1350SMichael S. Tsirkin         p = e + 1;
761315a1350SMichael S. Tsirkin         val = strtoul(p, &e, 16);
762315a1350SMichael S. Tsirkin         if (e == p)
763315a1350SMichael S. Tsirkin             return -1;
764315a1350SMichael S. Tsirkin         if (*e == ':') {
765315a1350SMichael S. Tsirkin             dom = bus;
766315a1350SMichael S. Tsirkin             bus = val;
767315a1350SMichael S. Tsirkin             p = e + 1;
768315a1350SMichael S. Tsirkin             val = strtoul(p, &e, 16);
769315a1350SMichael S. Tsirkin             if (e == p)
770315a1350SMichael S. Tsirkin                 return -1;
771315a1350SMichael S. Tsirkin         }
772315a1350SMichael S. Tsirkin     }
773315a1350SMichael S. Tsirkin 
774315a1350SMichael S. Tsirkin     slot = val;
775315a1350SMichael S. Tsirkin 
776315a1350SMichael S. Tsirkin     if (funcp != NULL) {
777315a1350SMichael S. Tsirkin         if (*e != '.')
778315a1350SMichael S. Tsirkin             return -1;
779315a1350SMichael S. Tsirkin 
780315a1350SMichael S. Tsirkin         p = e + 1;
781315a1350SMichael S. Tsirkin         val = strtoul(p, &e, 16);
782315a1350SMichael S. Tsirkin         if (e == p)
783315a1350SMichael S. Tsirkin             return -1;
784315a1350SMichael S. Tsirkin 
785315a1350SMichael S. Tsirkin         func = val;
786315a1350SMichael S. Tsirkin     }
787315a1350SMichael S. Tsirkin 
788315a1350SMichael S. Tsirkin     /* if funcp == NULL func is 0 */
789315a1350SMichael S. Tsirkin     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
790315a1350SMichael S. Tsirkin         return -1;
791315a1350SMichael S. Tsirkin 
792315a1350SMichael S. Tsirkin     if (*e)
793315a1350SMichael S. Tsirkin         return -1;
794315a1350SMichael S. Tsirkin 
795315a1350SMichael S. Tsirkin     *domp = dom;
796315a1350SMichael S. Tsirkin     *busp = bus;
797315a1350SMichael S. Tsirkin     *slotp = slot;
798315a1350SMichael S. Tsirkin     if (funcp != NULL)
799315a1350SMichael S. Tsirkin         *funcp = func;
800315a1350SMichael S. Tsirkin     return 0;
801315a1350SMichael S. Tsirkin }
802315a1350SMichael S. Tsirkin 
803315a1350SMichael S. Tsirkin static void pci_init_cmask(PCIDevice *dev)
804315a1350SMichael S. Tsirkin {
805315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
806315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
807315a1350SMichael S. Tsirkin     dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
808315a1350SMichael S. Tsirkin     dev->cmask[PCI_REVISION_ID] = 0xff;
809315a1350SMichael S. Tsirkin     dev->cmask[PCI_CLASS_PROG] = 0xff;
810315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
811315a1350SMichael S. Tsirkin     dev->cmask[PCI_HEADER_TYPE] = 0xff;
812315a1350SMichael S. Tsirkin     dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
813315a1350SMichael S. Tsirkin }
814315a1350SMichael S. Tsirkin 
815315a1350SMichael S. Tsirkin static void pci_init_wmask(PCIDevice *dev)
816315a1350SMichael S. Tsirkin {
817315a1350SMichael S. Tsirkin     int config_size = pci_config_size(dev);
818315a1350SMichael S. Tsirkin 
819315a1350SMichael S. Tsirkin     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
820315a1350SMichael S. Tsirkin     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
821315a1350SMichael S. Tsirkin     pci_set_word(dev->wmask + PCI_COMMAND,
822315a1350SMichael S. Tsirkin                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
823315a1350SMichael S. Tsirkin                  PCI_COMMAND_INTX_DISABLE);
824315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
825315a1350SMichael S. Tsirkin 
826315a1350SMichael S. Tsirkin     memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
827315a1350SMichael S. Tsirkin            config_size - PCI_CONFIG_HEADER_SIZE);
828315a1350SMichael S. Tsirkin }
829315a1350SMichael S. Tsirkin 
830315a1350SMichael S. Tsirkin static void pci_init_w1cmask(PCIDevice *dev)
831315a1350SMichael S. Tsirkin {
832315a1350SMichael S. Tsirkin     /*
833315a1350SMichael S. Tsirkin      * Note: It's okay to set w1cmask even for readonly bits as
834315a1350SMichael S. Tsirkin      * long as their value is hardwired to 0.
835315a1350SMichael S. Tsirkin      */
836315a1350SMichael S. Tsirkin     pci_set_word(dev->w1cmask + PCI_STATUS,
837315a1350SMichael S. Tsirkin                  PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
838315a1350SMichael S. Tsirkin                  PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
839315a1350SMichael S. Tsirkin                  PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
840315a1350SMichael S. Tsirkin }
841315a1350SMichael S. Tsirkin 
842315a1350SMichael S. Tsirkin static void pci_init_mask_bridge(PCIDevice *d)
843315a1350SMichael S. Tsirkin {
844315a1350SMichael S. Tsirkin     /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
845315a1350SMichael S. Tsirkin        PCI_SEC_LETENCY_TIMER */
846315a1350SMichael S. Tsirkin     memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
847315a1350SMichael S. Tsirkin 
848315a1350SMichael S. Tsirkin     /* base and limit */
849315a1350SMichael S. Tsirkin     d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
850315a1350SMichael S. Tsirkin     d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
851315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_MEMORY_BASE,
852315a1350SMichael S. Tsirkin                  PCI_MEMORY_RANGE_MASK & 0xffff);
853315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
854315a1350SMichael S. Tsirkin                  PCI_MEMORY_RANGE_MASK & 0xffff);
855315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
856315a1350SMichael S. Tsirkin                  PCI_PREF_RANGE_MASK & 0xffff);
857315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
858315a1350SMichael S. Tsirkin                  PCI_PREF_RANGE_MASK & 0xffff);
859315a1350SMichael S. Tsirkin 
860315a1350SMichael S. Tsirkin     /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
861315a1350SMichael S. Tsirkin     memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
862315a1350SMichael S. Tsirkin 
863315a1350SMichael S. Tsirkin     /* Supported memory and i/o types */
864315a1350SMichael S. Tsirkin     d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16;
865315a1350SMichael S. Tsirkin     d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16;
866315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE,
867315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_64);
868315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT,
869315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_64);
870315a1350SMichael S. Tsirkin 
871ba7d8515SAlex Williamson     /*
872ba7d8515SAlex Williamson      * TODO: Bridges default to 10-bit VGA decoding but we currently only
873ba7d8515SAlex Williamson      * implement 16-bit decoding (no alias support).
874ba7d8515SAlex Williamson      */
875315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
876315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_PARITY |
877315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_SERR |
878315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_ISA |
879315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_VGA |
880315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_VGA_16BIT |
881315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_MASTER_ABORT |
882315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_BUS_RESET |
883315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_FAST_BACK |
884315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD |
885315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_SEC_DISCARD |
886315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD_SERR);
887315a1350SMichael S. Tsirkin     /* Below does not do anything as we never set this bit, put here for
888315a1350SMichael S. Tsirkin      * completeness. */
889315a1350SMichael S. Tsirkin     pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
890315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD_STATUS);
891315a1350SMichael S. Tsirkin     d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK;
892315a1350SMichael S. Tsirkin     d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK;
893315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE,
894315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_MASK);
895315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT,
896315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_MASK);
897315a1350SMichael S. Tsirkin }
898315a1350SMichael S. Tsirkin 
899133e9b22SMarkus Armbruster static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp)
900315a1350SMichael S. Tsirkin {
901315a1350SMichael S. Tsirkin     uint8_t slot = PCI_SLOT(dev->devfn);
902315a1350SMichael S. Tsirkin     uint8_t func;
903315a1350SMichael S. Tsirkin 
904315a1350SMichael S. Tsirkin     if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
905315a1350SMichael S. Tsirkin         dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
906315a1350SMichael S. Tsirkin     }
907315a1350SMichael S. Tsirkin 
908315a1350SMichael S. Tsirkin     /*
9097c0fa8dfSKnut Omang      * With SR/IOV and ARI, a device at function 0 need not be a multifunction
9107c0fa8dfSKnut Omang      * device, as it may just be a VF that ended up with function 0 in
9117c0fa8dfSKnut Omang      * the legacy PCI interpretation. Avoid failing in such cases:
9127c0fa8dfSKnut Omang      */
9137c0fa8dfSKnut Omang     if (pci_is_vf(dev) &&
9147c0fa8dfSKnut Omang         dev->exp.sriov_vf.pf->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
9157c0fa8dfSKnut Omang         return;
9167c0fa8dfSKnut Omang     }
9177c0fa8dfSKnut Omang 
9187c0fa8dfSKnut Omang     /*
919315a1350SMichael S. Tsirkin      * multifunction bit is interpreted in two ways as follows.
920315a1350SMichael S. Tsirkin      *   - all functions must set the bit to 1.
921315a1350SMichael S. Tsirkin      *     Example: Intel X53
922315a1350SMichael S. Tsirkin      *   - function 0 must set the bit, but the rest function (> 0)
923315a1350SMichael S. Tsirkin      *     is allowed to leave the bit to 0.
924315a1350SMichael S. Tsirkin      *     Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
925315a1350SMichael S. Tsirkin      *
926315a1350SMichael S. Tsirkin      * So OS (at least Linux) checks the bit of only function 0,
927315a1350SMichael S. Tsirkin      * and doesn't see the bit of function > 0.
928315a1350SMichael S. Tsirkin      *
929315a1350SMichael S. Tsirkin      * The below check allows both interpretation.
930315a1350SMichael S. Tsirkin      */
931315a1350SMichael S. Tsirkin     if (PCI_FUNC(dev->devfn)) {
932315a1350SMichael S. Tsirkin         PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
933315a1350SMichael S. Tsirkin         if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
934315a1350SMichael S. Tsirkin             /* function 0 should set multifunction bit */
935133e9b22SMarkus Armbruster             error_setg(errp, "PCI: single function device can't be populated "
936315a1350SMichael S. Tsirkin                        "in function %x.%x", slot, PCI_FUNC(dev->devfn));
937133e9b22SMarkus Armbruster             return;
938315a1350SMichael S. Tsirkin         }
939133e9b22SMarkus Armbruster         return;
940315a1350SMichael S. Tsirkin     }
941315a1350SMichael S. Tsirkin 
942315a1350SMichael S. Tsirkin     if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
943133e9b22SMarkus Armbruster         return;
944315a1350SMichael S. Tsirkin     }
945315a1350SMichael S. Tsirkin     /* function 0 indicates single function, so function > 0 must be NULL */
946315a1350SMichael S. Tsirkin     for (func = 1; func < PCI_FUNC_MAX; ++func) {
947315a1350SMichael S. Tsirkin         if (bus->devices[PCI_DEVFN(slot, func)]) {
948133e9b22SMarkus Armbruster             error_setg(errp, "PCI: %x.0 indicates single function, "
949315a1350SMichael S. Tsirkin                        "but %x.%x is already populated.",
950315a1350SMichael S. Tsirkin                        slot, slot, func);
951133e9b22SMarkus Armbruster             return;
952315a1350SMichael S. Tsirkin         }
953315a1350SMichael S. Tsirkin     }
954315a1350SMichael S. Tsirkin }
955315a1350SMichael S. Tsirkin 
956315a1350SMichael S. Tsirkin static void pci_config_alloc(PCIDevice *pci_dev)
957315a1350SMichael S. Tsirkin {
958315a1350SMichael S. Tsirkin     int config_size = pci_config_size(pci_dev);
959315a1350SMichael S. Tsirkin 
960315a1350SMichael S. Tsirkin     pci_dev->config = g_malloc0(config_size);
961315a1350SMichael S. Tsirkin     pci_dev->cmask = g_malloc0(config_size);
962315a1350SMichael S. Tsirkin     pci_dev->wmask = g_malloc0(config_size);
963315a1350SMichael S. Tsirkin     pci_dev->w1cmask = g_malloc0(config_size);
964315a1350SMichael S. Tsirkin     pci_dev->used = g_malloc0(config_size);
965315a1350SMichael S. Tsirkin }
966315a1350SMichael S. Tsirkin 
967315a1350SMichael S. Tsirkin static void pci_config_free(PCIDevice *pci_dev)
968315a1350SMichael S. Tsirkin {
969315a1350SMichael S. Tsirkin     g_free(pci_dev->config);
970315a1350SMichael S. Tsirkin     g_free(pci_dev->cmask);
971315a1350SMichael S. Tsirkin     g_free(pci_dev->wmask);
972315a1350SMichael S. Tsirkin     g_free(pci_dev->w1cmask);
973315a1350SMichael S. Tsirkin     g_free(pci_dev->used);
974315a1350SMichael S. Tsirkin }
975315a1350SMichael S. Tsirkin 
97630607764SMarcel Apfelbaum static void do_pci_unregister_device(PCIDevice *pci_dev)
97730607764SMarcel Apfelbaum {
978fd56e061SDavid Gibson     pci_get_bus(pci_dev)->devices[pci_dev->devfn] = NULL;
97930607764SMarcel Apfelbaum     pci_config_free(pci_dev);
98030607764SMarcel Apfelbaum 
981193982c6SAlexey Kardashevskiy     if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
982c53598edSAlexey Kardashevskiy         memory_region_del_subregion(&pci_dev->bus_master_container_region,
983c53598edSAlexey Kardashevskiy                                     &pci_dev->bus_master_enable_region);
984193982c6SAlexey Kardashevskiy     }
98530607764SMarcel Apfelbaum     address_space_destroy(&pci_dev->bus_master_as);
98630607764SMarcel Apfelbaum }
98730607764SMarcel Apfelbaum 
9884a94b3aaSPeter Xu /* Extract PCIReqIDCache into BDF format */
9894a94b3aaSPeter Xu static uint16_t pci_req_id_cache_extract(PCIReqIDCache *cache)
9904a94b3aaSPeter Xu {
9914a94b3aaSPeter Xu     uint8_t bus_n;
9924a94b3aaSPeter Xu     uint16_t result;
9934a94b3aaSPeter Xu 
9944a94b3aaSPeter Xu     switch (cache->type) {
9954a94b3aaSPeter Xu     case PCI_REQ_ID_BDF:
9964a94b3aaSPeter Xu         result = pci_get_bdf(cache->dev);
9974a94b3aaSPeter Xu         break;
9984a94b3aaSPeter Xu     case PCI_REQ_ID_SECONDARY_BUS:
999fd56e061SDavid Gibson         bus_n = pci_dev_bus_num(cache->dev);
10004a94b3aaSPeter Xu         result = PCI_BUILD_BDF(bus_n, 0);
10014a94b3aaSPeter Xu         break;
10024a94b3aaSPeter Xu     default:
1003eaf27fabSMarkus Armbruster         error_report("Invalid PCI requester ID cache type: %d",
10044a94b3aaSPeter Xu                      cache->type);
10054a94b3aaSPeter Xu         exit(1);
10064a94b3aaSPeter Xu         break;
10074a94b3aaSPeter Xu     }
10084a94b3aaSPeter Xu 
10094a94b3aaSPeter Xu     return result;
10104a94b3aaSPeter Xu }
10114a94b3aaSPeter Xu 
10124a94b3aaSPeter Xu /* Parse bridges up to the root complex and return requester ID
10134a94b3aaSPeter Xu  * cache for specific device.  For full PCIe topology, the cache
10144a94b3aaSPeter Xu  * result would be exactly the same as getting BDF of the device.
10154a94b3aaSPeter Xu  * However, several tricks are required when system mixed up with
10164a94b3aaSPeter Xu  * legacy PCI devices and PCIe-to-PCI bridges.
10174a94b3aaSPeter Xu  *
10184a94b3aaSPeter Xu  * Here we cache the proxy device (and type) not requester ID since
10194a94b3aaSPeter Xu  * bus number might change from time to time.
10204a94b3aaSPeter Xu  */
10214a94b3aaSPeter Xu static PCIReqIDCache pci_req_id_cache_get(PCIDevice *dev)
10224a94b3aaSPeter Xu {
10234a94b3aaSPeter Xu     PCIDevice *parent;
10244a94b3aaSPeter Xu     PCIReqIDCache cache = {
10254a94b3aaSPeter Xu         .dev = dev,
10264a94b3aaSPeter Xu         .type = PCI_REQ_ID_BDF,
10274a94b3aaSPeter Xu     };
10284a94b3aaSPeter Xu 
1029fd56e061SDavid Gibson     while (!pci_bus_is_root(pci_get_bus(dev))) {
10304a94b3aaSPeter Xu         /* We are under PCI/PCIe bridges */
1031fd56e061SDavid Gibson         parent = pci_get_bus(dev)->parent_dev;
10324a94b3aaSPeter Xu         if (pci_is_express(parent)) {
10334a94b3aaSPeter Xu             if (pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) {
10344a94b3aaSPeter Xu                 /* When we pass through PCIe-to-PCI/PCIX bridges, we
10354a94b3aaSPeter Xu                  * override the requester ID using secondary bus
10364a94b3aaSPeter Xu                  * number of parent bridge with zeroed devfn
10374a94b3aaSPeter Xu                  * (pcie-to-pci bridge spec chap 2.3). */
10384a94b3aaSPeter Xu                 cache.type = PCI_REQ_ID_SECONDARY_BUS;
10394a94b3aaSPeter Xu                 cache.dev = dev;
10404a94b3aaSPeter Xu             }
10414a94b3aaSPeter Xu         } else {
10424a94b3aaSPeter Xu             /* Legacy PCI, override requester ID with the bridge's
10434a94b3aaSPeter Xu              * BDF upstream.  When the root complex connects to
10444a94b3aaSPeter Xu              * legacy PCI devices (including buses), it can only
10454a94b3aaSPeter Xu              * obtain requester ID info from directly attached
10464a94b3aaSPeter Xu              * devices.  If devices are attached under bridges, only
10474a94b3aaSPeter Xu              * the requester ID of the bridge that is directly
10484a94b3aaSPeter Xu              * attached to the root complex can be recognized. */
10494a94b3aaSPeter Xu             cache.type = PCI_REQ_ID_BDF;
10504a94b3aaSPeter Xu             cache.dev = parent;
10514a94b3aaSPeter Xu         }
10524a94b3aaSPeter Xu         dev = parent;
10534a94b3aaSPeter Xu     }
10544a94b3aaSPeter Xu 
10554a94b3aaSPeter Xu     return cache;
10564a94b3aaSPeter Xu }
10574a94b3aaSPeter Xu 
10584a94b3aaSPeter Xu uint16_t pci_requester_id(PCIDevice *dev)
10594a94b3aaSPeter Xu {
10604a94b3aaSPeter Xu     return pci_req_id_cache_extract(&dev->requester_id_cache);
10614a94b3aaSPeter Xu }
10624a94b3aaSPeter Xu 
10639b717a3aSMark Cave-Ayland static bool pci_bus_devfn_available(PCIBus *bus, int devfn)
10649b717a3aSMark Cave-Ayland {
10659b717a3aSMark Cave-Ayland     return !(bus->devices[devfn]);
10669b717a3aSMark Cave-Ayland }
10679b717a3aSMark Cave-Ayland 
10688b884984SMark Cave-Ayland static bool pci_bus_devfn_reserved(PCIBus *bus, int devfn)
10698b884984SMark Cave-Ayland {
10708b884984SMark Cave-Ayland     return bus->slot_reserved_mask & (1UL << PCI_SLOT(devfn));
10718b884984SMark Cave-Ayland }
10728b884984SMark Cave-Ayland 
1073315a1350SMichael S. Tsirkin /* -1 for devfn means auto assign */
1074fd56e061SDavid Gibson static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
1075133e9b22SMarkus Armbruster                                          const char *name, int devfn,
1076133e9b22SMarkus Armbruster                                          Error **errp)
1077315a1350SMichael S. Tsirkin {
1078315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
1079315a1350SMichael S. Tsirkin     PCIConfigReadFunc *config_read = pc->config_read;
1080315a1350SMichael S. Tsirkin     PCIConfigWriteFunc *config_write = pc->config_write;
1081133e9b22SMarkus Armbruster     Error *local_err = NULL;
10823f1e1478SCao jin     DeviceState *dev = DEVICE(pci_dev);
1083fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
10843f1e1478SCao jin 
10850144f6f1SMarcel Apfelbaum     /* Only pci bridges can be attached to extra PCI root buses */
10860144f6f1SMarcel Apfelbaum     if (pci_bus_is_root(bus) && bus->parent_dev && !pc->is_bridge) {
10870144f6f1SMarcel Apfelbaum         error_setg(errp,
10880144f6f1SMarcel Apfelbaum                    "PCI: Only PCI/PCIe bridges can be plugged into %s",
10890144f6f1SMarcel Apfelbaum                     bus->parent_dev->name);
10900144f6f1SMarcel Apfelbaum         return NULL;
10910144f6f1SMarcel Apfelbaum     }
1092315a1350SMichael S. Tsirkin 
1093315a1350SMichael S. Tsirkin     if (devfn < 0) {
1094315a1350SMichael S. Tsirkin         for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
1095315a1350SMichael S. Tsirkin             devfn += PCI_FUNC_MAX) {
10968b884984SMark Cave-Ayland             if (pci_bus_devfn_available(bus, devfn) &&
10978b884984SMark Cave-Ayland                    !pci_bus_devfn_reserved(bus, devfn)) {
1098315a1350SMichael S. Tsirkin                 goto found;
1099315a1350SMichael S. Tsirkin             }
11009b717a3aSMark Cave-Ayland         }
11018b884984SMark Cave-Ayland         error_setg(errp, "PCI: no slot/function available for %s, all in use "
11028b884984SMark Cave-Ayland                    "or reserved", name);
1103315a1350SMichael S. Tsirkin         return NULL;
1104315a1350SMichael S. Tsirkin     found: ;
11058b884984SMark Cave-Ayland     } else if (pci_bus_devfn_reserved(bus, devfn)) {
11068b884984SMark Cave-Ayland         error_setg(errp, "PCI: slot %d function %d not available for %s,"
11078b884984SMark Cave-Ayland                    " reserved",
11088b884984SMark Cave-Ayland                    PCI_SLOT(devfn), PCI_FUNC(devfn), name);
11098b884984SMark Cave-Ayland         return NULL;
11109b717a3aSMark Cave-Ayland     } else if (!pci_bus_devfn_available(bus, devfn)) {
1111133e9b22SMarkus Armbruster         error_setg(errp, "PCI: slot %d function %d not available for %s,"
1112ad003b9eSZhenzhong Duan                    " in use by %s,id=%s",
1113133e9b22SMarkus Armbruster                    PCI_SLOT(devfn), PCI_FUNC(devfn), name,
1114ad003b9eSZhenzhong Duan                    bus->devices[devfn]->name, bus->devices[devfn]->qdev.id);
1115315a1350SMichael S. Tsirkin         return NULL;
11163f1e1478SCao jin     } else if (dev->hotplugged &&
11177c0fa8dfSKnut Omang                !pci_is_vf(pci_dev) &&
11183f1e1478SCao jin                pci_get_function_0(pci_dev)) {
11193298bbceSJulia Suvorova         error_setg(errp, "PCI: slot %d function 0 already occupied by %s,"
11203f1e1478SCao jin                    " new func %s cannot be exposed to guest.",
1121d93ddfb1SMichael S. Tsirkin                    PCI_SLOT(pci_get_function_0(pci_dev)->devfn),
1122d93ddfb1SMichael S. Tsirkin                    pci_get_function_0(pci_dev)->name,
11233f1e1478SCao jin                    name);
11243f1e1478SCao jin 
11253f1e1478SCao jin        return NULL;
1126315a1350SMichael S. Tsirkin     }
1127e00387d5SAvi Kivity 
1128efc8188eSLe Tan     pci_dev->devfn = devfn;
11294a94b3aaSPeter Xu     pci_dev->requester_id_cache = pci_req_id_cache_get(pci_dev);
1130d06bce95SAlexey Kardashevskiy     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
1131e00387d5SAvi Kivity 
11323716d590SJason Wang     memory_region_init(&pci_dev->bus_master_container_region, OBJECT(pci_dev),
11333716d590SJason Wang                        "bus master container", UINT64_MAX);
11343716d590SJason Wang     address_space_init(&pci_dev->bus_master_as,
11353716d590SJason Wang                        &pci_dev->bus_master_container_region, pci_dev->name);
11363716d590SJason Wang 
11372f181fbdSPaolo Bonzini     if (phase_check(PHASE_MACHINE_READY)) {
1138b86eacb8SMarcel Apfelbaum         pci_init_bus_master(pci_dev);
1139b86eacb8SMarcel Apfelbaum     }
1140315a1350SMichael S. Tsirkin     pci_dev->irq_state = 0;
1141315a1350SMichael S. Tsirkin     pci_config_alloc(pci_dev);
1142315a1350SMichael S. Tsirkin 
1143315a1350SMichael S. Tsirkin     pci_config_set_vendor_id(pci_dev->config, pc->vendor_id);
1144315a1350SMichael S. Tsirkin     pci_config_set_device_id(pci_dev->config, pc->device_id);
1145315a1350SMichael S. Tsirkin     pci_config_set_revision(pci_dev->config, pc->revision);
1146315a1350SMichael S. Tsirkin     pci_config_set_class(pci_dev->config, pc->class_id);
1147315a1350SMichael S. Tsirkin 
1148315a1350SMichael S. Tsirkin     if (!pc->is_bridge) {
1149315a1350SMichael S. Tsirkin         if (pc->subsystem_vendor_id || pc->subsystem_id) {
1150315a1350SMichael S. Tsirkin             pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
1151315a1350SMichael S. Tsirkin                          pc->subsystem_vendor_id);
1152315a1350SMichael S. Tsirkin             pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
1153315a1350SMichael S. Tsirkin                          pc->subsystem_id);
1154315a1350SMichael S. Tsirkin         } else {
1155315a1350SMichael S. Tsirkin             pci_set_default_subsystem_id(pci_dev);
1156315a1350SMichael S. Tsirkin         }
1157315a1350SMichael S. Tsirkin     } else {
1158315a1350SMichael S. Tsirkin         /* subsystem_vendor_id/subsystem_id are only for header type 0 */
1159315a1350SMichael S. Tsirkin         assert(!pc->subsystem_vendor_id);
1160315a1350SMichael S. Tsirkin         assert(!pc->subsystem_id);
1161315a1350SMichael S. Tsirkin     }
1162315a1350SMichael S. Tsirkin     pci_init_cmask(pci_dev);
1163315a1350SMichael S. Tsirkin     pci_init_wmask(pci_dev);
1164315a1350SMichael S. Tsirkin     pci_init_w1cmask(pci_dev);
1165315a1350SMichael S. Tsirkin     if (pc->is_bridge) {
1166315a1350SMichael S. Tsirkin         pci_init_mask_bridge(pci_dev);
1167315a1350SMichael S. Tsirkin     }
1168133e9b22SMarkus Armbruster     pci_init_multifunction(bus, pci_dev, &local_err);
1169133e9b22SMarkus Armbruster     if (local_err) {
1170133e9b22SMarkus Armbruster         error_propagate(errp, local_err);
117130607764SMarcel Apfelbaum         do_pci_unregister_device(pci_dev);
1172315a1350SMichael S. Tsirkin         return NULL;
1173315a1350SMichael S. Tsirkin     }
1174315a1350SMichael S. Tsirkin 
1175315a1350SMichael S. Tsirkin     if (!config_read)
1176315a1350SMichael S. Tsirkin         config_read = pci_default_read_config;
1177315a1350SMichael S. Tsirkin     if (!config_write)
1178315a1350SMichael S. Tsirkin         config_write = pci_default_write_config;
1179315a1350SMichael S. Tsirkin     pci_dev->config_read = config_read;
1180315a1350SMichael S. Tsirkin     pci_dev->config_write = config_write;
1181315a1350SMichael S. Tsirkin     bus->devices[devfn] = pci_dev;
1182315a1350SMichael S. Tsirkin     pci_dev->version_id = 2; /* Current pci device vmstate version */
1183315a1350SMichael S. Tsirkin     return pci_dev;
1184315a1350SMichael S. Tsirkin }
1185315a1350SMichael S. Tsirkin 
1186315a1350SMichael S. Tsirkin static void pci_unregister_io_regions(PCIDevice *pci_dev)
1187315a1350SMichael S. Tsirkin {
1188315a1350SMichael S. Tsirkin     PCIIORegion *r;
1189315a1350SMichael S. Tsirkin     int i;
1190315a1350SMichael S. Tsirkin 
1191315a1350SMichael S. Tsirkin     for(i = 0; i < PCI_NUM_REGIONS; i++) {
1192315a1350SMichael S. Tsirkin         r = &pci_dev->io_regions[i];
1193315a1350SMichael S. Tsirkin         if (!r->size || r->addr == PCI_BAR_UNMAPPED)
1194315a1350SMichael S. Tsirkin             continue;
1195315a1350SMichael S. Tsirkin         memory_region_del_subregion(r->address_space, r->memory);
1196315a1350SMichael S. Tsirkin     }
1197e01fd687SAlex Williamson 
1198e01fd687SAlex Williamson     pci_unregister_vga(pci_dev);
1199315a1350SMichael S. Tsirkin }
1200315a1350SMichael S. Tsirkin 
1201b69c3c21SMarkus Armbruster static void pci_qdev_unrealize(DeviceState *dev)
1202315a1350SMichael S. Tsirkin {
1203315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = PCI_DEVICE(dev);
1204315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
1205315a1350SMichael S. Tsirkin 
1206315a1350SMichael S. Tsirkin     pci_unregister_io_regions(pci_dev);
1207315a1350SMichael S. Tsirkin     pci_del_option_rom(pci_dev);
1208315a1350SMichael S. Tsirkin 
1209315a1350SMichael S. Tsirkin     if (pc->exit) {
1210315a1350SMichael S. Tsirkin         pc->exit(pci_dev);
1211315a1350SMichael S. Tsirkin     }
1212315a1350SMichael S. Tsirkin 
12133936161fSHerongguang (Stephen)     pci_device_deassert_intx(pci_dev);
1214315a1350SMichael S. Tsirkin     do_pci_unregister_device(pci_dev);
1215315a1350SMichael S. Tsirkin }
1216315a1350SMichael S. Tsirkin 
1217315a1350SMichael S. Tsirkin void pci_register_bar(PCIDevice *pci_dev, int region_num,
1218315a1350SMichael S. Tsirkin                       uint8_t type, MemoryRegion *memory)
1219315a1350SMichael S. Tsirkin {
1220315a1350SMichael S. Tsirkin     PCIIORegion *r;
12215178ecd8SCao jin     uint32_t addr; /* offset in pci config space */
1222315a1350SMichael S. Tsirkin     uint64_t wmask;
1223315a1350SMichael S. Tsirkin     pcibus_t size = memory_region_size(memory);
12246a5b19caSBen Widawsky     uint8_t hdr_type;
1225315a1350SMichael S. Tsirkin 
12267c0fa8dfSKnut Omang     assert(!pci_is_vf(pci_dev)); /* VFs must use pcie_sriov_vf_register_bar */
1227315a1350SMichael S. Tsirkin     assert(region_num >= 0);
1228315a1350SMichael S. Tsirkin     assert(region_num < PCI_NUM_REGIONS);
12292c729dc8SBen Widawsky     assert(is_power_of_2(size));
1230315a1350SMichael S. Tsirkin 
12316a5b19caSBen Widawsky     /* A PCI bridge device (with Type 1 header) may only have at most 2 BARs */
12326a5b19caSBen Widawsky     hdr_type =
12336a5b19caSBen Widawsky         pci_dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
12346a5b19caSBen Widawsky     assert(hdr_type != PCI_HEADER_TYPE_BRIDGE || region_num < 2);
12356a5b19caSBen Widawsky 
1236315a1350SMichael S. Tsirkin     r = &pci_dev->io_regions[region_num];
1237315a1350SMichael S. Tsirkin     r->addr = PCI_BAR_UNMAPPED;
1238315a1350SMichael S. Tsirkin     r->size = size;
1239315a1350SMichael S. Tsirkin     r->type = type;
12405178ecd8SCao jin     r->memory = memory;
12415178ecd8SCao jin     r->address_space = type & PCI_BASE_ADDRESS_SPACE_IO
1242fd56e061SDavid Gibson                         ? pci_get_bus(pci_dev)->address_space_io
1243fd56e061SDavid Gibson                         : pci_get_bus(pci_dev)->address_space_mem;
1244315a1350SMichael S. Tsirkin 
1245315a1350SMichael S. Tsirkin     wmask = ~(size - 1);
1246315a1350SMichael S. Tsirkin     if (region_num == PCI_ROM_SLOT) {
1247315a1350SMichael S. Tsirkin         /* ROM enable bit is writable */
1248315a1350SMichael S. Tsirkin         wmask |= PCI_ROM_ADDRESS_ENABLE;
1249315a1350SMichael S. Tsirkin     }
12505178ecd8SCao jin 
12515178ecd8SCao jin     addr = pci_bar(pci_dev, region_num);
1252315a1350SMichael S. Tsirkin     pci_set_long(pci_dev->config + addr, type);
12535178ecd8SCao jin 
1254315a1350SMichael S. Tsirkin     if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
1255315a1350SMichael S. Tsirkin         r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1256315a1350SMichael S. Tsirkin         pci_set_quad(pci_dev->wmask + addr, wmask);
1257315a1350SMichael S. Tsirkin         pci_set_quad(pci_dev->cmask + addr, ~0ULL);
1258315a1350SMichael S. Tsirkin     } else {
1259315a1350SMichael S. Tsirkin         pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
1260315a1350SMichael S. Tsirkin         pci_set_long(pci_dev->cmask + addr, 0xffffffff);
1261315a1350SMichael S. Tsirkin     }
1262315a1350SMichael S. Tsirkin }
1263315a1350SMichael S. Tsirkin 
1264e01fd687SAlex Williamson static void pci_update_vga(PCIDevice *pci_dev)
1265e01fd687SAlex Williamson {
1266e01fd687SAlex Williamson     uint16_t cmd;
1267e01fd687SAlex Williamson 
1268e01fd687SAlex Williamson     if (!pci_dev->has_vga) {
1269e01fd687SAlex Williamson         return;
1270e01fd687SAlex Williamson     }
1271e01fd687SAlex Williamson 
1272e01fd687SAlex Williamson     cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
1273e01fd687SAlex Williamson 
1274e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM],
1275e01fd687SAlex Williamson                               cmd & PCI_COMMAND_MEMORY);
1276e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO],
1277e01fd687SAlex Williamson                               cmd & PCI_COMMAND_IO);
1278e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI],
1279e01fd687SAlex Williamson                               cmd & PCI_COMMAND_IO);
1280e01fd687SAlex Williamson }
1281e01fd687SAlex Williamson 
1282e01fd687SAlex Williamson void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
1283e01fd687SAlex Williamson                       MemoryRegion *io_lo, MemoryRegion *io_hi)
1284e01fd687SAlex Williamson {
1285fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
1286fd56e061SDavid Gibson 
1287e01fd687SAlex Williamson     assert(!pci_dev->has_vga);
1288e01fd687SAlex Williamson 
1289e01fd687SAlex Williamson     assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE);
1290e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem;
1291fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_mem,
1292e01fd687SAlex Williamson                                         QEMU_PCI_VGA_MEM_BASE, mem, 1);
1293e01fd687SAlex Williamson 
1294e01fd687SAlex Williamson     assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE);
1295e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo;
1296fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_io,
1297e01fd687SAlex Williamson                                         QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1);
1298e01fd687SAlex Williamson 
1299e01fd687SAlex Williamson     assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE);
1300e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi;
1301fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_io,
1302e01fd687SAlex Williamson                                         QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1);
1303e01fd687SAlex Williamson     pci_dev->has_vga = true;
1304e01fd687SAlex Williamson 
1305e01fd687SAlex Williamson     pci_update_vga(pci_dev);
1306e01fd687SAlex Williamson }
1307e01fd687SAlex Williamson 
1308e01fd687SAlex Williamson void pci_unregister_vga(PCIDevice *pci_dev)
1309e01fd687SAlex Williamson {
1310fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
1311fd56e061SDavid Gibson 
1312e01fd687SAlex Williamson     if (!pci_dev->has_vga) {
1313e01fd687SAlex Williamson         return;
1314e01fd687SAlex Williamson     }
1315e01fd687SAlex Williamson 
1316fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_mem,
1317e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_MEM]);
1318fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_io,
1319e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]);
1320fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_io,
1321e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]);
1322e01fd687SAlex Williamson     pci_dev->has_vga = false;
1323e01fd687SAlex Williamson }
1324e01fd687SAlex Williamson 
1325315a1350SMichael S. Tsirkin pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
1326315a1350SMichael S. Tsirkin {
1327315a1350SMichael S. Tsirkin     return pci_dev->io_regions[region_num].addr;
1328315a1350SMichael S. Tsirkin }
1329315a1350SMichael S. Tsirkin 
13307c0fa8dfSKnut Omang static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg,
13317c0fa8dfSKnut Omang                                         uint8_t type, pcibus_t size)
13327c0fa8dfSKnut Omang {
13337c0fa8dfSKnut Omang     pcibus_t new_addr;
13347c0fa8dfSKnut Omang     if (!pci_is_vf(d)) {
13357c0fa8dfSKnut Omang         int bar = pci_bar(d, reg);
13367c0fa8dfSKnut Omang         if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
13377c0fa8dfSKnut Omang             new_addr = pci_get_quad(d->config + bar);
13387c0fa8dfSKnut Omang         } else {
13397c0fa8dfSKnut Omang             new_addr = pci_get_long(d->config + bar);
13407c0fa8dfSKnut Omang         }
13417c0fa8dfSKnut Omang     } else {
13427c0fa8dfSKnut Omang         PCIDevice *pf = d->exp.sriov_vf.pf;
13437c0fa8dfSKnut Omang         uint16_t sriov_cap = pf->exp.sriov_cap;
13447c0fa8dfSKnut Omang         int bar = sriov_cap + PCI_SRIOV_BAR + reg * 4;
13457c0fa8dfSKnut Omang         uint16_t vf_offset =
13467c0fa8dfSKnut Omang             pci_get_word(pf->config + sriov_cap + PCI_SRIOV_VF_OFFSET);
13477c0fa8dfSKnut Omang         uint16_t vf_stride =
13487c0fa8dfSKnut Omang             pci_get_word(pf->config + sriov_cap + PCI_SRIOV_VF_STRIDE);
13497c0fa8dfSKnut Omang         uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride;
13507c0fa8dfSKnut Omang 
13517c0fa8dfSKnut Omang         if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
13527c0fa8dfSKnut Omang             new_addr = pci_get_quad(pf->config + bar);
13537c0fa8dfSKnut Omang         } else {
13547c0fa8dfSKnut Omang             new_addr = pci_get_long(pf->config + bar);
13557c0fa8dfSKnut Omang         }
13567c0fa8dfSKnut Omang         new_addr += vf_num * size;
13577c0fa8dfSKnut Omang     }
13587c0fa8dfSKnut Omang     /* The ROM slot has a specific enable bit, keep it intact */
13597c0fa8dfSKnut Omang     if (reg != PCI_ROM_SLOT) {
13607c0fa8dfSKnut Omang         new_addr &= ~(size - 1);
13617c0fa8dfSKnut Omang     }
13627c0fa8dfSKnut Omang     return new_addr;
13637c0fa8dfSKnut Omang }
13647c0fa8dfSKnut Omang 
13657c0fa8dfSKnut Omang pcibus_t pci_bar_address(PCIDevice *d,
1366315a1350SMichael S. Tsirkin                          int reg, uint8_t type, pcibus_t size)
1367315a1350SMichael S. Tsirkin {
1368315a1350SMichael S. Tsirkin     pcibus_t new_addr, last_addr;
1369315a1350SMichael S. Tsirkin     uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
1370e4024630SLaurent Vivier     Object *machine = qdev_get_machine();
1371e4024630SLaurent Vivier     ObjectClass *oc = object_get_class(machine);
1372e4024630SLaurent Vivier     MachineClass *mc = MACHINE_CLASS(oc);
1373e4024630SLaurent Vivier     bool allow_0_address = mc->pci_allow_0_address;
1374315a1350SMichael S. Tsirkin 
1375315a1350SMichael S. Tsirkin     if (type & PCI_BASE_ADDRESS_SPACE_IO) {
1376315a1350SMichael S. Tsirkin         if (!(cmd & PCI_COMMAND_IO)) {
1377315a1350SMichael S. Tsirkin             return PCI_BAR_UNMAPPED;
1378315a1350SMichael S. Tsirkin         }
13797c0fa8dfSKnut Omang         new_addr = pci_config_get_bar_addr(d, reg, type, size);
1380315a1350SMichael S. Tsirkin         last_addr = new_addr + size - 1;
13819f1a029aSHervé Poussineau         /* Check if 32 bit BAR wraps around explicitly.
13829f1a029aSHervé Poussineau          * TODO: make priorities correct and remove this work around.
13839f1a029aSHervé Poussineau          */
1384e4024630SLaurent Vivier         if (last_addr <= new_addr || last_addr >= UINT32_MAX ||
1385e4024630SLaurent Vivier             (!allow_0_address && new_addr == 0)) {
1386315a1350SMichael S. Tsirkin             return PCI_BAR_UNMAPPED;
1387315a1350SMichael S. Tsirkin         }
1388315a1350SMichael S. Tsirkin         return new_addr;
1389315a1350SMichael S. Tsirkin     }
1390315a1350SMichael S. Tsirkin 
1391315a1350SMichael S. Tsirkin     if (!(cmd & PCI_COMMAND_MEMORY)) {
1392315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1393315a1350SMichael S. Tsirkin     }
13947c0fa8dfSKnut Omang     new_addr = pci_config_get_bar_addr(d, reg, type, size);
1395315a1350SMichael S. Tsirkin     /* the ROM slot has a specific enable bit */
1396315a1350SMichael S. Tsirkin     if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
1397315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1398315a1350SMichael S. Tsirkin     }
1399315a1350SMichael S. Tsirkin     new_addr &= ~(size - 1);
1400315a1350SMichael S. Tsirkin     last_addr = new_addr + size - 1;
1401315a1350SMichael S. Tsirkin     /* NOTE: we do not support wrapping */
1402315a1350SMichael S. Tsirkin     /* XXX: as we cannot support really dynamic
1403315a1350SMichael S. Tsirkin        mappings, we handle specific values as invalid
1404315a1350SMichael S. Tsirkin        mappings. */
1405e4024630SLaurent Vivier     if (last_addr <= new_addr || last_addr == PCI_BAR_UNMAPPED ||
1406e4024630SLaurent Vivier         (!allow_0_address && new_addr == 0)) {
1407315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1408315a1350SMichael S. Tsirkin     }
1409315a1350SMichael S. Tsirkin 
1410315a1350SMichael S. Tsirkin     /* Now pcibus_t is 64bit.
1411315a1350SMichael S. Tsirkin      * Check if 32 bit BAR wraps around explicitly.
1412315a1350SMichael S. Tsirkin      * Without this, PC ide doesn't work well.
1413315a1350SMichael S. Tsirkin      * TODO: remove this work around.
1414315a1350SMichael S. Tsirkin      */
1415315a1350SMichael S. Tsirkin     if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
1416315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1417315a1350SMichael S. Tsirkin     }
1418315a1350SMichael S. Tsirkin 
1419315a1350SMichael S. Tsirkin     /*
1420315a1350SMichael S. Tsirkin      * OS is allowed to set BAR beyond its addressable
1421315a1350SMichael S. Tsirkin      * bits. For example, 32 bit OS can set 64bit bar
1422315a1350SMichael S. Tsirkin      * to >4G. Check it. TODO: we might need to support
1423315a1350SMichael S. Tsirkin      * it in the future for e.g. PAE.
1424315a1350SMichael S. Tsirkin      */
1425315a1350SMichael S. Tsirkin     if (last_addr >= HWADDR_MAX) {
1426315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1427315a1350SMichael S. Tsirkin     }
1428315a1350SMichael S. Tsirkin 
1429315a1350SMichael S. Tsirkin     return new_addr;
1430315a1350SMichael S. Tsirkin }
1431315a1350SMichael S. Tsirkin 
1432315a1350SMichael S. Tsirkin static void pci_update_mappings(PCIDevice *d)
1433315a1350SMichael S. Tsirkin {
1434315a1350SMichael S. Tsirkin     PCIIORegion *r;
1435315a1350SMichael S. Tsirkin     int i;
1436315a1350SMichael S. Tsirkin     pcibus_t new_addr;
1437315a1350SMichael S. Tsirkin 
1438315a1350SMichael S. Tsirkin     for(i = 0; i < PCI_NUM_REGIONS; i++) {
1439315a1350SMichael S. Tsirkin         r = &d->io_regions[i];
1440315a1350SMichael S. Tsirkin 
1441315a1350SMichael S. Tsirkin         /* this region isn't registered */
1442315a1350SMichael S. Tsirkin         if (!r->size)
1443315a1350SMichael S. Tsirkin             continue;
1444315a1350SMichael S. Tsirkin 
1445315a1350SMichael S. Tsirkin         new_addr = pci_bar_address(d, i, r->type, r->size);
144623786d13SGerd Hoffmann         if (!d->has_power) {
144723786d13SGerd Hoffmann             new_addr = PCI_BAR_UNMAPPED;
144823786d13SGerd Hoffmann         }
1449315a1350SMichael S. Tsirkin 
1450315a1350SMichael S. Tsirkin         /* This bar isn't changed */
1451315a1350SMichael S. Tsirkin         if (new_addr == r->addr)
1452315a1350SMichael S. Tsirkin             continue;
1453315a1350SMichael S. Tsirkin 
1454315a1350SMichael S. Tsirkin         /* now do the real mapping */
1455315a1350SMichael S. Tsirkin         if (r->addr != PCI_BAR_UNMAPPED) {
1456deeb956cSLaurent Vivier             trace_pci_update_mappings_del(d->name, pci_dev_bus_num(d),
14577828d750SDon Koch                                           PCI_SLOT(d->devfn),
14580f288f85SLaszlo Ersek                                           PCI_FUNC(d->devfn),
14597828d750SDon Koch                                           i, r->addr, r->size);
1460315a1350SMichael S. Tsirkin             memory_region_del_subregion(r->address_space, r->memory);
1461315a1350SMichael S. Tsirkin         }
1462315a1350SMichael S. Tsirkin         r->addr = new_addr;
1463315a1350SMichael S. Tsirkin         if (r->addr != PCI_BAR_UNMAPPED) {
1464deeb956cSLaurent Vivier             trace_pci_update_mappings_add(d->name, pci_dev_bus_num(d),
14657828d750SDon Koch                                           PCI_SLOT(d->devfn),
14660f288f85SLaszlo Ersek                                           PCI_FUNC(d->devfn),
14677828d750SDon Koch                                           i, r->addr, r->size);
1468315a1350SMichael S. Tsirkin             memory_region_add_subregion_overlap(r->address_space,
1469315a1350SMichael S. Tsirkin                                                 r->addr, r->memory, 1);
1470315a1350SMichael S. Tsirkin         }
1471315a1350SMichael S. Tsirkin     }
1472e01fd687SAlex Williamson 
1473e01fd687SAlex Williamson     pci_update_vga(d);
1474315a1350SMichael S. Tsirkin }
1475315a1350SMichael S. Tsirkin 
1476315a1350SMichael S. Tsirkin static inline int pci_irq_disabled(PCIDevice *d)
1477315a1350SMichael S. Tsirkin {
1478315a1350SMichael S. Tsirkin     return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
1479315a1350SMichael S. Tsirkin }
1480315a1350SMichael S. Tsirkin 
1481315a1350SMichael S. Tsirkin /* Called after interrupt disabled field update in config space,
1482315a1350SMichael S. Tsirkin  * assert/deassert interrupts if necessary.
1483315a1350SMichael S. Tsirkin  * Gets original interrupt disable bit value (before update). */
1484315a1350SMichael S. Tsirkin static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
1485315a1350SMichael S. Tsirkin {
1486315a1350SMichael S. Tsirkin     int i, disabled = pci_irq_disabled(d);
1487315a1350SMichael S. Tsirkin     if (disabled == was_irq_disabled)
1488315a1350SMichael S. Tsirkin         return;
1489315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
1490315a1350SMichael S. Tsirkin         int state = pci_irq_state(d, i);
1491315a1350SMichael S. Tsirkin         pci_change_irq_level(d, i, disabled ? -state : state);
1492315a1350SMichael S. Tsirkin     }
1493315a1350SMichael S. Tsirkin }
1494315a1350SMichael S. Tsirkin 
1495315a1350SMichael S. Tsirkin uint32_t pci_default_read_config(PCIDevice *d,
1496315a1350SMichael S. Tsirkin                                  uint32_t address, int len)
1497315a1350SMichael S. Tsirkin {
1498315a1350SMichael S. Tsirkin     uint32_t val = 0;
1499315a1350SMichael S. Tsirkin 
1500f7d6a635SPrasad J Pandit     assert(address + len <= pci_config_size(d));
1501f7d6a635SPrasad J Pandit 
1502727b4866SAlex Williamson     if (pci_is_express_downstream_port(d) &&
1503727b4866SAlex Williamson         ranges_overlap(address, len, d->exp.exp_cap + PCI_EXP_LNKSTA, 2)) {
1504727b4866SAlex Williamson         pcie_sync_bridge_lnk(d);
1505727b4866SAlex Williamson     }
1506315a1350SMichael S. Tsirkin     memcpy(&val, d->config + address, len);
1507315a1350SMichael S. Tsirkin     return le32_to_cpu(val);
1508315a1350SMichael S. Tsirkin }
1509315a1350SMichael S. Tsirkin 
1510d7efb7e0SKnut Omang void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
1511315a1350SMichael S. Tsirkin {
1512315a1350SMichael S. Tsirkin     int i, was_irq_disabled = pci_irq_disabled(d);
1513d7efb7e0SKnut Omang     uint32_t val = val_in;
1514315a1350SMichael S. Tsirkin 
1515f7d6a635SPrasad J Pandit     assert(addr + l <= pci_config_size(d));
1516f7d6a635SPrasad J Pandit 
1517315a1350SMichael S. Tsirkin     for (i = 0; i < l; val >>= 8, ++i) {
1518315a1350SMichael S. Tsirkin         uint8_t wmask = d->wmask[addr + i];
1519315a1350SMichael S. Tsirkin         uint8_t w1cmask = d->w1cmask[addr + i];
1520315a1350SMichael S. Tsirkin         assert(!(wmask & w1cmask));
1521315a1350SMichael S. Tsirkin         d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
1522315a1350SMichael S. Tsirkin         d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
1523315a1350SMichael S. Tsirkin     }
1524315a1350SMichael S. Tsirkin     if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1525315a1350SMichael S. Tsirkin         ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1526315a1350SMichael S. Tsirkin         ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1527315a1350SMichael S. Tsirkin         range_covers_byte(addr, l, PCI_COMMAND))
1528315a1350SMichael S. Tsirkin         pci_update_mappings(d);
1529315a1350SMichael S. Tsirkin 
1530315a1350SMichael S. Tsirkin     if (range_covers_byte(addr, l, PCI_COMMAND)) {
1531315a1350SMichael S. Tsirkin         pci_update_irq_disabled(d, was_irq_disabled);
1532315a1350SMichael S. Tsirkin         memory_region_set_enabled(&d->bus_master_enable_region,
153323786d13SGerd Hoffmann                                   (pci_get_word(d->config + PCI_COMMAND)
153423786d13SGerd Hoffmann                                    & PCI_COMMAND_MASTER) && d->has_power);
1535315a1350SMichael S. Tsirkin     }
1536315a1350SMichael S. Tsirkin 
1537d7efb7e0SKnut Omang     msi_write_config(d, addr, val_in, l);
1538d7efb7e0SKnut Omang     msix_write_config(d, addr, val_in, l);
15397c0fa8dfSKnut Omang     pcie_sriov_config_write(d, addr, val_in, l);
1540315a1350SMichael S. Tsirkin }
1541315a1350SMichael S. Tsirkin 
1542315a1350SMichael S. Tsirkin /***********************************************************/
1543315a1350SMichael S. Tsirkin /* generic PCI irq support */
1544315a1350SMichael S. Tsirkin 
1545315a1350SMichael S. Tsirkin /* 0 <= irq_num <= 3. level must be 0 or 1 */
1546d98f08f5SMarcel Apfelbaum static void pci_irq_handler(void *opaque, int irq_num, int level)
1547315a1350SMichael S. Tsirkin {
1548315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = opaque;
1549315a1350SMichael S. Tsirkin     int change;
1550315a1350SMichael S. Tsirkin 
15518ddf5432SIsaku Yamahata     assert(0 <= irq_num && irq_num < PCI_NUM_PINS);
15528ddf5432SIsaku Yamahata     assert(level == 0 || level == 1);
1553315a1350SMichael S. Tsirkin     change = level - pci_irq_state(pci_dev, irq_num);
1554315a1350SMichael S. Tsirkin     if (!change)
1555315a1350SMichael S. Tsirkin         return;
1556315a1350SMichael S. Tsirkin 
1557315a1350SMichael S. Tsirkin     pci_set_irq_state(pci_dev, irq_num, level);
1558315a1350SMichael S. Tsirkin     pci_update_irq_status(pci_dev);
1559315a1350SMichael S. Tsirkin     if (pci_irq_disabled(pci_dev))
1560315a1350SMichael S. Tsirkin         return;
1561315a1350SMichael S. Tsirkin     pci_change_irq_level(pci_dev, irq_num, change);
1562315a1350SMichael S. Tsirkin }
1563315a1350SMichael S. Tsirkin 
1564d98f08f5SMarcel Apfelbaum qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
1565d98f08f5SMarcel Apfelbaum {
1566d98f08f5SMarcel Apfelbaum     int intx = pci_intx(pci_dev);
15678ddf5432SIsaku Yamahata     assert(0 <= intx && intx < PCI_NUM_PINS);
1568d98f08f5SMarcel Apfelbaum 
1569d98f08f5SMarcel Apfelbaum     return qemu_allocate_irq(pci_irq_handler, pci_dev, intx);
1570d98f08f5SMarcel Apfelbaum }
1571d98f08f5SMarcel Apfelbaum 
1572d98f08f5SMarcel Apfelbaum void pci_set_irq(PCIDevice *pci_dev, int level)
1573d98f08f5SMarcel Apfelbaum {
1574d98f08f5SMarcel Apfelbaum     int intx = pci_intx(pci_dev);
1575d98f08f5SMarcel Apfelbaum     pci_irq_handler(pci_dev, intx, level);
1576d98f08f5SMarcel Apfelbaum }
1577d98f08f5SMarcel Apfelbaum 
1578315a1350SMichael S. Tsirkin /* Special hooks used by device assignment */
1579315a1350SMichael S. Tsirkin void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq)
1580315a1350SMichael S. Tsirkin {
15810889464aSAlex Williamson     assert(pci_bus_is_root(bus));
1582315a1350SMichael S. Tsirkin     bus->route_intx_to_irq = route_intx_to_irq;
1583315a1350SMichael S. Tsirkin }
1584315a1350SMichael S. Tsirkin 
1585315a1350SMichael S. Tsirkin PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin)
1586315a1350SMichael S. Tsirkin {
1587315a1350SMichael S. Tsirkin     PCIBus *bus;
1588315a1350SMichael S. Tsirkin 
1589315a1350SMichael S. Tsirkin     do {
1590fd56e061SDavid Gibson         bus = pci_get_bus(dev);
1591315a1350SMichael S. Tsirkin         pin = bus->map_irq(dev, pin);
1592315a1350SMichael S. Tsirkin         dev = bus->parent_dev;
1593315a1350SMichael S. Tsirkin     } while (dev);
1594315a1350SMichael S. Tsirkin 
1595315a1350SMichael S. Tsirkin     if (!bus->route_intx_to_irq) {
1596312fd5f2SMarkus Armbruster         error_report("PCI: Bug - unimplemented PCI INTx routing (%s)",
1597315a1350SMichael S. Tsirkin                      object_get_typename(OBJECT(bus->qbus.parent)));
1598315a1350SMichael S. Tsirkin         return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 };
1599315a1350SMichael S. Tsirkin     }
1600315a1350SMichael S. Tsirkin 
1601315a1350SMichael S. Tsirkin     return bus->route_intx_to_irq(bus->irq_opaque, pin);
1602315a1350SMichael S. Tsirkin }
1603315a1350SMichael S. Tsirkin 
1604315a1350SMichael S. Tsirkin bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new)
1605315a1350SMichael S. Tsirkin {
1606315a1350SMichael S. Tsirkin     return old->mode != new->mode || old->irq != new->irq;
1607315a1350SMichael S. Tsirkin }
1608315a1350SMichael S. Tsirkin 
1609315a1350SMichael S. Tsirkin void pci_bus_fire_intx_routing_notifier(PCIBus *bus)
1610315a1350SMichael S. Tsirkin {
1611315a1350SMichael S. Tsirkin     PCIDevice *dev;
1612315a1350SMichael S. Tsirkin     PCIBus *sec;
1613315a1350SMichael S. Tsirkin     int i;
1614315a1350SMichael S. Tsirkin 
1615315a1350SMichael S. Tsirkin     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
1616315a1350SMichael S. Tsirkin         dev = bus->devices[i];
1617315a1350SMichael S. Tsirkin         if (dev && dev->intx_routing_notifier) {
1618315a1350SMichael S. Tsirkin             dev->intx_routing_notifier(dev);
1619315a1350SMichael S. Tsirkin         }
1620e5368f0dSAlex Williamson     }
1621e5368f0dSAlex Williamson 
1622315a1350SMichael S. Tsirkin     QLIST_FOREACH(sec, &bus->child, sibling) {
1623315a1350SMichael S. Tsirkin         pci_bus_fire_intx_routing_notifier(sec);
1624315a1350SMichael S. Tsirkin     }
1625315a1350SMichael S. Tsirkin }
1626315a1350SMichael S. Tsirkin 
1627315a1350SMichael S. Tsirkin void pci_device_set_intx_routing_notifier(PCIDevice *dev,
1628315a1350SMichael S. Tsirkin                                           PCIINTxRoutingNotifier notifier)
1629315a1350SMichael S. Tsirkin {
1630315a1350SMichael S. Tsirkin     dev->intx_routing_notifier = notifier;
1631315a1350SMichael S. Tsirkin }
1632315a1350SMichael S. Tsirkin 
1633315a1350SMichael S. Tsirkin /*
1634315a1350SMichael S. Tsirkin  * PCI-to-PCI bridge specification
1635315a1350SMichael S. Tsirkin  * 9.1: Interrupt routing. Table 9-1
1636315a1350SMichael S. Tsirkin  *
1637315a1350SMichael S. Tsirkin  * the PCI Express Base Specification, Revision 2.1
1638315a1350SMichael S. Tsirkin  * 2.2.8.1: INTx interrutp signaling - Rules
1639315a1350SMichael S. Tsirkin  *          the Implementation Note
1640315a1350SMichael S. Tsirkin  *          Table 2-20
1641315a1350SMichael S. Tsirkin  */
1642315a1350SMichael S. Tsirkin /*
1643315a1350SMichael S. Tsirkin  * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD
1644315a1350SMichael S. Tsirkin  * 0-origin unlike PCI interrupt pin register.
1645315a1350SMichael S. Tsirkin  */
1646315a1350SMichael S. Tsirkin int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
1647315a1350SMichael S. Tsirkin {
1648e8ec4adfSGreg Kurz     return pci_swizzle(PCI_SLOT(pci_dev->devfn), pin);
1649315a1350SMichael S. Tsirkin }
1650315a1350SMichael S. Tsirkin 
1651315a1350SMichael S. Tsirkin /***********************************************************/
1652315a1350SMichael S. Tsirkin /* monitor info on PCI */
1653315a1350SMichael S. Tsirkin 
1654315a1350SMichael S. Tsirkin typedef struct {
1655315a1350SMichael S. Tsirkin     uint16_t class;
1656315a1350SMichael S. Tsirkin     const char *desc;
1657315a1350SMichael S. Tsirkin     const char *fw_name;
1658315a1350SMichael S. Tsirkin     uint16_t fw_ign_bits;
1659315a1350SMichael S. Tsirkin } pci_class_desc;
1660315a1350SMichael S. Tsirkin 
1661315a1350SMichael S. Tsirkin static const pci_class_desc pci_class_descriptions[] =
1662315a1350SMichael S. Tsirkin {
1663315a1350SMichael S. Tsirkin     { 0x0001, "VGA controller", "display"},
1664315a1350SMichael S. Tsirkin     { 0x0100, "SCSI controller", "scsi"},
1665315a1350SMichael S. Tsirkin     { 0x0101, "IDE controller", "ide"},
1666315a1350SMichael S. Tsirkin     { 0x0102, "Floppy controller", "fdc"},
1667315a1350SMichael S. Tsirkin     { 0x0103, "IPI controller", "ipi"},
1668315a1350SMichael S. Tsirkin     { 0x0104, "RAID controller", "raid"},
1669315a1350SMichael S. Tsirkin     { 0x0106, "SATA controller"},
1670315a1350SMichael S. Tsirkin     { 0x0107, "SAS controller"},
1671315a1350SMichael S. Tsirkin     { 0x0180, "Storage controller"},
1672315a1350SMichael S. Tsirkin     { 0x0200, "Ethernet controller", "ethernet"},
1673315a1350SMichael S. Tsirkin     { 0x0201, "Token Ring controller", "token-ring"},
1674315a1350SMichael S. Tsirkin     { 0x0202, "FDDI controller", "fddi"},
1675315a1350SMichael S. Tsirkin     { 0x0203, "ATM controller", "atm"},
1676315a1350SMichael S. Tsirkin     { 0x0280, "Network controller"},
1677315a1350SMichael S. Tsirkin     { 0x0300, "VGA controller", "display", 0x00ff},
1678315a1350SMichael S. Tsirkin     { 0x0301, "XGA controller"},
1679315a1350SMichael S. Tsirkin     { 0x0302, "3D controller"},
1680315a1350SMichael S. Tsirkin     { 0x0380, "Display controller"},
1681315a1350SMichael S. Tsirkin     { 0x0400, "Video controller", "video"},
1682315a1350SMichael S. Tsirkin     { 0x0401, "Audio controller", "sound"},
1683315a1350SMichael S. Tsirkin     { 0x0402, "Phone"},
1684315a1350SMichael S. Tsirkin     { 0x0403, "Audio controller", "sound"},
1685315a1350SMichael S. Tsirkin     { 0x0480, "Multimedia controller"},
1686315a1350SMichael S. Tsirkin     { 0x0500, "RAM controller", "memory"},
1687315a1350SMichael S. Tsirkin     { 0x0501, "Flash controller", "flash"},
1688315a1350SMichael S. Tsirkin     { 0x0580, "Memory controller"},
1689315a1350SMichael S. Tsirkin     { 0x0600, "Host bridge", "host"},
1690315a1350SMichael S. Tsirkin     { 0x0601, "ISA bridge", "isa"},
1691315a1350SMichael S. Tsirkin     { 0x0602, "EISA bridge", "eisa"},
1692315a1350SMichael S. Tsirkin     { 0x0603, "MC bridge", "mca"},
16934c41425dSGerd Hoffmann     { 0x0604, "PCI bridge", "pci-bridge"},
1694315a1350SMichael S. Tsirkin     { 0x0605, "PCMCIA bridge", "pcmcia"},
1695315a1350SMichael S. Tsirkin     { 0x0606, "NUBUS bridge", "nubus"},
1696315a1350SMichael S. Tsirkin     { 0x0607, "CARDBUS bridge", "cardbus"},
1697315a1350SMichael S. Tsirkin     { 0x0608, "RACEWAY bridge"},
1698315a1350SMichael S. Tsirkin     { 0x0680, "Bridge"},
1699315a1350SMichael S. Tsirkin     { 0x0700, "Serial port", "serial"},
1700315a1350SMichael S. Tsirkin     { 0x0701, "Parallel port", "parallel"},
1701315a1350SMichael S. Tsirkin     { 0x0800, "Interrupt controller", "interrupt-controller"},
1702315a1350SMichael S. Tsirkin     { 0x0801, "DMA controller", "dma-controller"},
1703315a1350SMichael S. Tsirkin     { 0x0802, "Timer", "timer"},
1704315a1350SMichael S. Tsirkin     { 0x0803, "RTC", "rtc"},
1705315a1350SMichael S. Tsirkin     { 0x0900, "Keyboard", "keyboard"},
1706315a1350SMichael S. Tsirkin     { 0x0901, "Pen", "pen"},
1707315a1350SMichael S. Tsirkin     { 0x0902, "Mouse", "mouse"},
1708315a1350SMichael S. Tsirkin     { 0x0A00, "Dock station", "dock", 0x00ff},
1709315a1350SMichael S. Tsirkin     { 0x0B00, "i386 cpu", "cpu", 0x00ff},
1710d1e9e646SRebecca Cran     { 0x0c00, "Firewire controller", "firewire"},
1711315a1350SMichael S. Tsirkin     { 0x0c01, "Access bus controller", "access-bus"},
1712315a1350SMichael S. Tsirkin     { 0x0c02, "SSA controller", "ssa"},
1713315a1350SMichael S. Tsirkin     { 0x0c03, "USB controller", "usb"},
1714315a1350SMichael S. Tsirkin     { 0x0c04, "Fibre channel controller", "fibre-channel"},
1715315a1350SMichael S. Tsirkin     { 0x0c05, "SMBus"},
1716315a1350SMichael S. Tsirkin     { 0, NULL}
1717315a1350SMichael S. Tsirkin };
1718315a1350SMichael S. Tsirkin 
17192914fc61SPeter Xu void pci_for_each_device_under_bus_reverse(PCIBus *bus,
1720b3dcf94fSPeter Xu                                            pci_bus_dev_fn fn,
1721a8eeafdaSGreg Kurz                                            void *opaque)
1722a8eeafdaSGreg Kurz {
1723a8eeafdaSGreg Kurz     PCIDevice *d;
1724a8eeafdaSGreg Kurz     int devfn;
1725a8eeafdaSGreg Kurz 
1726a8eeafdaSGreg Kurz     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1727a8eeafdaSGreg Kurz         d = bus->devices[ARRAY_SIZE(bus->devices) - 1 - devfn];
1728a8eeafdaSGreg Kurz         if (d) {
1729a8eeafdaSGreg Kurz             fn(bus, d, opaque);
1730a8eeafdaSGreg Kurz         }
1731a8eeafdaSGreg Kurz     }
1732a8eeafdaSGreg Kurz }
1733a8eeafdaSGreg Kurz 
1734a8eeafdaSGreg Kurz void pci_for_each_device_reverse(PCIBus *bus, int bus_num,
1735b3dcf94fSPeter Xu                                  pci_bus_dev_fn fn, void *opaque)
1736a8eeafdaSGreg Kurz {
1737a8eeafdaSGreg Kurz     bus = pci_find_bus_nr(bus, bus_num);
1738a8eeafdaSGreg Kurz 
1739a8eeafdaSGreg Kurz     if (bus) {
1740a8eeafdaSGreg Kurz         pci_for_each_device_under_bus_reverse(bus, fn, opaque);
1741a8eeafdaSGreg Kurz     }
1742a8eeafdaSGreg Kurz }
1743a8eeafdaSGreg Kurz 
17442914fc61SPeter Xu void pci_for_each_device_under_bus(PCIBus *bus,
1745b3dcf94fSPeter Xu                                    pci_bus_dev_fn fn, void *opaque)
1746315a1350SMichael S. Tsirkin {
1747315a1350SMichael S. Tsirkin     PCIDevice *d;
1748315a1350SMichael S. Tsirkin     int devfn;
1749315a1350SMichael S. Tsirkin 
1750315a1350SMichael S. Tsirkin     for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1751315a1350SMichael S. Tsirkin         d = bus->devices[devfn];
1752315a1350SMichael S. Tsirkin         if (d) {
1753315a1350SMichael S. Tsirkin             fn(bus, d, opaque);
1754315a1350SMichael S. Tsirkin         }
1755315a1350SMichael S. Tsirkin     }
1756315a1350SMichael S. Tsirkin }
1757315a1350SMichael S. Tsirkin 
1758315a1350SMichael S. Tsirkin void pci_for_each_device(PCIBus *bus, int bus_num,
1759b3dcf94fSPeter Xu                          pci_bus_dev_fn fn, void *opaque)
1760315a1350SMichael S. Tsirkin {
1761315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
1762315a1350SMichael S. Tsirkin 
1763315a1350SMichael S. Tsirkin     if (bus) {
1764315a1350SMichael S. Tsirkin         pci_for_each_device_under_bus(bus, fn, opaque);
1765315a1350SMichael S. Tsirkin     }
1766315a1350SMichael S. Tsirkin }
1767315a1350SMichael S. Tsirkin 
1768315a1350SMichael S. Tsirkin static const pci_class_desc *get_class_desc(int class)
1769315a1350SMichael S. Tsirkin {
1770315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
1771315a1350SMichael S. Tsirkin 
1772315a1350SMichael S. Tsirkin     desc = pci_class_descriptions;
1773315a1350SMichael S. Tsirkin     while (desc->desc && class != desc->class) {
1774315a1350SMichael S. Tsirkin         desc++;
1775315a1350SMichael S. Tsirkin     }
1776315a1350SMichael S. Tsirkin 
1777315a1350SMichael S. Tsirkin     return desc;
1778315a1350SMichael S. Tsirkin }
1779315a1350SMichael S. Tsirkin 
1780315a1350SMichael S. Tsirkin static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
1781315a1350SMichael S. Tsirkin 
1782315a1350SMichael S. Tsirkin static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
1783315a1350SMichael S. Tsirkin {
178495b3a8c8SEric Blake     PciMemoryRegionList *head = NULL, **tail = &head;
1785315a1350SMichael S. Tsirkin     int i;
1786315a1350SMichael S. Tsirkin 
1787315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; i++) {
1788315a1350SMichael S. Tsirkin         const PCIIORegion *r = &dev->io_regions[i];
178995b3a8c8SEric Blake         PciMemoryRegion *region;
1790315a1350SMichael S. Tsirkin 
1791315a1350SMichael S. Tsirkin         if (!r->size) {
1792315a1350SMichael S. Tsirkin             continue;
1793315a1350SMichael S. Tsirkin         }
1794315a1350SMichael S. Tsirkin 
1795315a1350SMichael S. Tsirkin         region = g_malloc0(sizeof(*region));
1796315a1350SMichael S. Tsirkin 
1797315a1350SMichael S. Tsirkin         if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
179895b3a8c8SEric Blake             region->type = g_strdup("io");
1799315a1350SMichael S. Tsirkin         } else {
180095b3a8c8SEric Blake             region->type = g_strdup("memory");
180195b3a8c8SEric Blake             region->has_prefetch = true;
180295b3a8c8SEric Blake             region->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
180395b3a8c8SEric Blake             region->has_mem_type_64 = true;
180495b3a8c8SEric Blake             region->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
1805315a1350SMichael S. Tsirkin         }
1806315a1350SMichael S. Tsirkin 
180795b3a8c8SEric Blake         region->bar = i;
180895b3a8c8SEric Blake         region->address = r->addr;
180995b3a8c8SEric Blake         region->size = r->size;
1810315a1350SMichael S. Tsirkin 
181195b3a8c8SEric Blake         QAPI_LIST_APPEND(tail, region);
1812315a1350SMichael S. Tsirkin     }
1813315a1350SMichael S. Tsirkin 
1814315a1350SMichael S. Tsirkin     return head;
1815315a1350SMichael S. Tsirkin }
1816315a1350SMichael S. Tsirkin 
1817315a1350SMichael S. Tsirkin static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
1818315a1350SMichael S. Tsirkin                                            int bus_num)
1819315a1350SMichael S. Tsirkin {
1820315a1350SMichael S. Tsirkin     PciBridgeInfo *info;
18219fa02cd1SEric Blake     PciMemoryRange *range;
1822315a1350SMichael S. Tsirkin 
18239fa02cd1SEric Blake     info = g_new0(PciBridgeInfo, 1);
1824315a1350SMichael S. Tsirkin 
18259fa02cd1SEric Blake     info->bus = g_new0(PciBusInfo, 1);
18269fa02cd1SEric Blake     info->bus->number = dev->config[PCI_PRIMARY_BUS];
18279fa02cd1SEric Blake     info->bus->secondary = dev->config[PCI_SECONDARY_BUS];
18289fa02cd1SEric Blake     info->bus->subordinate = dev->config[PCI_SUBORDINATE_BUS];
1829315a1350SMichael S. Tsirkin 
18309fa02cd1SEric Blake     range = info->bus->io_range = g_new0(PciMemoryRange, 1);
18319fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
18329fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
1833315a1350SMichael S. Tsirkin 
18349fa02cd1SEric Blake     range = info->bus->memory_range = g_new0(PciMemoryRange, 1);
18359fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
18369fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
1837315a1350SMichael S. Tsirkin 
18389fa02cd1SEric Blake     range = info->bus->prefetchable_range = g_new0(PciMemoryRange, 1);
18399fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
18409fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
1841315a1350SMichael S. Tsirkin 
1842315a1350SMichael S. Tsirkin     if (dev->config[PCI_SECONDARY_BUS] != 0) {
1843315a1350SMichael S. Tsirkin         PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]);
1844315a1350SMichael S. Tsirkin         if (child_bus) {
1845315a1350SMichael S. Tsirkin             info->has_devices = true;
1846315a1350SMichael S. Tsirkin             info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]);
1847315a1350SMichael S. Tsirkin         }
1848315a1350SMichael S. Tsirkin     }
1849315a1350SMichael S. Tsirkin 
1850315a1350SMichael S. Tsirkin     return info;
1851315a1350SMichael S. Tsirkin }
1852315a1350SMichael S. Tsirkin 
1853315a1350SMichael S. Tsirkin static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
1854315a1350SMichael S. Tsirkin                                            int bus_num)
1855315a1350SMichael S. Tsirkin {
1856315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
1857315a1350SMichael S. Tsirkin     PciDeviceInfo *info;
1858315a1350SMichael S. Tsirkin     uint8_t type;
1859315a1350SMichael S. Tsirkin     int class;
1860315a1350SMichael S. Tsirkin 
18619fa02cd1SEric Blake     info = g_new0(PciDeviceInfo, 1);
1862315a1350SMichael S. Tsirkin     info->bus = bus_num;
1863315a1350SMichael S. Tsirkin     info->slot = PCI_SLOT(dev->devfn);
1864315a1350SMichael S. Tsirkin     info->function = PCI_FUNC(dev->devfn);
1865315a1350SMichael S. Tsirkin 
18669fa02cd1SEric Blake     info->class_info = g_new0(PciDeviceClass, 1);
1867315a1350SMichael S. Tsirkin     class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
18689fa02cd1SEric Blake     info->class_info->q_class = class;
1869315a1350SMichael S. Tsirkin     desc = get_class_desc(class);
1870315a1350SMichael S. Tsirkin     if (desc->desc) {
18719fa02cd1SEric Blake         info->class_info->has_desc = true;
18729fa02cd1SEric Blake         info->class_info->desc = g_strdup(desc->desc);
1873315a1350SMichael S. Tsirkin     }
1874315a1350SMichael S. Tsirkin 
18759fa02cd1SEric Blake     info->id = g_new0(PciDeviceId, 1);
18769fa02cd1SEric Blake     info->id->vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
18779fa02cd1SEric Blake     info->id->device = pci_get_word(dev->config + PCI_DEVICE_ID);
1878315a1350SMichael S. Tsirkin     info->regions = qmp_query_pci_regions(dev);
1879315a1350SMichael S. Tsirkin     info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
1880315a1350SMichael S. Tsirkin 
188112fcf49cSPeter Xu     info->irq_pin = dev->config[PCI_INTERRUPT_PIN];
1882315a1350SMichael S. Tsirkin     if (dev->config[PCI_INTERRUPT_PIN] != 0) {
1883315a1350SMichael S. Tsirkin         info->has_irq = true;
1884315a1350SMichael S. Tsirkin         info->irq = dev->config[PCI_INTERRUPT_LINE];
1885315a1350SMichael S. Tsirkin     }
1886315a1350SMichael S. Tsirkin 
1887315a1350SMichael S. Tsirkin     type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1888315a1350SMichael S. Tsirkin     if (type == PCI_HEADER_TYPE_BRIDGE) {
1889315a1350SMichael S. Tsirkin         info->has_pci_bridge = true;
1890315a1350SMichael S. Tsirkin         info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
189118613dc6SDenis V. Lunev     } else if (type == PCI_HEADER_TYPE_NORMAL) {
189218613dc6SDenis V. Lunev         info->id->has_subsystem = info->id->has_subsystem_vendor = true;
189318613dc6SDenis V. Lunev         info->id->subsystem = pci_get_word(dev->config + PCI_SUBSYSTEM_ID);
189418613dc6SDenis V. Lunev         info->id->subsystem_vendor =
189518613dc6SDenis V. Lunev             pci_get_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID);
189618613dc6SDenis V. Lunev     } else if (type == PCI_HEADER_TYPE_CARDBUS) {
189718613dc6SDenis V. Lunev         info->id->has_subsystem = info->id->has_subsystem_vendor = true;
189818613dc6SDenis V. Lunev         info->id->subsystem = pci_get_word(dev->config + PCI_CB_SUBSYSTEM_ID);
189918613dc6SDenis V. Lunev         info->id->subsystem_vendor =
190018613dc6SDenis V. Lunev             pci_get_word(dev->config + PCI_CB_SUBSYSTEM_VENDOR_ID);
1901315a1350SMichael S. Tsirkin     }
1902315a1350SMichael S. Tsirkin 
1903315a1350SMichael S. Tsirkin     return info;
1904315a1350SMichael S. Tsirkin }
1905315a1350SMichael S. Tsirkin 
1906315a1350SMichael S. Tsirkin static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
1907315a1350SMichael S. Tsirkin {
190895b3a8c8SEric Blake     PciDeviceInfoList *head = NULL, **tail = &head;
1909315a1350SMichael S. Tsirkin     PCIDevice *dev;
1910315a1350SMichael S. Tsirkin     int devfn;
1911315a1350SMichael S. Tsirkin 
1912315a1350SMichael S. Tsirkin     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1913315a1350SMichael S. Tsirkin         dev = bus->devices[devfn];
1914315a1350SMichael S. Tsirkin         if (dev) {
191595b3a8c8SEric Blake             QAPI_LIST_APPEND(tail, qmp_query_pci_device(dev, bus, bus_num));
1916315a1350SMichael S. Tsirkin         }
1917315a1350SMichael S. Tsirkin     }
1918315a1350SMichael S. Tsirkin 
1919315a1350SMichael S. Tsirkin     return head;
1920315a1350SMichael S. Tsirkin }
1921315a1350SMichael S. Tsirkin 
1922315a1350SMichael S. Tsirkin static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
1923315a1350SMichael S. Tsirkin {
1924315a1350SMichael S. Tsirkin     PciInfo *info = NULL;
1925315a1350SMichael S. Tsirkin 
1926315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
1927315a1350SMichael S. Tsirkin     if (bus) {
1928315a1350SMichael S. Tsirkin         info = g_malloc0(sizeof(*info));
1929315a1350SMichael S. Tsirkin         info->bus = bus_num;
1930315a1350SMichael S. Tsirkin         info->devices = qmp_query_pci_devices(bus, bus_num);
1931315a1350SMichael S. Tsirkin     }
1932315a1350SMichael S. Tsirkin 
1933315a1350SMichael S. Tsirkin     return info;
1934315a1350SMichael S. Tsirkin }
1935315a1350SMichael S. Tsirkin 
1936315a1350SMichael S. Tsirkin PciInfoList *qmp_query_pci(Error **errp)
1937315a1350SMichael S. Tsirkin {
193895b3a8c8SEric Blake     PciInfoList *head = NULL, **tail = &head;
19397588e2b0SDavid Gibson     PCIHostState *host_bridge;
1940315a1350SMichael S. Tsirkin 
19417588e2b0SDavid Gibson     QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
194295b3a8c8SEric Blake         QAPI_LIST_APPEND(tail,
194395b3a8c8SEric Blake                          qmp_query_pci_bus(host_bridge->bus,
194495b3a8c8SEric Blake                                            pci_bus_num(host_bridge->bus)));
1945315a1350SMichael S. Tsirkin     }
1946315a1350SMichael S. Tsirkin 
1947315a1350SMichael S. Tsirkin     return head;
1948315a1350SMichael S. Tsirkin }
1949315a1350SMichael S. Tsirkin 
1950315a1350SMichael S. Tsirkin /* Initialize a PCI NIC.  */
195151f7cb97SThomas Huth PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
195229b358f9SDavid Gibson                                const char *default_model,
195351f7cb97SThomas Huth                                const char *default_devaddr)
1954315a1350SMichael S. Tsirkin {
1955315a1350SMichael S. Tsirkin     const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
195652310c3fSPaolo Bonzini     GSList *list;
195752310c3fSPaolo Bonzini     GPtrArray *pci_nic_models;
1958315a1350SMichael S. Tsirkin     PCIBus *bus;
1959315a1350SMichael S. Tsirkin     PCIDevice *pci_dev;
1960315a1350SMichael S. Tsirkin     DeviceState *dev;
196151f7cb97SThomas Huth     int devfn;
1962315a1350SMichael S. Tsirkin     int i;
19632ad778b8SDavid Gibson     int dom, busnr;
19642ad778b8SDavid Gibson     unsigned slot;
1965315a1350SMichael S. Tsirkin 
196652310c3fSPaolo Bonzini     if (nd->model && !strcmp(nd->model, "virtio")) {
196752310c3fSPaolo Bonzini         g_free(nd->model);
196852310c3fSPaolo Bonzini         nd->model = g_strdup("virtio-net-pci");
196952310c3fSPaolo Bonzini     }
197052310c3fSPaolo Bonzini 
197152310c3fSPaolo Bonzini     list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
197252310c3fSPaolo Bonzini     pci_nic_models = g_ptr_array_new();
197352310c3fSPaolo Bonzini     while (list) {
197452310c3fSPaolo Bonzini         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
197552310c3fSPaolo Bonzini                                              TYPE_DEVICE);
197652310c3fSPaolo Bonzini         GSList *next;
197752310c3fSPaolo Bonzini         if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
197852310c3fSPaolo Bonzini             dc->user_creatable) {
197952310c3fSPaolo Bonzini             const char *name = object_class_get_name(list->data);
198000823980SThomas Huth             /*
198100823980SThomas Huth              * A network device might also be something else than a NIC, see
198200823980SThomas Huth              * e.g. the "rocker" device. Thus we have to look for the "netdev"
198300823980SThomas Huth              * property, too. Unfortunately, some devices like virtio-net only
198400823980SThomas Huth              * create this property during instance_init, so we have to create
198500823980SThomas Huth              * a temporary instance here to be able to check it.
198600823980SThomas Huth              */
198700823980SThomas Huth             Object *obj = object_new_with_class(OBJECT_CLASS(dc));
1988efba1595SDaniel P. Berrangé             if (object_property_find(obj, "netdev")) {
198952310c3fSPaolo Bonzini                 g_ptr_array_add(pci_nic_models, (gpointer)name);
199052310c3fSPaolo Bonzini             }
199100823980SThomas Huth             object_unref(obj);
199200823980SThomas Huth         }
199352310c3fSPaolo Bonzini         next = list->next;
199452310c3fSPaolo Bonzini         g_slist_free_1(list);
199552310c3fSPaolo Bonzini         list = next;
199652310c3fSPaolo Bonzini     }
199752310c3fSPaolo Bonzini     g_ptr_array_add(pci_nic_models, NULL);
199852310c3fSPaolo Bonzini 
199952310c3fSPaolo Bonzini     if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) {
200051f7cb97SThomas Huth         exit(0);
200151f7cb97SThomas Huth     }
200251f7cb97SThomas Huth 
200352310c3fSPaolo Bonzini     i = qemu_find_nic_model(nd, (const char **)pci_nic_models->pdata,
200452310c3fSPaolo Bonzini                             default_model);
200551f7cb97SThomas Huth     if (i < 0) {
200651f7cb97SThomas Huth         exit(1);
200751f7cb97SThomas Huth     }
2008315a1350SMichael S. Tsirkin 
20092ad778b8SDavid Gibson     if (!rootbus) {
20102ad778b8SDavid Gibson         error_report("No primary PCI bus");
20112ad778b8SDavid Gibson         exit(1);
20122ad778b8SDavid Gibson     }
20132ad778b8SDavid Gibson 
20142ad778b8SDavid Gibson     assert(!rootbus->parent_dev);
20152ad778b8SDavid Gibson 
20162ad778b8SDavid Gibson     if (!devaddr) {
20172ad778b8SDavid Gibson         devfn = -1;
20182ad778b8SDavid Gibson         busnr = 0;
20192ad778b8SDavid Gibson     } else {
20202ad778b8SDavid Gibson         if (pci_parse_devaddr(devaddr, &dom, &busnr, &slot, NULL) < 0) {
20212ad778b8SDavid Gibson             error_report("Invalid PCI device address %s for device %s",
20222ad778b8SDavid Gibson                          devaddr, nd->model);
20232ad778b8SDavid Gibson             exit(1);
20242ad778b8SDavid Gibson         }
20252ad778b8SDavid Gibson 
20262ad778b8SDavid Gibson         if (dom != 0) {
20272ad778b8SDavid Gibson             error_report("No support for non-zero PCI domains");
20282ad778b8SDavid Gibson             exit(1);
20292ad778b8SDavid Gibson         }
20302ad778b8SDavid Gibson 
20312ad778b8SDavid Gibson         devfn = PCI_DEVFN(slot, 0);
20322ad778b8SDavid Gibson     }
20332ad778b8SDavid Gibson 
20342ad778b8SDavid Gibson     bus = pci_find_bus_nr(rootbus, busnr);
2035315a1350SMichael S. Tsirkin     if (!bus) {
2036315a1350SMichael S. Tsirkin         error_report("Invalid PCI device address %s for device %s",
203752310c3fSPaolo Bonzini                      devaddr, nd->model);
203851f7cb97SThomas Huth         exit(1);
2039315a1350SMichael S. Tsirkin     }
2040315a1350SMichael S. Tsirkin 
20419307d06dSMarkus Armbruster     pci_dev = pci_new(devfn, nd->model);
2042315a1350SMichael S. Tsirkin     dev = &pci_dev->qdev;
2043315a1350SMichael S. Tsirkin     qdev_set_nic_properties(dev, nd);
20449307d06dSMarkus Armbruster     pci_realize_and_unref(pci_dev, bus, &error_fatal);
204552310c3fSPaolo Bonzini     g_ptr_array_free(pci_nic_models, true);
204651f7cb97SThomas Huth     return pci_dev;
2047315a1350SMichael S. Tsirkin }
2048315a1350SMichael S. Tsirkin 
2049315a1350SMichael S. Tsirkin PCIDevice *pci_vga_init(PCIBus *bus)
2050315a1350SMichael S. Tsirkin {
2051f9bcb2d6SGautam Agrawal     vga_interface_created = true;
2052315a1350SMichael S. Tsirkin     switch (vga_interface_type) {
2053315a1350SMichael S. Tsirkin     case VGA_CIRRUS:
2054315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "cirrus-vga");
2055315a1350SMichael S. Tsirkin     case VGA_QXL:
2056315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "qxl-vga");
2057315a1350SMichael S. Tsirkin     case VGA_STD:
2058315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "VGA");
2059315a1350SMichael S. Tsirkin     case VGA_VMWARE:
2060315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "vmware-svga");
2061a94f0c5cSGerd Hoffmann     case VGA_VIRTIO:
2062a94f0c5cSGerd Hoffmann         return pci_create_simple(bus, -1, "virtio-vga");
2063315a1350SMichael S. Tsirkin     case VGA_NONE:
2064315a1350SMichael S. Tsirkin     default: /* Other non-PCI types. Checking for unsupported types is already
2065315a1350SMichael S. Tsirkin                 done in vl.c. */
2066315a1350SMichael S. Tsirkin         return NULL;
2067315a1350SMichael S. Tsirkin     }
2068315a1350SMichael S. Tsirkin }
2069315a1350SMichael S. Tsirkin 
2070315a1350SMichael S. Tsirkin /* Whether a given bus number is in range of the secondary
2071315a1350SMichael S. Tsirkin  * bus of the given bridge device. */
2072315a1350SMichael S. Tsirkin static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
2073315a1350SMichael S. Tsirkin {
2074315a1350SMichael S. Tsirkin     return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
2075315a1350SMichael S. Tsirkin              PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
207609e5b819SMarcel Apfelbaum         dev->config[PCI_SECONDARY_BUS] <= bus_num &&
2077315a1350SMichael S. Tsirkin         bus_num <= dev->config[PCI_SUBORDINATE_BUS];
2078315a1350SMichael S. Tsirkin }
2079315a1350SMichael S. Tsirkin 
208009e5b819SMarcel Apfelbaum /* Whether a given bus number is in a range of a root bus */
208109e5b819SMarcel Apfelbaum static bool pci_root_bus_in_range(PCIBus *bus, int bus_num)
208209e5b819SMarcel Apfelbaum {
208309e5b819SMarcel Apfelbaum     int i;
208409e5b819SMarcel Apfelbaum 
208509e5b819SMarcel Apfelbaum     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
208609e5b819SMarcel Apfelbaum         PCIDevice *dev = bus->devices[i];
208709e5b819SMarcel Apfelbaum 
208809e5b819SMarcel Apfelbaum         if (dev && PCI_DEVICE_GET_CLASS(dev)->is_bridge) {
208909e5b819SMarcel Apfelbaum             if (pci_secondary_bus_in_range(dev, bus_num)) {
209009e5b819SMarcel Apfelbaum                 return true;
209109e5b819SMarcel Apfelbaum             }
209209e5b819SMarcel Apfelbaum         }
209309e5b819SMarcel Apfelbaum     }
209409e5b819SMarcel Apfelbaum 
209509e5b819SMarcel Apfelbaum     return false;
209609e5b819SMarcel Apfelbaum }
209709e5b819SMarcel Apfelbaum 
2098315a1350SMichael S. Tsirkin static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
2099315a1350SMichael S. Tsirkin {
2100315a1350SMichael S. Tsirkin     PCIBus *sec;
2101315a1350SMichael S. Tsirkin 
2102315a1350SMichael S. Tsirkin     if (!bus) {
2103315a1350SMichael S. Tsirkin         return NULL;
2104315a1350SMichael S. Tsirkin     }
2105315a1350SMichael S. Tsirkin 
2106315a1350SMichael S. Tsirkin     if (pci_bus_num(bus) == bus_num) {
2107315a1350SMichael S. Tsirkin         return bus;
2108315a1350SMichael S. Tsirkin     }
2109315a1350SMichael S. Tsirkin 
2110315a1350SMichael S. Tsirkin     /* Consider all bus numbers in range for the host pci bridge. */
21110889464aSAlex Williamson     if (!pci_bus_is_root(bus) &&
2112315a1350SMichael S. Tsirkin         !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
2113315a1350SMichael S. Tsirkin         return NULL;
2114315a1350SMichael S. Tsirkin     }
2115315a1350SMichael S. Tsirkin 
2116315a1350SMichael S. Tsirkin     /* try child bus */
2117315a1350SMichael S. Tsirkin     for (; bus; bus = sec) {
2118315a1350SMichael S. Tsirkin         QLIST_FOREACH(sec, &bus->child, sibling) {
211909e5b819SMarcel Apfelbaum             if (pci_bus_num(sec) == bus_num) {
2120315a1350SMichael S. Tsirkin                 return sec;
2121315a1350SMichael S. Tsirkin             }
212209e5b819SMarcel Apfelbaum             /* PXB buses assumed to be children of bus 0 */
212309e5b819SMarcel Apfelbaum             if (pci_bus_is_root(sec)) {
212409e5b819SMarcel Apfelbaum                 if (pci_root_bus_in_range(sec, bus_num)) {
212509e5b819SMarcel Apfelbaum                     break;
212609e5b819SMarcel Apfelbaum                 }
212709e5b819SMarcel Apfelbaum             } else {
2128315a1350SMichael S. Tsirkin                 if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
2129315a1350SMichael S. Tsirkin                     break;
2130315a1350SMichael S. Tsirkin                 }
2131315a1350SMichael S. Tsirkin             }
2132315a1350SMichael S. Tsirkin         }
213309e5b819SMarcel Apfelbaum     }
2134315a1350SMichael S. Tsirkin 
2135315a1350SMichael S. Tsirkin     return NULL;
2136315a1350SMichael S. Tsirkin }
2137315a1350SMichael S. Tsirkin 
2138b3dcf94fSPeter Xu void pci_for_each_bus_depth_first(PCIBus *bus, pci_bus_ret_fn begin,
2139b3dcf94fSPeter Xu                                   pci_bus_fn end, void *parent_state)
2140eb0acfddSMichael S. Tsirkin {
2141eb0acfddSMichael S. Tsirkin     PCIBus *sec;
2142eb0acfddSMichael S. Tsirkin     void *state;
2143eb0acfddSMichael S. Tsirkin 
2144eb0acfddSMichael S. Tsirkin     if (!bus) {
2145eb0acfddSMichael S. Tsirkin         return;
2146eb0acfddSMichael S. Tsirkin     }
2147eb0acfddSMichael S. Tsirkin 
2148eb0acfddSMichael S. Tsirkin     if (begin) {
2149eb0acfddSMichael S. Tsirkin         state = begin(bus, parent_state);
2150eb0acfddSMichael S. Tsirkin     } else {
2151eb0acfddSMichael S. Tsirkin         state = parent_state;
2152eb0acfddSMichael S. Tsirkin     }
2153eb0acfddSMichael S. Tsirkin 
2154eb0acfddSMichael S. Tsirkin     QLIST_FOREACH(sec, &bus->child, sibling) {
2155eb0acfddSMichael S. Tsirkin         pci_for_each_bus_depth_first(sec, begin, end, state);
2156eb0acfddSMichael S. Tsirkin     }
2157eb0acfddSMichael S. Tsirkin 
2158eb0acfddSMichael S. Tsirkin     if (end) {
2159eb0acfddSMichael S. Tsirkin         end(bus, state);
2160eb0acfddSMichael S. Tsirkin     }
2161eb0acfddSMichael S. Tsirkin }
2162eb0acfddSMichael S. Tsirkin 
2163eb0acfddSMichael S. Tsirkin 
2164315a1350SMichael S. Tsirkin PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
2165315a1350SMichael S. Tsirkin {
2166315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
2167315a1350SMichael S. Tsirkin 
2168315a1350SMichael S. Tsirkin     if (!bus)
2169315a1350SMichael S. Tsirkin         return NULL;
2170315a1350SMichael S. Tsirkin 
2171315a1350SMichael S. Tsirkin     return bus->devices[devfn];
2172315a1350SMichael S. Tsirkin }
2173315a1350SMichael S. Tsirkin 
2174133e9b22SMarkus Armbruster static void pci_qdev_realize(DeviceState *qdev, Error **errp)
2175315a1350SMichael S. Tsirkin {
2176315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = (PCIDevice *)qdev;
2177315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
2178d61a363dSYoni Bettan     ObjectClass *klass = OBJECT_CLASS(pc);
2179133e9b22SMarkus Armbruster     Error *local_err = NULL;
2180315a1350SMichael S. Tsirkin     bool is_default_rom;
21814f5b6a05SJens Freimann     uint16_t class_id;
2182315a1350SMichael S. Tsirkin 
218308b1df8fSPaolo Bonzini     if (pci_dev->romsize != -1 && !is_power_of_2(pci_dev->romsize)) {
218408b1df8fSPaolo Bonzini         error_setg(errp, "ROM size %u is not a power of two", pci_dev->romsize);
218508b1df8fSPaolo Bonzini         return;
218608b1df8fSPaolo Bonzini     }
218708b1df8fSPaolo Bonzini 
2188d61a363dSYoni Bettan     /* initialize cap_present for pci_is_express() and pci_config_size(),
2189d61a363dSYoni Bettan      * Note that hybrid PCIs are not set automatically and need to manage
2190d61a363dSYoni Bettan      * QEMU_PCI_CAP_EXPRESS manually */
2191d61a363dSYoni Bettan     if (object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE) &&
2192d61a363dSYoni Bettan        !object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE)) {
2193315a1350SMichael S. Tsirkin         pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
2194315a1350SMichael S. Tsirkin     }
2195315a1350SMichael S. Tsirkin 
2196cf04aba2SBen Widawsky     if (object_class_dynamic_cast(klass, INTERFACE_CXL_DEVICE)) {
2197cf04aba2SBen Widawsky         pci_dev->cap_present |= QEMU_PCIE_CAP_CXL;
2198cf04aba2SBen Widawsky     }
2199cf04aba2SBen Widawsky 
2200fd56e061SDavid Gibson     pci_dev = do_pci_register_device(pci_dev,
2201315a1350SMichael S. Tsirkin                                      object_get_typename(OBJECT(qdev)),
2202133e9b22SMarkus Armbruster                                      pci_dev->devfn, errp);
2203315a1350SMichael S. Tsirkin     if (pci_dev == NULL)
2204133e9b22SMarkus Armbruster         return;
22052897ae02SIgor Mammedov 
22067ee6c1e1SMarkus Armbruster     if (pc->realize) {
22077ee6c1e1SMarkus Armbruster         pc->realize(pci_dev, &local_err);
22087ee6c1e1SMarkus Armbruster         if (local_err) {
22097ee6c1e1SMarkus Armbruster             error_propagate(errp, local_err);
2210315a1350SMichael S. Tsirkin             do_pci_unregister_device(pci_dev);
2211133e9b22SMarkus Armbruster             return;
2212315a1350SMichael S. Tsirkin         }
2213315a1350SMichael S. Tsirkin     }
2214315a1350SMichael S. Tsirkin 
22154f5b6a05SJens Freimann     if (pci_dev->failover_pair_id) {
22164f5b6a05SJens Freimann         if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
22174f5b6a05SJens Freimann             error_setg(errp, "failover primary device must be on "
22184f5b6a05SJens Freimann                              "PCIExpress bus");
2219b69c3c21SMarkus Armbruster             pci_qdev_unrealize(DEVICE(pci_dev));
22204f5b6a05SJens Freimann             return;
22214f5b6a05SJens Freimann         }
22224f5b6a05SJens Freimann         class_id = pci_get_word(pci_dev->config + PCI_CLASS_DEVICE);
22234f5b6a05SJens Freimann         if (class_id != PCI_CLASS_NETWORK_ETHERNET) {
22244f5b6a05SJens Freimann             error_setg(errp, "failover primary device is not an "
22254f5b6a05SJens Freimann                              "Ethernet device");
2226b69c3c21SMarkus Armbruster             pci_qdev_unrealize(DEVICE(pci_dev));
22274f5b6a05SJens Freimann             return;
22284f5b6a05SJens Freimann         }
2229b01a4901SLaurent Vivier         if ((pci_dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)
2230b01a4901SLaurent Vivier             || (PCI_FUNC(pci_dev->devfn) != 0)) {
22314f5b6a05SJens Freimann             error_setg(errp, "failover: primary device must be in its own "
22324f5b6a05SJens Freimann                               "PCI slot");
2233b69c3c21SMarkus Armbruster             pci_qdev_unrealize(DEVICE(pci_dev));
22344f5b6a05SJens Freimann             return;
22354f5b6a05SJens Freimann         }
2236a1190ab6SJens Freimann         qdev->allow_unplug_during_migration = true;
22374f5b6a05SJens Freimann     }
22384f5b6a05SJens Freimann 
2239315a1350SMichael S. Tsirkin     /* rom loading */
2240315a1350SMichael S. Tsirkin     is_default_rom = false;
2241315a1350SMichael S. Tsirkin     if (pci_dev->romfile == NULL && pc->romfile != NULL) {
2242315a1350SMichael S. Tsirkin         pci_dev->romfile = g_strdup(pc->romfile);
2243315a1350SMichael S. Tsirkin         is_default_rom = true;
2244315a1350SMichael S. Tsirkin     }
2245178e785fSMarcel Apfelbaum 
2246133e9b22SMarkus Armbruster     pci_add_option_rom(pci_dev, is_default_rom, &local_err);
2247133e9b22SMarkus Armbruster     if (local_err) {
2248133e9b22SMarkus Armbruster         error_propagate(errp, local_err);
2249b69c3c21SMarkus Armbruster         pci_qdev_unrealize(DEVICE(pci_dev));
2250133e9b22SMarkus Armbruster         return;
2251178e785fSMarcel Apfelbaum     }
225223786d13SGerd Hoffmann 
225323786d13SGerd Hoffmann     pci_set_power(pci_dev, true);
2254315a1350SMichael S. Tsirkin }
2255315a1350SMichael S. Tsirkin 
22567411aa63SMarkus Armbruster PCIDevice *pci_new_multifunction(int devfn, bool multifunction,
22577411aa63SMarkus Armbruster                                  const char *name)
22587411aa63SMarkus Armbruster {
22597411aa63SMarkus Armbruster     DeviceState *dev;
22607411aa63SMarkus Armbruster 
22617411aa63SMarkus Armbruster     dev = qdev_new(name);
22627411aa63SMarkus Armbruster     qdev_prop_set_int32(dev, "addr", devfn);
22637411aa63SMarkus Armbruster     qdev_prop_set_bit(dev, "multifunction", multifunction);
22647411aa63SMarkus Armbruster     return PCI_DEVICE(dev);
22657411aa63SMarkus Armbruster }
22667411aa63SMarkus Armbruster 
22677411aa63SMarkus Armbruster PCIDevice *pci_new(int devfn, const char *name)
22687411aa63SMarkus Armbruster {
22697411aa63SMarkus Armbruster     return pci_new_multifunction(devfn, false, name);
22707411aa63SMarkus Armbruster }
22717411aa63SMarkus Armbruster 
22727411aa63SMarkus Armbruster bool pci_realize_and_unref(PCIDevice *dev, PCIBus *bus, Error **errp)
22737411aa63SMarkus Armbruster {
22747411aa63SMarkus Armbruster     return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp);
22757411aa63SMarkus Armbruster }
22767411aa63SMarkus Armbruster 
2277315a1350SMichael S. Tsirkin PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
2278315a1350SMichael S. Tsirkin                                            bool multifunction,
2279315a1350SMichael S. Tsirkin                                            const char *name)
2280315a1350SMichael S. Tsirkin {
22819307d06dSMarkus Armbruster     PCIDevice *dev = pci_new_multifunction(devfn, multifunction, name);
22829307d06dSMarkus Armbruster     pci_realize_and_unref(dev, bus, &error_fatal);
2283315a1350SMichael S. Tsirkin     return dev;
2284315a1350SMichael S. Tsirkin }
2285315a1350SMichael S. Tsirkin 
2286315a1350SMichael S. Tsirkin PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
2287315a1350SMichael S. Tsirkin {
2288315a1350SMichael S. Tsirkin     return pci_create_simple_multifunction(bus, devfn, false, name);
2289315a1350SMichael S. Tsirkin }
2290315a1350SMichael S. Tsirkin 
2291315a1350SMichael S. Tsirkin static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
2292315a1350SMichael S. Tsirkin {
2293315a1350SMichael S. Tsirkin     int offset = PCI_CONFIG_HEADER_SIZE;
2294315a1350SMichael S. Tsirkin     int i;
2295315a1350SMichael S. Tsirkin     for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
2296315a1350SMichael S. Tsirkin         if (pdev->used[i])
2297315a1350SMichael S. Tsirkin             offset = i + 1;
2298315a1350SMichael S. Tsirkin         else if (i - offset + 1 == size)
2299315a1350SMichael S. Tsirkin             return offset;
2300315a1350SMichael S. Tsirkin     }
2301315a1350SMichael S. Tsirkin     return 0;
2302315a1350SMichael S. Tsirkin }
2303315a1350SMichael S. Tsirkin 
2304315a1350SMichael S. Tsirkin static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
2305315a1350SMichael S. Tsirkin                                         uint8_t *prev_p)
2306315a1350SMichael S. Tsirkin {
2307315a1350SMichael S. Tsirkin     uint8_t next, prev;
2308315a1350SMichael S. Tsirkin 
2309315a1350SMichael S. Tsirkin     if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
2310315a1350SMichael S. Tsirkin         return 0;
2311315a1350SMichael S. Tsirkin 
2312315a1350SMichael S. Tsirkin     for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
2313315a1350SMichael S. Tsirkin          prev = next + PCI_CAP_LIST_NEXT)
2314315a1350SMichael S. Tsirkin         if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
2315315a1350SMichael S. Tsirkin             break;
2316315a1350SMichael S. Tsirkin 
2317315a1350SMichael S. Tsirkin     if (prev_p)
2318315a1350SMichael S. Tsirkin         *prev_p = prev;
2319315a1350SMichael S. Tsirkin     return next;
2320315a1350SMichael S. Tsirkin }
2321315a1350SMichael S. Tsirkin 
2322315a1350SMichael S. Tsirkin static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
2323315a1350SMichael S. Tsirkin {
2324315a1350SMichael S. Tsirkin     uint8_t next, prev, found = 0;
2325315a1350SMichael S. Tsirkin 
2326315a1350SMichael S. Tsirkin     if (!(pdev->used[offset])) {
2327315a1350SMichael S. Tsirkin         return 0;
2328315a1350SMichael S. Tsirkin     }
2329315a1350SMichael S. Tsirkin 
2330315a1350SMichael S. Tsirkin     assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST);
2331315a1350SMichael S. Tsirkin 
2332315a1350SMichael S. Tsirkin     for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
2333315a1350SMichael S. Tsirkin          prev = next + PCI_CAP_LIST_NEXT) {
2334315a1350SMichael S. Tsirkin         if (next <= offset && next > found) {
2335315a1350SMichael S. Tsirkin             found = next;
2336315a1350SMichael S. Tsirkin         }
2337315a1350SMichael S. Tsirkin     }
2338315a1350SMichael S. Tsirkin     return found;
2339315a1350SMichael S. Tsirkin }
2340315a1350SMichael S. Tsirkin 
2341315a1350SMichael S. Tsirkin /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
2342315a1350SMichael S. Tsirkin    This is needed for an option rom which is used for more than one device. */
23437c16b5bbSPaolo Bonzini static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, uint32_t size)
2344315a1350SMichael S. Tsirkin {
2345315a1350SMichael S. Tsirkin     uint16_t vendor_id;
2346315a1350SMichael S. Tsirkin     uint16_t device_id;
2347315a1350SMichael S. Tsirkin     uint16_t rom_vendor_id;
2348315a1350SMichael S. Tsirkin     uint16_t rom_device_id;
2349315a1350SMichael S. Tsirkin     uint16_t rom_magic;
2350315a1350SMichael S. Tsirkin     uint16_t pcir_offset;
2351315a1350SMichael S. Tsirkin     uint8_t checksum;
2352315a1350SMichael S. Tsirkin 
2353315a1350SMichael S. Tsirkin     /* Words in rom data are little endian (like in PCI configuration),
2354315a1350SMichael S. Tsirkin        so they can be read / written with pci_get_word / pci_set_word. */
2355315a1350SMichael S. Tsirkin 
2356315a1350SMichael S. Tsirkin     /* Only a valid rom will be patched. */
2357315a1350SMichael S. Tsirkin     rom_magic = pci_get_word(ptr);
2358315a1350SMichael S. Tsirkin     if (rom_magic != 0xaa55) {
2359315a1350SMichael S. Tsirkin         PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
2360315a1350SMichael S. Tsirkin         return;
2361315a1350SMichael S. Tsirkin     }
2362315a1350SMichael S. Tsirkin     pcir_offset = pci_get_word(ptr + 0x18);
2363315a1350SMichael S. Tsirkin     if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
2364315a1350SMichael S. Tsirkin         PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
2365315a1350SMichael S. Tsirkin         return;
2366315a1350SMichael S. Tsirkin     }
2367315a1350SMichael S. Tsirkin 
2368315a1350SMichael S. Tsirkin     vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
2369315a1350SMichael S. Tsirkin     device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
2370315a1350SMichael S. Tsirkin     rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
2371315a1350SMichael S. Tsirkin     rom_device_id = pci_get_word(ptr + pcir_offset + 6);
2372315a1350SMichael S. Tsirkin 
2373315a1350SMichael S. Tsirkin     PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
2374315a1350SMichael S. Tsirkin                 vendor_id, device_id, rom_vendor_id, rom_device_id);
2375315a1350SMichael S. Tsirkin 
2376315a1350SMichael S. Tsirkin     checksum = ptr[6];
2377315a1350SMichael S. Tsirkin 
2378315a1350SMichael S. Tsirkin     if (vendor_id != rom_vendor_id) {
2379315a1350SMichael S. Tsirkin         /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
2380315a1350SMichael S. Tsirkin         checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
2381315a1350SMichael S. Tsirkin         checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
2382315a1350SMichael S. Tsirkin         PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2383315a1350SMichael S. Tsirkin         ptr[6] = checksum;
2384315a1350SMichael S. Tsirkin         pci_set_word(ptr + pcir_offset + 4, vendor_id);
2385315a1350SMichael S. Tsirkin     }
2386315a1350SMichael S. Tsirkin 
2387315a1350SMichael S. Tsirkin     if (device_id != rom_device_id) {
2388315a1350SMichael S. Tsirkin         /* Patch device id and checksum (at offset 6 for etherboot roms). */
2389315a1350SMichael S. Tsirkin         checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
2390315a1350SMichael S. Tsirkin         checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
2391315a1350SMichael S. Tsirkin         PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2392315a1350SMichael S. Tsirkin         ptr[6] = checksum;
2393315a1350SMichael S. Tsirkin         pci_set_word(ptr + pcir_offset + 6, device_id);
2394315a1350SMichael S. Tsirkin     }
2395315a1350SMichael S. Tsirkin }
2396315a1350SMichael S. Tsirkin 
2397315a1350SMichael S. Tsirkin /* Add an option rom for the device */
2398133e9b22SMarkus Armbruster static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
2399133e9b22SMarkus Armbruster                                Error **errp)
2400315a1350SMichael S. Tsirkin {
24017c16b5bbSPaolo Bonzini     int64_t size;
2402315a1350SMichael S. Tsirkin     char *path;
2403315a1350SMichael S. Tsirkin     void *ptr;
2404315a1350SMichael S. Tsirkin     char name[32];
2405315a1350SMichael S. Tsirkin     const VMStateDescription *vmsd;
2406315a1350SMichael S. Tsirkin 
2407315a1350SMichael S. Tsirkin     if (!pdev->romfile)
2408133e9b22SMarkus Armbruster         return;
2409315a1350SMichael S. Tsirkin     if (strlen(pdev->romfile) == 0)
2410133e9b22SMarkus Armbruster         return;
2411315a1350SMichael S. Tsirkin 
2412315a1350SMichael S. Tsirkin     if (!pdev->rom_bar) {
2413315a1350SMichael S. Tsirkin         /*
2414315a1350SMichael S. Tsirkin          * Load rom via fw_cfg instead of creating a rom bar,
2415315a1350SMichael S. Tsirkin          * for 0.11 compatibility.
2416315a1350SMichael S. Tsirkin          */
2417315a1350SMichael S. Tsirkin         int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
2418db80c7b9SMarcel Apfelbaum 
2419db80c7b9SMarcel Apfelbaum         /*
2420db80c7b9SMarcel Apfelbaum          * Hot-plugged devices can't use the option ROM
2421db80c7b9SMarcel Apfelbaum          * if the rom bar is disabled.
2422db80c7b9SMarcel Apfelbaum          */
2423db80c7b9SMarcel Apfelbaum         if (DEVICE(pdev)->hotplugged) {
2424133e9b22SMarkus Armbruster             error_setg(errp, "Hot-plugged device without ROM bar"
2425133e9b22SMarkus Armbruster                        " can't have an option ROM");
2426133e9b22SMarkus Armbruster             return;
2427db80c7b9SMarcel Apfelbaum         }
2428db80c7b9SMarcel Apfelbaum 
2429315a1350SMichael S. Tsirkin         if (class == 0x0300) {
2430315a1350SMichael S. Tsirkin             rom_add_vga(pdev->romfile);
2431315a1350SMichael S. Tsirkin         } else {
2432315a1350SMichael S. Tsirkin             rom_add_option(pdev->romfile, -1);
2433315a1350SMichael S. Tsirkin         }
2434133e9b22SMarkus Armbruster         return;
2435315a1350SMichael S. Tsirkin     }
2436315a1350SMichael S. Tsirkin 
2437315a1350SMichael S. Tsirkin     path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
2438315a1350SMichael S. Tsirkin     if (path == NULL) {
2439315a1350SMichael S. Tsirkin         path = g_strdup(pdev->romfile);
2440315a1350SMichael S. Tsirkin     }
2441315a1350SMichael S. Tsirkin 
2442315a1350SMichael S. Tsirkin     size = get_image_size(path);
2443315a1350SMichael S. Tsirkin     if (size < 0) {
2444133e9b22SMarkus Armbruster         error_setg(errp, "failed to find romfile \"%s\"", pdev->romfile);
24458c7f3dd0SStefan Hajnoczi         g_free(path);
2446133e9b22SMarkus Armbruster         return;
24478c7f3dd0SStefan Hajnoczi     } else if (size == 0) {
2448133e9b22SMarkus Armbruster         error_setg(errp, "romfile \"%s\" is empty", pdev->romfile);
2449315a1350SMichael S. Tsirkin         g_free(path);
2450133e9b22SMarkus Armbruster         return;
24517c16b5bbSPaolo Bonzini     } else if (size > 2 * GiB) {
24527c16b5bbSPaolo Bonzini         error_setg(errp, "romfile \"%s\" too large (size cannot exceed 2 GiB)",
24537c16b5bbSPaolo Bonzini                    pdev->romfile);
24547c16b5bbSPaolo Bonzini         g_free(path);
24557c16b5bbSPaolo Bonzini         return;
2456315a1350SMichael S. Tsirkin     }
245708b1df8fSPaolo Bonzini     if (pdev->romsize != -1) {
245808b1df8fSPaolo Bonzini         if (size > pdev->romsize) {
245908b1df8fSPaolo Bonzini             error_setg(errp, "romfile \"%s\" (%u bytes) is too large for ROM size %u",
246008b1df8fSPaolo Bonzini                        pdev->romfile, (uint32_t)size, pdev->romsize);
246108b1df8fSPaolo Bonzini             g_free(path);
246208b1df8fSPaolo Bonzini             return;
246308b1df8fSPaolo Bonzini         }
246408b1df8fSPaolo Bonzini     } else {
246508b1df8fSPaolo Bonzini         pdev->romsize = pow2ceil(size);
246608b1df8fSPaolo Bonzini     }
2467315a1350SMichael S. Tsirkin 
2468315a1350SMichael S. Tsirkin     vmsd = qdev_get_vmsd(DEVICE(pdev));
2469315a1350SMichael S. Tsirkin 
2470315a1350SMichael S. Tsirkin     if (vmsd) {
2471315a1350SMichael S. Tsirkin         snprintf(name, sizeof(name), "%s.rom", vmsd->name);
2472315a1350SMichael S. Tsirkin     } else {
2473315a1350SMichael S. Tsirkin         snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev)));
2474315a1350SMichael S. Tsirkin     }
2475315a1350SMichael S. Tsirkin     pdev->has_rom = true;
247608b1df8fSPaolo Bonzini     memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, pdev->romsize, &error_fatal);
2477315a1350SMichael S. Tsirkin     ptr = memory_region_get_ram_ptr(&pdev->rom);
247836bde091SPeter Maydell     if (load_image_size(path, ptr, size) < 0) {
247936bde091SPeter Maydell         error_setg(errp, "failed to load romfile \"%s\"", pdev->romfile);
248036bde091SPeter Maydell         g_free(path);
248136bde091SPeter Maydell         return;
248236bde091SPeter Maydell     }
2483315a1350SMichael S. Tsirkin     g_free(path);
2484315a1350SMichael S. Tsirkin 
2485315a1350SMichael S. Tsirkin     if (is_default_rom) {
2486315a1350SMichael S. Tsirkin         /* Only the default rom images will be patched (if needed). */
2487315a1350SMichael S. Tsirkin         pci_patch_ids(pdev, ptr, size);
2488315a1350SMichael S. Tsirkin     }
2489315a1350SMichael S. Tsirkin 
2490315a1350SMichael S. Tsirkin     pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
2491315a1350SMichael S. Tsirkin }
2492315a1350SMichael S. Tsirkin 
2493315a1350SMichael S. Tsirkin static void pci_del_option_rom(PCIDevice *pdev)
2494315a1350SMichael S. Tsirkin {
2495315a1350SMichael S. Tsirkin     if (!pdev->has_rom)
2496315a1350SMichael S. Tsirkin         return;
2497315a1350SMichael S. Tsirkin 
2498315a1350SMichael S. Tsirkin     vmstate_unregister_ram(&pdev->rom, &pdev->qdev);
2499315a1350SMichael S. Tsirkin     pdev->has_rom = false;
2500315a1350SMichael S. Tsirkin }
2501315a1350SMichael S. Tsirkin 
2502315a1350SMichael S. Tsirkin /*
250327841278SMao Zhongyi  * On success, pci_add_capability() returns a positive value
2504eacbc632SMao Zhongyi  * that the offset of the pci capability.
2505eacbc632SMao Zhongyi  * On failure, it sets an error and returns a negative error
2506eacbc632SMao Zhongyi  * code.
2507eacbc632SMao Zhongyi  */
250827841278SMao Zhongyi int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
2509cd9aa33eSLaszlo Ersek                        uint8_t offset, uint8_t size,
2510cd9aa33eSLaszlo Ersek                        Error **errp)
2511cd9aa33eSLaszlo Ersek {
2512315a1350SMichael S. Tsirkin     uint8_t *config;
2513315a1350SMichael S. Tsirkin     int i, overlapping_cap;
2514315a1350SMichael S. Tsirkin 
2515315a1350SMichael S. Tsirkin     if (!offset) {
2516315a1350SMichael S. Tsirkin         offset = pci_find_space(pdev, size);
251797fe42f1SCao jin         /* out of PCI config space is programming error */
251897fe42f1SCao jin         assert(offset);
2519315a1350SMichael S. Tsirkin     } else {
2520315a1350SMichael S. Tsirkin         /* Verify that capabilities don't overlap.  Note: device assignment
2521315a1350SMichael S. Tsirkin          * depends on this check to verify that the device is not broken.
2522315a1350SMichael S. Tsirkin          * Should never trigger for emulated devices, but it's helpful
2523315a1350SMichael S. Tsirkin          * for debugging these. */
2524315a1350SMichael S. Tsirkin         for (i = offset; i < offset + size; i++) {
2525315a1350SMichael S. Tsirkin             overlapping_cap = pci_find_capability_at_offset(pdev, i);
2526315a1350SMichael S. Tsirkin             if (overlapping_cap) {
2527cd9aa33eSLaszlo Ersek                 error_setg(errp, "%s:%02x:%02x.%x "
2528315a1350SMichael S. Tsirkin                            "Attempt to add PCI capability %x at offset "
2529cd9aa33eSLaszlo Ersek                            "%x overlaps existing capability %x at offset %x",
2530fd56e061SDavid Gibson                            pci_root_bus_path(pdev), pci_dev_bus_num(pdev),
2531315a1350SMichael S. Tsirkin                            PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2532315a1350SMichael S. Tsirkin                            cap_id, offset, overlapping_cap, i);
2533315a1350SMichael S. Tsirkin                 return -EINVAL;
2534315a1350SMichael S. Tsirkin             }
2535315a1350SMichael S. Tsirkin         }
2536315a1350SMichael S. Tsirkin     }
2537315a1350SMichael S. Tsirkin 
2538315a1350SMichael S. Tsirkin     config = pdev->config + offset;
2539315a1350SMichael S. Tsirkin     config[PCI_CAP_LIST_ID] = cap_id;
2540315a1350SMichael S. Tsirkin     config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
2541315a1350SMichael S. Tsirkin     pdev->config[PCI_CAPABILITY_LIST] = offset;
2542315a1350SMichael S. Tsirkin     pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
2543315a1350SMichael S. Tsirkin     memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));
2544315a1350SMichael S. Tsirkin     /* Make capability read-only by default */
2545315a1350SMichael S. Tsirkin     memset(pdev->wmask + offset, 0, size);
2546315a1350SMichael S. Tsirkin     /* Check capability by default */
2547315a1350SMichael S. Tsirkin     memset(pdev->cmask + offset, 0xFF, size);
2548315a1350SMichael S. Tsirkin     return offset;
2549315a1350SMichael S. Tsirkin }
2550315a1350SMichael S. Tsirkin 
2551315a1350SMichael S. Tsirkin /* Unlink capability from the pci config space. */
2552315a1350SMichael S. Tsirkin void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
2553315a1350SMichael S. Tsirkin {
2554315a1350SMichael S. Tsirkin     uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
2555315a1350SMichael S. Tsirkin     if (!offset)
2556315a1350SMichael S. Tsirkin         return;
2557315a1350SMichael S. Tsirkin     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
2558315a1350SMichael S. Tsirkin     /* Make capability writable again */
2559315a1350SMichael S. Tsirkin     memset(pdev->wmask + offset, 0xff, size);
2560315a1350SMichael S. Tsirkin     memset(pdev->w1cmask + offset, 0, size);
2561315a1350SMichael S. Tsirkin     /* Clear cmask as device-specific registers can't be checked */
2562315a1350SMichael S. Tsirkin     memset(pdev->cmask + offset, 0, size);
2563315a1350SMichael S. Tsirkin     memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4));
2564315a1350SMichael S. Tsirkin 
2565315a1350SMichael S. Tsirkin     if (!pdev->config[PCI_CAPABILITY_LIST])
2566315a1350SMichael S. Tsirkin         pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
2567315a1350SMichael S. Tsirkin }
2568315a1350SMichael S. Tsirkin 
2569315a1350SMichael S. Tsirkin uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
2570315a1350SMichael S. Tsirkin {
2571315a1350SMichael S. Tsirkin     return pci_find_capability_list(pdev, cap_id, NULL);
2572315a1350SMichael S. Tsirkin }
2573315a1350SMichael S. Tsirkin 
2574315a1350SMichael S. Tsirkin static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
2575315a1350SMichael S. Tsirkin {
2576315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2577315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
2578315a1350SMichael S. Tsirkin     char ctxt[64];
2579315a1350SMichael S. Tsirkin     PCIIORegion *r;
2580315a1350SMichael S. Tsirkin     int i, class;
2581315a1350SMichael S. Tsirkin 
2582315a1350SMichael S. Tsirkin     class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2583315a1350SMichael S. Tsirkin     desc = pci_class_descriptions;
2584315a1350SMichael S. Tsirkin     while (desc->desc && class != desc->class)
2585315a1350SMichael S. Tsirkin         desc++;
2586315a1350SMichael S. Tsirkin     if (desc->desc) {
2587315a1350SMichael S. Tsirkin         snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
2588315a1350SMichael S. Tsirkin     } else {
2589315a1350SMichael S. Tsirkin         snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
2590315a1350SMichael S. Tsirkin     }
2591315a1350SMichael S. Tsirkin 
2592315a1350SMichael S. Tsirkin     monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
2593315a1350SMichael S. Tsirkin                    "pci id %04x:%04x (sub %04x:%04x)\n",
2594fd56e061SDavid Gibson                    indent, "", ctxt, pci_dev_bus_num(d),
2595315a1350SMichael S. Tsirkin                    PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
2596315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_VENDOR_ID),
2597315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_DEVICE_ID),
2598315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
2599315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_SUBSYSTEM_ID));
2600315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; i++) {
2601315a1350SMichael S. Tsirkin         r = &d->io_regions[i];
2602315a1350SMichael S. Tsirkin         if (!r->size)
2603315a1350SMichael S. Tsirkin             continue;
2604315a1350SMichael S. Tsirkin         monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
2605315a1350SMichael S. Tsirkin                        " [0x%"FMT_PCIBUS"]\n",
2606315a1350SMichael S. Tsirkin                        indent, "",
2607315a1350SMichael S. Tsirkin                        i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
2608315a1350SMichael S. Tsirkin                        r->addr, r->addr + r->size - 1);
2609315a1350SMichael S. Tsirkin     }
2610315a1350SMichael S. Tsirkin }
2611315a1350SMichael S. Tsirkin 
2612315a1350SMichael S. Tsirkin static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
2613315a1350SMichael S. Tsirkin {
2614315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2615315a1350SMichael S. Tsirkin     const char *name = NULL;
2616315a1350SMichael S. Tsirkin     const pci_class_desc *desc =  pci_class_descriptions;
2617315a1350SMichael S. Tsirkin     int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2618315a1350SMichael S. Tsirkin 
2619315a1350SMichael S. Tsirkin     while (desc->desc &&
2620315a1350SMichael S. Tsirkin           (class & ~desc->fw_ign_bits) !=
2621315a1350SMichael S. Tsirkin           (desc->class & ~desc->fw_ign_bits)) {
2622315a1350SMichael S. Tsirkin         desc++;
2623315a1350SMichael S. Tsirkin     }
2624315a1350SMichael S. Tsirkin 
2625315a1350SMichael S. Tsirkin     if (desc->desc) {
2626315a1350SMichael S. Tsirkin         name = desc->fw_name;
2627315a1350SMichael S. Tsirkin     }
2628315a1350SMichael S. Tsirkin 
2629315a1350SMichael S. Tsirkin     if (name) {
2630315a1350SMichael S. Tsirkin         pstrcpy(buf, len, name);
2631315a1350SMichael S. Tsirkin     } else {
2632315a1350SMichael S. Tsirkin         snprintf(buf, len, "pci%04x,%04x",
2633315a1350SMichael S. Tsirkin                  pci_get_word(d->config + PCI_VENDOR_ID),
2634315a1350SMichael S. Tsirkin                  pci_get_word(d->config + PCI_DEVICE_ID));
2635315a1350SMichael S. Tsirkin     }
2636315a1350SMichael S. Tsirkin 
2637315a1350SMichael S. Tsirkin     return buf;
2638315a1350SMichael S. Tsirkin }
2639315a1350SMichael S. Tsirkin 
2640315a1350SMichael S. Tsirkin static char *pcibus_get_fw_dev_path(DeviceState *dev)
2641315a1350SMichael S. Tsirkin {
2642315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2643*36f18c69SClaudio Fontana     char name[33];
2644*36f18c69SClaudio Fontana     int has_func = !!PCI_FUNC(d->devfn);
2645315a1350SMichael S. Tsirkin 
2646*36f18c69SClaudio Fontana     return g_strdup_printf("%s@%x%s%.*x",
2647*36f18c69SClaudio Fontana                            pci_dev_fw_name(dev, name, sizeof(name)),
2648*36f18c69SClaudio Fontana                            PCI_SLOT(d->devfn),
2649*36f18c69SClaudio Fontana                            has_func ? "," : "",
2650*36f18c69SClaudio Fontana                            has_func,
2651*36f18c69SClaudio Fontana                            PCI_FUNC(d->devfn));
2652315a1350SMichael S. Tsirkin }
2653315a1350SMichael S. Tsirkin 
2654315a1350SMichael S. Tsirkin static char *pcibus_get_dev_path(DeviceState *dev)
2655315a1350SMichael S. Tsirkin {
2656315a1350SMichael S. Tsirkin     PCIDevice *d = container_of(dev, PCIDevice, qdev);
2657315a1350SMichael S. Tsirkin     PCIDevice *t;
2658315a1350SMichael S. Tsirkin     int slot_depth;
2659315a1350SMichael S. Tsirkin     /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
2660315a1350SMichael S. Tsirkin      * 00 is added here to make this format compatible with
2661315a1350SMichael S. Tsirkin      * domain:Bus:Slot.Func for systems without nested PCI bridges.
2662315a1350SMichael S. Tsirkin      * Slot.Function list specifies the slot and function numbers for all
2663315a1350SMichael S. Tsirkin      * devices on the path from root to the specific device. */
2664568f0690SDavid Gibson     const char *root_bus_path;
2665568f0690SDavid Gibson     int root_bus_len;
2666315a1350SMichael S. Tsirkin     char slot[] = ":SS.F";
2667315a1350SMichael S. Tsirkin     int slot_len = sizeof slot - 1 /* For '\0' */;
2668315a1350SMichael S. Tsirkin     int path_len;
2669315a1350SMichael S. Tsirkin     char *path, *p;
2670315a1350SMichael S. Tsirkin     int s;
2671315a1350SMichael S. Tsirkin 
2672568f0690SDavid Gibson     root_bus_path = pci_root_bus_path(d);
2673568f0690SDavid Gibson     root_bus_len = strlen(root_bus_path);
2674568f0690SDavid Gibson 
2675315a1350SMichael S. Tsirkin     /* Calculate # of slots on path between device and root. */;
2676315a1350SMichael S. Tsirkin     slot_depth = 0;
2677fd56e061SDavid Gibson     for (t = d; t; t = pci_get_bus(t)->parent_dev) {
2678315a1350SMichael S. Tsirkin         ++slot_depth;
2679315a1350SMichael S. Tsirkin     }
2680315a1350SMichael S. Tsirkin 
2681568f0690SDavid Gibson     path_len = root_bus_len + slot_len * slot_depth;
2682315a1350SMichael S. Tsirkin 
2683315a1350SMichael S. Tsirkin     /* Allocate memory, fill in the terminating null byte. */
2684315a1350SMichael S. Tsirkin     path = g_malloc(path_len + 1 /* For '\0' */);
2685315a1350SMichael S. Tsirkin     path[path_len] = '\0';
2686315a1350SMichael S. Tsirkin 
2687568f0690SDavid Gibson     memcpy(path, root_bus_path, root_bus_len);
2688315a1350SMichael S. Tsirkin 
2689315a1350SMichael S. Tsirkin     /* Fill in slot numbers. We walk up from device to root, so need to print
2690315a1350SMichael S. Tsirkin      * them in the reverse order, last to first. */
2691315a1350SMichael S. Tsirkin     p = path + path_len;
2692fd56e061SDavid Gibson     for (t = d; t; t = pci_get_bus(t)->parent_dev) {
2693315a1350SMichael S. Tsirkin         p -= slot_len;
2694315a1350SMichael S. Tsirkin         s = snprintf(slot, sizeof slot, ":%02x.%x",
2695315a1350SMichael S. Tsirkin                      PCI_SLOT(t->devfn), PCI_FUNC(t->devfn));
2696315a1350SMichael S. Tsirkin         assert(s == slot_len);
2697315a1350SMichael S. Tsirkin         memcpy(p, slot, slot_len);
2698315a1350SMichael S. Tsirkin     }
2699315a1350SMichael S. Tsirkin 
2700315a1350SMichael S. Tsirkin     return path;
2701315a1350SMichael S. Tsirkin }
2702315a1350SMichael S. Tsirkin 
2703315a1350SMichael S. Tsirkin static int pci_qdev_find_recursive(PCIBus *bus,
2704315a1350SMichael S. Tsirkin                                    const char *id, PCIDevice **pdev)
2705315a1350SMichael S. Tsirkin {
2706315a1350SMichael S. Tsirkin     DeviceState *qdev = qdev_find_recursive(&bus->qbus, id);
2707315a1350SMichael S. Tsirkin     if (!qdev) {
2708315a1350SMichael S. Tsirkin         return -ENODEV;
2709315a1350SMichael S. Tsirkin     }
2710315a1350SMichael S. Tsirkin 
2711315a1350SMichael S. Tsirkin     /* roughly check if given qdev is pci device */
2712315a1350SMichael S. Tsirkin     if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) {
2713315a1350SMichael S. Tsirkin         *pdev = PCI_DEVICE(qdev);
2714315a1350SMichael S. Tsirkin         return 0;
2715315a1350SMichael S. Tsirkin     }
2716315a1350SMichael S. Tsirkin     return -EINVAL;
2717315a1350SMichael S. Tsirkin }
2718315a1350SMichael S. Tsirkin 
2719315a1350SMichael S. Tsirkin int pci_qdev_find_device(const char *id, PCIDevice **pdev)
2720315a1350SMichael S. Tsirkin {
27217588e2b0SDavid Gibson     PCIHostState *host_bridge;
2722315a1350SMichael S. Tsirkin     int rc = -ENODEV;
2723315a1350SMichael S. Tsirkin 
27247588e2b0SDavid Gibson     QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
27257588e2b0SDavid Gibson         int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev);
2726315a1350SMichael S. Tsirkin         if (!tmp) {
2727315a1350SMichael S. Tsirkin             rc = 0;
2728315a1350SMichael S. Tsirkin             break;
2729315a1350SMichael S. Tsirkin         }
2730315a1350SMichael S. Tsirkin         if (tmp != -ENODEV) {
2731315a1350SMichael S. Tsirkin             rc = tmp;
2732315a1350SMichael S. Tsirkin         }
2733315a1350SMichael S. Tsirkin     }
2734315a1350SMichael S. Tsirkin 
2735315a1350SMichael S. Tsirkin     return rc;
2736315a1350SMichael S. Tsirkin }
2737315a1350SMichael S. Tsirkin 
2738315a1350SMichael S. Tsirkin MemoryRegion *pci_address_space(PCIDevice *dev)
2739315a1350SMichael S. Tsirkin {
2740fd56e061SDavid Gibson     return pci_get_bus(dev)->address_space_mem;
2741315a1350SMichael S. Tsirkin }
2742315a1350SMichael S. Tsirkin 
2743315a1350SMichael S. Tsirkin MemoryRegion *pci_address_space_io(PCIDevice *dev)
2744315a1350SMichael S. Tsirkin {
2745fd56e061SDavid Gibson     return pci_get_bus(dev)->address_space_io;
2746315a1350SMichael S. Tsirkin }
2747315a1350SMichael S. Tsirkin 
2748315a1350SMichael S. Tsirkin static void pci_device_class_init(ObjectClass *klass, void *data)
2749315a1350SMichael S. Tsirkin {
2750315a1350SMichael S. Tsirkin     DeviceClass *k = DEVICE_CLASS(klass);
27517ee6c1e1SMarkus Armbruster 
2752133e9b22SMarkus Armbruster     k->realize = pci_qdev_realize;
2753133e9b22SMarkus Armbruster     k->unrealize = pci_qdev_unrealize;
2754315a1350SMichael S. Tsirkin     k->bus_type = TYPE_PCI_BUS;
27554f67d30bSMarc-André Lureau     device_class_set_props(k, pci_props);
2756315a1350SMichael S. Tsirkin }
2757315a1350SMichael S. Tsirkin 
27582fefa16cSEduardo Habkost static void pci_device_class_base_init(ObjectClass *klass, void *data)
27592fefa16cSEduardo Habkost {
27602fefa16cSEduardo Habkost     if (!object_class_is_abstract(klass)) {
27612fefa16cSEduardo Habkost         ObjectClass *conventional =
27622fefa16cSEduardo Habkost             object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE);
27632fefa16cSEduardo Habkost         ObjectClass *pcie =
27642fefa16cSEduardo Habkost             object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE);
2765d86d3019SBen Widawsky         ObjectClass *cxl =
2766d86d3019SBen Widawsky             object_class_dynamic_cast(klass, INTERFACE_CXL_DEVICE);
2767d86d3019SBen Widawsky         assert(conventional || pcie || cxl);
27682fefa16cSEduardo Habkost     }
27692fefa16cSEduardo Habkost }
27702fefa16cSEduardo Habkost 
27719eda7d37SAlexey Kardashevskiy AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
27729eda7d37SAlexey Kardashevskiy {
2773fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(dev);
27745af2ae23SBenjamin Herrenschmidt     PCIBus *iommu_bus = bus;
277577ef8f8dSAlex Williamson     uint8_t devfn = dev->devfn;
27769eda7d37SAlexey Kardashevskiy 
27775af2ae23SBenjamin Herrenschmidt     while (iommu_bus && !iommu_bus->iommu_fn && iommu_bus->parent_dev) {
277877ef8f8dSAlex Williamson         PCIBus *parent_bus = pci_get_bus(iommu_bus->parent_dev);
277977ef8f8dSAlex Williamson 
278077ef8f8dSAlex Williamson         /*
278177ef8f8dSAlex Williamson          * The requester ID of the provided device may be aliased, as seen from
278277ef8f8dSAlex Williamson          * the IOMMU, due to topology limitations.  The IOMMU relies on a
278377ef8f8dSAlex Williamson          * requester ID to provide a unique AddressSpace for devices, but
278477ef8f8dSAlex Williamson          * conventional PCI buses pre-date such concepts.  Instead, the PCIe-
278577ef8f8dSAlex Williamson          * to-PCI bridge creates and accepts transactions on behalf of down-
278677ef8f8dSAlex Williamson          * stream devices.  When doing so, all downstream devices are masked
278777ef8f8dSAlex Williamson          * (aliased) behind a single requester ID.  The requester ID used
278877ef8f8dSAlex Williamson          * depends on the format of the bridge devices.  Proper PCIe-to-PCI
278977ef8f8dSAlex Williamson          * bridges, with a PCIe capability indicating such, follow the
279077ef8f8dSAlex Williamson          * guidelines of chapter 2.3 of the PCIe-to-PCI/X bridge specification,
279177ef8f8dSAlex Williamson          * where the bridge uses the seconary bus as the bridge portion of the
279277ef8f8dSAlex Williamson          * requester ID and devfn of 00.0.  For other bridges, typically those
279377ef8f8dSAlex Williamson          * found on the root complex such as the dmi-to-pci-bridge, we follow
279477ef8f8dSAlex Williamson          * the convention of typical bare-metal hardware, which uses the
279577ef8f8dSAlex Williamson          * requester ID of the bridge itself.  There are device specific
279677ef8f8dSAlex Williamson          * exceptions to these rules, but these are the defaults that the
279777ef8f8dSAlex Williamson          * Linux kernel uses when determining DMA aliases itself and believed
279877ef8f8dSAlex Williamson          * to be true for the bare metal equivalents of the devices emulated
279977ef8f8dSAlex Williamson          * in QEMU.
280077ef8f8dSAlex Williamson          */
280177ef8f8dSAlex Williamson         if (!pci_bus_is_express(iommu_bus)) {
280277ef8f8dSAlex Williamson             PCIDevice *parent = iommu_bus->parent_dev;
280377ef8f8dSAlex Williamson 
280477ef8f8dSAlex Williamson             if (pci_is_express(parent) &&
280577ef8f8dSAlex Williamson                 pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) {
280677ef8f8dSAlex Williamson                 devfn = PCI_DEVFN(0, 0);
280777ef8f8dSAlex Williamson                 bus = iommu_bus;
280877ef8f8dSAlex Williamson             } else {
280977ef8f8dSAlex Williamson                 devfn = parent->devfn;
281077ef8f8dSAlex Williamson                 bus = parent_bus;
281177ef8f8dSAlex Williamson             }
281277ef8f8dSAlex Williamson         }
281377ef8f8dSAlex Williamson 
281477ef8f8dSAlex Williamson         iommu_bus = parent_bus;
28159eda7d37SAlexey Kardashevskiy     }
28162d64b7bbSXingang Wang     if (!pci_bus_bypass_iommu(bus) && iommu_bus && iommu_bus->iommu_fn) {
281777ef8f8dSAlex Williamson         return iommu_bus->iommu_fn(bus, iommu_bus->iommu_opaque, devfn);
28189eda7d37SAlexey Kardashevskiy     }
28199eda7d37SAlexey Kardashevskiy     return &address_space_memory;
28209eda7d37SAlexey Kardashevskiy }
28219eda7d37SAlexey Kardashevskiy 
2822e00387d5SAvi Kivity void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
2823315a1350SMichael S. Tsirkin {
2824e00387d5SAvi Kivity     bus->iommu_fn = fn;
2825e00387d5SAvi Kivity     bus->iommu_opaque = opaque;
2826315a1350SMichael S. Tsirkin }
2827315a1350SMichael S. Tsirkin 
282843864069SMichael S. Tsirkin static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque)
282943864069SMichael S. Tsirkin {
283043864069SMichael S. Tsirkin     Range *range = opaque;
283143864069SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
283243864069SMichael S. Tsirkin     uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND);
283377d6f4eaSMichael S. Tsirkin     int i;
283443864069SMichael S. Tsirkin 
283543864069SMichael S. Tsirkin     if (!(cmd & PCI_COMMAND_MEMORY)) {
283643864069SMichael S. Tsirkin         return;
283743864069SMichael S. Tsirkin     }
283843864069SMichael S. Tsirkin 
283943864069SMichael S. Tsirkin     if (pc->is_bridge) {
284043864069SMichael S. Tsirkin         pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
284143864069SMichael S. Tsirkin         pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
284243864069SMichael S. Tsirkin 
284343864069SMichael S. Tsirkin         base = MAX(base, 0x1ULL << 32);
284443864069SMichael S. Tsirkin 
284543864069SMichael S. Tsirkin         if (limit >= base) {
284643864069SMichael S. Tsirkin             Range pref_range;
2847a0efbf16SMarkus Armbruster             range_set_bounds(&pref_range, base, limit);
284843864069SMichael S. Tsirkin             range_extend(range, &pref_range);
284943864069SMichael S. Tsirkin         }
285043864069SMichael S. Tsirkin     }
285177d6f4eaSMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; ++i) {
285277d6f4eaSMichael S. Tsirkin         PCIIORegion *r = &dev->io_regions[i];
2853a0efbf16SMarkus Armbruster         pcibus_t lob, upb;
285443864069SMichael S. Tsirkin         Range region_range;
285543864069SMichael S. Tsirkin 
285677d6f4eaSMichael S. Tsirkin         if (!r->size ||
285777d6f4eaSMichael S. Tsirkin             (r->type & PCI_BASE_ADDRESS_SPACE_IO) ||
285877d6f4eaSMichael S. Tsirkin             !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
285943864069SMichael S. Tsirkin             continue;
286043864069SMichael S. Tsirkin         }
286177d6f4eaSMichael S. Tsirkin 
2862a0efbf16SMarkus Armbruster         lob = pci_bar_address(dev, i, r->type, r->size);
2863a0efbf16SMarkus Armbruster         upb = lob + r->size - 1;
2864a0efbf16SMarkus Armbruster         if (lob == PCI_BAR_UNMAPPED) {
286577d6f4eaSMichael S. Tsirkin             continue;
286677d6f4eaSMichael S. Tsirkin         }
286743864069SMichael S. Tsirkin 
2868a0efbf16SMarkus Armbruster         lob = MAX(lob, 0x1ULL << 32);
286943864069SMichael S. Tsirkin 
2870a0efbf16SMarkus Armbruster         if (upb >= lob) {
2871a0efbf16SMarkus Armbruster             range_set_bounds(&region_range, lob, upb);
287243864069SMichael S. Tsirkin             range_extend(range, &region_range);
287343864069SMichael S. Tsirkin         }
287443864069SMichael S. Tsirkin     }
287543864069SMichael S. Tsirkin }
287643864069SMichael S. Tsirkin 
287743864069SMichael S. Tsirkin void pci_bus_get_w64_range(PCIBus *bus, Range *range)
287843864069SMichael S. Tsirkin {
2879a0efbf16SMarkus Armbruster     range_make_empty(range);
288043864069SMichael S. Tsirkin     pci_for_each_device_under_bus(bus, pci_dev_get_w64, range);
288143864069SMichael S. Tsirkin }
288243864069SMichael S. Tsirkin 
28833f1e1478SCao jin static bool pcie_has_upstream_port(PCIDevice *dev)
28843f1e1478SCao jin {
2885fd56e061SDavid Gibson     PCIDevice *parent_dev = pci_bridge_get_device(pci_get_bus(dev));
28863f1e1478SCao jin 
28873f1e1478SCao jin     /* Device associated with an upstream port.
28883f1e1478SCao jin      * As there are several types of these, it's easier to check the
28893f1e1478SCao jin      * parent device: upstream ports are always connected to
28903f1e1478SCao jin      * root or downstream ports.
28913f1e1478SCao jin      */
28923f1e1478SCao jin     return parent_dev &&
28933f1e1478SCao jin         pci_is_express(parent_dev) &&
28943f1e1478SCao jin         parent_dev->exp.exp_cap &&
28953f1e1478SCao jin         (pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_ROOT_PORT ||
28963f1e1478SCao jin          pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_DOWNSTREAM);
28973f1e1478SCao jin }
28983f1e1478SCao jin 
28993f1e1478SCao jin PCIDevice *pci_get_function_0(PCIDevice *pci_dev)
29003f1e1478SCao jin {
2901fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
2902fd56e061SDavid Gibson 
29033f1e1478SCao jin     if(pcie_has_upstream_port(pci_dev)) {
29043f1e1478SCao jin         /* With an upstream PCIe port, we only support 1 device at slot 0 */
2905fd56e061SDavid Gibson         return bus->devices[0];
29063f1e1478SCao jin     } else {
29073f1e1478SCao jin         /* Other bus types might support multiple devices at slots 0-31 */
2908fd56e061SDavid Gibson         return bus->devices[PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 0)];
29093f1e1478SCao jin     }
29103f1e1478SCao jin }
29113f1e1478SCao jin 
2912e1d4fb2dSPeter Xu MSIMessage pci_get_msi_message(PCIDevice *dev, int vector)
2913e1d4fb2dSPeter Xu {
2914e1d4fb2dSPeter Xu     MSIMessage msg;
2915e1d4fb2dSPeter Xu     if (msix_enabled(dev)) {
2916e1d4fb2dSPeter Xu         msg = msix_get_message(dev, vector);
2917e1d4fb2dSPeter Xu     } else if (msi_enabled(dev)) {
2918e1d4fb2dSPeter Xu         msg = msi_get_message(dev, vector);
2919e1d4fb2dSPeter Xu     } else {
2920e1d4fb2dSPeter Xu         /* Should never happen */
2921e1d4fb2dSPeter Xu         error_report("%s: unknown interrupt type", __func__);
2922e1d4fb2dSPeter Xu         abort();
2923e1d4fb2dSPeter Xu     }
2924e1d4fb2dSPeter Xu     return msg;
2925e1d4fb2dSPeter Xu }
2926e1d4fb2dSPeter Xu 
292723786d13SGerd Hoffmann void pci_set_power(PCIDevice *d, bool state)
292823786d13SGerd Hoffmann {
292923786d13SGerd Hoffmann     if (d->has_power == state) {
293023786d13SGerd Hoffmann         return;
293123786d13SGerd Hoffmann     }
293223786d13SGerd Hoffmann 
293323786d13SGerd Hoffmann     d->has_power = state;
293423786d13SGerd Hoffmann     pci_update_mappings(d);
293523786d13SGerd Hoffmann     memory_region_set_enabled(&d->bus_master_enable_region,
293623786d13SGerd Hoffmann                               (pci_get_word(d->config + PCI_COMMAND)
293723786d13SGerd Hoffmann                                & PCI_COMMAND_MASTER) && d->has_power);
293823786d13SGerd Hoffmann     if (!d->has_power) {
293923786d13SGerd Hoffmann         pci_device_reset(d);
294023786d13SGerd Hoffmann     }
294123786d13SGerd Hoffmann }
294223786d13SGerd Hoffmann 
29438c43a6f0SAndreas Färber static const TypeInfo pci_device_type_info = {
2944315a1350SMichael S. Tsirkin     .name = TYPE_PCI_DEVICE,
2945315a1350SMichael S. Tsirkin     .parent = TYPE_DEVICE,
2946315a1350SMichael S. Tsirkin     .instance_size = sizeof(PCIDevice),
2947315a1350SMichael S. Tsirkin     .abstract = true,
2948315a1350SMichael S. Tsirkin     .class_size = sizeof(PCIDeviceClass),
2949315a1350SMichael S. Tsirkin     .class_init = pci_device_class_init,
29502fefa16cSEduardo Habkost     .class_base_init = pci_device_class_base_init,
2951315a1350SMichael S. Tsirkin };
2952315a1350SMichael S. Tsirkin 
2953315a1350SMichael S. Tsirkin static void pci_register_types(void)
2954315a1350SMichael S. Tsirkin {
2955315a1350SMichael S. Tsirkin     type_register_static(&pci_bus_info);
29563a861c46SAlex Williamson     type_register_static(&pcie_bus_info);
29574f8db871SBen Widawsky     type_register_static(&cxl_bus_info);
2958619f02aeSEduardo Habkost     type_register_static(&conventional_pci_interface_info);
2959cf04aba2SBen Widawsky     type_register_static(&cxl_interface_info);
2960619f02aeSEduardo Habkost     type_register_static(&pcie_interface_info);
2961315a1350SMichael S. Tsirkin     type_register_static(&pci_device_type_info);
2962315a1350SMichael S. Tsirkin }
2963315a1350SMichael S. Tsirkin 
2964315a1350SMichael S. Tsirkin type_init(pci_register_types)
2965