xref: /openbmc/qemu/hw/pci/pci.c (revision 459ca8bf)
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"
26a8d25326SMarkus Armbruster #include "qemu-common.h"
2764552b6bSMarkus Armbruster #include "hw/irq.h"
28c759b24fSMichael S. Tsirkin #include "hw/pci/pci.h"
29c759b24fSMichael S. Tsirkin #include "hw/pci/pci_bridge.h"
3006aac7bdSMichael S. Tsirkin #include "hw/pci/pci_bus.h"
31568f0690SDavid Gibson #include "hw/pci/pci_host.h"
32a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
33ca77ee28SMarkus Armbruster #include "migration/qemu-file-types.h"
34d6454270SMarkus Armbruster #include "migration/vmstate.h"
3583c9089eSPaolo Bonzini #include "monitor/monitor.h"
361422e32dSPaolo Bonzini #include "net/net.h"
37b58c5c2dSMarkus Armbruster #include "sysemu/numa.h"
3846517dd4SMarkus Armbruster #include "sysemu/sysemu.h"
39c759b24fSMichael S. Tsirkin #include "hw/loader.h"
40d49b6836SMarkus Armbruster #include "qemu/error-report.h"
411de7afc9SPaolo Bonzini #include "qemu/range.h"
427828d750SDon Koch #include "trace.h"
43c759b24fSMichael S. Tsirkin #include "hw/pci/msi.h"
44c759b24fSMichael S. Tsirkin #include "hw/pci/msix.h"
45022c62cbSPaolo Bonzini #include "exec/address-spaces.h"
465e954943SIgor Mammedov #include "hw/hotplug.h"
47e4024630SLaurent Vivier #include "hw/boards.h"
48e688df6bSMarkus Armbruster #include "qapi/error.h"
4961c7f987SPhilippe Mathieu-Daudé #include "qapi/qapi-commands-pci.h"
50f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
51315a1350SMichael S. Tsirkin 
52315a1350SMichael S. Tsirkin //#define DEBUG_PCI
53315a1350SMichael S. Tsirkin #ifdef DEBUG_PCI
54315a1350SMichael S. Tsirkin # define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
55315a1350SMichael S. Tsirkin #else
56315a1350SMichael S. Tsirkin # define PCI_DPRINTF(format, ...)       do { } while (0)
57315a1350SMichael S. Tsirkin #endif
58315a1350SMichael S. Tsirkin 
5988c725c7SCornelia Huck bool pci_available = true;
6088c725c7SCornelia Huck 
61315a1350SMichael S. Tsirkin static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
62315a1350SMichael S. Tsirkin static char *pcibus_get_dev_path(DeviceState *dev);
63315a1350SMichael S. Tsirkin static char *pcibus_get_fw_dev_path(DeviceState *dev);
64dcc20931SPaolo Bonzini static void pcibus_reset(BusState *qbus);
65315a1350SMichael S. Tsirkin 
66315a1350SMichael S. Tsirkin static Property pci_props[] = {
67315a1350SMichael S. Tsirkin     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
68315a1350SMichael S. Tsirkin     DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
69315a1350SMichael S. Tsirkin     DEFINE_PROP_UINT32("rombar",  PCIDevice, rom_bar, 1),
70315a1350SMichael S. Tsirkin     DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
71315a1350SMichael S. Tsirkin                     QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
726b449540SMichael S. Tsirkin     DEFINE_PROP_BIT("x-pcie-lnksta-dllla", PCIDevice, cap_present,
736b449540SMichael S. Tsirkin                     QEMU_PCIE_LNKSTA_DLLLA_BITNR, true),
74f03d8ea3SMarcel Apfelbaum     DEFINE_PROP_BIT("x-pcie-extcap-init", PCIDevice, cap_present,
75f03d8ea3SMarcel Apfelbaum                     QEMU_PCIE_EXTCAP_INIT_BITNR, true),
764f5b6a05SJens Freimann     DEFINE_PROP_STRING("failover_pair_id", PCIDevice,
774f5b6a05SJens Freimann                        failover_pair_id),
78315a1350SMichael S. Tsirkin     DEFINE_PROP_END_OF_LIST()
79315a1350SMichael S. Tsirkin };
80315a1350SMichael S. Tsirkin 
81d2f69df7SBandan Das static const VMStateDescription vmstate_pcibus = {
82d2f69df7SBandan Das     .name = "PCIBUS",
83d2f69df7SBandan Das     .version_id = 1,
84d2f69df7SBandan Das     .minimum_version_id = 1,
85d2f69df7SBandan Das     .fields = (VMStateField[]) {
86d2164ad3SHalil Pasic         VMSTATE_INT32_EQUAL(nirq, PCIBus, NULL),
87d2f69df7SBandan Das         VMSTATE_VARRAY_INT32(irq_count, PCIBus,
88d2f69df7SBandan Das                              nirq, 0, vmstate_info_int32,
89d2f69df7SBandan Das                              int32_t),
90d2f69df7SBandan Das         VMSTATE_END_OF_LIST()
91d2f69df7SBandan Das     }
92d2f69df7SBandan Das };
93d2f69df7SBandan Das 
94b86eacb8SMarcel Apfelbaum static void pci_init_bus_master(PCIDevice *pci_dev)
95b86eacb8SMarcel Apfelbaum {
96b86eacb8SMarcel Apfelbaum     AddressSpace *dma_as = pci_device_iommu_address_space(pci_dev);
97b86eacb8SMarcel Apfelbaum 
98b86eacb8SMarcel Apfelbaum     memory_region_init_alias(&pci_dev->bus_master_enable_region,
99b86eacb8SMarcel Apfelbaum                              OBJECT(pci_dev), "bus master",
100b86eacb8SMarcel Apfelbaum                              dma_as->root, 0, memory_region_size(dma_as->root));
101b86eacb8SMarcel Apfelbaum     memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
1023716d590SJason Wang     memory_region_add_subregion(&pci_dev->bus_master_container_region, 0,
1033716d590SJason Wang                                 &pci_dev->bus_master_enable_region);
104b86eacb8SMarcel Apfelbaum }
105b86eacb8SMarcel Apfelbaum 
106b86eacb8SMarcel Apfelbaum static void pcibus_machine_done(Notifier *notifier, void *data)
107b86eacb8SMarcel Apfelbaum {
108b86eacb8SMarcel Apfelbaum     PCIBus *bus = container_of(notifier, PCIBus, machine_done);
109b86eacb8SMarcel Apfelbaum     int i;
110b86eacb8SMarcel Apfelbaum 
111b86eacb8SMarcel Apfelbaum     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
112b86eacb8SMarcel Apfelbaum         if (bus->devices[i]) {
113b86eacb8SMarcel Apfelbaum             pci_init_bus_master(bus->devices[i]);
114b86eacb8SMarcel Apfelbaum         }
115b86eacb8SMarcel Apfelbaum     }
116b86eacb8SMarcel Apfelbaum }
117b86eacb8SMarcel Apfelbaum 
118d2f69df7SBandan Das static void pci_bus_realize(BusState *qbus, Error **errp)
119d2f69df7SBandan Das {
120d2f69df7SBandan Das     PCIBus *bus = PCI_BUS(qbus);
121d2f69df7SBandan Das 
122b86eacb8SMarcel Apfelbaum     bus->machine_done.notify = pcibus_machine_done;
123b86eacb8SMarcel Apfelbaum     qemu_add_machine_init_done_notifier(&bus->machine_done);
124b86eacb8SMarcel Apfelbaum 
1251df2c9a2SPeter Xu     vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY, &vmstate_pcibus, bus);
126d2f69df7SBandan Das }
127d2f69df7SBandan Das 
1282f57db8aSDavid Gibson static void pcie_bus_realize(BusState *qbus, Error **errp)
1292f57db8aSDavid Gibson {
1302f57db8aSDavid Gibson     PCIBus *bus = PCI_BUS(qbus);
1312f57db8aSDavid Gibson 
1322f57db8aSDavid Gibson     pci_bus_realize(qbus, errp);
1332f57db8aSDavid Gibson 
1342f57db8aSDavid Gibson     /*
1352f57db8aSDavid Gibson      * A PCI-E bus can support extended config space if it's the root
1362f57db8aSDavid Gibson      * bus, or if the bus/bridge above it does as well
1372f57db8aSDavid Gibson      */
1382f57db8aSDavid Gibson     if (pci_bus_is_root(bus)) {
1392f57db8aSDavid Gibson         bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE;
1402f57db8aSDavid Gibson     } else {
1412f57db8aSDavid Gibson         PCIBus *parent_bus = pci_get_bus(bus->parent_dev);
1422f57db8aSDavid Gibson 
1432f57db8aSDavid Gibson         if (pci_bus_allows_extended_config_space(parent_bus)) {
1442f57db8aSDavid Gibson             bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE;
1452f57db8aSDavid Gibson         }
1462f57db8aSDavid Gibson     }
1472f57db8aSDavid Gibson }
1482f57db8aSDavid Gibson 
149b69c3c21SMarkus Armbruster static void pci_bus_unrealize(BusState *qbus)
150d2f69df7SBandan Das {
151d2f69df7SBandan Das     PCIBus *bus = PCI_BUS(qbus);
152d2f69df7SBandan Das 
153b86eacb8SMarcel Apfelbaum     qemu_remove_machine_init_done_notifier(&bus->machine_done);
154b86eacb8SMarcel Apfelbaum 
155d2f69df7SBandan Das     vmstate_unregister(NULL, &vmstate_pcibus, bus);
156d2f69df7SBandan Das }
157d2f69df7SBandan Das 
158602141d9SMarcel Apfelbaum static int pcibus_num(PCIBus *bus)
159602141d9SMarcel Apfelbaum {
160b0e5196aSDavid Gibson     if (pci_bus_is_root(bus)) {
161602141d9SMarcel Apfelbaum         return 0; /* pci host bridge */
162602141d9SMarcel Apfelbaum     }
163602141d9SMarcel Apfelbaum     return bus->parent_dev->config[PCI_SECONDARY_BUS];
164602141d9SMarcel Apfelbaum }
165602141d9SMarcel Apfelbaum 
1666a3042b2SMarcel Apfelbaum static uint16_t pcibus_numa_node(PCIBus *bus)
1676a3042b2SMarcel Apfelbaum {
1686a3042b2SMarcel Apfelbaum     return NUMA_NODE_UNASSIGNED;
1696a3042b2SMarcel Apfelbaum }
1706a3042b2SMarcel Apfelbaum 
171315a1350SMichael S. Tsirkin static void pci_bus_class_init(ObjectClass *klass, void *data)
172315a1350SMichael S. Tsirkin {
173315a1350SMichael S. Tsirkin     BusClass *k = BUS_CLASS(klass);
174ce6a28eeSMarcel Apfelbaum     PCIBusClass *pbc = PCI_BUS_CLASS(klass);
175315a1350SMichael S. Tsirkin 
176315a1350SMichael S. Tsirkin     k->print_dev = pcibus_dev_print;
177315a1350SMichael S. Tsirkin     k->get_dev_path = pcibus_get_dev_path;
178315a1350SMichael S. Tsirkin     k->get_fw_dev_path = pcibus_get_fw_dev_path;
179d2f69df7SBandan Das     k->realize = pci_bus_realize;
180d2f69df7SBandan Das     k->unrealize = pci_bus_unrealize;
181315a1350SMichael S. Tsirkin     k->reset = pcibus_reset;
182ce6a28eeSMarcel Apfelbaum 
183602141d9SMarcel Apfelbaum     pbc->bus_num = pcibus_num;
1846a3042b2SMarcel Apfelbaum     pbc->numa_node = pcibus_numa_node;
185315a1350SMichael S. Tsirkin }
186315a1350SMichael S. Tsirkin 
187315a1350SMichael S. Tsirkin static const TypeInfo pci_bus_info = {
188315a1350SMichael S. Tsirkin     .name = TYPE_PCI_BUS,
189315a1350SMichael S. Tsirkin     .parent = TYPE_BUS,
190315a1350SMichael S. Tsirkin     .instance_size = sizeof(PCIBus),
191ce6a28eeSMarcel Apfelbaum     .class_size = sizeof(PCIBusClass),
192315a1350SMichael S. Tsirkin     .class_init = pci_bus_class_init,
193315a1350SMichael S. Tsirkin };
194315a1350SMichael S. Tsirkin 
195619f02aeSEduardo Habkost static const TypeInfo pcie_interface_info = {
196619f02aeSEduardo Habkost     .name          = INTERFACE_PCIE_DEVICE,
197619f02aeSEduardo Habkost     .parent        = TYPE_INTERFACE,
198619f02aeSEduardo Habkost };
199619f02aeSEduardo Habkost 
200619f02aeSEduardo Habkost static const TypeInfo conventional_pci_interface_info = {
201619f02aeSEduardo Habkost     .name          = INTERFACE_CONVENTIONAL_PCI_DEVICE,
202619f02aeSEduardo Habkost     .parent        = TYPE_INTERFACE,
203619f02aeSEduardo Habkost };
204619f02aeSEduardo Habkost 
2051c685a90SGreg Kurz static void pcie_bus_class_init(ObjectClass *klass, void *data)
2061c685a90SGreg Kurz {
2072f57db8aSDavid Gibson     BusClass *k = BUS_CLASS(klass);
2081c685a90SGreg Kurz 
2092f57db8aSDavid Gibson     k->realize = pcie_bus_realize;
2101c685a90SGreg Kurz }
2111c685a90SGreg Kurz 
2123a861c46SAlex Williamson static const TypeInfo pcie_bus_info = {
2133a861c46SAlex Williamson     .name = TYPE_PCIE_BUS,
2143a861c46SAlex Williamson     .parent = TYPE_PCI_BUS,
2151c685a90SGreg Kurz     .class_init = pcie_bus_class_init,
2163a861c46SAlex Williamson };
2173a861c46SAlex Williamson 
218315a1350SMichael S. Tsirkin static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
219315a1350SMichael S. Tsirkin static void pci_update_mappings(PCIDevice *d);
220d98f08f5SMarcel Apfelbaum static void pci_irq_handler(void *opaque, int irq_num, int level);
221133e9b22SMarkus Armbruster static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, Error **);
222315a1350SMichael S. Tsirkin static void pci_del_option_rom(PCIDevice *pdev);
223315a1350SMichael S. Tsirkin 
224315a1350SMichael S. Tsirkin static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
225315a1350SMichael S. Tsirkin static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
226315a1350SMichael S. Tsirkin 
2277588e2b0SDavid Gibson static QLIST_HEAD(, PCIHostState) pci_host_bridges;
228315a1350SMichael S. Tsirkin 
229cf8c704dSMichael Roth int pci_bar(PCIDevice *d, int reg)
230315a1350SMichael S. Tsirkin {
231315a1350SMichael S. Tsirkin     uint8_t type;
232315a1350SMichael S. Tsirkin 
233315a1350SMichael S. Tsirkin     if (reg != PCI_ROM_SLOT)
234315a1350SMichael S. Tsirkin         return PCI_BASE_ADDRESS_0 + reg * 4;
235315a1350SMichael S. Tsirkin 
236315a1350SMichael S. Tsirkin     type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
237315a1350SMichael S. Tsirkin     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
238315a1350SMichael S. Tsirkin }
239315a1350SMichael S. Tsirkin 
240315a1350SMichael S. Tsirkin static inline int pci_irq_state(PCIDevice *d, int irq_num)
241315a1350SMichael S. Tsirkin {
242315a1350SMichael S. Tsirkin         return (d->irq_state >> irq_num) & 0x1;
243315a1350SMichael S. Tsirkin }
244315a1350SMichael S. Tsirkin 
245315a1350SMichael S. Tsirkin static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
246315a1350SMichael S. Tsirkin {
247315a1350SMichael S. Tsirkin         d->irq_state &= ~(0x1 << irq_num);
248315a1350SMichael S. Tsirkin         d->irq_state |= level << irq_num;
249315a1350SMichael S. Tsirkin }
250315a1350SMichael S. Tsirkin 
251b06fe3e7SPhilippe Mathieu-Daudé static void pci_bus_change_irq_level(PCIBus *bus, int irq_num, int change)
252b06fe3e7SPhilippe Mathieu-Daudé {
253*459ca8bfSMark Cave-Ayland     assert(irq_num >= 0);
254*459ca8bfSMark Cave-Ayland     assert(irq_num < bus->nirq);
255b06fe3e7SPhilippe Mathieu-Daudé     bus->irq_count[irq_num] += change;
256b06fe3e7SPhilippe Mathieu-Daudé     bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
257b06fe3e7SPhilippe Mathieu-Daudé }
258b06fe3e7SPhilippe Mathieu-Daudé 
259315a1350SMichael S. Tsirkin static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
260315a1350SMichael S. Tsirkin {
261315a1350SMichael S. Tsirkin     PCIBus *bus;
262315a1350SMichael S. Tsirkin     for (;;) {
263fd56e061SDavid Gibson         bus = pci_get_bus(pci_dev);
264315a1350SMichael S. Tsirkin         irq_num = bus->map_irq(pci_dev, irq_num);
265315a1350SMichael S. Tsirkin         if (bus->set_irq)
266315a1350SMichael S. Tsirkin             break;
267315a1350SMichael S. Tsirkin         pci_dev = bus->parent_dev;
268315a1350SMichael S. Tsirkin     }
269b06fe3e7SPhilippe Mathieu-Daudé     pci_bus_change_irq_level(bus, irq_num, change);
270315a1350SMichael S. Tsirkin }
271315a1350SMichael S. Tsirkin 
272315a1350SMichael S. Tsirkin int pci_bus_get_irq_level(PCIBus *bus, int irq_num)
273315a1350SMichael S. Tsirkin {
274315a1350SMichael S. Tsirkin     assert(irq_num >= 0);
275315a1350SMichael S. Tsirkin     assert(irq_num < bus->nirq);
276315a1350SMichael S. Tsirkin     return !!bus->irq_count[irq_num];
277315a1350SMichael S. Tsirkin }
278315a1350SMichael S. Tsirkin 
279315a1350SMichael S. Tsirkin /* Update interrupt status bit in config space on interrupt
280315a1350SMichael S. Tsirkin  * state change. */
281315a1350SMichael S. Tsirkin static void pci_update_irq_status(PCIDevice *dev)
282315a1350SMichael S. Tsirkin {
283315a1350SMichael S. Tsirkin     if (dev->irq_state) {
284315a1350SMichael S. Tsirkin         dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
285315a1350SMichael S. Tsirkin     } else {
286315a1350SMichael S. Tsirkin         dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
287315a1350SMichael S. Tsirkin     }
288315a1350SMichael S. Tsirkin }
289315a1350SMichael S. Tsirkin 
290315a1350SMichael S. Tsirkin void pci_device_deassert_intx(PCIDevice *dev)
291315a1350SMichael S. Tsirkin {
292315a1350SMichael S. Tsirkin     int i;
293315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
294d98f08f5SMarcel Apfelbaum         pci_irq_handler(dev, i, 0);
295315a1350SMichael S. Tsirkin     }
296315a1350SMichael S. Tsirkin }
297315a1350SMichael S. Tsirkin 
298dcc20931SPaolo Bonzini static void pci_do_device_reset(PCIDevice *dev)
299315a1350SMichael S. Tsirkin {
300315a1350SMichael S. Tsirkin     int r;
301315a1350SMichael S. Tsirkin 
302315a1350SMichael S. Tsirkin     pci_device_deassert_intx(dev);
30358b59014SCole Robinson     assert(dev->irq_state == 0);
30458b59014SCole Robinson 
305315a1350SMichael S. Tsirkin     /* Clear all writable bits */
306315a1350SMichael S. Tsirkin     pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
307315a1350SMichael S. Tsirkin                                  pci_get_word(dev->wmask + PCI_COMMAND) |
308315a1350SMichael S. Tsirkin                                  pci_get_word(dev->w1cmask + PCI_COMMAND));
309315a1350SMichael S. Tsirkin     pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
310315a1350SMichael S. Tsirkin                                  pci_get_word(dev->wmask + PCI_STATUS) |
311315a1350SMichael S. Tsirkin                                  pci_get_word(dev->w1cmask + PCI_STATUS));
3127ff81d63SBALATON Zoltan     /* Some devices make bits of PCI_INTERRUPT_LINE read only */
3137ff81d63SBALATON Zoltan     pci_byte_test_and_clear_mask(dev->config + PCI_INTERRUPT_LINE,
3147ff81d63SBALATON Zoltan                               pci_get_word(dev->wmask + PCI_INTERRUPT_LINE) |
3157ff81d63SBALATON Zoltan                               pci_get_word(dev->w1cmask + PCI_INTERRUPT_LINE));
316315a1350SMichael S. Tsirkin     dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
317315a1350SMichael S. Tsirkin     for (r = 0; r < PCI_NUM_REGIONS; ++r) {
318315a1350SMichael S. Tsirkin         PCIIORegion *region = &dev->io_regions[r];
319315a1350SMichael S. Tsirkin         if (!region->size) {
320315a1350SMichael S. Tsirkin             continue;
321315a1350SMichael S. Tsirkin         }
322315a1350SMichael S. Tsirkin 
323315a1350SMichael S. Tsirkin         if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
324315a1350SMichael S. Tsirkin             region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
325315a1350SMichael S. Tsirkin             pci_set_quad(dev->config + pci_bar(dev, r), region->type);
326315a1350SMichael S. Tsirkin         } else {
327315a1350SMichael S. Tsirkin             pci_set_long(dev->config + pci_bar(dev, r), region->type);
328315a1350SMichael S. Tsirkin         }
329315a1350SMichael S. Tsirkin     }
330315a1350SMichael S. Tsirkin     pci_update_mappings(dev);
331315a1350SMichael S. Tsirkin 
332315a1350SMichael S. Tsirkin     msi_reset(dev);
333315a1350SMichael S. Tsirkin     msix_reset(dev);
334315a1350SMichael S. Tsirkin }
335315a1350SMichael S. Tsirkin 
336315a1350SMichael S. Tsirkin /*
337dcc20931SPaolo Bonzini  * This function is called on #RST and FLR.
338dcc20931SPaolo Bonzini  * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
339315a1350SMichael S. Tsirkin  */
340dcc20931SPaolo Bonzini void pci_device_reset(PCIDevice *dev)
341dcc20931SPaolo Bonzini {
342dcc20931SPaolo Bonzini     qdev_reset_all(&dev->qdev);
343dcc20931SPaolo Bonzini     pci_do_device_reset(dev);
344dcc20931SPaolo Bonzini }
345dcc20931SPaolo Bonzini 
346dcc20931SPaolo Bonzini /*
347dcc20931SPaolo Bonzini  * Trigger pci bus reset under a given bus.
348dcc20931SPaolo Bonzini  * Called via qbus_reset_all on RST# assert, after the devices
349dcc20931SPaolo Bonzini  * have been reset qdev_reset_all-ed already.
350dcc20931SPaolo Bonzini  */
351dcc20931SPaolo Bonzini static void pcibus_reset(BusState *qbus)
352315a1350SMichael S. Tsirkin {
35381e3e75bSPaolo Bonzini     PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
354315a1350SMichael S. Tsirkin     int i;
355315a1350SMichael S. Tsirkin 
356315a1350SMichael S. Tsirkin     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
357315a1350SMichael S. Tsirkin         if (bus->devices[i]) {
358dcc20931SPaolo Bonzini             pci_do_device_reset(bus->devices[i]);
359315a1350SMichael S. Tsirkin         }
360315a1350SMichael S. Tsirkin     }
361315a1350SMichael S. Tsirkin 
3629bdbbfc3SPaolo Bonzini     for (i = 0; i < bus->nirq; i++) {
3639bdbbfc3SPaolo Bonzini         assert(bus->irq_count[i] == 0);
3649bdbbfc3SPaolo Bonzini     }
365315a1350SMichael S. Tsirkin }
366315a1350SMichael S. Tsirkin 
3673dbc01aeSCao jin static void pci_host_bus_register(DeviceState *host)
368315a1350SMichael S. Tsirkin {
3693dbc01aeSCao jin     PCIHostState *host_bridge = PCI_HOST_BRIDGE(host);
3707588e2b0SDavid Gibson 
3717588e2b0SDavid Gibson     QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next);
372315a1350SMichael S. Tsirkin }
373315a1350SMichael S. Tsirkin 
374c13ee169SMichael Roth static void pci_host_bus_unregister(DeviceState *host)
375c13ee169SMichael Roth {
376c13ee169SMichael Roth     PCIHostState *host_bridge = PCI_HOST_BRIDGE(host);
377c13ee169SMichael Roth 
378c13ee169SMichael Roth     QLIST_REMOVE(host_bridge, next);
379c13ee169SMichael Roth }
380c13ee169SMichael Roth 
381c473d18dSDavid Gibson PCIBus *pci_device_root_bus(const PCIDevice *d)
382315a1350SMichael S. Tsirkin {
383fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(d);
384315a1350SMichael S. Tsirkin 
385ce6a28eeSMarcel Apfelbaum     while (!pci_bus_is_root(bus)) {
386ce6a28eeSMarcel Apfelbaum         d = bus->parent_dev;
387ce6a28eeSMarcel Apfelbaum         assert(d != NULL);
388ce6a28eeSMarcel Apfelbaum 
389fd56e061SDavid Gibson         bus = pci_get_bus(d);
390315a1350SMichael S. Tsirkin     }
391315a1350SMichael S. Tsirkin 
392c473d18dSDavid Gibson     return bus;
393315a1350SMichael S. Tsirkin }
394315a1350SMichael S. Tsirkin 
395568f0690SDavid Gibson const char *pci_root_bus_path(PCIDevice *dev)
396c473d18dSDavid Gibson {
397568f0690SDavid Gibson     PCIBus *rootbus = pci_device_root_bus(dev);
398568f0690SDavid Gibson     PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
399568f0690SDavid Gibson     PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge);
400c473d18dSDavid Gibson 
401568f0690SDavid Gibson     assert(host_bridge->bus == rootbus);
402568f0690SDavid Gibson 
403568f0690SDavid Gibson     if (hc->root_bus_path) {
404568f0690SDavid Gibson         return (*hc->root_bus_path)(host_bridge, rootbus);
405315a1350SMichael S. Tsirkin     }
406315a1350SMichael S. Tsirkin 
407568f0690SDavid Gibson     return rootbus->qbus.name;
408315a1350SMichael S. Tsirkin }
409315a1350SMichael S. Tsirkin 
4101115ff6dSDavid Gibson static void pci_root_bus_init(PCIBus *bus, DeviceState *parent,
411315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_mem,
412315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_io,
413315a1350SMichael S. Tsirkin                               uint8_t devfn_min)
414315a1350SMichael S. Tsirkin {
415315a1350SMichael S. Tsirkin     assert(PCI_FUNC(devfn_min) == 0);
416315a1350SMichael S. Tsirkin     bus->devfn_min = devfn_min;
4178b884984SMark Cave-Ayland     bus->slot_reserved_mask = 0x0;
418315a1350SMichael S. Tsirkin     bus->address_space_mem = address_space_mem;
419315a1350SMichael S. Tsirkin     bus->address_space_io = address_space_io;
420b0e5196aSDavid Gibson     bus->flags |= PCI_BUS_IS_ROOT;
421315a1350SMichael S. Tsirkin 
422315a1350SMichael S. Tsirkin     /* host bridge */
423315a1350SMichael S. Tsirkin     QLIST_INIT(&bus->child);
4242b8cc89aSDavid Gibson 
4253dbc01aeSCao jin     pci_host_bus_register(parent);
426315a1350SMichael S. Tsirkin }
427315a1350SMichael S. Tsirkin 
428c13ee169SMichael Roth static void pci_bus_uninit(PCIBus *bus)
429c13ee169SMichael Roth {
430c13ee169SMichael Roth     pci_host_bus_unregister(BUS(bus)->parent);
431c13ee169SMichael Roth }
432c13ee169SMichael Roth 
4338c0bf9e2SAlex Williamson bool pci_bus_is_express(PCIBus *bus)
4348c0bf9e2SAlex Williamson {
4358c0bf9e2SAlex Williamson     return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
4368c0bf9e2SAlex Williamson }
4378c0bf9e2SAlex Williamson 
4381115ff6dSDavid Gibson void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent,
4394fec6404SPaolo Bonzini                               const char *name,
4404fec6404SPaolo Bonzini                               MemoryRegion *address_space_mem,
4414fec6404SPaolo Bonzini                               MemoryRegion *address_space_io,
44260a0e443SAlex Williamson                               uint8_t devfn_min, const char *typename)
4434fec6404SPaolo Bonzini {
444fb17dfe0SAndreas Färber     qbus_create_inplace(bus, bus_size, typename, parent, name);
4451115ff6dSDavid Gibson     pci_root_bus_init(bus, parent, address_space_mem, address_space_io,
4461115ff6dSDavid Gibson                       devfn_min);
4474fec6404SPaolo Bonzini }
4484fec6404SPaolo Bonzini 
4491115ff6dSDavid Gibson PCIBus *pci_root_bus_new(DeviceState *parent, const char *name,
450315a1350SMichael S. Tsirkin                          MemoryRegion *address_space_mem,
451315a1350SMichael S. Tsirkin                          MemoryRegion *address_space_io,
45260a0e443SAlex Williamson                          uint8_t devfn_min, const char *typename)
453315a1350SMichael S. Tsirkin {
454315a1350SMichael S. Tsirkin     PCIBus *bus;
455315a1350SMichael S. Tsirkin 
45660a0e443SAlex Williamson     bus = PCI_BUS(qbus_create(typename, parent, name));
4571115ff6dSDavid Gibson     pci_root_bus_init(bus, parent, address_space_mem, address_space_io,
4581115ff6dSDavid Gibson                       devfn_min);
459315a1350SMichael S. Tsirkin     return bus;
460315a1350SMichael S. Tsirkin }
461315a1350SMichael S. Tsirkin 
462c13ee169SMichael Roth void pci_root_bus_cleanup(PCIBus *bus)
463c13ee169SMichael Roth {
464c13ee169SMichael Roth     pci_bus_uninit(bus);
46507578b0aSDavid Hildenbrand     /* the caller of the unplug hotplug handler will delete this device */
466f1483b46SMarkus Armbruster     qbus_unrealize(BUS(bus));
467c13ee169SMichael Roth }
468c13ee169SMichael Roth 
469315a1350SMichael S. Tsirkin void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
470315a1350SMichael S. Tsirkin                   void *irq_opaque, int nirq)
471315a1350SMichael S. Tsirkin {
472315a1350SMichael S. Tsirkin     bus->set_irq = set_irq;
473315a1350SMichael S. Tsirkin     bus->map_irq = map_irq;
474315a1350SMichael S. Tsirkin     bus->irq_opaque = irq_opaque;
475315a1350SMichael S. Tsirkin     bus->nirq = nirq;
476315a1350SMichael S. Tsirkin     bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0]));
477315a1350SMichael S. Tsirkin }
478315a1350SMichael S. Tsirkin 
479c13ee169SMichael Roth void pci_bus_irqs_cleanup(PCIBus *bus)
480c13ee169SMichael Roth {
481c13ee169SMichael Roth     bus->set_irq = NULL;
482c13ee169SMichael Roth     bus->map_irq = NULL;
483c13ee169SMichael Roth     bus->irq_opaque = NULL;
484c13ee169SMichael Roth     bus->nirq = 0;
485c13ee169SMichael Roth     g_free(bus->irq_count);
486c13ee169SMichael Roth }
487c13ee169SMichael Roth 
4881115ff6dSDavid Gibson PCIBus *pci_register_root_bus(DeviceState *parent, const char *name,
489315a1350SMichael S. Tsirkin                               pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
490315a1350SMichael S. Tsirkin                               void *irq_opaque,
491315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_mem,
492315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_io,
4931115ff6dSDavid Gibson                               uint8_t devfn_min, int nirq,
4941115ff6dSDavid Gibson                               const char *typename)
495315a1350SMichael S. Tsirkin {
496315a1350SMichael S. Tsirkin     PCIBus *bus;
497315a1350SMichael S. Tsirkin 
4981115ff6dSDavid Gibson     bus = pci_root_bus_new(parent, name, address_space_mem,
49960a0e443SAlex Williamson                            address_space_io, devfn_min, typename);
500315a1350SMichael S. Tsirkin     pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
501315a1350SMichael S. Tsirkin     return bus;
502315a1350SMichael S. Tsirkin }
503315a1350SMichael S. Tsirkin 
504c13ee169SMichael Roth void pci_unregister_root_bus(PCIBus *bus)
505c13ee169SMichael Roth {
506c13ee169SMichael Roth     pci_bus_irqs_cleanup(bus);
507c13ee169SMichael Roth     pci_root_bus_cleanup(bus);
508c13ee169SMichael Roth }
509c13ee169SMichael Roth 
510315a1350SMichael S. Tsirkin int pci_bus_num(PCIBus *s)
511315a1350SMichael S. Tsirkin {
512602141d9SMarcel Apfelbaum     return PCI_BUS_GET_CLASS(s)->bus_num(s);
513315a1350SMichael S. Tsirkin }
514315a1350SMichael S. Tsirkin 
5156a3042b2SMarcel Apfelbaum int pci_bus_numa_node(PCIBus *bus)
5166a3042b2SMarcel Apfelbaum {
5176a3042b2SMarcel Apfelbaum     return PCI_BUS_GET_CLASS(bus)->numa_node(bus);
518315a1350SMichael S. Tsirkin }
519315a1350SMichael S. Tsirkin 
5202c21ee76SJianjun Duan static int get_pci_config_device(QEMUFile *f, void *pv, size_t size,
52103fee66fSMarc-André Lureau                                  const VMStateField *field)
522315a1350SMichael S. Tsirkin {
523315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, config);
524e78e9ae4SDon Koch     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(s);
525315a1350SMichael S. Tsirkin     uint8_t *config;
526315a1350SMichael S. Tsirkin     int i;
527315a1350SMichael S. Tsirkin 
528315a1350SMichael S. Tsirkin     assert(size == pci_config_size(s));
529315a1350SMichael S. Tsirkin     config = g_malloc(size);
530315a1350SMichael S. Tsirkin 
531315a1350SMichael S. Tsirkin     qemu_get_buffer(f, config, size);
532315a1350SMichael S. Tsirkin     for (i = 0; i < size; ++i) {
533315a1350SMichael S. Tsirkin         if ((config[i] ^ s->config[i]) &
534315a1350SMichael S. Tsirkin             s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
5357c59364dSDr. David Alan Gilbert             error_report("%s: Bad config data: i=0x%x read: %x device: %x "
5367c59364dSDr. David Alan Gilbert                          "cmask: %x wmask: %x w1cmask:%x", __func__,
5377c59364dSDr. David Alan Gilbert                          i, config[i], s->config[i],
5387c59364dSDr. David Alan Gilbert                          s->cmask[i], s->wmask[i], s->w1cmask[i]);
539315a1350SMichael S. Tsirkin             g_free(config);
540315a1350SMichael S. Tsirkin             return -EINVAL;
541315a1350SMichael S. Tsirkin         }
542315a1350SMichael S. Tsirkin     }
543315a1350SMichael S. Tsirkin     memcpy(s->config, config, size);
544315a1350SMichael S. Tsirkin 
545315a1350SMichael S. Tsirkin     pci_update_mappings(s);
546e78e9ae4SDon Koch     if (pc->is_bridge) {
547f055e96bSAndreas Färber         PCIBridge *b = PCI_BRIDGE(s);
548e78e9ae4SDon Koch         pci_bridge_update_mappings(b);
549e78e9ae4SDon Koch     }
550315a1350SMichael S. Tsirkin 
551315a1350SMichael S. Tsirkin     memory_region_set_enabled(&s->bus_master_enable_region,
552315a1350SMichael S. Tsirkin                               pci_get_word(s->config + PCI_COMMAND)
553315a1350SMichael S. Tsirkin                               & PCI_COMMAND_MASTER);
554315a1350SMichael S. Tsirkin 
555315a1350SMichael S. Tsirkin     g_free(config);
556315a1350SMichael S. Tsirkin     return 0;
557315a1350SMichael S. Tsirkin }
558315a1350SMichael S. Tsirkin 
559315a1350SMichael S. Tsirkin /* just put buffer */
5602c21ee76SJianjun Duan static int put_pci_config_device(QEMUFile *f, void *pv, size_t size,
56103fee66fSMarc-André Lureau                                  const VMStateField *field, QJSON *vmdesc)
562315a1350SMichael S. Tsirkin {
563315a1350SMichael S. Tsirkin     const uint8_t **v = pv;
564315a1350SMichael S. Tsirkin     assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
565315a1350SMichael S. Tsirkin     qemu_put_buffer(f, *v, size);
5662c21ee76SJianjun Duan 
5672c21ee76SJianjun Duan     return 0;
568315a1350SMichael S. Tsirkin }
569315a1350SMichael S. Tsirkin 
570315a1350SMichael S. Tsirkin static VMStateInfo vmstate_info_pci_config = {
571315a1350SMichael S. Tsirkin     .name = "pci config",
572315a1350SMichael S. Tsirkin     .get  = get_pci_config_device,
573315a1350SMichael S. Tsirkin     .put  = put_pci_config_device,
574315a1350SMichael S. Tsirkin };
575315a1350SMichael S. Tsirkin 
5762c21ee76SJianjun Duan static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size,
57703fee66fSMarc-André Lureau                              const VMStateField *field)
578315a1350SMichael S. Tsirkin {
579315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, irq_state);
580315a1350SMichael S. Tsirkin     uint32_t irq_state[PCI_NUM_PINS];
581315a1350SMichael S. Tsirkin     int i;
582315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
583315a1350SMichael S. Tsirkin         irq_state[i] = qemu_get_be32(f);
584315a1350SMichael S. Tsirkin         if (irq_state[i] != 0x1 && irq_state[i] != 0) {
585315a1350SMichael S. Tsirkin             fprintf(stderr, "irq state %d: must be 0 or 1.\n",
586315a1350SMichael S. Tsirkin                     irq_state[i]);
587315a1350SMichael S. Tsirkin             return -EINVAL;
588315a1350SMichael S. Tsirkin         }
589315a1350SMichael S. Tsirkin     }
590315a1350SMichael S. Tsirkin 
591315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
592315a1350SMichael S. Tsirkin         pci_set_irq_state(s, i, irq_state[i]);
593315a1350SMichael S. Tsirkin     }
594315a1350SMichael S. Tsirkin 
595315a1350SMichael S. Tsirkin     return 0;
596315a1350SMichael S. Tsirkin }
597315a1350SMichael S. Tsirkin 
5982c21ee76SJianjun Duan static int put_pci_irq_state(QEMUFile *f, void *pv, size_t size,
59903fee66fSMarc-André Lureau                              const VMStateField *field, QJSON *vmdesc)
600315a1350SMichael S. Tsirkin {
601315a1350SMichael S. Tsirkin     int i;
602315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, irq_state);
603315a1350SMichael S. Tsirkin 
604315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
605315a1350SMichael S. Tsirkin         qemu_put_be32(f, pci_irq_state(s, i));
606315a1350SMichael S. Tsirkin     }
6072c21ee76SJianjun Duan 
6082c21ee76SJianjun Duan     return 0;
609315a1350SMichael S. Tsirkin }
610315a1350SMichael S. Tsirkin 
611315a1350SMichael S. Tsirkin static VMStateInfo vmstate_info_pci_irq_state = {
612315a1350SMichael S. Tsirkin     .name = "pci irq state",
613315a1350SMichael S. Tsirkin     .get  = get_pci_irq_state,
614315a1350SMichael S. Tsirkin     .put  = put_pci_irq_state,
615315a1350SMichael S. Tsirkin };
616315a1350SMichael S. Tsirkin 
61720daa90aSDr. David Alan Gilbert static bool migrate_is_pcie(void *opaque, int version_id)
61820daa90aSDr. David Alan Gilbert {
61920daa90aSDr. David Alan Gilbert     return pci_is_express((PCIDevice *)opaque);
62020daa90aSDr. David Alan Gilbert }
62120daa90aSDr. David Alan Gilbert 
62220daa90aSDr. David Alan Gilbert static bool migrate_is_not_pcie(void *opaque, int version_id)
62320daa90aSDr. David Alan Gilbert {
62420daa90aSDr. David Alan Gilbert     return !pci_is_express((PCIDevice *)opaque);
62520daa90aSDr. David Alan Gilbert }
62620daa90aSDr. David Alan Gilbert 
627315a1350SMichael S. Tsirkin const VMStateDescription vmstate_pci_device = {
628315a1350SMichael S. Tsirkin     .name = "PCIDevice",
629315a1350SMichael S. Tsirkin     .version_id = 2,
630315a1350SMichael S. Tsirkin     .minimum_version_id = 1,
631315a1350SMichael S. Tsirkin     .fields = (VMStateField[]) {
6323476436aSMichael S. Tsirkin         VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice),
63320daa90aSDr. David Alan Gilbert         VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice,
63420daa90aSDr. David Alan Gilbert                                    migrate_is_not_pcie,
63520daa90aSDr. David Alan Gilbert                                    0, vmstate_info_pci_config,
636315a1350SMichael S. Tsirkin                                    PCI_CONFIG_SPACE_SIZE),
63720daa90aSDr. David Alan Gilbert         VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice,
63820daa90aSDr. David Alan Gilbert                                    migrate_is_pcie,
63920daa90aSDr. David Alan Gilbert                                    0, vmstate_info_pci_config,
640315a1350SMichael S. Tsirkin                                    PCIE_CONFIG_SPACE_SIZE),
641315a1350SMichael S. Tsirkin         VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
642315a1350SMichael S. Tsirkin                                    vmstate_info_pci_irq_state,
643315a1350SMichael S. Tsirkin                                    PCI_NUM_PINS * sizeof(int32_t)),
644315a1350SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
645315a1350SMichael S. Tsirkin     }
646315a1350SMichael S. Tsirkin };
647315a1350SMichael S. Tsirkin 
648315a1350SMichael S. Tsirkin 
649315a1350SMichael S. Tsirkin void pci_device_save(PCIDevice *s, QEMUFile *f)
650315a1350SMichael S. Tsirkin {
651315a1350SMichael S. Tsirkin     /* Clear interrupt status bit: it is implicit
652315a1350SMichael S. Tsirkin      * in irq_state which we are saving.
653315a1350SMichael S. Tsirkin      * This makes us compatible with old devices
654315a1350SMichael S. Tsirkin      * which never set or clear this bit. */
655315a1350SMichael S. Tsirkin     s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
65620daa90aSDr. David Alan Gilbert     vmstate_save_state(f, &vmstate_pci_device, s, NULL);
657315a1350SMichael S. Tsirkin     /* Restore the interrupt status bit. */
658315a1350SMichael S. Tsirkin     pci_update_irq_status(s);
659315a1350SMichael S. Tsirkin }
660315a1350SMichael S. Tsirkin 
661315a1350SMichael S. Tsirkin int pci_device_load(PCIDevice *s, QEMUFile *f)
662315a1350SMichael S. Tsirkin {
663315a1350SMichael S. Tsirkin     int ret;
66420daa90aSDr. David Alan Gilbert     ret = vmstate_load_state(f, &vmstate_pci_device, s, s->version_id);
665315a1350SMichael S. Tsirkin     /* Restore the interrupt status bit. */
666315a1350SMichael S. Tsirkin     pci_update_irq_status(s);
667315a1350SMichael S. Tsirkin     return ret;
668315a1350SMichael S. Tsirkin }
669315a1350SMichael S. Tsirkin 
670315a1350SMichael S. Tsirkin static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
671315a1350SMichael S. Tsirkin {
672315a1350SMichael S. Tsirkin     pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
673315a1350SMichael S. Tsirkin                  pci_default_sub_vendor_id);
674315a1350SMichael S. Tsirkin     pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
675315a1350SMichael S. Tsirkin                  pci_default_sub_device_id);
676315a1350SMichael S. Tsirkin }
677315a1350SMichael S. Tsirkin 
678315a1350SMichael S. Tsirkin /*
679315a1350SMichael S. Tsirkin  * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
680315a1350SMichael S. Tsirkin  *       [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
681315a1350SMichael S. Tsirkin  */
6826dbcb819SMarkus Armbruster static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
683315a1350SMichael S. Tsirkin                              unsigned int *slotp, unsigned int *funcp)
684315a1350SMichael S. Tsirkin {
685315a1350SMichael S. Tsirkin     const char *p;
686315a1350SMichael S. Tsirkin     char *e;
687315a1350SMichael S. Tsirkin     unsigned long val;
688315a1350SMichael S. Tsirkin     unsigned long dom = 0, bus = 0;
689315a1350SMichael S. Tsirkin     unsigned int slot = 0;
690315a1350SMichael S. Tsirkin     unsigned int func = 0;
691315a1350SMichael S. Tsirkin 
692315a1350SMichael S. Tsirkin     p = addr;
693315a1350SMichael S. Tsirkin     val = strtoul(p, &e, 16);
694315a1350SMichael S. Tsirkin     if (e == p)
695315a1350SMichael S. Tsirkin         return -1;
696315a1350SMichael S. Tsirkin     if (*e == ':') {
697315a1350SMichael S. Tsirkin         bus = val;
698315a1350SMichael S. Tsirkin         p = e + 1;
699315a1350SMichael S. Tsirkin         val = strtoul(p, &e, 16);
700315a1350SMichael S. Tsirkin         if (e == p)
701315a1350SMichael S. Tsirkin             return -1;
702315a1350SMichael S. Tsirkin         if (*e == ':') {
703315a1350SMichael S. Tsirkin             dom = bus;
704315a1350SMichael S. Tsirkin             bus = val;
705315a1350SMichael S. Tsirkin             p = e + 1;
706315a1350SMichael S. Tsirkin             val = strtoul(p, &e, 16);
707315a1350SMichael S. Tsirkin             if (e == p)
708315a1350SMichael S. Tsirkin                 return -1;
709315a1350SMichael S. Tsirkin         }
710315a1350SMichael S. Tsirkin     }
711315a1350SMichael S. Tsirkin 
712315a1350SMichael S. Tsirkin     slot = val;
713315a1350SMichael S. Tsirkin 
714315a1350SMichael S. Tsirkin     if (funcp != NULL) {
715315a1350SMichael S. Tsirkin         if (*e != '.')
716315a1350SMichael S. Tsirkin             return -1;
717315a1350SMichael S. Tsirkin 
718315a1350SMichael S. Tsirkin         p = e + 1;
719315a1350SMichael S. Tsirkin         val = strtoul(p, &e, 16);
720315a1350SMichael S. Tsirkin         if (e == p)
721315a1350SMichael S. Tsirkin             return -1;
722315a1350SMichael S. Tsirkin 
723315a1350SMichael S. Tsirkin         func = val;
724315a1350SMichael S. Tsirkin     }
725315a1350SMichael S. Tsirkin 
726315a1350SMichael S. Tsirkin     /* if funcp == NULL func is 0 */
727315a1350SMichael S. Tsirkin     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
728315a1350SMichael S. Tsirkin         return -1;
729315a1350SMichael S. Tsirkin 
730315a1350SMichael S. Tsirkin     if (*e)
731315a1350SMichael S. Tsirkin         return -1;
732315a1350SMichael S. Tsirkin 
733315a1350SMichael S. Tsirkin     *domp = dom;
734315a1350SMichael S. Tsirkin     *busp = bus;
735315a1350SMichael S. Tsirkin     *slotp = slot;
736315a1350SMichael S. Tsirkin     if (funcp != NULL)
737315a1350SMichael S. Tsirkin         *funcp = func;
738315a1350SMichael S. Tsirkin     return 0;
739315a1350SMichael S. Tsirkin }
740315a1350SMichael S. Tsirkin 
741315a1350SMichael S. Tsirkin static void pci_init_cmask(PCIDevice *dev)
742315a1350SMichael S. Tsirkin {
743315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
744315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
745315a1350SMichael S. Tsirkin     dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
746315a1350SMichael S. Tsirkin     dev->cmask[PCI_REVISION_ID] = 0xff;
747315a1350SMichael S. Tsirkin     dev->cmask[PCI_CLASS_PROG] = 0xff;
748315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
749315a1350SMichael S. Tsirkin     dev->cmask[PCI_HEADER_TYPE] = 0xff;
750315a1350SMichael S. Tsirkin     dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
751315a1350SMichael S. Tsirkin }
752315a1350SMichael S. Tsirkin 
753315a1350SMichael S. Tsirkin static void pci_init_wmask(PCIDevice *dev)
754315a1350SMichael S. Tsirkin {
755315a1350SMichael S. Tsirkin     int config_size = pci_config_size(dev);
756315a1350SMichael S. Tsirkin 
757315a1350SMichael S. Tsirkin     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
758315a1350SMichael S. Tsirkin     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
759315a1350SMichael S. Tsirkin     pci_set_word(dev->wmask + PCI_COMMAND,
760315a1350SMichael S. Tsirkin                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
761315a1350SMichael S. Tsirkin                  PCI_COMMAND_INTX_DISABLE);
762315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
763315a1350SMichael S. Tsirkin 
764315a1350SMichael S. Tsirkin     memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
765315a1350SMichael S. Tsirkin            config_size - PCI_CONFIG_HEADER_SIZE);
766315a1350SMichael S. Tsirkin }
767315a1350SMichael S. Tsirkin 
768315a1350SMichael S. Tsirkin static void pci_init_w1cmask(PCIDevice *dev)
769315a1350SMichael S. Tsirkin {
770315a1350SMichael S. Tsirkin     /*
771315a1350SMichael S. Tsirkin      * Note: It's okay to set w1cmask even for readonly bits as
772315a1350SMichael S. Tsirkin      * long as their value is hardwired to 0.
773315a1350SMichael S. Tsirkin      */
774315a1350SMichael S. Tsirkin     pci_set_word(dev->w1cmask + PCI_STATUS,
775315a1350SMichael S. Tsirkin                  PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
776315a1350SMichael S. Tsirkin                  PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
777315a1350SMichael S. Tsirkin                  PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
778315a1350SMichael S. Tsirkin }
779315a1350SMichael S. Tsirkin 
780315a1350SMichael S. Tsirkin static void pci_init_mask_bridge(PCIDevice *d)
781315a1350SMichael S. Tsirkin {
782315a1350SMichael S. Tsirkin     /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
783315a1350SMichael S. Tsirkin        PCI_SEC_LETENCY_TIMER */
784315a1350SMichael S. Tsirkin     memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
785315a1350SMichael S. Tsirkin 
786315a1350SMichael S. Tsirkin     /* base and limit */
787315a1350SMichael S. Tsirkin     d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
788315a1350SMichael S. Tsirkin     d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
789315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_MEMORY_BASE,
790315a1350SMichael S. Tsirkin                  PCI_MEMORY_RANGE_MASK & 0xffff);
791315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
792315a1350SMichael S. Tsirkin                  PCI_MEMORY_RANGE_MASK & 0xffff);
793315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
794315a1350SMichael S. Tsirkin                  PCI_PREF_RANGE_MASK & 0xffff);
795315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
796315a1350SMichael S. Tsirkin                  PCI_PREF_RANGE_MASK & 0xffff);
797315a1350SMichael S. Tsirkin 
798315a1350SMichael S. Tsirkin     /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
799315a1350SMichael S. Tsirkin     memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
800315a1350SMichael S. Tsirkin 
801315a1350SMichael S. Tsirkin     /* Supported memory and i/o types */
802315a1350SMichael S. Tsirkin     d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16;
803315a1350SMichael S. Tsirkin     d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16;
804315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE,
805315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_64);
806315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT,
807315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_64);
808315a1350SMichael S. Tsirkin 
809ba7d8515SAlex Williamson     /*
810ba7d8515SAlex Williamson      * TODO: Bridges default to 10-bit VGA decoding but we currently only
811ba7d8515SAlex Williamson      * implement 16-bit decoding (no alias support).
812ba7d8515SAlex Williamson      */
813315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
814315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_PARITY |
815315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_SERR |
816315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_ISA |
817315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_VGA |
818315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_VGA_16BIT |
819315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_MASTER_ABORT |
820315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_BUS_RESET |
821315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_FAST_BACK |
822315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD |
823315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_SEC_DISCARD |
824315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD_SERR);
825315a1350SMichael S. Tsirkin     /* Below does not do anything as we never set this bit, put here for
826315a1350SMichael S. Tsirkin      * completeness. */
827315a1350SMichael S. Tsirkin     pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
828315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD_STATUS);
829315a1350SMichael S. Tsirkin     d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK;
830315a1350SMichael S. Tsirkin     d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK;
831315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE,
832315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_MASK);
833315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT,
834315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_MASK);
835315a1350SMichael S. Tsirkin }
836315a1350SMichael S. Tsirkin 
837133e9b22SMarkus Armbruster static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp)
838315a1350SMichael S. Tsirkin {
839315a1350SMichael S. Tsirkin     uint8_t slot = PCI_SLOT(dev->devfn);
840315a1350SMichael S. Tsirkin     uint8_t func;
841315a1350SMichael S. Tsirkin 
842315a1350SMichael S. Tsirkin     if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
843315a1350SMichael S. Tsirkin         dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
844315a1350SMichael S. Tsirkin     }
845315a1350SMichael S. Tsirkin 
846315a1350SMichael S. Tsirkin     /*
847315a1350SMichael S. Tsirkin      * multifunction bit is interpreted in two ways as follows.
848315a1350SMichael S. Tsirkin      *   - all functions must set the bit to 1.
849315a1350SMichael S. Tsirkin      *     Example: Intel X53
850315a1350SMichael S. Tsirkin      *   - function 0 must set the bit, but the rest function (> 0)
851315a1350SMichael S. Tsirkin      *     is allowed to leave the bit to 0.
852315a1350SMichael S. Tsirkin      *     Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
853315a1350SMichael S. Tsirkin      *
854315a1350SMichael S. Tsirkin      * So OS (at least Linux) checks the bit of only function 0,
855315a1350SMichael S. Tsirkin      * and doesn't see the bit of function > 0.
856315a1350SMichael S. Tsirkin      *
857315a1350SMichael S. Tsirkin      * The below check allows both interpretation.
858315a1350SMichael S. Tsirkin      */
859315a1350SMichael S. Tsirkin     if (PCI_FUNC(dev->devfn)) {
860315a1350SMichael S. Tsirkin         PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
861315a1350SMichael S. Tsirkin         if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
862315a1350SMichael S. Tsirkin             /* function 0 should set multifunction bit */
863133e9b22SMarkus Armbruster             error_setg(errp, "PCI: single function device can't be populated "
864315a1350SMichael S. Tsirkin                        "in function %x.%x", slot, PCI_FUNC(dev->devfn));
865133e9b22SMarkus Armbruster             return;
866315a1350SMichael S. Tsirkin         }
867133e9b22SMarkus Armbruster         return;
868315a1350SMichael S. Tsirkin     }
869315a1350SMichael S. Tsirkin 
870315a1350SMichael S. Tsirkin     if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
871133e9b22SMarkus Armbruster         return;
872315a1350SMichael S. Tsirkin     }
873315a1350SMichael S. Tsirkin     /* function 0 indicates single function, so function > 0 must be NULL */
874315a1350SMichael S. Tsirkin     for (func = 1; func < PCI_FUNC_MAX; ++func) {
875315a1350SMichael S. Tsirkin         if (bus->devices[PCI_DEVFN(slot, func)]) {
876133e9b22SMarkus Armbruster             error_setg(errp, "PCI: %x.0 indicates single function, "
877315a1350SMichael S. Tsirkin                        "but %x.%x is already populated.",
878315a1350SMichael S. Tsirkin                        slot, slot, func);
879133e9b22SMarkus Armbruster             return;
880315a1350SMichael S. Tsirkin         }
881315a1350SMichael S. Tsirkin     }
882315a1350SMichael S. Tsirkin }
883315a1350SMichael S. Tsirkin 
884315a1350SMichael S. Tsirkin static void pci_config_alloc(PCIDevice *pci_dev)
885315a1350SMichael S. Tsirkin {
886315a1350SMichael S. Tsirkin     int config_size = pci_config_size(pci_dev);
887315a1350SMichael S. Tsirkin 
888315a1350SMichael S. Tsirkin     pci_dev->config = g_malloc0(config_size);
889315a1350SMichael S. Tsirkin     pci_dev->cmask = g_malloc0(config_size);
890315a1350SMichael S. Tsirkin     pci_dev->wmask = g_malloc0(config_size);
891315a1350SMichael S. Tsirkin     pci_dev->w1cmask = g_malloc0(config_size);
892315a1350SMichael S. Tsirkin     pci_dev->used = g_malloc0(config_size);
893315a1350SMichael S. Tsirkin }
894315a1350SMichael S. Tsirkin 
895315a1350SMichael S. Tsirkin static void pci_config_free(PCIDevice *pci_dev)
896315a1350SMichael S. Tsirkin {
897315a1350SMichael S. Tsirkin     g_free(pci_dev->config);
898315a1350SMichael S. Tsirkin     g_free(pci_dev->cmask);
899315a1350SMichael S. Tsirkin     g_free(pci_dev->wmask);
900315a1350SMichael S. Tsirkin     g_free(pci_dev->w1cmask);
901315a1350SMichael S. Tsirkin     g_free(pci_dev->used);
902315a1350SMichael S. Tsirkin }
903315a1350SMichael S. Tsirkin 
90430607764SMarcel Apfelbaum static void do_pci_unregister_device(PCIDevice *pci_dev)
90530607764SMarcel Apfelbaum {
906fd56e061SDavid Gibson     pci_get_bus(pci_dev)->devices[pci_dev->devfn] = NULL;
90730607764SMarcel Apfelbaum     pci_config_free(pci_dev);
90830607764SMarcel Apfelbaum 
909193982c6SAlexey Kardashevskiy     if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
910c53598edSAlexey Kardashevskiy         memory_region_del_subregion(&pci_dev->bus_master_container_region,
911c53598edSAlexey Kardashevskiy                                     &pci_dev->bus_master_enable_region);
912193982c6SAlexey Kardashevskiy     }
91330607764SMarcel Apfelbaum     address_space_destroy(&pci_dev->bus_master_as);
91430607764SMarcel Apfelbaum }
91530607764SMarcel Apfelbaum 
9164a94b3aaSPeter Xu /* Extract PCIReqIDCache into BDF format */
9174a94b3aaSPeter Xu static uint16_t pci_req_id_cache_extract(PCIReqIDCache *cache)
9184a94b3aaSPeter Xu {
9194a94b3aaSPeter Xu     uint8_t bus_n;
9204a94b3aaSPeter Xu     uint16_t result;
9214a94b3aaSPeter Xu 
9224a94b3aaSPeter Xu     switch (cache->type) {
9234a94b3aaSPeter Xu     case PCI_REQ_ID_BDF:
9244a94b3aaSPeter Xu         result = pci_get_bdf(cache->dev);
9254a94b3aaSPeter Xu         break;
9264a94b3aaSPeter Xu     case PCI_REQ_ID_SECONDARY_BUS:
927fd56e061SDavid Gibson         bus_n = pci_dev_bus_num(cache->dev);
9284a94b3aaSPeter Xu         result = PCI_BUILD_BDF(bus_n, 0);
9294a94b3aaSPeter Xu         break;
9304a94b3aaSPeter Xu     default:
931eaf27fabSMarkus Armbruster         error_report("Invalid PCI requester ID cache type: %d",
9324a94b3aaSPeter Xu                      cache->type);
9334a94b3aaSPeter Xu         exit(1);
9344a94b3aaSPeter Xu         break;
9354a94b3aaSPeter Xu     }
9364a94b3aaSPeter Xu 
9374a94b3aaSPeter Xu     return result;
9384a94b3aaSPeter Xu }
9394a94b3aaSPeter Xu 
9404a94b3aaSPeter Xu /* Parse bridges up to the root complex and return requester ID
9414a94b3aaSPeter Xu  * cache for specific device.  For full PCIe topology, the cache
9424a94b3aaSPeter Xu  * result would be exactly the same as getting BDF of the device.
9434a94b3aaSPeter Xu  * However, several tricks are required when system mixed up with
9444a94b3aaSPeter Xu  * legacy PCI devices and PCIe-to-PCI bridges.
9454a94b3aaSPeter Xu  *
9464a94b3aaSPeter Xu  * Here we cache the proxy device (and type) not requester ID since
9474a94b3aaSPeter Xu  * bus number might change from time to time.
9484a94b3aaSPeter Xu  */
9494a94b3aaSPeter Xu static PCIReqIDCache pci_req_id_cache_get(PCIDevice *dev)
9504a94b3aaSPeter Xu {
9514a94b3aaSPeter Xu     PCIDevice *parent;
9524a94b3aaSPeter Xu     PCIReqIDCache cache = {
9534a94b3aaSPeter Xu         .dev = dev,
9544a94b3aaSPeter Xu         .type = PCI_REQ_ID_BDF,
9554a94b3aaSPeter Xu     };
9564a94b3aaSPeter Xu 
957fd56e061SDavid Gibson     while (!pci_bus_is_root(pci_get_bus(dev))) {
9584a94b3aaSPeter Xu         /* We are under PCI/PCIe bridges */
959fd56e061SDavid Gibson         parent = pci_get_bus(dev)->parent_dev;
9604a94b3aaSPeter Xu         if (pci_is_express(parent)) {
9614a94b3aaSPeter Xu             if (pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) {
9624a94b3aaSPeter Xu                 /* When we pass through PCIe-to-PCI/PCIX bridges, we
9634a94b3aaSPeter Xu                  * override the requester ID using secondary bus
9644a94b3aaSPeter Xu                  * number of parent bridge with zeroed devfn
9654a94b3aaSPeter Xu                  * (pcie-to-pci bridge spec chap 2.3). */
9664a94b3aaSPeter Xu                 cache.type = PCI_REQ_ID_SECONDARY_BUS;
9674a94b3aaSPeter Xu                 cache.dev = dev;
9684a94b3aaSPeter Xu             }
9694a94b3aaSPeter Xu         } else {
9704a94b3aaSPeter Xu             /* Legacy PCI, override requester ID with the bridge's
9714a94b3aaSPeter Xu              * BDF upstream.  When the root complex connects to
9724a94b3aaSPeter Xu              * legacy PCI devices (including buses), it can only
9734a94b3aaSPeter Xu              * obtain requester ID info from directly attached
9744a94b3aaSPeter Xu              * devices.  If devices are attached under bridges, only
9754a94b3aaSPeter Xu              * the requester ID of the bridge that is directly
9764a94b3aaSPeter Xu              * attached to the root complex can be recognized. */
9774a94b3aaSPeter Xu             cache.type = PCI_REQ_ID_BDF;
9784a94b3aaSPeter Xu             cache.dev = parent;
9794a94b3aaSPeter Xu         }
9804a94b3aaSPeter Xu         dev = parent;
9814a94b3aaSPeter Xu     }
9824a94b3aaSPeter Xu 
9834a94b3aaSPeter Xu     return cache;
9844a94b3aaSPeter Xu }
9854a94b3aaSPeter Xu 
9864a94b3aaSPeter Xu uint16_t pci_requester_id(PCIDevice *dev)
9874a94b3aaSPeter Xu {
9884a94b3aaSPeter Xu     return pci_req_id_cache_extract(&dev->requester_id_cache);
9894a94b3aaSPeter Xu }
9904a94b3aaSPeter Xu 
9919b717a3aSMark Cave-Ayland static bool pci_bus_devfn_available(PCIBus *bus, int devfn)
9929b717a3aSMark Cave-Ayland {
9939b717a3aSMark Cave-Ayland     return !(bus->devices[devfn]);
9949b717a3aSMark Cave-Ayland }
9959b717a3aSMark Cave-Ayland 
9968b884984SMark Cave-Ayland static bool pci_bus_devfn_reserved(PCIBus *bus, int devfn)
9978b884984SMark Cave-Ayland {
9988b884984SMark Cave-Ayland     return bus->slot_reserved_mask & (1UL << PCI_SLOT(devfn));
9998b884984SMark Cave-Ayland }
10008b884984SMark Cave-Ayland 
1001315a1350SMichael S. Tsirkin /* -1 for devfn means auto assign */
1002fd56e061SDavid Gibson static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
1003133e9b22SMarkus Armbruster                                          const char *name, int devfn,
1004133e9b22SMarkus Armbruster                                          Error **errp)
1005315a1350SMichael S. Tsirkin {
1006315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
1007315a1350SMichael S. Tsirkin     PCIConfigReadFunc *config_read = pc->config_read;
1008315a1350SMichael S. Tsirkin     PCIConfigWriteFunc *config_write = pc->config_write;
1009133e9b22SMarkus Armbruster     Error *local_err = NULL;
10103f1e1478SCao jin     DeviceState *dev = DEVICE(pci_dev);
1011fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
10123f1e1478SCao jin 
10130144f6f1SMarcel Apfelbaum     /* Only pci bridges can be attached to extra PCI root buses */
10140144f6f1SMarcel Apfelbaum     if (pci_bus_is_root(bus) && bus->parent_dev && !pc->is_bridge) {
10150144f6f1SMarcel Apfelbaum         error_setg(errp,
10160144f6f1SMarcel Apfelbaum                    "PCI: Only PCI/PCIe bridges can be plugged into %s",
10170144f6f1SMarcel Apfelbaum                     bus->parent_dev->name);
10180144f6f1SMarcel Apfelbaum         return NULL;
10190144f6f1SMarcel Apfelbaum     }
1020315a1350SMichael S. Tsirkin 
1021315a1350SMichael S. Tsirkin     if (devfn < 0) {
1022315a1350SMichael S. Tsirkin         for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
1023315a1350SMichael S. Tsirkin             devfn += PCI_FUNC_MAX) {
10248b884984SMark Cave-Ayland             if (pci_bus_devfn_available(bus, devfn) &&
10258b884984SMark Cave-Ayland                    !pci_bus_devfn_reserved(bus, devfn)) {
1026315a1350SMichael S. Tsirkin                 goto found;
1027315a1350SMichael S. Tsirkin             }
10289b717a3aSMark Cave-Ayland         }
10298b884984SMark Cave-Ayland         error_setg(errp, "PCI: no slot/function available for %s, all in use "
10308b884984SMark Cave-Ayland                    "or reserved", name);
1031315a1350SMichael S. Tsirkin         return NULL;
1032315a1350SMichael S. Tsirkin     found: ;
10338b884984SMark Cave-Ayland     } else if (pci_bus_devfn_reserved(bus, devfn)) {
10348b884984SMark Cave-Ayland         error_setg(errp, "PCI: slot %d function %d not available for %s,"
10358b884984SMark Cave-Ayland                    " reserved",
10368b884984SMark Cave-Ayland                    PCI_SLOT(devfn), PCI_FUNC(devfn), name);
10378b884984SMark Cave-Ayland         return NULL;
10389b717a3aSMark Cave-Ayland     } else if (!pci_bus_devfn_available(bus, devfn)) {
1039133e9b22SMarkus Armbruster         error_setg(errp, "PCI: slot %d function %d not available for %s,"
1040133e9b22SMarkus Armbruster                    " in use by %s",
1041133e9b22SMarkus Armbruster                    PCI_SLOT(devfn), PCI_FUNC(devfn), name,
1042133e9b22SMarkus Armbruster                    bus->devices[devfn]->name);
1043315a1350SMichael S. Tsirkin         return NULL;
10443f1e1478SCao jin     } else if (dev->hotplugged &&
10453f1e1478SCao jin                pci_get_function_0(pci_dev)) {
10463298bbceSJulia Suvorova         error_setg(errp, "PCI: slot %d function 0 already occupied by %s,"
10473f1e1478SCao jin                    " new func %s cannot be exposed to guest.",
1048d93ddfb1SMichael S. Tsirkin                    PCI_SLOT(pci_get_function_0(pci_dev)->devfn),
1049d93ddfb1SMichael S. Tsirkin                    pci_get_function_0(pci_dev)->name,
10503f1e1478SCao jin                    name);
10513f1e1478SCao jin 
10523f1e1478SCao jin        return NULL;
1053315a1350SMichael S. Tsirkin     }
1054e00387d5SAvi Kivity 
1055efc8188eSLe Tan     pci_dev->devfn = devfn;
10564a94b3aaSPeter Xu     pci_dev->requester_id_cache = pci_req_id_cache_get(pci_dev);
1057d06bce95SAlexey Kardashevskiy     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
1058e00387d5SAvi Kivity 
10593716d590SJason Wang     memory_region_init(&pci_dev->bus_master_container_region, OBJECT(pci_dev),
10603716d590SJason Wang                        "bus master container", UINT64_MAX);
10613716d590SJason Wang     address_space_init(&pci_dev->bus_master_as,
10623716d590SJason Wang                        &pci_dev->bus_master_container_region, pci_dev->name);
10633716d590SJason Wang 
1064b86eacb8SMarcel Apfelbaum     if (qdev_hotplug) {
1065b86eacb8SMarcel Apfelbaum         pci_init_bus_master(pci_dev);
1066b86eacb8SMarcel Apfelbaum     }
1067315a1350SMichael S. Tsirkin     pci_dev->irq_state = 0;
1068315a1350SMichael S. Tsirkin     pci_config_alloc(pci_dev);
1069315a1350SMichael S. Tsirkin 
1070315a1350SMichael S. Tsirkin     pci_config_set_vendor_id(pci_dev->config, pc->vendor_id);
1071315a1350SMichael S. Tsirkin     pci_config_set_device_id(pci_dev->config, pc->device_id);
1072315a1350SMichael S. Tsirkin     pci_config_set_revision(pci_dev->config, pc->revision);
1073315a1350SMichael S. Tsirkin     pci_config_set_class(pci_dev->config, pc->class_id);
1074315a1350SMichael S. Tsirkin 
1075315a1350SMichael S. Tsirkin     if (!pc->is_bridge) {
1076315a1350SMichael S. Tsirkin         if (pc->subsystem_vendor_id || pc->subsystem_id) {
1077315a1350SMichael S. Tsirkin             pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
1078315a1350SMichael S. Tsirkin                          pc->subsystem_vendor_id);
1079315a1350SMichael S. Tsirkin             pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
1080315a1350SMichael S. Tsirkin                          pc->subsystem_id);
1081315a1350SMichael S. Tsirkin         } else {
1082315a1350SMichael S. Tsirkin             pci_set_default_subsystem_id(pci_dev);
1083315a1350SMichael S. Tsirkin         }
1084315a1350SMichael S. Tsirkin     } else {
1085315a1350SMichael S. Tsirkin         /* subsystem_vendor_id/subsystem_id are only for header type 0 */
1086315a1350SMichael S. Tsirkin         assert(!pc->subsystem_vendor_id);
1087315a1350SMichael S. Tsirkin         assert(!pc->subsystem_id);
1088315a1350SMichael S. Tsirkin     }
1089315a1350SMichael S. Tsirkin     pci_init_cmask(pci_dev);
1090315a1350SMichael S. Tsirkin     pci_init_wmask(pci_dev);
1091315a1350SMichael S. Tsirkin     pci_init_w1cmask(pci_dev);
1092315a1350SMichael S. Tsirkin     if (pc->is_bridge) {
1093315a1350SMichael S. Tsirkin         pci_init_mask_bridge(pci_dev);
1094315a1350SMichael S. Tsirkin     }
1095133e9b22SMarkus Armbruster     pci_init_multifunction(bus, pci_dev, &local_err);
1096133e9b22SMarkus Armbruster     if (local_err) {
1097133e9b22SMarkus Armbruster         error_propagate(errp, local_err);
109830607764SMarcel Apfelbaum         do_pci_unregister_device(pci_dev);
1099315a1350SMichael S. Tsirkin         return NULL;
1100315a1350SMichael S. Tsirkin     }
1101315a1350SMichael S. Tsirkin 
1102315a1350SMichael S. Tsirkin     if (!config_read)
1103315a1350SMichael S. Tsirkin         config_read = pci_default_read_config;
1104315a1350SMichael S. Tsirkin     if (!config_write)
1105315a1350SMichael S. Tsirkin         config_write = pci_default_write_config;
1106315a1350SMichael S. Tsirkin     pci_dev->config_read = config_read;
1107315a1350SMichael S. Tsirkin     pci_dev->config_write = config_write;
1108315a1350SMichael S. Tsirkin     bus->devices[devfn] = pci_dev;
1109315a1350SMichael S. Tsirkin     pci_dev->version_id = 2; /* Current pci device vmstate version */
1110315a1350SMichael S. Tsirkin     return pci_dev;
1111315a1350SMichael S. Tsirkin }
1112315a1350SMichael S. Tsirkin 
1113315a1350SMichael S. Tsirkin static void pci_unregister_io_regions(PCIDevice *pci_dev)
1114315a1350SMichael S. Tsirkin {
1115315a1350SMichael S. Tsirkin     PCIIORegion *r;
1116315a1350SMichael S. Tsirkin     int i;
1117315a1350SMichael S. Tsirkin 
1118315a1350SMichael S. Tsirkin     for(i = 0; i < PCI_NUM_REGIONS; i++) {
1119315a1350SMichael S. Tsirkin         r = &pci_dev->io_regions[i];
1120315a1350SMichael S. Tsirkin         if (!r->size || r->addr == PCI_BAR_UNMAPPED)
1121315a1350SMichael S. Tsirkin             continue;
1122315a1350SMichael S. Tsirkin         memory_region_del_subregion(r->address_space, r->memory);
1123315a1350SMichael S. Tsirkin     }
1124e01fd687SAlex Williamson 
1125e01fd687SAlex Williamson     pci_unregister_vga(pci_dev);
1126315a1350SMichael S. Tsirkin }
1127315a1350SMichael S. Tsirkin 
1128b69c3c21SMarkus Armbruster static void pci_qdev_unrealize(DeviceState *dev)
1129315a1350SMichael S. Tsirkin {
1130315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = PCI_DEVICE(dev);
1131315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
1132315a1350SMichael S. Tsirkin 
1133315a1350SMichael S. Tsirkin     pci_unregister_io_regions(pci_dev);
1134315a1350SMichael S. Tsirkin     pci_del_option_rom(pci_dev);
1135315a1350SMichael S. Tsirkin 
1136315a1350SMichael S. Tsirkin     if (pc->exit) {
1137315a1350SMichael S. Tsirkin         pc->exit(pci_dev);
1138315a1350SMichael S. Tsirkin     }
1139315a1350SMichael S. Tsirkin 
11403936161fSHerongguang (Stephen)     pci_device_deassert_intx(pci_dev);
1141315a1350SMichael S. Tsirkin     do_pci_unregister_device(pci_dev);
1142315a1350SMichael S. Tsirkin }
1143315a1350SMichael S. Tsirkin 
1144315a1350SMichael S. Tsirkin void pci_register_bar(PCIDevice *pci_dev, int region_num,
1145315a1350SMichael S. Tsirkin                       uint8_t type, MemoryRegion *memory)
1146315a1350SMichael S. Tsirkin {
1147315a1350SMichael S. Tsirkin     PCIIORegion *r;
11485178ecd8SCao jin     uint32_t addr; /* offset in pci config space */
1149315a1350SMichael S. Tsirkin     uint64_t wmask;
1150315a1350SMichael S. Tsirkin     pcibus_t size = memory_region_size(memory);
1151315a1350SMichael S. Tsirkin 
1152315a1350SMichael S. Tsirkin     assert(region_num >= 0);
1153315a1350SMichael S. Tsirkin     assert(region_num < PCI_NUM_REGIONS);
1154315a1350SMichael S. Tsirkin     if (size & (size-1)) {
11550151abe4SAlistair Francis         error_report("ERROR: PCI region size must be pow2 "
11560151abe4SAlistair Francis                     "type=0x%x, size=0x%"FMT_PCIBUS"", type, size);
1157315a1350SMichael S. Tsirkin         exit(1);
1158315a1350SMichael S. Tsirkin     }
1159315a1350SMichael S. Tsirkin 
1160315a1350SMichael S. Tsirkin     r = &pci_dev->io_regions[region_num];
1161315a1350SMichael S. Tsirkin     r->addr = PCI_BAR_UNMAPPED;
1162315a1350SMichael S. Tsirkin     r->size = size;
1163315a1350SMichael S. Tsirkin     r->type = type;
11645178ecd8SCao jin     r->memory = memory;
11655178ecd8SCao jin     r->address_space = type & PCI_BASE_ADDRESS_SPACE_IO
1166fd56e061SDavid Gibson                         ? pci_get_bus(pci_dev)->address_space_io
1167fd56e061SDavid Gibson                         : pci_get_bus(pci_dev)->address_space_mem;
1168315a1350SMichael S. Tsirkin 
1169315a1350SMichael S. Tsirkin     wmask = ~(size - 1);
1170315a1350SMichael S. Tsirkin     if (region_num == PCI_ROM_SLOT) {
1171315a1350SMichael S. Tsirkin         /* ROM enable bit is writable */
1172315a1350SMichael S. Tsirkin         wmask |= PCI_ROM_ADDRESS_ENABLE;
1173315a1350SMichael S. Tsirkin     }
11745178ecd8SCao jin 
11755178ecd8SCao jin     addr = pci_bar(pci_dev, region_num);
1176315a1350SMichael S. Tsirkin     pci_set_long(pci_dev->config + addr, type);
11775178ecd8SCao jin 
1178315a1350SMichael S. Tsirkin     if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
1179315a1350SMichael S. Tsirkin         r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1180315a1350SMichael S. Tsirkin         pci_set_quad(pci_dev->wmask + addr, wmask);
1181315a1350SMichael S. Tsirkin         pci_set_quad(pci_dev->cmask + addr, ~0ULL);
1182315a1350SMichael S. Tsirkin     } else {
1183315a1350SMichael S. Tsirkin         pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
1184315a1350SMichael S. Tsirkin         pci_set_long(pci_dev->cmask + addr, 0xffffffff);
1185315a1350SMichael S. Tsirkin     }
1186315a1350SMichael S. Tsirkin }
1187315a1350SMichael S. Tsirkin 
1188e01fd687SAlex Williamson static void pci_update_vga(PCIDevice *pci_dev)
1189e01fd687SAlex Williamson {
1190e01fd687SAlex Williamson     uint16_t cmd;
1191e01fd687SAlex Williamson 
1192e01fd687SAlex Williamson     if (!pci_dev->has_vga) {
1193e01fd687SAlex Williamson         return;
1194e01fd687SAlex Williamson     }
1195e01fd687SAlex Williamson 
1196e01fd687SAlex Williamson     cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
1197e01fd687SAlex Williamson 
1198e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM],
1199e01fd687SAlex Williamson                               cmd & PCI_COMMAND_MEMORY);
1200e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO],
1201e01fd687SAlex Williamson                               cmd & PCI_COMMAND_IO);
1202e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI],
1203e01fd687SAlex Williamson                               cmd & PCI_COMMAND_IO);
1204e01fd687SAlex Williamson }
1205e01fd687SAlex Williamson 
1206e01fd687SAlex Williamson void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
1207e01fd687SAlex Williamson                       MemoryRegion *io_lo, MemoryRegion *io_hi)
1208e01fd687SAlex Williamson {
1209fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
1210fd56e061SDavid Gibson 
1211e01fd687SAlex Williamson     assert(!pci_dev->has_vga);
1212e01fd687SAlex Williamson 
1213e01fd687SAlex Williamson     assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE);
1214e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem;
1215fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_mem,
1216e01fd687SAlex Williamson                                         QEMU_PCI_VGA_MEM_BASE, mem, 1);
1217e01fd687SAlex Williamson 
1218e01fd687SAlex Williamson     assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE);
1219e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo;
1220fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_io,
1221e01fd687SAlex Williamson                                         QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1);
1222e01fd687SAlex Williamson 
1223e01fd687SAlex Williamson     assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE);
1224e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi;
1225fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_io,
1226e01fd687SAlex Williamson                                         QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1);
1227e01fd687SAlex Williamson     pci_dev->has_vga = true;
1228e01fd687SAlex Williamson 
1229e01fd687SAlex Williamson     pci_update_vga(pci_dev);
1230e01fd687SAlex Williamson }
1231e01fd687SAlex Williamson 
1232e01fd687SAlex Williamson void pci_unregister_vga(PCIDevice *pci_dev)
1233e01fd687SAlex Williamson {
1234fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
1235fd56e061SDavid Gibson 
1236e01fd687SAlex Williamson     if (!pci_dev->has_vga) {
1237e01fd687SAlex Williamson         return;
1238e01fd687SAlex Williamson     }
1239e01fd687SAlex Williamson 
1240fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_mem,
1241e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_MEM]);
1242fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_io,
1243e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]);
1244fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_io,
1245e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]);
1246e01fd687SAlex Williamson     pci_dev->has_vga = false;
1247e01fd687SAlex Williamson }
1248e01fd687SAlex Williamson 
1249315a1350SMichael S. Tsirkin pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
1250315a1350SMichael S. Tsirkin {
1251315a1350SMichael S. Tsirkin     return pci_dev->io_regions[region_num].addr;
1252315a1350SMichael S. Tsirkin }
1253315a1350SMichael S. Tsirkin 
1254315a1350SMichael S. Tsirkin static pcibus_t pci_bar_address(PCIDevice *d,
1255315a1350SMichael S. Tsirkin                                 int reg, uint8_t type, pcibus_t size)
1256315a1350SMichael S. Tsirkin {
1257315a1350SMichael S. Tsirkin     pcibus_t new_addr, last_addr;
1258315a1350SMichael S. Tsirkin     int bar = pci_bar(d, reg);
1259315a1350SMichael S. Tsirkin     uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
1260e4024630SLaurent Vivier     Object *machine = qdev_get_machine();
1261e4024630SLaurent Vivier     ObjectClass *oc = object_get_class(machine);
1262e4024630SLaurent Vivier     MachineClass *mc = MACHINE_CLASS(oc);
1263e4024630SLaurent Vivier     bool allow_0_address = mc->pci_allow_0_address;
1264315a1350SMichael S. Tsirkin 
1265315a1350SMichael S. Tsirkin     if (type & PCI_BASE_ADDRESS_SPACE_IO) {
1266315a1350SMichael S. Tsirkin         if (!(cmd & PCI_COMMAND_IO)) {
1267315a1350SMichael S. Tsirkin             return PCI_BAR_UNMAPPED;
1268315a1350SMichael S. Tsirkin         }
1269315a1350SMichael S. Tsirkin         new_addr = pci_get_long(d->config + bar) & ~(size - 1);
1270315a1350SMichael S. Tsirkin         last_addr = new_addr + size - 1;
12719f1a029aSHervé Poussineau         /* Check if 32 bit BAR wraps around explicitly.
12729f1a029aSHervé Poussineau          * TODO: make priorities correct and remove this work around.
12739f1a029aSHervé Poussineau          */
1274e4024630SLaurent Vivier         if (last_addr <= new_addr || last_addr >= UINT32_MAX ||
1275e4024630SLaurent Vivier             (!allow_0_address && new_addr == 0)) {
1276315a1350SMichael S. Tsirkin             return PCI_BAR_UNMAPPED;
1277315a1350SMichael S. Tsirkin         }
1278315a1350SMichael S. Tsirkin         return new_addr;
1279315a1350SMichael S. Tsirkin     }
1280315a1350SMichael S. Tsirkin 
1281315a1350SMichael S. Tsirkin     if (!(cmd & PCI_COMMAND_MEMORY)) {
1282315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1283315a1350SMichael S. Tsirkin     }
1284315a1350SMichael S. Tsirkin     if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1285315a1350SMichael S. Tsirkin         new_addr = pci_get_quad(d->config + bar);
1286315a1350SMichael S. Tsirkin     } else {
1287315a1350SMichael S. Tsirkin         new_addr = pci_get_long(d->config + bar);
1288315a1350SMichael S. Tsirkin     }
1289315a1350SMichael S. Tsirkin     /* the ROM slot has a specific enable bit */
1290315a1350SMichael S. Tsirkin     if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
1291315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1292315a1350SMichael S. Tsirkin     }
1293315a1350SMichael S. Tsirkin     new_addr &= ~(size - 1);
1294315a1350SMichael S. Tsirkin     last_addr = new_addr + size - 1;
1295315a1350SMichael S. Tsirkin     /* NOTE: we do not support wrapping */
1296315a1350SMichael S. Tsirkin     /* XXX: as we cannot support really dynamic
1297315a1350SMichael S. Tsirkin        mappings, we handle specific values as invalid
1298315a1350SMichael S. Tsirkin        mappings. */
1299e4024630SLaurent Vivier     if (last_addr <= new_addr || last_addr == PCI_BAR_UNMAPPED ||
1300e4024630SLaurent Vivier         (!allow_0_address && new_addr == 0)) {
1301315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1302315a1350SMichael S. Tsirkin     }
1303315a1350SMichael S. Tsirkin 
1304315a1350SMichael S. Tsirkin     /* Now pcibus_t is 64bit.
1305315a1350SMichael S. Tsirkin      * Check if 32 bit BAR wraps around explicitly.
1306315a1350SMichael S. Tsirkin      * Without this, PC ide doesn't work well.
1307315a1350SMichael S. Tsirkin      * TODO: remove this work around.
1308315a1350SMichael S. Tsirkin      */
1309315a1350SMichael S. Tsirkin     if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
1310315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1311315a1350SMichael S. Tsirkin     }
1312315a1350SMichael S. Tsirkin 
1313315a1350SMichael S. Tsirkin     /*
1314315a1350SMichael S. Tsirkin      * OS is allowed to set BAR beyond its addressable
1315315a1350SMichael S. Tsirkin      * bits. For example, 32 bit OS can set 64bit bar
1316315a1350SMichael S. Tsirkin      * to >4G. Check it. TODO: we might need to support
1317315a1350SMichael S. Tsirkin      * it in the future for e.g. PAE.
1318315a1350SMichael S. Tsirkin      */
1319315a1350SMichael S. Tsirkin     if (last_addr >= HWADDR_MAX) {
1320315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1321315a1350SMichael S. Tsirkin     }
1322315a1350SMichael S. Tsirkin 
1323315a1350SMichael S. Tsirkin     return new_addr;
1324315a1350SMichael S. Tsirkin }
1325315a1350SMichael S. Tsirkin 
1326315a1350SMichael S. Tsirkin static void pci_update_mappings(PCIDevice *d)
1327315a1350SMichael S. Tsirkin {
1328315a1350SMichael S. Tsirkin     PCIIORegion *r;
1329315a1350SMichael S. Tsirkin     int i;
1330315a1350SMichael S. Tsirkin     pcibus_t new_addr;
1331315a1350SMichael S. Tsirkin 
1332315a1350SMichael S. Tsirkin     for(i = 0; i < PCI_NUM_REGIONS; i++) {
1333315a1350SMichael S. Tsirkin         r = &d->io_regions[i];
1334315a1350SMichael S. Tsirkin 
1335315a1350SMichael S. Tsirkin         /* this region isn't registered */
1336315a1350SMichael S. Tsirkin         if (!r->size)
1337315a1350SMichael S. Tsirkin             continue;
1338315a1350SMichael S. Tsirkin 
1339315a1350SMichael S. Tsirkin         new_addr = pci_bar_address(d, i, r->type, r->size);
1340315a1350SMichael S. Tsirkin 
1341315a1350SMichael S. Tsirkin         /* This bar isn't changed */
1342315a1350SMichael S. Tsirkin         if (new_addr == r->addr)
1343315a1350SMichael S. Tsirkin             continue;
1344315a1350SMichael S. Tsirkin 
1345315a1350SMichael S. Tsirkin         /* now do the real mapping */
1346315a1350SMichael S. Tsirkin         if (r->addr != PCI_BAR_UNMAPPED) {
1347fd56e061SDavid Gibson             trace_pci_update_mappings_del(d, pci_dev_bus_num(d),
13487828d750SDon Koch                                           PCI_SLOT(d->devfn),
13490f288f85SLaszlo Ersek                                           PCI_FUNC(d->devfn),
13507828d750SDon Koch                                           i, r->addr, r->size);
1351315a1350SMichael S. Tsirkin             memory_region_del_subregion(r->address_space, r->memory);
1352315a1350SMichael S. Tsirkin         }
1353315a1350SMichael S. Tsirkin         r->addr = new_addr;
1354315a1350SMichael S. Tsirkin         if (r->addr != PCI_BAR_UNMAPPED) {
1355fd56e061SDavid Gibson             trace_pci_update_mappings_add(d, pci_dev_bus_num(d),
13567828d750SDon Koch                                           PCI_SLOT(d->devfn),
13570f288f85SLaszlo Ersek                                           PCI_FUNC(d->devfn),
13587828d750SDon Koch                                           i, r->addr, r->size);
1359315a1350SMichael S. Tsirkin             memory_region_add_subregion_overlap(r->address_space,
1360315a1350SMichael S. Tsirkin                                                 r->addr, r->memory, 1);
1361315a1350SMichael S. Tsirkin         }
1362315a1350SMichael S. Tsirkin     }
1363e01fd687SAlex Williamson 
1364e01fd687SAlex Williamson     pci_update_vga(d);
1365315a1350SMichael S. Tsirkin }
1366315a1350SMichael S. Tsirkin 
1367315a1350SMichael S. Tsirkin static inline int pci_irq_disabled(PCIDevice *d)
1368315a1350SMichael S. Tsirkin {
1369315a1350SMichael S. Tsirkin     return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
1370315a1350SMichael S. Tsirkin }
1371315a1350SMichael S. Tsirkin 
1372315a1350SMichael S. Tsirkin /* Called after interrupt disabled field update in config space,
1373315a1350SMichael S. Tsirkin  * assert/deassert interrupts if necessary.
1374315a1350SMichael S. Tsirkin  * Gets original interrupt disable bit value (before update). */
1375315a1350SMichael S. Tsirkin static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
1376315a1350SMichael S. Tsirkin {
1377315a1350SMichael S. Tsirkin     int i, disabled = pci_irq_disabled(d);
1378315a1350SMichael S. Tsirkin     if (disabled == was_irq_disabled)
1379315a1350SMichael S. Tsirkin         return;
1380315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
1381315a1350SMichael S. Tsirkin         int state = pci_irq_state(d, i);
1382315a1350SMichael S. Tsirkin         pci_change_irq_level(d, i, disabled ? -state : state);
1383315a1350SMichael S. Tsirkin     }
1384315a1350SMichael S. Tsirkin }
1385315a1350SMichael S. Tsirkin 
1386315a1350SMichael S. Tsirkin uint32_t pci_default_read_config(PCIDevice *d,
1387315a1350SMichael S. Tsirkin                                  uint32_t address, int len)
1388315a1350SMichael S. Tsirkin {
1389315a1350SMichael S. Tsirkin     uint32_t val = 0;
1390315a1350SMichael S. Tsirkin 
1391f7d6a635SPrasad J Pandit     assert(address + len <= pci_config_size(d));
1392f7d6a635SPrasad J Pandit 
1393727b4866SAlex Williamson     if (pci_is_express_downstream_port(d) &&
1394727b4866SAlex Williamson         ranges_overlap(address, len, d->exp.exp_cap + PCI_EXP_LNKSTA, 2)) {
1395727b4866SAlex Williamson         pcie_sync_bridge_lnk(d);
1396727b4866SAlex Williamson     }
1397315a1350SMichael S. Tsirkin     memcpy(&val, d->config + address, len);
1398315a1350SMichael S. Tsirkin     return le32_to_cpu(val);
1399315a1350SMichael S. Tsirkin }
1400315a1350SMichael S. Tsirkin 
1401d7efb7e0SKnut Omang void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
1402315a1350SMichael S. Tsirkin {
1403315a1350SMichael S. Tsirkin     int i, was_irq_disabled = pci_irq_disabled(d);
1404d7efb7e0SKnut Omang     uint32_t val = val_in;
1405315a1350SMichael S. Tsirkin 
1406f7d6a635SPrasad J Pandit     assert(addr + l <= pci_config_size(d));
1407f7d6a635SPrasad J Pandit 
1408315a1350SMichael S. Tsirkin     for (i = 0; i < l; val >>= 8, ++i) {
1409315a1350SMichael S. Tsirkin         uint8_t wmask = d->wmask[addr + i];
1410315a1350SMichael S. Tsirkin         uint8_t w1cmask = d->w1cmask[addr + i];
1411315a1350SMichael S. Tsirkin         assert(!(wmask & w1cmask));
1412315a1350SMichael S. Tsirkin         d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
1413315a1350SMichael S. Tsirkin         d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
1414315a1350SMichael S. Tsirkin     }
1415315a1350SMichael S. Tsirkin     if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1416315a1350SMichael S. Tsirkin         ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1417315a1350SMichael S. Tsirkin         ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1418315a1350SMichael S. Tsirkin         range_covers_byte(addr, l, PCI_COMMAND))
1419315a1350SMichael S. Tsirkin         pci_update_mappings(d);
1420315a1350SMichael S. Tsirkin 
1421315a1350SMichael S. Tsirkin     if (range_covers_byte(addr, l, PCI_COMMAND)) {
1422315a1350SMichael S. Tsirkin         pci_update_irq_disabled(d, was_irq_disabled);
1423315a1350SMichael S. Tsirkin         memory_region_set_enabled(&d->bus_master_enable_region,
1424315a1350SMichael S. Tsirkin                                   pci_get_word(d->config + PCI_COMMAND)
1425315a1350SMichael S. Tsirkin                                     & PCI_COMMAND_MASTER);
1426315a1350SMichael S. Tsirkin     }
1427315a1350SMichael S. Tsirkin 
1428d7efb7e0SKnut Omang     msi_write_config(d, addr, val_in, l);
1429d7efb7e0SKnut Omang     msix_write_config(d, addr, val_in, l);
1430315a1350SMichael S. Tsirkin }
1431315a1350SMichael S. Tsirkin 
1432315a1350SMichael S. Tsirkin /***********************************************************/
1433315a1350SMichael S. Tsirkin /* generic PCI irq support */
1434315a1350SMichael S. Tsirkin 
1435315a1350SMichael S. Tsirkin /* 0 <= irq_num <= 3. level must be 0 or 1 */
1436d98f08f5SMarcel Apfelbaum static void pci_irq_handler(void *opaque, int irq_num, int level)
1437315a1350SMichael S. Tsirkin {
1438315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = opaque;
1439315a1350SMichael S. Tsirkin     int change;
1440315a1350SMichael S. Tsirkin 
1441315a1350SMichael S. Tsirkin     change = level - pci_irq_state(pci_dev, irq_num);
1442315a1350SMichael S. Tsirkin     if (!change)
1443315a1350SMichael S. Tsirkin         return;
1444315a1350SMichael S. Tsirkin 
1445315a1350SMichael S. Tsirkin     pci_set_irq_state(pci_dev, irq_num, level);
1446315a1350SMichael S. Tsirkin     pci_update_irq_status(pci_dev);
1447315a1350SMichael S. Tsirkin     if (pci_irq_disabled(pci_dev))
1448315a1350SMichael S. Tsirkin         return;
1449315a1350SMichael S. Tsirkin     pci_change_irq_level(pci_dev, irq_num, change);
1450315a1350SMichael S. Tsirkin }
1451315a1350SMichael S. Tsirkin 
1452d98f08f5SMarcel Apfelbaum static inline int pci_intx(PCIDevice *pci_dev)
1453d98f08f5SMarcel Apfelbaum {
1454d98f08f5SMarcel Apfelbaum     return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
1455d98f08f5SMarcel Apfelbaum }
1456d98f08f5SMarcel Apfelbaum 
1457d98f08f5SMarcel Apfelbaum qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
1458d98f08f5SMarcel Apfelbaum {
1459d98f08f5SMarcel Apfelbaum     int intx = pci_intx(pci_dev);
1460d98f08f5SMarcel Apfelbaum 
1461d98f08f5SMarcel Apfelbaum     return qemu_allocate_irq(pci_irq_handler, pci_dev, intx);
1462d98f08f5SMarcel Apfelbaum }
1463d98f08f5SMarcel Apfelbaum 
1464d98f08f5SMarcel Apfelbaum void pci_set_irq(PCIDevice *pci_dev, int level)
1465d98f08f5SMarcel Apfelbaum {
1466d98f08f5SMarcel Apfelbaum     int intx = pci_intx(pci_dev);
1467d98f08f5SMarcel Apfelbaum     pci_irq_handler(pci_dev, intx, level);
1468d98f08f5SMarcel Apfelbaum }
1469d98f08f5SMarcel Apfelbaum 
1470315a1350SMichael S. Tsirkin /* Special hooks used by device assignment */
1471315a1350SMichael S. Tsirkin void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq)
1472315a1350SMichael S. Tsirkin {
14730889464aSAlex Williamson     assert(pci_bus_is_root(bus));
1474315a1350SMichael S. Tsirkin     bus->route_intx_to_irq = route_intx_to_irq;
1475315a1350SMichael S. Tsirkin }
1476315a1350SMichael S. Tsirkin 
1477315a1350SMichael S. Tsirkin PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin)
1478315a1350SMichael S. Tsirkin {
1479315a1350SMichael S. Tsirkin     PCIBus *bus;
1480315a1350SMichael S. Tsirkin 
1481315a1350SMichael S. Tsirkin     do {
1482fd56e061SDavid Gibson         bus = pci_get_bus(dev);
1483315a1350SMichael S. Tsirkin         pin = bus->map_irq(dev, pin);
1484315a1350SMichael S. Tsirkin         dev = bus->parent_dev;
1485315a1350SMichael S. Tsirkin     } while (dev);
1486315a1350SMichael S. Tsirkin 
1487315a1350SMichael S. Tsirkin     if (!bus->route_intx_to_irq) {
1488312fd5f2SMarkus Armbruster         error_report("PCI: Bug - unimplemented PCI INTx routing (%s)",
1489315a1350SMichael S. Tsirkin                      object_get_typename(OBJECT(bus->qbus.parent)));
1490315a1350SMichael S. Tsirkin         return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 };
1491315a1350SMichael S. Tsirkin     }
1492315a1350SMichael S. Tsirkin 
1493315a1350SMichael S. Tsirkin     return bus->route_intx_to_irq(bus->irq_opaque, pin);
1494315a1350SMichael S. Tsirkin }
1495315a1350SMichael S. Tsirkin 
1496315a1350SMichael S. Tsirkin bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new)
1497315a1350SMichael S. Tsirkin {
1498315a1350SMichael S. Tsirkin     return old->mode != new->mode || old->irq != new->irq;
1499315a1350SMichael S. Tsirkin }
1500315a1350SMichael S. Tsirkin 
1501315a1350SMichael S. Tsirkin void pci_bus_fire_intx_routing_notifier(PCIBus *bus)
1502315a1350SMichael S. Tsirkin {
1503315a1350SMichael S. Tsirkin     PCIDevice *dev;
1504315a1350SMichael S. Tsirkin     PCIBus *sec;
1505315a1350SMichael S. Tsirkin     int i;
1506315a1350SMichael S. Tsirkin 
1507315a1350SMichael S. Tsirkin     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
1508315a1350SMichael S. Tsirkin         dev = bus->devices[i];
1509315a1350SMichael S. Tsirkin         if (dev && dev->intx_routing_notifier) {
1510315a1350SMichael S. Tsirkin             dev->intx_routing_notifier(dev);
1511315a1350SMichael S. Tsirkin         }
1512e5368f0dSAlex Williamson     }
1513e5368f0dSAlex Williamson 
1514315a1350SMichael S. Tsirkin     QLIST_FOREACH(sec, &bus->child, sibling) {
1515315a1350SMichael S. Tsirkin         pci_bus_fire_intx_routing_notifier(sec);
1516315a1350SMichael S. Tsirkin     }
1517315a1350SMichael S. Tsirkin }
1518315a1350SMichael S. Tsirkin 
1519315a1350SMichael S. Tsirkin void pci_device_set_intx_routing_notifier(PCIDevice *dev,
1520315a1350SMichael S. Tsirkin                                           PCIINTxRoutingNotifier notifier)
1521315a1350SMichael S. Tsirkin {
1522315a1350SMichael S. Tsirkin     dev->intx_routing_notifier = notifier;
1523315a1350SMichael S. Tsirkin }
1524315a1350SMichael S. Tsirkin 
1525315a1350SMichael S. Tsirkin /*
1526315a1350SMichael S. Tsirkin  * PCI-to-PCI bridge specification
1527315a1350SMichael S. Tsirkin  * 9.1: Interrupt routing. Table 9-1
1528315a1350SMichael S. Tsirkin  *
1529315a1350SMichael S. Tsirkin  * the PCI Express Base Specification, Revision 2.1
1530315a1350SMichael S. Tsirkin  * 2.2.8.1: INTx interrutp signaling - Rules
1531315a1350SMichael S. Tsirkin  *          the Implementation Note
1532315a1350SMichael S. Tsirkin  *          Table 2-20
1533315a1350SMichael S. Tsirkin  */
1534315a1350SMichael S. Tsirkin /*
1535315a1350SMichael S. Tsirkin  * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD
1536315a1350SMichael S. Tsirkin  * 0-origin unlike PCI interrupt pin register.
1537315a1350SMichael S. Tsirkin  */
1538315a1350SMichael S. Tsirkin int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
1539315a1350SMichael S. Tsirkin {
1540e8ec4adfSGreg Kurz     return pci_swizzle(PCI_SLOT(pci_dev->devfn), pin);
1541315a1350SMichael S. Tsirkin }
1542315a1350SMichael S. Tsirkin 
1543315a1350SMichael S. Tsirkin /***********************************************************/
1544315a1350SMichael S. Tsirkin /* monitor info on PCI */
1545315a1350SMichael S. Tsirkin 
1546315a1350SMichael S. Tsirkin typedef struct {
1547315a1350SMichael S. Tsirkin     uint16_t class;
1548315a1350SMichael S. Tsirkin     const char *desc;
1549315a1350SMichael S. Tsirkin     const char *fw_name;
1550315a1350SMichael S. Tsirkin     uint16_t fw_ign_bits;
1551315a1350SMichael S. Tsirkin } pci_class_desc;
1552315a1350SMichael S. Tsirkin 
1553315a1350SMichael S. Tsirkin static const pci_class_desc pci_class_descriptions[] =
1554315a1350SMichael S. Tsirkin {
1555315a1350SMichael S. Tsirkin     { 0x0001, "VGA controller", "display"},
1556315a1350SMichael S. Tsirkin     { 0x0100, "SCSI controller", "scsi"},
1557315a1350SMichael S. Tsirkin     { 0x0101, "IDE controller", "ide"},
1558315a1350SMichael S. Tsirkin     { 0x0102, "Floppy controller", "fdc"},
1559315a1350SMichael S. Tsirkin     { 0x0103, "IPI controller", "ipi"},
1560315a1350SMichael S. Tsirkin     { 0x0104, "RAID controller", "raid"},
1561315a1350SMichael S. Tsirkin     { 0x0106, "SATA controller"},
1562315a1350SMichael S. Tsirkin     { 0x0107, "SAS controller"},
1563315a1350SMichael S. Tsirkin     { 0x0180, "Storage controller"},
1564315a1350SMichael S. Tsirkin     { 0x0200, "Ethernet controller", "ethernet"},
1565315a1350SMichael S. Tsirkin     { 0x0201, "Token Ring controller", "token-ring"},
1566315a1350SMichael S. Tsirkin     { 0x0202, "FDDI controller", "fddi"},
1567315a1350SMichael S. Tsirkin     { 0x0203, "ATM controller", "atm"},
1568315a1350SMichael S. Tsirkin     { 0x0280, "Network controller"},
1569315a1350SMichael S. Tsirkin     { 0x0300, "VGA controller", "display", 0x00ff},
1570315a1350SMichael S. Tsirkin     { 0x0301, "XGA controller"},
1571315a1350SMichael S. Tsirkin     { 0x0302, "3D controller"},
1572315a1350SMichael S. Tsirkin     { 0x0380, "Display controller"},
1573315a1350SMichael S. Tsirkin     { 0x0400, "Video controller", "video"},
1574315a1350SMichael S. Tsirkin     { 0x0401, "Audio controller", "sound"},
1575315a1350SMichael S. Tsirkin     { 0x0402, "Phone"},
1576315a1350SMichael S. Tsirkin     { 0x0403, "Audio controller", "sound"},
1577315a1350SMichael S. Tsirkin     { 0x0480, "Multimedia controller"},
1578315a1350SMichael S. Tsirkin     { 0x0500, "RAM controller", "memory"},
1579315a1350SMichael S. Tsirkin     { 0x0501, "Flash controller", "flash"},
1580315a1350SMichael S. Tsirkin     { 0x0580, "Memory controller"},
1581315a1350SMichael S. Tsirkin     { 0x0600, "Host bridge", "host"},
1582315a1350SMichael S. Tsirkin     { 0x0601, "ISA bridge", "isa"},
1583315a1350SMichael S. Tsirkin     { 0x0602, "EISA bridge", "eisa"},
1584315a1350SMichael S. Tsirkin     { 0x0603, "MC bridge", "mca"},
15854c41425dSGerd Hoffmann     { 0x0604, "PCI bridge", "pci-bridge"},
1586315a1350SMichael S. Tsirkin     { 0x0605, "PCMCIA bridge", "pcmcia"},
1587315a1350SMichael S. Tsirkin     { 0x0606, "NUBUS bridge", "nubus"},
1588315a1350SMichael S. Tsirkin     { 0x0607, "CARDBUS bridge", "cardbus"},
1589315a1350SMichael S. Tsirkin     { 0x0608, "RACEWAY bridge"},
1590315a1350SMichael S. Tsirkin     { 0x0680, "Bridge"},
1591315a1350SMichael S. Tsirkin     { 0x0700, "Serial port", "serial"},
1592315a1350SMichael S. Tsirkin     { 0x0701, "Parallel port", "parallel"},
1593315a1350SMichael S. Tsirkin     { 0x0800, "Interrupt controller", "interrupt-controller"},
1594315a1350SMichael S. Tsirkin     { 0x0801, "DMA controller", "dma-controller"},
1595315a1350SMichael S. Tsirkin     { 0x0802, "Timer", "timer"},
1596315a1350SMichael S. Tsirkin     { 0x0803, "RTC", "rtc"},
1597315a1350SMichael S. Tsirkin     { 0x0900, "Keyboard", "keyboard"},
1598315a1350SMichael S. Tsirkin     { 0x0901, "Pen", "pen"},
1599315a1350SMichael S. Tsirkin     { 0x0902, "Mouse", "mouse"},
1600315a1350SMichael S. Tsirkin     { 0x0A00, "Dock station", "dock", 0x00ff},
1601315a1350SMichael S. Tsirkin     { 0x0B00, "i386 cpu", "cpu", 0x00ff},
1602315a1350SMichael S. Tsirkin     { 0x0c00, "Fireware contorller", "fireware"},
1603315a1350SMichael S. Tsirkin     { 0x0c01, "Access bus controller", "access-bus"},
1604315a1350SMichael S. Tsirkin     { 0x0c02, "SSA controller", "ssa"},
1605315a1350SMichael S. Tsirkin     { 0x0c03, "USB controller", "usb"},
1606315a1350SMichael S. Tsirkin     { 0x0c04, "Fibre channel controller", "fibre-channel"},
1607315a1350SMichael S. Tsirkin     { 0x0c05, "SMBus"},
1608315a1350SMichael S. Tsirkin     { 0, NULL}
1609315a1350SMichael S. Tsirkin };
1610315a1350SMichael S. Tsirkin 
1611a8eeafdaSGreg Kurz static void pci_for_each_device_under_bus_reverse(PCIBus *bus,
1612a8eeafdaSGreg Kurz                                                   void (*fn)(PCIBus *b,
1613a8eeafdaSGreg Kurz                                                              PCIDevice *d,
1614a8eeafdaSGreg Kurz                                                              void *opaque),
1615a8eeafdaSGreg Kurz                                                   void *opaque)
1616a8eeafdaSGreg Kurz {
1617a8eeafdaSGreg Kurz     PCIDevice *d;
1618a8eeafdaSGreg Kurz     int devfn;
1619a8eeafdaSGreg Kurz 
1620a8eeafdaSGreg Kurz     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1621a8eeafdaSGreg Kurz         d = bus->devices[ARRAY_SIZE(bus->devices) - 1 - devfn];
1622a8eeafdaSGreg Kurz         if (d) {
1623a8eeafdaSGreg Kurz             fn(bus, d, opaque);
1624a8eeafdaSGreg Kurz         }
1625a8eeafdaSGreg Kurz     }
1626a8eeafdaSGreg Kurz }
1627a8eeafdaSGreg Kurz 
1628a8eeafdaSGreg Kurz void pci_for_each_device_reverse(PCIBus *bus, int bus_num,
1629a8eeafdaSGreg Kurz                          void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
1630a8eeafdaSGreg Kurz                          void *opaque)
1631a8eeafdaSGreg Kurz {
1632a8eeafdaSGreg Kurz     bus = pci_find_bus_nr(bus, bus_num);
1633a8eeafdaSGreg Kurz 
1634a8eeafdaSGreg Kurz     if (bus) {
1635a8eeafdaSGreg Kurz         pci_for_each_device_under_bus_reverse(bus, fn, opaque);
1636a8eeafdaSGreg Kurz     }
1637a8eeafdaSGreg Kurz }
1638a8eeafdaSGreg Kurz 
1639315a1350SMichael S. Tsirkin static void pci_for_each_device_under_bus(PCIBus *bus,
1640315a1350SMichael S. Tsirkin                                           void (*fn)(PCIBus *b, PCIDevice *d,
1641315a1350SMichael S. Tsirkin                                                      void *opaque),
1642315a1350SMichael S. Tsirkin                                           void *opaque)
1643315a1350SMichael S. Tsirkin {
1644315a1350SMichael S. Tsirkin     PCIDevice *d;
1645315a1350SMichael S. Tsirkin     int devfn;
1646315a1350SMichael S. Tsirkin 
1647315a1350SMichael S. Tsirkin     for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1648315a1350SMichael S. Tsirkin         d = bus->devices[devfn];
1649315a1350SMichael S. Tsirkin         if (d) {
1650315a1350SMichael S. Tsirkin             fn(bus, d, opaque);
1651315a1350SMichael S. Tsirkin         }
1652315a1350SMichael S. Tsirkin     }
1653315a1350SMichael S. Tsirkin }
1654315a1350SMichael S. Tsirkin 
1655315a1350SMichael S. Tsirkin void pci_for_each_device(PCIBus *bus, int bus_num,
1656315a1350SMichael S. Tsirkin                          void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
1657315a1350SMichael S. Tsirkin                          void *opaque)
1658315a1350SMichael S. Tsirkin {
1659315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
1660315a1350SMichael S. Tsirkin 
1661315a1350SMichael S. Tsirkin     if (bus) {
1662315a1350SMichael S. Tsirkin         pci_for_each_device_under_bus(bus, fn, opaque);
1663315a1350SMichael S. Tsirkin     }
1664315a1350SMichael S. Tsirkin }
1665315a1350SMichael S. Tsirkin 
1666315a1350SMichael S. Tsirkin static const pci_class_desc *get_class_desc(int class)
1667315a1350SMichael S. Tsirkin {
1668315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
1669315a1350SMichael S. Tsirkin 
1670315a1350SMichael S. Tsirkin     desc = pci_class_descriptions;
1671315a1350SMichael S. Tsirkin     while (desc->desc && class != desc->class) {
1672315a1350SMichael S. Tsirkin         desc++;
1673315a1350SMichael S. Tsirkin     }
1674315a1350SMichael S. Tsirkin 
1675315a1350SMichael S. Tsirkin     return desc;
1676315a1350SMichael S. Tsirkin }
1677315a1350SMichael S. Tsirkin 
1678315a1350SMichael S. Tsirkin static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
1679315a1350SMichael S. Tsirkin 
1680315a1350SMichael S. Tsirkin static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
1681315a1350SMichael S. Tsirkin {
1682315a1350SMichael S. Tsirkin     PciMemoryRegionList *head = NULL, *cur_item = NULL;
1683315a1350SMichael S. Tsirkin     int i;
1684315a1350SMichael S. Tsirkin 
1685315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; i++) {
1686315a1350SMichael S. Tsirkin         const PCIIORegion *r = &dev->io_regions[i];
1687315a1350SMichael S. Tsirkin         PciMemoryRegionList *region;
1688315a1350SMichael S. Tsirkin 
1689315a1350SMichael S. Tsirkin         if (!r->size) {
1690315a1350SMichael S. Tsirkin             continue;
1691315a1350SMichael S. Tsirkin         }
1692315a1350SMichael S. Tsirkin 
1693315a1350SMichael S. Tsirkin         region = g_malloc0(sizeof(*region));
1694315a1350SMichael S. Tsirkin         region->value = g_malloc0(sizeof(*region->value));
1695315a1350SMichael S. Tsirkin 
1696315a1350SMichael S. Tsirkin         if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1697315a1350SMichael S. Tsirkin             region->value->type = g_strdup("io");
1698315a1350SMichael S. Tsirkin         } else {
1699315a1350SMichael S. Tsirkin             region->value->type = g_strdup("memory");
1700315a1350SMichael S. Tsirkin             region->value->has_prefetch = true;
1701315a1350SMichael S. Tsirkin             region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
1702315a1350SMichael S. Tsirkin             region->value->has_mem_type_64 = true;
1703315a1350SMichael S. Tsirkin             region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
1704315a1350SMichael S. Tsirkin         }
1705315a1350SMichael S. Tsirkin 
1706315a1350SMichael S. Tsirkin         region->value->bar = i;
1707315a1350SMichael S. Tsirkin         region->value->address = r->addr;
1708315a1350SMichael S. Tsirkin         region->value->size = r->size;
1709315a1350SMichael S. Tsirkin 
1710315a1350SMichael S. Tsirkin         /* XXX: waiting for the qapi to support GSList */
1711315a1350SMichael S. Tsirkin         if (!cur_item) {
1712315a1350SMichael S. Tsirkin             head = cur_item = region;
1713315a1350SMichael S. Tsirkin         } else {
1714315a1350SMichael S. Tsirkin             cur_item->next = region;
1715315a1350SMichael S. Tsirkin             cur_item = region;
1716315a1350SMichael S. Tsirkin         }
1717315a1350SMichael S. Tsirkin     }
1718315a1350SMichael S. Tsirkin 
1719315a1350SMichael S. Tsirkin     return head;
1720315a1350SMichael S. Tsirkin }
1721315a1350SMichael S. Tsirkin 
1722315a1350SMichael S. Tsirkin static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
1723315a1350SMichael S. Tsirkin                                            int bus_num)
1724315a1350SMichael S. Tsirkin {
1725315a1350SMichael S. Tsirkin     PciBridgeInfo *info;
17269fa02cd1SEric Blake     PciMemoryRange *range;
1727315a1350SMichael S. Tsirkin 
17289fa02cd1SEric Blake     info = g_new0(PciBridgeInfo, 1);
1729315a1350SMichael S. Tsirkin 
17309fa02cd1SEric Blake     info->bus = g_new0(PciBusInfo, 1);
17319fa02cd1SEric Blake     info->bus->number = dev->config[PCI_PRIMARY_BUS];
17329fa02cd1SEric Blake     info->bus->secondary = dev->config[PCI_SECONDARY_BUS];
17339fa02cd1SEric Blake     info->bus->subordinate = dev->config[PCI_SUBORDINATE_BUS];
1734315a1350SMichael S. Tsirkin 
17359fa02cd1SEric Blake     range = info->bus->io_range = g_new0(PciMemoryRange, 1);
17369fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
17379fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
1738315a1350SMichael S. Tsirkin 
17399fa02cd1SEric Blake     range = info->bus->memory_range = g_new0(PciMemoryRange, 1);
17409fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
17419fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
1742315a1350SMichael S. Tsirkin 
17439fa02cd1SEric Blake     range = info->bus->prefetchable_range = g_new0(PciMemoryRange, 1);
17449fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
17459fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
1746315a1350SMichael S. Tsirkin 
1747315a1350SMichael S. Tsirkin     if (dev->config[PCI_SECONDARY_BUS] != 0) {
1748315a1350SMichael S. Tsirkin         PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]);
1749315a1350SMichael S. Tsirkin         if (child_bus) {
1750315a1350SMichael S. Tsirkin             info->has_devices = true;
1751315a1350SMichael S. Tsirkin             info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]);
1752315a1350SMichael S. Tsirkin         }
1753315a1350SMichael S. Tsirkin     }
1754315a1350SMichael S. Tsirkin 
1755315a1350SMichael S. Tsirkin     return info;
1756315a1350SMichael S. Tsirkin }
1757315a1350SMichael S. Tsirkin 
1758315a1350SMichael S. Tsirkin static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
1759315a1350SMichael S. Tsirkin                                            int bus_num)
1760315a1350SMichael S. Tsirkin {
1761315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
1762315a1350SMichael S. Tsirkin     PciDeviceInfo *info;
1763315a1350SMichael S. Tsirkin     uint8_t type;
1764315a1350SMichael S. Tsirkin     int class;
1765315a1350SMichael S. Tsirkin 
17669fa02cd1SEric Blake     info = g_new0(PciDeviceInfo, 1);
1767315a1350SMichael S. Tsirkin     info->bus = bus_num;
1768315a1350SMichael S. Tsirkin     info->slot = PCI_SLOT(dev->devfn);
1769315a1350SMichael S. Tsirkin     info->function = PCI_FUNC(dev->devfn);
1770315a1350SMichael S. Tsirkin 
17719fa02cd1SEric Blake     info->class_info = g_new0(PciDeviceClass, 1);
1772315a1350SMichael S. Tsirkin     class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
17739fa02cd1SEric Blake     info->class_info->q_class = class;
1774315a1350SMichael S. Tsirkin     desc = get_class_desc(class);
1775315a1350SMichael S. Tsirkin     if (desc->desc) {
17769fa02cd1SEric Blake         info->class_info->has_desc = true;
17779fa02cd1SEric Blake         info->class_info->desc = g_strdup(desc->desc);
1778315a1350SMichael S. Tsirkin     }
1779315a1350SMichael S. Tsirkin 
17809fa02cd1SEric Blake     info->id = g_new0(PciDeviceId, 1);
17819fa02cd1SEric Blake     info->id->vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
17829fa02cd1SEric Blake     info->id->device = pci_get_word(dev->config + PCI_DEVICE_ID);
1783315a1350SMichael S. Tsirkin     info->regions = qmp_query_pci_regions(dev);
1784315a1350SMichael S. Tsirkin     info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
1785315a1350SMichael S. Tsirkin 
178612fcf49cSPeter Xu     info->irq_pin = dev->config[PCI_INTERRUPT_PIN];
1787315a1350SMichael S. Tsirkin     if (dev->config[PCI_INTERRUPT_PIN] != 0) {
1788315a1350SMichael S. Tsirkin         info->has_irq = true;
1789315a1350SMichael S. Tsirkin         info->irq = dev->config[PCI_INTERRUPT_LINE];
1790315a1350SMichael S. Tsirkin     }
1791315a1350SMichael S. Tsirkin 
1792315a1350SMichael S. Tsirkin     type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1793315a1350SMichael S. Tsirkin     if (type == PCI_HEADER_TYPE_BRIDGE) {
1794315a1350SMichael S. Tsirkin         info->has_pci_bridge = true;
1795315a1350SMichael S. Tsirkin         info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
179618613dc6SDenis V. Lunev     } else if (type == PCI_HEADER_TYPE_NORMAL) {
179718613dc6SDenis V. Lunev         info->id->has_subsystem = info->id->has_subsystem_vendor = true;
179818613dc6SDenis V. Lunev         info->id->subsystem = pci_get_word(dev->config + PCI_SUBSYSTEM_ID);
179918613dc6SDenis V. Lunev         info->id->subsystem_vendor =
180018613dc6SDenis V. Lunev             pci_get_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID);
180118613dc6SDenis V. Lunev     } else if (type == PCI_HEADER_TYPE_CARDBUS) {
180218613dc6SDenis V. Lunev         info->id->has_subsystem = info->id->has_subsystem_vendor = true;
180318613dc6SDenis V. Lunev         info->id->subsystem = pci_get_word(dev->config + PCI_CB_SUBSYSTEM_ID);
180418613dc6SDenis V. Lunev         info->id->subsystem_vendor =
180518613dc6SDenis V. Lunev             pci_get_word(dev->config + PCI_CB_SUBSYSTEM_VENDOR_ID);
1806315a1350SMichael S. Tsirkin     }
1807315a1350SMichael S. Tsirkin 
1808315a1350SMichael S. Tsirkin     return info;
1809315a1350SMichael S. Tsirkin }
1810315a1350SMichael S. Tsirkin 
1811315a1350SMichael S. Tsirkin static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
1812315a1350SMichael S. Tsirkin {
1813315a1350SMichael S. Tsirkin     PciDeviceInfoList *info, *head = NULL, *cur_item = NULL;
1814315a1350SMichael S. Tsirkin     PCIDevice *dev;
1815315a1350SMichael S. Tsirkin     int devfn;
1816315a1350SMichael S. Tsirkin 
1817315a1350SMichael S. Tsirkin     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1818315a1350SMichael S. Tsirkin         dev = bus->devices[devfn];
1819315a1350SMichael S. Tsirkin         if (dev) {
1820315a1350SMichael S. Tsirkin             info = g_malloc0(sizeof(*info));
1821315a1350SMichael S. Tsirkin             info->value = qmp_query_pci_device(dev, bus, bus_num);
1822315a1350SMichael S. Tsirkin 
1823315a1350SMichael S. Tsirkin             /* XXX: waiting for the qapi to support GSList */
1824315a1350SMichael S. Tsirkin             if (!cur_item) {
1825315a1350SMichael S. Tsirkin                 head = cur_item = info;
1826315a1350SMichael S. Tsirkin             } else {
1827315a1350SMichael S. Tsirkin                 cur_item->next = info;
1828315a1350SMichael S. Tsirkin                 cur_item = info;
1829315a1350SMichael S. Tsirkin             }
1830315a1350SMichael S. Tsirkin         }
1831315a1350SMichael S. Tsirkin     }
1832315a1350SMichael S. Tsirkin 
1833315a1350SMichael S. Tsirkin     return head;
1834315a1350SMichael S. Tsirkin }
1835315a1350SMichael S. Tsirkin 
1836315a1350SMichael S. Tsirkin static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
1837315a1350SMichael S. Tsirkin {
1838315a1350SMichael S. Tsirkin     PciInfo *info = NULL;
1839315a1350SMichael S. Tsirkin 
1840315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
1841315a1350SMichael S. Tsirkin     if (bus) {
1842315a1350SMichael S. Tsirkin         info = g_malloc0(sizeof(*info));
1843315a1350SMichael S. Tsirkin         info->bus = bus_num;
1844315a1350SMichael S. Tsirkin         info->devices = qmp_query_pci_devices(bus, bus_num);
1845315a1350SMichael S. Tsirkin     }
1846315a1350SMichael S. Tsirkin 
1847315a1350SMichael S. Tsirkin     return info;
1848315a1350SMichael S. Tsirkin }
1849315a1350SMichael S. Tsirkin 
1850315a1350SMichael S. Tsirkin PciInfoList *qmp_query_pci(Error **errp)
1851315a1350SMichael S. Tsirkin {
1852315a1350SMichael S. Tsirkin     PciInfoList *info, *head = NULL, *cur_item = NULL;
18537588e2b0SDavid Gibson     PCIHostState *host_bridge;
1854315a1350SMichael S. Tsirkin 
18557588e2b0SDavid Gibson     QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
1856315a1350SMichael S. Tsirkin         info = g_malloc0(sizeof(*info));
1857cb2ed8b3SMarcel Apfelbaum         info->value = qmp_query_pci_bus(host_bridge->bus,
1858cb2ed8b3SMarcel Apfelbaum                                         pci_bus_num(host_bridge->bus));
1859315a1350SMichael S. Tsirkin 
1860315a1350SMichael S. Tsirkin         /* XXX: waiting for the qapi to support GSList */
1861315a1350SMichael S. Tsirkin         if (!cur_item) {
1862315a1350SMichael S. Tsirkin             head = cur_item = info;
1863315a1350SMichael S. Tsirkin         } else {
1864315a1350SMichael S. Tsirkin             cur_item->next = info;
1865315a1350SMichael S. Tsirkin             cur_item = info;
1866315a1350SMichael S. Tsirkin         }
1867315a1350SMichael S. Tsirkin     }
1868315a1350SMichael S. Tsirkin 
1869315a1350SMichael S. Tsirkin     return head;
1870315a1350SMichael S. Tsirkin }
1871315a1350SMichael S. Tsirkin 
1872315a1350SMichael S. Tsirkin /* Initialize a PCI NIC.  */
187351f7cb97SThomas Huth PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
187429b358f9SDavid Gibson                                const char *default_model,
187551f7cb97SThomas Huth                                const char *default_devaddr)
1876315a1350SMichael S. Tsirkin {
1877315a1350SMichael S. Tsirkin     const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
187852310c3fSPaolo Bonzini     GSList *list;
187952310c3fSPaolo Bonzini     GPtrArray *pci_nic_models;
1880315a1350SMichael S. Tsirkin     PCIBus *bus;
1881315a1350SMichael S. Tsirkin     PCIDevice *pci_dev;
1882315a1350SMichael S. Tsirkin     DeviceState *dev;
188351f7cb97SThomas Huth     int devfn;
1884315a1350SMichael S. Tsirkin     int i;
18852ad778b8SDavid Gibson     int dom, busnr;
18862ad778b8SDavid Gibson     unsigned slot;
1887315a1350SMichael S. Tsirkin 
188852310c3fSPaolo Bonzini     if (nd->model && !strcmp(nd->model, "virtio")) {
188952310c3fSPaolo Bonzini         g_free(nd->model);
189052310c3fSPaolo Bonzini         nd->model = g_strdup("virtio-net-pci");
189152310c3fSPaolo Bonzini     }
189252310c3fSPaolo Bonzini 
189352310c3fSPaolo Bonzini     list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
189452310c3fSPaolo Bonzini     pci_nic_models = g_ptr_array_new();
189552310c3fSPaolo Bonzini     while (list) {
189652310c3fSPaolo Bonzini         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
189752310c3fSPaolo Bonzini                                              TYPE_DEVICE);
189852310c3fSPaolo Bonzini         GSList *next;
189952310c3fSPaolo Bonzini         if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
190052310c3fSPaolo Bonzini             dc->user_creatable) {
190152310c3fSPaolo Bonzini             const char *name = object_class_get_name(list->data);
190200823980SThomas Huth             /*
190300823980SThomas Huth              * A network device might also be something else than a NIC, see
190400823980SThomas Huth              * e.g. the "rocker" device. Thus we have to look for the "netdev"
190500823980SThomas Huth              * property, too. Unfortunately, some devices like virtio-net only
190600823980SThomas Huth              * create this property during instance_init, so we have to create
190700823980SThomas Huth              * a temporary instance here to be able to check it.
190800823980SThomas Huth              */
190900823980SThomas Huth             Object *obj = object_new_with_class(OBJECT_CLASS(dc));
1910efba1595SDaniel P. Berrangé             if (object_property_find(obj, "netdev")) {
191152310c3fSPaolo Bonzini                 g_ptr_array_add(pci_nic_models, (gpointer)name);
191252310c3fSPaolo Bonzini             }
191300823980SThomas Huth             object_unref(obj);
191400823980SThomas Huth         }
191552310c3fSPaolo Bonzini         next = list->next;
191652310c3fSPaolo Bonzini         g_slist_free_1(list);
191752310c3fSPaolo Bonzini         list = next;
191852310c3fSPaolo Bonzini     }
191952310c3fSPaolo Bonzini     g_ptr_array_add(pci_nic_models, NULL);
192052310c3fSPaolo Bonzini 
192152310c3fSPaolo Bonzini     if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) {
192251f7cb97SThomas Huth         exit(0);
192351f7cb97SThomas Huth     }
192451f7cb97SThomas Huth 
192552310c3fSPaolo Bonzini     i = qemu_find_nic_model(nd, (const char **)pci_nic_models->pdata,
192652310c3fSPaolo Bonzini                             default_model);
192751f7cb97SThomas Huth     if (i < 0) {
192851f7cb97SThomas Huth         exit(1);
192951f7cb97SThomas Huth     }
1930315a1350SMichael S. Tsirkin 
19312ad778b8SDavid Gibson     if (!rootbus) {
19322ad778b8SDavid Gibson         error_report("No primary PCI bus");
19332ad778b8SDavid Gibson         exit(1);
19342ad778b8SDavid Gibson     }
19352ad778b8SDavid Gibson 
19362ad778b8SDavid Gibson     assert(!rootbus->parent_dev);
19372ad778b8SDavid Gibson 
19382ad778b8SDavid Gibson     if (!devaddr) {
19392ad778b8SDavid Gibson         devfn = -1;
19402ad778b8SDavid Gibson         busnr = 0;
19412ad778b8SDavid Gibson     } else {
19422ad778b8SDavid Gibson         if (pci_parse_devaddr(devaddr, &dom, &busnr, &slot, NULL) < 0) {
19432ad778b8SDavid Gibson             error_report("Invalid PCI device address %s for device %s",
19442ad778b8SDavid Gibson                          devaddr, nd->model);
19452ad778b8SDavid Gibson             exit(1);
19462ad778b8SDavid Gibson         }
19472ad778b8SDavid Gibson 
19482ad778b8SDavid Gibson         if (dom != 0) {
19492ad778b8SDavid Gibson             error_report("No support for non-zero PCI domains");
19502ad778b8SDavid Gibson             exit(1);
19512ad778b8SDavid Gibson         }
19522ad778b8SDavid Gibson 
19532ad778b8SDavid Gibson         devfn = PCI_DEVFN(slot, 0);
19542ad778b8SDavid Gibson     }
19552ad778b8SDavid Gibson 
19562ad778b8SDavid Gibson     bus = pci_find_bus_nr(rootbus, busnr);
1957315a1350SMichael S. Tsirkin     if (!bus) {
1958315a1350SMichael S. Tsirkin         error_report("Invalid PCI device address %s for device %s",
195952310c3fSPaolo Bonzini                      devaddr, nd->model);
196051f7cb97SThomas Huth         exit(1);
1961315a1350SMichael S. Tsirkin     }
1962315a1350SMichael S. Tsirkin 
19639307d06dSMarkus Armbruster     pci_dev = pci_new(devfn, nd->model);
1964315a1350SMichael S. Tsirkin     dev = &pci_dev->qdev;
1965315a1350SMichael S. Tsirkin     qdev_set_nic_properties(dev, nd);
19669307d06dSMarkus Armbruster     pci_realize_and_unref(pci_dev, bus, &error_fatal);
196752310c3fSPaolo Bonzini     g_ptr_array_free(pci_nic_models, true);
196851f7cb97SThomas Huth     return pci_dev;
1969315a1350SMichael S. Tsirkin }
1970315a1350SMichael S. Tsirkin 
1971315a1350SMichael S. Tsirkin PCIDevice *pci_vga_init(PCIBus *bus)
1972315a1350SMichael S. Tsirkin {
1973315a1350SMichael S. Tsirkin     switch (vga_interface_type) {
1974315a1350SMichael S. Tsirkin     case VGA_CIRRUS:
1975315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "cirrus-vga");
1976315a1350SMichael S. Tsirkin     case VGA_QXL:
1977315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "qxl-vga");
1978315a1350SMichael S. Tsirkin     case VGA_STD:
1979315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "VGA");
1980315a1350SMichael S. Tsirkin     case VGA_VMWARE:
1981315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "vmware-svga");
1982a94f0c5cSGerd Hoffmann     case VGA_VIRTIO:
1983a94f0c5cSGerd Hoffmann         return pci_create_simple(bus, -1, "virtio-vga");
1984315a1350SMichael S. Tsirkin     case VGA_NONE:
1985315a1350SMichael S. Tsirkin     default: /* Other non-PCI types. Checking for unsupported types is already
1986315a1350SMichael S. Tsirkin                 done in vl.c. */
1987315a1350SMichael S. Tsirkin         return NULL;
1988315a1350SMichael S. Tsirkin     }
1989315a1350SMichael S. Tsirkin }
1990315a1350SMichael S. Tsirkin 
1991315a1350SMichael S. Tsirkin /* Whether a given bus number is in range of the secondary
1992315a1350SMichael S. Tsirkin  * bus of the given bridge device. */
1993315a1350SMichael S. Tsirkin static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
1994315a1350SMichael S. Tsirkin {
1995315a1350SMichael S. Tsirkin     return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
1996315a1350SMichael S. Tsirkin              PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
199709e5b819SMarcel Apfelbaum         dev->config[PCI_SECONDARY_BUS] <= bus_num &&
1998315a1350SMichael S. Tsirkin         bus_num <= dev->config[PCI_SUBORDINATE_BUS];
1999315a1350SMichael S. Tsirkin }
2000315a1350SMichael S. Tsirkin 
200109e5b819SMarcel Apfelbaum /* Whether a given bus number is in a range of a root bus */
200209e5b819SMarcel Apfelbaum static bool pci_root_bus_in_range(PCIBus *bus, int bus_num)
200309e5b819SMarcel Apfelbaum {
200409e5b819SMarcel Apfelbaum     int i;
200509e5b819SMarcel Apfelbaum 
200609e5b819SMarcel Apfelbaum     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
200709e5b819SMarcel Apfelbaum         PCIDevice *dev = bus->devices[i];
200809e5b819SMarcel Apfelbaum 
200909e5b819SMarcel Apfelbaum         if (dev && PCI_DEVICE_GET_CLASS(dev)->is_bridge) {
201009e5b819SMarcel Apfelbaum             if (pci_secondary_bus_in_range(dev, bus_num)) {
201109e5b819SMarcel Apfelbaum                 return true;
201209e5b819SMarcel Apfelbaum             }
201309e5b819SMarcel Apfelbaum         }
201409e5b819SMarcel Apfelbaum     }
201509e5b819SMarcel Apfelbaum 
201609e5b819SMarcel Apfelbaum     return false;
201709e5b819SMarcel Apfelbaum }
201809e5b819SMarcel Apfelbaum 
2019315a1350SMichael S. Tsirkin static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
2020315a1350SMichael S. Tsirkin {
2021315a1350SMichael S. Tsirkin     PCIBus *sec;
2022315a1350SMichael S. Tsirkin 
2023315a1350SMichael S. Tsirkin     if (!bus) {
2024315a1350SMichael S. Tsirkin         return NULL;
2025315a1350SMichael S. Tsirkin     }
2026315a1350SMichael S. Tsirkin 
2027315a1350SMichael S. Tsirkin     if (pci_bus_num(bus) == bus_num) {
2028315a1350SMichael S. Tsirkin         return bus;
2029315a1350SMichael S. Tsirkin     }
2030315a1350SMichael S. Tsirkin 
2031315a1350SMichael S. Tsirkin     /* Consider all bus numbers in range for the host pci bridge. */
20320889464aSAlex Williamson     if (!pci_bus_is_root(bus) &&
2033315a1350SMichael S. Tsirkin         !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
2034315a1350SMichael S. Tsirkin         return NULL;
2035315a1350SMichael S. Tsirkin     }
2036315a1350SMichael S. Tsirkin 
2037315a1350SMichael S. Tsirkin     /* try child bus */
2038315a1350SMichael S. Tsirkin     for (; bus; bus = sec) {
2039315a1350SMichael S. Tsirkin         QLIST_FOREACH(sec, &bus->child, sibling) {
204009e5b819SMarcel Apfelbaum             if (pci_bus_num(sec) == bus_num) {
2041315a1350SMichael S. Tsirkin                 return sec;
2042315a1350SMichael S. Tsirkin             }
204309e5b819SMarcel Apfelbaum             /* PXB buses assumed to be children of bus 0 */
204409e5b819SMarcel Apfelbaum             if (pci_bus_is_root(sec)) {
204509e5b819SMarcel Apfelbaum                 if (pci_root_bus_in_range(sec, bus_num)) {
204609e5b819SMarcel Apfelbaum                     break;
204709e5b819SMarcel Apfelbaum                 }
204809e5b819SMarcel Apfelbaum             } else {
2049315a1350SMichael S. Tsirkin                 if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
2050315a1350SMichael S. Tsirkin                     break;
2051315a1350SMichael S. Tsirkin                 }
2052315a1350SMichael S. Tsirkin             }
2053315a1350SMichael S. Tsirkin         }
205409e5b819SMarcel Apfelbaum     }
2055315a1350SMichael S. Tsirkin 
2056315a1350SMichael S. Tsirkin     return NULL;
2057315a1350SMichael S. Tsirkin }
2058315a1350SMichael S. Tsirkin 
2059eb0acfddSMichael S. Tsirkin void pci_for_each_bus_depth_first(PCIBus *bus,
2060eb0acfddSMichael S. Tsirkin                                   void *(*begin)(PCIBus *bus, void *parent_state),
2061eb0acfddSMichael S. Tsirkin                                   void (*end)(PCIBus *bus, void *state),
2062eb0acfddSMichael S. Tsirkin                                   void *parent_state)
2063eb0acfddSMichael S. Tsirkin {
2064eb0acfddSMichael S. Tsirkin     PCIBus *sec;
2065eb0acfddSMichael S. Tsirkin     void *state;
2066eb0acfddSMichael S. Tsirkin 
2067eb0acfddSMichael S. Tsirkin     if (!bus) {
2068eb0acfddSMichael S. Tsirkin         return;
2069eb0acfddSMichael S. Tsirkin     }
2070eb0acfddSMichael S. Tsirkin 
2071eb0acfddSMichael S. Tsirkin     if (begin) {
2072eb0acfddSMichael S. Tsirkin         state = begin(bus, parent_state);
2073eb0acfddSMichael S. Tsirkin     } else {
2074eb0acfddSMichael S. Tsirkin         state = parent_state;
2075eb0acfddSMichael S. Tsirkin     }
2076eb0acfddSMichael S. Tsirkin 
2077eb0acfddSMichael S. Tsirkin     QLIST_FOREACH(sec, &bus->child, sibling) {
2078eb0acfddSMichael S. Tsirkin         pci_for_each_bus_depth_first(sec, begin, end, state);
2079eb0acfddSMichael S. Tsirkin     }
2080eb0acfddSMichael S. Tsirkin 
2081eb0acfddSMichael S. Tsirkin     if (end) {
2082eb0acfddSMichael S. Tsirkin         end(bus, state);
2083eb0acfddSMichael S. Tsirkin     }
2084eb0acfddSMichael S. Tsirkin }
2085eb0acfddSMichael S. Tsirkin 
2086eb0acfddSMichael S. Tsirkin 
2087315a1350SMichael S. Tsirkin PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
2088315a1350SMichael S. Tsirkin {
2089315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
2090315a1350SMichael S. Tsirkin 
2091315a1350SMichael S. Tsirkin     if (!bus)
2092315a1350SMichael S. Tsirkin         return NULL;
2093315a1350SMichael S. Tsirkin 
2094315a1350SMichael S. Tsirkin     return bus->devices[devfn];
2095315a1350SMichael S. Tsirkin }
2096315a1350SMichael S. Tsirkin 
2097133e9b22SMarkus Armbruster static void pci_qdev_realize(DeviceState *qdev, Error **errp)
2098315a1350SMichael S. Tsirkin {
2099315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = (PCIDevice *)qdev;
2100315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
2101d61a363dSYoni Bettan     ObjectClass *klass = OBJECT_CLASS(pc);
2102133e9b22SMarkus Armbruster     Error *local_err = NULL;
2103315a1350SMichael S. Tsirkin     bool is_default_rom;
21044f5b6a05SJens Freimann     uint16_t class_id;
2105315a1350SMichael S. Tsirkin 
2106d61a363dSYoni Bettan     /* initialize cap_present for pci_is_express() and pci_config_size(),
2107d61a363dSYoni Bettan      * Note that hybrid PCIs are not set automatically and need to manage
2108d61a363dSYoni Bettan      * QEMU_PCI_CAP_EXPRESS manually */
2109d61a363dSYoni Bettan     if (object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE) &&
2110d61a363dSYoni Bettan        !object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE)) {
2111315a1350SMichael S. Tsirkin         pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
2112315a1350SMichael S. Tsirkin     }
2113315a1350SMichael S. Tsirkin 
2114fd56e061SDavid Gibson     pci_dev = do_pci_register_device(pci_dev,
2115315a1350SMichael S. Tsirkin                                      object_get_typename(OBJECT(qdev)),
2116133e9b22SMarkus Armbruster                                      pci_dev->devfn, errp);
2117315a1350SMichael S. Tsirkin     if (pci_dev == NULL)
2118133e9b22SMarkus Armbruster         return;
21192897ae02SIgor Mammedov 
21207ee6c1e1SMarkus Armbruster     if (pc->realize) {
21217ee6c1e1SMarkus Armbruster         pc->realize(pci_dev, &local_err);
21227ee6c1e1SMarkus Armbruster         if (local_err) {
21237ee6c1e1SMarkus Armbruster             error_propagate(errp, local_err);
2124315a1350SMichael S. Tsirkin             do_pci_unregister_device(pci_dev);
2125133e9b22SMarkus Armbruster             return;
2126315a1350SMichael S. Tsirkin         }
2127315a1350SMichael S. Tsirkin     }
2128315a1350SMichael S. Tsirkin 
21294f5b6a05SJens Freimann     if (pci_dev->failover_pair_id) {
21304f5b6a05SJens Freimann         if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
21314f5b6a05SJens Freimann             error_setg(errp, "failover primary device must be on "
21324f5b6a05SJens Freimann                              "PCIExpress bus");
2133b69c3c21SMarkus Armbruster             pci_qdev_unrealize(DEVICE(pci_dev));
21344f5b6a05SJens Freimann             return;
21354f5b6a05SJens Freimann         }
21364f5b6a05SJens Freimann         class_id = pci_get_word(pci_dev->config + PCI_CLASS_DEVICE);
21374f5b6a05SJens Freimann         if (class_id != PCI_CLASS_NETWORK_ETHERNET) {
21384f5b6a05SJens Freimann             error_setg(errp, "failover primary device is not an "
21394f5b6a05SJens Freimann                              "Ethernet device");
2140b69c3c21SMarkus Armbruster             pci_qdev_unrealize(DEVICE(pci_dev));
21414f5b6a05SJens Freimann             return;
21424f5b6a05SJens Freimann         }
21434f5b6a05SJens Freimann         if (!(pci_dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)
21444f5b6a05SJens Freimann             && (PCI_FUNC(pci_dev->devfn) == 0)) {
21454f5b6a05SJens Freimann             qdev->allow_unplug_during_migration = true;
21464f5b6a05SJens Freimann         } else {
21474f5b6a05SJens Freimann             error_setg(errp, "failover: primary device must be in its own "
21484f5b6a05SJens Freimann                               "PCI slot");
2149b69c3c21SMarkus Armbruster             pci_qdev_unrealize(DEVICE(pci_dev));
21504f5b6a05SJens Freimann             return;
21514f5b6a05SJens Freimann         }
2152a1190ab6SJens Freimann         qdev->allow_unplug_during_migration = true;
21534f5b6a05SJens Freimann     }
21544f5b6a05SJens Freimann 
2155315a1350SMichael S. Tsirkin     /* rom loading */
2156315a1350SMichael S. Tsirkin     is_default_rom = false;
2157315a1350SMichael S. Tsirkin     if (pci_dev->romfile == NULL && pc->romfile != NULL) {
2158315a1350SMichael S. Tsirkin         pci_dev->romfile = g_strdup(pc->romfile);
2159315a1350SMichael S. Tsirkin         is_default_rom = true;
2160315a1350SMichael S. Tsirkin     }
2161178e785fSMarcel Apfelbaum 
2162133e9b22SMarkus Armbruster     pci_add_option_rom(pci_dev, is_default_rom, &local_err);
2163133e9b22SMarkus Armbruster     if (local_err) {
2164133e9b22SMarkus Armbruster         error_propagate(errp, local_err);
2165b69c3c21SMarkus Armbruster         pci_qdev_unrealize(DEVICE(pci_dev));
2166133e9b22SMarkus Armbruster         return;
2167178e785fSMarcel Apfelbaum     }
2168315a1350SMichael S. Tsirkin }
2169315a1350SMichael S. Tsirkin 
21707411aa63SMarkus Armbruster PCIDevice *pci_new_multifunction(int devfn, bool multifunction,
21717411aa63SMarkus Armbruster                                  const char *name)
21727411aa63SMarkus Armbruster {
21737411aa63SMarkus Armbruster     DeviceState *dev;
21747411aa63SMarkus Armbruster 
21757411aa63SMarkus Armbruster     dev = qdev_new(name);
21767411aa63SMarkus Armbruster     qdev_prop_set_int32(dev, "addr", devfn);
21777411aa63SMarkus Armbruster     qdev_prop_set_bit(dev, "multifunction", multifunction);
21787411aa63SMarkus Armbruster     return PCI_DEVICE(dev);
21797411aa63SMarkus Armbruster }
21807411aa63SMarkus Armbruster 
21817411aa63SMarkus Armbruster PCIDevice *pci_new(int devfn, const char *name)
21827411aa63SMarkus Armbruster {
21837411aa63SMarkus Armbruster     return pci_new_multifunction(devfn, false, name);
21847411aa63SMarkus Armbruster }
21857411aa63SMarkus Armbruster 
21867411aa63SMarkus Armbruster bool pci_realize_and_unref(PCIDevice *dev, PCIBus *bus, Error **errp)
21877411aa63SMarkus Armbruster {
21887411aa63SMarkus Armbruster     return qdev_realize_and_unref(&dev->qdev, &bus->qbus, errp);
21897411aa63SMarkus Armbruster }
21907411aa63SMarkus Armbruster 
2191315a1350SMichael S. Tsirkin PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
2192315a1350SMichael S. Tsirkin                                            bool multifunction,
2193315a1350SMichael S. Tsirkin                                            const char *name)
2194315a1350SMichael S. Tsirkin {
21959307d06dSMarkus Armbruster     PCIDevice *dev = pci_new_multifunction(devfn, multifunction, name);
21969307d06dSMarkus Armbruster     pci_realize_and_unref(dev, bus, &error_fatal);
2197315a1350SMichael S. Tsirkin     return dev;
2198315a1350SMichael S. Tsirkin }
2199315a1350SMichael S. Tsirkin 
2200315a1350SMichael S. Tsirkin PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
2201315a1350SMichael S. Tsirkin {
2202315a1350SMichael S. Tsirkin     return pci_create_simple_multifunction(bus, devfn, false, name);
2203315a1350SMichael S. Tsirkin }
2204315a1350SMichael S. Tsirkin 
2205315a1350SMichael S. Tsirkin static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
2206315a1350SMichael S. Tsirkin {
2207315a1350SMichael S. Tsirkin     int offset = PCI_CONFIG_HEADER_SIZE;
2208315a1350SMichael S. Tsirkin     int i;
2209315a1350SMichael S. Tsirkin     for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
2210315a1350SMichael S. Tsirkin         if (pdev->used[i])
2211315a1350SMichael S. Tsirkin             offset = i + 1;
2212315a1350SMichael S. Tsirkin         else if (i - offset + 1 == size)
2213315a1350SMichael S. Tsirkin             return offset;
2214315a1350SMichael S. Tsirkin     }
2215315a1350SMichael S. Tsirkin     return 0;
2216315a1350SMichael S. Tsirkin }
2217315a1350SMichael S. Tsirkin 
2218315a1350SMichael S. Tsirkin static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
2219315a1350SMichael S. Tsirkin                                         uint8_t *prev_p)
2220315a1350SMichael S. Tsirkin {
2221315a1350SMichael S. Tsirkin     uint8_t next, prev;
2222315a1350SMichael S. Tsirkin 
2223315a1350SMichael S. Tsirkin     if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
2224315a1350SMichael S. Tsirkin         return 0;
2225315a1350SMichael S. Tsirkin 
2226315a1350SMichael S. Tsirkin     for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
2227315a1350SMichael S. Tsirkin          prev = next + PCI_CAP_LIST_NEXT)
2228315a1350SMichael S. Tsirkin         if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
2229315a1350SMichael S. Tsirkin             break;
2230315a1350SMichael S. Tsirkin 
2231315a1350SMichael S. Tsirkin     if (prev_p)
2232315a1350SMichael S. Tsirkin         *prev_p = prev;
2233315a1350SMichael S. Tsirkin     return next;
2234315a1350SMichael S. Tsirkin }
2235315a1350SMichael S. Tsirkin 
2236315a1350SMichael S. Tsirkin static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
2237315a1350SMichael S. Tsirkin {
2238315a1350SMichael S. Tsirkin     uint8_t next, prev, found = 0;
2239315a1350SMichael S. Tsirkin 
2240315a1350SMichael S. Tsirkin     if (!(pdev->used[offset])) {
2241315a1350SMichael S. Tsirkin         return 0;
2242315a1350SMichael S. Tsirkin     }
2243315a1350SMichael S. Tsirkin 
2244315a1350SMichael S. Tsirkin     assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST);
2245315a1350SMichael S. Tsirkin 
2246315a1350SMichael S. Tsirkin     for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
2247315a1350SMichael S. Tsirkin          prev = next + PCI_CAP_LIST_NEXT) {
2248315a1350SMichael S. Tsirkin         if (next <= offset && next > found) {
2249315a1350SMichael S. Tsirkin             found = next;
2250315a1350SMichael S. Tsirkin         }
2251315a1350SMichael S. Tsirkin     }
2252315a1350SMichael S. Tsirkin     return found;
2253315a1350SMichael S. Tsirkin }
2254315a1350SMichael S. Tsirkin 
2255315a1350SMichael S. Tsirkin /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
2256315a1350SMichael S. Tsirkin    This is needed for an option rom which is used for more than one device. */
2257315a1350SMichael S. Tsirkin static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)
2258315a1350SMichael S. Tsirkin {
2259315a1350SMichael S. Tsirkin     uint16_t vendor_id;
2260315a1350SMichael S. Tsirkin     uint16_t device_id;
2261315a1350SMichael S. Tsirkin     uint16_t rom_vendor_id;
2262315a1350SMichael S. Tsirkin     uint16_t rom_device_id;
2263315a1350SMichael S. Tsirkin     uint16_t rom_magic;
2264315a1350SMichael S. Tsirkin     uint16_t pcir_offset;
2265315a1350SMichael S. Tsirkin     uint8_t checksum;
2266315a1350SMichael S. Tsirkin 
2267315a1350SMichael S. Tsirkin     /* Words in rom data are little endian (like in PCI configuration),
2268315a1350SMichael S. Tsirkin        so they can be read / written with pci_get_word / pci_set_word. */
2269315a1350SMichael S. Tsirkin 
2270315a1350SMichael S. Tsirkin     /* Only a valid rom will be patched. */
2271315a1350SMichael S. Tsirkin     rom_magic = pci_get_word(ptr);
2272315a1350SMichael S. Tsirkin     if (rom_magic != 0xaa55) {
2273315a1350SMichael S. Tsirkin         PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
2274315a1350SMichael S. Tsirkin         return;
2275315a1350SMichael S. Tsirkin     }
2276315a1350SMichael S. Tsirkin     pcir_offset = pci_get_word(ptr + 0x18);
2277315a1350SMichael S. Tsirkin     if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
2278315a1350SMichael S. Tsirkin         PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
2279315a1350SMichael S. Tsirkin         return;
2280315a1350SMichael S. Tsirkin     }
2281315a1350SMichael S. Tsirkin 
2282315a1350SMichael S. Tsirkin     vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
2283315a1350SMichael S. Tsirkin     device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
2284315a1350SMichael S. Tsirkin     rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
2285315a1350SMichael S. Tsirkin     rom_device_id = pci_get_word(ptr + pcir_offset + 6);
2286315a1350SMichael S. Tsirkin 
2287315a1350SMichael S. Tsirkin     PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
2288315a1350SMichael S. Tsirkin                 vendor_id, device_id, rom_vendor_id, rom_device_id);
2289315a1350SMichael S. Tsirkin 
2290315a1350SMichael S. Tsirkin     checksum = ptr[6];
2291315a1350SMichael S. Tsirkin 
2292315a1350SMichael S. Tsirkin     if (vendor_id != rom_vendor_id) {
2293315a1350SMichael S. Tsirkin         /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
2294315a1350SMichael S. Tsirkin         checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
2295315a1350SMichael S. Tsirkin         checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
2296315a1350SMichael S. Tsirkin         PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2297315a1350SMichael S. Tsirkin         ptr[6] = checksum;
2298315a1350SMichael S. Tsirkin         pci_set_word(ptr + pcir_offset + 4, vendor_id);
2299315a1350SMichael S. Tsirkin     }
2300315a1350SMichael S. Tsirkin 
2301315a1350SMichael S. Tsirkin     if (device_id != rom_device_id) {
2302315a1350SMichael S. Tsirkin         /* Patch device id and checksum (at offset 6 for etherboot roms). */
2303315a1350SMichael S. Tsirkin         checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
2304315a1350SMichael S. Tsirkin         checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
2305315a1350SMichael S. Tsirkin         PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2306315a1350SMichael S. Tsirkin         ptr[6] = checksum;
2307315a1350SMichael S. Tsirkin         pci_set_word(ptr + pcir_offset + 6, device_id);
2308315a1350SMichael S. Tsirkin     }
2309315a1350SMichael S. Tsirkin }
2310315a1350SMichael S. Tsirkin 
2311315a1350SMichael S. Tsirkin /* Add an option rom for the device */
2312133e9b22SMarkus Armbruster static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
2313133e9b22SMarkus Armbruster                                Error **errp)
2314315a1350SMichael S. Tsirkin {
2315315a1350SMichael S. Tsirkin     int size;
2316315a1350SMichael S. Tsirkin     char *path;
2317315a1350SMichael S. Tsirkin     void *ptr;
2318315a1350SMichael S. Tsirkin     char name[32];
2319315a1350SMichael S. Tsirkin     const VMStateDescription *vmsd;
2320315a1350SMichael S. Tsirkin 
2321315a1350SMichael S. Tsirkin     if (!pdev->romfile)
2322133e9b22SMarkus Armbruster         return;
2323315a1350SMichael S. Tsirkin     if (strlen(pdev->romfile) == 0)
2324133e9b22SMarkus Armbruster         return;
2325315a1350SMichael S. Tsirkin 
2326315a1350SMichael S. Tsirkin     if (!pdev->rom_bar) {
2327315a1350SMichael S. Tsirkin         /*
2328315a1350SMichael S. Tsirkin          * Load rom via fw_cfg instead of creating a rom bar,
2329315a1350SMichael S. Tsirkin          * for 0.11 compatibility.
2330315a1350SMichael S. Tsirkin          */
2331315a1350SMichael S. Tsirkin         int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
2332db80c7b9SMarcel Apfelbaum 
2333db80c7b9SMarcel Apfelbaum         /*
2334db80c7b9SMarcel Apfelbaum          * Hot-plugged devices can't use the option ROM
2335db80c7b9SMarcel Apfelbaum          * if the rom bar is disabled.
2336db80c7b9SMarcel Apfelbaum          */
2337db80c7b9SMarcel Apfelbaum         if (DEVICE(pdev)->hotplugged) {
2338133e9b22SMarkus Armbruster             error_setg(errp, "Hot-plugged device without ROM bar"
2339133e9b22SMarkus Armbruster                        " can't have an option ROM");
2340133e9b22SMarkus Armbruster             return;
2341db80c7b9SMarcel Apfelbaum         }
2342db80c7b9SMarcel Apfelbaum 
2343315a1350SMichael S. Tsirkin         if (class == 0x0300) {
2344315a1350SMichael S. Tsirkin             rom_add_vga(pdev->romfile);
2345315a1350SMichael S. Tsirkin         } else {
2346315a1350SMichael S. Tsirkin             rom_add_option(pdev->romfile, -1);
2347315a1350SMichael S. Tsirkin         }
2348133e9b22SMarkus Armbruster         return;
2349315a1350SMichael S. Tsirkin     }
2350315a1350SMichael S. Tsirkin 
2351315a1350SMichael S. Tsirkin     path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
2352315a1350SMichael S. Tsirkin     if (path == NULL) {
2353315a1350SMichael S. Tsirkin         path = g_strdup(pdev->romfile);
2354315a1350SMichael S. Tsirkin     }
2355315a1350SMichael S. Tsirkin 
2356315a1350SMichael S. Tsirkin     size = get_image_size(path);
2357315a1350SMichael S. Tsirkin     if (size < 0) {
2358133e9b22SMarkus Armbruster         error_setg(errp, "failed to find romfile \"%s\"", pdev->romfile);
23598c7f3dd0SStefan Hajnoczi         g_free(path);
2360133e9b22SMarkus Armbruster         return;
23618c7f3dd0SStefan Hajnoczi     } else if (size == 0) {
2362133e9b22SMarkus Armbruster         error_setg(errp, "romfile \"%s\" is empty", pdev->romfile);
2363315a1350SMichael S. Tsirkin         g_free(path);
2364133e9b22SMarkus Armbruster         return;
2365315a1350SMichael S. Tsirkin     }
23669bff5d81SPeter Maydell     size = pow2ceil(size);
2367315a1350SMichael S. Tsirkin 
2368315a1350SMichael S. Tsirkin     vmsd = qdev_get_vmsd(DEVICE(pdev));
2369315a1350SMichael S. Tsirkin 
2370315a1350SMichael S. Tsirkin     if (vmsd) {
2371315a1350SMichael S. Tsirkin         snprintf(name, sizeof(name), "%s.rom", vmsd->name);
2372315a1350SMichael S. Tsirkin     } else {
2373315a1350SMichael S. Tsirkin         snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev)));
2374315a1350SMichael S. Tsirkin     }
2375315a1350SMichael S. Tsirkin     pdev->has_rom = true;
2376fefa9256SPeter Maydell     memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, size, &error_fatal);
2377315a1350SMichael S. Tsirkin     ptr = memory_region_get_ram_ptr(&pdev->rom);
237836bde091SPeter Maydell     if (load_image_size(path, ptr, size) < 0) {
237936bde091SPeter Maydell         error_setg(errp, "failed to load romfile \"%s\"", pdev->romfile);
238036bde091SPeter Maydell         g_free(path);
238136bde091SPeter Maydell         return;
238236bde091SPeter Maydell     }
2383315a1350SMichael S. Tsirkin     g_free(path);
2384315a1350SMichael S. Tsirkin 
2385315a1350SMichael S. Tsirkin     if (is_default_rom) {
2386315a1350SMichael S. Tsirkin         /* Only the default rom images will be patched (if needed). */
2387315a1350SMichael S. Tsirkin         pci_patch_ids(pdev, ptr, size);
2388315a1350SMichael S. Tsirkin     }
2389315a1350SMichael S. Tsirkin 
2390315a1350SMichael S. Tsirkin     pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
2391315a1350SMichael S. Tsirkin }
2392315a1350SMichael S. Tsirkin 
2393315a1350SMichael S. Tsirkin static void pci_del_option_rom(PCIDevice *pdev)
2394315a1350SMichael S. Tsirkin {
2395315a1350SMichael S. Tsirkin     if (!pdev->has_rom)
2396315a1350SMichael S. Tsirkin         return;
2397315a1350SMichael S. Tsirkin 
2398315a1350SMichael S. Tsirkin     vmstate_unregister_ram(&pdev->rom, &pdev->qdev);
2399315a1350SMichael S. Tsirkin     pdev->has_rom = false;
2400315a1350SMichael S. Tsirkin }
2401315a1350SMichael S. Tsirkin 
2402315a1350SMichael S. Tsirkin /*
240327841278SMao Zhongyi  * On success, pci_add_capability() returns a positive value
2404eacbc632SMao Zhongyi  * that the offset of the pci capability.
2405eacbc632SMao Zhongyi  * On failure, it sets an error and returns a negative error
2406eacbc632SMao Zhongyi  * code.
2407eacbc632SMao Zhongyi  */
240827841278SMao Zhongyi int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
2409cd9aa33eSLaszlo Ersek                        uint8_t offset, uint8_t size,
2410cd9aa33eSLaszlo Ersek                        Error **errp)
2411cd9aa33eSLaszlo Ersek {
2412315a1350SMichael S. Tsirkin     uint8_t *config;
2413315a1350SMichael S. Tsirkin     int i, overlapping_cap;
2414315a1350SMichael S. Tsirkin 
2415315a1350SMichael S. Tsirkin     if (!offset) {
2416315a1350SMichael S. Tsirkin         offset = pci_find_space(pdev, size);
241797fe42f1SCao jin         /* out of PCI config space is programming error */
241897fe42f1SCao jin         assert(offset);
2419315a1350SMichael S. Tsirkin     } else {
2420315a1350SMichael S. Tsirkin         /* Verify that capabilities don't overlap.  Note: device assignment
2421315a1350SMichael S. Tsirkin          * depends on this check to verify that the device is not broken.
2422315a1350SMichael S. Tsirkin          * Should never trigger for emulated devices, but it's helpful
2423315a1350SMichael S. Tsirkin          * for debugging these. */
2424315a1350SMichael S. Tsirkin         for (i = offset; i < offset + size; i++) {
2425315a1350SMichael S. Tsirkin             overlapping_cap = pci_find_capability_at_offset(pdev, i);
2426315a1350SMichael S. Tsirkin             if (overlapping_cap) {
2427cd9aa33eSLaszlo Ersek                 error_setg(errp, "%s:%02x:%02x.%x "
2428315a1350SMichael S. Tsirkin                            "Attempt to add PCI capability %x at offset "
2429cd9aa33eSLaszlo Ersek                            "%x overlaps existing capability %x at offset %x",
2430fd56e061SDavid Gibson                            pci_root_bus_path(pdev), pci_dev_bus_num(pdev),
2431315a1350SMichael S. Tsirkin                            PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2432315a1350SMichael S. Tsirkin                            cap_id, offset, overlapping_cap, i);
2433315a1350SMichael S. Tsirkin                 return -EINVAL;
2434315a1350SMichael S. Tsirkin             }
2435315a1350SMichael S. Tsirkin         }
2436315a1350SMichael S. Tsirkin     }
2437315a1350SMichael S. Tsirkin 
2438315a1350SMichael S. Tsirkin     config = pdev->config + offset;
2439315a1350SMichael S. Tsirkin     config[PCI_CAP_LIST_ID] = cap_id;
2440315a1350SMichael S. Tsirkin     config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
2441315a1350SMichael S. Tsirkin     pdev->config[PCI_CAPABILITY_LIST] = offset;
2442315a1350SMichael S. Tsirkin     pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
2443315a1350SMichael S. Tsirkin     memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));
2444315a1350SMichael S. Tsirkin     /* Make capability read-only by default */
2445315a1350SMichael S. Tsirkin     memset(pdev->wmask + offset, 0, size);
2446315a1350SMichael S. Tsirkin     /* Check capability by default */
2447315a1350SMichael S. Tsirkin     memset(pdev->cmask + offset, 0xFF, size);
2448315a1350SMichael S. Tsirkin     return offset;
2449315a1350SMichael S. Tsirkin }
2450315a1350SMichael S. Tsirkin 
2451315a1350SMichael S. Tsirkin /* Unlink capability from the pci config space. */
2452315a1350SMichael S. Tsirkin void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
2453315a1350SMichael S. Tsirkin {
2454315a1350SMichael S. Tsirkin     uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
2455315a1350SMichael S. Tsirkin     if (!offset)
2456315a1350SMichael S. Tsirkin         return;
2457315a1350SMichael S. Tsirkin     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
2458315a1350SMichael S. Tsirkin     /* Make capability writable again */
2459315a1350SMichael S. Tsirkin     memset(pdev->wmask + offset, 0xff, size);
2460315a1350SMichael S. Tsirkin     memset(pdev->w1cmask + offset, 0, size);
2461315a1350SMichael S. Tsirkin     /* Clear cmask as device-specific registers can't be checked */
2462315a1350SMichael S. Tsirkin     memset(pdev->cmask + offset, 0, size);
2463315a1350SMichael S. Tsirkin     memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4));
2464315a1350SMichael S. Tsirkin 
2465315a1350SMichael S. Tsirkin     if (!pdev->config[PCI_CAPABILITY_LIST])
2466315a1350SMichael S. Tsirkin         pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
2467315a1350SMichael S. Tsirkin }
2468315a1350SMichael S. Tsirkin 
2469315a1350SMichael S. Tsirkin uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
2470315a1350SMichael S. Tsirkin {
2471315a1350SMichael S. Tsirkin     return pci_find_capability_list(pdev, cap_id, NULL);
2472315a1350SMichael S. Tsirkin }
2473315a1350SMichael S. Tsirkin 
2474315a1350SMichael S. Tsirkin static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
2475315a1350SMichael S. Tsirkin {
2476315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2477315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
2478315a1350SMichael S. Tsirkin     char ctxt[64];
2479315a1350SMichael S. Tsirkin     PCIIORegion *r;
2480315a1350SMichael S. Tsirkin     int i, class;
2481315a1350SMichael S. Tsirkin 
2482315a1350SMichael S. Tsirkin     class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2483315a1350SMichael S. Tsirkin     desc = pci_class_descriptions;
2484315a1350SMichael S. Tsirkin     while (desc->desc && class != desc->class)
2485315a1350SMichael S. Tsirkin         desc++;
2486315a1350SMichael S. Tsirkin     if (desc->desc) {
2487315a1350SMichael S. Tsirkin         snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
2488315a1350SMichael S. Tsirkin     } else {
2489315a1350SMichael S. Tsirkin         snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
2490315a1350SMichael S. Tsirkin     }
2491315a1350SMichael S. Tsirkin 
2492315a1350SMichael S. Tsirkin     monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
2493315a1350SMichael S. Tsirkin                    "pci id %04x:%04x (sub %04x:%04x)\n",
2494fd56e061SDavid Gibson                    indent, "", ctxt, pci_dev_bus_num(d),
2495315a1350SMichael S. Tsirkin                    PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
2496315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_VENDOR_ID),
2497315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_DEVICE_ID),
2498315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
2499315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_SUBSYSTEM_ID));
2500315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; i++) {
2501315a1350SMichael S. Tsirkin         r = &d->io_regions[i];
2502315a1350SMichael S. Tsirkin         if (!r->size)
2503315a1350SMichael S. Tsirkin             continue;
2504315a1350SMichael S. Tsirkin         monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
2505315a1350SMichael S. Tsirkin                        " [0x%"FMT_PCIBUS"]\n",
2506315a1350SMichael S. Tsirkin                        indent, "",
2507315a1350SMichael S. Tsirkin                        i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
2508315a1350SMichael S. Tsirkin                        r->addr, r->addr + r->size - 1);
2509315a1350SMichael S. Tsirkin     }
2510315a1350SMichael S. Tsirkin }
2511315a1350SMichael S. Tsirkin 
2512315a1350SMichael S. Tsirkin static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
2513315a1350SMichael S. Tsirkin {
2514315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2515315a1350SMichael S. Tsirkin     const char *name = NULL;
2516315a1350SMichael S. Tsirkin     const pci_class_desc *desc =  pci_class_descriptions;
2517315a1350SMichael S. Tsirkin     int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2518315a1350SMichael S. Tsirkin 
2519315a1350SMichael S. Tsirkin     while (desc->desc &&
2520315a1350SMichael S. Tsirkin           (class & ~desc->fw_ign_bits) !=
2521315a1350SMichael S. Tsirkin           (desc->class & ~desc->fw_ign_bits)) {
2522315a1350SMichael S. Tsirkin         desc++;
2523315a1350SMichael S. Tsirkin     }
2524315a1350SMichael S. Tsirkin 
2525315a1350SMichael S. Tsirkin     if (desc->desc) {
2526315a1350SMichael S. Tsirkin         name = desc->fw_name;
2527315a1350SMichael S. Tsirkin     }
2528315a1350SMichael S. Tsirkin 
2529315a1350SMichael S. Tsirkin     if (name) {
2530315a1350SMichael S. Tsirkin         pstrcpy(buf, len, name);
2531315a1350SMichael S. Tsirkin     } else {
2532315a1350SMichael S. Tsirkin         snprintf(buf, len, "pci%04x,%04x",
2533315a1350SMichael S. Tsirkin                  pci_get_word(d->config + PCI_VENDOR_ID),
2534315a1350SMichael S. Tsirkin                  pci_get_word(d->config + PCI_DEVICE_ID));
2535315a1350SMichael S. Tsirkin     }
2536315a1350SMichael S. Tsirkin 
2537315a1350SMichael S. Tsirkin     return buf;
2538315a1350SMichael S. Tsirkin }
2539315a1350SMichael S. Tsirkin 
2540315a1350SMichael S. Tsirkin static char *pcibus_get_fw_dev_path(DeviceState *dev)
2541315a1350SMichael S. Tsirkin {
2542315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2543315a1350SMichael S. Tsirkin     char path[50], name[33];
2544315a1350SMichael S. Tsirkin     int off;
2545315a1350SMichael S. Tsirkin 
2546315a1350SMichael S. Tsirkin     off = snprintf(path, sizeof(path), "%s@%x",
2547315a1350SMichael S. Tsirkin                    pci_dev_fw_name(dev, name, sizeof name),
2548315a1350SMichael S. Tsirkin                    PCI_SLOT(d->devfn));
2549315a1350SMichael S. Tsirkin     if (PCI_FUNC(d->devfn))
2550315a1350SMichael S. Tsirkin         snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
2551315a1350SMichael S. Tsirkin     return g_strdup(path);
2552315a1350SMichael S. Tsirkin }
2553315a1350SMichael S. Tsirkin 
2554315a1350SMichael S. Tsirkin static char *pcibus_get_dev_path(DeviceState *dev)
2555315a1350SMichael S. Tsirkin {
2556315a1350SMichael S. Tsirkin     PCIDevice *d = container_of(dev, PCIDevice, qdev);
2557315a1350SMichael S. Tsirkin     PCIDevice *t;
2558315a1350SMichael S. Tsirkin     int slot_depth;
2559315a1350SMichael S. Tsirkin     /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
2560315a1350SMichael S. Tsirkin      * 00 is added here to make this format compatible with
2561315a1350SMichael S. Tsirkin      * domain:Bus:Slot.Func for systems without nested PCI bridges.
2562315a1350SMichael S. Tsirkin      * Slot.Function list specifies the slot and function numbers for all
2563315a1350SMichael S. Tsirkin      * devices on the path from root to the specific device. */
2564568f0690SDavid Gibson     const char *root_bus_path;
2565568f0690SDavid Gibson     int root_bus_len;
2566315a1350SMichael S. Tsirkin     char slot[] = ":SS.F";
2567315a1350SMichael S. Tsirkin     int slot_len = sizeof slot - 1 /* For '\0' */;
2568315a1350SMichael S. Tsirkin     int path_len;
2569315a1350SMichael S. Tsirkin     char *path, *p;
2570315a1350SMichael S. Tsirkin     int s;
2571315a1350SMichael S. Tsirkin 
2572568f0690SDavid Gibson     root_bus_path = pci_root_bus_path(d);
2573568f0690SDavid Gibson     root_bus_len = strlen(root_bus_path);
2574568f0690SDavid Gibson 
2575315a1350SMichael S. Tsirkin     /* Calculate # of slots on path between device and root. */;
2576315a1350SMichael S. Tsirkin     slot_depth = 0;
2577fd56e061SDavid Gibson     for (t = d; t; t = pci_get_bus(t)->parent_dev) {
2578315a1350SMichael S. Tsirkin         ++slot_depth;
2579315a1350SMichael S. Tsirkin     }
2580315a1350SMichael S. Tsirkin 
2581568f0690SDavid Gibson     path_len = root_bus_len + slot_len * slot_depth;
2582315a1350SMichael S. Tsirkin 
2583315a1350SMichael S. Tsirkin     /* Allocate memory, fill in the terminating null byte. */
2584315a1350SMichael S. Tsirkin     path = g_malloc(path_len + 1 /* For '\0' */);
2585315a1350SMichael S. Tsirkin     path[path_len] = '\0';
2586315a1350SMichael S. Tsirkin 
2587568f0690SDavid Gibson     memcpy(path, root_bus_path, root_bus_len);
2588315a1350SMichael S. Tsirkin 
2589315a1350SMichael S. Tsirkin     /* Fill in slot numbers. We walk up from device to root, so need to print
2590315a1350SMichael S. Tsirkin      * them in the reverse order, last to first. */
2591315a1350SMichael S. Tsirkin     p = path + path_len;
2592fd56e061SDavid Gibson     for (t = d; t; t = pci_get_bus(t)->parent_dev) {
2593315a1350SMichael S. Tsirkin         p -= slot_len;
2594315a1350SMichael S. Tsirkin         s = snprintf(slot, sizeof slot, ":%02x.%x",
2595315a1350SMichael S. Tsirkin                      PCI_SLOT(t->devfn), PCI_FUNC(t->devfn));
2596315a1350SMichael S. Tsirkin         assert(s == slot_len);
2597315a1350SMichael S. Tsirkin         memcpy(p, slot, slot_len);
2598315a1350SMichael S. Tsirkin     }
2599315a1350SMichael S. Tsirkin 
2600315a1350SMichael S. Tsirkin     return path;
2601315a1350SMichael S. Tsirkin }
2602315a1350SMichael S. Tsirkin 
2603315a1350SMichael S. Tsirkin static int pci_qdev_find_recursive(PCIBus *bus,
2604315a1350SMichael S. Tsirkin                                    const char *id, PCIDevice **pdev)
2605315a1350SMichael S. Tsirkin {
2606315a1350SMichael S. Tsirkin     DeviceState *qdev = qdev_find_recursive(&bus->qbus, id);
2607315a1350SMichael S. Tsirkin     if (!qdev) {
2608315a1350SMichael S. Tsirkin         return -ENODEV;
2609315a1350SMichael S. Tsirkin     }
2610315a1350SMichael S. Tsirkin 
2611315a1350SMichael S. Tsirkin     /* roughly check if given qdev is pci device */
2612315a1350SMichael S. Tsirkin     if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) {
2613315a1350SMichael S. Tsirkin         *pdev = PCI_DEVICE(qdev);
2614315a1350SMichael S. Tsirkin         return 0;
2615315a1350SMichael S. Tsirkin     }
2616315a1350SMichael S. Tsirkin     return -EINVAL;
2617315a1350SMichael S. Tsirkin }
2618315a1350SMichael S. Tsirkin 
2619315a1350SMichael S. Tsirkin int pci_qdev_find_device(const char *id, PCIDevice **pdev)
2620315a1350SMichael S. Tsirkin {
26217588e2b0SDavid Gibson     PCIHostState *host_bridge;
2622315a1350SMichael S. Tsirkin     int rc = -ENODEV;
2623315a1350SMichael S. Tsirkin 
26247588e2b0SDavid Gibson     QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
26257588e2b0SDavid Gibson         int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev);
2626315a1350SMichael S. Tsirkin         if (!tmp) {
2627315a1350SMichael S. Tsirkin             rc = 0;
2628315a1350SMichael S. Tsirkin             break;
2629315a1350SMichael S. Tsirkin         }
2630315a1350SMichael S. Tsirkin         if (tmp != -ENODEV) {
2631315a1350SMichael S. Tsirkin             rc = tmp;
2632315a1350SMichael S. Tsirkin         }
2633315a1350SMichael S. Tsirkin     }
2634315a1350SMichael S. Tsirkin 
2635315a1350SMichael S. Tsirkin     return rc;
2636315a1350SMichael S. Tsirkin }
2637315a1350SMichael S. Tsirkin 
2638315a1350SMichael S. Tsirkin MemoryRegion *pci_address_space(PCIDevice *dev)
2639315a1350SMichael S. Tsirkin {
2640fd56e061SDavid Gibson     return pci_get_bus(dev)->address_space_mem;
2641315a1350SMichael S. Tsirkin }
2642315a1350SMichael S. Tsirkin 
2643315a1350SMichael S. Tsirkin MemoryRegion *pci_address_space_io(PCIDevice *dev)
2644315a1350SMichael S. Tsirkin {
2645fd56e061SDavid Gibson     return pci_get_bus(dev)->address_space_io;
2646315a1350SMichael S. Tsirkin }
2647315a1350SMichael S. Tsirkin 
2648315a1350SMichael S. Tsirkin static void pci_device_class_init(ObjectClass *klass, void *data)
2649315a1350SMichael S. Tsirkin {
2650315a1350SMichael S. Tsirkin     DeviceClass *k = DEVICE_CLASS(klass);
26517ee6c1e1SMarkus Armbruster 
2652133e9b22SMarkus Armbruster     k->realize = pci_qdev_realize;
2653133e9b22SMarkus Armbruster     k->unrealize = pci_qdev_unrealize;
2654315a1350SMichael S. Tsirkin     k->bus_type = TYPE_PCI_BUS;
26554f67d30bSMarc-André Lureau     device_class_set_props(k, pci_props);
2656315a1350SMichael S. Tsirkin }
2657315a1350SMichael S. Tsirkin 
26582fefa16cSEduardo Habkost static void pci_device_class_base_init(ObjectClass *klass, void *data)
26592fefa16cSEduardo Habkost {
26602fefa16cSEduardo Habkost     if (!object_class_is_abstract(klass)) {
26612fefa16cSEduardo Habkost         ObjectClass *conventional =
26622fefa16cSEduardo Habkost             object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE);
26632fefa16cSEduardo Habkost         ObjectClass *pcie =
26642fefa16cSEduardo Habkost             object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE);
26652fefa16cSEduardo Habkost         assert(conventional || pcie);
26662fefa16cSEduardo Habkost     }
26672fefa16cSEduardo Habkost }
26682fefa16cSEduardo Habkost 
26699eda7d37SAlexey Kardashevskiy AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
26709eda7d37SAlexey Kardashevskiy {
2671fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(dev);
26725af2ae23SBenjamin Herrenschmidt     PCIBus *iommu_bus = bus;
267377ef8f8dSAlex Williamson     uint8_t devfn = dev->devfn;
26749eda7d37SAlexey Kardashevskiy 
26755af2ae23SBenjamin Herrenschmidt     while (iommu_bus && !iommu_bus->iommu_fn && iommu_bus->parent_dev) {
267677ef8f8dSAlex Williamson         PCIBus *parent_bus = pci_get_bus(iommu_bus->parent_dev);
267777ef8f8dSAlex Williamson 
267877ef8f8dSAlex Williamson         /*
267977ef8f8dSAlex Williamson          * The requester ID of the provided device may be aliased, as seen from
268077ef8f8dSAlex Williamson          * the IOMMU, due to topology limitations.  The IOMMU relies on a
268177ef8f8dSAlex Williamson          * requester ID to provide a unique AddressSpace for devices, but
268277ef8f8dSAlex Williamson          * conventional PCI buses pre-date such concepts.  Instead, the PCIe-
268377ef8f8dSAlex Williamson          * to-PCI bridge creates and accepts transactions on behalf of down-
268477ef8f8dSAlex Williamson          * stream devices.  When doing so, all downstream devices are masked
268577ef8f8dSAlex Williamson          * (aliased) behind a single requester ID.  The requester ID used
268677ef8f8dSAlex Williamson          * depends on the format of the bridge devices.  Proper PCIe-to-PCI
268777ef8f8dSAlex Williamson          * bridges, with a PCIe capability indicating such, follow the
268877ef8f8dSAlex Williamson          * guidelines of chapter 2.3 of the PCIe-to-PCI/X bridge specification,
268977ef8f8dSAlex Williamson          * where the bridge uses the seconary bus as the bridge portion of the
269077ef8f8dSAlex Williamson          * requester ID and devfn of 00.0.  For other bridges, typically those
269177ef8f8dSAlex Williamson          * found on the root complex such as the dmi-to-pci-bridge, we follow
269277ef8f8dSAlex Williamson          * the convention of typical bare-metal hardware, which uses the
269377ef8f8dSAlex Williamson          * requester ID of the bridge itself.  There are device specific
269477ef8f8dSAlex Williamson          * exceptions to these rules, but these are the defaults that the
269577ef8f8dSAlex Williamson          * Linux kernel uses when determining DMA aliases itself and believed
269677ef8f8dSAlex Williamson          * to be true for the bare metal equivalents of the devices emulated
269777ef8f8dSAlex Williamson          * in QEMU.
269877ef8f8dSAlex Williamson          */
269977ef8f8dSAlex Williamson         if (!pci_bus_is_express(iommu_bus)) {
270077ef8f8dSAlex Williamson             PCIDevice *parent = iommu_bus->parent_dev;
270177ef8f8dSAlex Williamson 
270277ef8f8dSAlex Williamson             if (pci_is_express(parent) &&
270377ef8f8dSAlex Williamson                 pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) {
270477ef8f8dSAlex Williamson                 devfn = PCI_DEVFN(0, 0);
270577ef8f8dSAlex Williamson                 bus = iommu_bus;
270677ef8f8dSAlex Williamson             } else {
270777ef8f8dSAlex Williamson                 devfn = parent->devfn;
270877ef8f8dSAlex Williamson                 bus = parent_bus;
270977ef8f8dSAlex Williamson             }
271077ef8f8dSAlex Williamson         }
271177ef8f8dSAlex Williamson 
271277ef8f8dSAlex Williamson         iommu_bus = parent_bus;
27139eda7d37SAlexey Kardashevskiy     }
27145af2ae23SBenjamin Herrenschmidt     if (iommu_bus && iommu_bus->iommu_fn) {
271577ef8f8dSAlex Williamson         return iommu_bus->iommu_fn(bus, iommu_bus->iommu_opaque, devfn);
27169eda7d37SAlexey Kardashevskiy     }
27179eda7d37SAlexey Kardashevskiy     return &address_space_memory;
27189eda7d37SAlexey Kardashevskiy }
27199eda7d37SAlexey Kardashevskiy 
2720e00387d5SAvi Kivity void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
2721315a1350SMichael S. Tsirkin {
2722e00387d5SAvi Kivity     bus->iommu_fn = fn;
2723e00387d5SAvi Kivity     bus->iommu_opaque = opaque;
2724315a1350SMichael S. Tsirkin }
2725315a1350SMichael S. Tsirkin 
272643864069SMichael S. Tsirkin static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque)
272743864069SMichael S. Tsirkin {
272843864069SMichael S. Tsirkin     Range *range = opaque;
272943864069SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
273043864069SMichael S. Tsirkin     uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND);
273177d6f4eaSMichael S. Tsirkin     int i;
273243864069SMichael S. Tsirkin 
273343864069SMichael S. Tsirkin     if (!(cmd & PCI_COMMAND_MEMORY)) {
273443864069SMichael S. Tsirkin         return;
273543864069SMichael S. Tsirkin     }
273643864069SMichael S. Tsirkin 
273743864069SMichael S. Tsirkin     if (pc->is_bridge) {
273843864069SMichael S. Tsirkin         pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
273943864069SMichael S. Tsirkin         pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
274043864069SMichael S. Tsirkin 
274143864069SMichael S. Tsirkin         base = MAX(base, 0x1ULL << 32);
274243864069SMichael S. Tsirkin 
274343864069SMichael S. Tsirkin         if (limit >= base) {
274443864069SMichael S. Tsirkin             Range pref_range;
2745a0efbf16SMarkus Armbruster             range_set_bounds(&pref_range, base, limit);
274643864069SMichael S. Tsirkin             range_extend(range, &pref_range);
274743864069SMichael S. Tsirkin         }
274843864069SMichael S. Tsirkin     }
274977d6f4eaSMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; ++i) {
275077d6f4eaSMichael S. Tsirkin         PCIIORegion *r = &dev->io_regions[i];
2751a0efbf16SMarkus Armbruster         pcibus_t lob, upb;
275243864069SMichael S. Tsirkin         Range region_range;
275343864069SMichael S. Tsirkin 
275477d6f4eaSMichael S. Tsirkin         if (!r->size ||
275577d6f4eaSMichael S. Tsirkin             (r->type & PCI_BASE_ADDRESS_SPACE_IO) ||
275677d6f4eaSMichael S. Tsirkin             !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
275743864069SMichael S. Tsirkin             continue;
275843864069SMichael S. Tsirkin         }
275977d6f4eaSMichael S. Tsirkin 
2760a0efbf16SMarkus Armbruster         lob = pci_bar_address(dev, i, r->type, r->size);
2761a0efbf16SMarkus Armbruster         upb = lob + r->size - 1;
2762a0efbf16SMarkus Armbruster         if (lob == PCI_BAR_UNMAPPED) {
276377d6f4eaSMichael S. Tsirkin             continue;
276477d6f4eaSMichael S. Tsirkin         }
276543864069SMichael S. Tsirkin 
2766a0efbf16SMarkus Armbruster         lob = MAX(lob, 0x1ULL << 32);
276743864069SMichael S. Tsirkin 
2768a0efbf16SMarkus Armbruster         if (upb >= lob) {
2769a0efbf16SMarkus Armbruster             range_set_bounds(&region_range, lob, upb);
277043864069SMichael S. Tsirkin             range_extend(range, &region_range);
277143864069SMichael S. Tsirkin         }
277243864069SMichael S. Tsirkin     }
277343864069SMichael S. Tsirkin }
277443864069SMichael S. Tsirkin 
277543864069SMichael S. Tsirkin void pci_bus_get_w64_range(PCIBus *bus, Range *range)
277643864069SMichael S. Tsirkin {
2777a0efbf16SMarkus Armbruster     range_make_empty(range);
277843864069SMichael S. Tsirkin     pci_for_each_device_under_bus(bus, pci_dev_get_w64, range);
277943864069SMichael S. Tsirkin }
278043864069SMichael S. Tsirkin 
27813f1e1478SCao jin static bool pcie_has_upstream_port(PCIDevice *dev)
27823f1e1478SCao jin {
2783fd56e061SDavid Gibson     PCIDevice *parent_dev = pci_bridge_get_device(pci_get_bus(dev));
27843f1e1478SCao jin 
27853f1e1478SCao jin     /* Device associated with an upstream port.
27863f1e1478SCao jin      * As there are several types of these, it's easier to check the
27873f1e1478SCao jin      * parent device: upstream ports are always connected to
27883f1e1478SCao jin      * root or downstream ports.
27893f1e1478SCao jin      */
27903f1e1478SCao jin     return parent_dev &&
27913f1e1478SCao jin         pci_is_express(parent_dev) &&
27923f1e1478SCao jin         parent_dev->exp.exp_cap &&
27933f1e1478SCao jin         (pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_ROOT_PORT ||
27943f1e1478SCao jin          pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_DOWNSTREAM);
27953f1e1478SCao jin }
27963f1e1478SCao jin 
27973f1e1478SCao jin PCIDevice *pci_get_function_0(PCIDevice *pci_dev)
27983f1e1478SCao jin {
2799fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
2800fd56e061SDavid Gibson 
28013f1e1478SCao jin     if(pcie_has_upstream_port(pci_dev)) {
28023f1e1478SCao jin         /* With an upstream PCIe port, we only support 1 device at slot 0 */
2803fd56e061SDavid Gibson         return bus->devices[0];
28043f1e1478SCao jin     } else {
28053f1e1478SCao jin         /* Other bus types might support multiple devices at slots 0-31 */
2806fd56e061SDavid Gibson         return bus->devices[PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 0)];
28073f1e1478SCao jin     }
28083f1e1478SCao jin }
28093f1e1478SCao jin 
2810e1d4fb2dSPeter Xu MSIMessage pci_get_msi_message(PCIDevice *dev, int vector)
2811e1d4fb2dSPeter Xu {
2812e1d4fb2dSPeter Xu     MSIMessage msg;
2813e1d4fb2dSPeter Xu     if (msix_enabled(dev)) {
2814e1d4fb2dSPeter Xu         msg = msix_get_message(dev, vector);
2815e1d4fb2dSPeter Xu     } else if (msi_enabled(dev)) {
2816e1d4fb2dSPeter Xu         msg = msi_get_message(dev, vector);
2817e1d4fb2dSPeter Xu     } else {
2818e1d4fb2dSPeter Xu         /* Should never happen */
2819e1d4fb2dSPeter Xu         error_report("%s: unknown interrupt type", __func__);
2820e1d4fb2dSPeter Xu         abort();
2821e1d4fb2dSPeter Xu     }
2822e1d4fb2dSPeter Xu     return msg;
2823e1d4fb2dSPeter Xu }
2824e1d4fb2dSPeter Xu 
28258c43a6f0SAndreas Färber static const TypeInfo pci_device_type_info = {
2826315a1350SMichael S. Tsirkin     .name = TYPE_PCI_DEVICE,
2827315a1350SMichael S. Tsirkin     .parent = TYPE_DEVICE,
2828315a1350SMichael S. Tsirkin     .instance_size = sizeof(PCIDevice),
2829315a1350SMichael S. Tsirkin     .abstract = true,
2830315a1350SMichael S. Tsirkin     .class_size = sizeof(PCIDeviceClass),
2831315a1350SMichael S. Tsirkin     .class_init = pci_device_class_init,
28322fefa16cSEduardo Habkost     .class_base_init = pci_device_class_base_init,
2833315a1350SMichael S. Tsirkin };
2834315a1350SMichael S. Tsirkin 
2835315a1350SMichael S. Tsirkin static void pci_register_types(void)
2836315a1350SMichael S. Tsirkin {
2837315a1350SMichael S. Tsirkin     type_register_static(&pci_bus_info);
28383a861c46SAlex Williamson     type_register_static(&pcie_bus_info);
2839619f02aeSEduardo Habkost     type_register_static(&conventional_pci_interface_info);
2840619f02aeSEduardo Habkost     type_register_static(&pcie_interface_info);
2841315a1350SMichael S. Tsirkin     type_register_static(&pci_device_type_info);
2842315a1350SMichael S. Tsirkin }
2843315a1350SMichael S. Tsirkin 
2844315a1350SMichael S. Tsirkin type_init(pci_register_types)
2845