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