xref: /openbmc/qemu/hw/pci/pci.c (revision b0e5196a)
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"
26c759b24fSMichael S. Tsirkin #include "hw/hw.h"
27c759b24fSMichael S. Tsirkin #include "hw/pci/pci.h"
28c759b24fSMichael S. Tsirkin #include "hw/pci/pci_bridge.h"
2906aac7bdSMichael S. Tsirkin #include "hw/pci/pci_bus.h"
30568f0690SDavid Gibson #include "hw/pci/pci_host.h"
3183c9089eSPaolo Bonzini #include "monitor/monitor.h"
321422e32dSPaolo Bonzini #include "net/net.h"
339c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
34c759b24fSMichael S. Tsirkin #include "hw/loader.h"
35d49b6836SMarkus Armbruster #include "qemu/error-report.h"
361de7afc9SPaolo Bonzini #include "qemu/range.h"
377828d750SDon Koch #include "trace.h"
38c759b24fSMichael S. Tsirkin #include "hw/pci/msi.h"
39c759b24fSMichael S. Tsirkin #include "hw/pci/msix.h"
40022c62cbSPaolo Bonzini #include "exec/address-spaces.h"
415e954943SIgor Mammedov #include "hw/hotplug.h"
42e4024630SLaurent Vivier #include "hw/boards.h"
43e688df6bSMarkus Armbruster #include "qapi/error.h"
44112ed241SMarkus Armbruster #include "qapi/qapi-commands-misc.h"
45f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
46315a1350SMichael S. Tsirkin 
47315a1350SMichael S. Tsirkin //#define DEBUG_PCI
48315a1350SMichael S. Tsirkin #ifdef DEBUG_PCI
49315a1350SMichael S. Tsirkin # define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
50315a1350SMichael S. Tsirkin #else
51315a1350SMichael S. Tsirkin # define PCI_DPRINTF(format, ...)       do { } while (0)
52315a1350SMichael S. Tsirkin #endif
53315a1350SMichael S. Tsirkin 
5488c725c7SCornelia Huck bool pci_available = true;
5588c725c7SCornelia Huck 
56315a1350SMichael S. Tsirkin static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
57315a1350SMichael S. Tsirkin static char *pcibus_get_dev_path(DeviceState *dev);
58315a1350SMichael S. Tsirkin static char *pcibus_get_fw_dev_path(DeviceState *dev);
59dcc20931SPaolo Bonzini static void pcibus_reset(BusState *qbus);
60315a1350SMichael S. Tsirkin 
61315a1350SMichael S. Tsirkin static Property pci_props[] = {
62315a1350SMichael S. Tsirkin     DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
63315a1350SMichael S. Tsirkin     DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
64315a1350SMichael S. Tsirkin     DEFINE_PROP_UINT32("rombar",  PCIDevice, rom_bar, 1),
65315a1350SMichael S. Tsirkin     DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
66315a1350SMichael S. Tsirkin                     QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
67315a1350SMichael S. Tsirkin     DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present,
68315a1350SMichael S. Tsirkin                     QEMU_PCI_CAP_SERR_BITNR, true),
696b449540SMichael S. Tsirkin     DEFINE_PROP_BIT("x-pcie-lnksta-dllla", PCIDevice, cap_present,
706b449540SMichael S. Tsirkin                     QEMU_PCIE_LNKSTA_DLLLA_BITNR, true),
71f03d8ea3SMarcel Apfelbaum     DEFINE_PROP_BIT("x-pcie-extcap-init", PCIDevice, cap_present,
72f03d8ea3SMarcel Apfelbaum                     QEMU_PCIE_EXTCAP_INIT_BITNR, true),
73315a1350SMichael S. Tsirkin     DEFINE_PROP_END_OF_LIST()
74315a1350SMichael S. Tsirkin };
75315a1350SMichael S. Tsirkin 
76d2f69df7SBandan Das static const VMStateDescription vmstate_pcibus = {
77d2f69df7SBandan Das     .name = "PCIBUS",
78d2f69df7SBandan Das     .version_id = 1,
79d2f69df7SBandan Das     .minimum_version_id = 1,
80d2f69df7SBandan Das     .fields = (VMStateField[]) {
81d2164ad3SHalil Pasic         VMSTATE_INT32_EQUAL(nirq, PCIBus, NULL),
82d2f69df7SBandan Das         VMSTATE_VARRAY_INT32(irq_count, PCIBus,
83d2f69df7SBandan Das                              nirq, 0, vmstate_info_int32,
84d2f69df7SBandan Das                              int32_t),
85d2f69df7SBandan Das         VMSTATE_END_OF_LIST()
86d2f69df7SBandan Das     }
87d2f69df7SBandan Das };
88d2f69df7SBandan Das 
89b86eacb8SMarcel Apfelbaum static void pci_init_bus_master(PCIDevice *pci_dev)
90b86eacb8SMarcel Apfelbaum {
91b86eacb8SMarcel Apfelbaum     AddressSpace *dma_as = pci_device_iommu_address_space(pci_dev);
92b86eacb8SMarcel Apfelbaum 
93b86eacb8SMarcel Apfelbaum     memory_region_init_alias(&pci_dev->bus_master_enable_region,
94b86eacb8SMarcel Apfelbaum                              OBJECT(pci_dev), "bus master",
95b86eacb8SMarcel Apfelbaum                              dma_as->root, 0, memory_region_size(dma_as->root));
96b86eacb8SMarcel Apfelbaum     memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
973716d590SJason Wang     memory_region_add_subregion(&pci_dev->bus_master_container_region, 0,
983716d590SJason Wang                                 &pci_dev->bus_master_enable_region);
99b86eacb8SMarcel Apfelbaum }
100b86eacb8SMarcel Apfelbaum 
101b86eacb8SMarcel Apfelbaum static void pcibus_machine_done(Notifier *notifier, void *data)
102b86eacb8SMarcel Apfelbaum {
103b86eacb8SMarcel Apfelbaum     PCIBus *bus = container_of(notifier, PCIBus, machine_done);
104b86eacb8SMarcel Apfelbaum     int i;
105b86eacb8SMarcel Apfelbaum 
106b86eacb8SMarcel Apfelbaum     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
107b86eacb8SMarcel Apfelbaum         if (bus->devices[i]) {
108b86eacb8SMarcel Apfelbaum             pci_init_bus_master(bus->devices[i]);
109b86eacb8SMarcel Apfelbaum         }
110b86eacb8SMarcel Apfelbaum     }
111b86eacb8SMarcel Apfelbaum }
112b86eacb8SMarcel Apfelbaum 
113d2f69df7SBandan Das static void pci_bus_realize(BusState *qbus, Error **errp)
114d2f69df7SBandan Das {
115d2f69df7SBandan Das     PCIBus *bus = PCI_BUS(qbus);
116d2f69df7SBandan Das 
117b86eacb8SMarcel Apfelbaum     bus->machine_done.notify = pcibus_machine_done;
118b86eacb8SMarcel Apfelbaum     qemu_add_machine_init_done_notifier(&bus->machine_done);
119b86eacb8SMarcel Apfelbaum 
120d2f69df7SBandan Das     vmstate_register(NULL, -1, &vmstate_pcibus, bus);
121d2f69df7SBandan Das }
122d2f69df7SBandan Das 
123d2f69df7SBandan Das static void pci_bus_unrealize(BusState *qbus, Error **errp)
124d2f69df7SBandan Das {
125d2f69df7SBandan Das     PCIBus *bus = PCI_BUS(qbus);
126d2f69df7SBandan Das 
127b86eacb8SMarcel Apfelbaum     qemu_remove_machine_init_done_notifier(&bus->machine_done);
128b86eacb8SMarcel Apfelbaum 
129d2f69df7SBandan Das     vmstate_unregister(NULL, &vmstate_pcibus, bus);
130d2f69df7SBandan Das }
131d2f69df7SBandan Das 
132602141d9SMarcel Apfelbaum static int pcibus_num(PCIBus *bus)
133602141d9SMarcel Apfelbaum {
134*b0e5196aSDavid Gibson     if (pci_bus_is_root(bus)) {
135602141d9SMarcel Apfelbaum         return 0; /* pci host bridge */
136602141d9SMarcel Apfelbaum     }
137602141d9SMarcel Apfelbaum     return bus->parent_dev->config[PCI_SECONDARY_BUS];
138602141d9SMarcel Apfelbaum }
139602141d9SMarcel Apfelbaum 
1406a3042b2SMarcel Apfelbaum static uint16_t pcibus_numa_node(PCIBus *bus)
1416a3042b2SMarcel Apfelbaum {
1426a3042b2SMarcel Apfelbaum     return NUMA_NODE_UNASSIGNED;
1436a3042b2SMarcel Apfelbaum }
1446a3042b2SMarcel Apfelbaum 
1451c685a90SGreg Kurz static bool pcibus_allows_extended_config_space(PCIBus *bus)
1461c685a90SGreg Kurz {
1471c685a90SGreg Kurz     return false;
1481c685a90SGreg Kurz }
1491c685a90SGreg Kurz 
150315a1350SMichael S. Tsirkin static void pci_bus_class_init(ObjectClass *klass, void *data)
151315a1350SMichael S. Tsirkin {
152315a1350SMichael S. Tsirkin     BusClass *k = BUS_CLASS(klass);
153ce6a28eeSMarcel Apfelbaum     PCIBusClass *pbc = PCI_BUS_CLASS(klass);
154315a1350SMichael S. Tsirkin 
155315a1350SMichael S. Tsirkin     k->print_dev = pcibus_dev_print;
156315a1350SMichael S. Tsirkin     k->get_dev_path = pcibus_get_dev_path;
157315a1350SMichael S. Tsirkin     k->get_fw_dev_path = pcibus_get_fw_dev_path;
158d2f69df7SBandan Das     k->realize = pci_bus_realize;
159d2f69df7SBandan Das     k->unrealize = pci_bus_unrealize;
160315a1350SMichael S. Tsirkin     k->reset = pcibus_reset;
161ce6a28eeSMarcel Apfelbaum 
162602141d9SMarcel Apfelbaum     pbc->bus_num = pcibus_num;
1636a3042b2SMarcel Apfelbaum     pbc->numa_node = pcibus_numa_node;
1641c685a90SGreg Kurz     pbc->allows_extended_config_space = pcibus_allows_extended_config_space;
165315a1350SMichael S. Tsirkin }
166315a1350SMichael S. Tsirkin 
167315a1350SMichael S. Tsirkin static const TypeInfo pci_bus_info = {
168315a1350SMichael S. Tsirkin     .name = TYPE_PCI_BUS,
169315a1350SMichael S. Tsirkin     .parent = TYPE_BUS,
170315a1350SMichael S. Tsirkin     .instance_size = sizeof(PCIBus),
171ce6a28eeSMarcel Apfelbaum     .class_size = sizeof(PCIBusClass),
172315a1350SMichael S. Tsirkin     .class_init = pci_bus_class_init,
173315a1350SMichael S. Tsirkin };
174315a1350SMichael S. Tsirkin 
175619f02aeSEduardo Habkost static const TypeInfo pcie_interface_info = {
176619f02aeSEduardo Habkost     .name          = INTERFACE_PCIE_DEVICE,
177619f02aeSEduardo Habkost     .parent        = TYPE_INTERFACE,
178619f02aeSEduardo Habkost };
179619f02aeSEduardo Habkost 
180619f02aeSEduardo Habkost static const TypeInfo conventional_pci_interface_info = {
181619f02aeSEduardo Habkost     .name          = INTERFACE_CONVENTIONAL_PCI_DEVICE,
182619f02aeSEduardo Habkost     .parent        = TYPE_INTERFACE,
183619f02aeSEduardo Habkost };
184619f02aeSEduardo Habkost 
1851c685a90SGreg Kurz static bool pciebus_allows_extended_config_space(PCIBus *bus)
1861c685a90SGreg Kurz {
1871c685a90SGreg Kurz     return true;
1881c685a90SGreg Kurz }
1891c685a90SGreg Kurz 
1901c685a90SGreg Kurz static void pcie_bus_class_init(ObjectClass *klass, void *data)
1911c685a90SGreg Kurz {
1921c685a90SGreg Kurz     PCIBusClass *pbc = PCI_BUS_CLASS(klass);
1931c685a90SGreg Kurz 
1941c685a90SGreg Kurz     pbc->allows_extended_config_space = pciebus_allows_extended_config_space;
1951c685a90SGreg Kurz }
1961c685a90SGreg Kurz 
1973a861c46SAlex Williamson static const TypeInfo pcie_bus_info = {
1983a861c46SAlex Williamson     .name = TYPE_PCIE_BUS,
1993a861c46SAlex Williamson     .parent = TYPE_PCI_BUS,
2001c685a90SGreg Kurz     .class_init = pcie_bus_class_init,
2013a861c46SAlex Williamson };
2023a861c46SAlex Williamson 
203315a1350SMichael S. Tsirkin static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
204315a1350SMichael S. Tsirkin static void pci_update_mappings(PCIDevice *d);
205d98f08f5SMarcel Apfelbaum static void pci_irq_handler(void *opaque, int irq_num, int level);
206133e9b22SMarkus Armbruster static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, Error **);
207315a1350SMichael S. Tsirkin static void pci_del_option_rom(PCIDevice *pdev);
208315a1350SMichael S. Tsirkin 
209315a1350SMichael S. Tsirkin static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
210315a1350SMichael S. Tsirkin static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
211315a1350SMichael S. Tsirkin 
2127588e2b0SDavid Gibson static QLIST_HEAD(, PCIHostState) pci_host_bridges;
213315a1350SMichael S. Tsirkin 
214cf8c704dSMichael Roth int pci_bar(PCIDevice *d, int reg)
215315a1350SMichael S. Tsirkin {
216315a1350SMichael S. Tsirkin     uint8_t type;
217315a1350SMichael S. Tsirkin 
218315a1350SMichael S. Tsirkin     if (reg != PCI_ROM_SLOT)
219315a1350SMichael S. Tsirkin         return PCI_BASE_ADDRESS_0 + reg * 4;
220315a1350SMichael S. Tsirkin 
221315a1350SMichael S. Tsirkin     type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
222315a1350SMichael S. Tsirkin     return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
223315a1350SMichael S. Tsirkin }
224315a1350SMichael S. Tsirkin 
225315a1350SMichael S. Tsirkin static inline int pci_irq_state(PCIDevice *d, int irq_num)
226315a1350SMichael S. Tsirkin {
227315a1350SMichael S. Tsirkin         return (d->irq_state >> irq_num) & 0x1;
228315a1350SMichael S. Tsirkin }
229315a1350SMichael S. Tsirkin 
230315a1350SMichael S. Tsirkin static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
231315a1350SMichael S. Tsirkin {
232315a1350SMichael S. Tsirkin         d->irq_state &= ~(0x1 << irq_num);
233315a1350SMichael S. Tsirkin         d->irq_state |= level << irq_num;
234315a1350SMichael S. Tsirkin }
235315a1350SMichael S. Tsirkin 
236315a1350SMichael S. Tsirkin static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
237315a1350SMichael S. Tsirkin {
238315a1350SMichael S. Tsirkin     PCIBus *bus;
239315a1350SMichael S. Tsirkin     for (;;) {
240fd56e061SDavid Gibson         bus = pci_get_bus(pci_dev);
241315a1350SMichael S. Tsirkin         irq_num = bus->map_irq(pci_dev, irq_num);
242315a1350SMichael S. Tsirkin         if (bus->set_irq)
243315a1350SMichael S. Tsirkin             break;
244315a1350SMichael S. Tsirkin         pci_dev = bus->parent_dev;
245315a1350SMichael S. Tsirkin     }
246315a1350SMichael S. Tsirkin     bus->irq_count[irq_num] += change;
247315a1350SMichael S. Tsirkin     bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
248315a1350SMichael S. Tsirkin }
249315a1350SMichael S. Tsirkin 
250315a1350SMichael S. Tsirkin int pci_bus_get_irq_level(PCIBus *bus, int irq_num)
251315a1350SMichael S. Tsirkin {
252315a1350SMichael S. Tsirkin     assert(irq_num >= 0);
253315a1350SMichael S. Tsirkin     assert(irq_num < bus->nirq);
254315a1350SMichael S. Tsirkin     return !!bus->irq_count[irq_num];
255315a1350SMichael S. Tsirkin }
256315a1350SMichael S. Tsirkin 
257315a1350SMichael S. Tsirkin /* Update interrupt status bit in config space on interrupt
258315a1350SMichael S. Tsirkin  * state change. */
259315a1350SMichael S. Tsirkin static void pci_update_irq_status(PCIDevice *dev)
260315a1350SMichael S. Tsirkin {
261315a1350SMichael S. Tsirkin     if (dev->irq_state) {
262315a1350SMichael S. Tsirkin         dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
263315a1350SMichael S. Tsirkin     } else {
264315a1350SMichael S. Tsirkin         dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
265315a1350SMichael S. Tsirkin     }
266315a1350SMichael S. Tsirkin }
267315a1350SMichael S. Tsirkin 
268315a1350SMichael S. Tsirkin void pci_device_deassert_intx(PCIDevice *dev)
269315a1350SMichael S. Tsirkin {
270315a1350SMichael S. Tsirkin     int i;
271315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
272d98f08f5SMarcel Apfelbaum         pci_irq_handler(dev, i, 0);
273315a1350SMichael S. Tsirkin     }
274315a1350SMichael S. Tsirkin }
275315a1350SMichael S. Tsirkin 
276dcc20931SPaolo Bonzini static void pci_do_device_reset(PCIDevice *dev)
277315a1350SMichael S. Tsirkin {
278315a1350SMichael S. Tsirkin     int r;
279315a1350SMichael S. Tsirkin 
280315a1350SMichael S. Tsirkin     pci_device_deassert_intx(dev);
28158b59014SCole Robinson     assert(dev->irq_state == 0);
28258b59014SCole Robinson 
283315a1350SMichael S. Tsirkin     /* Clear all writable bits */
284315a1350SMichael S. Tsirkin     pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
285315a1350SMichael S. Tsirkin                                  pci_get_word(dev->wmask + PCI_COMMAND) |
286315a1350SMichael S. Tsirkin                                  pci_get_word(dev->w1cmask + PCI_COMMAND));
287315a1350SMichael S. Tsirkin     pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
288315a1350SMichael S. Tsirkin                                  pci_get_word(dev->wmask + PCI_STATUS) |
289315a1350SMichael S. Tsirkin                                  pci_get_word(dev->w1cmask + PCI_STATUS));
290315a1350SMichael S. Tsirkin     dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
291315a1350SMichael S. Tsirkin     dev->config[PCI_INTERRUPT_LINE] = 0x0;
292315a1350SMichael S. Tsirkin     for (r = 0; r < PCI_NUM_REGIONS; ++r) {
293315a1350SMichael S. Tsirkin         PCIIORegion *region = &dev->io_regions[r];
294315a1350SMichael S. Tsirkin         if (!region->size) {
295315a1350SMichael S. Tsirkin             continue;
296315a1350SMichael S. Tsirkin         }
297315a1350SMichael S. Tsirkin 
298315a1350SMichael S. Tsirkin         if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
299315a1350SMichael S. Tsirkin             region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
300315a1350SMichael S. Tsirkin             pci_set_quad(dev->config + pci_bar(dev, r), region->type);
301315a1350SMichael S. Tsirkin         } else {
302315a1350SMichael S. Tsirkin             pci_set_long(dev->config + pci_bar(dev, r), region->type);
303315a1350SMichael S. Tsirkin         }
304315a1350SMichael S. Tsirkin     }
305315a1350SMichael S. Tsirkin     pci_update_mappings(dev);
306315a1350SMichael S. Tsirkin 
307315a1350SMichael S. Tsirkin     msi_reset(dev);
308315a1350SMichael S. Tsirkin     msix_reset(dev);
309315a1350SMichael S. Tsirkin }
310315a1350SMichael S. Tsirkin 
311315a1350SMichael S. Tsirkin /*
312dcc20931SPaolo Bonzini  * This function is called on #RST and FLR.
313dcc20931SPaolo Bonzini  * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
314315a1350SMichael S. Tsirkin  */
315dcc20931SPaolo Bonzini void pci_device_reset(PCIDevice *dev)
316dcc20931SPaolo Bonzini {
317dcc20931SPaolo Bonzini     qdev_reset_all(&dev->qdev);
318dcc20931SPaolo Bonzini     pci_do_device_reset(dev);
319dcc20931SPaolo Bonzini }
320dcc20931SPaolo Bonzini 
321dcc20931SPaolo Bonzini /*
322dcc20931SPaolo Bonzini  * Trigger pci bus reset under a given bus.
323dcc20931SPaolo Bonzini  * Called via qbus_reset_all on RST# assert, after the devices
324dcc20931SPaolo Bonzini  * have been reset qdev_reset_all-ed already.
325dcc20931SPaolo Bonzini  */
326dcc20931SPaolo Bonzini static void pcibus_reset(BusState *qbus)
327315a1350SMichael S. Tsirkin {
32881e3e75bSPaolo Bonzini     PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
329315a1350SMichael S. Tsirkin     int i;
330315a1350SMichael S. Tsirkin 
331315a1350SMichael S. Tsirkin     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
332315a1350SMichael S. Tsirkin         if (bus->devices[i]) {
333dcc20931SPaolo Bonzini             pci_do_device_reset(bus->devices[i]);
334315a1350SMichael S. Tsirkin         }
335315a1350SMichael S. Tsirkin     }
336315a1350SMichael S. Tsirkin 
3379bdbbfc3SPaolo Bonzini     for (i = 0; i < bus->nirq; i++) {
3389bdbbfc3SPaolo Bonzini         assert(bus->irq_count[i] == 0);
3399bdbbfc3SPaolo Bonzini     }
340315a1350SMichael S. Tsirkin }
341315a1350SMichael S. Tsirkin 
3423dbc01aeSCao jin static void pci_host_bus_register(DeviceState *host)
343315a1350SMichael S. Tsirkin {
3443dbc01aeSCao jin     PCIHostState *host_bridge = PCI_HOST_BRIDGE(host);
3457588e2b0SDavid Gibson 
3467588e2b0SDavid Gibson     QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next);
347315a1350SMichael S. Tsirkin }
348315a1350SMichael S. Tsirkin 
349c13ee169SMichael Roth static void pci_host_bus_unregister(DeviceState *host)
350c13ee169SMichael Roth {
351c13ee169SMichael Roth     PCIHostState *host_bridge = PCI_HOST_BRIDGE(host);
352c13ee169SMichael Roth 
353c13ee169SMichael Roth     QLIST_REMOVE(host_bridge, next);
354c13ee169SMichael Roth }
355c13ee169SMichael Roth 
356c473d18dSDavid Gibson PCIBus *pci_device_root_bus(const PCIDevice *d)
357315a1350SMichael S. Tsirkin {
358fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(d);
359315a1350SMichael S. Tsirkin 
360ce6a28eeSMarcel Apfelbaum     while (!pci_bus_is_root(bus)) {
361ce6a28eeSMarcel Apfelbaum         d = bus->parent_dev;
362ce6a28eeSMarcel Apfelbaum         assert(d != NULL);
363ce6a28eeSMarcel Apfelbaum 
364fd56e061SDavid Gibson         bus = pci_get_bus(d);
365315a1350SMichael S. Tsirkin     }
366315a1350SMichael S. Tsirkin 
367c473d18dSDavid Gibson     return bus;
368315a1350SMichael S. Tsirkin }
369315a1350SMichael S. Tsirkin 
370568f0690SDavid Gibson const char *pci_root_bus_path(PCIDevice *dev)
371c473d18dSDavid Gibson {
372568f0690SDavid Gibson     PCIBus *rootbus = pci_device_root_bus(dev);
373568f0690SDavid Gibson     PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
374568f0690SDavid Gibson     PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge);
375c473d18dSDavid Gibson 
376568f0690SDavid Gibson     assert(host_bridge->bus == rootbus);
377568f0690SDavid Gibson 
378568f0690SDavid Gibson     if (hc->root_bus_path) {
379568f0690SDavid Gibson         return (*hc->root_bus_path)(host_bridge, rootbus);
380315a1350SMichael S. Tsirkin     }
381315a1350SMichael S. Tsirkin 
382568f0690SDavid Gibson     return rootbus->qbus.name;
383315a1350SMichael S. Tsirkin }
384315a1350SMichael S. Tsirkin 
3851115ff6dSDavid Gibson static void pci_root_bus_init(PCIBus *bus, DeviceState *parent,
386315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_mem,
387315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_io,
388315a1350SMichael S. Tsirkin                               uint8_t devfn_min)
389315a1350SMichael S. Tsirkin {
390315a1350SMichael S. Tsirkin     assert(PCI_FUNC(devfn_min) == 0);
391315a1350SMichael S. Tsirkin     bus->devfn_min = devfn_min;
3928b884984SMark Cave-Ayland     bus->slot_reserved_mask = 0x0;
393315a1350SMichael S. Tsirkin     bus->address_space_mem = address_space_mem;
394315a1350SMichael S. Tsirkin     bus->address_space_io = address_space_io;
395*b0e5196aSDavid Gibson     bus->flags |= PCI_BUS_IS_ROOT;
396315a1350SMichael S. Tsirkin 
397315a1350SMichael S. Tsirkin     /* host bridge */
398315a1350SMichael S. Tsirkin     QLIST_INIT(&bus->child);
3992b8cc89aSDavid Gibson 
4003dbc01aeSCao jin     pci_host_bus_register(parent);
401315a1350SMichael S. Tsirkin }
402315a1350SMichael S. Tsirkin 
403c13ee169SMichael Roth static void pci_bus_uninit(PCIBus *bus)
404c13ee169SMichael Roth {
405c13ee169SMichael Roth     pci_host_bus_unregister(BUS(bus)->parent);
406c13ee169SMichael Roth }
407c13ee169SMichael Roth 
4088c0bf9e2SAlex Williamson bool pci_bus_is_express(PCIBus *bus)
4098c0bf9e2SAlex Williamson {
4108c0bf9e2SAlex Williamson     return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
4118c0bf9e2SAlex Williamson }
4128c0bf9e2SAlex Williamson 
4131c685a90SGreg Kurz bool pci_bus_allows_extended_config_space(PCIBus *bus)
4141c685a90SGreg Kurz {
4151c685a90SGreg Kurz     return PCI_BUS_GET_CLASS(bus)->allows_extended_config_space(bus);
4161c685a90SGreg Kurz }
4171c685a90SGreg Kurz 
4181115ff6dSDavid Gibson void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent,
4194fec6404SPaolo Bonzini                               const char *name,
4204fec6404SPaolo Bonzini                               MemoryRegion *address_space_mem,
4214fec6404SPaolo Bonzini                               MemoryRegion *address_space_io,
42260a0e443SAlex Williamson                               uint8_t devfn_min, const char *typename)
4234fec6404SPaolo Bonzini {
424fb17dfe0SAndreas Färber     qbus_create_inplace(bus, bus_size, typename, parent, name);
4251115ff6dSDavid Gibson     pci_root_bus_init(bus, parent, address_space_mem, address_space_io,
4261115ff6dSDavid Gibson                       devfn_min);
4274fec6404SPaolo Bonzini }
4284fec6404SPaolo Bonzini 
4291115ff6dSDavid Gibson PCIBus *pci_root_bus_new(DeviceState *parent, const char *name,
430315a1350SMichael S. Tsirkin                          MemoryRegion *address_space_mem,
431315a1350SMichael S. Tsirkin                          MemoryRegion *address_space_io,
43260a0e443SAlex Williamson                          uint8_t devfn_min, const char *typename)
433315a1350SMichael S. Tsirkin {
434315a1350SMichael S. Tsirkin     PCIBus *bus;
435315a1350SMichael S. Tsirkin 
43660a0e443SAlex Williamson     bus = PCI_BUS(qbus_create(typename, parent, name));
4371115ff6dSDavid Gibson     pci_root_bus_init(bus, parent, address_space_mem, address_space_io,
4381115ff6dSDavid Gibson                       devfn_min);
439315a1350SMichael S. Tsirkin     return bus;
440315a1350SMichael S. Tsirkin }
441315a1350SMichael S. Tsirkin 
442c13ee169SMichael Roth void pci_root_bus_cleanup(PCIBus *bus)
443c13ee169SMichael Roth {
444c13ee169SMichael Roth     pci_bus_uninit(bus);
44507578b0aSDavid Hildenbrand     /* the caller of the unplug hotplug handler will delete this device */
44607578b0aSDavid Hildenbrand     object_property_set_bool(OBJECT(bus), false, "realized", NULL);
447c13ee169SMichael Roth }
448c13ee169SMichael Roth 
449315a1350SMichael S. Tsirkin void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
450315a1350SMichael S. Tsirkin                   void *irq_opaque, int nirq)
451315a1350SMichael S. Tsirkin {
452315a1350SMichael S. Tsirkin     bus->set_irq = set_irq;
453315a1350SMichael S. Tsirkin     bus->map_irq = map_irq;
454315a1350SMichael S. Tsirkin     bus->irq_opaque = irq_opaque;
455315a1350SMichael S. Tsirkin     bus->nirq = nirq;
456315a1350SMichael S. Tsirkin     bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0]));
457315a1350SMichael S. Tsirkin }
458315a1350SMichael S. Tsirkin 
459c13ee169SMichael Roth void pci_bus_irqs_cleanup(PCIBus *bus)
460c13ee169SMichael Roth {
461c13ee169SMichael Roth     bus->set_irq = NULL;
462c13ee169SMichael Roth     bus->map_irq = NULL;
463c13ee169SMichael Roth     bus->irq_opaque = NULL;
464c13ee169SMichael Roth     bus->nirq = 0;
465c13ee169SMichael Roth     g_free(bus->irq_count);
466c13ee169SMichael Roth }
467c13ee169SMichael Roth 
4681115ff6dSDavid Gibson PCIBus *pci_register_root_bus(DeviceState *parent, const char *name,
469315a1350SMichael S. Tsirkin                               pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
470315a1350SMichael S. Tsirkin                               void *irq_opaque,
471315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_mem,
472315a1350SMichael S. Tsirkin                               MemoryRegion *address_space_io,
4731115ff6dSDavid Gibson                               uint8_t devfn_min, int nirq,
4741115ff6dSDavid Gibson                               const char *typename)
475315a1350SMichael S. Tsirkin {
476315a1350SMichael S. Tsirkin     PCIBus *bus;
477315a1350SMichael S. Tsirkin 
4781115ff6dSDavid Gibson     bus = pci_root_bus_new(parent, name, address_space_mem,
47960a0e443SAlex Williamson                            address_space_io, devfn_min, typename);
480315a1350SMichael S. Tsirkin     pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
481315a1350SMichael S. Tsirkin     return bus;
482315a1350SMichael S. Tsirkin }
483315a1350SMichael S. Tsirkin 
484c13ee169SMichael Roth void pci_unregister_root_bus(PCIBus *bus)
485c13ee169SMichael Roth {
486c13ee169SMichael Roth     pci_bus_irqs_cleanup(bus);
487c13ee169SMichael Roth     pci_root_bus_cleanup(bus);
488c13ee169SMichael Roth }
489c13ee169SMichael Roth 
490315a1350SMichael S. Tsirkin int pci_bus_num(PCIBus *s)
491315a1350SMichael S. Tsirkin {
492602141d9SMarcel Apfelbaum     return PCI_BUS_GET_CLASS(s)->bus_num(s);
493315a1350SMichael S. Tsirkin }
494315a1350SMichael S. Tsirkin 
4956a3042b2SMarcel Apfelbaum int pci_bus_numa_node(PCIBus *bus)
4966a3042b2SMarcel Apfelbaum {
4976a3042b2SMarcel Apfelbaum     return PCI_BUS_GET_CLASS(bus)->numa_node(bus);
498315a1350SMichael S. Tsirkin }
499315a1350SMichael S. Tsirkin 
5002c21ee76SJianjun Duan static int get_pci_config_device(QEMUFile *f, void *pv, size_t size,
50103fee66fSMarc-André Lureau                                  const VMStateField *field)
502315a1350SMichael S. Tsirkin {
503315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, config);
504e78e9ae4SDon Koch     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(s);
505315a1350SMichael S. Tsirkin     uint8_t *config;
506315a1350SMichael S. Tsirkin     int i;
507315a1350SMichael S. Tsirkin 
508315a1350SMichael S. Tsirkin     assert(size == pci_config_size(s));
509315a1350SMichael S. Tsirkin     config = g_malloc(size);
510315a1350SMichael S. Tsirkin 
511315a1350SMichael S. Tsirkin     qemu_get_buffer(f, config, size);
512315a1350SMichael S. Tsirkin     for (i = 0; i < size; ++i) {
513315a1350SMichael S. Tsirkin         if ((config[i] ^ s->config[i]) &
514315a1350SMichael S. Tsirkin             s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
5157c59364dSDr. David Alan Gilbert             error_report("%s: Bad config data: i=0x%x read: %x device: %x "
5167c59364dSDr. David Alan Gilbert                          "cmask: %x wmask: %x w1cmask:%x", __func__,
5177c59364dSDr. David Alan Gilbert                          i, config[i], s->config[i],
5187c59364dSDr. David Alan Gilbert                          s->cmask[i], s->wmask[i], s->w1cmask[i]);
519315a1350SMichael S. Tsirkin             g_free(config);
520315a1350SMichael S. Tsirkin             return -EINVAL;
521315a1350SMichael S. Tsirkin         }
522315a1350SMichael S. Tsirkin     }
523315a1350SMichael S. Tsirkin     memcpy(s->config, config, size);
524315a1350SMichael S. Tsirkin 
525315a1350SMichael S. Tsirkin     pci_update_mappings(s);
526e78e9ae4SDon Koch     if (pc->is_bridge) {
527f055e96bSAndreas Färber         PCIBridge *b = PCI_BRIDGE(s);
528e78e9ae4SDon Koch         pci_bridge_update_mappings(b);
529e78e9ae4SDon Koch     }
530315a1350SMichael S. Tsirkin 
531315a1350SMichael S. Tsirkin     memory_region_set_enabled(&s->bus_master_enable_region,
532315a1350SMichael S. Tsirkin                               pci_get_word(s->config + PCI_COMMAND)
533315a1350SMichael S. Tsirkin                               & PCI_COMMAND_MASTER);
534315a1350SMichael S. Tsirkin 
535315a1350SMichael S. Tsirkin     g_free(config);
536315a1350SMichael S. Tsirkin     return 0;
537315a1350SMichael S. Tsirkin }
538315a1350SMichael S. Tsirkin 
539315a1350SMichael S. Tsirkin /* just put buffer */
5402c21ee76SJianjun Duan static int put_pci_config_device(QEMUFile *f, void *pv, size_t size,
54103fee66fSMarc-André Lureau                                  const VMStateField *field, QJSON *vmdesc)
542315a1350SMichael S. Tsirkin {
543315a1350SMichael S. Tsirkin     const uint8_t **v = pv;
544315a1350SMichael S. Tsirkin     assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
545315a1350SMichael S. Tsirkin     qemu_put_buffer(f, *v, size);
5462c21ee76SJianjun Duan 
5472c21ee76SJianjun Duan     return 0;
548315a1350SMichael S. Tsirkin }
549315a1350SMichael S. Tsirkin 
550315a1350SMichael S. Tsirkin static VMStateInfo vmstate_info_pci_config = {
551315a1350SMichael S. Tsirkin     .name = "pci config",
552315a1350SMichael S. Tsirkin     .get  = get_pci_config_device,
553315a1350SMichael S. Tsirkin     .put  = put_pci_config_device,
554315a1350SMichael S. Tsirkin };
555315a1350SMichael S. Tsirkin 
5562c21ee76SJianjun Duan static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size,
55703fee66fSMarc-André Lureau                              const VMStateField *field)
558315a1350SMichael S. Tsirkin {
559315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, irq_state);
560315a1350SMichael S. Tsirkin     uint32_t irq_state[PCI_NUM_PINS];
561315a1350SMichael S. Tsirkin     int i;
562315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
563315a1350SMichael S. Tsirkin         irq_state[i] = qemu_get_be32(f);
564315a1350SMichael S. Tsirkin         if (irq_state[i] != 0x1 && irq_state[i] != 0) {
565315a1350SMichael S. Tsirkin             fprintf(stderr, "irq state %d: must be 0 or 1.\n",
566315a1350SMichael S. Tsirkin                     irq_state[i]);
567315a1350SMichael S. Tsirkin             return -EINVAL;
568315a1350SMichael S. Tsirkin         }
569315a1350SMichael S. Tsirkin     }
570315a1350SMichael S. Tsirkin 
571315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
572315a1350SMichael S. Tsirkin         pci_set_irq_state(s, i, irq_state[i]);
573315a1350SMichael S. Tsirkin     }
574315a1350SMichael S. Tsirkin 
575315a1350SMichael S. Tsirkin     return 0;
576315a1350SMichael S. Tsirkin }
577315a1350SMichael S. Tsirkin 
5782c21ee76SJianjun Duan static int put_pci_irq_state(QEMUFile *f, void *pv, size_t size,
57903fee66fSMarc-André Lureau                              const VMStateField *field, QJSON *vmdesc)
580315a1350SMichael S. Tsirkin {
581315a1350SMichael S. Tsirkin     int i;
582315a1350SMichael S. Tsirkin     PCIDevice *s = container_of(pv, PCIDevice, irq_state);
583315a1350SMichael S. Tsirkin 
584315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
585315a1350SMichael S. Tsirkin         qemu_put_be32(f, pci_irq_state(s, i));
586315a1350SMichael S. Tsirkin     }
5872c21ee76SJianjun Duan 
5882c21ee76SJianjun Duan     return 0;
589315a1350SMichael S. Tsirkin }
590315a1350SMichael S. Tsirkin 
591315a1350SMichael S. Tsirkin static VMStateInfo vmstate_info_pci_irq_state = {
592315a1350SMichael S. Tsirkin     .name = "pci irq state",
593315a1350SMichael S. Tsirkin     .get  = get_pci_irq_state,
594315a1350SMichael S. Tsirkin     .put  = put_pci_irq_state,
595315a1350SMichael S. Tsirkin };
596315a1350SMichael S. Tsirkin 
59720daa90aSDr. David Alan Gilbert static bool migrate_is_pcie(void *opaque, int version_id)
59820daa90aSDr. David Alan Gilbert {
59920daa90aSDr. David Alan Gilbert     return pci_is_express((PCIDevice *)opaque);
60020daa90aSDr. David Alan Gilbert }
60120daa90aSDr. David Alan Gilbert 
60220daa90aSDr. David Alan Gilbert static bool migrate_is_not_pcie(void *opaque, int version_id)
60320daa90aSDr. David Alan Gilbert {
60420daa90aSDr. David Alan Gilbert     return !pci_is_express((PCIDevice *)opaque);
60520daa90aSDr. David Alan Gilbert }
60620daa90aSDr. David Alan Gilbert 
607315a1350SMichael S. Tsirkin const VMStateDescription vmstate_pci_device = {
608315a1350SMichael S. Tsirkin     .name = "PCIDevice",
609315a1350SMichael S. Tsirkin     .version_id = 2,
610315a1350SMichael S. Tsirkin     .minimum_version_id = 1,
611315a1350SMichael S. Tsirkin     .fields = (VMStateField[]) {
6123476436aSMichael S. Tsirkin         VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice),
61320daa90aSDr. David Alan Gilbert         VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice,
61420daa90aSDr. David Alan Gilbert                                    migrate_is_not_pcie,
61520daa90aSDr. David Alan Gilbert                                    0, vmstate_info_pci_config,
616315a1350SMichael S. Tsirkin                                    PCI_CONFIG_SPACE_SIZE),
61720daa90aSDr. David Alan Gilbert         VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice,
61820daa90aSDr. David Alan Gilbert                                    migrate_is_pcie,
61920daa90aSDr. David Alan Gilbert                                    0, vmstate_info_pci_config,
620315a1350SMichael S. Tsirkin                                    PCIE_CONFIG_SPACE_SIZE),
621315a1350SMichael S. Tsirkin         VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
622315a1350SMichael S. Tsirkin                                    vmstate_info_pci_irq_state,
623315a1350SMichael S. Tsirkin                                    PCI_NUM_PINS * sizeof(int32_t)),
624315a1350SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
625315a1350SMichael S. Tsirkin     }
626315a1350SMichael S. Tsirkin };
627315a1350SMichael S. Tsirkin 
628315a1350SMichael S. Tsirkin 
629315a1350SMichael S. Tsirkin void pci_device_save(PCIDevice *s, QEMUFile *f)
630315a1350SMichael S. Tsirkin {
631315a1350SMichael S. Tsirkin     /* Clear interrupt status bit: it is implicit
632315a1350SMichael S. Tsirkin      * in irq_state which we are saving.
633315a1350SMichael S. Tsirkin      * This makes us compatible with old devices
634315a1350SMichael S. Tsirkin      * which never set or clear this bit. */
635315a1350SMichael S. Tsirkin     s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
63620daa90aSDr. David Alan Gilbert     vmstate_save_state(f, &vmstate_pci_device, s, NULL);
637315a1350SMichael S. Tsirkin     /* Restore the interrupt status bit. */
638315a1350SMichael S. Tsirkin     pci_update_irq_status(s);
639315a1350SMichael S. Tsirkin }
640315a1350SMichael S. Tsirkin 
641315a1350SMichael S. Tsirkin int pci_device_load(PCIDevice *s, QEMUFile *f)
642315a1350SMichael S. Tsirkin {
643315a1350SMichael S. Tsirkin     int ret;
64420daa90aSDr. David Alan Gilbert     ret = vmstate_load_state(f, &vmstate_pci_device, s, s->version_id);
645315a1350SMichael S. Tsirkin     /* Restore the interrupt status bit. */
646315a1350SMichael S. Tsirkin     pci_update_irq_status(s);
647315a1350SMichael S. Tsirkin     return ret;
648315a1350SMichael S. Tsirkin }
649315a1350SMichael S. Tsirkin 
650315a1350SMichael S. Tsirkin static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
651315a1350SMichael S. Tsirkin {
652315a1350SMichael S. Tsirkin     pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
653315a1350SMichael S. Tsirkin                  pci_default_sub_vendor_id);
654315a1350SMichael S. Tsirkin     pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
655315a1350SMichael S. Tsirkin                  pci_default_sub_device_id);
656315a1350SMichael S. Tsirkin }
657315a1350SMichael S. Tsirkin 
658315a1350SMichael S. Tsirkin /*
659315a1350SMichael S. Tsirkin  * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
660315a1350SMichael S. Tsirkin  *       [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
661315a1350SMichael S. Tsirkin  */
6626dbcb819SMarkus Armbruster static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
663315a1350SMichael S. Tsirkin                              unsigned int *slotp, unsigned int *funcp)
664315a1350SMichael S. Tsirkin {
665315a1350SMichael S. Tsirkin     const char *p;
666315a1350SMichael S. Tsirkin     char *e;
667315a1350SMichael S. Tsirkin     unsigned long val;
668315a1350SMichael S. Tsirkin     unsigned long dom = 0, bus = 0;
669315a1350SMichael S. Tsirkin     unsigned int slot = 0;
670315a1350SMichael S. Tsirkin     unsigned int func = 0;
671315a1350SMichael S. Tsirkin 
672315a1350SMichael S. Tsirkin     p = addr;
673315a1350SMichael S. Tsirkin     val = strtoul(p, &e, 16);
674315a1350SMichael S. Tsirkin     if (e == p)
675315a1350SMichael S. Tsirkin         return -1;
676315a1350SMichael S. Tsirkin     if (*e == ':') {
677315a1350SMichael S. Tsirkin         bus = val;
678315a1350SMichael S. Tsirkin         p = e + 1;
679315a1350SMichael S. Tsirkin         val = strtoul(p, &e, 16);
680315a1350SMichael S. Tsirkin         if (e == p)
681315a1350SMichael S. Tsirkin             return -1;
682315a1350SMichael S. Tsirkin         if (*e == ':') {
683315a1350SMichael S. Tsirkin             dom = bus;
684315a1350SMichael S. Tsirkin             bus = val;
685315a1350SMichael S. Tsirkin             p = e + 1;
686315a1350SMichael S. Tsirkin             val = strtoul(p, &e, 16);
687315a1350SMichael S. Tsirkin             if (e == p)
688315a1350SMichael S. Tsirkin                 return -1;
689315a1350SMichael S. Tsirkin         }
690315a1350SMichael S. Tsirkin     }
691315a1350SMichael S. Tsirkin 
692315a1350SMichael S. Tsirkin     slot = val;
693315a1350SMichael S. Tsirkin 
694315a1350SMichael S. Tsirkin     if (funcp != NULL) {
695315a1350SMichael S. Tsirkin         if (*e != '.')
696315a1350SMichael S. Tsirkin             return -1;
697315a1350SMichael S. Tsirkin 
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 
703315a1350SMichael S. Tsirkin         func = val;
704315a1350SMichael S. Tsirkin     }
705315a1350SMichael S. Tsirkin 
706315a1350SMichael S. Tsirkin     /* if funcp == NULL func is 0 */
707315a1350SMichael S. Tsirkin     if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
708315a1350SMichael S. Tsirkin         return -1;
709315a1350SMichael S. Tsirkin 
710315a1350SMichael S. Tsirkin     if (*e)
711315a1350SMichael S. Tsirkin         return -1;
712315a1350SMichael S. Tsirkin 
713315a1350SMichael S. Tsirkin     *domp = dom;
714315a1350SMichael S. Tsirkin     *busp = bus;
715315a1350SMichael S. Tsirkin     *slotp = slot;
716315a1350SMichael S. Tsirkin     if (funcp != NULL)
717315a1350SMichael S. Tsirkin         *funcp = func;
718315a1350SMichael S. Tsirkin     return 0;
719315a1350SMichael S. Tsirkin }
720315a1350SMichael S. Tsirkin 
7216dbcb819SMarkus Armbruster static PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root,
7226dbcb819SMarkus Armbruster                                  const char *devaddr)
723315a1350SMichael S. Tsirkin {
724315a1350SMichael S. Tsirkin     int dom, bus;
725315a1350SMichael S. Tsirkin     unsigned slot;
726315a1350SMichael S. Tsirkin 
7271ef7a2a2SDavid Gibson     if (!root) {
7281ef7a2a2SDavid Gibson         fprintf(stderr, "No primary PCI bus\n");
7291ef7a2a2SDavid Gibson         return NULL;
7301ef7a2a2SDavid Gibson     }
7311ef7a2a2SDavid Gibson 
732b645000eSSaravanakumar     assert(!root->parent_dev);
733b645000eSSaravanakumar 
734315a1350SMichael S. Tsirkin     if (!devaddr) {
735315a1350SMichael S. Tsirkin         *devfnp = -1;
7361ef7a2a2SDavid Gibson         return pci_find_bus_nr(root, 0);
737315a1350SMichael S. Tsirkin     }
738315a1350SMichael S. Tsirkin 
739315a1350SMichael S. Tsirkin     if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
740315a1350SMichael S. Tsirkin         return NULL;
741315a1350SMichael S. Tsirkin     }
742315a1350SMichael S. Tsirkin 
7431ef7a2a2SDavid Gibson     if (dom != 0) {
7441ef7a2a2SDavid Gibson         fprintf(stderr, "No support for non-zero PCI domains\n");
7451ef7a2a2SDavid Gibson         return NULL;
7461ef7a2a2SDavid Gibson     }
7471ef7a2a2SDavid Gibson 
748315a1350SMichael S. Tsirkin     *devfnp = PCI_DEVFN(slot, 0);
7491ef7a2a2SDavid Gibson     return pci_find_bus_nr(root, bus);
750315a1350SMichael S. Tsirkin }
751315a1350SMichael S. Tsirkin 
752315a1350SMichael S. Tsirkin static void pci_init_cmask(PCIDevice *dev)
753315a1350SMichael S. Tsirkin {
754315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
755315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
756315a1350SMichael S. Tsirkin     dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
757315a1350SMichael S. Tsirkin     dev->cmask[PCI_REVISION_ID] = 0xff;
758315a1350SMichael S. Tsirkin     dev->cmask[PCI_CLASS_PROG] = 0xff;
759315a1350SMichael S. Tsirkin     pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
760315a1350SMichael S. Tsirkin     dev->cmask[PCI_HEADER_TYPE] = 0xff;
761315a1350SMichael S. Tsirkin     dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
762315a1350SMichael S. Tsirkin }
763315a1350SMichael S. Tsirkin 
764315a1350SMichael S. Tsirkin static void pci_init_wmask(PCIDevice *dev)
765315a1350SMichael S. Tsirkin {
766315a1350SMichael S. Tsirkin     int config_size = pci_config_size(dev);
767315a1350SMichael S. Tsirkin 
768315a1350SMichael S. Tsirkin     dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
769315a1350SMichael S. Tsirkin     dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
770315a1350SMichael S. Tsirkin     pci_set_word(dev->wmask + PCI_COMMAND,
771315a1350SMichael S. Tsirkin                  PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
772315a1350SMichael S. Tsirkin                  PCI_COMMAND_INTX_DISABLE);
773315a1350SMichael S. Tsirkin     if (dev->cap_present & QEMU_PCI_CAP_SERR) {
774315a1350SMichael S. Tsirkin         pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
775315a1350SMichael S. Tsirkin     }
776315a1350SMichael S. Tsirkin 
777315a1350SMichael S. Tsirkin     memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
778315a1350SMichael S. Tsirkin            config_size - PCI_CONFIG_HEADER_SIZE);
779315a1350SMichael S. Tsirkin }
780315a1350SMichael S. Tsirkin 
781315a1350SMichael S. Tsirkin static void pci_init_w1cmask(PCIDevice *dev)
782315a1350SMichael S. Tsirkin {
783315a1350SMichael S. Tsirkin     /*
784315a1350SMichael S. Tsirkin      * Note: It's okay to set w1cmask even for readonly bits as
785315a1350SMichael S. Tsirkin      * long as their value is hardwired to 0.
786315a1350SMichael S. Tsirkin      */
787315a1350SMichael S. Tsirkin     pci_set_word(dev->w1cmask + PCI_STATUS,
788315a1350SMichael S. Tsirkin                  PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
789315a1350SMichael S. Tsirkin                  PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
790315a1350SMichael S. Tsirkin                  PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
791315a1350SMichael S. Tsirkin }
792315a1350SMichael S. Tsirkin 
793315a1350SMichael S. Tsirkin static void pci_init_mask_bridge(PCIDevice *d)
794315a1350SMichael S. Tsirkin {
795315a1350SMichael S. Tsirkin     /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
796315a1350SMichael S. Tsirkin        PCI_SEC_LETENCY_TIMER */
797315a1350SMichael S. Tsirkin     memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
798315a1350SMichael S. Tsirkin 
799315a1350SMichael S. Tsirkin     /* base and limit */
800315a1350SMichael S. Tsirkin     d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
801315a1350SMichael S. Tsirkin     d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
802315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_MEMORY_BASE,
803315a1350SMichael S. Tsirkin                  PCI_MEMORY_RANGE_MASK & 0xffff);
804315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
805315a1350SMichael S. Tsirkin                  PCI_MEMORY_RANGE_MASK & 0xffff);
806315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
807315a1350SMichael S. Tsirkin                  PCI_PREF_RANGE_MASK & 0xffff);
808315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
809315a1350SMichael S. Tsirkin                  PCI_PREF_RANGE_MASK & 0xffff);
810315a1350SMichael S. Tsirkin 
811315a1350SMichael S. Tsirkin     /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
812315a1350SMichael S. Tsirkin     memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
813315a1350SMichael S. Tsirkin 
814315a1350SMichael S. Tsirkin     /* Supported memory and i/o types */
815315a1350SMichael S. Tsirkin     d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16;
816315a1350SMichael S. Tsirkin     d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16;
817315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE,
818315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_64);
819315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT,
820315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_64);
821315a1350SMichael S. Tsirkin 
822ba7d8515SAlex Williamson     /*
823ba7d8515SAlex Williamson      * TODO: Bridges default to 10-bit VGA decoding but we currently only
824ba7d8515SAlex Williamson      * implement 16-bit decoding (no alias support).
825ba7d8515SAlex Williamson      */
826315a1350SMichael S. Tsirkin     pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
827315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_PARITY |
828315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_SERR |
829315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_ISA |
830315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_VGA |
831315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_VGA_16BIT |
832315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_MASTER_ABORT |
833315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_BUS_RESET |
834315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_FAST_BACK |
835315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD |
836315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_SEC_DISCARD |
837315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD_SERR);
838315a1350SMichael S. Tsirkin     /* Below does not do anything as we never set this bit, put here for
839315a1350SMichael S. Tsirkin      * completeness. */
840315a1350SMichael S. Tsirkin     pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
841315a1350SMichael S. Tsirkin                  PCI_BRIDGE_CTL_DISCARD_STATUS);
842315a1350SMichael S. Tsirkin     d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK;
843315a1350SMichael S. Tsirkin     d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK;
844315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE,
845315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_MASK);
846315a1350SMichael S. Tsirkin     pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT,
847315a1350SMichael S. Tsirkin                                PCI_PREF_RANGE_TYPE_MASK);
848315a1350SMichael S. Tsirkin }
849315a1350SMichael S. Tsirkin 
850133e9b22SMarkus Armbruster static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp)
851315a1350SMichael S. Tsirkin {
852315a1350SMichael S. Tsirkin     uint8_t slot = PCI_SLOT(dev->devfn);
853315a1350SMichael S. Tsirkin     uint8_t func;
854315a1350SMichael S. Tsirkin 
855315a1350SMichael S. Tsirkin     if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
856315a1350SMichael S. Tsirkin         dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
857315a1350SMichael S. Tsirkin     }
858315a1350SMichael S. Tsirkin 
859315a1350SMichael S. Tsirkin     /*
860315a1350SMichael S. Tsirkin      * multifunction bit is interpreted in two ways as follows.
861315a1350SMichael S. Tsirkin      *   - all functions must set the bit to 1.
862315a1350SMichael S. Tsirkin      *     Example: Intel X53
863315a1350SMichael S. Tsirkin      *   - function 0 must set the bit, but the rest function (> 0)
864315a1350SMichael S. Tsirkin      *     is allowed to leave the bit to 0.
865315a1350SMichael S. Tsirkin      *     Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
866315a1350SMichael S. Tsirkin      *
867315a1350SMichael S. Tsirkin      * So OS (at least Linux) checks the bit of only function 0,
868315a1350SMichael S. Tsirkin      * and doesn't see the bit of function > 0.
869315a1350SMichael S. Tsirkin      *
870315a1350SMichael S. Tsirkin      * The below check allows both interpretation.
871315a1350SMichael S. Tsirkin      */
872315a1350SMichael S. Tsirkin     if (PCI_FUNC(dev->devfn)) {
873315a1350SMichael S. Tsirkin         PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
874315a1350SMichael S. Tsirkin         if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
875315a1350SMichael S. Tsirkin             /* function 0 should set multifunction bit */
876133e9b22SMarkus Armbruster             error_setg(errp, "PCI: single function device can't be populated "
877315a1350SMichael S. Tsirkin                        "in function %x.%x", slot, PCI_FUNC(dev->devfn));
878133e9b22SMarkus Armbruster             return;
879315a1350SMichael S. Tsirkin         }
880133e9b22SMarkus Armbruster         return;
881315a1350SMichael S. Tsirkin     }
882315a1350SMichael S. Tsirkin 
883315a1350SMichael S. Tsirkin     if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
884133e9b22SMarkus Armbruster         return;
885315a1350SMichael S. Tsirkin     }
886315a1350SMichael S. Tsirkin     /* function 0 indicates single function, so function > 0 must be NULL */
887315a1350SMichael S. Tsirkin     for (func = 1; func < PCI_FUNC_MAX; ++func) {
888315a1350SMichael S. Tsirkin         if (bus->devices[PCI_DEVFN(slot, func)]) {
889133e9b22SMarkus Armbruster             error_setg(errp, "PCI: %x.0 indicates single function, "
890315a1350SMichael S. Tsirkin                        "but %x.%x is already populated.",
891315a1350SMichael S. Tsirkin                        slot, slot, func);
892133e9b22SMarkus Armbruster             return;
893315a1350SMichael S. Tsirkin         }
894315a1350SMichael S. Tsirkin     }
895315a1350SMichael S. Tsirkin }
896315a1350SMichael S. Tsirkin 
897315a1350SMichael S. Tsirkin static void pci_config_alloc(PCIDevice *pci_dev)
898315a1350SMichael S. Tsirkin {
899315a1350SMichael S. Tsirkin     int config_size = pci_config_size(pci_dev);
900315a1350SMichael S. Tsirkin 
901315a1350SMichael S. Tsirkin     pci_dev->config = g_malloc0(config_size);
902315a1350SMichael S. Tsirkin     pci_dev->cmask = g_malloc0(config_size);
903315a1350SMichael S. Tsirkin     pci_dev->wmask = g_malloc0(config_size);
904315a1350SMichael S. Tsirkin     pci_dev->w1cmask = g_malloc0(config_size);
905315a1350SMichael S. Tsirkin     pci_dev->used = g_malloc0(config_size);
906315a1350SMichael S. Tsirkin }
907315a1350SMichael S. Tsirkin 
908315a1350SMichael S. Tsirkin static void pci_config_free(PCIDevice *pci_dev)
909315a1350SMichael S. Tsirkin {
910315a1350SMichael S. Tsirkin     g_free(pci_dev->config);
911315a1350SMichael S. Tsirkin     g_free(pci_dev->cmask);
912315a1350SMichael S. Tsirkin     g_free(pci_dev->wmask);
913315a1350SMichael S. Tsirkin     g_free(pci_dev->w1cmask);
914315a1350SMichael S. Tsirkin     g_free(pci_dev->used);
915315a1350SMichael S. Tsirkin }
916315a1350SMichael S. Tsirkin 
91730607764SMarcel Apfelbaum static void do_pci_unregister_device(PCIDevice *pci_dev)
91830607764SMarcel Apfelbaum {
919fd56e061SDavid Gibson     pci_get_bus(pci_dev)->devices[pci_dev->devfn] = NULL;
92030607764SMarcel Apfelbaum     pci_config_free(pci_dev);
92130607764SMarcel Apfelbaum 
922193982c6SAlexey Kardashevskiy     if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
923c53598edSAlexey Kardashevskiy         memory_region_del_subregion(&pci_dev->bus_master_container_region,
924c53598edSAlexey Kardashevskiy                                     &pci_dev->bus_master_enable_region);
925193982c6SAlexey Kardashevskiy     }
92630607764SMarcel Apfelbaum     address_space_destroy(&pci_dev->bus_master_as);
92730607764SMarcel Apfelbaum }
92830607764SMarcel Apfelbaum 
9294a94b3aaSPeter Xu /* Extract PCIReqIDCache into BDF format */
9304a94b3aaSPeter Xu static uint16_t pci_req_id_cache_extract(PCIReqIDCache *cache)
9314a94b3aaSPeter Xu {
9324a94b3aaSPeter Xu     uint8_t bus_n;
9334a94b3aaSPeter Xu     uint16_t result;
9344a94b3aaSPeter Xu 
9354a94b3aaSPeter Xu     switch (cache->type) {
9364a94b3aaSPeter Xu     case PCI_REQ_ID_BDF:
9374a94b3aaSPeter Xu         result = pci_get_bdf(cache->dev);
9384a94b3aaSPeter Xu         break;
9394a94b3aaSPeter Xu     case PCI_REQ_ID_SECONDARY_BUS:
940fd56e061SDavid Gibson         bus_n = pci_dev_bus_num(cache->dev);
9414a94b3aaSPeter Xu         result = PCI_BUILD_BDF(bus_n, 0);
9424a94b3aaSPeter Xu         break;
9434a94b3aaSPeter Xu     default:
944eaf27fabSMarkus Armbruster         error_report("Invalid PCI requester ID cache type: %d",
9454a94b3aaSPeter Xu                      cache->type);
9464a94b3aaSPeter Xu         exit(1);
9474a94b3aaSPeter Xu         break;
9484a94b3aaSPeter Xu     }
9494a94b3aaSPeter Xu 
9504a94b3aaSPeter Xu     return result;
9514a94b3aaSPeter Xu }
9524a94b3aaSPeter Xu 
9534a94b3aaSPeter Xu /* Parse bridges up to the root complex and return requester ID
9544a94b3aaSPeter Xu  * cache for specific device.  For full PCIe topology, the cache
9554a94b3aaSPeter Xu  * result would be exactly the same as getting BDF of the device.
9564a94b3aaSPeter Xu  * However, several tricks are required when system mixed up with
9574a94b3aaSPeter Xu  * legacy PCI devices and PCIe-to-PCI bridges.
9584a94b3aaSPeter Xu  *
9594a94b3aaSPeter Xu  * Here we cache the proxy device (and type) not requester ID since
9604a94b3aaSPeter Xu  * bus number might change from time to time.
9614a94b3aaSPeter Xu  */
9624a94b3aaSPeter Xu static PCIReqIDCache pci_req_id_cache_get(PCIDevice *dev)
9634a94b3aaSPeter Xu {
9644a94b3aaSPeter Xu     PCIDevice *parent;
9654a94b3aaSPeter Xu     PCIReqIDCache cache = {
9664a94b3aaSPeter Xu         .dev = dev,
9674a94b3aaSPeter Xu         .type = PCI_REQ_ID_BDF,
9684a94b3aaSPeter Xu     };
9694a94b3aaSPeter Xu 
970fd56e061SDavid Gibson     while (!pci_bus_is_root(pci_get_bus(dev))) {
9714a94b3aaSPeter Xu         /* We are under PCI/PCIe bridges */
972fd56e061SDavid Gibson         parent = pci_get_bus(dev)->parent_dev;
9734a94b3aaSPeter Xu         if (pci_is_express(parent)) {
9744a94b3aaSPeter Xu             if (pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) {
9754a94b3aaSPeter Xu                 /* When we pass through PCIe-to-PCI/PCIX bridges, we
9764a94b3aaSPeter Xu                  * override the requester ID using secondary bus
9774a94b3aaSPeter Xu                  * number of parent bridge with zeroed devfn
9784a94b3aaSPeter Xu                  * (pcie-to-pci bridge spec chap 2.3). */
9794a94b3aaSPeter Xu                 cache.type = PCI_REQ_ID_SECONDARY_BUS;
9804a94b3aaSPeter Xu                 cache.dev = dev;
9814a94b3aaSPeter Xu             }
9824a94b3aaSPeter Xu         } else {
9834a94b3aaSPeter Xu             /* Legacy PCI, override requester ID with the bridge's
9844a94b3aaSPeter Xu              * BDF upstream.  When the root complex connects to
9854a94b3aaSPeter Xu              * legacy PCI devices (including buses), it can only
9864a94b3aaSPeter Xu              * obtain requester ID info from directly attached
9874a94b3aaSPeter Xu              * devices.  If devices are attached under bridges, only
9884a94b3aaSPeter Xu              * the requester ID of the bridge that is directly
9894a94b3aaSPeter Xu              * attached to the root complex can be recognized. */
9904a94b3aaSPeter Xu             cache.type = PCI_REQ_ID_BDF;
9914a94b3aaSPeter Xu             cache.dev = parent;
9924a94b3aaSPeter Xu         }
9934a94b3aaSPeter Xu         dev = parent;
9944a94b3aaSPeter Xu     }
9954a94b3aaSPeter Xu 
9964a94b3aaSPeter Xu     return cache;
9974a94b3aaSPeter Xu }
9984a94b3aaSPeter Xu 
9994a94b3aaSPeter Xu uint16_t pci_requester_id(PCIDevice *dev)
10004a94b3aaSPeter Xu {
10014a94b3aaSPeter Xu     return pci_req_id_cache_extract(&dev->requester_id_cache);
10024a94b3aaSPeter Xu }
10034a94b3aaSPeter Xu 
10049b717a3aSMark Cave-Ayland static bool pci_bus_devfn_available(PCIBus *bus, int devfn)
10059b717a3aSMark Cave-Ayland {
10069b717a3aSMark Cave-Ayland     return !(bus->devices[devfn]);
10079b717a3aSMark Cave-Ayland }
10089b717a3aSMark Cave-Ayland 
10098b884984SMark Cave-Ayland static bool pci_bus_devfn_reserved(PCIBus *bus, int devfn)
10108b884984SMark Cave-Ayland {
10118b884984SMark Cave-Ayland     return bus->slot_reserved_mask & (1UL << PCI_SLOT(devfn));
10128b884984SMark Cave-Ayland }
10138b884984SMark Cave-Ayland 
1014315a1350SMichael S. Tsirkin /* -1 for devfn means auto assign */
1015fd56e061SDavid Gibson static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
1016133e9b22SMarkus Armbruster                                          const char *name, int devfn,
1017133e9b22SMarkus Armbruster                                          Error **errp)
1018315a1350SMichael S. Tsirkin {
1019315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
1020315a1350SMichael S. Tsirkin     PCIConfigReadFunc *config_read = pc->config_read;
1021315a1350SMichael S. Tsirkin     PCIConfigWriteFunc *config_write = pc->config_write;
1022133e9b22SMarkus Armbruster     Error *local_err = NULL;
10233f1e1478SCao jin     DeviceState *dev = DEVICE(pci_dev);
1024fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
10253f1e1478SCao jin 
10260144f6f1SMarcel Apfelbaum     /* Only pci bridges can be attached to extra PCI root buses */
10270144f6f1SMarcel Apfelbaum     if (pci_bus_is_root(bus) && bus->parent_dev && !pc->is_bridge) {
10280144f6f1SMarcel Apfelbaum         error_setg(errp,
10290144f6f1SMarcel Apfelbaum                    "PCI: Only PCI/PCIe bridges can be plugged into %s",
10300144f6f1SMarcel Apfelbaum                     bus->parent_dev->name);
10310144f6f1SMarcel Apfelbaum         return NULL;
10320144f6f1SMarcel Apfelbaum     }
1033315a1350SMichael S. Tsirkin 
1034315a1350SMichael S. Tsirkin     if (devfn < 0) {
1035315a1350SMichael S. Tsirkin         for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
1036315a1350SMichael S. Tsirkin             devfn += PCI_FUNC_MAX) {
10378b884984SMark Cave-Ayland             if (pci_bus_devfn_available(bus, devfn) &&
10388b884984SMark Cave-Ayland                    !pci_bus_devfn_reserved(bus, devfn)) {
1039315a1350SMichael S. Tsirkin                 goto found;
1040315a1350SMichael S. Tsirkin             }
10419b717a3aSMark Cave-Ayland         }
10428b884984SMark Cave-Ayland         error_setg(errp, "PCI: no slot/function available for %s, all in use "
10438b884984SMark Cave-Ayland                    "or reserved", name);
1044315a1350SMichael S. Tsirkin         return NULL;
1045315a1350SMichael S. Tsirkin     found: ;
10468b884984SMark Cave-Ayland     } else if (pci_bus_devfn_reserved(bus, devfn)) {
10478b884984SMark Cave-Ayland         error_setg(errp, "PCI: slot %d function %d not available for %s,"
10488b884984SMark Cave-Ayland                    " reserved",
10498b884984SMark Cave-Ayland                    PCI_SLOT(devfn), PCI_FUNC(devfn), name);
10508b884984SMark Cave-Ayland         return NULL;
10519b717a3aSMark Cave-Ayland     } else if (!pci_bus_devfn_available(bus, devfn)) {
1052133e9b22SMarkus Armbruster         error_setg(errp, "PCI: slot %d function %d not available for %s,"
1053133e9b22SMarkus Armbruster                    " in use by %s",
1054133e9b22SMarkus Armbruster                    PCI_SLOT(devfn), PCI_FUNC(devfn), name,
1055133e9b22SMarkus Armbruster                    bus->devices[devfn]->name);
1056315a1350SMichael S. Tsirkin         return NULL;
10573f1e1478SCao jin     } else if (dev->hotplugged &&
10583f1e1478SCao jin                pci_get_function_0(pci_dev)) {
10593f1e1478SCao jin         error_setg(errp, "PCI: slot %d function 0 already ocuppied by %s,"
10603f1e1478SCao jin                    " new func %s cannot be exposed to guest.",
1061d93ddfb1SMichael S. Tsirkin                    PCI_SLOT(pci_get_function_0(pci_dev)->devfn),
1062d93ddfb1SMichael S. Tsirkin                    pci_get_function_0(pci_dev)->name,
10633f1e1478SCao jin                    name);
10643f1e1478SCao jin 
10653f1e1478SCao jin        return NULL;
1066315a1350SMichael S. Tsirkin     }
1067e00387d5SAvi Kivity 
1068efc8188eSLe Tan     pci_dev->devfn = devfn;
10694a94b3aaSPeter Xu     pci_dev->requester_id_cache = pci_req_id_cache_get(pci_dev);
1070d06bce95SAlexey Kardashevskiy     pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
1071e00387d5SAvi Kivity 
10723716d590SJason Wang     memory_region_init(&pci_dev->bus_master_container_region, OBJECT(pci_dev),
10733716d590SJason Wang                        "bus master container", UINT64_MAX);
10743716d590SJason Wang     address_space_init(&pci_dev->bus_master_as,
10753716d590SJason Wang                        &pci_dev->bus_master_container_region, pci_dev->name);
10763716d590SJason Wang 
1077b86eacb8SMarcel Apfelbaum     if (qdev_hotplug) {
1078b86eacb8SMarcel Apfelbaum         pci_init_bus_master(pci_dev);
1079b86eacb8SMarcel Apfelbaum     }
1080315a1350SMichael S. Tsirkin     pci_dev->irq_state = 0;
1081315a1350SMichael S. Tsirkin     pci_config_alloc(pci_dev);
1082315a1350SMichael S. Tsirkin 
1083315a1350SMichael S. Tsirkin     pci_config_set_vendor_id(pci_dev->config, pc->vendor_id);
1084315a1350SMichael S. Tsirkin     pci_config_set_device_id(pci_dev->config, pc->device_id);
1085315a1350SMichael S. Tsirkin     pci_config_set_revision(pci_dev->config, pc->revision);
1086315a1350SMichael S. Tsirkin     pci_config_set_class(pci_dev->config, pc->class_id);
1087315a1350SMichael S. Tsirkin 
1088315a1350SMichael S. Tsirkin     if (!pc->is_bridge) {
1089315a1350SMichael S. Tsirkin         if (pc->subsystem_vendor_id || pc->subsystem_id) {
1090315a1350SMichael S. Tsirkin             pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
1091315a1350SMichael S. Tsirkin                          pc->subsystem_vendor_id);
1092315a1350SMichael S. Tsirkin             pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
1093315a1350SMichael S. Tsirkin                          pc->subsystem_id);
1094315a1350SMichael S. Tsirkin         } else {
1095315a1350SMichael S. Tsirkin             pci_set_default_subsystem_id(pci_dev);
1096315a1350SMichael S. Tsirkin         }
1097315a1350SMichael S. Tsirkin     } else {
1098315a1350SMichael S. Tsirkin         /* subsystem_vendor_id/subsystem_id are only for header type 0 */
1099315a1350SMichael S. Tsirkin         assert(!pc->subsystem_vendor_id);
1100315a1350SMichael S. Tsirkin         assert(!pc->subsystem_id);
1101315a1350SMichael S. Tsirkin     }
1102315a1350SMichael S. Tsirkin     pci_init_cmask(pci_dev);
1103315a1350SMichael S. Tsirkin     pci_init_wmask(pci_dev);
1104315a1350SMichael S. Tsirkin     pci_init_w1cmask(pci_dev);
1105315a1350SMichael S. Tsirkin     if (pc->is_bridge) {
1106315a1350SMichael S. Tsirkin         pci_init_mask_bridge(pci_dev);
1107315a1350SMichael S. Tsirkin     }
1108133e9b22SMarkus Armbruster     pci_init_multifunction(bus, pci_dev, &local_err);
1109133e9b22SMarkus Armbruster     if (local_err) {
1110133e9b22SMarkus Armbruster         error_propagate(errp, local_err);
111130607764SMarcel Apfelbaum         do_pci_unregister_device(pci_dev);
1112315a1350SMichael S. Tsirkin         return NULL;
1113315a1350SMichael S. Tsirkin     }
1114315a1350SMichael S. Tsirkin 
1115315a1350SMichael S. Tsirkin     if (!config_read)
1116315a1350SMichael S. Tsirkin         config_read = pci_default_read_config;
1117315a1350SMichael S. Tsirkin     if (!config_write)
1118315a1350SMichael S. Tsirkin         config_write = pci_default_write_config;
1119315a1350SMichael S. Tsirkin     pci_dev->config_read = config_read;
1120315a1350SMichael S. Tsirkin     pci_dev->config_write = config_write;
1121315a1350SMichael S. Tsirkin     bus->devices[devfn] = pci_dev;
1122315a1350SMichael S. Tsirkin     pci_dev->version_id = 2; /* Current pci device vmstate version */
1123315a1350SMichael S. Tsirkin     return pci_dev;
1124315a1350SMichael S. Tsirkin }
1125315a1350SMichael S. Tsirkin 
1126315a1350SMichael S. Tsirkin static void pci_unregister_io_regions(PCIDevice *pci_dev)
1127315a1350SMichael S. Tsirkin {
1128315a1350SMichael S. Tsirkin     PCIIORegion *r;
1129315a1350SMichael S. Tsirkin     int i;
1130315a1350SMichael S. Tsirkin 
1131315a1350SMichael S. Tsirkin     for(i = 0; i < PCI_NUM_REGIONS; i++) {
1132315a1350SMichael S. Tsirkin         r = &pci_dev->io_regions[i];
1133315a1350SMichael S. Tsirkin         if (!r->size || r->addr == PCI_BAR_UNMAPPED)
1134315a1350SMichael S. Tsirkin             continue;
1135315a1350SMichael S. Tsirkin         memory_region_del_subregion(r->address_space, r->memory);
1136315a1350SMichael S. Tsirkin     }
1137e01fd687SAlex Williamson 
1138e01fd687SAlex Williamson     pci_unregister_vga(pci_dev);
1139315a1350SMichael S. Tsirkin }
1140315a1350SMichael S. Tsirkin 
1141133e9b22SMarkus Armbruster static void pci_qdev_unrealize(DeviceState *dev, Error **errp)
1142315a1350SMichael S. Tsirkin {
1143315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = PCI_DEVICE(dev);
1144315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
1145315a1350SMichael S. Tsirkin 
1146315a1350SMichael S. Tsirkin     pci_unregister_io_regions(pci_dev);
1147315a1350SMichael S. Tsirkin     pci_del_option_rom(pci_dev);
1148315a1350SMichael S. Tsirkin 
1149315a1350SMichael S. Tsirkin     if (pc->exit) {
1150315a1350SMichael S. Tsirkin         pc->exit(pci_dev);
1151315a1350SMichael S. Tsirkin     }
1152315a1350SMichael S. Tsirkin 
11533936161fSHerongguang (Stephen)     pci_device_deassert_intx(pci_dev);
1154315a1350SMichael S. Tsirkin     do_pci_unregister_device(pci_dev);
1155315a1350SMichael S. Tsirkin }
1156315a1350SMichael S. Tsirkin 
1157315a1350SMichael S. Tsirkin void pci_register_bar(PCIDevice *pci_dev, int region_num,
1158315a1350SMichael S. Tsirkin                       uint8_t type, MemoryRegion *memory)
1159315a1350SMichael S. Tsirkin {
1160315a1350SMichael S. Tsirkin     PCIIORegion *r;
11615178ecd8SCao jin     uint32_t addr; /* offset in pci config space */
1162315a1350SMichael S. Tsirkin     uint64_t wmask;
1163315a1350SMichael S. Tsirkin     pcibus_t size = memory_region_size(memory);
1164315a1350SMichael S. Tsirkin 
1165315a1350SMichael S. Tsirkin     assert(region_num >= 0);
1166315a1350SMichael S. Tsirkin     assert(region_num < PCI_NUM_REGIONS);
1167315a1350SMichael S. Tsirkin     if (size & (size-1)) {
11680151abe4SAlistair Francis         error_report("ERROR: PCI region size must be pow2 "
11690151abe4SAlistair Francis                     "type=0x%x, size=0x%"FMT_PCIBUS"", type, size);
1170315a1350SMichael S. Tsirkin         exit(1);
1171315a1350SMichael S. Tsirkin     }
1172315a1350SMichael S. Tsirkin 
1173315a1350SMichael S. Tsirkin     r = &pci_dev->io_regions[region_num];
1174315a1350SMichael S. Tsirkin     r->addr = PCI_BAR_UNMAPPED;
1175315a1350SMichael S. Tsirkin     r->size = size;
1176315a1350SMichael S. Tsirkin     r->type = type;
11775178ecd8SCao jin     r->memory = memory;
11785178ecd8SCao jin     r->address_space = type & PCI_BASE_ADDRESS_SPACE_IO
1179fd56e061SDavid Gibson                         ? pci_get_bus(pci_dev)->address_space_io
1180fd56e061SDavid Gibson                         : pci_get_bus(pci_dev)->address_space_mem;
1181315a1350SMichael S. Tsirkin 
1182315a1350SMichael S. Tsirkin     wmask = ~(size - 1);
1183315a1350SMichael S. Tsirkin     if (region_num == PCI_ROM_SLOT) {
1184315a1350SMichael S. Tsirkin         /* ROM enable bit is writable */
1185315a1350SMichael S. Tsirkin         wmask |= PCI_ROM_ADDRESS_ENABLE;
1186315a1350SMichael S. Tsirkin     }
11875178ecd8SCao jin 
11885178ecd8SCao jin     addr = pci_bar(pci_dev, region_num);
1189315a1350SMichael S. Tsirkin     pci_set_long(pci_dev->config + addr, type);
11905178ecd8SCao jin 
1191315a1350SMichael S. Tsirkin     if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
1192315a1350SMichael S. Tsirkin         r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1193315a1350SMichael S. Tsirkin         pci_set_quad(pci_dev->wmask + addr, wmask);
1194315a1350SMichael S. Tsirkin         pci_set_quad(pci_dev->cmask + addr, ~0ULL);
1195315a1350SMichael S. Tsirkin     } else {
1196315a1350SMichael S. Tsirkin         pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
1197315a1350SMichael S. Tsirkin         pci_set_long(pci_dev->cmask + addr, 0xffffffff);
1198315a1350SMichael S. Tsirkin     }
1199315a1350SMichael S. Tsirkin }
1200315a1350SMichael S. Tsirkin 
1201e01fd687SAlex Williamson static void pci_update_vga(PCIDevice *pci_dev)
1202e01fd687SAlex Williamson {
1203e01fd687SAlex Williamson     uint16_t cmd;
1204e01fd687SAlex Williamson 
1205e01fd687SAlex Williamson     if (!pci_dev->has_vga) {
1206e01fd687SAlex Williamson         return;
1207e01fd687SAlex Williamson     }
1208e01fd687SAlex Williamson 
1209e01fd687SAlex Williamson     cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
1210e01fd687SAlex Williamson 
1211e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM],
1212e01fd687SAlex Williamson                               cmd & PCI_COMMAND_MEMORY);
1213e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO],
1214e01fd687SAlex Williamson                               cmd & PCI_COMMAND_IO);
1215e01fd687SAlex Williamson     memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI],
1216e01fd687SAlex Williamson                               cmd & PCI_COMMAND_IO);
1217e01fd687SAlex Williamson }
1218e01fd687SAlex Williamson 
1219e01fd687SAlex Williamson void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
1220e01fd687SAlex Williamson                       MemoryRegion *io_lo, MemoryRegion *io_hi)
1221e01fd687SAlex Williamson {
1222fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
1223fd56e061SDavid Gibson 
1224e01fd687SAlex Williamson     assert(!pci_dev->has_vga);
1225e01fd687SAlex Williamson 
1226e01fd687SAlex Williamson     assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE);
1227e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem;
1228fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_mem,
1229e01fd687SAlex Williamson                                         QEMU_PCI_VGA_MEM_BASE, mem, 1);
1230e01fd687SAlex Williamson 
1231e01fd687SAlex Williamson     assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE);
1232e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo;
1233fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_io,
1234e01fd687SAlex Williamson                                         QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1);
1235e01fd687SAlex Williamson 
1236e01fd687SAlex Williamson     assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE);
1237e01fd687SAlex Williamson     pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi;
1238fd56e061SDavid Gibson     memory_region_add_subregion_overlap(bus->address_space_io,
1239e01fd687SAlex Williamson                                         QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1);
1240e01fd687SAlex Williamson     pci_dev->has_vga = true;
1241e01fd687SAlex Williamson 
1242e01fd687SAlex Williamson     pci_update_vga(pci_dev);
1243e01fd687SAlex Williamson }
1244e01fd687SAlex Williamson 
1245e01fd687SAlex Williamson void pci_unregister_vga(PCIDevice *pci_dev)
1246e01fd687SAlex Williamson {
1247fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
1248fd56e061SDavid Gibson 
1249e01fd687SAlex Williamson     if (!pci_dev->has_vga) {
1250e01fd687SAlex Williamson         return;
1251e01fd687SAlex Williamson     }
1252e01fd687SAlex Williamson 
1253fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_mem,
1254e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_MEM]);
1255fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_io,
1256e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]);
1257fd56e061SDavid Gibson     memory_region_del_subregion(bus->address_space_io,
1258e01fd687SAlex Williamson                                 pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]);
1259e01fd687SAlex Williamson     pci_dev->has_vga = false;
1260e01fd687SAlex Williamson }
1261e01fd687SAlex Williamson 
1262315a1350SMichael S. Tsirkin pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
1263315a1350SMichael S. Tsirkin {
1264315a1350SMichael S. Tsirkin     return pci_dev->io_regions[region_num].addr;
1265315a1350SMichael S. Tsirkin }
1266315a1350SMichael S. Tsirkin 
1267315a1350SMichael S. Tsirkin static pcibus_t pci_bar_address(PCIDevice *d,
1268315a1350SMichael S. Tsirkin                                 int reg, uint8_t type, pcibus_t size)
1269315a1350SMichael S. Tsirkin {
1270315a1350SMichael S. Tsirkin     pcibus_t new_addr, last_addr;
1271315a1350SMichael S. Tsirkin     int bar = pci_bar(d, reg);
1272315a1350SMichael S. Tsirkin     uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
1273e4024630SLaurent Vivier     Object *machine = qdev_get_machine();
1274e4024630SLaurent Vivier     ObjectClass *oc = object_get_class(machine);
1275e4024630SLaurent Vivier     MachineClass *mc = MACHINE_CLASS(oc);
1276e4024630SLaurent Vivier     bool allow_0_address = mc->pci_allow_0_address;
1277315a1350SMichael S. Tsirkin 
1278315a1350SMichael S. Tsirkin     if (type & PCI_BASE_ADDRESS_SPACE_IO) {
1279315a1350SMichael S. Tsirkin         if (!(cmd & PCI_COMMAND_IO)) {
1280315a1350SMichael S. Tsirkin             return PCI_BAR_UNMAPPED;
1281315a1350SMichael S. Tsirkin         }
1282315a1350SMichael S. Tsirkin         new_addr = pci_get_long(d->config + bar) & ~(size - 1);
1283315a1350SMichael S. Tsirkin         last_addr = new_addr + size - 1;
12849f1a029aSHervé Poussineau         /* Check if 32 bit BAR wraps around explicitly.
12859f1a029aSHervé Poussineau          * TODO: make priorities correct and remove this work around.
12869f1a029aSHervé Poussineau          */
1287e4024630SLaurent Vivier         if (last_addr <= new_addr || last_addr >= UINT32_MAX ||
1288e4024630SLaurent Vivier             (!allow_0_address && new_addr == 0)) {
1289315a1350SMichael S. Tsirkin             return PCI_BAR_UNMAPPED;
1290315a1350SMichael S. Tsirkin         }
1291315a1350SMichael S. Tsirkin         return new_addr;
1292315a1350SMichael S. Tsirkin     }
1293315a1350SMichael S. Tsirkin 
1294315a1350SMichael S. Tsirkin     if (!(cmd & PCI_COMMAND_MEMORY)) {
1295315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1296315a1350SMichael S. Tsirkin     }
1297315a1350SMichael S. Tsirkin     if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1298315a1350SMichael S. Tsirkin         new_addr = pci_get_quad(d->config + bar);
1299315a1350SMichael S. Tsirkin     } else {
1300315a1350SMichael S. Tsirkin         new_addr = pci_get_long(d->config + bar);
1301315a1350SMichael S. Tsirkin     }
1302315a1350SMichael S. Tsirkin     /* the ROM slot has a specific enable bit */
1303315a1350SMichael S. Tsirkin     if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
1304315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1305315a1350SMichael S. Tsirkin     }
1306315a1350SMichael S. Tsirkin     new_addr &= ~(size - 1);
1307315a1350SMichael S. Tsirkin     last_addr = new_addr + size - 1;
1308315a1350SMichael S. Tsirkin     /* NOTE: we do not support wrapping */
1309315a1350SMichael S. Tsirkin     /* XXX: as we cannot support really dynamic
1310315a1350SMichael S. Tsirkin        mappings, we handle specific values as invalid
1311315a1350SMichael S. Tsirkin        mappings. */
1312e4024630SLaurent Vivier     if (last_addr <= new_addr || last_addr == PCI_BAR_UNMAPPED ||
1313e4024630SLaurent Vivier         (!allow_0_address && new_addr == 0)) {
1314315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1315315a1350SMichael S. Tsirkin     }
1316315a1350SMichael S. Tsirkin 
1317315a1350SMichael S. Tsirkin     /* Now pcibus_t is 64bit.
1318315a1350SMichael S. Tsirkin      * Check if 32 bit BAR wraps around explicitly.
1319315a1350SMichael S. Tsirkin      * Without this, PC ide doesn't work well.
1320315a1350SMichael S. Tsirkin      * TODO: remove this work around.
1321315a1350SMichael S. Tsirkin      */
1322315a1350SMichael S. Tsirkin     if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
1323315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1324315a1350SMichael S. Tsirkin     }
1325315a1350SMichael S. Tsirkin 
1326315a1350SMichael S. Tsirkin     /*
1327315a1350SMichael S. Tsirkin      * OS is allowed to set BAR beyond its addressable
1328315a1350SMichael S. Tsirkin      * bits. For example, 32 bit OS can set 64bit bar
1329315a1350SMichael S. Tsirkin      * to >4G. Check it. TODO: we might need to support
1330315a1350SMichael S. Tsirkin      * it in the future for e.g. PAE.
1331315a1350SMichael S. Tsirkin      */
1332315a1350SMichael S. Tsirkin     if (last_addr >= HWADDR_MAX) {
1333315a1350SMichael S. Tsirkin         return PCI_BAR_UNMAPPED;
1334315a1350SMichael S. Tsirkin     }
1335315a1350SMichael S. Tsirkin 
1336315a1350SMichael S. Tsirkin     return new_addr;
1337315a1350SMichael S. Tsirkin }
1338315a1350SMichael S. Tsirkin 
1339315a1350SMichael S. Tsirkin static void pci_update_mappings(PCIDevice *d)
1340315a1350SMichael S. Tsirkin {
1341315a1350SMichael S. Tsirkin     PCIIORegion *r;
1342315a1350SMichael S. Tsirkin     int i;
1343315a1350SMichael S. Tsirkin     pcibus_t new_addr;
1344315a1350SMichael S. Tsirkin 
1345315a1350SMichael S. Tsirkin     for(i = 0; i < PCI_NUM_REGIONS; i++) {
1346315a1350SMichael S. Tsirkin         r = &d->io_regions[i];
1347315a1350SMichael S. Tsirkin 
1348315a1350SMichael S. Tsirkin         /* this region isn't registered */
1349315a1350SMichael S. Tsirkin         if (!r->size)
1350315a1350SMichael S. Tsirkin             continue;
1351315a1350SMichael S. Tsirkin 
1352315a1350SMichael S. Tsirkin         new_addr = pci_bar_address(d, i, r->type, r->size);
1353315a1350SMichael S. Tsirkin 
1354315a1350SMichael S. Tsirkin         /* This bar isn't changed */
1355315a1350SMichael S. Tsirkin         if (new_addr == r->addr)
1356315a1350SMichael S. Tsirkin             continue;
1357315a1350SMichael S. Tsirkin 
1358315a1350SMichael S. Tsirkin         /* now do the real mapping */
1359315a1350SMichael S. Tsirkin         if (r->addr != PCI_BAR_UNMAPPED) {
1360fd56e061SDavid Gibson             trace_pci_update_mappings_del(d, pci_dev_bus_num(d),
13617828d750SDon Koch                                           PCI_SLOT(d->devfn),
13620f288f85SLaszlo Ersek                                           PCI_FUNC(d->devfn),
13637828d750SDon Koch                                           i, r->addr, r->size);
1364315a1350SMichael S. Tsirkin             memory_region_del_subregion(r->address_space, r->memory);
1365315a1350SMichael S. Tsirkin         }
1366315a1350SMichael S. Tsirkin         r->addr = new_addr;
1367315a1350SMichael S. Tsirkin         if (r->addr != PCI_BAR_UNMAPPED) {
1368fd56e061SDavid Gibson             trace_pci_update_mappings_add(d, pci_dev_bus_num(d),
13697828d750SDon Koch                                           PCI_SLOT(d->devfn),
13700f288f85SLaszlo Ersek                                           PCI_FUNC(d->devfn),
13717828d750SDon Koch                                           i, r->addr, r->size);
1372315a1350SMichael S. Tsirkin             memory_region_add_subregion_overlap(r->address_space,
1373315a1350SMichael S. Tsirkin                                                 r->addr, r->memory, 1);
1374315a1350SMichael S. Tsirkin         }
1375315a1350SMichael S. Tsirkin     }
1376e01fd687SAlex Williamson 
1377e01fd687SAlex Williamson     pci_update_vga(d);
1378315a1350SMichael S. Tsirkin }
1379315a1350SMichael S. Tsirkin 
1380315a1350SMichael S. Tsirkin static inline int pci_irq_disabled(PCIDevice *d)
1381315a1350SMichael S. Tsirkin {
1382315a1350SMichael S. Tsirkin     return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
1383315a1350SMichael S. Tsirkin }
1384315a1350SMichael S. Tsirkin 
1385315a1350SMichael S. Tsirkin /* Called after interrupt disabled field update in config space,
1386315a1350SMichael S. Tsirkin  * assert/deassert interrupts if necessary.
1387315a1350SMichael S. Tsirkin  * Gets original interrupt disable bit value (before update). */
1388315a1350SMichael S. Tsirkin static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
1389315a1350SMichael S. Tsirkin {
1390315a1350SMichael S. Tsirkin     int i, disabled = pci_irq_disabled(d);
1391315a1350SMichael S. Tsirkin     if (disabled == was_irq_disabled)
1392315a1350SMichael S. Tsirkin         return;
1393315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_PINS; ++i) {
1394315a1350SMichael S. Tsirkin         int state = pci_irq_state(d, i);
1395315a1350SMichael S. Tsirkin         pci_change_irq_level(d, i, disabled ? -state : state);
1396315a1350SMichael S. Tsirkin     }
1397315a1350SMichael S. Tsirkin }
1398315a1350SMichael S. Tsirkin 
1399315a1350SMichael S. Tsirkin uint32_t pci_default_read_config(PCIDevice *d,
1400315a1350SMichael S. Tsirkin                                  uint32_t address, int len)
1401315a1350SMichael S. Tsirkin {
1402315a1350SMichael S. Tsirkin     uint32_t val = 0;
1403315a1350SMichael S. Tsirkin 
1404727b4866SAlex Williamson     if (pci_is_express_downstream_port(d) &&
1405727b4866SAlex Williamson         ranges_overlap(address, len, d->exp.exp_cap + PCI_EXP_LNKSTA, 2)) {
1406727b4866SAlex Williamson         pcie_sync_bridge_lnk(d);
1407727b4866SAlex Williamson     }
1408315a1350SMichael S. Tsirkin     memcpy(&val, d->config + address, len);
1409315a1350SMichael S. Tsirkin     return le32_to_cpu(val);
1410315a1350SMichael S. Tsirkin }
1411315a1350SMichael S. Tsirkin 
1412d7efb7e0SKnut Omang void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
1413315a1350SMichael S. Tsirkin {
1414315a1350SMichael S. Tsirkin     int i, was_irq_disabled = pci_irq_disabled(d);
1415d7efb7e0SKnut Omang     uint32_t val = val_in;
1416315a1350SMichael S. Tsirkin 
1417315a1350SMichael S. Tsirkin     for (i = 0; i < l; val >>= 8, ++i) {
1418315a1350SMichael S. Tsirkin         uint8_t wmask = d->wmask[addr + i];
1419315a1350SMichael S. Tsirkin         uint8_t w1cmask = d->w1cmask[addr + i];
1420315a1350SMichael S. Tsirkin         assert(!(wmask & w1cmask));
1421315a1350SMichael S. Tsirkin         d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
1422315a1350SMichael S. Tsirkin         d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
1423315a1350SMichael S. Tsirkin     }
1424315a1350SMichael S. Tsirkin     if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1425315a1350SMichael S. Tsirkin         ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1426315a1350SMichael S. Tsirkin         ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1427315a1350SMichael S. Tsirkin         range_covers_byte(addr, l, PCI_COMMAND))
1428315a1350SMichael S. Tsirkin         pci_update_mappings(d);
1429315a1350SMichael S. Tsirkin 
1430315a1350SMichael S. Tsirkin     if (range_covers_byte(addr, l, PCI_COMMAND)) {
1431315a1350SMichael S. Tsirkin         pci_update_irq_disabled(d, was_irq_disabled);
1432315a1350SMichael S. Tsirkin         memory_region_set_enabled(&d->bus_master_enable_region,
1433315a1350SMichael S. Tsirkin                                   pci_get_word(d->config + PCI_COMMAND)
1434315a1350SMichael S. Tsirkin                                     & PCI_COMMAND_MASTER);
1435315a1350SMichael S. Tsirkin     }
1436315a1350SMichael S. Tsirkin 
1437d7efb7e0SKnut Omang     msi_write_config(d, addr, val_in, l);
1438d7efb7e0SKnut Omang     msix_write_config(d, addr, val_in, l);
1439315a1350SMichael S. Tsirkin }
1440315a1350SMichael S. Tsirkin 
1441315a1350SMichael S. Tsirkin /***********************************************************/
1442315a1350SMichael S. Tsirkin /* generic PCI irq support */
1443315a1350SMichael S. Tsirkin 
1444315a1350SMichael S. Tsirkin /* 0 <= irq_num <= 3. level must be 0 or 1 */
1445d98f08f5SMarcel Apfelbaum static void pci_irq_handler(void *opaque, int irq_num, int level)
1446315a1350SMichael S. Tsirkin {
1447315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = opaque;
1448315a1350SMichael S. Tsirkin     int change;
1449315a1350SMichael S. Tsirkin 
1450315a1350SMichael S. Tsirkin     change = level - pci_irq_state(pci_dev, irq_num);
1451315a1350SMichael S. Tsirkin     if (!change)
1452315a1350SMichael S. Tsirkin         return;
1453315a1350SMichael S. Tsirkin 
1454315a1350SMichael S. Tsirkin     pci_set_irq_state(pci_dev, irq_num, level);
1455315a1350SMichael S. Tsirkin     pci_update_irq_status(pci_dev);
1456315a1350SMichael S. Tsirkin     if (pci_irq_disabled(pci_dev))
1457315a1350SMichael S. Tsirkin         return;
1458315a1350SMichael S. Tsirkin     pci_change_irq_level(pci_dev, irq_num, change);
1459315a1350SMichael S. Tsirkin }
1460315a1350SMichael S. Tsirkin 
1461d98f08f5SMarcel Apfelbaum static inline int pci_intx(PCIDevice *pci_dev)
1462d98f08f5SMarcel Apfelbaum {
1463d98f08f5SMarcel Apfelbaum     return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
1464d98f08f5SMarcel Apfelbaum }
1465d98f08f5SMarcel Apfelbaum 
1466d98f08f5SMarcel Apfelbaum qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
1467d98f08f5SMarcel Apfelbaum {
1468d98f08f5SMarcel Apfelbaum     int intx = pci_intx(pci_dev);
1469d98f08f5SMarcel Apfelbaum 
1470d98f08f5SMarcel Apfelbaum     return qemu_allocate_irq(pci_irq_handler, pci_dev, intx);
1471d98f08f5SMarcel Apfelbaum }
1472d98f08f5SMarcel Apfelbaum 
1473d98f08f5SMarcel Apfelbaum void pci_set_irq(PCIDevice *pci_dev, int level)
1474d98f08f5SMarcel Apfelbaum {
1475d98f08f5SMarcel Apfelbaum     int intx = pci_intx(pci_dev);
1476d98f08f5SMarcel Apfelbaum     pci_irq_handler(pci_dev, intx, level);
1477d98f08f5SMarcel Apfelbaum }
1478d98f08f5SMarcel Apfelbaum 
1479315a1350SMichael S. Tsirkin /* Special hooks used by device assignment */
1480315a1350SMichael S. Tsirkin void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq)
1481315a1350SMichael S. Tsirkin {
14820889464aSAlex Williamson     assert(pci_bus_is_root(bus));
1483315a1350SMichael S. Tsirkin     bus->route_intx_to_irq = route_intx_to_irq;
1484315a1350SMichael S. Tsirkin }
1485315a1350SMichael S. Tsirkin 
1486315a1350SMichael S. Tsirkin PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin)
1487315a1350SMichael S. Tsirkin {
1488315a1350SMichael S. Tsirkin     PCIBus *bus;
1489315a1350SMichael S. Tsirkin 
1490315a1350SMichael S. Tsirkin     do {
1491fd56e061SDavid Gibson         bus = pci_get_bus(dev);
1492315a1350SMichael S. Tsirkin         pin = bus->map_irq(dev, pin);
1493315a1350SMichael S. Tsirkin         dev = bus->parent_dev;
1494315a1350SMichael S. Tsirkin     } while (dev);
1495315a1350SMichael S. Tsirkin 
1496315a1350SMichael S. Tsirkin     if (!bus->route_intx_to_irq) {
1497312fd5f2SMarkus Armbruster         error_report("PCI: Bug - unimplemented PCI INTx routing (%s)",
1498315a1350SMichael S. Tsirkin                      object_get_typename(OBJECT(bus->qbus.parent)));
1499315a1350SMichael S. Tsirkin         return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 };
1500315a1350SMichael S. Tsirkin     }
1501315a1350SMichael S. Tsirkin 
1502315a1350SMichael S. Tsirkin     return bus->route_intx_to_irq(bus->irq_opaque, pin);
1503315a1350SMichael S. Tsirkin }
1504315a1350SMichael S. Tsirkin 
1505315a1350SMichael S. Tsirkin bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new)
1506315a1350SMichael S. Tsirkin {
1507315a1350SMichael S. Tsirkin     return old->mode != new->mode || old->irq != new->irq;
1508315a1350SMichael S. Tsirkin }
1509315a1350SMichael S. Tsirkin 
1510315a1350SMichael S. Tsirkin void pci_bus_fire_intx_routing_notifier(PCIBus *bus)
1511315a1350SMichael S. Tsirkin {
1512315a1350SMichael S. Tsirkin     PCIDevice *dev;
1513315a1350SMichael S. Tsirkin     PCIBus *sec;
1514315a1350SMichael S. Tsirkin     int i;
1515315a1350SMichael S. Tsirkin 
1516315a1350SMichael S. Tsirkin     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
1517315a1350SMichael S. Tsirkin         dev = bus->devices[i];
1518315a1350SMichael S. Tsirkin         if (dev && dev->intx_routing_notifier) {
1519315a1350SMichael S. Tsirkin             dev->intx_routing_notifier(dev);
1520315a1350SMichael S. Tsirkin         }
1521e5368f0dSAlex Williamson     }
1522e5368f0dSAlex Williamson 
1523315a1350SMichael S. Tsirkin     QLIST_FOREACH(sec, &bus->child, sibling) {
1524315a1350SMichael S. Tsirkin         pci_bus_fire_intx_routing_notifier(sec);
1525315a1350SMichael S. Tsirkin     }
1526315a1350SMichael S. Tsirkin }
1527315a1350SMichael S. Tsirkin 
1528315a1350SMichael S. Tsirkin void pci_device_set_intx_routing_notifier(PCIDevice *dev,
1529315a1350SMichael S. Tsirkin                                           PCIINTxRoutingNotifier notifier)
1530315a1350SMichael S. Tsirkin {
1531315a1350SMichael S. Tsirkin     dev->intx_routing_notifier = notifier;
1532315a1350SMichael S. Tsirkin }
1533315a1350SMichael S. Tsirkin 
1534315a1350SMichael S. Tsirkin /*
1535315a1350SMichael S. Tsirkin  * PCI-to-PCI bridge specification
1536315a1350SMichael S. Tsirkin  * 9.1: Interrupt routing. Table 9-1
1537315a1350SMichael S. Tsirkin  *
1538315a1350SMichael S. Tsirkin  * the PCI Express Base Specification, Revision 2.1
1539315a1350SMichael S. Tsirkin  * 2.2.8.1: INTx interrutp signaling - Rules
1540315a1350SMichael S. Tsirkin  *          the Implementation Note
1541315a1350SMichael S. Tsirkin  *          Table 2-20
1542315a1350SMichael S. Tsirkin  */
1543315a1350SMichael S. Tsirkin /*
1544315a1350SMichael S. Tsirkin  * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD
1545315a1350SMichael S. Tsirkin  * 0-origin unlike PCI interrupt pin register.
1546315a1350SMichael S. Tsirkin  */
1547315a1350SMichael S. Tsirkin int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
1548315a1350SMichael S. Tsirkin {
1549e8ec4adfSGreg Kurz     return pci_swizzle(PCI_SLOT(pci_dev->devfn), pin);
1550315a1350SMichael S. Tsirkin }
1551315a1350SMichael S. Tsirkin 
1552315a1350SMichael S. Tsirkin /***********************************************************/
1553315a1350SMichael S. Tsirkin /* monitor info on PCI */
1554315a1350SMichael S. Tsirkin 
1555315a1350SMichael S. Tsirkin typedef struct {
1556315a1350SMichael S. Tsirkin     uint16_t class;
1557315a1350SMichael S. Tsirkin     const char *desc;
1558315a1350SMichael S. Tsirkin     const char *fw_name;
1559315a1350SMichael S. Tsirkin     uint16_t fw_ign_bits;
1560315a1350SMichael S. Tsirkin } pci_class_desc;
1561315a1350SMichael S. Tsirkin 
1562315a1350SMichael S. Tsirkin static const pci_class_desc pci_class_descriptions[] =
1563315a1350SMichael S. Tsirkin {
1564315a1350SMichael S. Tsirkin     { 0x0001, "VGA controller", "display"},
1565315a1350SMichael S. Tsirkin     { 0x0100, "SCSI controller", "scsi"},
1566315a1350SMichael S. Tsirkin     { 0x0101, "IDE controller", "ide"},
1567315a1350SMichael S. Tsirkin     { 0x0102, "Floppy controller", "fdc"},
1568315a1350SMichael S. Tsirkin     { 0x0103, "IPI controller", "ipi"},
1569315a1350SMichael S. Tsirkin     { 0x0104, "RAID controller", "raid"},
1570315a1350SMichael S. Tsirkin     { 0x0106, "SATA controller"},
1571315a1350SMichael S. Tsirkin     { 0x0107, "SAS controller"},
1572315a1350SMichael S. Tsirkin     { 0x0180, "Storage controller"},
1573315a1350SMichael S. Tsirkin     { 0x0200, "Ethernet controller", "ethernet"},
1574315a1350SMichael S. Tsirkin     { 0x0201, "Token Ring controller", "token-ring"},
1575315a1350SMichael S. Tsirkin     { 0x0202, "FDDI controller", "fddi"},
1576315a1350SMichael S. Tsirkin     { 0x0203, "ATM controller", "atm"},
1577315a1350SMichael S. Tsirkin     { 0x0280, "Network controller"},
1578315a1350SMichael S. Tsirkin     { 0x0300, "VGA controller", "display", 0x00ff},
1579315a1350SMichael S. Tsirkin     { 0x0301, "XGA controller"},
1580315a1350SMichael S. Tsirkin     { 0x0302, "3D controller"},
1581315a1350SMichael S. Tsirkin     { 0x0380, "Display controller"},
1582315a1350SMichael S. Tsirkin     { 0x0400, "Video controller", "video"},
1583315a1350SMichael S. Tsirkin     { 0x0401, "Audio controller", "sound"},
1584315a1350SMichael S. Tsirkin     { 0x0402, "Phone"},
1585315a1350SMichael S. Tsirkin     { 0x0403, "Audio controller", "sound"},
1586315a1350SMichael S. Tsirkin     { 0x0480, "Multimedia controller"},
1587315a1350SMichael S. Tsirkin     { 0x0500, "RAM controller", "memory"},
1588315a1350SMichael S. Tsirkin     { 0x0501, "Flash controller", "flash"},
1589315a1350SMichael S. Tsirkin     { 0x0580, "Memory controller"},
1590315a1350SMichael S. Tsirkin     { 0x0600, "Host bridge", "host"},
1591315a1350SMichael S. Tsirkin     { 0x0601, "ISA bridge", "isa"},
1592315a1350SMichael S. Tsirkin     { 0x0602, "EISA bridge", "eisa"},
1593315a1350SMichael S. Tsirkin     { 0x0603, "MC bridge", "mca"},
15944c41425dSGerd Hoffmann     { 0x0604, "PCI bridge", "pci-bridge"},
1595315a1350SMichael S. Tsirkin     { 0x0605, "PCMCIA bridge", "pcmcia"},
1596315a1350SMichael S. Tsirkin     { 0x0606, "NUBUS bridge", "nubus"},
1597315a1350SMichael S. Tsirkin     { 0x0607, "CARDBUS bridge", "cardbus"},
1598315a1350SMichael S. Tsirkin     { 0x0608, "RACEWAY bridge"},
1599315a1350SMichael S. Tsirkin     { 0x0680, "Bridge"},
1600315a1350SMichael S. Tsirkin     { 0x0700, "Serial port", "serial"},
1601315a1350SMichael S. Tsirkin     { 0x0701, "Parallel port", "parallel"},
1602315a1350SMichael S. Tsirkin     { 0x0800, "Interrupt controller", "interrupt-controller"},
1603315a1350SMichael S. Tsirkin     { 0x0801, "DMA controller", "dma-controller"},
1604315a1350SMichael S. Tsirkin     { 0x0802, "Timer", "timer"},
1605315a1350SMichael S. Tsirkin     { 0x0803, "RTC", "rtc"},
1606315a1350SMichael S. Tsirkin     { 0x0900, "Keyboard", "keyboard"},
1607315a1350SMichael S. Tsirkin     { 0x0901, "Pen", "pen"},
1608315a1350SMichael S. Tsirkin     { 0x0902, "Mouse", "mouse"},
1609315a1350SMichael S. Tsirkin     { 0x0A00, "Dock station", "dock", 0x00ff},
1610315a1350SMichael S. Tsirkin     { 0x0B00, "i386 cpu", "cpu", 0x00ff},
1611315a1350SMichael S. Tsirkin     { 0x0c00, "Fireware contorller", "fireware"},
1612315a1350SMichael S. Tsirkin     { 0x0c01, "Access bus controller", "access-bus"},
1613315a1350SMichael S. Tsirkin     { 0x0c02, "SSA controller", "ssa"},
1614315a1350SMichael S. Tsirkin     { 0x0c03, "USB controller", "usb"},
1615315a1350SMichael S. Tsirkin     { 0x0c04, "Fibre channel controller", "fibre-channel"},
1616315a1350SMichael S. Tsirkin     { 0x0c05, "SMBus"},
1617315a1350SMichael S. Tsirkin     { 0, NULL}
1618315a1350SMichael S. Tsirkin };
1619315a1350SMichael S. Tsirkin 
1620a8eeafdaSGreg Kurz static void pci_for_each_device_under_bus_reverse(PCIBus *bus,
1621a8eeafdaSGreg Kurz                                                   void (*fn)(PCIBus *b,
1622a8eeafdaSGreg Kurz                                                              PCIDevice *d,
1623a8eeafdaSGreg Kurz                                                              void *opaque),
1624a8eeafdaSGreg Kurz                                                   void *opaque)
1625a8eeafdaSGreg Kurz {
1626a8eeafdaSGreg Kurz     PCIDevice *d;
1627a8eeafdaSGreg Kurz     int devfn;
1628a8eeafdaSGreg Kurz 
1629a8eeafdaSGreg Kurz     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1630a8eeafdaSGreg Kurz         d = bus->devices[ARRAY_SIZE(bus->devices) - 1 - devfn];
1631a8eeafdaSGreg Kurz         if (d) {
1632a8eeafdaSGreg Kurz             fn(bus, d, opaque);
1633a8eeafdaSGreg Kurz         }
1634a8eeafdaSGreg Kurz     }
1635a8eeafdaSGreg Kurz }
1636a8eeafdaSGreg Kurz 
1637a8eeafdaSGreg Kurz void pci_for_each_device_reverse(PCIBus *bus, int bus_num,
1638a8eeafdaSGreg Kurz                          void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
1639a8eeafdaSGreg Kurz                          void *opaque)
1640a8eeafdaSGreg Kurz {
1641a8eeafdaSGreg Kurz     bus = pci_find_bus_nr(bus, bus_num);
1642a8eeafdaSGreg Kurz 
1643a8eeafdaSGreg Kurz     if (bus) {
1644a8eeafdaSGreg Kurz         pci_for_each_device_under_bus_reverse(bus, fn, opaque);
1645a8eeafdaSGreg Kurz     }
1646a8eeafdaSGreg Kurz }
1647a8eeafdaSGreg Kurz 
1648315a1350SMichael S. Tsirkin static void pci_for_each_device_under_bus(PCIBus *bus,
1649315a1350SMichael S. Tsirkin                                           void (*fn)(PCIBus *b, PCIDevice *d,
1650315a1350SMichael S. Tsirkin                                                      void *opaque),
1651315a1350SMichael S. Tsirkin                                           void *opaque)
1652315a1350SMichael S. Tsirkin {
1653315a1350SMichael S. Tsirkin     PCIDevice *d;
1654315a1350SMichael S. Tsirkin     int devfn;
1655315a1350SMichael S. Tsirkin 
1656315a1350SMichael S. Tsirkin     for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1657315a1350SMichael S. Tsirkin         d = bus->devices[devfn];
1658315a1350SMichael S. Tsirkin         if (d) {
1659315a1350SMichael S. Tsirkin             fn(bus, d, opaque);
1660315a1350SMichael S. Tsirkin         }
1661315a1350SMichael S. Tsirkin     }
1662315a1350SMichael S. Tsirkin }
1663315a1350SMichael S. Tsirkin 
1664315a1350SMichael S. Tsirkin void pci_for_each_device(PCIBus *bus, int bus_num,
1665315a1350SMichael S. Tsirkin                          void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
1666315a1350SMichael S. Tsirkin                          void *opaque)
1667315a1350SMichael S. Tsirkin {
1668315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
1669315a1350SMichael S. Tsirkin 
1670315a1350SMichael S. Tsirkin     if (bus) {
1671315a1350SMichael S. Tsirkin         pci_for_each_device_under_bus(bus, fn, opaque);
1672315a1350SMichael S. Tsirkin     }
1673315a1350SMichael S. Tsirkin }
1674315a1350SMichael S. Tsirkin 
1675315a1350SMichael S. Tsirkin static const pci_class_desc *get_class_desc(int class)
1676315a1350SMichael S. Tsirkin {
1677315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
1678315a1350SMichael S. Tsirkin 
1679315a1350SMichael S. Tsirkin     desc = pci_class_descriptions;
1680315a1350SMichael S. Tsirkin     while (desc->desc && class != desc->class) {
1681315a1350SMichael S. Tsirkin         desc++;
1682315a1350SMichael S. Tsirkin     }
1683315a1350SMichael S. Tsirkin 
1684315a1350SMichael S. Tsirkin     return desc;
1685315a1350SMichael S. Tsirkin }
1686315a1350SMichael S. Tsirkin 
1687315a1350SMichael S. Tsirkin static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
1688315a1350SMichael S. Tsirkin 
1689315a1350SMichael S. Tsirkin static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
1690315a1350SMichael S. Tsirkin {
1691315a1350SMichael S. Tsirkin     PciMemoryRegionList *head = NULL, *cur_item = NULL;
1692315a1350SMichael S. Tsirkin     int i;
1693315a1350SMichael S. Tsirkin 
1694315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; i++) {
1695315a1350SMichael S. Tsirkin         const PCIIORegion *r = &dev->io_regions[i];
1696315a1350SMichael S. Tsirkin         PciMemoryRegionList *region;
1697315a1350SMichael S. Tsirkin 
1698315a1350SMichael S. Tsirkin         if (!r->size) {
1699315a1350SMichael S. Tsirkin             continue;
1700315a1350SMichael S. Tsirkin         }
1701315a1350SMichael S. Tsirkin 
1702315a1350SMichael S. Tsirkin         region = g_malloc0(sizeof(*region));
1703315a1350SMichael S. Tsirkin         region->value = g_malloc0(sizeof(*region->value));
1704315a1350SMichael S. Tsirkin 
1705315a1350SMichael S. Tsirkin         if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1706315a1350SMichael S. Tsirkin             region->value->type = g_strdup("io");
1707315a1350SMichael S. Tsirkin         } else {
1708315a1350SMichael S. Tsirkin             region->value->type = g_strdup("memory");
1709315a1350SMichael S. Tsirkin             region->value->has_prefetch = true;
1710315a1350SMichael S. Tsirkin             region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
1711315a1350SMichael S. Tsirkin             region->value->has_mem_type_64 = true;
1712315a1350SMichael S. Tsirkin             region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
1713315a1350SMichael S. Tsirkin         }
1714315a1350SMichael S. Tsirkin 
1715315a1350SMichael S. Tsirkin         region->value->bar = i;
1716315a1350SMichael S. Tsirkin         region->value->address = r->addr;
1717315a1350SMichael S. Tsirkin         region->value->size = r->size;
1718315a1350SMichael S. Tsirkin 
1719315a1350SMichael S. Tsirkin         /* XXX: waiting for the qapi to support GSList */
1720315a1350SMichael S. Tsirkin         if (!cur_item) {
1721315a1350SMichael S. Tsirkin             head = cur_item = region;
1722315a1350SMichael S. Tsirkin         } else {
1723315a1350SMichael S. Tsirkin             cur_item->next = region;
1724315a1350SMichael S. Tsirkin             cur_item = region;
1725315a1350SMichael S. Tsirkin         }
1726315a1350SMichael S. Tsirkin     }
1727315a1350SMichael S. Tsirkin 
1728315a1350SMichael S. Tsirkin     return head;
1729315a1350SMichael S. Tsirkin }
1730315a1350SMichael S. Tsirkin 
1731315a1350SMichael S. Tsirkin static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
1732315a1350SMichael S. Tsirkin                                            int bus_num)
1733315a1350SMichael S. Tsirkin {
1734315a1350SMichael S. Tsirkin     PciBridgeInfo *info;
17359fa02cd1SEric Blake     PciMemoryRange *range;
1736315a1350SMichael S. Tsirkin 
17379fa02cd1SEric Blake     info = g_new0(PciBridgeInfo, 1);
1738315a1350SMichael S. Tsirkin 
17399fa02cd1SEric Blake     info->bus = g_new0(PciBusInfo, 1);
17409fa02cd1SEric Blake     info->bus->number = dev->config[PCI_PRIMARY_BUS];
17419fa02cd1SEric Blake     info->bus->secondary = dev->config[PCI_SECONDARY_BUS];
17429fa02cd1SEric Blake     info->bus->subordinate = dev->config[PCI_SUBORDINATE_BUS];
1743315a1350SMichael S. Tsirkin 
17449fa02cd1SEric Blake     range = info->bus->io_range = g_new0(PciMemoryRange, 1);
17459fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
17469fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
1747315a1350SMichael S. Tsirkin 
17489fa02cd1SEric Blake     range = info->bus->memory_range = g_new0(PciMemoryRange, 1);
17499fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
17509fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
1751315a1350SMichael S. Tsirkin 
17529fa02cd1SEric Blake     range = info->bus->prefetchable_range = g_new0(PciMemoryRange, 1);
17539fa02cd1SEric Blake     range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
17549fa02cd1SEric Blake     range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
1755315a1350SMichael S. Tsirkin 
1756315a1350SMichael S. Tsirkin     if (dev->config[PCI_SECONDARY_BUS] != 0) {
1757315a1350SMichael S. Tsirkin         PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]);
1758315a1350SMichael S. Tsirkin         if (child_bus) {
1759315a1350SMichael S. Tsirkin             info->has_devices = true;
1760315a1350SMichael S. Tsirkin             info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]);
1761315a1350SMichael S. Tsirkin         }
1762315a1350SMichael S. Tsirkin     }
1763315a1350SMichael S. Tsirkin 
1764315a1350SMichael S. Tsirkin     return info;
1765315a1350SMichael S. Tsirkin }
1766315a1350SMichael S. Tsirkin 
1767315a1350SMichael S. Tsirkin static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
1768315a1350SMichael S. Tsirkin                                            int bus_num)
1769315a1350SMichael S. Tsirkin {
1770315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
1771315a1350SMichael S. Tsirkin     PciDeviceInfo *info;
1772315a1350SMichael S. Tsirkin     uint8_t type;
1773315a1350SMichael S. Tsirkin     int class;
1774315a1350SMichael S. Tsirkin 
17759fa02cd1SEric Blake     info = g_new0(PciDeviceInfo, 1);
1776315a1350SMichael S. Tsirkin     info->bus = bus_num;
1777315a1350SMichael S. Tsirkin     info->slot = PCI_SLOT(dev->devfn);
1778315a1350SMichael S. Tsirkin     info->function = PCI_FUNC(dev->devfn);
1779315a1350SMichael S. Tsirkin 
17809fa02cd1SEric Blake     info->class_info = g_new0(PciDeviceClass, 1);
1781315a1350SMichael S. Tsirkin     class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
17829fa02cd1SEric Blake     info->class_info->q_class = class;
1783315a1350SMichael S. Tsirkin     desc = get_class_desc(class);
1784315a1350SMichael S. Tsirkin     if (desc->desc) {
17859fa02cd1SEric Blake         info->class_info->has_desc = true;
17869fa02cd1SEric Blake         info->class_info->desc = g_strdup(desc->desc);
1787315a1350SMichael S. Tsirkin     }
1788315a1350SMichael S. Tsirkin 
17899fa02cd1SEric Blake     info->id = g_new0(PciDeviceId, 1);
17909fa02cd1SEric Blake     info->id->vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
17919fa02cd1SEric Blake     info->id->device = pci_get_word(dev->config + PCI_DEVICE_ID);
1792315a1350SMichael S. Tsirkin     info->regions = qmp_query_pci_regions(dev);
1793315a1350SMichael S. Tsirkin     info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
1794315a1350SMichael S. Tsirkin 
1795315a1350SMichael S. Tsirkin     if (dev->config[PCI_INTERRUPT_PIN] != 0) {
1796315a1350SMichael S. Tsirkin         info->has_irq = true;
1797315a1350SMichael S. Tsirkin         info->irq = dev->config[PCI_INTERRUPT_LINE];
1798315a1350SMichael S. Tsirkin     }
1799315a1350SMichael S. Tsirkin 
1800315a1350SMichael S. Tsirkin     type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1801315a1350SMichael S. Tsirkin     if (type == PCI_HEADER_TYPE_BRIDGE) {
1802315a1350SMichael S. Tsirkin         info->has_pci_bridge = true;
1803315a1350SMichael S. Tsirkin         info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
180418613dc6SDenis V. Lunev     } else if (type == PCI_HEADER_TYPE_NORMAL) {
180518613dc6SDenis V. Lunev         info->id->has_subsystem = info->id->has_subsystem_vendor = true;
180618613dc6SDenis V. Lunev         info->id->subsystem = pci_get_word(dev->config + PCI_SUBSYSTEM_ID);
180718613dc6SDenis V. Lunev         info->id->subsystem_vendor =
180818613dc6SDenis V. Lunev             pci_get_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID);
180918613dc6SDenis V. Lunev     } else if (type == PCI_HEADER_TYPE_CARDBUS) {
181018613dc6SDenis V. Lunev         info->id->has_subsystem = info->id->has_subsystem_vendor = true;
181118613dc6SDenis V. Lunev         info->id->subsystem = pci_get_word(dev->config + PCI_CB_SUBSYSTEM_ID);
181218613dc6SDenis V. Lunev         info->id->subsystem_vendor =
181318613dc6SDenis V. Lunev             pci_get_word(dev->config + PCI_CB_SUBSYSTEM_VENDOR_ID);
1814315a1350SMichael S. Tsirkin     }
1815315a1350SMichael S. Tsirkin 
1816315a1350SMichael S. Tsirkin     return info;
1817315a1350SMichael S. Tsirkin }
1818315a1350SMichael S. Tsirkin 
1819315a1350SMichael S. Tsirkin static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
1820315a1350SMichael S. Tsirkin {
1821315a1350SMichael S. Tsirkin     PciDeviceInfoList *info, *head = NULL, *cur_item = NULL;
1822315a1350SMichael S. Tsirkin     PCIDevice *dev;
1823315a1350SMichael S. Tsirkin     int devfn;
1824315a1350SMichael S. Tsirkin 
1825315a1350SMichael S. Tsirkin     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1826315a1350SMichael S. Tsirkin         dev = bus->devices[devfn];
1827315a1350SMichael S. Tsirkin         if (dev) {
1828315a1350SMichael S. Tsirkin             info = g_malloc0(sizeof(*info));
1829315a1350SMichael S. Tsirkin             info->value = qmp_query_pci_device(dev, bus, bus_num);
1830315a1350SMichael S. Tsirkin 
1831315a1350SMichael S. Tsirkin             /* XXX: waiting for the qapi to support GSList */
1832315a1350SMichael S. Tsirkin             if (!cur_item) {
1833315a1350SMichael S. Tsirkin                 head = cur_item = info;
1834315a1350SMichael S. Tsirkin             } else {
1835315a1350SMichael S. Tsirkin                 cur_item->next = info;
1836315a1350SMichael S. Tsirkin                 cur_item = info;
1837315a1350SMichael S. Tsirkin             }
1838315a1350SMichael S. Tsirkin         }
1839315a1350SMichael S. Tsirkin     }
1840315a1350SMichael S. Tsirkin 
1841315a1350SMichael S. Tsirkin     return head;
1842315a1350SMichael S. Tsirkin }
1843315a1350SMichael S. Tsirkin 
1844315a1350SMichael S. Tsirkin static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
1845315a1350SMichael S. Tsirkin {
1846315a1350SMichael S. Tsirkin     PciInfo *info = NULL;
1847315a1350SMichael S. Tsirkin 
1848315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
1849315a1350SMichael S. Tsirkin     if (bus) {
1850315a1350SMichael S. Tsirkin         info = g_malloc0(sizeof(*info));
1851315a1350SMichael S. Tsirkin         info->bus = bus_num;
1852315a1350SMichael S. Tsirkin         info->devices = qmp_query_pci_devices(bus, bus_num);
1853315a1350SMichael S. Tsirkin     }
1854315a1350SMichael S. Tsirkin 
1855315a1350SMichael S. Tsirkin     return info;
1856315a1350SMichael S. Tsirkin }
1857315a1350SMichael S. Tsirkin 
1858315a1350SMichael S. Tsirkin PciInfoList *qmp_query_pci(Error **errp)
1859315a1350SMichael S. Tsirkin {
1860315a1350SMichael S. Tsirkin     PciInfoList *info, *head = NULL, *cur_item = NULL;
18617588e2b0SDavid Gibson     PCIHostState *host_bridge;
1862315a1350SMichael S. Tsirkin 
18637588e2b0SDavid Gibson     QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
1864315a1350SMichael S. Tsirkin         info = g_malloc0(sizeof(*info));
1865cb2ed8b3SMarcel Apfelbaum         info->value = qmp_query_pci_bus(host_bridge->bus,
1866cb2ed8b3SMarcel Apfelbaum                                         pci_bus_num(host_bridge->bus));
1867315a1350SMichael S. Tsirkin 
1868315a1350SMichael S. Tsirkin         /* XXX: waiting for the qapi to support GSList */
1869315a1350SMichael S. Tsirkin         if (!cur_item) {
1870315a1350SMichael S. Tsirkin             head = cur_item = info;
1871315a1350SMichael S. Tsirkin         } else {
1872315a1350SMichael S. Tsirkin             cur_item->next = info;
1873315a1350SMichael S. Tsirkin             cur_item = info;
1874315a1350SMichael S. Tsirkin         }
1875315a1350SMichael S. Tsirkin     }
1876315a1350SMichael S. Tsirkin 
1877315a1350SMichael S. Tsirkin     return head;
1878315a1350SMichael S. Tsirkin }
1879315a1350SMichael S. Tsirkin 
1880315a1350SMichael S. Tsirkin /* Initialize a PCI NIC.  */
188151f7cb97SThomas Huth PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
188229b358f9SDavid Gibson                                const char *default_model,
188351f7cb97SThomas Huth                                const char *default_devaddr)
1884315a1350SMichael S. Tsirkin {
1885315a1350SMichael S. Tsirkin     const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
188652310c3fSPaolo Bonzini     GSList *list;
188752310c3fSPaolo Bonzini     GPtrArray *pci_nic_models;
1888315a1350SMichael S. Tsirkin     PCIBus *bus;
1889315a1350SMichael S. Tsirkin     PCIDevice *pci_dev;
1890315a1350SMichael S. Tsirkin     DeviceState *dev;
189151f7cb97SThomas Huth     int devfn;
1892315a1350SMichael S. Tsirkin     int i;
1893315a1350SMichael S. Tsirkin 
189452310c3fSPaolo Bonzini     if (nd->model && !strcmp(nd->model, "virtio")) {
189552310c3fSPaolo Bonzini         g_free(nd->model);
189652310c3fSPaolo Bonzini         nd->model = g_strdup("virtio-net-pci");
189752310c3fSPaolo Bonzini     }
189852310c3fSPaolo Bonzini 
189952310c3fSPaolo Bonzini     list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
190052310c3fSPaolo Bonzini     pci_nic_models = g_ptr_array_new();
190152310c3fSPaolo Bonzini     while (list) {
190252310c3fSPaolo Bonzini         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
190352310c3fSPaolo Bonzini                                              TYPE_DEVICE);
190452310c3fSPaolo Bonzini         GSList *next;
190552310c3fSPaolo Bonzini         if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
190652310c3fSPaolo Bonzini             dc->user_creatable) {
190752310c3fSPaolo Bonzini             const char *name = object_class_get_name(list->data);
190852310c3fSPaolo Bonzini             g_ptr_array_add(pci_nic_models, (gpointer)name);
190952310c3fSPaolo Bonzini         }
191052310c3fSPaolo Bonzini         next = list->next;
191152310c3fSPaolo Bonzini         g_slist_free_1(list);
191252310c3fSPaolo Bonzini         list = next;
191352310c3fSPaolo Bonzini     }
191452310c3fSPaolo Bonzini     g_ptr_array_add(pci_nic_models, NULL);
191552310c3fSPaolo Bonzini 
191652310c3fSPaolo Bonzini     if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) {
191751f7cb97SThomas Huth         exit(0);
191851f7cb97SThomas Huth     }
191951f7cb97SThomas Huth 
192052310c3fSPaolo Bonzini     i = qemu_find_nic_model(nd, (const char **)pci_nic_models->pdata,
192152310c3fSPaolo Bonzini                             default_model);
192251f7cb97SThomas Huth     if (i < 0) {
192351f7cb97SThomas Huth         exit(1);
192451f7cb97SThomas Huth     }
1925315a1350SMichael S. Tsirkin 
192629b358f9SDavid Gibson     bus = pci_get_bus_devfn(&devfn, rootbus, devaddr);
1927315a1350SMichael S. Tsirkin     if (!bus) {
1928315a1350SMichael S. Tsirkin         error_report("Invalid PCI device address %s for device %s",
192952310c3fSPaolo Bonzini                      devaddr, nd->model);
193051f7cb97SThomas Huth         exit(1);
1931315a1350SMichael S. Tsirkin     }
1932315a1350SMichael S. Tsirkin 
193352310c3fSPaolo Bonzini     pci_dev = pci_create(bus, devfn, nd->model);
1934315a1350SMichael S. Tsirkin     dev = &pci_dev->qdev;
1935315a1350SMichael S. Tsirkin     qdev_set_nic_properties(dev, nd);
1936a023b7acSAlex Kompel     qdev_init_nofail(dev);
193752310c3fSPaolo Bonzini     g_ptr_array_free(pci_nic_models, true);
193851f7cb97SThomas Huth     return pci_dev;
1939315a1350SMichael S. Tsirkin }
1940315a1350SMichael S. Tsirkin 
1941315a1350SMichael S. Tsirkin PCIDevice *pci_vga_init(PCIBus *bus)
1942315a1350SMichael S. Tsirkin {
1943315a1350SMichael S. Tsirkin     switch (vga_interface_type) {
1944315a1350SMichael S. Tsirkin     case VGA_CIRRUS:
1945315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "cirrus-vga");
1946315a1350SMichael S. Tsirkin     case VGA_QXL:
1947315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "qxl-vga");
1948315a1350SMichael S. Tsirkin     case VGA_STD:
1949315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "VGA");
1950315a1350SMichael S. Tsirkin     case VGA_VMWARE:
1951315a1350SMichael S. Tsirkin         return pci_create_simple(bus, -1, "vmware-svga");
1952a94f0c5cSGerd Hoffmann     case VGA_VIRTIO:
1953a94f0c5cSGerd Hoffmann         return pci_create_simple(bus, -1, "virtio-vga");
1954315a1350SMichael S. Tsirkin     case VGA_NONE:
1955315a1350SMichael S. Tsirkin     default: /* Other non-PCI types. Checking for unsupported types is already
1956315a1350SMichael S. Tsirkin                 done in vl.c. */
1957315a1350SMichael S. Tsirkin         return NULL;
1958315a1350SMichael S. Tsirkin     }
1959315a1350SMichael S. Tsirkin }
1960315a1350SMichael S. Tsirkin 
1961315a1350SMichael S. Tsirkin /* Whether a given bus number is in range of the secondary
1962315a1350SMichael S. Tsirkin  * bus of the given bridge device. */
1963315a1350SMichael S. Tsirkin static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
1964315a1350SMichael S. Tsirkin {
1965315a1350SMichael S. Tsirkin     return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
1966315a1350SMichael S. Tsirkin              PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
196709e5b819SMarcel Apfelbaum         dev->config[PCI_SECONDARY_BUS] <= bus_num &&
1968315a1350SMichael S. Tsirkin         bus_num <= dev->config[PCI_SUBORDINATE_BUS];
1969315a1350SMichael S. Tsirkin }
1970315a1350SMichael S. Tsirkin 
197109e5b819SMarcel Apfelbaum /* Whether a given bus number is in a range of a root bus */
197209e5b819SMarcel Apfelbaum static bool pci_root_bus_in_range(PCIBus *bus, int bus_num)
197309e5b819SMarcel Apfelbaum {
197409e5b819SMarcel Apfelbaum     int i;
197509e5b819SMarcel Apfelbaum 
197609e5b819SMarcel Apfelbaum     for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
197709e5b819SMarcel Apfelbaum         PCIDevice *dev = bus->devices[i];
197809e5b819SMarcel Apfelbaum 
197909e5b819SMarcel Apfelbaum         if (dev && PCI_DEVICE_GET_CLASS(dev)->is_bridge) {
198009e5b819SMarcel Apfelbaum             if (pci_secondary_bus_in_range(dev, bus_num)) {
198109e5b819SMarcel Apfelbaum                 return true;
198209e5b819SMarcel Apfelbaum             }
198309e5b819SMarcel Apfelbaum         }
198409e5b819SMarcel Apfelbaum     }
198509e5b819SMarcel Apfelbaum 
198609e5b819SMarcel Apfelbaum     return false;
198709e5b819SMarcel Apfelbaum }
198809e5b819SMarcel Apfelbaum 
1989315a1350SMichael S. Tsirkin static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
1990315a1350SMichael S. Tsirkin {
1991315a1350SMichael S. Tsirkin     PCIBus *sec;
1992315a1350SMichael S. Tsirkin 
1993315a1350SMichael S. Tsirkin     if (!bus) {
1994315a1350SMichael S. Tsirkin         return NULL;
1995315a1350SMichael S. Tsirkin     }
1996315a1350SMichael S. Tsirkin 
1997315a1350SMichael S. Tsirkin     if (pci_bus_num(bus) == bus_num) {
1998315a1350SMichael S. Tsirkin         return bus;
1999315a1350SMichael S. Tsirkin     }
2000315a1350SMichael S. Tsirkin 
2001315a1350SMichael S. Tsirkin     /* Consider all bus numbers in range for the host pci bridge. */
20020889464aSAlex Williamson     if (!pci_bus_is_root(bus) &&
2003315a1350SMichael S. Tsirkin         !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
2004315a1350SMichael S. Tsirkin         return NULL;
2005315a1350SMichael S. Tsirkin     }
2006315a1350SMichael S. Tsirkin 
2007315a1350SMichael S. Tsirkin     /* try child bus */
2008315a1350SMichael S. Tsirkin     for (; bus; bus = sec) {
2009315a1350SMichael S. Tsirkin         QLIST_FOREACH(sec, &bus->child, sibling) {
201009e5b819SMarcel Apfelbaum             if (pci_bus_num(sec) == bus_num) {
2011315a1350SMichael S. Tsirkin                 return sec;
2012315a1350SMichael S. Tsirkin             }
201309e5b819SMarcel Apfelbaum             /* PXB buses assumed to be children of bus 0 */
201409e5b819SMarcel Apfelbaum             if (pci_bus_is_root(sec)) {
201509e5b819SMarcel Apfelbaum                 if (pci_root_bus_in_range(sec, bus_num)) {
201609e5b819SMarcel Apfelbaum                     break;
201709e5b819SMarcel Apfelbaum                 }
201809e5b819SMarcel Apfelbaum             } else {
2019315a1350SMichael S. Tsirkin                 if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
2020315a1350SMichael S. Tsirkin                     break;
2021315a1350SMichael S. Tsirkin                 }
2022315a1350SMichael S. Tsirkin             }
2023315a1350SMichael S. Tsirkin         }
202409e5b819SMarcel Apfelbaum     }
2025315a1350SMichael S. Tsirkin 
2026315a1350SMichael S. Tsirkin     return NULL;
2027315a1350SMichael S. Tsirkin }
2028315a1350SMichael S. Tsirkin 
2029eb0acfddSMichael S. Tsirkin void pci_for_each_bus_depth_first(PCIBus *bus,
2030eb0acfddSMichael S. Tsirkin                                   void *(*begin)(PCIBus *bus, void *parent_state),
2031eb0acfddSMichael S. Tsirkin                                   void (*end)(PCIBus *bus, void *state),
2032eb0acfddSMichael S. Tsirkin                                   void *parent_state)
2033eb0acfddSMichael S. Tsirkin {
2034eb0acfddSMichael S. Tsirkin     PCIBus *sec;
2035eb0acfddSMichael S. Tsirkin     void *state;
2036eb0acfddSMichael S. Tsirkin 
2037eb0acfddSMichael S. Tsirkin     if (!bus) {
2038eb0acfddSMichael S. Tsirkin         return;
2039eb0acfddSMichael S. Tsirkin     }
2040eb0acfddSMichael S. Tsirkin 
2041eb0acfddSMichael S. Tsirkin     if (begin) {
2042eb0acfddSMichael S. Tsirkin         state = begin(bus, parent_state);
2043eb0acfddSMichael S. Tsirkin     } else {
2044eb0acfddSMichael S. Tsirkin         state = parent_state;
2045eb0acfddSMichael S. Tsirkin     }
2046eb0acfddSMichael S. Tsirkin 
2047eb0acfddSMichael S. Tsirkin     QLIST_FOREACH(sec, &bus->child, sibling) {
2048eb0acfddSMichael S. Tsirkin         pci_for_each_bus_depth_first(sec, begin, end, state);
2049eb0acfddSMichael S. Tsirkin     }
2050eb0acfddSMichael S. Tsirkin 
2051eb0acfddSMichael S. Tsirkin     if (end) {
2052eb0acfddSMichael S. Tsirkin         end(bus, state);
2053eb0acfddSMichael S. Tsirkin     }
2054eb0acfddSMichael S. Tsirkin }
2055eb0acfddSMichael S. Tsirkin 
2056eb0acfddSMichael S. Tsirkin 
2057315a1350SMichael S. Tsirkin PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
2058315a1350SMichael S. Tsirkin {
2059315a1350SMichael S. Tsirkin     bus = pci_find_bus_nr(bus, bus_num);
2060315a1350SMichael S. Tsirkin 
2061315a1350SMichael S. Tsirkin     if (!bus)
2062315a1350SMichael S. Tsirkin         return NULL;
2063315a1350SMichael S. Tsirkin 
2064315a1350SMichael S. Tsirkin     return bus->devices[devfn];
2065315a1350SMichael S. Tsirkin }
2066315a1350SMichael S. Tsirkin 
2067133e9b22SMarkus Armbruster static void pci_qdev_realize(DeviceState *qdev, Error **errp)
2068315a1350SMichael S. Tsirkin {
2069315a1350SMichael S. Tsirkin     PCIDevice *pci_dev = (PCIDevice *)qdev;
2070315a1350SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
2071d61a363dSYoni Bettan     ObjectClass *klass = OBJECT_CLASS(pc);
2072133e9b22SMarkus Armbruster     Error *local_err = NULL;
2073315a1350SMichael S. Tsirkin     bool is_default_rom;
2074315a1350SMichael S. Tsirkin 
2075d61a363dSYoni Bettan     /* initialize cap_present for pci_is_express() and pci_config_size(),
2076d61a363dSYoni Bettan      * Note that hybrid PCIs are not set automatically and need to manage
2077d61a363dSYoni Bettan      * QEMU_PCI_CAP_EXPRESS manually */
2078d61a363dSYoni Bettan     if (object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE) &&
2079d61a363dSYoni Bettan        !object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE)) {
2080315a1350SMichael S. Tsirkin         pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
2081315a1350SMichael S. Tsirkin     }
2082315a1350SMichael S. Tsirkin 
2083fd56e061SDavid Gibson     pci_dev = do_pci_register_device(pci_dev,
2084315a1350SMichael S. Tsirkin                                      object_get_typename(OBJECT(qdev)),
2085133e9b22SMarkus Armbruster                                      pci_dev->devfn, errp);
2086315a1350SMichael S. Tsirkin     if (pci_dev == NULL)
2087133e9b22SMarkus Armbruster         return;
20882897ae02SIgor Mammedov 
20897ee6c1e1SMarkus Armbruster     if (pc->realize) {
20907ee6c1e1SMarkus Armbruster         pc->realize(pci_dev, &local_err);
20917ee6c1e1SMarkus Armbruster         if (local_err) {
20927ee6c1e1SMarkus Armbruster             error_propagate(errp, local_err);
2093315a1350SMichael S. Tsirkin             do_pci_unregister_device(pci_dev);
2094133e9b22SMarkus Armbruster             return;
2095315a1350SMichael S. Tsirkin         }
2096315a1350SMichael S. Tsirkin     }
2097315a1350SMichael S. Tsirkin 
2098315a1350SMichael S. Tsirkin     /* rom loading */
2099315a1350SMichael S. Tsirkin     is_default_rom = false;
2100315a1350SMichael S. Tsirkin     if (pci_dev->romfile == NULL && pc->romfile != NULL) {
2101315a1350SMichael S. Tsirkin         pci_dev->romfile = g_strdup(pc->romfile);
2102315a1350SMichael S. Tsirkin         is_default_rom = true;
2103315a1350SMichael S. Tsirkin     }
2104178e785fSMarcel Apfelbaum 
2105133e9b22SMarkus Armbruster     pci_add_option_rom(pci_dev, is_default_rom, &local_err);
2106133e9b22SMarkus Armbruster     if (local_err) {
2107133e9b22SMarkus Armbruster         error_propagate(errp, local_err);
2108133e9b22SMarkus Armbruster         pci_qdev_unrealize(DEVICE(pci_dev), NULL);
2109133e9b22SMarkus Armbruster         return;
2110178e785fSMarcel Apfelbaum     }
2111315a1350SMichael S. Tsirkin }
2112315a1350SMichael S. Tsirkin 
2113315a1350SMichael S. Tsirkin PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
2114315a1350SMichael S. Tsirkin                                     const char *name)
2115315a1350SMichael S. Tsirkin {
2116315a1350SMichael S. Tsirkin     DeviceState *dev;
2117315a1350SMichael S. Tsirkin 
2118315a1350SMichael S. Tsirkin     dev = qdev_create(&bus->qbus, name);
2119315a1350SMichael S. Tsirkin     qdev_prop_set_int32(dev, "addr", devfn);
2120315a1350SMichael S. Tsirkin     qdev_prop_set_bit(dev, "multifunction", multifunction);
2121315a1350SMichael S. Tsirkin     return PCI_DEVICE(dev);
2122315a1350SMichael S. Tsirkin }
2123315a1350SMichael S. Tsirkin 
2124315a1350SMichael S. Tsirkin PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
2125315a1350SMichael S. Tsirkin                                            bool multifunction,
2126315a1350SMichael S. Tsirkin                                            const char *name)
2127315a1350SMichael S. Tsirkin {
2128315a1350SMichael S. Tsirkin     PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name);
2129315a1350SMichael S. Tsirkin     qdev_init_nofail(&dev->qdev);
2130315a1350SMichael S. Tsirkin     return dev;
2131315a1350SMichael S. Tsirkin }
2132315a1350SMichael S. Tsirkin 
2133315a1350SMichael S. Tsirkin PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
2134315a1350SMichael S. Tsirkin {
2135315a1350SMichael S. Tsirkin     return pci_create_multifunction(bus, devfn, false, name);
2136315a1350SMichael S. Tsirkin }
2137315a1350SMichael S. Tsirkin 
2138315a1350SMichael S. Tsirkin PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
2139315a1350SMichael S. Tsirkin {
2140315a1350SMichael S. Tsirkin     return pci_create_simple_multifunction(bus, devfn, false, name);
2141315a1350SMichael S. Tsirkin }
2142315a1350SMichael S. Tsirkin 
2143315a1350SMichael S. Tsirkin static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
2144315a1350SMichael S. Tsirkin {
2145315a1350SMichael S. Tsirkin     int offset = PCI_CONFIG_HEADER_SIZE;
2146315a1350SMichael S. Tsirkin     int i;
2147315a1350SMichael S. Tsirkin     for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
2148315a1350SMichael S. Tsirkin         if (pdev->used[i])
2149315a1350SMichael S. Tsirkin             offset = i + 1;
2150315a1350SMichael S. Tsirkin         else if (i - offset + 1 == size)
2151315a1350SMichael S. Tsirkin             return offset;
2152315a1350SMichael S. Tsirkin     }
2153315a1350SMichael S. Tsirkin     return 0;
2154315a1350SMichael S. Tsirkin }
2155315a1350SMichael S. Tsirkin 
2156315a1350SMichael S. Tsirkin static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
2157315a1350SMichael S. Tsirkin                                         uint8_t *prev_p)
2158315a1350SMichael S. Tsirkin {
2159315a1350SMichael S. Tsirkin     uint8_t next, prev;
2160315a1350SMichael S. Tsirkin 
2161315a1350SMichael S. Tsirkin     if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
2162315a1350SMichael S. Tsirkin         return 0;
2163315a1350SMichael S. Tsirkin 
2164315a1350SMichael S. Tsirkin     for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
2165315a1350SMichael S. Tsirkin          prev = next + PCI_CAP_LIST_NEXT)
2166315a1350SMichael S. Tsirkin         if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
2167315a1350SMichael S. Tsirkin             break;
2168315a1350SMichael S. Tsirkin 
2169315a1350SMichael S. Tsirkin     if (prev_p)
2170315a1350SMichael S. Tsirkin         *prev_p = prev;
2171315a1350SMichael S. Tsirkin     return next;
2172315a1350SMichael S. Tsirkin }
2173315a1350SMichael S. Tsirkin 
2174315a1350SMichael S. Tsirkin static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
2175315a1350SMichael S. Tsirkin {
2176315a1350SMichael S. Tsirkin     uint8_t next, prev, found = 0;
2177315a1350SMichael S. Tsirkin 
2178315a1350SMichael S. Tsirkin     if (!(pdev->used[offset])) {
2179315a1350SMichael S. Tsirkin         return 0;
2180315a1350SMichael S. Tsirkin     }
2181315a1350SMichael S. Tsirkin 
2182315a1350SMichael S. Tsirkin     assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST);
2183315a1350SMichael S. Tsirkin 
2184315a1350SMichael S. Tsirkin     for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
2185315a1350SMichael S. Tsirkin          prev = next + PCI_CAP_LIST_NEXT) {
2186315a1350SMichael S. Tsirkin         if (next <= offset && next > found) {
2187315a1350SMichael S. Tsirkin             found = next;
2188315a1350SMichael S. Tsirkin         }
2189315a1350SMichael S. Tsirkin     }
2190315a1350SMichael S. Tsirkin     return found;
2191315a1350SMichael S. Tsirkin }
2192315a1350SMichael S. Tsirkin 
2193315a1350SMichael S. Tsirkin /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
2194315a1350SMichael S. Tsirkin    This is needed for an option rom which is used for more than one device. */
2195315a1350SMichael S. Tsirkin static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)
2196315a1350SMichael S. Tsirkin {
2197315a1350SMichael S. Tsirkin     uint16_t vendor_id;
2198315a1350SMichael S. Tsirkin     uint16_t device_id;
2199315a1350SMichael S. Tsirkin     uint16_t rom_vendor_id;
2200315a1350SMichael S. Tsirkin     uint16_t rom_device_id;
2201315a1350SMichael S. Tsirkin     uint16_t rom_magic;
2202315a1350SMichael S. Tsirkin     uint16_t pcir_offset;
2203315a1350SMichael S. Tsirkin     uint8_t checksum;
2204315a1350SMichael S. Tsirkin 
2205315a1350SMichael S. Tsirkin     /* Words in rom data are little endian (like in PCI configuration),
2206315a1350SMichael S. Tsirkin        so they can be read / written with pci_get_word / pci_set_word. */
2207315a1350SMichael S. Tsirkin 
2208315a1350SMichael S. Tsirkin     /* Only a valid rom will be patched. */
2209315a1350SMichael S. Tsirkin     rom_magic = pci_get_word(ptr);
2210315a1350SMichael S. Tsirkin     if (rom_magic != 0xaa55) {
2211315a1350SMichael S. Tsirkin         PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
2212315a1350SMichael S. Tsirkin         return;
2213315a1350SMichael S. Tsirkin     }
2214315a1350SMichael S. Tsirkin     pcir_offset = pci_get_word(ptr + 0x18);
2215315a1350SMichael S. Tsirkin     if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
2216315a1350SMichael S. Tsirkin         PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
2217315a1350SMichael S. Tsirkin         return;
2218315a1350SMichael S. Tsirkin     }
2219315a1350SMichael S. Tsirkin 
2220315a1350SMichael S. Tsirkin     vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
2221315a1350SMichael S. Tsirkin     device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
2222315a1350SMichael S. Tsirkin     rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
2223315a1350SMichael S. Tsirkin     rom_device_id = pci_get_word(ptr + pcir_offset + 6);
2224315a1350SMichael S. Tsirkin 
2225315a1350SMichael S. Tsirkin     PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
2226315a1350SMichael S. Tsirkin                 vendor_id, device_id, rom_vendor_id, rom_device_id);
2227315a1350SMichael S. Tsirkin 
2228315a1350SMichael S. Tsirkin     checksum = ptr[6];
2229315a1350SMichael S. Tsirkin 
2230315a1350SMichael S. Tsirkin     if (vendor_id != rom_vendor_id) {
2231315a1350SMichael S. Tsirkin         /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
2232315a1350SMichael S. Tsirkin         checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
2233315a1350SMichael S. Tsirkin         checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
2234315a1350SMichael S. Tsirkin         PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2235315a1350SMichael S. Tsirkin         ptr[6] = checksum;
2236315a1350SMichael S. Tsirkin         pci_set_word(ptr + pcir_offset + 4, vendor_id);
2237315a1350SMichael S. Tsirkin     }
2238315a1350SMichael S. Tsirkin 
2239315a1350SMichael S. Tsirkin     if (device_id != rom_device_id) {
2240315a1350SMichael S. Tsirkin         /* Patch device id and checksum (at offset 6 for etherboot roms). */
2241315a1350SMichael S. Tsirkin         checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
2242315a1350SMichael S. Tsirkin         checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
2243315a1350SMichael S. Tsirkin         PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2244315a1350SMichael S. Tsirkin         ptr[6] = checksum;
2245315a1350SMichael S. Tsirkin         pci_set_word(ptr + pcir_offset + 6, device_id);
2246315a1350SMichael S. Tsirkin     }
2247315a1350SMichael S. Tsirkin }
2248315a1350SMichael S. Tsirkin 
2249315a1350SMichael S. Tsirkin /* Add an option rom for the device */
2250133e9b22SMarkus Armbruster static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
2251133e9b22SMarkus Armbruster                                Error **errp)
2252315a1350SMichael S. Tsirkin {
2253315a1350SMichael S. Tsirkin     int size;
2254315a1350SMichael S. Tsirkin     char *path;
2255315a1350SMichael S. Tsirkin     void *ptr;
2256315a1350SMichael S. Tsirkin     char name[32];
2257315a1350SMichael S. Tsirkin     const VMStateDescription *vmsd;
2258315a1350SMichael S. Tsirkin 
2259315a1350SMichael S. Tsirkin     if (!pdev->romfile)
2260133e9b22SMarkus Armbruster         return;
2261315a1350SMichael S. Tsirkin     if (strlen(pdev->romfile) == 0)
2262133e9b22SMarkus Armbruster         return;
2263315a1350SMichael S. Tsirkin 
2264315a1350SMichael S. Tsirkin     if (!pdev->rom_bar) {
2265315a1350SMichael S. Tsirkin         /*
2266315a1350SMichael S. Tsirkin          * Load rom via fw_cfg instead of creating a rom bar,
2267315a1350SMichael S. Tsirkin          * for 0.11 compatibility.
2268315a1350SMichael S. Tsirkin          */
2269315a1350SMichael S. Tsirkin         int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
2270db80c7b9SMarcel Apfelbaum 
2271db80c7b9SMarcel Apfelbaum         /*
2272db80c7b9SMarcel Apfelbaum          * Hot-plugged devices can't use the option ROM
2273db80c7b9SMarcel Apfelbaum          * if the rom bar is disabled.
2274db80c7b9SMarcel Apfelbaum          */
2275db80c7b9SMarcel Apfelbaum         if (DEVICE(pdev)->hotplugged) {
2276133e9b22SMarkus Armbruster             error_setg(errp, "Hot-plugged device without ROM bar"
2277133e9b22SMarkus Armbruster                        " can't have an option ROM");
2278133e9b22SMarkus Armbruster             return;
2279db80c7b9SMarcel Apfelbaum         }
2280db80c7b9SMarcel Apfelbaum 
2281315a1350SMichael S. Tsirkin         if (class == 0x0300) {
2282315a1350SMichael S. Tsirkin             rom_add_vga(pdev->romfile);
2283315a1350SMichael S. Tsirkin         } else {
2284315a1350SMichael S. Tsirkin             rom_add_option(pdev->romfile, -1);
2285315a1350SMichael S. Tsirkin         }
2286133e9b22SMarkus Armbruster         return;
2287315a1350SMichael S. Tsirkin     }
2288315a1350SMichael S. Tsirkin 
2289315a1350SMichael S. Tsirkin     path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
2290315a1350SMichael S. Tsirkin     if (path == NULL) {
2291315a1350SMichael S. Tsirkin         path = g_strdup(pdev->romfile);
2292315a1350SMichael S. Tsirkin     }
2293315a1350SMichael S. Tsirkin 
2294315a1350SMichael S. Tsirkin     size = get_image_size(path);
2295315a1350SMichael S. Tsirkin     if (size < 0) {
2296133e9b22SMarkus Armbruster         error_setg(errp, "failed to find romfile \"%s\"", pdev->romfile);
22978c7f3dd0SStefan Hajnoczi         g_free(path);
2298133e9b22SMarkus Armbruster         return;
22998c7f3dd0SStefan Hajnoczi     } else if (size == 0) {
2300133e9b22SMarkus Armbruster         error_setg(errp, "romfile \"%s\" is empty", pdev->romfile);
2301315a1350SMichael S. Tsirkin         g_free(path);
2302133e9b22SMarkus Armbruster         return;
2303315a1350SMichael S. Tsirkin     }
23049bff5d81SPeter Maydell     size = pow2ceil(size);
2305315a1350SMichael S. Tsirkin 
2306315a1350SMichael S. Tsirkin     vmsd = qdev_get_vmsd(DEVICE(pdev));
2307315a1350SMichael S. Tsirkin 
2308315a1350SMichael S. Tsirkin     if (vmsd) {
2309315a1350SMichael S. Tsirkin         snprintf(name, sizeof(name), "%s.rom", vmsd->name);
2310315a1350SMichael S. Tsirkin     } else {
2311315a1350SMichael S. Tsirkin         snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev)));
2312315a1350SMichael S. Tsirkin     }
2313315a1350SMichael S. Tsirkin     pdev->has_rom = true;
2314fefa9256SPeter Maydell     memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, size, &error_fatal);
2315315a1350SMichael S. Tsirkin     ptr = memory_region_get_ram_ptr(&pdev->rom);
231636bde091SPeter Maydell     if (load_image_size(path, ptr, size) < 0) {
231736bde091SPeter Maydell         error_setg(errp, "failed to load romfile \"%s\"", pdev->romfile);
231836bde091SPeter Maydell         g_free(path);
231936bde091SPeter Maydell         return;
232036bde091SPeter Maydell     }
2321315a1350SMichael S. Tsirkin     g_free(path);
2322315a1350SMichael S. Tsirkin 
2323315a1350SMichael S. Tsirkin     if (is_default_rom) {
2324315a1350SMichael S. Tsirkin         /* Only the default rom images will be patched (if needed). */
2325315a1350SMichael S. Tsirkin         pci_patch_ids(pdev, ptr, size);
2326315a1350SMichael S. Tsirkin     }
2327315a1350SMichael S. Tsirkin 
2328315a1350SMichael S. Tsirkin     pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
2329315a1350SMichael S. Tsirkin }
2330315a1350SMichael S. Tsirkin 
2331315a1350SMichael S. Tsirkin static void pci_del_option_rom(PCIDevice *pdev)
2332315a1350SMichael S. Tsirkin {
2333315a1350SMichael S. Tsirkin     if (!pdev->has_rom)
2334315a1350SMichael S. Tsirkin         return;
2335315a1350SMichael S. Tsirkin 
2336315a1350SMichael S. Tsirkin     vmstate_unregister_ram(&pdev->rom, &pdev->qdev);
2337315a1350SMichael S. Tsirkin     pdev->has_rom = false;
2338315a1350SMichael S. Tsirkin }
2339315a1350SMichael S. Tsirkin 
2340315a1350SMichael S. Tsirkin /*
234127841278SMao Zhongyi  * On success, pci_add_capability() returns a positive value
2342eacbc632SMao Zhongyi  * that the offset of the pci capability.
2343eacbc632SMao Zhongyi  * On failure, it sets an error and returns a negative error
2344eacbc632SMao Zhongyi  * code.
2345eacbc632SMao Zhongyi  */
234627841278SMao Zhongyi int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
2347cd9aa33eSLaszlo Ersek                        uint8_t offset, uint8_t size,
2348cd9aa33eSLaszlo Ersek                        Error **errp)
2349cd9aa33eSLaszlo Ersek {
2350315a1350SMichael S. Tsirkin     uint8_t *config;
2351315a1350SMichael S. Tsirkin     int i, overlapping_cap;
2352315a1350SMichael S. Tsirkin 
2353315a1350SMichael S. Tsirkin     if (!offset) {
2354315a1350SMichael S. Tsirkin         offset = pci_find_space(pdev, size);
235597fe42f1SCao jin         /* out of PCI config space is programming error */
235697fe42f1SCao jin         assert(offset);
2357315a1350SMichael S. Tsirkin     } else {
2358315a1350SMichael S. Tsirkin         /* Verify that capabilities don't overlap.  Note: device assignment
2359315a1350SMichael S. Tsirkin          * depends on this check to verify that the device is not broken.
2360315a1350SMichael S. Tsirkin          * Should never trigger for emulated devices, but it's helpful
2361315a1350SMichael S. Tsirkin          * for debugging these. */
2362315a1350SMichael S. Tsirkin         for (i = offset; i < offset + size; i++) {
2363315a1350SMichael S. Tsirkin             overlapping_cap = pci_find_capability_at_offset(pdev, i);
2364315a1350SMichael S. Tsirkin             if (overlapping_cap) {
2365cd9aa33eSLaszlo Ersek                 error_setg(errp, "%s:%02x:%02x.%x "
2366315a1350SMichael S. Tsirkin                            "Attempt to add PCI capability %x at offset "
2367cd9aa33eSLaszlo Ersek                            "%x overlaps existing capability %x at offset %x",
2368fd56e061SDavid Gibson                            pci_root_bus_path(pdev), pci_dev_bus_num(pdev),
2369315a1350SMichael S. Tsirkin                            PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2370315a1350SMichael S. Tsirkin                            cap_id, offset, overlapping_cap, i);
2371315a1350SMichael S. Tsirkin                 return -EINVAL;
2372315a1350SMichael S. Tsirkin             }
2373315a1350SMichael S. Tsirkin         }
2374315a1350SMichael S. Tsirkin     }
2375315a1350SMichael S. Tsirkin 
2376315a1350SMichael S. Tsirkin     config = pdev->config + offset;
2377315a1350SMichael S. Tsirkin     config[PCI_CAP_LIST_ID] = cap_id;
2378315a1350SMichael S. Tsirkin     config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
2379315a1350SMichael S. Tsirkin     pdev->config[PCI_CAPABILITY_LIST] = offset;
2380315a1350SMichael S. Tsirkin     pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
2381315a1350SMichael S. Tsirkin     memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));
2382315a1350SMichael S. Tsirkin     /* Make capability read-only by default */
2383315a1350SMichael S. Tsirkin     memset(pdev->wmask + offset, 0, size);
2384315a1350SMichael S. Tsirkin     /* Check capability by default */
2385315a1350SMichael S. Tsirkin     memset(pdev->cmask + offset, 0xFF, size);
2386315a1350SMichael S. Tsirkin     return offset;
2387315a1350SMichael S. Tsirkin }
2388315a1350SMichael S. Tsirkin 
2389315a1350SMichael S. Tsirkin /* Unlink capability from the pci config space. */
2390315a1350SMichael S. Tsirkin void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
2391315a1350SMichael S. Tsirkin {
2392315a1350SMichael S. Tsirkin     uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
2393315a1350SMichael S. Tsirkin     if (!offset)
2394315a1350SMichael S. Tsirkin         return;
2395315a1350SMichael S. Tsirkin     pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
2396315a1350SMichael S. Tsirkin     /* Make capability writable again */
2397315a1350SMichael S. Tsirkin     memset(pdev->wmask + offset, 0xff, size);
2398315a1350SMichael S. Tsirkin     memset(pdev->w1cmask + offset, 0, size);
2399315a1350SMichael S. Tsirkin     /* Clear cmask as device-specific registers can't be checked */
2400315a1350SMichael S. Tsirkin     memset(pdev->cmask + offset, 0, size);
2401315a1350SMichael S. Tsirkin     memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4));
2402315a1350SMichael S. Tsirkin 
2403315a1350SMichael S. Tsirkin     if (!pdev->config[PCI_CAPABILITY_LIST])
2404315a1350SMichael S. Tsirkin         pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
2405315a1350SMichael S. Tsirkin }
2406315a1350SMichael S. Tsirkin 
2407315a1350SMichael S. Tsirkin uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
2408315a1350SMichael S. Tsirkin {
2409315a1350SMichael S. Tsirkin     return pci_find_capability_list(pdev, cap_id, NULL);
2410315a1350SMichael S. Tsirkin }
2411315a1350SMichael S. Tsirkin 
2412315a1350SMichael S. Tsirkin static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
2413315a1350SMichael S. Tsirkin {
2414315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2415315a1350SMichael S. Tsirkin     const pci_class_desc *desc;
2416315a1350SMichael S. Tsirkin     char ctxt[64];
2417315a1350SMichael S. Tsirkin     PCIIORegion *r;
2418315a1350SMichael S. Tsirkin     int i, class;
2419315a1350SMichael S. Tsirkin 
2420315a1350SMichael S. Tsirkin     class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2421315a1350SMichael S. Tsirkin     desc = pci_class_descriptions;
2422315a1350SMichael S. Tsirkin     while (desc->desc && class != desc->class)
2423315a1350SMichael S. Tsirkin         desc++;
2424315a1350SMichael S. Tsirkin     if (desc->desc) {
2425315a1350SMichael S. Tsirkin         snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
2426315a1350SMichael S. Tsirkin     } else {
2427315a1350SMichael S. Tsirkin         snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
2428315a1350SMichael S. Tsirkin     }
2429315a1350SMichael S. Tsirkin 
2430315a1350SMichael S. Tsirkin     monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
2431315a1350SMichael S. Tsirkin                    "pci id %04x:%04x (sub %04x:%04x)\n",
2432fd56e061SDavid Gibson                    indent, "", ctxt, pci_dev_bus_num(d),
2433315a1350SMichael S. Tsirkin                    PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
2434315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_VENDOR_ID),
2435315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_DEVICE_ID),
2436315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
2437315a1350SMichael S. Tsirkin                    pci_get_word(d->config + PCI_SUBSYSTEM_ID));
2438315a1350SMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; i++) {
2439315a1350SMichael S. Tsirkin         r = &d->io_regions[i];
2440315a1350SMichael S. Tsirkin         if (!r->size)
2441315a1350SMichael S. Tsirkin             continue;
2442315a1350SMichael S. Tsirkin         monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
2443315a1350SMichael S. Tsirkin                        " [0x%"FMT_PCIBUS"]\n",
2444315a1350SMichael S. Tsirkin                        indent, "",
2445315a1350SMichael S. Tsirkin                        i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
2446315a1350SMichael S. Tsirkin                        r->addr, r->addr + r->size - 1);
2447315a1350SMichael S. Tsirkin     }
2448315a1350SMichael S. Tsirkin }
2449315a1350SMichael S. Tsirkin 
2450315a1350SMichael S. Tsirkin static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
2451315a1350SMichael S. Tsirkin {
2452315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2453315a1350SMichael S. Tsirkin     const char *name = NULL;
2454315a1350SMichael S. Tsirkin     const pci_class_desc *desc =  pci_class_descriptions;
2455315a1350SMichael S. Tsirkin     int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2456315a1350SMichael S. Tsirkin 
2457315a1350SMichael S. Tsirkin     while (desc->desc &&
2458315a1350SMichael S. Tsirkin           (class & ~desc->fw_ign_bits) !=
2459315a1350SMichael S. Tsirkin           (desc->class & ~desc->fw_ign_bits)) {
2460315a1350SMichael S. Tsirkin         desc++;
2461315a1350SMichael S. Tsirkin     }
2462315a1350SMichael S. Tsirkin 
2463315a1350SMichael S. Tsirkin     if (desc->desc) {
2464315a1350SMichael S. Tsirkin         name = desc->fw_name;
2465315a1350SMichael S. Tsirkin     }
2466315a1350SMichael S. Tsirkin 
2467315a1350SMichael S. Tsirkin     if (name) {
2468315a1350SMichael S. Tsirkin         pstrcpy(buf, len, name);
2469315a1350SMichael S. Tsirkin     } else {
2470315a1350SMichael S. Tsirkin         snprintf(buf, len, "pci%04x,%04x",
2471315a1350SMichael S. Tsirkin                  pci_get_word(d->config + PCI_VENDOR_ID),
2472315a1350SMichael S. Tsirkin                  pci_get_word(d->config + PCI_DEVICE_ID));
2473315a1350SMichael S. Tsirkin     }
2474315a1350SMichael S. Tsirkin 
2475315a1350SMichael S. Tsirkin     return buf;
2476315a1350SMichael S. Tsirkin }
2477315a1350SMichael S. Tsirkin 
2478315a1350SMichael S. Tsirkin static char *pcibus_get_fw_dev_path(DeviceState *dev)
2479315a1350SMichael S. Tsirkin {
2480315a1350SMichael S. Tsirkin     PCIDevice *d = (PCIDevice *)dev;
2481315a1350SMichael S. Tsirkin     char path[50], name[33];
2482315a1350SMichael S. Tsirkin     int off;
2483315a1350SMichael S. Tsirkin 
2484315a1350SMichael S. Tsirkin     off = snprintf(path, sizeof(path), "%s@%x",
2485315a1350SMichael S. Tsirkin                    pci_dev_fw_name(dev, name, sizeof name),
2486315a1350SMichael S. Tsirkin                    PCI_SLOT(d->devfn));
2487315a1350SMichael S. Tsirkin     if (PCI_FUNC(d->devfn))
2488315a1350SMichael S. Tsirkin         snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
2489315a1350SMichael S. Tsirkin     return g_strdup(path);
2490315a1350SMichael S. Tsirkin }
2491315a1350SMichael S. Tsirkin 
2492315a1350SMichael S. Tsirkin static char *pcibus_get_dev_path(DeviceState *dev)
2493315a1350SMichael S. Tsirkin {
2494315a1350SMichael S. Tsirkin     PCIDevice *d = container_of(dev, PCIDevice, qdev);
2495315a1350SMichael S. Tsirkin     PCIDevice *t;
2496315a1350SMichael S. Tsirkin     int slot_depth;
2497315a1350SMichael S. Tsirkin     /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
2498315a1350SMichael S. Tsirkin      * 00 is added here to make this format compatible with
2499315a1350SMichael S. Tsirkin      * domain:Bus:Slot.Func for systems without nested PCI bridges.
2500315a1350SMichael S. Tsirkin      * Slot.Function list specifies the slot and function numbers for all
2501315a1350SMichael S. Tsirkin      * devices on the path from root to the specific device. */
2502568f0690SDavid Gibson     const char *root_bus_path;
2503568f0690SDavid Gibson     int root_bus_len;
2504315a1350SMichael S. Tsirkin     char slot[] = ":SS.F";
2505315a1350SMichael S. Tsirkin     int slot_len = sizeof slot - 1 /* For '\0' */;
2506315a1350SMichael S. Tsirkin     int path_len;
2507315a1350SMichael S. Tsirkin     char *path, *p;
2508315a1350SMichael S. Tsirkin     int s;
2509315a1350SMichael S. Tsirkin 
2510568f0690SDavid Gibson     root_bus_path = pci_root_bus_path(d);
2511568f0690SDavid Gibson     root_bus_len = strlen(root_bus_path);
2512568f0690SDavid Gibson 
2513315a1350SMichael S. Tsirkin     /* Calculate # of slots on path between device and root. */;
2514315a1350SMichael S. Tsirkin     slot_depth = 0;
2515fd56e061SDavid Gibson     for (t = d; t; t = pci_get_bus(t)->parent_dev) {
2516315a1350SMichael S. Tsirkin         ++slot_depth;
2517315a1350SMichael S. Tsirkin     }
2518315a1350SMichael S. Tsirkin 
2519568f0690SDavid Gibson     path_len = root_bus_len + slot_len * slot_depth;
2520315a1350SMichael S. Tsirkin 
2521315a1350SMichael S. Tsirkin     /* Allocate memory, fill in the terminating null byte. */
2522315a1350SMichael S. Tsirkin     path = g_malloc(path_len + 1 /* For '\0' */);
2523315a1350SMichael S. Tsirkin     path[path_len] = '\0';
2524315a1350SMichael S. Tsirkin 
2525568f0690SDavid Gibson     memcpy(path, root_bus_path, root_bus_len);
2526315a1350SMichael S. Tsirkin 
2527315a1350SMichael S. Tsirkin     /* Fill in slot numbers. We walk up from device to root, so need to print
2528315a1350SMichael S. Tsirkin      * them in the reverse order, last to first. */
2529315a1350SMichael S. Tsirkin     p = path + path_len;
2530fd56e061SDavid Gibson     for (t = d; t; t = pci_get_bus(t)->parent_dev) {
2531315a1350SMichael S. Tsirkin         p -= slot_len;
2532315a1350SMichael S. Tsirkin         s = snprintf(slot, sizeof slot, ":%02x.%x",
2533315a1350SMichael S. Tsirkin                      PCI_SLOT(t->devfn), PCI_FUNC(t->devfn));
2534315a1350SMichael S. Tsirkin         assert(s == slot_len);
2535315a1350SMichael S. Tsirkin         memcpy(p, slot, slot_len);
2536315a1350SMichael S. Tsirkin     }
2537315a1350SMichael S. Tsirkin 
2538315a1350SMichael S. Tsirkin     return path;
2539315a1350SMichael S. Tsirkin }
2540315a1350SMichael S. Tsirkin 
2541315a1350SMichael S. Tsirkin static int pci_qdev_find_recursive(PCIBus *bus,
2542315a1350SMichael S. Tsirkin                                    const char *id, PCIDevice **pdev)
2543315a1350SMichael S. Tsirkin {
2544315a1350SMichael S. Tsirkin     DeviceState *qdev = qdev_find_recursive(&bus->qbus, id);
2545315a1350SMichael S. Tsirkin     if (!qdev) {
2546315a1350SMichael S. Tsirkin         return -ENODEV;
2547315a1350SMichael S. Tsirkin     }
2548315a1350SMichael S. Tsirkin 
2549315a1350SMichael S. Tsirkin     /* roughly check if given qdev is pci device */
2550315a1350SMichael S. Tsirkin     if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) {
2551315a1350SMichael S. Tsirkin         *pdev = PCI_DEVICE(qdev);
2552315a1350SMichael S. Tsirkin         return 0;
2553315a1350SMichael S. Tsirkin     }
2554315a1350SMichael S. Tsirkin     return -EINVAL;
2555315a1350SMichael S. Tsirkin }
2556315a1350SMichael S. Tsirkin 
2557315a1350SMichael S. Tsirkin int pci_qdev_find_device(const char *id, PCIDevice **pdev)
2558315a1350SMichael S. Tsirkin {
25597588e2b0SDavid Gibson     PCIHostState *host_bridge;
2560315a1350SMichael S. Tsirkin     int rc = -ENODEV;
2561315a1350SMichael S. Tsirkin 
25627588e2b0SDavid Gibson     QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
25637588e2b0SDavid Gibson         int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev);
2564315a1350SMichael S. Tsirkin         if (!tmp) {
2565315a1350SMichael S. Tsirkin             rc = 0;
2566315a1350SMichael S. Tsirkin             break;
2567315a1350SMichael S. Tsirkin         }
2568315a1350SMichael S. Tsirkin         if (tmp != -ENODEV) {
2569315a1350SMichael S. Tsirkin             rc = tmp;
2570315a1350SMichael S. Tsirkin         }
2571315a1350SMichael S. Tsirkin     }
2572315a1350SMichael S. Tsirkin 
2573315a1350SMichael S. Tsirkin     return rc;
2574315a1350SMichael S. Tsirkin }
2575315a1350SMichael S. Tsirkin 
2576315a1350SMichael S. Tsirkin MemoryRegion *pci_address_space(PCIDevice *dev)
2577315a1350SMichael S. Tsirkin {
2578fd56e061SDavid Gibson     return pci_get_bus(dev)->address_space_mem;
2579315a1350SMichael S. Tsirkin }
2580315a1350SMichael S. Tsirkin 
2581315a1350SMichael S. Tsirkin MemoryRegion *pci_address_space_io(PCIDevice *dev)
2582315a1350SMichael S. Tsirkin {
2583fd56e061SDavid Gibson     return pci_get_bus(dev)->address_space_io;
2584315a1350SMichael S. Tsirkin }
2585315a1350SMichael S. Tsirkin 
2586315a1350SMichael S. Tsirkin static void pci_device_class_init(ObjectClass *klass, void *data)
2587315a1350SMichael S. Tsirkin {
2588315a1350SMichael S. Tsirkin     DeviceClass *k = DEVICE_CLASS(klass);
25897ee6c1e1SMarkus Armbruster 
2590133e9b22SMarkus Armbruster     k->realize = pci_qdev_realize;
2591133e9b22SMarkus Armbruster     k->unrealize = pci_qdev_unrealize;
2592315a1350SMichael S. Tsirkin     k->bus_type = TYPE_PCI_BUS;
2593315a1350SMichael S. Tsirkin     k->props = pci_props;
2594315a1350SMichael S. Tsirkin }
2595315a1350SMichael S. Tsirkin 
25962fefa16cSEduardo Habkost static void pci_device_class_base_init(ObjectClass *klass, void *data)
25972fefa16cSEduardo Habkost {
25982fefa16cSEduardo Habkost     if (!object_class_is_abstract(klass)) {
25992fefa16cSEduardo Habkost         ObjectClass *conventional =
26002fefa16cSEduardo Habkost             object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE);
26012fefa16cSEduardo Habkost         ObjectClass *pcie =
26022fefa16cSEduardo Habkost             object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE);
26032fefa16cSEduardo Habkost         assert(conventional || pcie);
26042fefa16cSEduardo Habkost     }
26052fefa16cSEduardo Habkost }
26062fefa16cSEduardo Habkost 
26079eda7d37SAlexey Kardashevskiy AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
26089eda7d37SAlexey Kardashevskiy {
2609fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(dev);
26105af2ae23SBenjamin Herrenschmidt     PCIBus *iommu_bus = bus;
26119eda7d37SAlexey Kardashevskiy 
26125af2ae23SBenjamin Herrenschmidt     while(iommu_bus && !iommu_bus->iommu_fn && iommu_bus->parent_dev) {
2613fd56e061SDavid Gibson         iommu_bus = pci_get_bus(iommu_bus->parent_dev);
26149eda7d37SAlexey Kardashevskiy     }
26155af2ae23SBenjamin Herrenschmidt     if (iommu_bus && iommu_bus->iommu_fn) {
26165af2ae23SBenjamin Herrenschmidt         return iommu_bus->iommu_fn(bus, iommu_bus->iommu_opaque, dev->devfn);
26179eda7d37SAlexey Kardashevskiy     }
26189eda7d37SAlexey Kardashevskiy     return &address_space_memory;
26199eda7d37SAlexey Kardashevskiy }
26209eda7d37SAlexey Kardashevskiy 
2621e00387d5SAvi Kivity void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
2622315a1350SMichael S. Tsirkin {
2623e00387d5SAvi Kivity     bus->iommu_fn = fn;
2624e00387d5SAvi Kivity     bus->iommu_opaque = opaque;
2625315a1350SMichael S. Tsirkin }
2626315a1350SMichael S. Tsirkin 
262743864069SMichael S. Tsirkin static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque)
262843864069SMichael S. Tsirkin {
262943864069SMichael S. Tsirkin     Range *range = opaque;
263043864069SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
263143864069SMichael S. Tsirkin     uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND);
263277d6f4eaSMichael S. Tsirkin     int i;
263343864069SMichael S. Tsirkin 
263443864069SMichael S. Tsirkin     if (!(cmd & PCI_COMMAND_MEMORY)) {
263543864069SMichael S. Tsirkin         return;
263643864069SMichael S. Tsirkin     }
263743864069SMichael S. Tsirkin 
263843864069SMichael S. Tsirkin     if (pc->is_bridge) {
263943864069SMichael S. Tsirkin         pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
264043864069SMichael S. Tsirkin         pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
264143864069SMichael S. Tsirkin 
264243864069SMichael S. Tsirkin         base = MAX(base, 0x1ULL << 32);
264343864069SMichael S. Tsirkin 
264443864069SMichael S. Tsirkin         if (limit >= base) {
264543864069SMichael S. Tsirkin             Range pref_range;
2646a0efbf16SMarkus Armbruster             range_set_bounds(&pref_range, base, limit);
264743864069SMichael S. Tsirkin             range_extend(range, &pref_range);
264843864069SMichael S. Tsirkin         }
264943864069SMichael S. Tsirkin     }
265077d6f4eaSMichael S. Tsirkin     for (i = 0; i < PCI_NUM_REGIONS; ++i) {
265177d6f4eaSMichael S. Tsirkin         PCIIORegion *r = &dev->io_regions[i];
2652a0efbf16SMarkus Armbruster         pcibus_t lob, upb;
265343864069SMichael S. Tsirkin         Range region_range;
265443864069SMichael S. Tsirkin 
265577d6f4eaSMichael S. Tsirkin         if (!r->size ||
265677d6f4eaSMichael S. Tsirkin             (r->type & PCI_BASE_ADDRESS_SPACE_IO) ||
265777d6f4eaSMichael S. Tsirkin             !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
265843864069SMichael S. Tsirkin             continue;
265943864069SMichael S. Tsirkin         }
266077d6f4eaSMichael S. Tsirkin 
2661a0efbf16SMarkus Armbruster         lob = pci_bar_address(dev, i, r->type, r->size);
2662a0efbf16SMarkus Armbruster         upb = lob + r->size - 1;
2663a0efbf16SMarkus Armbruster         if (lob == PCI_BAR_UNMAPPED) {
266477d6f4eaSMichael S. Tsirkin             continue;
266577d6f4eaSMichael S. Tsirkin         }
266643864069SMichael S. Tsirkin 
2667a0efbf16SMarkus Armbruster         lob = MAX(lob, 0x1ULL << 32);
266843864069SMichael S. Tsirkin 
2669a0efbf16SMarkus Armbruster         if (upb >= lob) {
2670a0efbf16SMarkus Armbruster             range_set_bounds(&region_range, lob, upb);
267143864069SMichael S. Tsirkin             range_extend(range, &region_range);
267243864069SMichael S. Tsirkin         }
267343864069SMichael S. Tsirkin     }
267443864069SMichael S. Tsirkin }
267543864069SMichael S. Tsirkin 
267643864069SMichael S. Tsirkin void pci_bus_get_w64_range(PCIBus *bus, Range *range)
267743864069SMichael S. Tsirkin {
2678a0efbf16SMarkus Armbruster     range_make_empty(range);
267943864069SMichael S. Tsirkin     pci_for_each_device_under_bus(bus, pci_dev_get_w64, range);
268043864069SMichael S. Tsirkin }
268143864069SMichael S. Tsirkin 
26823f1e1478SCao jin static bool pcie_has_upstream_port(PCIDevice *dev)
26833f1e1478SCao jin {
2684fd56e061SDavid Gibson     PCIDevice *parent_dev = pci_bridge_get_device(pci_get_bus(dev));
26853f1e1478SCao jin 
26863f1e1478SCao jin     /* Device associated with an upstream port.
26873f1e1478SCao jin      * As there are several types of these, it's easier to check the
26883f1e1478SCao jin      * parent device: upstream ports are always connected to
26893f1e1478SCao jin      * root or downstream ports.
26903f1e1478SCao jin      */
26913f1e1478SCao jin     return parent_dev &&
26923f1e1478SCao jin         pci_is_express(parent_dev) &&
26933f1e1478SCao jin         parent_dev->exp.exp_cap &&
26943f1e1478SCao jin         (pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_ROOT_PORT ||
26953f1e1478SCao jin          pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_DOWNSTREAM);
26963f1e1478SCao jin }
26973f1e1478SCao jin 
26983f1e1478SCao jin PCIDevice *pci_get_function_0(PCIDevice *pci_dev)
26993f1e1478SCao jin {
2700fd56e061SDavid Gibson     PCIBus *bus = pci_get_bus(pci_dev);
2701fd56e061SDavid Gibson 
27023f1e1478SCao jin     if(pcie_has_upstream_port(pci_dev)) {
27033f1e1478SCao jin         /* With an upstream PCIe port, we only support 1 device at slot 0 */
2704fd56e061SDavid Gibson         return bus->devices[0];
27053f1e1478SCao jin     } else {
27063f1e1478SCao jin         /* Other bus types might support multiple devices at slots 0-31 */
2707fd56e061SDavid Gibson         return bus->devices[PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 0)];
27083f1e1478SCao jin     }
27093f1e1478SCao jin }
27103f1e1478SCao jin 
2711e1d4fb2dSPeter Xu MSIMessage pci_get_msi_message(PCIDevice *dev, int vector)
2712e1d4fb2dSPeter Xu {
2713e1d4fb2dSPeter Xu     MSIMessage msg;
2714e1d4fb2dSPeter Xu     if (msix_enabled(dev)) {
2715e1d4fb2dSPeter Xu         msg = msix_get_message(dev, vector);
2716e1d4fb2dSPeter Xu     } else if (msi_enabled(dev)) {
2717e1d4fb2dSPeter Xu         msg = msi_get_message(dev, vector);
2718e1d4fb2dSPeter Xu     } else {
2719e1d4fb2dSPeter Xu         /* Should never happen */
2720e1d4fb2dSPeter Xu         error_report("%s: unknown interrupt type", __func__);
2721e1d4fb2dSPeter Xu         abort();
2722e1d4fb2dSPeter Xu     }
2723e1d4fb2dSPeter Xu     return msg;
2724e1d4fb2dSPeter Xu }
2725e1d4fb2dSPeter Xu 
27268c43a6f0SAndreas Färber static const TypeInfo pci_device_type_info = {
2727315a1350SMichael S. Tsirkin     .name = TYPE_PCI_DEVICE,
2728315a1350SMichael S. Tsirkin     .parent = TYPE_DEVICE,
2729315a1350SMichael S. Tsirkin     .instance_size = sizeof(PCIDevice),
2730315a1350SMichael S. Tsirkin     .abstract = true,
2731315a1350SMichael S. Tsirkin     .class_size = sizeof(PCIDeviceClass),
2732315a1350SMichael S. Tsirkin     .class_init = pci_device_class_init,
27332fefa16cSEduardo Habkost     .class_base_init = pci_device_class_base_init,
2734315a1350SMichael S. Tsirkin };
2735315a1350SMichael S. Tsirkin 
2736315a1350SMichael S. Tsirkin static void pci_register_types(void)
2737315a1350SMichael S. Tsirkin {
2738315a1350SMichael S. Tsirkin     type_register_static(&pci_bus_info);
27393a861c46SAlex Williamson     type_register_static(&pcie_bus_info);
2740619f02aeSEduardo Habkost     type_register_static(&conventional_pci_interface_info);
2741619f02aeSEduardo Habkost     type_register_static(&pcie_interface_info);
2742315a1350SMichael S. Tsirkin     type_register_static(&pci_device_type_info);
2743315a1350SMichael S. Tsirkin }
2744315a1350SMichael S. Tsirkin 
2745315a1350SMichael S. Tsirkin type_init(pci_register_types)
2746