Statistics
| Branch: | Revision:

root / hw / pci.c @ 5ea3c2b4

History | View | Annotate | Download (66.2 kB)

1 69b91039 bellard
/*
2 69b91039 bellard
 * QEMU PCI bus manager
3 69b91039 bellard
 *
4 69b91039 bellard
 * Copyright (c) 2004 Fabrice Bellard
5 5fafdf24 ths
 *
6 69b91039 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 69b91039 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 69b91039 bellard
 * in the Software without restriction, including without limitation the rights
9 69b91039 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 69b91039 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 69b91039 bellard
 * furnished to do so, subject to the following conditions:
12 69b91039 bellard
 *
13 69b91039 bellard
 * The above copyright notice and this permission notice shall be included in
14 69b91039 bellard
 * all copies or substantial portions of the Software.
15 69b91039 bellard
 *
16 69b91039 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 69b91039 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 69b91039 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 69b91039 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 69b91039 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 69b91039 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 69b91039 bellard
 * THE SOFTWARE.
23 69b91039 bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "pci.h"
26 783753fd Isaku Yamahata
#include "pci_bridge.h"
27 cfb0a50a Isaku Yamahata
#include "pci_internals.h"
28 376253ec aliguori
#include "monitor.h"
29 87ecb68b pbrook
#include "net.h"
30 880345c4 aliguori
#include "sysemu.h"
31 c2039bd0 Anthony Liguori
#include "loader.h"
32 163c8a59 Luiz Capitulino
#include "qemu-objects.h"
33 bf1b0071 Blue Swirl
#include "range.h"
34 69b91039 bellard
35 69b91039 bellard
//#define DEBUG_PCI
36 d8d2e079 Isaku Yamahata
#ifdef DEBUG_PCI
37 2e49d64a Isaku Yamahata
# define PCI_DPRINTF(format, ...)       printf(format, ## __VA_ARGS__)
38 d8d2e079 Isaku Yamahata
#else
39 d8d2e079 Isaku Yamahata
# define PCI_DPRINTF(format, ...)       do { } while (0)
40 d8d2e079 Isaku Yamahata
#endif
41 69b91039 bellard
42 10c4c98a Gerd Hoffmann
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
43 4f43c1ff Alex Williamson
static char *pcibus_get_dev_path(DeviceState *dev);
44 5e0259e7 Gleb Natapov
static char *pcibus_get_fw_dev_path(DeviceState *dev);
45 9bb33586 Isaku Yamahata
static int pcibus_reset(BusState *qbus);
46 10c4c98a Gerd Hoffmann
47 cfb0a50a Isaku Yamahata
struct BusInfo pci_bus_info = {
48 10c4c98a Gerd Hoffmann
    .name       = "PCI",
49 10c4c98a Gerd Hoffmann
    .size       = sizeof(PCIBus),
50 10c4c98a Gerd Hoffmann
    .print_dev  = pcibus_dev_print,
51 4f43c1ff Alex Williamson
    .get_dev_path = pcibus_get_dev_path,
52 5e0259e7 Gleb Natapov
    .get_fw_dev_path = pcibus_get_fw_dev_path,
53 9bb33586 Isaku Yamahata
    .reset      = pcibus_reset,
54 ee6847d1 Gerd Hoffmann
    .props      = (Property[]) {
55 54586bd1 Gerd Hoffmann
        DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
56 8c52c8f3 Gerd Hoffmann
        DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
57 88169ddf Gerd Hoffmann
        DEFINE_PROP_UINT32("rombar",  PCIDevice, rom_bar, 1),
58 49823868 Isaku Yamahata
        DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
59 49823868 Isaku Yamahata
                        QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
60 b1aeb926 Isaku Yamahata
        DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present,
61 b1aeb926 Isaku Yamahata
                        QEMU_PCI_CAP_SERR_BITNR, true),
62 54586bd1 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST()
63 ee6847d1 Gerd Hoffmann
    }
64 30468f78 bellard
};
65 69b91039 bellard
66 1941d19c bellard
static void pci_update_mappings(PCIDevice *d);
67 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level);
68 ab85ceb1 Stefan Weil
static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom);
69 230741dc Alex Williamson
static void pci_del_option_rom(PCIDevice *pdev);
70 1941d19c bellard
71 d350d97d aliguori
static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
72 d350d97d aliguori
static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
73 e822a52a Isaku Yamahata
74 e822a52a Isaku Yamahata
struct PCIHostBus {
75 e822a52a Isaku Yamahata
    int domain;
76 e822a52a Isaku Yamahata
    struct PCIBus *bus;
77 e822a52a Isaku Yamahata
    QLIST_ENTRY(PCIHostBus) next;
78 e822a52a Isaku Yamahata
};
79 e822a52a Isaku Yamahata
static QLIST_HEAD(, PCIHostBus) host_buses;
80 30468f78 bellard
81 2d1e9f96 Juan Quintela
static const VMStateDescription vmstate_pcibus = {
82 2d1e9f96 Juan Quintela
    .name = "PCIBUS",
83 2d1e9f96 Juan Quintela
    .version_id = 1,
84 2d1e9f96 Juan Quintela
    .minimum_version_id = 1,
85 2d1e9f96 Juan Quintela
    .minimum_version_id_old = 1,
86 2d1e9f96 Juan Quintela
    .fields      = (VMStateField []) {
87 2d1e9f96 Juan Quintela
        VMSTATE_INT32_EQUAL(nirq, PCIBus),
88 c7bde572 Juan Quintela
        VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
89 2d1e9f96 Juan Quintela
        VMSTATE_END_OF_LIST()
90 52fc1d83 balrog
    }
91 2d1e9f96 Juan Quintela
};
92 52fc1d83 balrog
93 b3b11697 Isaku Yamahata
static int pci_bar(PCIDevice *d, int reg)
94 5330de09 Michael S. Tsirkin
{
95 b3b11697 Isaku Yamahata
    uint8_t type;
96 b3b11697 Isaku Yamahata
97 b3b11697 Isaku Yamahata
    if (reg != PCI_ROM_SLOT)
98 b3b11697 Isaku Yamahata
        return PCI_BASE_ADDRESS_0 + reg * 4;
99 b3b11697 Isaku Yamahata
100 b3b11697 Isaku Yamahata
    type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
101 b3b11697 Isaku Yamahata
    return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
102 5330de09 Michael S. Tsirkin
}
103 5330de09 Michael S. Tsirkin
104 d036bb21 Michael S. Tsirkin
static inline int pci_irq_state(PCIDevice *d, int irq_num)
105 d036bb21 Michael S. Tsirkin
{
106 d036bb21 Michael S. Tsirkin
        return (d->irq_state >> irq_num) & 0x1;
107 d036bb21 Michael S. Tsirkin
}
108 d036bb21 Michael S. Tsirkin
109 d036bb21 Michael S. Tsirkin
static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
110 d036bb21 Michael S. Tsirkin
{
111 d036bb21 Michael S. Tsirkin
        d->irq_state &= ~(0x1 << irq_num);
112 d036bb21 Michael S. Tsirkin
        d->irq_state |= level << irq_num;
113 d036bb21 Michael S. Tsirkin
}
114 d036bb21 Michael S. Tsirkin
115 d036bb21 Michael S. Tsirkin
static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
116 d036bb21 Michael S. Tsirkin
{
117 d036bb21 Michael S. Tsirkin
    PCIBus *bus;
118 d036bb21 Michael S. Tsirkin
    for (;;) {
119 d036bb21 Michael S. Tsirkin
        bus = pci_dev->bus;
120 d036bb21 Michael S. Tsirkin
        irq_num = bus->map_irq(pci_dev, irq_num);
121 d036bb21 Michael S. Tsirkin
        if (bus->set_irq)
122 d036bb21 Michael S. Tsirkin
            break;
123 d036bb21 Michael S. Tsirkin
        pci_dev = bus->parent_dev;
124 d036bb21 Michael S. Tsirkin
    }
125 d036bb21 Michael S. Tsirkin
    bus->irq_count[irq_num] += change;
126 d036bb21 Michael S. Tsirkin
    bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
127 d036bb21 Michael S. Tsirkin
}
128 d036bb21 Michael S. Tsirkin
129 9ddf8437 Isaku Yamahata
int pci_bus_get_irq_level(PCIBus *bus, int irq_num)
130 9ddf8437 Isaku Yamahata
{
131 9ddf8437 Isaku Yamahata
    assert(irq_num >= 0);
132 9ddf8437 Isaku Yamahata
    assert(irq_num < bus->nirq);
133 9ddf8437 Isaku Yamahata
    return !!bus->irq_count[irq_num];
134 9ddf8437 Isaku Yamahata
}
135 9ddf8437 Isaku Yamahata
136 f9bf77dd Michael S. Tsirkin
/* Update interrupt status bit in config space on interrupt
137 f9bf77dd Michael S. Tsirkin
 * state change. */
138 f9bf77dd Michael S. Tsirkin
static void pci_update_irq_status(PCIDevice *dev)
139 f9bf77dd Michael S. Tsirkin
{
140 f9bf77dd Michael S. Tsirkin
    if (dev->irq_state) {
141 f9bf77dd Michael S. Tsirkin
        dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
142 f9bf77dd Michael S. Tsirkin
    } else {
143 f9bf77dd Michael S. Tsirkin
        dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
144 f9bf77dd Michael S. Tsirkin
    }
145 f9bf77dd Michael S. Tsirkin
}
146 f9bf77dd Michael S. Tsirkin
147 4c92325b Isaku Yamahata
void pci_device_deassert_intx(PCIDevice *dev)
148 4c92325b Isaku Yamahata
{
149 4c92325b Isaku Yamahata
    int i;
150 4c92325b Isaku Yamahata
    for (i = 0; i < PCI_NUM_PINS; ++i) {
151 4c92325b Isaku Yamahata
        qemu_set_irq(dev->irq[i], 0);
152 4c92325b Isaku Yamahata
    }
153 4c92325b Isaku Yamahata
}
154 4c92325b Isaku Yamahata
155 0ead87c8 Isaku Yamahata
/*
156 0ead87c8 Isaku Yamahata
 * This function is called on #RST and FLR.
157 0ead87c8 Isaku Yamahata
 * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
158 0ead87c8 Isaku Yamahata
 */
159 0ead87c8 Isaku Yamahata
void pci_device_reset(PCIDevice *dev)
160 5330de09 Michael S. Tsirkin
{
161 c0b1905b Michael S. Tsirkin
    int r;
162 9bb33586 Isaku Yamahata
    /* TODO: call the below unconditionally once all pci devices
163 9bb33586 Isaku Yamahata
     * are qdevified */
164 9bb33586 Isaku Yamahata
    if (dev->qdev.info) {
165 9bb33586 Isaku Yamahata
        qdev_reset_all(&dev->qdev);
166 9bb33586 Isaku Yamahata
    }
167 c0b1905b Michael S. Tsirkin
168 d036bb21 Michael S. Tsirkin
    dev->irq_state = 0;
169 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(dev);
170 4c92325b Isaku Yamahata
    pci_device_deassert_intx(dev);
171 ebabb67a Stefan Weil
    /* Clear all writable bits */
172 99443c21 Isaku Yamahata
    pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
173 f9aebe2e Michael S. Tsirkin
                                 pci_get_word(dev->wmask + PCI_COMMAND) |
174 f9aebe2e Michael S. Tsirkin
                                 pci_get_word(dev->w1cmask + PCI_COMMAND));
175 89d437df Isaku Yamahata
    pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
176 89d437df Isaku Yamahata
                                 pci_get_word(dev->wmask + PCI_STATUS) |
177 89d437df Isaku Yamahata
                                 pci_get_word(dev->w1cmask + PCI_STATUS));
178 c0b1905b Michael S. Tsirkin
    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
179 c0b1905b Michael S. Tsirkin
    dev->config[PCI_INTERRUPT_LINE] = 0x0;
180 c0b1905b Michael S. Tsirkin
    for (r = 0; r < PCI_NUM_REGIONS; ++r) {
181 71ebd6dc Isaku Yamahata
        PCIIORegion *region = &dev->io_regions[r];
182 71ebd6dc Isaku Yamahata
        if (!region->size) {
183 c0b1905b Michael S. Tsirkin
            continue;
184 c0b1905b Michael S. Tsirkin
        }
185 71ebd6dc Isaku Yamahata
186 71ebd6dc Isaku Yamahata
        if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
187 71ebd6dc Isaku Yamahata
            region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
188 71ebd6dc Isaku Yamahata
            pci_set_quad(dev->config + pci_bar(dev, r), region->type);
189 71ebd6dc Isaku Yamahata
        } else {
190 71ebd6dc Isaku Yamahata
            pci_set_long(dev->config + pci_bar(dev, r), region->type);
191 71ebd6dc Isaku Yamahata
        }
192 c0b1905b Michael S. Tsirkin
    }
193 c0b1905b Michael S. Tsirkin
    pci_update_mappings(dev);
194 5330de09 Michael S. Tsirkin
}
195 5330de09 Michael S. Tsirkin
196 9bb33586 Isaku Yamahata
/*
197 9bb33586 Isaku Yamahata
 * Trigger pci bus reset under a given bus.
198 9bb33586 Isaku Yamahata
 * To be called on RST# assert.
199 9bb33586 Isaku Yamahata
 */
200 9bb33586 Isaku Yamahata
void pci_bus_reset(PCIBus *bus)
201 6eaa6847 Gleb Natapov
{
202 6eaa6847 Gleb Natapov
    int i;
203 6eaa6847 Gleb Natapov
204 6eaa6847 Gleb Natapov
    for (i = 0; i < bus->nirq; i++) {
205 6eaa6847 Gleb Natapov
        bus->irq_count[i] = 0;
206 6eaa6847 Gleb Natapov
    }
207 5330de09 Michael S. Tsirkin
    for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
208 5330de09 Michael S. Tsirkin
        if (bus->devices[i]) {
209 5330de09 Michael S. Tsirkin
            pci_device_reset(bus->devices[i]);
210 5330de09 Michael S. Tsirkin
        }
211 6eaa6847 Gleb Natapov
    }
212 6eaa6847 Gleb Natapov
}
213 6eaa6847 Gleb Natapov
214 9bb33586 Isaku Yamahata
static int pcibus_reset(BusState *qbus)
215 9bb33586 Isaku Yamahata
{
216 9bb33586 Isaku Yamahata
    pci_bus_reset(DO_UPCAST(PCIBus, qbus, qbus));
217 9bb33586 Isaku Yamahata
218 9bb33586 Isaku Yamahata
    /* topology traverse is done by pci_bus_reset().
219 9bb33586 Isaku Yamahata
       Tell qbus/qdev walker not to traverse the tree */
220 9bb33586 Isaku Yamahata
    return 1;
221 9bb33586 Isaku Yamahata
}
222 9bb33586 Isaku Yamahata
223 e822a52a Isaku Yamahata
static void pci_host_bus_register(int domain, PCIBus *bus)
224 e822a52a Isaku Yamahata
{
225 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
226 e822a52a Isaku Yamahata
    host = qemu_mallocz(sizeof(*host));
227 e822a52a Isaku Yamahata
    host->domain = domain;
228 e822a52a Isaku Yamahata
    host->bus = bus;
229 e822a52a Isaku Yamahata
    QLIST_INSERT_HEAD(&host_buses, host, next);
230 e822a52a Isaku Yamahata
}
231 e822a52a Isaku Yamahata
232 c469e1dd Isaku Yamahata
PCIBus *pci_find_root_bus(int domain)
233 e822a52a Isaku Yamahata
{
234 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
235 e822a52a Isaku Yamahata
236 e822a52a Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
237 e822a52a Isaku Yamahata
        if (host->domain == domain) {
238 e822a52a Isaku Yamahata
            return host->bus;
239 e822a52a Isaku Yamahata
        }
240 e822a52a Isaku Yamahata
    }
241 e822a52a Isaku Yamahata
242 e822a52a Isaku Yamahata
    return NULL;
243 e822a52a Isaku Yamahata
}
244 e822a52a Isaku Yamahata
245 e075e788 Isaku Yamahata
int pci_find_domain(const PCIBus *bus)
246 e075e788 Isaku Yamahata
{
247 e075e788 Isaku Yamahata
    PCIDevice *d;
248 e075e788 Isaku Yamahata
    struct PCIHostBus *host;
249 e075e788 Isaku Yamahata
250 e075e788 Isaku Yamahata
    /* obtain root bus */
251 e075e788 Isaku Yamahata
    while ((d = bus->parent_dev) != NULL) {
252 e075e788 Isaku Yamahata
        bus = d->bus;
253 e075e788 Isaku Yamahata
    }
254 e075e788 Isaku Yamahata
255 e075e788 Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
256 e075e788 Isaku Yamahata
        if (host->bus == bus) {
257 e075e788 Isaku Yamahata
            return host->domain;
258 e075e788 Isaku Yamahata
        }
259 e075e788 Isaku Yamahata
    }
260 e075e788 Isaku Yamahata
261 e075e788 Isaku Yamahata
    abort();    /* should not be reached */
262 e075e788 Isaku Yamahata
    return -1;
263 e075e788 Isaku Yamahata
}
264 e075e788 Isaku Yamahata
265 21eea4b3 Gerd Hoffmann
void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
266 6f3279b5 Isaku Yamahata
                         const char *name, uint8_t devfn_min)
267 30468f78 bellard
{
268 21eea4b3 Gerd Hoffmann
    qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
269 6fa84913 Isaku Yamahata
    assert(PCI_FUNC(devfn_min) == 0);
270 502a5395 pbrook
    bus->devfn_min = devfn_min;
271 e822a52a Isaku Yamahata
272 e822a52a Isaku Yamahata
    /* host bridge */
273 e822a52a Isaku Yamahata
    QLIST_INIT(&bus->child);
274 e822a52a Isaku Yamahata
    pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
275 e822a52a Isaku Yamahata
276 0be71e32 Alex Williamson
    vmstate_register(NULL, -1, &vmstate_pcibus, bus);
277 21eea4b3 Gerd Hoffmann
}
278 21eea4b3 Gerd Hoffmann
279 6f3279b5 Isaku Yamahata
PCIBus *pci_bus_new(DeviceState *parent, const char *name, uint8_t devfn_min)
280 21eea4b3 Gerd Hoffmann
{
281 21eea4b3 Gerd Hoffmann
    PCIBus *bus;
282 21eea4b3 Gerd Hoffmann
283 21eea4b3 Gerd Hoffmann
    bus = qemu_mallocz(sizeof(*bus));
284 21eea4b3 Gerd Hoffmann
    bus->qbus.qdev_allocated = 1;
285 21eea4b3 Gerd Hoffmann
    pci_bus_new_inplace(bus, parent, name, devfn_min);
286 21eea4b3 Gerd Hoffmann
    return bus;
287 21eea4b3 Gerd Hoffmann
}
288 21eea4b3 Gerd Hoffmann
289 21eea4b3 Gerd Hoffmann
void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
290 21eea4b3 Gerd Hoffmann
                  void *irq_opaque, int nirq)
291 21eea4b3 Gerd Hoffmann
{
292 21eea4b3 Gerd Hoffmann
    bus->set_irq = set_irq;
293 21eea4b3 Gerd Hoffmann
    bus->map_irq = map_irq;
294 21eea4b3 Gerd Hoffmann
    bus->irq_opaque = irq_opaque;
295 21eea4b3 Gerd Hoffmann
    bus->nirq = nirq;
296 21eea4b3 Gerd Hoffmann
    bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
297 21eea4b3 Gerd Hoffmann
}
298 21eea4b3 Gerd Hoffmann
299 87c30546 Isaku Yamahata
void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug, DeviceState *qdev)
300 ee995ffb Gerd Hoffmann
{
301 ee995ffb Gerd Hoffmann
    bus->qbus.allow_hotplug = 1;
302 ee995ffb Gerd Hoffmann
    bus->hotplug = hotplug;
303 87c30546 Isaku Yamahata
    bus->hotplug_qdev = qdev;
304 ee995ffb Gerd Hoffmann
}
305 ee995ffb Gerd Hoffmann
306 2e01c8cf Blue Swirl
void pci_bus_set_mem_base(PCIBus *bus, target_phys_addr_t base)
307 2e01c8cf Blue Swirl
{
308 2e01c8cf Blue Swirl
    bus->mem_base = base;
309 2e01c8cf Blue Swirl
}
310 2e01c8cf Blue Swirl
311 21eea4b3 Gerd Hoffmann
PCIBus *pci_register_bus(DeviceState *parent, const char *name,
312 21eea4b3 Gerd Hoffmann
                         pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
313 6f3279b5 Isaku Yamahata
                         void *irq_opaque, uint8_t devfn_min, int nirq)
314 21eea4b3 Gerd Hoffmann
{
315 21eea4b3 Gerd Hoffmann
    PCIBus *bus;
316 21eea4b3 Gerd Hoffmann
317 21eea4b3 Gerd Hoffmann
    bus = pci_bus_new(parent, name, devfn_min);
318 21eea4b3 Gerd Hoffmann
    pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
319 30468f78 bellard
    return bus;
320 30468f78 bellard
}
321 69b91039 bellard
322 502a5395 pbrook
int pci_bus_num(PCIBus *s)
323 502a5395 pbrook
{
324 e94ff650 Isaku Yamahata
    if (!s->parent_dev)
325 e94ff650 Isaku Yamahata
        return 0;       /* pci host bridge */
326 e94ff650 Isaku Yamahata
    return s->parent_dev->config[PCI_SECONDARY_BUS];
327 502a5395 pbrook
}
328 502a5395 pbrook
329 73534f2f Juan Quintela
static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
330 30ca2aab bellard
{
331 73534f2f Juan Quintela
    PCIDevice *s = container_of(pv, PCIDevice, config);
332 a9f49946 Isaku Yamahata
    uint8_t *config;
333 52fc1d83 balrog
    int i;
334 52fc1d83 balrog
335 a9f49946 Isaku Yamahata
    assert(size == pci_config_size(s));
336 a9f49946 Isaku Yamahata
    config = qemu_malloc(size);
337 a9f49946 Isaku Yamahata
338 a9f49946 Isaku Yamahata
    qemu_get_buffer(f, config, size);
339 a9f49946 Isaku Yamahata
    for (i = 0; i < size; ++i) {
340 f9aebe2e Michael S. Tsirkin
        if ((config[i] ^ s->config[i]) &
341 f9aebe2e Michael S. Tsirkin
            s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
342 a9f49946 Isaku Yamahata
            qemu_free(config);
343 bd4b65ee Michael S. Tsirkin
            return -EINVAL;
344 a9f49946 Isaku Yamahata
        }
345 a9f49946 Isaku Yamahata
    }
346 a9f49946 Isaku Yamahata
    memcpy(s->config, config, size);
347 bd4b65ee Michael S. Tsirkin
348 1941d19c bellard
    pci_update_mappings(s);
349 52fc1d83 balrog
350 a9f49946 Isaku Yamahata
    qemu_free(config);
351 30ca2aab bellard
    return 0;
352 30ca2aab bellard
}
353 30ca2aab bellard
354 73534f2f Juan Quintela
/* just put buffer */
355 84e2e3eb Juan Quintela
static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
356 73534f2f Juan Quintela
{
357 dbe73d7f Juan Quintela
    const uint8_t **v = pv;
358 a9f49946 Isaku Yamahata
    assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
359 dbe73d7f Juan Quintela
    qemu_put_buffer(f, *v, size);
360 73534f2f Juan Quintela
}
361 73534f2f Juan Quintela
362 73534f2f Juan Quintela
static VMStateInfo vmstate_info_pci_config = {
363 73534f2f Juan Quintela
    .name = "pci config",
364 73534f2f Juan Quintela
    .get  = get_pci_config_device,
365 73534f2f Juan Quintela
    .put  = put_pci_config_device,
366 73534f2f Juan Quintela
};
367 73534f2f Juan Quintela
368 d036bb21 Michael S. Tsirkin
static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
369 d036bb21 Michael S. Tsirkin
{
370 c3f8f611 Michael S. Tsirkin
    PCIDevice *s = container_of(pv, PCIDevice, irq_state);
371 d036bb21 Michael S. Tsirkin
    uint32_t irq_state[PCI_NUM_PINS];
372 d036bb21 Michael S. Tsirkin
    int i;
373 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
374 d036bb21 Michael S. Tsirkin
        irq_state[i] = qemu_get_be32(f);
375 d036bb21 Michael S. Tsirkin
        if (irq_state[i] != 0x1 && irq_state[i] != 0) {
376 d036bb21 Michael S. Tsirkin
            fprintf(stderr, "irq state %d: must be 0 or 1.\n",
377 d036bb21 Michael S. Tsirkin
                    irq_state[i]);
378 d036bb21 Michael S. Tsirkin
            return -EINVAL;
379 d036bb21 Michael S. Tsirkin
        }
380 d036bb21 Michael S. Tsirkin
    }
381 d036bb21 Michael S. Tsirkin
382 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
383 d036bb21 Michael S. Tsirkin
        pci_set_irq_state(s, i, irq_state[i]);
384 d036bb21 Michael S. Tsirkin
    }
385 d036bb21 Michael S. Tsirkin
386 d036bb21 Michael S. Tsirkin
    return 0;
387 d036bb21 Michael S. Tsirkin
}
388 d036bb21 Michael S. Tsirkin
389 d036bb21 Michael S. Tsirkin
static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
390 d036bb21 Michael S. Tsirkin
{
391 d036bb21 Michael S. Tsirkin
    int i;
392 c3f8f611 Michael S. Tsirkin
    PCIDevice *s = container_of(pv, PCIDevice, irq_state);
393 d036bb21 Michael S. Tsirkin
394 d036bb21 Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
395 d036bb21 Michael S. Tsirkin
        qemu_put_be32(f, pci_irq_state(s, i));
396 d036bb21 Michael S. Tsirkin
    }
397 d036bb21 Michael S. Tsirkin
}
398 d036bb21 Michael S. Tsirkin
399 d036bb21 Michael S. Tsirkin
static VMStateInfo vmstate_info_pci_irq_state = {
400 d036bb21 Michael S. Tsirkin
    .name = "pci irq state",
401 d036bb21 Michael S. Tsirkin
    .get  = get_pci_irq_state,
402 d036bb21 Michael S. Tsirkin
    .put  = put_pci_irq_state,
403 d036bb21 Michael S. Tsirkin
};
404 d036bb21 Michael S. Tsirkin
405 73534f2f Juan Quintela
const VMStateDescription vmstate_pci_device = {
406 73534f2f Juan Quintela
    .name = "PCIDevice",
407 73534f2f Juan Quintela
    .version_id = 2,
408 73534f2f Juan Quintela
    .minimum_version_id = 1,
409 73534f2f Juan Quintela
    .minimum_version_id_old = 1,
410 73534f2f Juan Quintela
    .fields      = (VMStateField []) {
411 73534f2f Juan Quintela
        VMSTATE_INT32_LE(version_id, PCIDevice),
412 a9f49946 Isaku Yamahata
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
413 a9f49946 Isaku Yamahata
                                   vmstate_info_pci_config,
414 a9f49946 Isaku Yamahata
                                   PCI_CONFIG_SPACE_SIZE),
415 d036bb21 Michael S. Tsirkin
        VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
416 d036bb21 Michael S. Tsirkin
                                   vmstate_info_pci_irq_state,
417 d036bb21 Michael S. Tsirkin
                                   PCI_NUM_PINS * sizeof(int32_t)),
418 a9f49946 Isaku Yamahata
        VMSTATE_END_OF_LIST()
419 a9f49946 Isaku Yamahata
    }
420 a9f49946 Isaku Yamahata
};
421 a9f49946 Isaku Yamahata
422 a9f49946 Isaku Yamahata
const VMStateDescription vmstate_pcie_device = {
423 a9f49946 Isaku Yamahata
    .name = "PCIDevice",
424 a9f49946 Isaku Yamahata
    .version_id = 2,
425 a9f49946 Isaku Yamahata
    .minimum_version_id = 1,
426 a9f49946 Isaku Yamahata
    .minimum_version_id_old = 1,
427 a9f49946 Isaku Yamahata
    .fields      = (VMStateField []) {
428 a9f49946 Isaku Yamahata
        VMSTATE_INT32_LE(version_id, PCIDevice),
429 a9f49946 Isaku Yamahata
        VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
430 a9f49946 Isaku Yamahata
                                   vmstate_info_pci_config,
431 a9f49946 Isaku Yamahata
                                   PCIE_CONFIG_SPACE_SIZE),
432 d036bb21 Michael S. Tsirkin
        VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
433 d036bb21 Michael S. Tsirkin
                                   vmstate_info_pci_irq_state,
434 d036bb21 Michael S. Tsirkin
                                   PCI_NUM_PINS * sizeof(int32_t)),
435 73534f2f Juan Quintela
        VMSTATE_END_OF_LIST()
436 73534f2f Juan Quintela
    }
437 73534f2f Juan Quintela
};
438 73534f2f Juan Quintela
439 a9f49946 Isaku Yamahata
static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
440 a9f49946 Isaku Yamahata
{
441 a9f49946 Isaku Yamahata
    return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
442 a9f49946 Isaku Yamahata
}
443 a9f49946 Isaku Yamahata
444 73534f2f Juan Quintela
void pci_device_save(PCIDevice *s, QEMUFile *f)
445 73534f2f Juan Quintela
{
446 f9bf77dd Michael S. Tsirkin
    /* Clear interrupt status bit: it is implicit
447 f9bf77dd Michael S. Tsirkin
     * in irq_state which we are saving.
448 f9bf77dd Michael S. Tsirkin
     * This makes us compatible with old devices
449 f9bf77dd Michael S. Tsirkin
     * which never set or clear this bit. */
450 f9bf77dd Michael S. Tsirkin
    s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
451 a9f49946 Isaku Yamahata
    vmstate_save_state(f, pci_get_vmstate(s), s);
452 f9bf77dd Michael S. Tsirkin
    /* Restore the interrupt status bit. */
453 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(s);
454 73534f2f Juan Quintela
}
455 73534f2f Juan Quintela
456 73534f2f Juan Quintela
int pci_device_load(PCIDevice *s, QEMUFile *f)
457 73534f2f Juan Quintela
{
458 f9bf77dd Michael S. Tsirkin
    int ret;
459 f9bf77dd Michael S. Tsirkin
    ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
460 f9bf77dd Michael S. Tsirkin
    /* Restore the interrupt status bit. */
461 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(s);
462 f9bf77dd Michael S. Tsirkin
    return ret;
463 73534f2f Juan Quintela
}
464 73534f2f Juan Quintela
465 5e434f4e Isaku Yamahata
static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
466 d350d97d aliguori
{
467 5e434f4e Isaku Yamahata
    pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
468 5e434f4e Isaku Yamahata
                 pci_default_sub_vendor_id);
469 5e434f4e Isaku Yamahata
    pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
470 5e434f4e Isaku Yamahata
                 pci_default_sub_device_id);
471 d350d97d aliguori
}
472 d350d97d aliguori
473 880345c4 aliguori
/*
474 43c945f1 Isaku Yamahata
 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
475 43c945f1 Isaku Yamahata
 *       [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
476 880345c4 aliguori
 */
477 43c945f1 Isaku Yamahata
int pci_parse_devaddr(const char *addr, int *domp, int *busp,
478 43c945f1 Isaku Yamahata
                      unsigned int *slotp, unsigned int *funcp)
479 880345c4 aliguori
{
480 880345c4 aliguori
    const char *p;
481 880345c4 aliguori
    char *e;
482 880345c4 aliguori
    unsigned long val;
483 880345c4 aliguori
    unsigned long dom = 0, bus = 0;
484 43c945f1 Isaku Yamahata
    unsigned int slot = 0;
485 43c945f1 Isaku Yamahata
    unsigned int func = 0;
486 880345c4 aliguori
487 880345c4 aliguori
    p = addr;
488 880345c4 aliguori
    val = strtoul(p, &e, 16);
489 880345c4 aliguori
    if (e == p)
490 880345c4 aliguori
        return -1;
491 880345c4 aliguori
    if (*e == ':') {
492 880345c4 aliguori
        bus = val;
493 880345c4 aliguori
        p = e + 1;
494 880345c4 aliguori
        val = strtoul(p, &e, 16);
495 880345c4 aliguori
        if (e == p)
496 880345c4 aliguori
            return -1;
497 880345c4 aliguori
        if (*e == ':') {
498 880345c4 aliguori
            dom = bus;
499 880345c4 aliguori
            bus = val;
500 880345c4 aliguori
            p = e + 1;
501 880345c4 aliguori
            val = strtoul(p, &e, 16);
502 880345c4 aliguori
            if (e == p)
503 880345c4 aliguori
                return -1;
504 880345c4 aliguori
        }
505 880345c4 aliguori
    }
506 880345c4 aliguori
507 880345c4 aliguori
    slot = val;
508 880345c4 aliguori
509 43c945f1 Isaku Yamahata
    if (funcp != NULL) {
510 43c945f1 Isaku Yamahata
        if (*e != '.')
511 43c945f1 Isaku Yamahata
            return -1;
512 43c945f1 Isaku Yamahata
513 43c945f1 Isaku Yamahata
        p = e + 1;
514 43c945f1 Isaku Yamahata
        val = strtoul(p, &e, 16);
515 43c945f1 Isaku Yamahata
        if (e == p)
516 43c945f1 Isaku Yamahata
            return -1;
517 43c945f1 Isaku Yamahata
518 43c945f1 Isaku Yamahata
        func = val;
519 43c945f1 Isaku Yamahata
    }
520 43c945f1 Isaku Yamahata
521 43c945f1 Isaku Yamahata
    /* if funcp == NULL func is 0 */
522 43c945f1 Isaku Yamahata
    if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
523 43c945f1 Isaku Yamahata
        return -1;
524 43c945f1 Isaku Yamahata
525 880345c4 aliguori
    if (*e)
526 880345c4 aliguori
        return -1;
527 880345c4 aliguori
528 880345c4 aliguori
    /* Note: QEMU doesn't implement domains other than 0 */
529 c469e1dd Isaku Yamahata
    if (!pci_find_bus(pci_find_root_bus(dom), bus))
530 880345c4 aliguori
        return -1;
531 880345c4 aliguori
532 880345c4 aliguori
    *domp = dom;
533 880345c4 aliguori
    *busp = bus;
534 880345c4 aliguori
    *slotp = slot;
535 43c945f1 Isaku Yamahata
    if (funcp != NULL)
536 43c945f1 Isaku Yamahata
        *funcp = func;
537 880345c4 aliguori
    return 0;
538 880345c4 aliguori
}
539 880345c4 aliguori
540 e9283f8b Jan Kiszka
int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
541 e9283f8b Jan Kiszka
                     unsigned *slotp)
542 880345c4 aliguori
{
543 e9283f8b Jan Kiszka
    /* strip legacy tag */
544 e9283f8b Jan Kiszka
    if (!strncmp(addr, "pci_addr=", 9)) {
545 e9283f8b Jan Kiszka
        addr += 9;
546 e9283f8b Jan Kiszka
    }
547 43c945f1 Isaku Yamahata
    if (pci_parse_devaddr(addr, domp, busp, slotp, NULL)) {
548 e9283f8b Jan Kiszka
        monitor_printf(mon, "Invalid pci address\n");
549 880345c4 aliguori
        return -1;
550 e9283f8b Jan Kiszka
    }
551 e9283f8b Jan Kiszka
    return 0;
552 880345c4 aliguori
}
553 880345c4 aliguori
554 49bd1458 Markus Armbruster
PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
555 5607c388 Markus Armbruster
{
556 5607c388 Markus Armbruster
    int dom, bus;
557 5607c388 Markus Armbruster
    unsigned slot;
558 5607c388 Markus Armbruster
559 5607c388 Markus Armbruster
    if (!devaddr) {
560 5607c388 Markus Armbruster
        *devfnp = -1;
561 c469e1dd Isaku Yamahata
        return pci_find_bus(pci_find_root_bus(0), 0);
562 5607c388 Markus Armbruster
    }
563 5607c388 Markus Armbruster
564 43c945f1 Isaku Yamahata
    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
565 5607c388 Markus Armbruster
        return NULL;
566 5607c388 Markus Armbruster
    }
567 5607c388 Markus Armbruster
568 6ff534b6 Isaku Yamahata
    *devfnp = PCI_DEVFN(slot, 0);
569 e075e788 Isaku Yamahata
    return pci_find_bus(pci_find_root_bus(dom), bus);
570 5607c388 Markus Armbruster
}
571 5607c388 Markus Armbruster
572 bd4b65ee Michael S. Tsirkin
static void pci_init_cmask(PCIDevice *dev)
573 bd4b65ee Michael S. Tsirkin
{
574 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
575 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
576 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
577 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_REVISION_ID] = 0xff;
578 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_CLASS_PROG] = 0xff;
579 bd4b65ee Michael S. Tsirkin
    pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
580 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_HEADER_TYPE] = 0xff;
581 bd4b65ee Michael S. Tsirkin
    dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
582 bd4b65ee Michael S. Tsirkin
}
583 bd4b65ee Michael S. Tsirkin
584 b7ee1603 Michael S. Tsirkin
static void pci_init_wmask(PCIDevice *dev)
585 b7ee1603 Michael S. Tsirkin
{
586 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(dev);
587 a9f49946 Isaku Yamahata
588 b7ee1603 Michael S. Tsirkin
    dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
589 b7ee1603 Michael S. Tsirkin
    dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
590 67a51b48 Isaku Yamahata
    pci_set_word(dev->wmask + PCI_COMMAND,
591 a7b15a5c Michael S. Tsirkin
                 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
592 a7b15a5c Michael S. Tsirkin
                 PCI_COMMAND_INTX_DISABLE);
593 b1aeb926 Isaku Yamahata
    if (dev->cap_present & QEMU_PCI_CAP_SERR) {
594 b1aeb926 Isaku Yamahata
        pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
595 b1aeb926 Isaku Yamahata
    }
596 3e21ffc9 Isaku Yamahata
597 3e21ffc9 Isaku Yamahata
    memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
598 3e21ffc9 Isaku Yamahata
           config_size - PCI_CONFIG_HEADER_SIZE);
599 b7ee1603 Michael S. Tsirkin
}
600 b7ee1603 Michael S. Tsirkin
601 89d437df Isaku Yamahata
static void pci_init_w1cmask(PCIDevice *dev)
602 89d437df Isaku Yamahata
{
603 89d437df Isaku Yamahata
    /*
604 f6bdfcc9 Michael S. Tsirkin
     * Note: It's okay to set w1cmask even for readonly bits as
605 89d437df Isaku Yamahata
     * long as their value is hardwired to 0.
606 89d437df Isaku Yamahata
     */
607 89d437df Isaku Yamahata
    pci_set_word(dev->w1cmask + PCI_STATUS,
608 89d437df Isaku Yamahata
                 PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
609 89d437df Isaku Yamahata
                 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
610 89d437df Isaku Yamahata
                 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
611 89d437df Isaku Yamahata
}
612 89d437df Isaku Yamahata
613 fb231628 Isaku Yamahata
static void pci_init_wmask_bridge(PCIDevice *d)
614 fb231628 Isaku Yamahata
{
615 fb231628 Isaku Yamahata
    /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
616 fb231628 Isaku Yamahata
       PCI_SEC_LETENCY_TIMER */
617 fb231628 Isaku Yamahata
    memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
618 fb231628 Isaku Yamahata
619 fb231628 Isaku Yamahata
    /* base and limit */
620 fb231628 Isaku Yamahata
    d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
621 fb231628 Isaku Yamahata
    d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
622 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_MEMORY_BASE,
623 fb231628 Isaku Yamahata
                 PCI_MEMORY_RANGE_MASK & 0xffff);
624 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
625 fb231628 Isaku Yamahata
                 PCI_MEMORY_RANGE_MASK & 0xffff);
626 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
627 fb231628 Isaku Yamahata
                 PCI_PREF_RANGE_MASK & 0xffff);
628 fb231628 Isaku Yamahata
    pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
629 fb231628 Isaku Yamahata
                 PCI_PREF_RANGE_MASK & 0xffff);
630 fb231628 Isaku Yamahata
631 fb231628 Isaku Yamahata
    /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
632 fb231628 Isaku Yamahata
    memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
633 fb231628 Isaku Yamahata
634 f6bdfcc9 Michael S. Tsirkin
/* TODO: add this define to pci_regs.h in linux and then in qemu. */
635 f6bdfcc9 Michael S. Tsirkin
#define  PCI_BRIDGE_CTL_VGA_16BIT        0x10        /* VGA 16-bit decode */
636 f6bdfcc9 Michael S. Tsirkin
#define  PCI_BRIDGE_CTL_DISCARD                0x100        /* Primary discard timer */
637 f6bdfcc9 Michael S. Tsirkin
#define  PCI_BRIDGE_CTL_SEC_DISCARD        0x200        /* Secondary discard timer */
638 f6bdfcc9 Michael S. Tsirkin
#define  PCI_BRIDGE_CTL_DISCARD_STATUS        0x400        /* Discard timer status */
639 f6bdfcc9 Michael S. Tsirkin
#define  PCI_BRIDGE_CTL_DISCARD_SERR        0x800        /* Discard timer SERR# enable */
640 f6bdfcc9 Michael S. Tsirkin
    pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
641 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_PARITY |
642 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_SERR |
643 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_ISA |
644 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_VGA |
645 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_VGA_16BIT |
646 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_MASTER_ABORT |
647 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_BUS_RESET |
648 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_FAST_BACK |
649 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_DISCARD |
650 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_SEC_DISCARD |
651 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_DISCARD_SERR);
652 f6bdfcc9 Michael S. Tsirkin
    /* Below does not do anything as we never set this bit, put here for
653 f6bdfcc9 Michael S. Tsirkin
     * completeness. */
654 f6bdfcc9 Michael S. Tsirkin
    pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
655 f6bdfcc9 Michael S. Tsirkin
                 PCI_BRIDGE_CTL_DISCARD_STATUS);
656 fb231628 Isaku Yamahata
}
657 fb231628 Isaku Yamahata
658 6eab3de1 Isaku Yamahata
static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev)
659 6eab3de1 Isaku Yamahata
{
660 6eab3de1 Isaku Yamahata
    uint8_t slot = PCI_SLOT(dev->devfn);
661 6eab3de1 Isaku Yamahata
    uint8_t func;
662 6eab3de1 Isaku Yamahata
663 6eab3de1 Isaku Yamahata
    if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
664 6eab3de1 Isaku Yamahata
        dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
665 6eab3de1 Isaku Yamahata
    }
666 6eab3de1 Isaku Yamahata
667 6eab3de1 Isaku Yamahata
    /*
668 b0cd712c Stefan Weil
     * multifunction bit is interpreted in two ways as follows.
669 6eab3de1 Isaku Yamahata
     *   - all functions must set the bit to 1.
670 6eab3de1 Isaku Yamahata
     *     Example: Intel X53
671 6eab3de1 Isaku Yamahata
     *   - function 0 must set the bit, but the rest function (> 0)
672 6eab3de1 Isaku Yamahata
     *     is allowed to leave the bit to 0.
673 6eab3de1 Isaku Yamahata
     *     Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
674 6eab3de1 Isaku Yamahata
     *
675 6eab3de1 Isaku Yamahata
     * So OS (at least Linux) checks the bit of only function 0,
676 6eab3de1 Isaku Yamahata
     * and doesn't see the bit of function > 0.
677 6eab3de1 Isaku Yamahata
     *
678 6eab3de1 Isaku Yamahata
     * The below check allows both interpretation.
679 6eab3de1 Isaku Yamahata
     */
680 6eab3de1 Isaku Yamahata
    if (PCI_FUNC(dev->devfn)) {
681 6eab3de1 Isaku Yamahata
        PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
682 6eab3de1 Isaku Yamahata
        if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
683 6eab3de1 Isaku Yamahata
            /* function 0 should set multifunction bit */
684 6eab3de1 Isaku Yamahata
            error_report("PCI: single function device can't be populated "
685 6eab3de1 Isaku Yamahata
                         "in function %x.%x", slot, PCI_FUNC(dev->devfn));
686 6eab3de1 Isaku Yamahata
            return -1;
687 6eab3de1 Isaku Yamahata
        }
688 6eab3de1 Isaku Yamahata
        return 0;
689 6eab3de1 Isaku Yamahata
    }
690 6eab3de1 Isaku Yamahata
691 6eab3de1 Isaku Yamahata
    if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
692 6eab3de1 Isaku Yamahata
        return 0;
693 6eab3de1 Isaku Yamahata
    }
694 6eab3de1 Isaku Yamahata
    /* function 0 indicates single function, so function > 0 must be NULL */
695 6eab3de1 Isaku Yamahata
    for (func = 1; func < PCI_FUNC_MAX; ++func) {
696 6eab3de1 Isaku Yamahata
        if (bus->devices[PCI_DEVFN(slot, func)]) {
697 6eab3de1 Isaku Yamahata
            error_report("PCI: %x.0 indicates single function, "
698 6eab3de1 Isaku Yamahata
                         "but %x.%x is already populated.",
699 6eab3de1 Isaku Yamahata
                         slot, slot, func);
700 6eab3de1 Isaku Yamahata
            return -1;
701 6eab3de1 Isaku Yamahata
        }
702 6eab3de1 Isaku Yamahata
    }
703 6eab3de1 Isaku Yamahata
    return 0;
704 6eab3de1 Isaku Yamahata
}
705 6eab3de1 Isaku Yamahata
706 a9f49946 Isaku Yamahata
static void pci_config_alloc(PCIDevice *pci_dev)
707 a9f49946 Isaku Yamahata
{
708 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(pci_dev);
709 a9f49946 Isaku Yamahata
710 a9f49946 Isaku Yamahata
    pci_dev->config = qemu_mallocz(config_size);
711 a9f49946 Isaku Yamahata
    pci_dev->cmask = qemu_mallocz(config_size);
712 a9f49946 Isaku Yamahata
    pci_dev->wmask = qemu_mallocz(config_size);
713 92ba5f51 Isaku Yamahata
    pci_dev->w1cmask = qemu_mallocz(config_size);
714 a9f49946 Isaku Yamahata
    pci_dev->used = qemu_mallocz(config_size);
715 a9f49946 Isaku Yamahata
}
716 a9f49946 Isaku Yamahata
717 a9f49946 Isaku Yamahata
static void pci_config_free(PCIDevice *pci_dev)
718 a9f49946 Isaku Yamahata
{
719 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->config);
720 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->cmask);
721 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->wmask);
722 92ba5f51 Isaku Yamahata
    qemu_free(pci_dev->w1cmask);
723 a9f49946 Isaku Yamahata
    qemu_free(pci_dev->used);
724 a9f49946 Isaku Yamahata
}
725 a9f49946 Isaku Yamahata
726 69b91039 bellard
/* -1 for devfn means auto assign */
727 6b1b92d3 Paul Brook
static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
728 6b1b92d3 Paul Brook
                                         const char *name, int devfn,
729 113f89df Isaku Yamahata
                                         const PCIDeviceInfo *info)
730 69b91039 bellard
{
731 113f89df Isaku Yamahata
    PCIConfigReadFunc *config_read = info->config_read;
732 113f89df Isaku Yamahata
    PCIConfigWriteFunc *config_write = info->config_write;
733 113f89df Isaku Yamahata
734 69b91039 bellard
    if (devfn < 0) {
735 b47b0706 Isaku Yamahata
        for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
736 6fa84913 Isaku Yamahata
            devfn += PCI_FUNC_MAX) {
737 30468f78 bellard
            if (!bus->devices[devfn])
738 69b91039 bellard
                goto found;
739 69b91039 bellard
        }
740 3709c1b7 Daniel P. Berrange
        error_report("PCI: no slot/function available for %s, all in use", name);
741 09e3acc6 Gerd Hoffmann
        return NULL;
742 69b91039 bellard
    found: ;
743 07b7d053 Markus Armbruster
    } else if (bus->devices[devfn]) {
744 3709c1b7 Daniel P. Berrange
        error_report("PCI: slot %d function %d not available for %s, in use by %s",
745 3709c1b7 Daniel P. Berrange
                     PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name);
746 09e3acc6 Gerd Hoffmann
        return NULL;
747 69b91039 bellard
    }
748 30468f78 bellard
    pci_dev->bus = bus;
749 69b91039 bellard
    pci_dev->devfn = devfn;
750 69b91039 bellard
    pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
751 d036bb21 Michael S. Tsirkin
    pci_dev->irq_state = 0;
752 a9f49946 Isaku Yamahata
    pci_config_alloc(pci_dev);
753 fb231628 Isaku Yamahata
754 113f89df Isaku Yamahata
    pci_config_set_vendor_id(pci_dev->config, info->vendor_id);
755 113f89df Isaku Yamahata
    pci_config_set_device_id(pci_dev->config, info->device_id);
756 113f89df Isaku Yamahata
    pci_config_set_revision(pci_dev->config, info->revision);
757 113f89df Isaku Yamahata
    pci_config_set_class(pci_dev->config, info->class_id);
758 113f89df Isaku Yamahata
759 113f89df Isaku Yamahata
    if (!info->is_bridge) {
760 113f89df Isaku Yamahata
        if (info->subsystem_vendor_id || info->subsystem_id) {
761 113f89df Isaku Yamahata
            pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
762 113f89df Isaku Yamahata
                         info->subsystem_vendor_id);
763 113f89df Isaku Yamahata
            pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
764 113f89df Isaku Yamahata
                         info->subsystem_id);
765 113f89df Isaku Yamahata
        } else {
766 113f89df Isaku Yamahata
            pci_set_default_subsystem_id(pci_dev);
767 113f89df Isaku Yamahata
        }
768 113f89df Isaku Yamahata
    } else {
769 113f89df Isaku Yamahata
        /* subsystem_vendor_id/subsystem_id are only for header type 0 */
770 113f89df Isaku Yamahata
        assert(!info->subsystem_vendor_id);
771 113f89df Isaku Yamahata
        assert(!info->subsystem_id);
772 fb231628 Isaku Yamahata
    }
773 bd4b65ee Michael S. Tsirkin
    pci_init_cmask(pci_dev);
774 b7ee1603 Michael S. Tsirkin
    pci_init_wmask(pci_dev);
775 89d437df Isaku Yamahata
    pci_init_w1cmask(pci_dev);
776 113f89df Isaku Yamahata
    if (info->is_bridge) {
777 fb231628 Isaku Yamahata
        pci_init_wmask_bridge(pci_dev);
778 fb231628 Isaku Yamahata
    }
779 6eab3de1 Isaku Yamahata
    if (pci_init_multifunction(bus, pci_dev)) {
780 6eab3de1 Isaku Yamahata
        pci_config_free(pci_dev);
781 6eab3de1 Isaku Yamahata
        return NULL;
782 6eab3de1 Isaku Yamahata
    }
783 0ac32c83 bellard
784 0ac32c83 bellard
    if (!config_read)
785 0ac32c83 bellard
        config_read = pci_default_read_config;
786 0ac32c83 bellard
    if (!config_write)
787 0ac32c83 bellard
        config_write = pci_default_write_config;
788 69b91039 bellard
    pci_dev->config_read = config_read;
789 69b91039 bellard
    pci_dev->config_write = config_write;
790 30468f78 bellard
    bus->devices[devfn] = pci_dev;
791 e369cad7 Isaku Yamahata
    pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
792 f16c4abf Juan Quintela
    pci_dev->version_id = 2; /* Current pci device vmstate version */
793 69b91039 bellard
    return pci_dev;
794 69b91039 bellard
}
795 69b91039 bellard
796 925fe64a Alex Williamson
static void do_pci_unregister_device(PCIDevice *pci_dev)
797 925fe64a Alex Williamson
{
798 925fe64a Alex Williamson
    qemu_free_irqs(pci_dev->irq);
799 925fe64a Alex Williamson
    pci_dev->bus->devices[pci_dev->devfn] = NULL;
800 925fe64a Alex Williamson
    pci_config_free(pci_dev);
801 925fe64a Alex Williamson
}
802 925fe64a Alex Williamson
803 113f89df Isaku Yamahata
/* TODO: obsolete. eliminate this once all pci devices are qdevifed. */
804 6b1b92d3 Paul Brook
PCIDevice *pci_register_device(PCIBus *bus, const char *name,
805 6b1b92d3 Paul Brook
                               int instance_size, int devfn,
806 6b1b92d3 Paul Brook
                               PCIConfigReadFunc *config_read,
807 6b1b92d3 Paul Brook
                               PCIConfigWriteFunc *config_write)
808 6b1b92d3 Paul Brook
{
809 6b1b92d3 Paul Brook
    PCIDevice *pci_dev;
810 113f89df Isaku Yamahata
    PCIDeviceInfo info = {
811 113f89df Isaku Yamahata
        .config_read = config_read,
812 113f89df Isaku Yamahata
        .config_write = config_write,
813 113f89df Isaku Yamahata
    };
814 6b1b92d3 Paul Brook
815 6b1b92d3 Paul Brook
    pci_dev = qemu_mallocz(instance_size);
816 113f89df Isaku Yamahata
    pci_dev = do_pci_register_device(pci_dev, bus, name, devfn, &info);
817 09e3acc6 Gerd Hoffmann
    if (pci_dev == NULL) {
818 09e3acc6 Gerd Hoffmann
        hw_error("PCI: can't register device\n");
819 09e3acc6 Gerd Hoffmann
    }
820 6b1b92d3 Paul Brook
    return pci_dev;
821 6b1b92d3 Paul Brook
}
822 2e01c8cf Blue Swirl
823 2e01c8cf Blue Swirl
static target_phys_addr_t pci_to_cpu_addr(PCIBus *bus,
824 2e01c8cf Blue Swirl
                                          target_phys_addr_t addr)
825 5851e08c aliguori
{
826 2e01c8cf Blue Swirl
    return addr + bus->mem_base;
827 5851e08c aliguori
}
828 5851e08c aliguori
829 5851e08c aliguori
static void pci_unregister_io_regions(PCIDevice *pci_dev)
830 5851e08c aliguori
{
831 5851e08c aliguori
    PCIIORegion *r;
832 5851e08c aliguori
    int i;
833 5851e08c aliguori
834 5851e08c aliguori
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
835 5851e08c aliguori
        r = &pci_dev->io_regions[i];
836 182f9c8a Isaku Yamahata
        if (!r->size || r->addr == PCI_BAR_UNMAPPED)
837 5851e08c aliguori
            continue;
838 0392a017 Isaku Yamahata
        if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
839 a0c7a97e Isaku Yamahata
            isa_unassign_ioport(r->addr, r->filtered_size);
840 5851e08c aliguori
        } else {
841 2e01c8cf Blue Swirl
            cpu_register_physical_memory(pci_to_cpu_addr(pci_dev->bus,
842 2e01c8cf Blue Swirl
                                                         r->addr),
843 2e01c8cf Blue Swirl
                                         r->filtered_size,
844 2e01c8cf Blue Swirl
                                         IO_MEM_UNASSIGNED);
845 5851e08c aliguori
        }
846 5851e08c aliguori
    }
847 5851e08c aliguori
}
848 5851e08c aliguori
849 a36a344d Gerd Hoffmann
static int pci_unregister_device(DeviceState *dev)
850 5851e08c aliguori
{
851 a36a344d Gerd Hoffmann
    PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
852 e3936fa5 Gerd Hoffmann
    PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
853 5851e08c aliguori
    int ret = 0;
854 5851e08c aliguori
855 e3936fa5 Gerd Hoffmann
    if (info->exit)
856 e3936fa5 Gerd Hoffmann
        ret = info->exit(pci_dev);
857 5851e08c aliguori
    if (ret)
858 5851e08c aliguori
        return ret;
859 5851e08c aliguori
860 5851e08c aliguori
    pci_unregister_io_regions(pci_dev);
861 230741dc Alex Williamson
    pci_del_option_rom(pci_dev);
862 be7052c2 Isaku Yamahata
    qemu_free(pci_dev->romfile);
863 925fe64a Alex Williamson
    do_pci_unregister_device(pci_dev);
864 5851e08c aliguori
    return 0;
865 5851e08c aliguori
}
866 5851e08c aliguori
867 28c2c264 Avi Kivity
void pci_register_bar(PCIDevice *pci_dev, int region_num,
868 0bb750ef Isaku Yamahata
                            pcibus_t size, uint8_t type,
869 69b91039 bellard
                            PCIMapIORegionFunc *map_func)
870 69b91039 bellard
{
871 69b91039 bellard
    PCIIORegion *r;
872 d7ce493a pbrook
    uint32_t addr;
873 5a9ff381 Isaku Yamahata
    uint64_t wmask;
874 a4c20c6a aliguori
875 2bbb9c2f Isaku Yamahata
    assert(region_num >= 0);
876 2bbb9c2f Isaku Yamahata
    assert(region_num < PCI_NUM_REGIONS);
877 a4c20c6a aliguori
    if (size & (size-1)) {
878 a4c20c6a aliguori
        fprintf(stderr, "ERROR: PCI region size must be pow2 "
879 89e8b13c Isaku Yamahata
                    "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
880 a4c20c6a aliguori
        exit(1);
881 a4c20c6a aliguori
    }
882 a4c20c6a aliguori
883 69b91039 bellard
    r = &pci_dev->io_regions[region_num];
884 182f9c8a Isaku Yamahata
    r->addr = PCI_BAR_UNMAPPED;
885 69b91039 bellard
    r->size = size;
886 a0c7a97e Isaku Yamahata
    r->filtered_size = size;
887 69b91039 bellard
    r->type = type;
888 69b91039 bellard
    r->map_func = map_func;
889 17cbcb0b Avi Kivity
    r->ram_addr = IO_MEM_UNASSIGNED;
890 b7ee1603 Michael S. Tsirkin
891 b7ee1603 Michael S. Tsirkin
    wmask = ~(size - 1);
892 b3b11697 Isaku Yamahata
    addr = pci_bar(pci_dev, region_num);
893 d7ce493a pbrook
    if (region_num == PCI_ROM_SLOT) {
894 ebabb67a Stefan Weil
        /* ROM enable bit is writable */
895 5330de09 Michael S. Tsirkin
        wmask |= PCI_ROM_ADDRESS_ENABLE;
896 d7ce493a pbrook
    }
897 b0ff8eb2 Isaku Yamahata
    pci_set_long(pci_dev->config + addr, type);
898 14421258 Isaku Yamahata
    if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
899 14421258 Isaku Yamahata
        r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
900 14421258 Isaku Yamahata
        pci_set_quad(pci_dev->wmask + addr, wmask);
901 14421258 Isaku Yamahata
        pci_set_quad(pci_dev->cmask + addr, ~0ULL);
902 14421258 Isaku Yamahata
    } else {
903 14421258 Isaku Yamahata
        pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
904 14421258 Isaku Yamahata
        pci_set_long(pci_dev->cmask + addr, 0xffffffff);
905 14421258 Isaku Yamahata
    }
906 69b91039 bellard
}
907 69b91039 bellard
908 17cbcb0b Avi Kivity
static void pci_simple_bar_mapfunc(PCIDevice *pci_dev, int region_num,
909 17cbcb0b Avi Kivity
                                   pcibus_t addr, pcibus_t size, int type)
910 17cbcb0b Avi Kivity
{
911 17cbcb0b Avi Kivity
    cpu_register_physical_memory(addr, size,
912 17cbcb0b Avi Kivity
                                 pci_dev->io_regions[region_num].ram_addr);
913 17cbcb0b Avi Kivity
}
914 17cbcb0b Avi Kivity
915 17cbcb0b Avi Kivity
void pci_register_bar_simple(PCIDevice *pci_dev, int region_num,
916 17cbcb0b Avi Kivity
                             pcibus_t size,  uint8_t attr, ram_addr_t ram_addr)
917 17cbcb0b Avi Kivity
{
918 17cbcb0b Avi Kivity
    pci_register_bar(pci_dev, region_num, size,
919 17cbcb0b Avi Kivity
                     PCI_BASE_ADDRESS_SPACE_MEMORY | attr,
920 17cbcb0b Avi Kivity
                     pci_simple_bar_mapfunc);
921 17cbcb0b Avi Kivity
    pci_dev->io_regions[region_num].ram_addr = ram_addr;
922 17cbcb0b Avi Kivity
}
923 17cbcb0b Avi Kivity
924 a0c7a97e Isaku Yamahata
static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size,
925 a0c7a97e Isaku Yamahata
                              uint8_t type)
926 a0c7a97e Isaku Yamahata
{
927 a0c7a97e Isaku Yamahata
    pcibus_t base = *addr;
928 a0c7a97e Isaku Yamahata
    pcibus_t limit = *addr + *size - 1;
929 a0c7a97e Isaku Yamahata
    PCIDevice *br;
930 a0c7a97e Isaku Yamahata
931 a0c7a97e Isaku Yamahata
    for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
932 a0c7a97e Isaku Yamahata
        uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
933 a0c7a97e Isaku Yamahata
934 a0c7a97e Isaku Yamahata
        if (type & PCI_BASE_ADDRESS_SPACE_IO) {
935 a0c7a97e Isaku Yamahata
            if (!(cmd & PCI_COMMAND_IO)) {
936 a0c7a97e Isaku Yamahata
                goto no_map;
937 a0c7a97e Isaku Yamahata
            }
938 a0c7a97e Isaku Yamahata
        } else {
939 a0c7a97e Isaku Yamahata
            if (!(cmd & PCI_COMMAND_MEMORY)) {
940 a0c7a97e Isaku Yamahata
                goto no_map;
941 a0c7a97e Isaku Yamahata
            }
942 a0c7a97e Isaku Yamahata
        }
943 a0c7a97e Isaku Yamahata
944 a0c7a97e Isaku Yamahata
        base = MAX(base, pci_bridge_get_base(br, type));
945 a0c7a97e Isaku Yamahata
        limit = MIN(limit, pci_bridge_get_limit(br, type));
946 a0c7a97e Isaku Yamahata
    }
947 a0c7a97e Isaku Yamahata
948 a0c7a97e Isaku Yamahata
    if (base > limit) {
949 88a95564 Michael S. Tsirkin
        goto no_map;
950 a0c7a97e Isaku Yamahata
    }
951 88a95564 Michael S. Tsirkin
    *addr = base;
952 88a95564 Michael S. Tsirkin
    *size = limit - base + 1;
953 88a95564 Michael S. Tsirkin
    return;
954 88a95564 Michael S. Tsirkin
no_map:
955 88a95564 Michael S. Tsirkin
    *addr = PCI_BAR_UNMAPPED;
956 88a95564 Michael S. Tsirkin
    *size = 0;
957 a0c7a97e Isaku Yamahata
}
958 a0c7a97e Isaku Yamahata
959 876a350d Michael S. Tsirkin
static pcibus_t pci_bar_address(PCIDevice *d,
960 876a350d Michael S. Tsirkin
                                int reg, uint8_t type, pcibus_t size)
961 876a350d Michael S. Tsirkin
{
962 876a350d Michael S. Tsirkin
    pcibus_t new_addr, last_addr;
963 876a350d Michael S. Tsirkin
    int bar = pci_bar(d, reg);
964 876a350d Michael S. Tsirkin
    uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
965 876a350d Michael S. Tsirkin
966 876a350d Michael S. Tsirkin
    if (type & PCI_BASE_ADDRESS_SPACE_IO) {
967 876a350d Michael S. Tsirkin
        if (!(cmd & PCI_COMMAND_IO)) {
968 876a350d Michael S. Tsirkin
            return PCI_BAR_UNMAPPED;
969 876a350d Michael S. Tsirkin
        }
970 876a350d Michael S. Tsirkin
        new_addr = pci_get_long(d->config + bar) & ~(size - 1);
971 876a350d Michael S. Tsirkin
        last_addr = new_addr + size - 1;
972 876a350d Michael S. Tsirkin
        /* NOTE: we have only 64K ioports on PC */
973 876a350d Michael S. Tsirkin
        if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) {
974 876a350d Michael S. Tsirkin
            return PCI_BAR_UNMAPPED;
975 876a350d Michael S. Tsirkin
        }
976 876a350d Michael S. Tsirkin
        return new_addr;
977 876a350d Michael S. Tsirkin
    }
978 876a350d Michael S. Tsirkin
979 876a350d Michael S. Tsirkin
    if (!(cmd & PCI_COMMAND_MEMORY)) {
980 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
981 876a350d Michael S. Tsirkin
    }
982 876a350d Michael S. Tsirkin
    if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
983 876a350d Michael S. Tsirkin
        new_addr = pci_get_quad(d->config + bar);
984 876a350d Michael S. Tsirkin
    } else {
985 876a350d Michael S. Tsirkin
        new_addr = pci_get_long(d->config + bar);
986 876a350d Michael S. Tsirkin
    }
987 876a350d Michael S. Tsirkin
    /* the ROM slot has a specific enable bit */
988 876a350d Michael S. Tsirkin
    if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
989 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
990 876a350d Michael S. Tsirkin
    }
991 876a350d Michael S. Tsirkin
    new_addr &= ~(size - 1);
992 876a350d Michael S. Tsirkin
    last_addr = new_addr + size - 1;
993 876a350d Michael S. Tsirkin
    /* NOTE: we do not support wrapping */
994 876a350d Michael S. Tsirkin
    /* XXX: as we cannot support really dynamic
995 876a350d Michael S. Tsirkin
       mappings, we handle specific values as invalid
996 876a350d Michael S. Tsirkin
       mappings. */
997 876a350d Michael S. Tsirkin
    if (last_addr <= new_addr || new_addr == 0 ||
998 876a350d Michael S. Tsirkin
        last_addr == PCI_BAR_UNMAPPED) {
999 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
1000 876a350d Michael S. Tsirkin
    }
1001 876a350d Michael S. Tsirkin
1002 876a350d Michael S. Tsirkin
    /* Now pcibus_t is 64bit.
1003 876a350d Michael S. Tsirkin
     * Check if 32 bit BAR wraps around explicitly.
1004 876a350d Michael S. Tsirkin
     * Without this, PC ide doesn't work well.
1005 876a350d Michael S. Tsirkin
     * TODO: remove this work around.
1006 876a350d Michael S. Tsirkin
     */
1007 876a350d Michael S. Tsirkin
    if  (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
1008 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
1009 876a350d Michael S. Tsirkin
    }
1010 876a350d Michael S. Tsirkin
1011 876a350d Michael S. Tsirkin
    /*
1012 876a350d Michael S. Tsirkin
     * OS is allowed to set BAR beyond its addressable
1013 876a350d Michael S. Tsirkin
     * bits. For example, 32 bit OS can set 64bit bar
1014 876a350d Michael S. Tsirkin
     * to >4G. Check it. TODO: we might need to support
1015 876a350d Michael S. Tsirkin
     * it in the future for e.g. PAE.
1016 876a350d Michael S. Tsirkin
     */
1017 876a350d Michael S. Tsirkin
    if (last_addr >= TARGET_PHYS_ADDR_MAX) {
1018 876a350d Michael S. Tsirkin
        return PCI_BAR_UNMAPPED;
1019 876a350d Michael S. Tsirkin
    }
1020 876a350d Michael S. Tsirkin
1021 876a350d Michael S. Tsirkin
    return new_addr;
1022 876a350d Michael S. Tsirkin
}
1023 876a350d Michael S. Tsirkin
1024 0ac32c83 bellard
static void pci_update_mappings(PCIDevice *d)
1025 0ac32c83 bellard
{
1026 0ac32c83 bellard
    PCIIORegion *r;
1027 876a350d Michael S. Tsirkin
    int i;
1028 c71b5b4a Blue Swirl
    pcibus_t new_addr, filtered_size;
1029 3b46e624 ths
1030 8a8696a3 bellard
    for(i = 0; i < PCI_NUM_REGIONS; i++) {
1031 0ac32c83 bellard
        r = &d->io_regions[i];
1032 a9688570 Isaku Yamahata
1033 a9688570 Isaku Yamahata
        /* this region isn't registered */
1034 ec503442 Isaku Yamahata
        if (!r->size)
1035 a9688570 Isaku Yamahata
            continue;
1036 a9688570 Isaku Yamahata
1037 876a350d Michael S. Tsirkin
        new_addr = pci_bar_address(d, i, r->type, r->size);
1038 a9688570 Isaku Yamahata
1039 a0c7a97e Isaku Yamahata
        /* bridge filtering */
1040 a0c7a97e Isaku Yamahata
        filtered_size = r->size;
1041 a0c7a97e Isaku Yamahata
        if (new_addr != PCI_BAR_UNMAPPED) {
1042 a0c7a97e Isaku Yamahata
            pci_bridge_filter(d, &new_addr, &filtered_size, r->type);
1043 a0c7a97e Isaku Yamahata
        }
1044 a0c7a97e Isaku Yamahata
1045 a9688570 Isaku Yamahata
        /* This bar isn't changed */
1046 a0c7a97e Isaku Yamahata
        if (new_addr == r->addr && filtered_size == r->filtered_size)
1047 a9688570 Isaku Yamahata
            continue;
1048 a9688570 Isaku Yamahata
1049 a9688570 Isaku Yamahata
        /* now do the real mapping */
1050 a9688570 Isaku Yamahata
        if (r->addr != PCI_BAR_UNMAPPED) {
1051 a9688570 Isaku Yamahata
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1052 a9688570 Isaku Yamahata
                int class;
1053 a9688570 Isaku Yamahata
                /* NOTE: specific hack for IDE in PC case:
1054 a9688570 Isaku Yamahata
                   only one byte must be mapped. */
1055 a9688570 Isaku Yamahata
                class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1056 a9688570 Isaku Yamahata
                if (class == 0x0101 && r->size == 4) {
1057 a9688570 Isaku Yamahata
                    isa_unassign_ioport(r->addr + 2, 1);
1058 a9688570 Isaku Yamahata
                } else {
1059 a0c7a97e Isaku Yamahata
                    isa_unassign_ioport(r->addr, r->filtered_size);
1060 0ac32c83 bellard
                }
1061 a9688570 Isaku Yamahata
            } else {
1062 c71b5b4a Blue Swirl
                cpu_register_physical_memory(pci_to_cpu_addr(d->bus, r->addr),
1063 a0c7a97e Isaku Yamahata
                                             r->filtered_size,
1064 a9688570 Isaku Yamahata
                                             IO_MEM_UNASSIGNED);
1065 a0c7a97e Isaku Yamahata
                qemu_unregister_coalesced_mmio(r->addr, r->filtered_size);
1066 0ac32c83 bellard
            }
1067 0ac32c83 bellard
        }
1068 a9688570 Isaku Yamahata
        r->addr = new_addr;
1069 a0c7a97e Isaku Yamahata
        r->filtered_size = filtered_size;
1070 a9688570 Isaku Yamahata
        if (r->addr != PCI_BAR_UNMAPPED) {
1071 a0c7a97e Isaku Yamahata
            /*
1072 a0c7a97e Isaku Yamahata
             * TODO: currently almost all the map funcions assumes
1073 a0c7a97e Isaku Yamahata
             * filtered_size == size and addr & ~(size - 1) == addr.
1074 a0c7a97e Isaku Yamahata
             * However with bridge filtering, they aren't always true.
1075 a0c7a97e Isaku Yamahata
             * Teach them such cases, such that filtered_size < size and
1076 a0c7a97e Isaku Yamahata
             * addr & (size - 1) != 0.
1077 a0c7a97e Isaku Yamahata
             */
1078 cf616802 Blue Swirl
            if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1079 cf616802 Blue Swirl
                r->map_func(d, i, r->addr, r->filtered_size, r->type);
1080 cf616802 Blue Swirl
            } else {
1081 cf616802 Blue Swirl
                r->map_func(d, i, pci_to_cpu_addr(d->bus, r->addr),
1082 cf616802 Blue Swirl
                            r->filtered_size, r->type);
1083 cf616802 Blue Swirl
            }
1084 a9688570 Isaku Yamahata
        }
1085 0ac32c83 bellard
    }
1086 0ac32c83 bellard
}
1087 0ac32c83 bellard
1088 a7b15a5c Michael S. Tsirkin
static inline int pci_irq_disabled(PCIDevice *d)
1089 a7b15a5c Michael S. Tsirkin
{
1090 a7b15a5c Michael S. Tsirkin
    return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
1091 a7b15a5c Michael S. Tsirkin
}
1092 a7b15a5c Michael S. Tsirkin
1093 a7b15a5c Michael S. Tsirkin
/* Called after interrupt disabled field update in config space,
1094 a7b15a5c Michael S. Tsirkin
 * assert/deassert interrupts if necessary.
1095 a7b15a5c Michael S. Tsirkin
 * Gets original interrupt disable bit value (before update). */
1096 a7b15a5c Michael S. Tsirkin
static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
1097 a7b15a5c Michael S. Tsirkin
{
1098 a7b15a5c Michael S. Tsirkin
    int i, disabled = pci_irq_disabled(d);
1099 a7b15a5c Michael S. Tsirkin
    if (disabled == was_irq_disabled)
1100 a7b15a5c Michael S. Tsirkin
        return;
1101 a7b15a5c Michael S. Tsirkin
    for (i = 0; i < PCI_NUM_PINS; ++i) {
1102 a7b15a5c Michael S. Tsirkin
        int state = pci_irq_state(d, i);
1103 a7b15a5c Michael S. Tsirkin
        pci_change_irq_level(d, i, disabled ? -state : state);
1104 a7b15a5c Michael S. Tsirkin
    }
1105 a7b15a5c Michael S. Tsirkin
}
1106 a7b15a5c Michael S. Tsirkin
1107 5fafdf24 ths
uint32_t pci_default_read_config(PCIDevice *d,
1108 0ac32c83 bellard
                                 uint32_t address, int len)
1109 69b91039 bellard
{
1110 5029fe12 Isaku Yamahata
    uint32_t val = 0;
1111 5029fe12 Isaku Yamahata
    assert(len == 1 || len == 2 || len == 4);
1112 a9f49946 Isaku Yamahata
    len = MIN(len, pci_config_size(d) - address);
1113 5029fe12 Isaku Yamahata
    memcpy(&val, d->config + address, len);
1114 5029fe12 Isaku Yamahata
    return le32_to_cpu(val);
1115 0ac32c83 bellard
}
1116 0ac32c83 bellard
1117 b7ee1603 Michael S. Tsirkin
void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
1118 0ac32c83 bellard
{
1119 a7b15a5c Michael S. Tsirkin
    int i, was_irq_disabled = pci_irq_disabled(d);
1120 a9f49946 Isaku Yamahata
    uint32_t config_size = pci_config_size(d);
1121 0ac32c83 bellard
1122 91011d4f Stefan Weil
    for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) {
1123 91011d4f Stefan Weil
        uint8_t wmask = d->wmask[addr + i];
1124 92ba5f51 Isaku Yamahata
        uint8_t w1cmask = d->w1cmask[addr + i];
1125 92ba5f51 Isaku Yamahata
        assert(!(wmask & w1cmask));
1126 91011d4f Stefan Weil
        d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
1127 92ba5f51 Isaku Yamahata
        d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
1128 0ac32c83 bellard
    }
1129 260c0cd3 Isaku Yamahata
    if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1130 edb00035 Isaku Yamahata
        ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1131 edb00035 Isaku Yamahata
        ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1132 260c0cd3 Isaku Yamahata
        range_covers_byte(addr, l, PCI_COMMAND))
1133 0ac32c83 bellard
        pci_update_mappings(d);
1134 a7b15a5c Michael S. Tsirkin
1135 a7b15a5c Michael S. Tsirkin
    if (range_covers_byte(addr, l, PCI_COMMAND))
1136 a7b15a5c Michael S. Tsirkin
        pci_update_irq_disabled(d, was_irq_disabled);
1137 69b91039 bellard
}
1138 69b91039 bellard
1139 502a5395 pbrook
/***********************************************************/
1140 502a5395 pbrook
/* generic PCI irq support */
1141 30468f78 bellard
1142 502a5395 pbrook
/* 0 <= irq_num <= 3. level must be 0 or 1 */
1143 d537cf6c pbrook
static void pci_set_irq(void *opaque, int irq_num, int level)
1144 69b91039 bellard
{
1145 a60380a5 Juan Quintela
    PCIDevice *pci_dev = opaque;
1146 80b3ada7 pbrook
    int change;
1147 3b46e624 ths
1148 d036bb21 Michael S. Tsirkin
    change = level - pci_irq_state(pci_dev, irq_num);
1149 80b3ada7 pbrook
    if (!change)
1150 80b3ada7 pbrook
        return;
1151 d2b59317 pbrook
1152 d036bb21 Michael S. Tsirkin
    pci_set_irq_state(pci_dev, irq_num, level);
1153 f9bf77dd Michael S. Tsirkin
    pci_update_irq_status(pci_dev);
1154 a7b15a5c Michael S. Tsirkin
    if (pci_irq_disabled(pci_dev))
1155 a7b15a5c Michael S. Tsirkin
        return;
1156 d036bb21 Michael S. Tsirkin
    pci_change_irq_level(pci_dev, irq_num, change);
1157 69b91039 bellard
}
1158 69b91039 bellard
1159 502a5395 pbrook
/***********************************************************/
1160 502a5395 pbrook
/* monitor info on PCI */
1161 0ac32c83 bellard
1162 6650ee6d pbrook
typedef struct {
1163 6650ee6d pbrook
    uint16_t class;
1164 6650ee6d pbrook
    const char *desc;
1165 5e0259e7 Gleb Natapov
    const char *fw_name;
1166 5e0259e7 Gleb Natapov
    uint16_t fw_ign_bits;
1167 6650ee6d pbrook
} pci_class_desc;
1168 6650ee6d pbrook
1169 09bc878a blueswir1
static const pci_class_desc pci_class_descriptions[] =
1170 6650ee6d pbrook
{
1171 5e0259e7 Gleb Natapov
    { 0x0001, "VGA controller", "display"},
1172 5e0259e7 Gleb Natapov
    { 0x0100, "SCSI controller", "scsi"},
1173 5e0259e7 Gleb Natapov
    { 0x0101, "IDE controller", "ide"},
1174 5e0259e7 Gleb Natapov
    { 0x0102, "Floppy controller", "fdc"},
1175 5e0259e7 Gleb Natapov
    { 0x0103, "IPI controller", "ipi"},
1176 5e0259e7 Gleb Natapov
    { 0x0104, "RAID controller", "raid"},
1177 dcb5b19a ths
    { 0x0106, "SATA controller"},
1178 dcb5b19a ths
    { 0x0107, "SAS controller"},
1179 dcb5b19a ths
    { 0x0180, "Storage controller"},
1180 5e0259e7 Gleb Natapov
    { 0x0200, "Ethernet controller", "ethernet"},
1181 5e0259e7 Gleb Natapov
    { 0x0201, "Token Ring controller", "token-ring"},
1182 5e0259e7 Gleb Natapov
    { 0x0202, "FDDI controller", "fddi"},
1183 5e0259e7 Gleb Natapov
    { 0x0203, "ATM controller", "atm"},
1184 dcb5b19a ths
    { 0x0280, "Network controller"},
1185 5e0259e7 Gleb Natapov
    { 0x0300, "VGA controller", "display", 0x00ff},
1186 dcb5b19a ths
    { 0x0301, "XGA controller"},
1187 dcb5b19a ths
    { 0x0302, "3D controller"},
1188 dcb5b19a ths
    { 0x0380, "Display controller"},
1189 5e0259e7 Gleb Natapov
    { 0x0400, "Video controller", "video"},
1190 5e0259e7 Gleb Natapov
    { 0x0401, "Audio controller", "sound"},
1191 dcb5b19a ths
    { 0x0402, "Phone"},
1192 602ef4d9 Jan Kiszka
    { 0x0403, "Audio controller", "sound"},
1193 dcb5b19a ths
    { 0x0480, "Multimedia controller"},
1194 5e0259e7 Gleb Natapov
    { 0x0500, "RAM controller", "memory"},
1195 5e0259e7 Gleb Natapov
    { 0x0501, "Flash controller", "flash"},
1196 dcb5b19a ths
    { 0x0580, "Memory controller"},
1197 5e0259e7 Gleb Natapov
    { 0x0600, "Host bridge", "host"},
1198 5e0259e7 Gleb Natapov
    { 0x0601, "ISA bridge", "isa"},
1199 5e0259e7 Gleb Natapov
    { 0x0602, "EISA bridge", "eisa"},
1200 5e0259e7 Gleb Natapov
    { 0x0603, "MC bridge", "mca"},
1201 5e0259e7 Gleb Natapov
    { 0x0604, "PCI bridge", "pci"},
1202 5e0259e7 Gleb Natapov
    { 0x0605, "PCMCIA bridge", "pcmcia"},
1203 5e0259e7 Gleb Natapov
    { 0x0606, "NUBUS bridge", "nubus"},
1204 5e0259e7 Gleb Natapov
    { 0x0607, "CARDBUS bridge", "cardbus"},
1205 dcb5b19a ths
    { 0x0608, "RACEWAY bridge"},
1206 dcb5b19a ths
    { 0x0680, "Bridge"},
1207 5e0259e7 Gleb Natapov
    { 0x0700, "Serial port", "serial"},
1208 5e0259e7 Gleb Natapov
    { 0x0701, "Parallel port", "parallel"},
1209 5e0259e7 Gleb Natapov
    { 0x0800, "Interrupt controller", "interrupt-controller"},
1210 5e0259e7 Gleb Natapov
    { 0x0801, "DMA controller", "dma-controller"},
1211 5e0259e7 Gleb Natapov
    { 0x0802, "Timer", "timer"},
1212 5e0259e7 Gleb Natapov
    { 0x0803, "RTC", "rtc"},
1213 5e0259e7 Gleb Natapov
    { 0x0900, "Keyboard", "keyboard"},
1214 5e0259e7 Gleb Natapov
    { 0x0901, "Pen", "pen"},
1215 5e0259e7 Gleb Natapov
    { 0x0902, "Mouse", "mouse"},
1216 5e0259e7 Gleb Natapov
    { 0x0A00, "Dock station", "dock", 0x00ff},
1217 5e0259e7 Gleb Natapov
    { 0x0B00, "i386 cpu", "cpu", 0x00ff},
1218 5e0259e7 Gleb Natapov
    { 0x0c00, "Fireware contorller", "fireware"},
1219 5e0259e7 Gleb Natapov
    { 0x0c01, "Access bus controller", "access-bus"},
1220 5e0259e7 Gleb Natapov
    { 0x0c02, "SSA controller", "ssa"},
1221 5e0259e7 Gleb Natapov
    { 0x0c03, "USB controller", "usb"},
1222 5e0259e7 Gleb Natapov
    { 0x0c04, "Fibre channel controller", "fibre-channel"},
1223 6650ee6d pbrook
    { 0, NULL}
1224 6650ee6d pbrook
};
1225 6650ee6d pbrook
1226 163c8a59 Luiz Capitulino
static void pci_for_each_device_under_bus(PCIBus *bus,
1227 163c8a59 Luiz Capitulino
                                          void (*fn)(PCIBus *b, PCIDevice *d))
1228 30468f78 bellard
{
1229 163c8a59 Luiz Capitulino
    PCIDevice *d;
1230 163c8a59 Luiz Capitulino
    int devfn;
1231 30468f78 bellard
1232 163c8a59 Luiz Capitulino
    for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1233 163c8a59 Luiz Capitulino
        d = bus->devices[devfn];
1234 163c8a59 Luiz Capitulino
        if (d) {
1235 163c8a59 Luiz Capitulino
            fn(bus, d);
1236 163c8a59 Luiz Capitulino
        }
1237 163c8a59 Luiz Capitulino
    }
1238 163c8a59 Luiz Capitulino
}
1239 163c8a59 Luiz Capitulino
1240 163c8a59 Luiz Capitulino
void pci_for_each_device(PCIBus *bus, int bus_num,
1241 163c8a59 Luiz Capitulino
                         void (*fn)(PCIBus *b, PCIDevice *d))
1242 163c8a59 Luiz Capitulino
{
1243 163c8a59 Luiz Capitulino
    bus = pci_find_bus(bus, bus_num);
1244 163c8a59 Luiz Capitulino
1245 163c8a59 Luiz Capitulino
    if (bus) {
1246 163c8a59 Luiz Capitulino
        pci_for_each_device_under_bus(bus, fn);
1247 163c8a59 Luiz Capitulino
    }
1248 163c8a59 Luiz Capitulino
}
1249 163c8a59 Luiz Capitulino
1250 163c8a59 Luiz Capitulino
static void pci_device_print(Monitor *mon, QDict *device)
1251 163c8a59 Luiz Capitulino
{
1252 163c8a59 Luiz Capitulino
    QDict *qdict;
1253 163c8a59 Luiz Capitulino
    QListEntry *entry;
1254 163c8a59 Luiz Capitulino
    uint64_t addr, size;
1255 163c8a59 Luiz Capitulino
1256 163c8a59 Luiz Capitulino
    monitor_printf(mon, "  Bus %2" PRId64 ", ", qdict_get_int(device, "bus"));
1257 163c8a59 Luiz Capitulino
    monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
1258 163c8a59 Luiz Capitulino
                        qdict_get_int(device, "slot"),
1259 163c8a59 Luiz Capitulino
                        qdict_get_int(device, "function"));
1260 376253ec aliguori
    monitor_printf(mon, "    ");
1261 163c8a59 Luiz Capitulino
1262 163c8a59 Luiz Capitulino
    qdict = qdict_get_qdict(device, "class_info");
1263 163c8a59 Luiz Capitulino
    if (qdict_haskey(qdict, "desc")) {
1264 163c8a59 Luiz Capitulino
        monitor_printf(mon, "%s", qdict_get_str(qdict, "desc"));
1265 6650ee6d pbrook
    } else {
1266 163c8a59 Luiz Capitulino
        monitor_printf(mon, "Class %04" PRId64, qdict_get_int(qdict, "class"));
1267 72cc6cfe bellard
    }
1268 30468f78 bellard
1269 163c8a59 Luiz Capitulino
    qdict = qdict_get_qdict(device, "id");
1270 163c8a59 Luiz Capitulino
    monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
1271 163c8a59 Luiz Capitulino
                        qdict_get_int(qdict, "device"),
1272 163c8a59 Luiz Capitulino
                        qdict_get_int(qdict, "vendor"));
1273 163c8a59 Luiz Capitulino
1274 163c8a59 Luiz Capitulino
    if (qdict_haskey(device, "irq")) {
1275 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      IRQ %" PRId64 ".\n",
1276 163c8a59 Luiz Capitulino
                            qdict_get_int(device, "irq"));
1277 30468f78 bellard
    }
1278 b4dccd8d Isaku Yamahata
1279 163c8a59 Luiz Capitulino
    if (qdict_haskey(device, "pci_bridge")) {
1280 163c8a59 Luiz Capitulino
        QDict *info;
1281 163c8a59 Luiz Capitulino
1282 163c8a59 Luiz Capitulino
        qdict = qdict_get_qdict(device, "pci_bridge");
1283 163c8a59 Luiz Capitulino
1284 163c8a59 Luiz Capitulino
        info = qdict_get_qdict(qdict, "bus");
1285 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      BUS %" PRId64 ".\n",
1286 163c8a59 Luiz Capitulino
                            qdict_get_int(info, "number"));
1287 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
1288 163c8a59 Luiz Capitulino
                            qdict_get_int(info, "secondary"));
1289 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
1290 163c8a59 Luiz Capitulino
                            qdict_get_int(info, "subordinate"));
1291 b4dccd8d Isaku Yamahata
1292 163c8a59 Luiz Capitulino
        info = qdict_get_qdict(qdict, "io_range");
1293 b4dccd8d Isaku Yamahata
        monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
1294 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "base"),
1295 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "limit"));
1296 b4dccd8d Isaku Yamahata
1297 163c8a59 Luiz Capitulino
        info = qdict_get_qdict(qdict, "memory_range");
1298 b4dccd8d Isaku Yamahata
        monitor_printf(mon,
1299 b4dccd8d Isaku Yamahata
                       "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
1300 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "base"),
1301 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "limit"));
1302 b4dccd8d Isaku Yamahata
1303 163c8a59 Luiz Capitulino
        info = qdict_get_qdict(qdict, "prefetchable_range");
1304 b4dccd8d Isaku Yamahata
        monitor_printf(mon, "      prefetchable memory range "
1305 163c8a59 Luiz Capitulino
                       "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
1306 163c8a59 Luiz Capitulino
                       qdict_get_int(info, "base"),
1307 163c8a59 Luiz Capitulino
        qdict_get_int(info, "limit"));
1308 80b3ada7 pbrook
    }
1309 14421258 Isaku Yamahata
1310 163c8a59 Luiz Capitulino
    QLIST_FOREACH_ENTRY(qdict_get_qlist(device, "regions"), entry) {
1311 163c8a59 Luiz Capitulino
        qdict = qobject_to_qdict(qlist_entry_obj(entry));
1312 163c8a59 Luiz Capitulino
        monitor_printf(mon, "      BAR%d: ", (int) qdict_get_int(qdict, "bar"));
1313 163c8a59 Luiz Capitulino
1314 163c8a59 Luiz Capitulino
        addr = qdict_get_int(qdict, "address");
1315 163c8a59 Luiz Capitulino
        size = qdict_get_int(qdict, "size");
1316 163c8a59 Luiz Capitulino
1317 163c8a59 Luiz Capitulino
        if (!strcmp(qdict_get_str(qdict, "type"), "io")) {
1318 163c8a59 Luiz Capitulino
            monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
1319 163c8a59 Luiz Capitulino
                                " [0x%04"FMT_PCIBUS"].\n",
1320 163c8a59 Luiz Capitulino
                                addr, addr + size - 1);
1321 163c8a59 Luiz Capitulino
        } else {
1322 163c8a59 Luiz Capitulino
            monitor_printf(mon, "%d bit%s memory at 0x%08"FMT_PCIBUS
1323 89e8b13c Isaku Yamahata
                               " [0x%08"FMT_PCIBUS"].\n",
1324 163c8a59 Luiz Capitulino
                                qdict_get_bool(qdict, "mem_type_64") ? 64 : 32,
1325 163c8a59 Luiz Capitulino
                                qdict_get_bool(qdict, "prefetch") ?
1326 163c8a59 Luiz Capitulino
                                " prefetchable" : "", addr, addr + size - 1);
1327 502a5395 pbrook
        }
1328 77d4bc34 bellard
    }
1329 163c8a59 Luiz Capitulino
1330 163c8a59 Luiz Capitulino
    monitor_printf(mon, "      id \"%s\"\n", qdict_get_str(device, "qdev_id"));
1331 163c8a59 Luiz Capitulino
1332 d5e4acf7 Luiz Capitulino
    if (qdict_haskey(device, "pci_bridge")) {
1333 d5e4acf7 Luiz Capitulino
        qdict = qdict_get_qdict(device, "pci_bridge");
1334 d5e4acf7 Luiz Capitulino
        if (qdict_haskey(qdict, "devices")) {
1335 d5e4acf7 Luiz Capitulino
            QListEntry *dev;
1336 d5e4acf7 Luiz Capitulino
            QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
1337 d5e4acf7 Luiz Capitulino
                pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev)));
1338 d5e4acf7 Luiz Capitulino
            }
1339 d5e4acf7 Luiz Capitulino
        }
1340 d5e4acf7 Luiz Capitulino
    }
1341 163c8a59 Luiz Capitulino
}
1342 163c8a59 Luiz Capitulino
1343 163c8a59 Luiz Capitulino
void do_pci_info_print(Monitor *mon, const QObject *data)
1344 163c8a59 Luiz Capitulino
{
1345 163c8a59 Luiz Capitulino
    QListEntry *bus, *dev;
1346 163c8a59 Luiz Capitulino
1347 163c8a59 Luiz Capitulino
    QLIST_FOREACH_ENTRY(qobject_to_qlist(data), bus) {
1348 163c8a59 Luiz Capitulino
        QDict *qdict = qobject_to_qdict(qlist_entry_obj(bus));
1349 163c8a59 Luiz Capitulino
        QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict, "devices"), dev) {
1350 163c8a59 Luiz Capitulino
            pci_device_print(mon, qobject_to_qdict(qlist_entry_obj(dev)));
1351 163c8a59 Luiz Capitulino
        }
1352 80b3ada7 pbrook
    }
1353 384d8876 bellard
}
1354 384d8876 bellard
1355 163c8a59 Luiz Capitulino
static QObject *pci_get_dev_class(const PCIDevice *dev)
1356 163c8a59 Luiz Capitulino
{
1357 163c8a59 Luiz Capitulino
    int class;
1358 163c8a59 Luiz Capitulino
    const pci_class_desc *desc;
1359 163c8a59 Luiz Capitulino
1360 163c8a59 Luiz Capitulino
    class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
1361 163c8a59 Luiz Capitulino
    desc = pci_class_descriptions;
1362 163c8a59 Luiz Capitulino
    while (desc->desc && class != desc->class)
1363 163c8a59 Luiz Capitulino
        desc++;
1364 163c8a59 Luiz Capitulino
1365 163c8a59 Luiz Capitulino
    if (desc->desc) {
1366 163c8a59 Luiz Capitulino
        return qobject_from_jsonf("{ 'desc': %s, 'class': %d }",
1367 163c8a59 Luiz Capitulino
                                  desc->desc, class);
1368 163c8a59 Luiz Capitulino
    } else {
1369 163c8a59 Luiz Capitulino
        return qobject_from_jsonf("{ 'class': %d }", class);
1370 163c8a59 Luiz Capitulino
    }
1371 163c8a59 Luiz Capitulino
}
1372 163c8a59 Luiz Capitulino
1373 163c8a59 Luiz Capitulino
static QObject *pci_get_dev_id(const PCIDevice *dev)
1374 163c8a59 Luiz Capitulino
{
1375 163c8a59 Luiz Capitulino
    return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }",
1376 163c8a59 Luiz Capitulino
                              pci_get_word(dev->config + PCI_VENDOR_ID),
1377 163c8a59 Luiz Capitulino
                              pci_get_word(dev->config + PCI_DEVICE_ID));
1378 163c8a59 Luiz Capitulino
}
1379 163c8a59 Luiz Capitulino
1380 163c8a59 Luiz Capitulino
static QObject *pci_get_regions_list(const PCIDevice *dev)
1381 163c8a59 Luiz Capitulino
{
1382 163c8a59 Luiz Capitulino
    int i;
1383 163c8a59 Luiz Capitulino
    QList *regions_list;
1384 163c8a59 Luiz Capitulino
1385 163c8a59 Luiz Capitulino
    regions_list = qlist_new();
1386 163c8a59 Luiz Capitulino
1387 163c8a59 Luiz Capitulino
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
1388 163c8a59 Luiz Capitulino
        QObject *obj;
1389 163c8a59 Luiz Capitulino
        const PCIIORegion *r = &dev->io_regions[i];
1390 163c8a59 Luiz Capitulino
1391 163c8a59 Luiz Capitulino
        if (!r->size) {
1392 163c8a59 Luiz Capitulino
            continue;
1393 163c8a59 Luiz Capitulino
        }
1394 163c8a59 Luiz Capitulino
1395 163c8a59 Luiz Capitulino
        if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1396 163c8a59 Luiz Capitulino
            obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
1397 163c8a59 Luiz Capitulino
                                     "'address': %" PRId64 ", "
1398 163c8a59 Luiz Capitulino
                                     "'size': %" PRId64 " }",
1399 163c8a59 Luiz Capitulino
                                     i, r->addr, r->size);
1400 163c8a59 Luiz Capitulino
        } else {
1401 163c8a59 Luiz Capitulino
            int mem_type_64 = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64;
1402 163c8a59 Luiz Capitulino
1403 163c8a59 Luiz Capitulino
            obj = qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
1404 163c8a59 Luiz Capitulino
                                     "'mem_type_64': %i, 'prefetch': %i, "
1405 163c8a59 Luiz Capitulino
                                     "'address': %" PRId64 ", "
1406 163c8a59 Luiz Capitulino
                                     "'size': %" PRId64 " }",
1407 163c8a59 Luiz Capitulino
                                     i, mem_type_64,
1408 163c8a59 Luiz Capitulino
                                     r->type & PCI_BASE_ADDRESS_MEM_PREFETCH,
1409 163c8a59 Luiz Capitulino
                                     r->addr, r->size);
1410 163c8a59 Luiz Capitulino
        }
1411 163c8a59 Luiz Capitulino
1412 163c8a59 Luiz Capitulino
        qlist_append_obj(regions_list, obj);
1413 163c8a59 Luiz Capitulino
    }
1414 163c8a59 Luiz Capitulino
1415 163c8a59 Luiz Capitulino
    return QOBJECT(regions_list);
1416 163c8a59 Luiz Capitulino
}
1417 163c8a59 Luiz Capitulino
1418 d5e4acf7 Luiz Capitulino
static QObject *pci_get_devices_list(PCIBus *bus, int bus_num);
1419 d5e4acf7 Luiz Capitulino
1420 d5e4acf7 Luiz Capitulino
static QObject *pci_get_dev_dict(PCIDevice *dev, PCIBus *bus, int bus_num)
1421 163c8a59 Luiz Capitulino
{
1422 b5937f29 Isaku Yamahata
    uint8_t type;
1423 163c8a59 Luiz Capitulino
    QObject *obj;
1424 163c8a59 Luiz Capitulino
1425 163c8a59 Luiz Capitulino
    obj = qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d,"                                       "'class_info': %p, 'id': %p, 'regions': %p,"
1426 163c8a59 Luiz Capitulino
                              " 'qdev_id': %s }",
1427 163c8a59 Luiz Capitulino
                              bus_num,
1428 163c8a59 Luiz Capitulino
                              PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
1429 163c8a59 Luiz Capitulino
                              pci_get_dev_class(dev), pci_get_dev_id(dev),
1430 163c8a59 Luiz Capitulino
                              pci_get_regions_list(dev),
1431 163c8a59 Luiz Capitulino
                              dev->qdev.id ? dev->qdev.id : "");
1432 163c8a59 Luiz Capitulino
1433 163c8a59 Luiz Capitulino
    if (dev->config[PCI_INTERRUPT_PIN] != 0) {
1434 163c8a59 Luiz Capitulino
        QDict *qdict = qobject_to_qdict(obj);
1435 163c8a59 Luiz Capitulino
        qdict_put(qdict, "irq", qint_from_int(dev->config[PCI_INTERRUPT_LINE]));
1436 163c8a59 Luiz Capitulino
    }
1437 163c8a59 Luiz Capitulino
1438 b5937f29 Isaku Yamahata
    type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1439 b5937f29 Isaku Yamahata
    if (type == PCI_HEADER_TYPE_BRIDGE) {
1440 163c8a59 Luiz Capitulino
        QDict *qdict;
1441 163c8a59 Luiz Capitulino
        QObject *pci_bridge;
1442 163c8a59 Luiz Capitulino
1443 163c8a59 Luiz Capitulino
        pci_bridge = qobject_from_jsonf("{ 'bus': "
1444 163c8a59 Luiz Capitulino
        "{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
1445 163c8a59 Luiz Capitulino
        "'io_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, "
1446 163c8a59 Luiz Capitulino
        "'memory_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "}, "
1447 163c8a59 Luiz Capitulino
        "'prefetchable_range': { 'base': %" PRId64 ", 'limit': %" PRId64 "} }",
1448 c021f8e6 Blue Swirl
        dev->config[PCI_PRIMARY_BUS], dev->config[PCI_SECONDARY_BUS],
1449 163c8a59 Luiz Capitulino
        dev->config[PCI_SUBORDINATE_BUS],
1450 163c8a59 Luiz Capitulino
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO),
1451 163c8a59 Luiz Capitulino
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO),
1452 163c8a59 Luiz Capitulino
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY),
1453 163c8a59 Luiz Capitulino
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY),
1454 163c8a59 Luiz Capitulino
        pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY |
1455 163c8a59 Luiz Capitulino
                               PCI_BASE_ADDRESS_MEM_PREFETCH),
1456 163c8a59 Luiz Capitulino
        pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY |
1457 163c8a59 Luiz Capitulino
                                PCI_BASE_ADDRESS_MEM_PREFETCH));
1458 163c8a59 Luiz Capitulino
1459 c021f8e6 Blue Swirl
        if (dev->config[PCI_SECONDARY_BUS] != 0) {
1460 c021f8e6 Blue Swirl
            PCIBus *child_bus = pci_find_bus(bus, dev->config[PCI_SECONDARY_BUS]);
1461 d5e4acf7 Luiz Capitulino
1462 c021f8e6 Blue Swirl
            if (child_bus) {
1463 c021f8e6 Blue Swirl
                qdict = qobject_to_qdict(pci_bridge);
1464 c021f8e6 Blue Swirl
                qdict_put_obj(qdict, "devices",
1465 c021f8e6 Blue Swirl
                              pci_get_devices_list(child_bus,
1466 c021f8e6 Blue Swirl
                                                   dev->config[PCI_SECONDARY_BUS]));
1467 c021f8e6 Blue Swirl
            }
1468 c021f8e6 Blue Swirl
        }
1469 163c8a59 Luiz Capitulino
        qdict = qobject_to_qdict(obj);
1470 163c8a59 Luiz Capitulino
        qdict_put_obj(qdict, "pci_bridge", pci_bridge);
1471 163c8a59 Luiz Capitulino
    }
1472 163c8a59 Luiz Capitulino
1473 163c8a59 Luiz Capitulino
    return obj;
1474 163c8a59 Luiz Capitulino
}
1475 163c8a59 Luiz Capitulino
1476 163c8a59 Luiz Capitulino
static QObject *pci_get_devices_list(PCIBus *bus, int bus_num)
1477 384d8876 bellard
{
1478 502a5395 pbrook
    int devfn;
1479 163c8a59 Luiz Capitulino
    PCIDevice *dev;
1480 163c8a59 Luiz Capitulino
    QList *dev_list;
1481 3b46e624 ths
1482 163c8a59 Luiz Capitulino
    dev_list = qlist_new();
1483 163c8a59 Luiz Capitulino
1484 163c8a59 Luiz Capitulino
    for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1485 163c8a59 Luiz Capitulino
        dev = bus->devices[devfn];
1486 163c8a59 Luiz Capitulino
        if (dev) {
1487 d5e4acf7 Luiz Capitulino
            qlist_append_obj(dev_list, pci_get_dev_dict(dev, bus, bus_num));
1488 163c8a59 Luiz Capitulino
        }
1489 1074df4f Isaku Yamahata
    }
1490 163c8a59 Luiz Capitulino
1491 163c8a59 Luiz Capitulino
    return QOBJECT(dev_list);
1492 1074df4f Isaku Yamahata
}
1493 1074df4f Isaku Yamahata
1494 163c8a59 Luiz Capitulino
static QObject *pci_get_bus_dict(PCIBus *bus, int bus_num)
1495 1074df4f Isaku Yamahata
{
1496 e822a52a Isaku Yamahata
    bus = pci_find_bus(bus, bus_num);
1497 502a5395 pbrook
    if (bus) {
1498 163c8a59 Luiz Capitulino
        return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }",
1499 163c8a59 Luiz Capitulino
                                  bus_num, pci_get_devices_list(bus, bus_num));
1500 f2aa58c6 bellard
    }
1501 163c8a59 Luiz Capitulino
1502 163c8a59 Luiz Capitulino
    return NULL;
1503 f2aa58c6 bellard
}
1504 f2aa58c6 bellard
1505 163c8a59 Luiz Capitulino
void do_pci_info(Monitor *mon, QObject **ret_data)
1506 f2aa58c6 bellard
{
1507 163c8a59 Luiz Capitulino
    QList *bus_list;
1508 e822a52a Isaku Yamahata
    struct PCIHostBus *host;
1509 163c8a59 Luiz Capitulino
1510 163c8a59 Luiz Capitulino
    bus_list = qlist_new();
1511 163c8a59 Luiz Capitulino
1512 e822a52a Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
1513 163c8a59 Luiz Capitulino
        QObject *obj = pci_get_bus_dict(host->bus, 0);
1514 163c8a59 Luiz Capitulino
        if (obj) {
1515 163c8a59 Luiz Capitulino
            qlist_append_obj(bus_list, obj);
1516 163c8a59 Luiz Capitulino
        }
1517 e822a52a Isaku Yamahata
    }
1518 163c8a59 Luiz Capitulino
1519 163c8a59 Luiz Capitulino
    *ret_data = QOBJECT(bus_list);
1520 77d4bc34 bellard
}
1521 a41b2ff2 pbrook
1522 cb457d76 aliguori
static const char * const pci_nic_models[] = {
1523 cb457d76 aliguori
    "ne2k_pci",
1524 cb457d76 aliguori
    "i82551",
1525 cb457d76 aliguori
    "i82557b",
1526 cb457d76 aliguori
    "i82559er",
1527 cb457d76 aliguori
    "rtl8139",
1528 cb457d76 aliguori
    "e1000",
1529 cb457d76 aliguori
    "pcnet",
1530 cb457d76 aliguori
    "virtio",
1531 cb457d76 aliguori
    NULL
1532 cb457d76 aliguori
};
1533 cb457d76 aliguori
1534 9d07d757 Paul Brook
static const char * const pci_nic_names[] = {
1535 9d07d757 Paul Brook
    "ne2k_pci",
1536 9d07d757 Paul Brook
    "i82551",
1537 9d07d757 Paul Brook
    "i82557b",
1538 9d07d757 Paul Brook
    "i82559er",
1539 9d07d757 Paul Brook
    "rtl8139",
1540 9d07d757 Paul Brook
    "e1000",
1541 9d07d757 Paul Brook
    "pcnet",
1542 53c25cea Paul Brook
    "virtio-net-pci",
1543 cb457d76 aliguori
    NULL
1544 cb457d76 aliguori
};
1545 cb457d76 aliguori
1546 a41b2ff2 pbrook
/* Initialize a PCI NIC.  */
1547 33e66b86 Markus Armbruster
/* FIXME callers should check for failure, but don't */
1548 5607c388 Markus Armbruster
PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
1549 5607c388 Markus Armbruster
                        const char *default_devaddr)
1550 a41b2ff2 pbrook
{
1551 5607c388 Markus Armbruster
    const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1552 07caea31 Markus Armbruster
    PCIBus *bus;
1553 07caea31 Markus Armbruster
    int devfn;
1554 5607c388 Markus Armbruster
    PCIDevice *pci_dev;
1555 9d07d757 Paul Brook
    DeviceState *dev;
1556 cb457d76 aliguori
    int i;
1557 cb457d76 aliguori
1558 07caea31 Markus Armbruster
    i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1559 07caea31 Markus Armbruster
    if (i < 0)
1560 07caea31 Markus Armbruster
        return NULL;
1561 07caea31 Markus Armbruster
1562 07caea31 Markus Armbruster
    bus = pci_get_bus_devfn(&devfn, devaddr);
1563 07caea31 Markus Armbruster
    if (!bus) {
1564 1ecda02b Markus Armbruster
        error_report("Invalid PCI device address %s for device %s",
1565 1ecda02b Markus Armbruster
                     devaddr, pci_nic_names[i]);
1566 07caea31 Markus Armbruster
        return NULL;
1567 07caea31 Markus Armbruster
    }
1568 07caea31 Markus Armbruster
1569 499cf102 Markus Armbruster
    pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1570 9ee05825 Markus Armbruster
    dev = &pci_dev->qdev;
1571 1cc33683 Gerd Hoffmann
    qdev_set_nic_properties(dev, nd);
1572 07caea31 Markus Armbruster
    if (qdev_init(dev) < 0)
1573 07caea31 Markus Armbruster
        return NULL;
1574 9ee05825 Markus Armbruster
    return pci_dev;
1575 a41b2ff2 pbrook
}
1576 a41b2ff2 pbrook
1577 07caea31 Markus Armbruster
PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
1578 07caea31 Markus Armbruster
                               const char *default_devaddr)
1579 07caea31 Markus Armbruster
{
1580 07caea31 Markus Armbruster
    PCIDevice *res;
1581 07caea31 Markus Armbruster
1582 07caea31 Markus Armbruster
    if (qemu_show_nic_models(nd->model, pci_nic_models))
1583 07caea31 Markus Armbruster
        exit(0);
1584 07caea31 Markus Armbruster
1585 07caea31 Markus Armbruster
    res = pci_nic_init(nd, default_model, default_devaddr);
1586 07caea31 Markus Armbruster
    if (!res)
1587 07caea31 Markus Armbruster
        exit(1);
1588 07caea31 Markus Armbruster
    return res;
1589 07caea31 Markus Armbruster
}
1590 07caea31 Markus Armbruster
1591 a0c7a97e Isaku Yamahata
static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d)
1592 a0c7a97e Isaku Yamahata
{
1593 a0c7a97e Isaku Yamahata
    pci_update_mappings(d);
1594 a0c7a97e Isaku Yamahata
}
1595 a0c7a97e Isaku Yamahata
1596 783753fd Isaku Yamahata
void pci_bridge_update_mappings(PCIBus *b)
1597 a0c7a97e Isaku Yamahata
{
1598 a0c7a97e Isaku Yamahata
    PCIBus *child;
1599 a0c7a97e Isaku Yamahata
1600 a0c7a97e Isaku Yamahata
    pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn);
1601 a0c7a97e Isaku Yamahata
1602 a0c7a97e Isaku Yamahata
    QLIST_FOREACH(child, &b->child, sibling) {
1603 a0c7a97e Isaku Yamahata
        pci_bridge_update_mappings(child);
1604 a0c7a97e Isaku Yamahata
    }
1605 a0c7a97e Isaku Yamahata
}
1606 a0c7a97e Isaku Yamahata
1607 929176c3 Michael S. Tsirkin
/* Whether a given bus number is in range of the secondary
1608 929176c3 Michael S. Tsirkin
 * bus of the given bridge device. */
1609 929176c3 Michael S. Tsirkin
static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
1610 929176c3 Michael S. Tsirkin
{
1611 929176c3 Michael S. Tsirkin
    return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
1612 929176c3 Michael S. Tsirkin
             PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
1613 929176c3 Michael S. Tsirkin
        dev->config[PCI_SECONDARY_BUS] < bus_num &&
1614 929176c3 Michael S. Tsirkin
        bus_num <= dev->config[PCI_SUBORDINATE_BUS];
1615 929176c3 Michael S. Tsirkin
}
1616 929176c3 Michael S. Tsirkin
1617 e822a52a Isaku Yamahata
PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
1618 3ae80618 aliguori
{
1619 470e6363 Isaku Yamahata
    PCIBus *sec;
1620 3ae80618 aliguori
1621 470e6363 Isaku Yamahata
    if (!bus) {
1622 e822a52a Isaku Yamahata
        return NULL;
1623 470e6363 Isaku Yamahata
    }
1624 3ae80618 aliguori
1625 e822a52a Isaku Yamahata
    if (pci_bus_num(bus) == bus_num) {
1626 e822a52a Isaku Yamahata
        return bus;
1627 e822a52a Isaku Yamahata
    }
1628 e822a52a Isaku Yamahata
1629 929176c3 Michael S. Tsirkin
    /* Consider all bus numbers in range for the host pci bridge. */
1630 929176c3 Michael S. Tsirkin
    if (bus->parent_dev &&
1631 929176c3 Michael S. Tsirkin
        !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
1632 929176c3 Michael S. Tsirkin
        return NULL;
1633 929176c3 Michael S. Tsirkin
    }
1634 929176c3 Michael S. Tsirkin
1635 e822a52a Isaku Yamahata
    /* try child bus */
1636 929176c3 Michael S. Tsirkin
    for (; bus; bus = sec) {
1637 929176c3 Michael S. Tsirkin
        QLIST_FOREACH(sec, &bus->child, sibling) {
1638 929176c3 Michael S. Tsirkin
            assert(sec->parent_dev);
1639 929176c3 Michael S. Tsirkin
            if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
1640 929176c3 Michael S. Tsirkin
                return sec;
1641 929176c3 Michael S. Tsirkin
            }
1642 929176c3 Michael S. Tsirkin
            if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
1643 929176c3 Michael S. Tsirkin
                break;
1644 c021f8e6 Blue Swirl
            }
1645 e822a52a Isaku Yamahata
        }
1646 e822a52a Isaku Yamahata
    }
1647 e822a52a Isaku Yamahata
1648 e822a52a Isaku Yamahata
    return NULL;
1649 3ae80618 aliguori
}
1650 3ae80618 aliguori
1651 5256d8bf Isaku Yamahata
PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
1652 3ae80618 aliguori
{
1653 e822a52a Isaku Yamahata
    bus = pci_find_bus(bus, bus_num);
1654 3ae80618 aliguori
1655 3ae80618 aliguori
    if (!bus)
1656 3ae80618 aliguori
        return NULL;
1657 3ae80618 aliguori
1658 5256d8bf Isaku Yamahata
    return bus->devices[devfn];
1659 3ae80618 aliguori
}
1660 3ae80618 aliguori
1661 81a322d4 Gerd Hoffmann
static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1662 6b1b92d3 Paul Brook
{
1663 6b1b92d3 Paul Brook
    PCIDevice *pci_dev = (PCIDevice *)qdev;
1664 02e2da45 Paul Brook
    PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1665 6b1b92d3 Paul Brook
    PCIBus *bus;
1666 113f89df Isaku Yamahata
    int rc;
1667 ab85ceb1 Stefan Weil
    bool is_default_rom;
1668 6b1b92d3 Paul Brook
1669 a9f49946 Isaku Yamahata
    /* initialize cap_present for pci_is_express() and pci_config_size() */
1670 a9f49946 Isaku Yamahata
    if (info->is_express) {
1671 a9f49946 Isaku Yamahata
        pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1672 a9f49946 Isaku Yamahata
    }
1673 a9f49946 Isaku Yamahata
1674 02e2da45 Paul Brook
    bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1675 113f89df Isaku Yamahata
    pci_dev = do_pci_register_device(pci_dev, bus, base->name,
1676 113f89df Isaku Yamahata
                                     pci_dev->devfn, info);
1677 09e3acc6 Gerd Hoffmann
    if (pci_dev == NULL)
1678 09e3acc6 Gerd Hoffmann
        return -1;
1679 180c22e1 Gerd Hoffmann
    if (qdev->hotplugged && info->no_hotplug) {
1680 180c22e1 Gerd Hoffmann
        qerror_report(QERR_DEVICE_NO_HOTPLUG, info->qdev.name);
1681 180c22e1 Gerd Hoffmann
        do_pci_unregister_device(pci_dev);
1682 180c22e1 Gerd Hoffmann
        return -1;
1683 180c22e1 Gerd Hoffmann
    }
1684 c2afc922 Isaku Yamahata
    if (info->init) {
1685 c2afc922 Isaku Yamahata
        rc = info->init(pci_dev);
1686 c2afc922 Isaku Yamahata
        if (rc != 0) {
1687 c2afc922 Isaku Yamahata
            do_pci_unregister_device(pci_dev);
1688 c2afc922 Isaku Yamahata
            return rc;
1689 c2afc922 Isaku Yamahata
        }
1690 925fe64a Alex Williamson
    }
1691 8c52c8f3 Gerd Hoffmann
1692 8c52c8f3 Gerd Hoffmann
    /* rom loading */
1693 ab85ceb1 Stefan Weil
    is_default_rom = false;
1694 ab85ceb1 Stefan Weil
    if (pci_dev->romfile == NULL && info->romfile != NULL) {
1695 8c52c8f3 Gerd Hoffmann
        pci_dev->romfile = qemu_strdup(info->romfile);
1696 ab85ceb1 Stefan Weil
        is_default_rom = true;
1697 ab85ceb1 Stefan Weil
    }
1698 ab85ceb1 Stefan Weil
    pci_add_option_rom(pci_dev, is_default_rom);
1699 8c52c8f3 Gerd Hoffmann
1700 5beb8ad5 Isaku Yamahata
    if (bus->hotplug) {
1701 e927d487 Michael S. Tsirkin
        /* Let buses differentiate between hotplug and when device is
1702 e927d487 Michael S. Tsirkin
         * enabled during qemu machine creation. */
1703 e927d487 Michael S. Tsirkin
        rc = bus->hotplug(bus->hotplug_qdev, pci_dev,
1704 e927d487 Michael S. Tsirkin
                          qdev->hotplugged ? PCI_HOTPLUG_ENABLED:
1705 e927d487 Michael S. Tsirkin
                          PCI_COLDPLUG_ENABLED);
1706 a213ff63 Isaku Yamahata
        if (rc != 0) {
1707 a213ff63 Isaku Yamahata
            int r = pci_unregister_device(&pci_dev->qdev);
1708 a213ff63 Isaku Yamahata
            assert(!r);
1709 a213ff63 Isaku Yamahata
            return rc;
1710 a213ff63 Isaku Yamahata
        }
1711 a213ff63 Isaku Yamahata
    }
1712 ee995ffb Gerd Hoffmann
    return 0;
1713 ee995ffb Gerd Hoffmann
}
1714 ee995ffb Gerd Hoffmann
1715 ee995ffb Gerd Hoffmann
static int pci_unplug_device(DeviceState *qdev)
1716 ee995ffb Gerd Hoffmann
{
1717 ee995ffb Gerd Hoffmann
    PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1718 180c22e1 Gerd Hoffmann
    PCIDeviceInfo *info = container_of(qdev->info, PCIDeviceInfo, qdev);
1719 ee995ffb Gerd Hoffmann
1720 180c22e1 Gerd Hoffmann
    if (info->no_hotplug) {
1721 180c22e1 Gerd Hoffmann
        qerror_report(QERR_DEVICE_NO_HOTPLUG, info->qdev.name);
1722 180c22e1 Gerd Hoffmann
        return -1;
1723 180c22e1 Gerd Hoffmann
    }
1724 e927d487 Michael S. Tsirkin
    return dev->bus->hotplug(dev->bus->hotplug_qdev, dev,
1725 e927d487 Michael S. Tsirkin
                             PCI_HOTPLUG_DISABLED);
1726 6b1b92d3 Paul Brook
}
1727 6b1b92d3 Paul Brook
1728 0aab0d3a Gerd Hoffmann
void pci_qdev_register(PCIDeviceInfo *info)
1729 6b1b92d3 Paul Brook
{
1730 02e2da45 Paul Brook
    info->qdev.init = pci_qdev_init;
1731 ee995ffb Gerd Hoffmann
    info->qdev.unplug = pci_unplug_device;
1732 a36a344d Gerd Hoffmann
    info->qdev.exit = pci_unregister_device;
1733 10c4c98a Gerd Hoffmann
    info->qdev.bus_info = &pci_bus_info;
1734 074f2fff Gerd Hoffmann
    qdev_register(&info->qdev);
1735 6b1b92d3 Paul Brook
}
1736 6b1b92d3 Paul Brook
1737 0aab0d3a Gerd Hoffmann
void pci_qdev_register_many(PCIDeviceInfo *info)
1738 0aab0d3a Gerd Hoffmann
{
1739 0aab0d3a Gerd Hoffmann
    while (info->qdev.name) {
1740 0aab0d3a Gerd Hoffmann
        pci_qdev_register(info);
1741 0aab0d3a Gerd Hoffmann
        info++;
1742 0aab0d3a Gerd Hoffmann
    }
1743 0aab0d3a Gerd Hoffmann
}
1744 0aab0d3a Gerd Hoffmann
1745 49823868 Isaku Yamahata
PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
1746 49823868 Isaku Yamahata
                                    const char *name)
1747 6b1b92d3 Paul Brook
{
1748 6b1b92d3 Paul Brook
    DeviceState *dev;
1749 6b1b92d3 Paul Brook
1750 02e2da45 Paul Brook
    dev = qdev_create(&bus->qbus, name);
1751 a6307b08 Gerd Hoffmann
    qdev_prop_set_uint32(dev, "addr", devfn);
1752 49823868 Isaku Yamahata
    qdev_prop_set_bit(dev, "multifunction", multifunction);
1753 71077c1c Gerd Hoffmann
    return DO_UPCAST(PCIDevice, qdev, dev);
1754 71077c1c Gerd Hoffmann
}
1755 6b1b92d3 Paul Brook
1756 7cc050b1 Blue Swirl
PCIDevice *pci_try_create_multifunction(PCIBus *bus, int devfn,
1757 7cc050b1 Blue Swirl
                                        bool multifunction,
1758 7cc050b1 Blue Swirl
                                        const char *name)
1759 7cc050b1 Blue Swirl
{
1760 7cc050b1 Blue Swirl
    DeviceState *dev;
1761 7cc050b1 Blue Swirl
1762 7cc050b1 Blue Swirl
    dev = qdev_try_create(&bus->qbus, name);
1763 7cc050b1 Blue Swirl
    if (!dev) {
1764 7cc050b1 Blue Swirl
        return NULL;
1765 7cc050b1 Blue Swirl
    }
1766 7cc050b1 Blue Swirl
    qdev_prop_set_uint32(dev, "addr", devfn);
1767 7cc050b1 Blue Swirl
    qdev_prop_set_bit(dev, "multifunction", multifunction);
1768 7cc050b1 Blue Swirl
    return DO_UPCAST(PCIDevice, qdev, dev);
1769 7cc050b1 Blue Swirl
}
1770 7cc050b1 Blue Swirl
1771 49823868 Isaku Yamahata
PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
1772 49823868 Isaku Yamahata
                                           bool multifunction,
1773 49823868 Isaku Yamahata
                                           const char *name)
1774 71077c1c Gerd Hoffmann
{
1775 49823868 Isaku Yamahata
    PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name);
1776 e23a1b33 Markus Armbruster
    qdev_init_nofail(&dev->qdev);
1777 71077c1c Gerd Hoffmann
    return dev;
1778 6b1b92d3 Paul Brook
}
1779 6f4cbd39 Michael S. Tsirkin
1780 49823868 Isaku Yamahata
PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1781 49823868 Isaku Yamahata
{
1782 49823868 Isaku Yamahata
    return pci_create_multifunction(bus, devfn, false, name);
1783 49823868 Isaku Yamahata
}
1784 49823868 Isaku Yamahata
1785 49823868 Isaku Yamahata
PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1786 49823868 Isaku Yamahata
{
1787 49823868 Isaku Yamahata
    return pci_create_simple_multifunction(bus, devfn, false, name);
1788 49823868 Isaku Yamahata
}
1789 49823868 Isaku Yamahata
1790 7cc050b1 Blue Swirl
PCIDevice *pci_try_create(PCIBus *bus, int devfn, const char *name)
1791 7cc050b1 Blue Swirl
{
1792 7cc050b1 Blue Swirl
    return pci_try_create_multifunction(bus, devfn, false, name);
1793 7cc050b1 Blue Swirl
}
1794 7cc050b1 Blue Swirl
1795 6f4cbd39 Michael S. Tsirkin
static int pci_find_space(PCIDevice *pdev, uint8_t size)
1796 6f4cbd39 Michael S. Tsirkin
{
1797 a9f49946 Isaku Yamahata
    int config_size = pci_config_size(pdev);
1798 6f4cbd39 Michael S. Tsirkin
    int offset = PCI_CONFIG_HEADER_SIZE;
1799 6f4cbd39 Michael S. Tsirkin
    int i;
1800 a9f49946 Isaku Yamahata
    for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
1801 6f4cbd39 Michael S. Tsirkin
        if (pdev->used[i])
1802 6f4cbd39 Michael S. Tsirkin
            offset = i + 1;
1803 6f4cbd39 Michael S. Tsirkin
        else if (i - offset + 1 == size)
1804 6f4cbd39 Michael S. Tsirkin
            return offset;
1805 6f4cbd39 Michael S. Tsirkin
    return 0;
1806 6f4cbd39 Michael S. Tsirkin
}
1807 6f4cbd39 Michael S. Tsirkin
1808 6f4cbd39 Michael S. Tsirkin
static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1809 6f4cbd39 Michael S. Tsirkin
                                        uint8_t *prev_p)
1810 6f4cbd39 Michael S. Tsirkin
{
1811 6f4cbd39 Michael S. Tsirkin
    uint8_t next, prev;
1812 6f4cbd39 Michael S. Tsirkin
1813 6f4cbd39 Michael S. Tsirkin
    if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1814 6f4cbd39 Michael S. Tsirkin
        return 0;
1815 6f4cbd39 Michael S. Tsirkin
1816 6f4cbd39 Michael S. Tsirkin
    for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1817 6f4cbd39 Michael S. Tsirkin
         prev = next + PCI_CAP_LIST_NEXT)
1818 6f4cbd39 Michael S. Tsirkin
        if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1819 6f4cbd39 Michael S. Tsirkin
            break;
1820 6f4cbd39 Michael S. Tsirkin
1821 6f4cbd39 Michael S. Tsirkin
    if (prev_p)
1822 6f4cbd39 Michael S. Tsirkin
        *prev_p = prev;
1823 6f4cbd39 Michael S. Tsirkin
    return next;
1824 6f4cbd39 Michael S. Tsirkin
}
1825 6f4cbd39 Michael S. Tsirkin
1826 c2039bd0 Anthony Liguori
static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type)
1827 c2039bd0 Anthony Liguori
{
1828 c2039bd0 Anthony Liguori
    cpu_register_physical_memory(addr, size, pdev->rom_offset);
1829 c2039bd0 Anthony Liguori
}
1830 c2039bd0 Anthony Liguori
1831 ab85ceb1 Stefan Weil
/* Patch the PCI vendor and device ids in a PCI rom image if necessary.
1832 ab85ceb1 Stefan Weil
   This is needed for an option rom which is used for more than one device. */
1833 ab85ceb1 Stefan Weil
static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)
1834 ab85ceb1 Stefan Weil
{
1835 ab85ceb1 Stefan Weil
    uint16_t vendor_id;
1836 ab85ceb1 Stefan Weil
    uint16_t device_id;
1837 ab85ceb1 Stefan Weil
    uint16_t rom_vendor_id;
1838 ab85ceb1 Stefan Weil
    uint16_t rom_device_id;
1839 ab85ceb1 Stefan Weil
    uint16_t rom_magic;
1840 ab85ceb1 Stefan Weil
    uint16_t pcir_offset;
1841 ab85ceb1 Stefan Weil
    uint8_t checksum;
1842 ab85ceb1 Stefan Weil
1843 ab85ceb1 Stefan Weil
    /* Words in rom data are little endian (like in PCI configuration),
1844 ab85ceb1 Stefan Weil
       so they can be read / written with pci_get_word / pci_set_word. */
1845 ab85ceb1 Stefan Weil
1846 ab85ceb1 Stefan Weil
    /* Only a valid rom will be patched. */
1847 ab85ceb1 Stefan Weil
    rom_magic = pci_get_word(ptr);
1848 ab85ceb1 Stefan Weil
    if (rom_magic != 0xaa55) {
1849 ab85ceb1 Stefan Weil
        PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
1850 ab85ceb1 Stefan Weil
        return;
1851 ab85ceb1 Stefan Weil
    }
1852 ab85ceb1 Stefan Weil
    pcir_offset = pci_get_word(ptr + 0x18);
1853 ab85ceb1 Stefan Weil
    if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
1854 ab85ceb1 Stefan Weil
        PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
1855 ab85ceb1 Stefan Weil
        return;
1856 ab85ceb1 Stefan Weil
    }
1857 ab85ceb1 Stefan Weil
1858 ab85ceb1 Stefan Weil
    vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
1859 ab85ceb1 Stefan Weil
    device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
1860 ab85ceb1 Stefan Weil
    rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
1861 ab85ceb1 Stefan Weil
    rom_device_id = pci_get_word(ptr + pcir_offset + 6);
1862 ab85ceb1 Stefan Weil
1863 ab85ceb1 Stefan Weil
    PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
1864 ab85ceb1 Stefan Weil
                vendor_id, device_id, rom_vendor_id, rom_device_id);
1865 ab85ceb1 Stefan Weil
1866 ab85ceb1 Stefan Weil
    checksum = ptr[6];
1867 ab85ceb1 Stefan Weil
1868 ab85ceb1 Stefan Weil
    if (vendor_id != rom_vendor_id) {
1869 ab85ceb1 Stefan Weil
        /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
1870 ab85ceb1 Stefan Weil
        checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
1871 ab85ceb1 Stefan Weil
        checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
1872 ab85ceb1 Stefan Weil
        PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
1873 ab85ceb1 Stefan Weil
        ptr[6] = checksum;
1874 ab85ceb1 Stefan Weil
        pci_set_word(ptr + pcir_offset + 4, vendor_id);
1875 ab85ceb1 Stefan Weil
    }
1876 ab85ceb1 Stefan Weil
1877 ab85ceb1 Stefan Weil
    if (device_id != rom_device_id) {
1878 ab85ceb1 Stefan Weil
        /* Patch device id and checksum (at offset 6 for etherboot roms). */
1879 ab85ceb1 Stefan Weil
        checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
1880 ab85ceb1 Stefan Weil
        checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
1881 ab85ceb1 Stefan Weil
        PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
1882 ab85ceb1 Stefan Weil
        ptr[6] = checksum;
1883 ab85ceb1 Stefan Weil
        pci_set_word(ptr + pcir_offset + 6, device_id);
1884 ab85ceb1 Stefan Weil
    }
1885 ab85ceb1 Stefan Weil
}
1886 ab85ceb1 Stefan Weil
1887 c2039bd0 Anthony Liguori
/* Add an option rom for the device */
1888 ab85ceb1 Stefan Weil
static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom)
1889 c2039bd0 Anthony Liguori
{
1890 c2039bd0 Anthony Liguori
    int size;
1891 c2039bd0 Anthony Liguori
    char *path;
1892 c2039bd0 Anthony Liguori
    void *ptr;
1893 1724f049 Alex Williamson
    char name[32];
1894 c2039bd0 Anthony Liguori
1895 8c52c8f3 Gerd Hoffmann
    if (!pdev->romfile)
1896 8c52c8f3 Gerd Hoffmann
        return 0;
1897 8c52c8f3 Gerd Hoffmann
    if (strlen(pdev->romfile) == 0)
1898 8c52c8f3 Gerd Hoffmann
        return 0;
1899 8c52c8f3 Gerd Hoffmann
1900 88169ddf Gerd Hoffmann
    if (!pdev->rom_bar) {
1901 88169ddf Gerd Hoffmann
        /*
1902 88169ddf Gerd Hoffmann
         * Load rom via fw_cfg instead of creating a rom bar,
1903 88169ddf Gerd Hoffmann
         * for 0.11 compatibility.
1904 88169ddf Gerd Hoffmann
         */
1905 88169ddf Gerd Hoffmann
        int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
1906 88169ddf Gerd Hoffmann
        if (class == 0x0300) {
1907 88169ddf Gerd Hoffmann
            rom_add_vga(pdev->romfile);
1908 88169ddf Gerd Hoffmann
        } else {
1909 2e55e842 Gleb Natapov
            rom_add_option(pdev->romfile, -1);
1910 88169ddf Gerd Hoffmann
        }
1911 88169ddf Gerd Hoffmann
        return 0;
1912 88169ddf Gerd Hoffmann
    }
1913 88169ddf Gerd Hoffmann
1914 8c52c8f3 Gerd Hoffmann
    path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
1915 c2039bd0 Anthony Liguori
    if (path == NULL) {
1916 8c52c8f3 Gerd Hoffmann
        path = qemu_strdup(pdev->romfile);
1917 c2039bd0 Anthony Liguori
    }
1918 c2039bd0 Anthony Liguori
1919 c2039bd0 Anthony Liguori
    size = get_image_size(path);
1920 8c52c8f3 Gerd Hoffmann
    if (size < 0) {
1921 1ecda02b Markus Armbruster
        error_report("%s: failed to find romfile \"%s\"",
1922 1ecda02b Markus Armbruster
                     __FUNCTION__, pdev->romfile);
1923 386bbf45 Stefan Weil
        qemu_free(path);
1924 8c52c8f3 Gerd Hoffmann
        return -1;
1925 8c52c8f3 Gerd Hoffmann
    }
1926 c2039bd0 Anthony Liguori
    if (size & (size - 1)) {
1927 c2039bd0 Anthony Liguori
        size = 1 << qemu_fls(size);
1928 c2039bd0 Anthony Liguori
    }
1929 c2039bd0 Anthony Liguori
1930 1724f049 Alex Williamson
    if (pdev->qdev.info->vmsd)
1931 1724f049 Alex Williamson
        snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->vmsd->name);
1932 1724f049 Alex Williamson
    else
1933 1724f049 Alex Williamson
        snprintf(name, sizeof(name), "%s.rom", pdev->qdev.info->name);
1934 1724f049 Alex Williamson
    pdev->rom_offset = qemu_ram_alloc(&pdev->qdev, name, size);
1935 c2039bd0 Anthony Liguori
1936 c2039bd0 Anthony Liguori
    ptr = qemu_get_ram_ptr(pdev->rom_offset);
1937 c2039bd0 Anthony Liguori
    load_image(path, ptr);
1938 c2039bd0 Anthony Liguori
    qemu_free(path);
1939 c2039bd0 Anthony Liguori
1940 ab85ceb1 Stefan Weil
    if (is_default_rom) {
1941 ab85ceb1 Stefan Weil
        /* Only the default rom images will be patched (if needed). */
1942 ab85ceb1 Stefan Weil
        pci_patch_ids(pdev, ptr, size);
1943 ab85ceb1 Stefan Weil
    }
1944 ab85ceb1 Stefan Weil
1945 8c12f191 John Baboval
    qemu_put_ram_ptr(ptr);
1946 8c12f191 John Baboval
1947 c2039bd0 Anthony Liguori
    pci_register_bar(pdev, PCI_ROM_SLOT, size,
1948 c2039bd0 Anthony Liguori
                     0, pci_map_option_rom);
1949 c2039bd0 Anthony Liguori
1950 c2039bd0 Anthony Liguori
    return 0;
1951 c2039bd0 Anthony Liguori
}
1952 c2039bd0 Anthony Liguori
1953 230741dc Alex Williamson
static void pci_del_option_rom(PCIDevice *pdev)
1954 230741dc Alex Williamson
{
1955 230741dc Alex Williamson
    if (!pdev->rom_offset)
1956 230741dc Alex Williamson
        return;
1957 230741dc Alex Williamson
1958 230741dc Alex Williamson
    qemu_ram_free(pdev->rom_offset);
1959 230741dc Alex Williamson
    pdev->rom_offset = 0;
1960 230741dc Alex Williamson
}
1961 230741dc Alex Williamson
1962 ca77089d Isaku Yamahata
/*
1963 ca77089d Isaku Yamahata
 * if !offset
1964 ca77089d Isaku Yamahata
 * Reserve space and add capability to the linked list in pci config space
1965 ca77089d Isaku Yamahata
 *
1966 ca77089d Isaku Yamahata
 * if offset = 0,
1967 ca77089d Isaku Yamahata
 * Find and reserve space and add capability to the linked list
1968 ca77089d Isaku Yamahata
 * in pci config space */
1969 ca77089d Isaku Yamahata
int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
1970 ca77089d Isaku Yamahata
                       uint8_t offset, uint8_t size)
1971 6f4cbd39 Michael S. Tsirkin
{
1972 ca77089d Isaku Yamahata
    uint8_t *config;
1973 ca77089d Isaku Yamahata
    if (!offset) {
1974 ca77089d Isaku Yamahata
        offset = pci_find_space(pdev, size);
1975 ca77089d Isaku Yamahata
        if (!offset) {
1976 ca77089d Isaku Yamahata
            return -ENOSPC;
1977 ca77089d Isaku Yamahata
        }
1978 ca77089d Isaku Yamahata
    }
1979 ca77089d Isaku Yamahata
1980 ca77089d Isaku Yamahata
    config = pdev->config + offset;
1981 6f4cbd39 Michael S. Tsirkin
    config[PCI_CAP_LIST_ID] = cap_id;
1982 6f4cbd39 Michael S. Tsirkin
    config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1983 6f4cbd39 Michael S. Tsirkin
    pdev->config[PCI_CAPABILITY_LIST] = offset;
1984 6f4cbd39 Michael S. Tsirkin
    pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1985 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0xFF, size);
1986 6f4cbd39 Michael S. Tsirkin
    /* Make capability read-only by default */
1987 6f4cbd39 Michael S. Tsirkin
    memset(pdev->wmask + offset, 0, size);
1988 bd4b65ee Michael S. Tsirkin
    /* Check capability by default */
1989 bd4b65ee Michael S. Tsirkin
    memset(pdev->cmask + offset, 0xFF, size);
1990 6f4cbd39 Michael S. Tsirkin
    return offset;
1991 6f4cbd39 Michael S. Tsirkin
}
1992 6f4cbd39 Michael S. Tsirkin
1993 6f4cbd39 Michael S. Tsirkin
/* Unlink capability from the pci config space. */
1994 6f4cbd39 Michael S. Tsirkin
void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1995 6f4cbd39 Michael S. Tsirkin
{
1996 6f4cbd39 Michael S. Tsirkin
    uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1997 6f4cbd39 Michael S. Tsirkin
    if (!offset)
1998 6f4cbd39 Michael S. Tsirkin
        return;
1999 6f4cbd39 Michael S. Tsirkin
    pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
2000 ebabb67a Stefan Weil
    /* Make capability writable again */
2001 6f4cbd39 Michael S. Tsirkin
    memset(pdev->wmask + offset, 0xff, size);
2002 1a4f5971 Isaku Yamahata
    memset(pdev->w1cmask + offset, 0, size);
2003 bd4b65ee Michael S. Tsirkin
    /* Clear cmask as device-specific registers can't be checked */
2004 bd4b65ee Michael S. Tsirkin
    memset(pdev->cmask + offset, 0, size);
2005 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0, size);
2006 6f4cbd39 Michael S. Tsirkin
2007 6f4cbd39 Michael S. Tsirkin
    if (!pdev->config[PCI_CAPABILITY_LIST])
2008 6f4cbd39 Michael S. Tsirkin
        pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
2009 6f4cbd39 Michael S. Tsirkin
}
2010 6f4cbd39 Michael S. Tsirkin
2011 6f4cbd39 Michael S. Tsirkin
/* Reserve space for capability at a known offset (to call after load). */
2012 6f4cbd39 Michael S. Tsirkin
void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
2013 6f4cbd39 Michael S. Tsirkin
{
2014 6f4cbd39 Michael S. Tsirkin
    memset(pdev->used + offset, 0xff, size);
2015 6f4cbd39 Michael S. Tsirkin
}
2016 6f4cbd39 Michael S. Tsirkin
2017 6f4cbd39 Michael S. Tsirkin
uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
2018 6f4cbd39 Michael S. Tsirkin
{
2019 6f4cbd39 Michael S. Tsirkin
    return pci_find_capability_list(pdev, cap_id, NULL);
2020 6f4cbd39 Michael S. Tsirkin
}
2021 10c4c98a Gerd Hoffmann
2022 10c4c98a Gerd Hoffmann
static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
2023 10c4c98a Gerd Hoffmann
{
2024 10c4c98a Gerd Hoffmann
    PCIDevice *d = (PCIDevice *)dev;
2025 10c4c98a Gerd Hoffmann
    const pci_class_desc *desc;
2026 10c4c98a Gerd Hoffmann
    char ctxt[64];
2027 10c4c98a Gerd Hoffmann
    PCIIORegion *r;
2028 10c4c98a Gerd Hoffmann
    int i, class;
2029 10c4c98a Gerd Hoffmann
2030 b0ff8eb2 Isaku Yamahata
    class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2031 10c4c98a Gerd Hoffmann
    desc = pci_class_descriptions;
2032 10c4c98a Gerd Hoffmann
    while (desc->desc && class != desc->class)
2033 10c4c98a Gerd Hoffmann
        desc++;
2034 10c4c98a Gerd Hoffmann
    if (desc->desc) {
2035 10c4c98a Gerd Hoffmann
        snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
2036 10c4c98a Gerd Hoffmann
    } else {
2037 10c4c98a Gerd Hoffmann
        snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
2038 10c4c98a Gerd Hoffmann
    }
2039 10c4c98a Gerd Hoffmann
2040 10c4c98a Gerd Hoffmann
    monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
2041 10c4c98a Gerd Hoffmann
                   "pci id %04x:%04x (sub %04x:%04x)\n",
2042 7f5feab4 Alex Williamson
                   indent, "", ctxt, pci_bus_num(d->bus),
2043 e822a52a Isaku Yamahata
                   PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
2044 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_VENDOR_ID),
2045 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_DEVICE_ID),
2046 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
2047 b0ff8eb2 Isaku Yamahata
                   pci_get_word(d->config + PCI_SUBSYSTEM_ID));
2048 10c4c98a Gerd Hoffmann
    for (i = 0; i < PCI_NUM_REGIONS; i++) {
2049 10c4c98a Gerd Hoffmann
        r = &d->io_regions[i];
2050 10c4c98a Gerd Hoffmann
        if (!r->size)
2051 10c4c98a Gerd Hoffmann
            continue;
2052 89e8b13c Isaku Yamahata
        monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
2053 89e8b13c Isaku Yamahata
                       " [0x%"FMT_PCIBUS"]\n",
2054 89e8b13c Isaku Yamahata
                       indent, "",
2055 0392a017 Isaku Yamahata
                       i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
2056 10c4c98a Gerd Hoffmann
                       r->addr, r->addr + r->size - 1);
2057 10c4c98a Gerd Hoffmann
    }
2058 10c4c98a Gerd Hoffmann
}
2059 03587182 Gerd Hoffmann
2060 5e0259e7 Gleb Natapov
static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
2061 5e0259e7 Gleb Natapov
{
2062 5e0259e7 Gleb Natapov
    PCIDevice *d = (PCIDevice *)dev;
2063 5e0259e7 Gleb Natapov
    const char *name = NULL;
2064 5e0259e7 Gleb Natapov
    const pci_class_desc *desc =  pci_class_descriptions;
2065 5e0259e7 Gleb Natapov
    int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2066 5e0259e7 Gleb Natapov
2067 5e0259e7 Gleb Natapov
    while (desc->desc &&
2068 5e0259e7 Gleb Natapov
          (class & ~desc->fw_ign_bits) !=
2069 5e0259e7 Gleb Natapov
          (desc->class & ~desc->fw_ign_bits)) {
2070 5e0259e7 Gleb Natapov
        desc++;
2071 5e0259e7 Gleb Natapov
    }
2072 5e0259e7 Gleb Natapov
2073 5e0259e7 Gleb Natapov
    if (desc->desc) {
2074 5e0259e7 Gleb Natapov
        name = desc->fw_name;
2075 5e0259e7 Gleb Natapov
    }
2076 5e0259e7 Gleb Natapov
2077 5e0259e7 Gleb Natapov
    if (name) {
2078 5e0259e7 Gleb Natapov
        pstrcpy(buf, len, name);
2079 5e0259e7 Gleb Natapov
    } else {
2080 5e0259e7 Gleb Natapov
        snprintf(buf, len, "pci%04x,%04x",
2081 5e0259e7 Gleb Natapov
                 pci_get_word(d->config + PCI_VENDOR_ID),
2082 5e0259e7 Gleb Natapov
                 pci_get_word(d->config + PCI_DEVICE_ID));
2083 5e0259e7 Gleb Natapov
    }
2084 5e0259e7 Gleb Natapov
2085 5e0259e7 Gleb Natapov
    return buf;
2086 5e0259e7 Gleb Natapov
}
2087 5e0259e7 Gleb Natapov
2088 5e0259e7 Gleb Natapov
static char *pcibus_get_fw_dev_path(DeviceState *dev)
2089 5e0259e7 Gleb Natapov
{
2090 5e0259e7 Gleb Natapov
    PCIDevice *d = (PCIDevice *)dev;
2091 5e0259e7 Gleb Natapov
    char path[50], name[33];
2092 5e0259e7 Gleb Natapov
    int off;
2093 5e0259e7 Gleb Natapov
2094 5e0259e7 Gleb Natapov
    off = snprintf(path, sizeof(path), "%s@%x",
2095 5e0259e7 Gleb Natapov
                   pci_dev_fw_name(dev, name, sizeof name),
2096 5e0259e7 Gleb Natapov
                   PCI_SLOT(d->devfn));
2097 5e0259e7 Gleb Natapov
    if (PCI_FUNC(d->devfn))
2098 5e0259e7 Gleb Natapov
        snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
2099 5e0259e7 Gleb Natapov
    return strdup(path);
2100 5e0259e7 Gleb Natapov
}
2101 5e0259e7 Gleb Natapov
2102 4f43c1ff Alex Williamson
static char *pcibus_get_dev_path(DeviceState *dev)
2103 4f43c1ff Alex Williamson
{
2104 a6a7005d Michael S. Tsirkin
    PCIDevice *d = container_of(dev, PCIDevice, qdev);
2105 a6a7005d Michael S. Tsirkin
    PCIDevice *t;
2106 a6a7005d Michael S. Tsirkin
    int slot_depth;
2107 a6a7005d Michael S. Tsirkin
    /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
2108 a6a7005d Michael S. Tsirkin
     * 00 is added here to make this format compatible with
2109 a6a7005d Michael S. Tsirkin
     * domain:Bus:Slot.Func for systems without nested PCI bridges.
2110 a6a7005d Michael S. Tsirkin
     * Slot.Function list specifies the slot and function numbers for all
2111 a6a7005d Michael S. Tsirkin
     * devices on the path from root to the specific device. */
2112 2991181a Michael S. Tsirkin
    char domain[] = "DDDD:00";
2113 2991181a Michael S. Tsirkin
    char slot[] = ":SS.F";
2114 2991181a Michael S. Tsirkin
    int domain_len = sizeof domain - 1 /* For '\0' */;
2115 2991181a Michael S. Tsirkin
    int slot_len = sizeof slot - 1 /* For '\0' */;
2116 a6a7005d Michael S. Tsirkin
    int path_len;
2117 a6a7005d Michael S. Tsirkin
    char *path, *p;
2118 2991181a Michael S. Tsirkin
    int s;
2119 a6a7005d Michael S. Tsirkin
2120 a6a7005d Michael S. Tsirkin
    /* Calculate # of slots on path between device and root. */;
2121 a6a7005d Michael S. Tsirkin
    slot_depth = 0;
2122 a6a7005d Michael S. Tsirkin
    for (t = d; t; t = t->bus->parent_dev) {
2123 a6a7005d Michael S. Tsirkin
        ++slot_depth;
2124 a6a7005d Michael S. Tsirkin
    }
2125 a6a7005d Michael S. Tsirkin
2126 a6a7005d Michael S. Tsirkin
    path_len = domain_len + slot_len * slot_depth;
2127 a6a7005d Michael S. Tsirkin
2128 a6a7005d Michael S. Tsirkin
    /* Allocate memory, fill in the terminating null byte. */
2129 e10990c3 Isaku Yamahata
    path = qemu_malloc(path_len + 1 /* For '\0' */);
2130 a6a7005d Michael S. Tsirkin
    path[path_len] = '\0';
2131 a6a7005d Michael S. Tsirkin
2132 a6a7005d Michael S. Tsirkin
    /* First field is the domain. */
2133 2991181a Michael S. Tsirkin
    s = snprintf(domain, sizeof domain, "%04x:00", pci_find_domain(d->bus));
2134 2991181a Michael S. Tsirkin
    assert(s == domain_len);
2135 2991181a Michael S. Tsirkin
    memcpy(path, domain, domain_len);
2136 a6a7005d Michael S. Tsirkin
2137 a6a7005d Michael S. Tsirkin
    /* Fill in slot numbers. We walk up from device to root, so need to print
2138 a6a7005d Michael S. Tsirkin
     * them in the reverse order, last to first. */
2139 a6a7005d Michael S. Tsirkin
    p = path + path_len;
2140 a6a7005d Michael S. Tsirkin
    for (t = d; t; t = t->bus->parent_dev) {
2141 a6a7005d Michael S. Tsirkin
        p -= slot_len;
2142 2991181a Michael S. Tsirkin
        s = snprintf(slot, sizeof slot, ":%02x.%x",
2143 4c900518 Isaku Yamahata
                     PCI_SLOT(t->devfn), PCI_FUNC(t->devfn));
2144 2991181a Michael S. Tsirkin
        assert(s == slot_len);
2145 2991181a Michael S. Tsirkin
        memcpy(p, slot, slot_len);
2146 a6a7005d Michael S. Tsirkin
    }
2147 a6a7005d Michael S. Tsirkin
2148 a6a7005d Michael S. Tsirkin
    return path;
2149 4f43c1ff Alex Williamson
}
2150 4f43c1ff Alex Williamson
2151 f3006dd1 Isaku Yamahata
static int pci_qdev_find_recursive(PCIBus *bus,
2152 f3006dd1 Isaku Yamahata
                                   const char *id, PCIDevice **pdev)
2153 f3006dd1 Isaku Yamahata
{
2154 f3006dd1 Isaku Yamahata
    DeviceState *qdev = qdev_find_recursive(&bus->qbus, id);
2155 f3006dd1 Isaku Yamahata
    if (!qdev) {
2156 f3006dd1 Isaku Yamahata
        return -ENODEV;
2157 f3006dd1 Isaku Yamahata
    }
2158 f3006dd1 Isaku Yamahata
2159 f3006dd1 Isaku Yamahata
    /* roughly check if given qdev is pci device */
2160 f3006dd1 Isaku Yamahata
    if (qdev->info->init == &pci_qdev_init &&
2161 f3006dd1 Isaku Yamahata
        qdev->parent_bus->info == &pci_bus_info) {
2162 f3006dd1 Isaku Yamahata
        *pdev = DO_UPCAST(PCIDevice, qdev, qdev);
2163 f3006dd1 Isaku Yamahata
        return 0;
2164 f3006dd1 Isaku Yamahata
    }
2165 f3006dd1 Isaku Yamahata
    return -EINVAL;
2166 f3006dd1 Isaku Yamahata
}
2167 f3006dd1 Isaku Yamahata
2168 f3006dd1 Isaku Yamahata
int pci_qdev_find_device(const char *id, PCIDevice **pdev)
2169 f3006dd1 Isaku Yamahata
{
2170 f3006dd1 Isaku Yamahata
    struct PCIHostBus *host;
2171 f3006dd1 Isaku Yamahata
    int rc = -ENODEV;
2172 f3006dd1 Isaku Yamahata
2173 f3006dd1 Isaku Yamahata
    QLIST_FOREACH(host, &host_buses, next) {
2174 f3006dd1 Isaku Yamahata
        int tmp = pci_qdev_find_recursive(host->bus, id, pdev);
2175 f3006dd1 Isaku Yamahata
        if (!tmp) {
2176 f3006dd1 Isaku Yamahata
            rc = 0;
2177 f3006dd1 Isaku Yamahata
            break;
2178 f3006dd1 Isaku Yamahata
        }
2179 f3006dd1 Isaku Yamahata
        if (tmp != -ENODEV) {
2180 f3006dd1 Isaku Yamahata
            rc = tmp;
2181 f3006dd1 Isaku Yamahata
        }
2182 f3006dd1 Isaku Yamahata
    }
2183 f3006dd1 Isaku Yamahata
2184 f3006dd1 Isaku Yamahata
    return rc;
2185 f3006dd1 Isaku Yamahata
}