Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / hw / pci / pci.c
blobc8b1ddbb61cf46a05d950d41d44bb4ab041fddb0
1 /*
2 * QEMU PCI bus manager
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "hw/hw.h"
25 #include "hw/pci/pci.h"
26 #include "hw/pci/pci_bridge.h"
27 #include "hw/pci/pci_bus.h"
28 #include "hw/pci/pci_host.h"
29 #include "monitor/monitor.h"
30 #include "net/net.h"
31 #include "sysemu/sysemu.h"
32 #include "hw/loader.h"
33 #include "qemu/error-report.h"
34 #include "qemu/range.h"
35 #include "qmp-commands.h"
36 #include "trace.h"
37 #include "hw/pci/msi.h"
38 #include "hw/pci/msix.h"
39 #include "exec/address-spaces.h"
40 #include "hw/hotplug.h"
41 #include "hw/boards.h"
43 //#define DEBUG_PCI
44 #ifdef DEBUG_PCI
45 # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
46 #else
47 # define PCI_DPRINTF(format, ...) do { } while (0)
48 #endif
50 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
51 static char *pcibus_get_dev_path(DeviceState *dev);
52 static char *pcibus_get_fw_dev_path(DeviceState *dev);
53 static void pcibus_reset(BusState *qbus);
55 static Property pci_props[] = {
56 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
57 DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
58 DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1),
59 DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
60 QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
61 DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present,
62 QEMU_PCI_CAP_SERR_BITNR, true),
63 DEFINE_PROP_END_OF_LIST()
66 static const VMStateDescription vmstate_pcibus = {
67 .name = "PCIBUS",
68 .version_id = 1,
69 .minimum_version_id = 1,
70 .fields = (VMStateField[]) {
71 VMSTATE_INT32_EQUAL(nirq, PCIBus),
72 VMSTATE_VARRAY_INT32(irq_count, PCIBus,
73 nirq, 0, vmstate_info_int32,
74 int32_t),
75 VMSTATE_END_OF_LIST()
79 static void pci_bus_realize(BusState *qbus, Error **errp)
81 PCIBus *bus = PCI_BUS(qbus);
83 vmstate_register(NULL, -1, &vmstate_pcibus, bus);
86 static void pci_bus_unrealize(BusState *qbus, Error **errp)
88 PCIBus *bus = PCI_BUS(qbus);
90 vmstate_unregister(NULL, &vmstate_pcibus, bus);
93 static bool pcibus_is_root(PCIBus *bus)
95 return !bus->parent_dev;
98 static int pcibus_num(PCIBus *bus)
100 if (pcibus_is_root(bus)) {
101 return 0; /* pci host bridge */
103 return bus->parent_dev->config[PCI_SECONDARY_BUS];
106 static uint16_t pcibus_numa_node(PCIBus *bus)
108 return NUMA_NODE_UNASSIGNED;
111 static void pci_bus_class_init(ObjectClass *klass, void *data)
113 BusClass *k = BUS_CLASS(klass);
114 PCIBusClass *pbc = PCI_BUS_CLASS(klass);
116 k->print_dev = pcibus_dev_print;
117 k->get_dev_path = pcibus_get_dev_path;
118 k->get_fw_dev_path = pcibus_get_fw_dev_path;
119 k->realize = pci_bus_realize;
120 k->unrealize = pci_bus_unrealize;
121 k->reset = pcibus_reset;
123 pbc->is_root = pcibus_is_root;
124 pbc->bus_num = pcibus_num;
125 pbc->numa_node = pcibus_numa_node;
128 static const TypeInfo pci_bus_info = {
129 .name = TYPE_PCI_BUS,
130 .parent = TYPE_BUS,
131 .instance_size = sizeof(PCIBus),
132 .class_size = sizeof(PCIBusClass),
133 .class_init = pci_bus_class_init,
136 static const TypeInfo pcie_bus_info = {
137 .name = TYPE_PCIE_BUS,
138 .parent = TYPE_PCI_BUS,
141 static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
142 static void pci_update_mappings(PCIDevice *d);
143 static void pci_irq_handler(void *opaque, int irq_num, int level);
144 static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, Error **);
145 static void pci_del_option_rom(PCIDevice *pdev);
147 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
148 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
150 static QLIST_HEAD(, PCIHostState) pci_host_bridges;
152 int pci_bar(PCIDevice *d, int reg)
154 uint8_t type;
156 if (reg != PCI_ROM_SLOT)
157 return PCI_BASE_ADDRESS_0 + reg * 4;
159 type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
160 return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
163 static inline int pci_irq_state(PCIDevice *d, int irq_num)
165 return (d->irq_state >> irq_num) & 0x1;
168 static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
170 d->irq_state &= ~(0x1 << irq_num);
171 d->irq_state |= level << irq_num;
174 static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
176 PCIBus *bus;
177 for (;;) {
178 bus = pci_dev->bus;
179 irq_num = bus->map_irq(pci_dev, irq_num);
180 if (bus->set_irq)
181 break;
182 pci_dev = bus->parent_dev;
184 bus->irq_count[irq_num] += change;
185 bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
188 int pci_bus_get_irq_level(PCIBus *bus, int irq_num)
190 assert(irq_num >= 0);
191 assert(irq_num < bus->nirq);
192 return !!bus->irq_count[irq_num];
195 /* Update interrupt status bit in config space on interrupt
196 * state change. */
197 static void pci_update_irq_status(PCIDevice *dev)
199 if (dev->irq_state) {
200 dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
201 } else {
202 dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
206 void pci_device_deassert_intx(PCIDevice *dev)
208 int i;
209 for (i = 0; i < PCI_NUM_PINS; ++i) {
210 pci_irq_handler(dev, i, 0);
214 static void pci_do_device_reset(PCIDevice *dev)
216 int r;
218 pci_device_deassert_intx(dev);
219 assert(dev->irq_state == 0);
221 /* Clear all writable bits */
222 pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
223 pci_get_word(dev->wmask + PCI_COMMAND) |
224 pci_get_word(dev->w1cmask + PCI_COMMAND));
225 pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
226 pci_get_word(dev->wmask + PCI_STATUS) |
227 pci_get_word(dev->w1cmask + PCI_STATUS));
228 dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
229 dev->config[PCI_INTERRUPT_LINE] = 0x0;
230 for (r = 0; r < PCI_NUM_REGIONS; ++r) {
231 PCIIORegion *region = &dev->io_regions[r];
232 if (!region->size) {
233 continue;
236 if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
237 region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
238 pci_set_quad(dev->config + pci_bar(dev, r), region->type);
239 } else {
240 pci_set_long(dev->config + pci_bar(dev, r), region->type);
243 pci_update_mappings(dev);
245 msi_reset(dev);
246 msix_reset(dev);
250 * This function is called on #RST and FLR.
251 * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
253 void pci_device_reset(PCIDevice *dev)
255 qdev_reset_all(&dev->qdev);
256 pci_do_device_reset(dev);
260 * Trigger pci bus reset under a given bus.
261 * Called via qbus_reset_all on RST# assert, after the devices
262 * have been reset qdev_reset_all-ed already.
264 static void pcibus_reset(BusState *qbus)
266 PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
267 int i;
269 for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
270 if (bus->devices[i]) {
271 pci_do_device_reset(bus->devices[i]);
275 for (i = 0; i < bus->nirq; i++) {
276 assert(bus->irq_count[i] == 0);
280 static void pci_host_bus_register(PCIBus *bus, DeviceState *parent)
282 PCIHostState *host_bridge = PCI_HOST_BRIDGE(parent);
284 QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next);
287 PCIBus *pci_find_primary_bus(void)
289 PCIBus *primary_bus = NULL;
290 PCIHostState *host;
292 QLIST_FOREACH(host, &pci_host_bridges, next) {
293 if (primary_bus) {
294 /* We have multiple root buses, refuse to select a primary */
295 return NULL;
297 primary_bus = host->bus;
300 return primary_bus;
303 PCIBus *pci_device_root_bus(const PCIDevice *d)
305 PCIBus *bus = d->bus;
307 while (!pci_bus_is_root(bus)) {
308 d = bus->parent_dev;
309 assert(d != NULL);
311 bus = d->bus;
314 return bus;
317 const char *pci_root_bus_path(PCIDevice *dev)
319 PCIBus *rootbus = pci_device_root_bus(dev);
320 PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
321 PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge);
323 assert(host_bridge->bus == rootbus);
325 if (hc->root_bus_path) {
326 return (*hc->root_bus_path)(host_bridge, rootbus);
329 return rootbus->qbus.name;
332 static void pci_bus_init(PCIBus *bus, DeviceState *parent,
333 const char *name,
334 MemoryRegion *address_space_mem,
335 MemoryRegion *address_space_io,
336 uint8_t devfn_min)
338 assert(PCI_FUNC(devfn_min) == 0);
339 bus->devfn_min = devfn_min;
340 bus->address_space_mem = address_space_mem;
341 bus->address_space_io = address_space_io;
343 /* host bridge */
344 QLIST_INIT(&bus->child);
346 pci_host_bus_register(bus, parent);
349 bool pci_bus_is_express(PCIBus *bus)
351 return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
354 bool pci_bus_is_root(PCIBus *bus)
356 return PCI_BUS_GET_CLASS(bus)->is_root(bus);
359 void pci_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent,
360 const char *name,
361 MemoryRegion *address_space_mem,
362 MemoryRegion *address_space_io,
363 uint8_t devfn_min, const char *typename)
365 qbus_create_inplace(bus, bus_size, typename, parent, name);
366 pci_bus_init(bus, parent, name, address_space_mem,
367 address_space_io, devfn_min);
370 PCIBus *pci_bus_new(DeviceState *parent, const char *name,
371 MemoryRegion *address_space_mem,
372 MemoryRegion *address_space_io,
373 uint8_t devfn_min, const char *typename)
375 PCIBus *bus;
377 bus = PCI_BUS(qbus_create(typename, parent, name));
378 pci_bus_init(bus, parent, name, address_space_mem,
379 address_space_io, devfn_min);
380 return bus;
383 void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
384 void *irq_opaque, int nirq)
386 bus->set_irq = set_irq;
387 bus->map_irq = map_irq;
388 bus->irq_opaque = irq_opaque;
389 bus->nirq = nirq;
390 bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0]));
393 PCIBus *pci_register_bus(DeviceState *parent, const char *name,
394 pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
395 void *irq_opaque,
396 MemoryRegion *address_space_mem,
397 MemoryRegion *address_space_io,
398 uint8_t devfn_min, int nirq, const char *typename)
400 PCIBus *bus;
402 bus = pci_bus_new(parent, name, address_space_mem,
403 address_space_io, devfn_min, typename);
404 pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
405 return bus;
408 int pci_bus_num(PCIBus *s)
410 return PCI_BUS_GET_CLASS(s)->bus_num(s);
413 int pci_bus_numa_node(PCIBus *bus)
415 return PCI_BUS_GET_CLASS(bus)->numa_node(bus);
418 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
420 PCIDevice *s = container_of(pv, PCIDevice, config);
421 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(s);
422 uint8_t *config;
423 int i;
425 assert(size == pci_config_size(s));
426 config = g_malloc(size);
428 qemu_get_buffer(f, config, size);
429 for (i = 0; i < size; ++i) {
430 if ((config[i] ^ s->config[i]) &
431 s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
432 error_report("%s: Bad config data: i=0x%x read: %x device: %x "
433 "cmask: %x wmask: %x w1cmask:%x", __func__,
434 i, config[i], s->config[i],
435 s->cmask[i], s->wmask[i], s->w1cmask[i]);
436 g_free(config);
437 return -EINVAL;
440 memcpy(s->config, config, size);
442 pci_update_mappings(s);
443 if (pc->is_bridge) {
444 PCIBridge *b = PCI_BRIDGE(s);
445 pci_bridge_update_mappings(b);
448 memory_region_set_enabled(&s->bus_master_enable_region,
449 pci_get_word(s->config + PCI_COMMAND)
450 & PCI_COMMAND_MASTER);
452 g_free(config);
453 return 0;
456 /* just put buffer */
457 static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
459 const uint8_t **v = pv;
460 assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
461 qemu_put_buffer(f, *v, size);
464 static VMStateInfo vmstate_info_pci_config = {
465 .name = "pci config",
466 .get = get_pci_config_device,
467 .put = put_pci_config_device,
470 static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
472 PCIDevice *s = container_of(pv, PCIDevice, irq_state);
473 uint32_t irq_state[PCI_NUM_PINS];
474 int i;
475 for (i = 0; i < PCI_NUM_PINS; ++i) {
476 irq_state[i] = qemu_get_be32(f);
477 if (irq_state[i] != 0x1 && irq_state[i] != 0) {
478 fprintf(stderr, "irq state %d: must be 0 or 1.\n",
479 irq_state[i]);
480 return -EINVAL;
484 for (i = 0; i < PCI_NUM_PINS; ++i) {
485 pci_set_irq_state(s, i, irq_state[i]);
488 return 0;
491 static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
493 int i;
494 PCIDevice *s = container_of(pv, PCIDevice, irq_state);
496 for (i = 0; i < PCI_NUM_PINS; ++i) {
497 qemu_put_be32(f, pci_irq_state(s, i));
501 static VMStateInfo vmstate_info_pci_irq_state = {
502 .name = "pci irq state",
503 .get = get_pci_irq_state,
504 .put = put_pci_irq_state,
507 const VMStateDescription vmstate_pci_device = {
508 .name = "PCIDevice",
509 .version_id = 2,
510 .minimum_version_id = 1,
511 .fields = (VMStateField[]) {
512 VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice),
513 VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
514 vmstate_info_pci_config,
515 PCI_CONFIG_SPACE_SIZE),
516 VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
517 vmstate_info_pci_irq_state,
518 PCI_NUM_PINS * sizeof(int32_t)),
519 VMSTATE_END_OF_LIST()
523 const VMStateDescription vmstate_pcie_device = {
524 .name = "PCIEDevice",
525 .version_id = 2,
526 .minimum_version_id = 1,
527 .fields = (VMStateField[]) {
528 VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice),
529 VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
530 vmstate_info_pci_config,
531 PCIE_CONFIG_SPACE_SIZE),
532 VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
533 vmstate_info_pci_irq_state,
534 PCI_NUM_PINS * sizeof(int32_t)),
535 VMSTATE_END_OF_LIST()
539 static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
541 return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
544 void pci_device_save(PCIDevice *s, QEMUFile *f)
546 /* Clear interrupt status bit: it is implicit
547 * in irq_state which we are saving.
548 * This makes us compatible with old devices
549 * which never set or clear this bit. */
550 s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
551 vmstate_save_state(f, pci_get_vmstate(s), s, NULL);
552 /* Restore the interrupt status bit. */
553 pci_update_irq_status(s);
556 int pci_device_load(PCIDevice *s, QEMUFile *f)
558 int ret;
559 ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
560 /* Restore the interrupt status bit. */
561 pci_update_irq_status(s);
562 return ret;
565 static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
567 pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
568 pci_default_sub_vendor_id);
569 pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
570 pci_default_sub_device_id);
574 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
575 * [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
577 static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
578 unsigned int *slotp, unsigned int *funcp)
580 const char *p;
581 char *e;
582 unsigned long val;
583 unsigned long dom = 0, bus = 0;
584 unsigned int slot = 0;
585 unsigned int func = 0;
587 p = addr;
588 val = strtoul(p, &e, 16);
589 if (e == p)
590 return -1;
591 if (*e == ':') {
592 bus = val;
593 p = e + 1;
594 val = strtoul(p, &e, 16);
595 if (e == p)
596 return -1;
597 if (*e == ':') {
598 dom = bus;
599 bus = val;
600 p = e + 1;
601 val = strtoul(p, &e, 16);
602 if (e == p)
603 return -1;
607 slot = val;
609 if (funcp != NULL) {
610 if (*e != '.')
611 return -1;
613 p = e + 1;
614 val = strtoul(p, &e, 16);
615 if (e == p)
616 return -1;
618 func = val;
621 /* if funcp == NULL func is 0 */
622 if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
623 return -1;
625 if (*e)
626 return -1;
628 *domp = dom;
629 *busp = bus;
630 *slotp = slot;
631 if (funcp != NULL)
632 *funcp = func;
633 return 0;
636 static PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root,
637 const char *devaddr)
639 int dom, bus;
640 unsigned slot;
642 if (!root) {
643 fprintf(stderr, "No primary PCI bus\n");
644 return NULL;
647 assert(!root->parent_dev);
649 if (!devaddr) {
650 *devfnp = -1;
651 return pci_find_bus_nr(root, 0);
654 if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
655 return NULL;
658 if (dom != 0) {
659 fprintf(stderr, "No support for non-zero PCI domains\n");
660 return NULL;
663 *devfnp = PCI_DEVFN(slot, 0);
664 return pci_find_bus_nr(root, bus);
667 static void pci_init_cmask(PCIDevice *dev)
669 pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
670 pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
671 dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
672 dev->cmask[PCI_REVISION_ID] = 0xff;
673 dev->cmask[PCI_CLASS_PROG] = 0xff;
674 pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
675 dev->cmask[PCI_HEADER_TYPE] = 0xff;
676 dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
679 static void pci_init_wmask(PCIDevice *dev)
681 int config_size = pci_config_size(dev);
683 dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
684 dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
685 pci_set_word(dev->wmask + PCI_COMMAND,
686 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
687 PCI_COMMAND_INTX_DISABLE);
688 if (dev->cap_present & QEMU_PCI_CAP_SERR) {
689 pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
692 memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
693 config_size - PCI_CONFIG_HEADER_SIZE);
696 static void pci_init_w1cmask(PCIDevice *dev)
699 * Note: It's okay to set w1cmask even for readonly bits as
700 * long as their value is hardwired to 0.
702 pci_set_word(dev->w1cmask + PCI_STATUS,
703 PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
704 PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
705 PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
708 static void pci_init_mask_bridge(PCIDevice *d)
710 /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
711 PCI_SEC_LETENCY_TIMER */
712 memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
714 /* base and limit */
715 d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
716 d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
717 pci_set_word(d->wmask + PCI_MEMORY_BASE,
718 PCI_MEMORY_RANGE_MASK & 0xffff);
719 pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
720 PCI_MEMORY_RANGE_MASK & 0xffff);
721 pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
722 PCI_PREF_RANGE_MASK & 0xffff);
723 pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
724 PCI_PREF_RANGE_MASK & 0xffff);
726 /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
727 memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
729 /* Supported memory and i/o types */
730 d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16;
731 d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16;
732 pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE,
733 PCI_PREF_RANGE_TYPE_64);
734 pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT,
735 PCI_PREF_RANGE_TYPE_64);
738 * TODO: Bridges default to 10-bit VGA decoding but we currently only
739 * implement 16-bit decoding (no alias support).
741 pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
742 PCI_BRIDGE_CTL_PARITY |
743 PCI_BRIDGE_CTL_SERR |
744 PCI_BRIDGE_CTL_ISA |
745 PCI_BRIDGE_CTL_VGA |
746 PCI_BRIDGE_CTL_VGA_16BIT |
747 PCI_BRIDGE_CTL_MASTER_ABORT |
748 PCI_BRIDGE_CTL_BUS_RESET |
749 PCI_BRIDGE_CTL_FAST_BACK |
750 PCI_BRIDGE_CTL_DISCARD |
751 PCI_BRIDGE_CTL_SEC_DISCARD |
752 PCI_BRIDGE_CTL_DISCARD_SERR);
753 /* Below does not do anything as we never set this bit, put here for
754 * completeness. */
755 pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
756 PCI_BRIDGE_CTL_DISCARD_STATUS);
757 d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK;
758 d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK;
759 pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE,
760 PCI_PREF_RANGE_TYPE_MASK);
761 pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT,
762 PCI_PREF_RANGE_TYPE_MASK);
765 static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp)
767 uint8_t slot = PCI_SLOT(dev->devfn);
768 uint8_t func;
770 if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
771 dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
775 * multifunction bit is interpreted in two ways as follows.
776 * - all functions must set the bit to 1.
777 * Example: Intel X53
778 * - function 0 must set the bit, but the rest function (> 0)
779 * is allowed to leave the bit to 0.
780 * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
782 * So OS (at least Linux) checks the bit of only function 0,
783 * and doesn't see the bit of function > 0.
785 * The below check allows both interpretation.
787 if (PCI_FUNC(dev->devfn)) {
788 PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
789 if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
790 /* function 0 should set multifunction bit */
791 error_setg(errp, "PCI: single function device can't be populated "
792 "in function %x.%x", slot, PCI_FUNC(dev->devfn));
793 return;
795 return;
798 if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
799 return;
801 /* function 0 indicates single function, so function > 0 must be NULL */
802 for (func = 1; func < PCI_FUNC_MAX; ++func) {
803 if (bus->devices[PCI_DEVFN(slot, func)]) {
804 error_setg(errp, "PCI: %x.0 indicates single function, "
805 "but %x.%x is already populated.",
806 slot, slot, func);
807 return;
812 static void pci_config_alloc(PCIDevice *pci_dev)
814 int config_size = pci_config_size(pci_dev);
816 pci_dev->config = g_malloc0(config_size);
817 pci_dev->cmask = g_malloc0(config_size);
818 pci_dev->wmask = g_malloc0(config_size);
819 pci_dev->w1cmask = g_malloc0(config_size);
820 pci_dev->used = g_malloc0(config_size);
823 static void pci_config_free(PCIDevice *pci_dev)
825 g_free(pci_dev->config);
826 g_free(pci_dev->cmask);
827 g_free(pci_dev->wmask);
828 g_free(pci_dev->w1cmask);
829 g_free(pci_dev->used);
832 static void do_pci_unregister_device(PCIDevice *pci_dev)
834 pci_dev->bus->devices[pci_dev->devfn] = NULL;
835 pci_config_free(pci_dev);
837 address_space_destroy(&pci_dev->bus_master_as);
840 /* -1 for devfn means auto assign */
841 static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
842 const char *name, int devfn,
843 Error **errp)
845 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
846 PCIConfigReadFunc *config_read = pc->config_read;
847 PCIConfigWriteFunc *config_write = pc->config_write;
848 Error *local_err = NULL;
849 AddressSpace *dma_as;
851 if (devfn < 0) {
852 for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
853 devfn += PCI_FUNC_MAX) {
854 if (!bus->devices[devfn])
855 goto found;
857 error_setg(errp, "PCI: no slot/function available for %s, all in use",
858 name);
859 return NULL;
860 found: ;
861 } else if (bus->devices[devfn]) {
862 error_setg(errp, "PCI: slot %d function %d not available for %s,"
863 " in use by %s",
864 PCI_SLOT(devfn), PCI_FUNC(devfn), name,
865 bus->devices[devfn]->name);
866 return NULL;
869 pci_dev->bus = bus;
870 pci_dev->devfn = devfn;
871 dma_as = pci_device_iommu_address_space(pci_dev);
873 memory_region_init_alias(&pci_dev->bus_master_enable_region,
874 OBJECT(pci_dev), "bus master",
875 dma_as->root, 0, memory_region_size(dma_as->root));
876 memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
877 address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region,
878 name);
880 pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
881 pci_dev->irq_state = 0;
882 pci_config_alloc(pci_dev);
884 pci_config_set_vendor_id(pci_dev->config, pc->vendor_id);
885 pci_config_set_device_id(pci_dev->config, pc->device_id);
886 pci_config_set_revision(pci_dev->config, pc->revision);
887 pci_config_set_class(pci_dev->config, pc->class_id);
889 if (!pc->is_bridge) {
890 if (pc->subsystem_vendor_id || pc->subsystem_id) {
891 pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
892 pc->subsystem_vendor_id);
893 pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
894 pc->subsystem_id);
895 } else {
896 pci_set_default_subsystem_id(pci_dev);
898 } else {
899 /* subsystem_vendor_id/subsystem_id are only for header type 0 */
900 assert(!pc->subsystem_vendor_id);
901 assert(!pc->subsystem_id);
903 pci_init_cmask(pci_dev);
904 pci_init_wmask(pci_dev);
905 pci_init_w1cmask(pci_dev);
906 if (pc->is_bridge) {
907 pci_init_mask_bridge(pci_dev);
909 pci_init_multifunction(bus, pci_dev, &local_err);
910 if (local_err) {
911 error_propagate(errp, local_err);
912 do_pci_unregister_device(pci_dev);
913 return NULL;
916 if (!config_read)
917 config_read = pci_default_read_config;
918 if (!config_write)
919 config_write = pci_default_write_config;
920 pci_dev->config_read = config_read;
921 pci_dev->config_write = config_write;
922 bus->devices[devfn] = pci_dev;
923 pci_dev->version_id = 2; /* Current pci device vmstate version */
924 return pci_dev;
927 static void pci_unregister_io_regions(PCIDevice *pci_dev)
929 PCIIORegion *r;
930 int i;
932 for(i = 0; i < PCI_NUM_REGIONS; i++) {
933 r = &pci_dev->io_regions[i];
934 if (!r->size || r->addr == PCI_BAR_UNMAPPED)
935 continue;
936 memory_region_del_subregion(r->address_space, r->memory);
939 pci_unregister_vga(pci_dev);
942 static void pci_qdev_unrealize(DeviceState *dev, Error **errp)
944 PCIDevice *pci_dev = PCI_DEVICE(dev);
945 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
947 pci_unregister_io_regions(pci_dev);
948 pci_del_option_rom(pci_dev);
950 if (pc->exit) {
951 pc->exit(pci_dev);
954 do_pci_unregister_device(pci_dev);
957 void pci_register_bar(PCIDevice *pci_dev, int region_num,
958 uint8_t type, MemoryRegion *memory)
960 PCIIORegion *r;
961 uint32_t addr;
962 uint64_t wmask;
963 pcibus_t size = memory_region_size(memory);
965 g_assert(region_num >= 0);
966 g_assert(region_num < PCI_NUM_REGIONS);
967 if (size & (size-1)) {
968 fprintf(stderr, "ERROR: PCI region size must be pow2 "
969 "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
970 exit(1);
973 r = &pci_dev->io_regions[region_num];
974 r->addr = PCI_BAR_UNMAPPED;
975 r->size = size;
976 r->type = type;
977 r->memory = NULL;
979 wmask = ~(size - 1);
980 addr = pci_bar(pci_dev, region_num);
981 if (region_num == PCI_ROM_SLOT) {
982 /* ROM enable bit is writable */
983 wmask |= PCI_ROM_ADDRESS_ENABLE;
985 pci_set_long(pci_dev->config + addr, type);
986 if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
987 r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
988 pci_set_quad(pci_dev->wmask + addr, wmask);
989 pci_set_quad(pci_dev->cmask + addr, ~0ULL);
990 } else {
991 pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
992 pci_set_long(pci_dev->cmask + addr, 0xffffffff);
994 pci_dev->io_regions[region_num].memory = memory;
995 pci_dev->io_regions[region_num].address_space
996 = type & PCI_BASE_ADDRESS_SPACE_IO
997 ? pci_dev->bus->address_space_io
998 : pci_dev->bus->address_space_mem;
1001 static void pci_update_vga(PCIDevice *pci_dev)
1003 uint16_t cmd;
1005 if (!pci_dev->has_vga) {
1006 return;
1009 cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
1011 memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM],
1012 cmd & PCI_COMMAND_MEMORY);
1013 memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO],
1014 cmd & PCI_COMMAND_IO);
1015 memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI],
1016 cmd & PCI_COMMAND_IO);
1019 void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
1020 MemoryRegion *io_lo, MemoryRegion *io_hi)
1022 assert(!pci_dev->has_vga);
1024 assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE);
1025 pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem;
1026 memory_region_add_subregion_overlap(pci_dev->bus->address_space_mem,
1027 QEMU_PCI_VGA_MEM_BASE, mem, 1);
1029 assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE);
1030 pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo;
1031 memory_region_add_subregion_overlap(pci_dev->bus->address_space_io,
1032 QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1);
1034 assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE);
1035 pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi;
1036 memory_region_add_subregion_overlap(pci_dev->bus->address_space_io,
1037 QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1);
1038 pci_dev->has_vga = true;
1040 pci_update_vga(pci_dev);
1043 void pci_unregister_vga(PCIDevice *pci_dev)
1045 if (!pci_dev->has_vga) {
1046 return;
1049 memory_region_del_subregion(pci_dev->bus->address_space_mem,
1050 pci_dev->vga_regions[QEMU_PCI_VGA_MEM]);
1051 memory_region_del_subregion(pci_dev->bus->address_space_io,
1052 pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]);
1053 memory_region_del_subregion(pci_dev->bus->address_space_io,
1054 pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]);
1055 pci_dev->has_vga = false;
1058 pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
1060 return pci_dev->io_regions[region_num].addr;
1063 static pcibus_t pci_bar_address(PCIDevice *d,
1064 int reg, uint8_t type, pcibus_t size)
1066 pcibus_t new_addr, last_addr;
1067 int bar = pci_bar(d, reg);
1068 uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
1069 Object *machine = qdev_get_machine();
1070 ObjectClass *oc = object_get_class(machine);
1071 MachineClass *mc = MACHINE_CLASS(oc);
1072 bool allow_0_address = mc->pci_allow_0_address;
1074 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
1075 if (!(cmd & PCI_COMMAND_IO)) {
1076 return PCI_BAR_UNMAPPED;
1078 new_addr = pci_get_long(d->config + bar) & ~(size - 1);
1079 last_addr = new_addr + size - 1;
1080 /* Check if 32 bit BAR wraps around explicitly.
1081 * TODO: make priorities correct and remove this work around.
1083 if (last_addr <= new_addr || last_addr >= UINT32_MAX ||
1084 (!allow_0_address && new_addr == 0)) {
1085 return PCI_BAR_UNMAPPED;
1087 return new_addr;
1090 if (!(cmd & PCI_COMMAND_MEMORY)) {
1091 return PCI_BAR_UNMAPPED;
1093 if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
1094 new_addr = pci_get_quad(d->config + bar);
1095 } else {
1096 new_addr = pci_get_long(d->config + bar);
1098 /* the ROM slot has a specific enable bit */
1099 if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
1100 return PCI_BAR_UNMAPPED;
1102 new_addr &= ~(size - 1);
1103 last_addr = new_addr + size - 1;
1104 /* NOTE: we do not support wrapping */
1105 /* XXX: as we cannot support really dynamic
1106 mappings, we handle specific values as invalid
1107 mappings. */
1108 if (last_addr <= new_addr || last_addr == PCI_BAR_UNMAPPED ||
1109 (!allow_0_address && new_addr == 0)) {
1110 return PCI_BAR_UNMAPPED;
1113 /* Now pcibus_t is 64bit.
1114 * Check if 32 bit BAR wraps around explicitly.
1115 * Without this, PC ide doesn't work well.
1116 * TODO: remove this work around.
1118 if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
1119 return PCI_BAR_UNMAPPED;
1123 * OS is allowed to set BAR beyond its addressable
1124 * bits. For example, 32 bit OS can set 64bit bar
1125 * to >4G. Check it. TODO: we might need to support
1126 * it in the future for e.g. PAE.
1128 if (last_addr >= HWADDR_MAX) {
1129 return PCI_BAR_UNMAPPED;
1132 return new_addr;
1135 static void pci_update_mappings(PCIDevice *d)
1137 PCIIORegion *r;
1138 int i;
1139 pcibus_t new_addr;
1141 for(i = 0; i < PCI_NUM_REGIONS; i++) {
1142 r = &d->io_regions[i];
1144 /* this region isn't registered */
1145 if (!r->size)
1146 continue;
1148 new_addr = pci_bar_address(d, i, r->type, r->size);
1150 /* This bar isn't changed */
1151 if (new_addr == r->addr)
1152 continue;
1154 /* now do the real mapping */
1155 if (r->addr != PCI_BAR_UNMAPPED) {
1156 trace_pci_update_mappings_del(d, pci_bus_num(d->bus),
1157 PCI_FUNC(d->devfn),
1158 PCI_SLOT(d->devfn),
1159 i, r->addr, r->size);
1160 memory_region_del_subregion(r->address_space, r->memory);
1162 r->addr = new_addr;
1163 if (r->addr != PCI_BAR_UNMAPPED) {
1164 trace_pci_update_mappings_add(d, pci_bus_num(d->bus),
1165 PCI_FUNC(d->devfn),
1166 PCI_SLOT(d->devfn),
1167 i, r->addr, r->size);
1168 memory_region_add_subregion_overlap(r->address_space,
1169 r->addr, r->memory, 1);
1173 pci_update_vga(d);
1176 static inline int pci_irq_disabled(PCIDevice *d)
1178 return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
1181 /* Called after interrupt disabled field update in config space,
1182 * assert/deassert interrupts if necessary.
1183 * Gets original interrupt disable bit value (before update). */
1184 static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
1186 int i, disabled = pci_irq_disabled(d);
1187 if (disabled == was_irq_disabled)
1188 return;
1189 for (i = 0; i < PCI_NUM_PINS; ++i) {
1190 int state = pci_irq_state(d, i);
1191 pci_change_irq_level(d, i, disabled ? -state : state);
1195 uint32_t pci_default_read_config(PCIDevice *d,
1196 uint32_t address, int len)
1198 uint32_t val = 0;
1200 memcpy(&val, d->config + address, len);
1201 return le32_to_cpu(val);
1204 void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
1206 int i, was_irq_disabled = pci_irq_disabled(d);
1207 uint32_t val = val_in;
1209 for (i = 0; i < l; val >>= 8, ++i) {
1210 uint8_t wmask = d->wmask[addr + i];
1211 uint8_t w1cmask = d->w1cmask[addr + i];
1212 assert(!(wmask & w1cmask));
1213 d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
1214 d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
1216 if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
1217 ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
1218 ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
1219 range_covers_byte(addr, l, PCI_COMMAND))
1220 pci_update_mappings(d);
1222 if (range_covers_byte(addr, l, PCI_COMMAND)) {
1223 pci_update_irq_disabled(d, was_irq_disabled);
1224 memory_region_set_enabled(&d->bus_master_enable_region,
1225 pci_get_word(d->config + PCI_COMMAND)
1226 & PCI_COMMAND_MASTER);
1229 msi_write_config(d, addr, val_in, l);
1230 msix_write_config(d, addr, val_in, l);
1233 /***********************************************************/
1234 /* generic PCI irq support */
1236 /* 0 <= irq_num <= 3. level must be 0 or 1 */
1237 static void pci_irq_handler(void *opaque, int irq_num, int level)
1239 PCIDevice *pci_dev = opaque;
1240 int change;
1242 change = level - pci_irq_state(pci_dev, irq_num);
1243 if (!change)
1244 return;
1246 pci_set_irq_state(pci_dev, irq_num, level);
1247 pci_update_irq_status(pci_dev);
1248 if (pci_irq_disabled(pci_dev))
1249 return;
1250 pci_change_irq_level(pci_dev, irq_num, change);
1253 static inline int pci_intx(PCIDevice *pci_dev)
1255 return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
1258 qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
1260 int intx = pci_intx(pci_dev);
1262 return qemu_allocate_irq(pci_irq_handler, pci_dev, intx);
1265 void pci_set_irq(PCIDevice *pci_dev, int level)
1267 int intx = pci_intx(pci_dev);
1268 pci_irq_handler(pci_dev, intx, level);
1271 /* Special hooks used by device assignment */
1272 void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq)
1274 assert(pci_bus_is_root(bus));
1275 bus->route_intx_to_irq = route_intx_to_irq;
1278 PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin)
1280 PCIBus *bus;
1282 do {
1283 bus = dev->bus;
1284 pin = bus->map_irq(dev, pin);
1285 dev = bus->parent_dev;
1286 } while (dev);
1288 if (!bus->route_intx_to_irq) {
1289 error_report("PCI: Bug - unimplemented PCI INTx routing (%s)",
1290 object_get_typename(OBJECT(bus->qbus.parent)));
1291 return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 };
1294 return bus->route_intx_to_irq(bus->irq_opaque, pin);
1297 bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new)
1299 return old->mode != new->mode || old->irq != new->irq;
1302 void pci_bus_fire_intx_routing_notifier(PCIBus *bus)
1304 PCIDevice *dev;
1305 PCIBus *sec;
1306 int i;
1308 for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
1309 dev = bus->devices[i];
1310 if (dev && dev->intx_routing_notifier) {
1311 dev->intx_routing_notifier(dev);
1315 QLIST_FOREACH(sec, &bus->child, sibling) {
1316 pci_bus_fire_intx_routing_notifier(sec);
1320 void pci_device_set_intx_routing_notifier(PCIDevice *dev,
1321 PCIINTxRoutingNotifier notifier)
1323 dev->intx_routing_notifier = notifier;
1327 * PCI-to-PCI bridge specification
1328 * 9.1: Interrupt routing. Table 9-1
1330 * the PCI Express Base Specification, Revision 2.1
1331 * 2.2.8.1: INTx interrutp signaling - Rules
1332 * the Implementation Note
1333 * Table 2-20
1336 * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD
1337 * 0-origin unlike PCI interrupt pin register.
1339 int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
1341 return (pin + PCI_SLOT(pci_dev->devfn)) % PCI_NUM_PINS;
1344 /***********************************************************/
1345 /* monitor info on PCI */
1347 typedef struct {
1348 uint16_t class;
1349 const char *desc;
1350 const char *fw_name;
1351 uint16_t fw_ign_bits;
1352 } pci_class_desc;
1354 static const pci_class_desc pci_class_descriptions[] =
1356 { 0x0001, "VGA controller", "display"},
1357 { 0x0100, "SCSI controller", "scsi"},
1358 { 0x0101, "IDE controller", "ide"},
1359 { 0x0102, "Floppy controller", "fdc"},
1360 { 0x0103, "IPI controller", "ipi"},
1361 { 0x0104, "RAID controller", "raid"},
1362 { 0x0106, "SATA controller"},
1363 { 0x0107, "SAS controller"},
1364 { 0x0180, "Storage controller"},
1365 { 0x0200, "Ethernet controller", "ethernet"},
1366 { 0x0201, "Token Ring controller", "token-ring"},
1367 { 0x0202, "FDDI controller", "fddi"},
1368 { 0x0203, "ATM controller", "atm"},
1369 { 0x0280, "Network controller"},
1370 { 0x0300, "VGA controller", "display", 0x00ff},
1371 { 0x0301, "XGA controller"},
1372 { 0x0302, "3D controller"},
1373 { 0x0380, "Display controller"},
1374 { 0x0400, "Video controller", "video"},
1375 { 0x0401, "Audio controller", "sound"},
1376 { 0x0402, "Phone"},
1377 { 0x0403, "Audio controller", "sound"},
1378 { 0x0480, "Multimedia controller"},
1379 { 0x0500, "RAM controller", "memory"},
1380 { 0x0501, "Flash controller", "flash"},
1381 { 0x0580, "Memory controller"},
1382 { 0x0600, "Host bridge", "host"},
1383 { 0x0601, "ISA bridge", "isa"},
1384 { 0x0602, "EISA bridge", "eisa"},
1385 { 0x0603, "MC bridge", "mca"},
1386 { 0x0604, "PCI bridge", "pci-bridge"},
1387 { 0x0605, "PCMCIA bridge", "pcmcia"},
1388 { 0x0606, "NUBUS bridge", "nubus"},
1389 { 0x0607, "CARDBUS bridge", "cardbus"},
1390 { 0x0608, "RACEWAY bridge"},
1391 { 0x0680, "Bridge"},
1392 { 0x0700, "Serial port", "serial"},
1393 { 0x0701, "Parallel port", "parallel"},
1394 { 0x0800, "Interrupt controller", "interrupt-controller"},
1395 { 0x0801, "DMA controller", "dma-controller"},
1396 { 0x0802, "Timer", "timer"},
1397 { 0x0803, "RTC", "rtc"},
1398 { 0x0900, "Keyboard", "keyboard"},
1399 { 0x0901, "Pen", "pen"},
1400 { 0x0902, "Mouse", "mouse"},
1401 { 0x0A00, "Dock station", "dock", 0x00ff},
1402 { 0x0B00, "i386 cpu", "cpu", 0x00ff},
1403 { 0x0c00, "Fireware contorller", "fireware"},
1404 { 0x0c01, "Access bus controller", "access-bus"},
1405 { 0x0c02, "SSA controller", "ssa"},
1406 { 0x0c03, "USB controller", "usb"},
1407 { 0x0c04, "Fibre channel controller", "fibre-channel"},
1408 { 0x0c05, "SMBus"},
1409 { 0, NULL}
1412 static void pci_for_each_device_under_bus(PCIBus *bus,
1413 void (*fn)(PCIBus *b, PCIDevice *d,
1414 void *opaque),
1415 void *opaque)
1417 PCIDevice *d;
1418 int devfn;
1420 for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1421 d = bus->devices[devfn];
1422 if (d) {
1423 fn(bus, d, opaque);
1428 void pci_for_each_device(PCIBus *bus, int bus_num,
1429 void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
1430 void *opaque)
1432 bus = pci_find_bus_nr(bus, bus_num);
1434 if (bus) {
1435 pci_for_each_device_under_bus(bus, fn, opaque);
1439 static const pci_class_desc *get_class_desc(int class)
1441 const pci_class_desc *desc;
1443 desc = pci_class_descriptions;
1444 while (desc->desc && class != desc->class) {
1445 desc++;
1448 return desc;
1451 static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
1453 static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
1455 PciMemoryRegionList *head = NULL, *cur_item = NULL;
1456 int i;
1458 for (i = 0; i < PCI_NUM_REGIONS; i++) {
1459 const PCIIORegion *r = &dev->io_regions[i];
1460 PciMemoryRegionList *region;
1462 if (!r->size) {
1463 continue;
1466 region = g_malloc0(sizeof(*region));
1467 region->value = g_malloc0(sizeof(*region->value));
1469 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1470 region->value->type = g_strdup("io");
1471 } else {
1472 region->value->type = g_strdup("memory");
1473 region->value->has_prefetch = true;
1474 region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
1475 region->value->has_mem_type_64 = true;
1476 region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
1479 region->value->bar = i;
1480 region->value->address = r->addr;
1481 region->value->size = r->size;
1483 /* XXX: waiting for the qapi to support GSList */
1484 if (!cur_item) {
1485 head = cur_item = region;
1486 } else {
1487 cur_item->next = region;
1488 cur_item = region;
1492 return head;
1495 static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
1496 int bus_num)
1498 PciBridgeInfo *info;
1499 PciMemoryRange *range;
1501 info = g_new0(PciBridgeInfo, 1);
1503 info->bus = g_new0(PciBusInfo, 1);
1504 info->bus->number = dev->config[PCI_PRIMARY_BUS];
1505 info->bus->secondary = dev->config[PCI_SECONDARY_BUS];
1506 info->bus->subordinate = dev->config[PCI_SUBORDINATE_BUS];
1508 range = info->bus->io_range = g_new0(PciMemoryRange, 1);
1509 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
1510 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
1512 range = info->bus->memory_range = g_new0(PciMemoryRange, 1);
1513 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
1514 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
1516 range = info->bus->prefetchable_range = g_new0(PciMemoryRange, 1);
1517 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
1518 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
1520 if (dev->config[PCI_SECONDARY_BUS] != 0) {
1521 PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]);
1522 if (child_bus) {
1523 info->has_devices = true;
1524 info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]);
1528 return info;
1531 static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
1532 int bus_num)
1534 const pci_class_desc *desc;
1535 PciDeviceInfo *info;
1536 uint8_t type;
1537 int class;
1539 info = g_new0(PciDeviceInfo, 1);
1540 info->bus = bus_num;
1541 info->slot = PCI_SLOT(dev->devfn);
1542 info->function = PCI_FUNC(dev->devfn);
1544 info->class_info = g_new0(PciDeviceClass, 1);
1545 class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
1546 info->class_info->q_class = class;
1547 desc = get_class_desc(class);
1548 if (desc->desc) {
1549 info->class_info->has_desc = true;
1550 info->class_info->desc = g_strdup(desc->desc);
1553 info->id = g_new0(PciDeviceId, 1);
1554 info->id->vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
1555 info->id->device = pci_get_word(dev->config + PCI_DEVICE_ID);
1556 info->regions = qmp_query_pci_regions(dev);
1557 info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
1559 if (dev->config[PCI_INTERRUPT_PIN] != 0) {
1560 info->has_irq = true;
1561 info->irq = dev->config[PCI_INTERRUPT_LINE];
1564 type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
1565 if (type == PCI_HEADER_TYPE_BRIDGE) {
1566 info->has_pci_bridge = true;
1567 info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
1570 return info;
1573 static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
1575 PciDeviceInfoList *info, *head = NULL, *cur_item = NULL;
1576 PCIDevice *dev;
1577 int devfn;
1579 for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1580 dev = bus->devices[devfn];
1581 if (dev) {
1582 info = g_malloc0(sizeof(*info));
1583 info->value = qmp_query_pci_device(dev, bus, bus_num);
1585 /* XXX: waiting for the qapi to support GSList */
1586 if (!cur_item) {
1587 head = cur_item = info;
1588 } else {
1589 cur_item->next = info;
1590 cur_item = info;
1595 return head;
1598 static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
1600 PciInfo *info = NULL;
1602 bus = pci_find_bus_nr(bus, bus_num);
1603 if (bus) {
1604 info = g_malloc0(sizeof(*info));
1605 info->bus = bus_num;
1606 info->devices = qmp_query_pci_devices(bus, bus_num);
1609 return info;
1612 PciInfoList *qmp_query_pci(Error **errp)
1614 PciInfoList *info, *head = NULL, *cur_item = NULL;
1615 PCIHostState *host_bridge;
1617 QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
1618 info = g_malloc0(sizeof(*info));
1619 info->value = qmp_query_pci_bus(host_bridge->bus,
1620 pci_bus_num(host_bridge->bus));
1622 /* XXX: waiting for the qapi to support GSList */
1623 if (!cur_item) {
1624 head = cur_item = info;
1625 } else {
1626 cur_item->next = info;
1627 cur_item = info;
1631 return head;
1634 static const char * const pci_nic_models[] = {
1635 #if !defined(CONFIG_WIN32)
1636 "atheros_wlan",
1637 #endif
1638 "dp83816",
1639 "e100",
1640 "ne2k_pci",
1641 "i82551",
1642 "i82557a",
1643 "i82557b",
1644 "i82557c",
1645 "i82558b",
1646 "i82559c",
1647 "i82559er",
1648 "i82801",
1649 "rtl8139",
1650 "e1000",
1651 "pcnet",
1652 "tnetw1130",
1653 "virtio",
1654 NULL
1657 static const char * const pci_nic_names[] = {
1658 #if !defined(CONFIG_WIN32)
1659 "atheros_wlan",
1660 #endif
1661 "dp83816",
1662 "e100",
1663 "ne2k_pci",
1664 "i82551",
1665 "i82557a",
1666 "i82557b",
1667 "i82557c",
1668 "i82558b",
1669 "i82559c",
1670 "i82559er",
1671 "i82801",
1672 "rtl8139",
1673 "e1000",
1674 "pcnet",
1675 "tnetw1130",
1676 "virtio-net-pci",
1677 NULL
1680 /* Initialize a PCI NIC. */
1681 PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
1682 const char *default_model,
1683 const char *default_devaddr)
1685 const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1686 Error *err = NULL;
1687 PCIBus *bus;
1688 PCIDevice *pci_dev;
1689 DeviceState *dev;
1690 int devfn;
1691 int i;
1693 if (qemu_show_nic_models(nd->model, pci_nic_models)) {
1694 exit(0);
1697 i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1698 if (i < 0) {
1699 exit(1);
1702 bus = pci_get_bus_devfn(&devfn, rootbus, devaddr);
1703 if (!bus) {
1704 error_report("Invalid PCI device address %s for device %s",
1705 devaddr, pci_nic_names[i]);
1706 exit(1);
1709 pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1710 dev = &pci_dev->qdev;
1711 qdev_set_nic_properties(dev, nd);
1713 object_property_set_bool(OBJECT(dev), true, "realized", &err);
1714 if (err) {
1715 error_report_err(err);
1716 object_unparent(OBJECT(dev));
1717 exit(1);
1720 return pci_dev;
1723 PCIDevice *pci_vga_init(PCIBus *bus)
1725 switch (vga_interface_type) {
1726 case VGA_CIRRUS:
1727 return pci_create_simple(bus, -1, "cirrus-vga");
1728 case VGA_QXL:
1729 return pci_create_simple(bus, -1, "qxl-vga");
1730 case VGA_STD:
1731 return pci_create_simple(bus, -1, "VGA");
1732 case VGA_VMWARE:
1733 return pci_create_simple(bus, -1, "vmware-svga");
1734 case VGA_VIRTIO:
1735 return pci_create_simple(bus, -1, "virtio-vga");
1736 case VGA_NONE:
1737 default: /* Other non-PCI types. Checking for unsupported types is already
1738 done in vl.c. */
1739 return NULL;
1743 /* Whether a given bus number is in range of the secondary
1744 * bus of the given bridge device. */
1745 static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
1747 return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
1748 PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
1749 dev->config[PCI_SECONDARY_BUS] <= bus_num &&
1750 bus_num <= dev->config[PCI_SUBORDINATE_BUS];
1753 /* Whether a given bus number is in a range of a root bus */
1754 static bool pci_root_bus_in_range(PCIBus *bus, int bus_num)
1756 int i;
1758 for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
1759 PCIDevice *dev = bus->devices[i];
1761 if (dev && PCI_DEVICE_GET_CLASS(dev)->is_bridge) {
1762 if (pci_secondary_bus_in_range(dev, bus_num)) {
1763 return true;
1768 return false;
1771 static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
1773 PCIBus *sec;
1775 if (!bus) {
1776 return NULL;
1779 if (pci_bus_num(bus) == bus_num) {
1780 return bus;
1783 /* Consider all bus numbers in range for the host pci bridge. */
1784 if (!pci_bus_is_root(bus) &&
1785 !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
1786 return NULL;
1789 /* try child bus */
1790 for (; bus; bus = sec) {
1791 QLIST_FOREACH(sec, &bus->child, sibling) {
1792 if (pci_bus_num(sec) == bus_num) {
1793 return sec;
1795 /* PXB buses assumed to be children of bus 0 */
1796 if (pci_bus_is_root(sec)) {
1797 if (pci_root_bus_in_range(sec, bus_num)) {
1798 break;
1800 } else {
1801 if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
1802 break;
1808 return NULL;
1811 void pci_for_each_bus_depth_first(PCIBus *bus,
1812 void *(*begin)(PCIBus *bus, void *parent_state),
1813 void (*end)(PCIBus *bus, void *state),
1814 void *parent_state)
1816 PCIBus *sec;
1817 void *state;
1819 if (!bus) {
1820 return;
1823 if (begin) {
1824 state = begin(bus, parent_state);
1825 } else {
1826 state = parent_state;
1829 QLIST_FOREACH(sec, &bus->child, sibling) {
1830 pci_for_each_bus_depth_first(sec, begin, end, state);
1833 if (end) {
1834 end(bus, state);
1839 PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
1841 bus = pci_find_bus_nr(bus, bus_num);
1843 if (!bus)
1844 return NULL;
1846 return bus->devices[devfn];
1849 static void pci_qdev_realize(DeviceState *qdev, Error **errp)
1851 PCIDevice *pci_dev = (PCIDevice *)qdev;
1852 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
1853 Error *local_err = NULL;
1854 PCIBus *bus;
1855 bool is_default_rom;
1857 /* initialize cap_present for pci_is_express() and pci_config_size() */
1858 if (pc->is_express) {
1859 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1862 bus = PCI_BUS(qdev_get_parent_bus(qdev));
1863 pci_dev = do_pci_register_device(pci_dev, bus,
1864 object_get_typename(OBJECT(qdev)),
1865 pci_dev->devfn, errp);
1866 if (pci_dev == NULL)
1867 return;
1869 if (pc->realize) {
1870 pc->realize(pci_dev, &local_err);
1871 if (local_err) {
1872 error_propagate(errp, local_err);
1873 do_pci_unregister_device(pci_dev);
1874 return;
1878 /* rom loading */
1879 is_default_rom = false;
1880 if (pci_dev->romfile == NULL && pc->romfile != NULL) {
1881 pci_dev->romfile = g_strdup(pc->romfile);
1882 is_default_rom = true;
1885 pci_add_option_rom(pci_dev, is_default_rom, &local_err);
1886 if (local_err) {
1887 error_propagate(errp, local_err);
1888 pci_qdev_unrealize(DEVICE(pci_dev), NULL);
1889 return;
1893 static void pci_default_realize(PCIDevice *dev, Error **errp)
1895 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1897 if (pc->init) {
1898 if (pc->init(dev) < 0) {
1899 error_setg(errp, "Device initialization failed");
1900 return;
1905 PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
1906 const char *name)
1908 DeviceState *dev;
1910 dev = qdev_create(&bus->qbus, name);
1911 qdev_prop_set_int32(dev, "addr", devfn);
1912 qdev_prop_set_bit(dev, "multifunction", multifunction);
1913 return PCI_DEVICE(dev);
1916 PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
1917 bool multifunction,
1918 const char *name)
1920 PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name);
1921 qdev_init_nofail(&dev->qdev);
1922 return dev;
1925 PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1927 return pci_create_multifunction(bus, devfn, false, name);
1930 PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1932 return pci_create_simple_multifunction(bus, devfn, false, name);
1935 static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
1937 int offset = PCI_CONFIG_HEADER_SIZE;
1938 int i;
1939 for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
1940 if (pdev->used[i])
1941 offset = i + 1;
1942 else if (i - offset + 1 == size)
1943 return offset;
1945 return 0;
1948 static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1949 uint8_t *prev_p)
1951 uint8_t next, prev;
1953 if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1954 return 0;
1956 for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1957 prev = next + PCI_CAP_LIST_NEXT)
1958 if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1959 break;
1961 if (prev_p)
1962 *prev_p = prev;
1963 return next;
1966 static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
1968 uint8_t next, prev, found = 0;
1970 if (!(pdev->used[offset])) {
1971 return 0;
1974 assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST);
1976 for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1977 prev = next + PCI_CAP_LIST_NEXT) {
1978 if (next <= offset && next > found) {
1979 found = next;
1982 return found;
1985 /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
1986 This is needed for an option rom which is used for more than one device. */
1987 static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)
1989 uint16_t vendor_id;
1990 uint16_t device_id;
1991 uint16_t rom_vendor_id;
1992 uint16_t rom_device_id;
1993 uint16_t rom_magic;
1994 uint16_t pcir_offset;
1995 uint8_t checksum;
1997 /* Words in rom data are little endian (like in PCI configuration),
1998 so they can be read / written with pci_get_word / pci_set_word. */
2000 /* Only a valid rom will be patched. */
2001 rom_magic = pci_get_word(ptr);
2002 if (rom_magic != 0xaa55) {
2003 PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
2004 return;
2006 pcir_offset = pci_get_word(ptr + 0x18);
2007 if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
2008 PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
2009 return;
2012 vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
2013 device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
2014 rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
2015 rom_device_id = pci_get_word(ptr + pcir_offset + 6);
2017 PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
2018 vendor_id, device_id, rom_vendor_id, rom_device_id);
2020 checksum = ptr[6];
2022 if (vendor_id != rom_vendor_id) {
2023 /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
2024 checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
2025 checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
2026 PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2027 ptr[6] = checksum;
2028 pci_set_word(ptr + pcir_offset + 4, vendor_id);
2031 if (device_id != rom_device_id) {
2032 /* Patch device id and checksum (at offset 6 for etherboot roms). */
2033 checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
2034 checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
2035 PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
2036 ptr[6] = checksum;
2037 pci_set_word(ptr + pcir_offset + 6, device_id);
2041 /* Add an option rom for the device */
2042 static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
2043 Error **errp)
2045 int size;
2046 char *path;
2047 void *ptr;
2048 char name[32];
2049 const VMStateDescription *vmsd;
2051 if (!pdev->romfile)
2052 return;
2053 if (strlen(pdev->romfile) == 0)
2054 return;
2056 if (!pdev->rom_bar) {
2058 * Load rom via fw_cfg instead of creating a rom bar,
2059 * for 0.11 compatibility.
2061 int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
2064 * Hot-plugged devices can't use the option ROM
2065 * if the rom bar is disabled.
2067 if (DEVICE(pdev)->hotplugged) {
2068 error_setg(errp, "Hot-plugged device without ROM bar"
2069 " can't have an option ROM");
2070 return;
2073 if (class == 0x0300) {
2074 rom_add_vga(pdev->romfile);
2075 } else {
2076 rom_add_option(pdev->romfile, -1);
2078 return;
2081 path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
2082 if (path == NULL) {
2083 path = g_strdup(pdev->romfile);
2086 size = get_image_size(path);
2087 if (size < 0) {
2088 error_setg(errp, "failed to find romfile \"%s\"", pdev->romfile);
2089 g_free(path);
2090 return;
2091 } else if (size == 0) {
2092 error_setg(errp, "romfile \"%s\" is empty", pdev->romfile);
2093 g_free(path);
2094 return;
2096 if (size & (size - 1)) {
2097 size = 1 << qemu_fls(size);
2100 vmsd = qdev_get_vmsd(DEVICE(pdev));
2102 if (vmsd) {
2103 snprintf(name, sizeof(name), "%s.rom", vmsd->name);
2104 } else {
2105 snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev)));
2107 pdev->has_rom = true;
2108 memory_region_init_ram(&pdev->rom, OBJECT(pdev), name, size, &error_abort);
2109 vmstate_register_ram(&pdev->rom, &pdev->qdev);
2110 ptr = memory_region_get_ram_ptr(&pdev->rom);
2111 load_image(path, ptr);
2112 g_free(path);
2114 if (is_default_rom) {
2115 /* Only the default rom images will be patched (if needed). */
2116 pci_patch_ids(pdev, ptr, size);
2119 pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
2122 static void pci_del_option_rom(PCIDevice *pdev)
2124 if (!pdev->has_rom)
2125 return;
2127 vmstate_unregister_ram(&pdev->rom, &pdev->qdev);
2128 pdev->has_rom = false;
2132 * if offset = 0,
2133 * Find and reserve space and add capability to the linked list
2134 * in pci config space
2136 int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
2137 uint8_t offset, uint8_t size)
2139 int ret;
2140 Error *local_err = NULL;
2142 ret = pci_add_capability2(pdev, cap_id, offset, size, &local_err);
2143 if (local_err) {
2144 assert(ret < 0);
2145 error_report_err(local_err);
2146 } else {
2147 /* success implies a positive offset in config space */
2148 assert(ret > 0);
2150 return ret;
2153 int pci_add_capability2(PCIDevice *pdev, uint8_t cap_id,
2154 uint8_t offset, uint8_t size,
2155 Error **errp)
2157 uint8_t *config;
2158 int i, overlapping_cap;
2160 if (!offset) {
2161 offset = pci_find_space(pdev, size);
2162 if (!offset) {
2163 error_setg(errp, "out of PCI config space");
2164 return -ENOSPC;
2166 } else {
2167 /* Verify that capabilities don't overlap. Note: device assignment
2168 * depends on this check to verify that the device is not broken.
2169 * Should never trigger for emulated devices, but it's helpful
2170 * for debugging these. */
2171 for (i = offset; i < offset + size; i++) {
2172 overlapping_cap = pci_find_capability_at_offset(pdev, i);
2173 if (overlapping_cap) {
2174 error_setg(errp, "%s:%02x:%02x.%x "
2175 "Attempt to add PCI capability %x at offset "
2176 "%x overlaps existing capability %x at offset %x",
2177 pci_root_bus_path(pdev), pci_bus_num(pdev->bus),
2178 PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
2179 cap_id, offset, overlapping_cap, i);
2180 return -EINVAL;
2185 config = pdev->config + offset;
2186 config[PCI_CAP_LIST_ID] = cap_id;
2187 config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
2188 pdev->config[PCI_CAPABILITY_LIST] = offset;
2189 pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
2190 memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));
2191 /* Make capability read-only by default */
2192 memset(pdev->wmask + offset, 0, size);
2193 /* Check capability by default */
2194 memset(pdev->cmask + offset, 0xFF, size);
2195 return offset;
2198 /* Unlink capability from the pci config space. */
2199 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
2201 uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
2202 if (!offset)
2203 return;
2204 pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
2205 /* Make capability writable again */
2206 memset(pdev->wmask + offset, 0xff, size);
2207 memset(pdev->w1cmask + offset, 0, size);
2208 /* Clear cmask as device-specific registers can't be checked */
2209 memset(pdev->cmask + offset, 0, size);
2210 memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4));
2212 if (!pdev->config[PCI_CAPABILITY_LIST])
2213 pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
2216 uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
2218 return pci_find_capability_list(pdev, cap_id, NULL);
2221 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
2223 PCIDevice *d = (PCIDevice *)dev;
2224 const pci_class_desc *desc;
2225 char ctxt[64];
2226 PCIIORegion *r;
2227 int i, class;
2229 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2230 desc = pci_class_descriptions;
2231 while (desc->desc && class != desc->class)
2232 desc++;
2233 if (desc->desc) {
2234 snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
2235 } else {
2236 snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
2239 monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
2240 "pci id %04x:%04x (sub %04x:%04x)\n",
2241 indent, "", ctxt, pci_bus_num(d->bus),
2242 PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
2243 pci_get_word(d->config + PCI_VENDOR_ID),
2244 pci_get_word(d->config + PCI_DEVICE_ID),
2245 pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
2246 pci_get_word(d->config + PCI_SUBSYSTEM_ID));
2247 for (i = 0; i < PCI_NUM_REGIONS; i++) {
2248 r = &d->io_regions[i];
2249 if (!r->size)
2250 continue;
2251 monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
2252 " [0x%"FMT_PCIBUS"]\n",
2253 indent, "",
2254 i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
2255 r->addr, r->addr + r->size - 1);
2259 static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
2261 PCIDevice *d = (PCIDevice *)dev;
2262 const char *name = NULL;
2263 const pci_class_desc *desc = pci_class_descriptions;
2264 int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
2266 while (desc->desc &&
2267 (class & ~desc->fw_ign_bits) !=
2268 (desc->class & ~desc->fw_ign_bits)) {
2269 desc++;
2272 if (desc->desc) {
2273 name = desc->fw_name;
2276 if (name) {
2277 pstrcpy(buf, len, name);
2278 } else {
2279 snprintf(buf, len, "pci%04x,%04x",
2280 pci_get_word(d->config + PCI_VENDOR_ID),
2281 pci_get_word(d->config + PCI_DEVICE_ID));
2284 return buf;
2287 static char *pcibus_get_fw_dev_path(DeviceState *dev)
2289 PCIDevice *d = (PCIDevice *)dev;
2290 char path[50], name[33];
2291 int off;
2293 off = snprintf(path, sizeof(path), "%s@%x",
2294 pci_dev_fw_name(dev, name, sizeof name),
2295 PCI_SLOT(d->devfn));
2296 if (PCI_FUNC(d->devfn))
2297 snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
2298 return g_strdup(path);
2301 static char *pcibus_get_dev_path(DeviceState *dev)
2303 PCIDevice *d = container_of(dev, PCIDevice, qdev);
2304 PCIDevice *t;
2305 int slot_depth;
2306 /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
2307 * 00 is added here to make this format compatible with
2308 * domain:Bus:Slot.Func for systems without nested PCI bridges.
2309 * Slot.Function list specifies the slot and function numbers for all
2310 * devices on the path from root to the specific device. */
2311 const char *root_bus_path;
2312 int root_bus_len;
2313 char slot[] = ":SS.F";
2314 int slot_len = sizeof slot - 1 /* For '\0' */;
2315 int path_len;
2316 char *path, *p;
2317 int s;
2319 root_bus_path = pci_root_bus_path(d);
2320 root_bus_len = strlen(root_bus_path);
2322 /* Calculate # of slots on path between device and root. */;
2323 slot_depth = 0;
2324 for (t = d; t; t = t->bus->parent_dev) {
2325 ++slot_depth;
2328 path_len = root_bus_len + slot_len * slot_depth;
2330 /* Allocate memory, fill in the terminating null byte. */
2331 path = g_malloc(path_len + 1 /* For '\0' */);
2332 path[path_len] = '\0';
2334 memcpy(path, root_bus_path, root_bus_len);
2336 /* Fill in slot numbers. We walk up from device to root, so need to print
2337 * them in the reverse order, last to first. */
2338 p = path + path_len;
2339 for (t = d; t; t = t->bus->parent_dev) {
2340 p -= slot_len;
2341 s = snprintf(slot, sizeof slot, ":%02x.%x",
2342 PCI_SLOT(t->devfn), PCI_FUNC(t->devfn));
2343 assert(s == slot_len);
2344 memcpy(p, slot, slot_len);
2347 return path;
2350 static int pci_qdev_find_recursive(PCIBus *bus,
2351 const char *id, PCIDevice **pdev)
2353 DeviceState *qdev = qdev_find_recursive(&bus->qbus, id);
2354 if (!qdev) {
2355 return -ENODEV;
2358 /* roughly check if given qdev is pci device */
2359 if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) {
2360 *pdev = PCI_DEVICE(qdev);
2361 return 0;
2363 return -EINVAL;
2366 int pci_qdev_find_device(const char *id, PCIDevice **pdev)
2368 PCIHostState *host_bridge;
2369 int rc = -ENODEV;
2371 QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
2372 int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev);
2373 if (!tmp) {
2374 rc = 0;
2375 break;
2377 if (tmp != -ENODEV) {
2378 rc = tmp;
2382 return rc;
2385 MemoryRegion *pci_address_space(PCIDevice *dev)
2387 return dev->bus->address_space_mem;
2390 MemoryRegion *pci_address_space_io(PCIDevice *dev)
2392 return dev->bus->address_space_io;
2395 static void pci_device_class_init(ObjectClass *klass, void *data)
2397 DeviceClass *k = DEVICE_CLASS(klass);
2398 PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
2400 k->realize = pci_qdev_realize;
2401 k->unrealize = pci_qdev_unrealize;
2402 k->bus_type = TYPE_PCI_BUS;
2403 k->props = pci_props;
2404 pc->realize = pci_default_realize;
2407 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
2409 PCIBus *bus = PCI_BUS(dev->bus);
2411 if (bus->iommu_fn) {
2412 return bus->iommu_fn(bus, bus->iommu_opaque, dev->devfn);
2415 if (bus->parent_dev) {
2416 /** We are ignoring the bus master DMA bit of the bridge
2417 * as it would complicate things such as VFIO for no good reason */
2418 return pci_device_iommu_address_space(bus->parent_dev);
2421 return &address_space_memory;
2424 void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
2426 bus->iommu_fn = fn;
2427 bus->iommu_opaque = opaque;
2430 static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque)
2432 Range *range = opaque;
2433 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
2434 uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND);
2435 int i;
2437 if (!(cmd & PCI_COMMAND_MEMORY)) {
2438 return;
2441 if (pc->is_bridge) {
2442 pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
2443 pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
2445 base = MAX(base, 0x1ULL << 32);
2447 if (limit >= base) {
2448 Range pref_range;
2449 pref_range.begin = base;
2450 pref_range.end = limit + 1;
2451 range_extend(range, &pref_range);
2454 for (i = 0; i < PCI_NUM_REGIONS; ++i) {
2455 PCIIORegion *r = &dev->io_regions[i];
2456 Range region_range;
2458 if (!r->size ||
2459 (r->type & PCI_BASE_ADDRESS_SPACE_IO) ||
2460 !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
2461 continue;
2463 region_range.begin = pci_bar_address(dev, i, r->type, r->size);
2464 region_range.end = region_range.begin + r->size;
2466 if (region_range.begin == PCI_BAR_UNMAPPED) {
2467 continue;
2470 region_range.begin = MAX(region_range.begin, 0x1ULL << 32);
2472 if (region_range.end - 1 >= region_range.begin) {
2473 range_extend(range, &region_range);
2478 void pci_bus_get_w64_range(PCIBus *bus, Range *range)
2480 range->begin = range->end = 0;
2481 pci_for_each_device_under_bus(bus, pci_dev_get_w64, range);
2484 static const TypeInfo pci_device_type_info = {
2485 .name = TYPE_PCI_DEVICE,
2486 .parent = TYPE_DEVICE,
2487 .instance_size = sizeof(PCIDevice),
2488 .abstract = true,
2489 .class_size = sizeof(PCIDeviceClass),
2490 .class_init = pci_device_class_init,
2493 static void pci_register_types(void)
2495 type_register_static(&pci_bus_info);
2496 type_register_static(&pcie_bus_info);
2497 type_register_static(&pci_device_type_info);
2500 type_init(pci_register_types)