Update for 0.12.4 release
[qemu/umeq.git] / hw / pci.c
blob8f30f73b7e096cf97f83627e43d78f559db19f26
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.h"
25 #include "pci.h"
26 #include "monitor.h"
27 #include "net.h"
28 #include "sysemu.h"
29 #include "loader.h"
31 //#define DEBUG_PCI
32 #ifdef DEBUG_PCI
33 # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
34 #else
35 # define PCI_DPRINTF(format, ...) do { } while (0)
36 #endif
38 struct PCIBus {
39 BusState qbus;
40 int devfn_min;
41 pci_set_irq_fn set_irq;
42 pci_map_irq_fn map_irq;
43 pci_hotplug_fn hotplug;
44 uint32_t config_reg; /* XXX: suppress */
45 void *irq_opaque;
46 PCIDevice *devices[256];
47 PCIDevice *parent_dev;
49 QLIST_HEAD(, PCIBus) child; /* this will be replaced by qdev later */
50 QLIST_ENTRY(PCIBus) sibling;/* this will be replaced by qdev later */
52 /* The bus IRQ state is the logical OR of the connected devices.
53 Keep a count of the number of devices with raised IRQs. */
54 int nirq;
55 int *irq_count;
58 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
60 static struct BusInfo pci_bus_info = {
61 .name = "PCI",
62 .size = sizeof(PCIBus),
63 .print_dev = pcibus_dev_print,
64 .props = (Property[]) {
65 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
66 DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
67 DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1),
68 DEFINE_PROP_END_OF_LIST()
72 static void pci_update_mappings(PCIDevice *d);
73 static void pci_set_irq(void *opaque, int irq_num, int level);
74 static int pci_add_option_rom(PCIDevice *pdev);
76 target_phys_addr_t pci_mem_base;
77 static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
78 static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
80 struct PCIHostBus {
81 int domain;
82 struct PCIBus *bus;
83 QLIST_ENTRY(PCIHostBus) next;
85 static QLIST_HEAD(, PCIHostBus) host_buses;
87 static const VMStateDescription vmstate_pcibus = {
88 .name = "PCIBUS",
89 .version_id = 1,
90 .minimum_version_id = 1,
91 .minimum_version_id_old = 1,
92 .fields = (VMStateField []) {
93 VMSTATE_INT32_EQUAL(nirq, PCIBus),
94 VMSTATE_VARRAY_INT32(irq_count, PCIBus, nirq, 0, vmstate_info_int32, int32_t),
95 VMSTATE_END_OF_LIST()
99 static int pci_bar(PCIDevice *d, int reg)
101 uint8_t type;
103 if (reg != PCI_ROM_SLOT)
104 return PCI_BASE_ADDRESS_0 + reg * 4;
106 type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
107 return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
110 static inline int pci_irq_state(PCIDevice *d, int irq_num)
112 return (d->irq_state >> irq_num) & 0x1;
115 static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
117 d->irq_state &= ~(0x1 << irq_num);
118 d->irq_state |= level << irq_num;
121 static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
123 PCIBus *bus;
124 for (;;) {
125 bus = pci_dev->bus;
126 irq_num = bus->map_irq(pci_dev, irq_num);
127 if (bus->set_irq)
128 break;
129 pci_dev = bus->parent_dev;
131 bus->irq_count[irq_num] += change;
132 bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
135 /* Update interrupt status bit in config space on interrupt
136 * state change. */
137 static void pci_update_irq_status(PCIDevice *dev)
139 if (dev->irq_state) {
140 dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
141 } else {
142 dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
146 static void pci_device_reset(PCIDevice *dev)
148 int r;
150 dev->irq_state = 0;
151 pci_update_irq_status(dev);
152 dev->config[PCI_COMMAND] &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
153 PCI_COMMAND_MASTER);
154 dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
155 dev->config[PCI_INTERRUPT_LINE] = 0x0;
156 for (r = 0; r < PCI_NUM_REGIONS; ++r) {
157 if (!dev->io_regions[r].size) {
158 continue;
160 pci_set_long(dev->config + pci_bar(dev, r), dev->io_regions[r].type);
162 pci_update_mappings(dev);
165 static void pci_bus_reset(void *opaque)
167 PCIBus *bus = opaque;
168 int i;
170 for (i = 0; i < bus->nirq; i++) {
171 bus->irq_count[i] = 0;
173 for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
174 if (bus->devices[i]) {
175 pci_device_reset(bus->devices[i]);
180 static void pci_host_bus_register(int domain, PCIBus *bus)
182 struct PCIHostBus *host;
183 host = qemu_mallocz(sizeof(*host));
184 host->domain = domain;
185 host->bus = bus;
186 QLIST_INSERT_HEAD(&host_buses, host, next);
189 PCIBus *pci_find_root_bus(int domain)
191 struct PCIHostBus *host;
193 QLIST_FOREACH(host, &host_buses, next) {
194 if (host->domain == domain) {
195 return host->bus;
199 return NULL;
202 void pci_bus_new_inplace(PCIBus *bus, DeviceState *parent,
203 const char *name, int devfn_min)
205 qbus_create_inplace(&bus->qbus, &pci_bus_info, parent, name);
206 bus->devfn_min = devfn_min;
208 /* host bridge */
209 QLIST_INIT(&bus->child);
210 pci_host_bus_register(0, bus); /* for now only pci domain 0 is supported */
212 vmstate_register(-1, &vmstate_pcibus, bus);
213 qemu_register_reset(pci_bus_reset, bus);
216 PCIBus *pci_bus_new(DeviceState *parent, const char *name, int devfn_min)
218 PCIBus *bus;
220 bus = qemu_mallocz(sizeof(*bus));
221 bus->qbus.qdev_allocated = 1;
222 pci_bus_new_inplace(bus, parent, name, devfn_min);
223 return bus;
226 void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
227 void *irq_opaque, int nirq)
229 bus->set_irq = set_irq;
230 bus->map_irq = map_irq;
231 bus->irq_opaque = irq_opaque;
232 bus->nirq = nirq;
233 bus->irq_count = qemu_mallocz(nirq * sizeof(bus->irq_count[0]));
236 void pci_bus_hotplug(PCIBus *bus, pci_hotplug_fn hotplug)
238 bus->qbus.allow_hotplug = 1;
239 bus->hotplug = hotplug;
242 PCIBus *pci_register_bus(DeviceState *parent, const char *name,
243 pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
244 void *irq_opaque, int devfn_min, int nirq)
246 PCIBus *bus;
248 bus = pci_bus_new(parent, name, devfn_min);
249 pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
250 return bus;
253 static void pci_register_secondary_bus(PCIBus *parent,
254 PCIBus *bus,
255 PCIDevice *dev,
256 pci_map_irq_fn map_irq,
257 const char *name)
259 qbus_create_inplace(&bus->qbus, &pci_bus_info, &dev->qdev, name);
260 bus->map_irq = map_irq;
261 bus->parent_dev = dev;
263 QLIST_INIT(&bus->child);
264 QLIST_INSERT_HEAD(&parent->child, bus, sibling);
267 static void pci_unregister_secondary_bus(PCIBus *bus)
269 assert(QLIST_EMPTY(&bus->child));
270 QLIST_REMOVE(bus, sibling);
273 int pci_bus_num(PCIBus *s)
275 if (!s->parent_dev)
276 return 0; /* pci host bridge */
277 return s->parent_dev->config[PCI_SECONDARY_BUS];
280 static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
282 PCIDevice *s = container_of(pv, PCIDevice, config);
283 uint8_t *config;
284 int i;
286 assert(size == pci_config_size(s));
287 config = qemu_malloc(size);
289 qemu_get_buffer(f, config, size);
290 for (i = 0; i < size; ++i) {
291 if ((config[i] ^ s->config[i]) & s->cmask[i] & ~s->wmask[i]) {
292 qemu_free(config);
293 return -EINVAL;
296 memcpy(s->config, config, size);
298 pci_update_mappings(s);
300 qemu_free(config);
301 return 0;
304 /* just put buffer */
305 static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
307 const uint8_t **v = pv;
308 assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
309 qemu_put_buffer(f, *v, size);
312 static VMStateInfo vmstate_info_pci_config = {
313 .name = "pci config",
314 .get = get_pci_config_device,
315 .put = put_pci_config_device,
318 static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
320 PCIDevice *s = container_of(pv, PCIDevice, config);
321 uint32_t irq_state[PCI_NUM_PINS];
322 int i;
323 for (i = 0; i < PCI_NUM_PINS; ++i) {
324 irq_state[i] = qemu_get_be32(f);
325 if (irq_state[i] != 0x1 && irq_state[i] != 0) {
326 fprintf(stderr, "irq state %d: must be 0 or 1.\n",
327 irq_state[i]);
328 return -EINVAL;
332 for (i = 0; i < PCI_NUM_PINS; ++i) {
333 pci_set_irq_state(s, i, irq_state[i]);
336 return 0;
339 static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
341 int i;
342 PCIDevice *s = container_of(pv, PCIDevice, config);
344 for (i = 0; i < PCI_NUM_PINS; ++i) {
345 qemu_put_be32(f, pci_irq_state(s, i));
349 static VMStateInfo vmstate_info_pci_irq_state = {
350 .name = "pci irq state",
351 .get = get_pci_irq_state,
352 .put = put_pci_irq_state,
355 const VMStateDescription vmstate_pci_device = {
356 .name = "PCIDevice",
357 .version_id = 2,
358 .minimum_version_id = 1,
359 .minimum_version_id_old = 1,
360 .fields = (VMStateField []) {
361 VMSTATE_INT32_LE(version_id, PCIDevice),
362 VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
363 vmstate_info_pci_config,
364 PCI_CONFIG_SPACE_SIZE),
365 VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
366 vmstate_info_pci_irq_state,
367 PCI_NUM_PINS * sizeof(int32_t)),
368 VMSTATE_END_OF_LIST()
372 const VMStateDescription vmstate_pcie_device = {
373 .name = "PCIDevice",
374 .version_id = 2,
375 .minimum_version_id = 1,
376 .minimum_version_id_old = 1,
377 .fields = (VMStateField []) {
378 VMSTATE_INT32_LE(version_id, PCIDevice),
379 VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
380 vmstate_info_pci_config,
381 PCIE_CONFIG_SPACE_SIZE),
382 VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
383 vmstate_info_pci_irq_state,
384 PCI_NUM_PINS * sizeof(int32_t)),
385 VMSTATE_END_OF_LIST()
389 static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
391 return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
394 void pci_device_save(PCIDevice *s, QEMUFile *f)
396 /* Clear interrupt status bit: it is implicit
397 * in irq_state which we are saving.
398 * This makes us compatible with old devices
399 * which never set or clear this bit. */
400 s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
401 vmstate_save_state(f, pci_get_vmstate(s), s);
402 /* Restore the interrupt status bit. */
403 pci_update_irq_status(s);
406 int pci_device_load(PCIDevice *s, QEMUFile *f)
408 int ret;
409 ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
410 /* Restore the interrupt status bit. */
411 pci_update_irq_status(s);
412 return ret;
415 static int pci_set_default_subsystem_id(PCIDevice *pci_dev)
417 uint16_t *id;
419 id = (void*)(&pci_dev->config[PCI_SUBVENDOR_ID]);
420 id[0] = cpu_to_le16(pci_default_sub_vendor_id);
421 id[1] = cpu_to_le16(pci_default_sub_device_id);
422 return 0;
426 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
428 static int pci_parse_devaddr(const char *addr, int *domp, int *busp, unsigned *slotp)
430 const char *p;
431 char *e;
432 unsigned long val;
433 unsigned long dom = 0, bus = 0;
434 unsigned slot = 0;
436 p = addr;
437 val = strtoul(p, &e, 16);
438 if (e == p)
439 return -1;
440 if (*e == ':') {
441 bus = val;
442 p = e + 1;
443 val = strtoul(p, &e, 16);
444 if (e == p)
445 return -1;
446 if (*e == ':') {
447 dom = bus;
448 bus = val;
449 p = e + 1;
450 val = strtoul(p, &e, 16);
451 if (e == p)
452 return -1;
456 if (dom > 0xffff || bus > 0xff || val > 0x1f)
457 return -1;
459 slot = val;
461 if (*e)
462 return -1;
464 /* Note: QEMU doesn't implement domains other than 0 */
465 if (!pci_find_bus(pci_find_root_bus(dom), bus))
466 return -1;
468 *domp = dom;
469 *busp = bus;
470 *slotp = slot;
471 return 0;
474 int pci_read_devaddr(Monitor *mon, const char *addr, int *domp, int *busp,
475 unsigned *slotp)
477 /* strip legacy tag */
478 if (!strncmp(addr, "pci_addr=", 9)) {
479 addr += 9;
481 if (pci_parse_devaddr(addr, domp, busp, slotp)) {
482 monitor_printf(mon, "Invalid pci address\n");
483 return -1;
485 return 0;
488 PCIBus *pci_get_bus_devfn(int *devfnp, const char *devaddr)
490 int dom, bus;
491 unsigned slot;
493 if (!devaddr) {
494 *devfnp = -1;
495 return pci_find_bus(pci_find_root_bus(0), 0);
498 if (pci_parse_devaddr(devaddr, &dom, &bus, &slot) < 0) {
499 return NULL;
502 *devfnp = slot << 3;
503 return pci_find_bus(pci_find_root_bus(0), bus);
506 static void pci_init_cmask(PCIDevice *dev)
508 pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
509 pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
510 dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
511 dev->cmask[PCI_REVISION_ID] = 0xff;
512 dev->cmask[PCI_CLASS_PROG] = 0xff;
513 pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
514 dev->cmask[PCI_HEADER_TYPE] = 0xff;
515 dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
518 static void pci_init_wmask(PCIDevice *dev)
520 int config_size = pci_config_size(dev);
522 dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
523 dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
524 pci_set_word(dev->wmask + PCI_COMMAND,
525 PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
527 memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
528 config_size - PCI_CONFIG_HEADER_SIZE);
531 static void pci_init_wmask_bridge(PCIDevice *d)
533 /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
534 PCI_SEC_LETENCY_TIMER */
535 memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
537 /* base and limit */
538 d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
539 d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
540 pci_set_word(d->wmask + PCI_MEMORY_BASE,
541 PCI_MEMORY_RANGE_MASK & 0xffff);
542 pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
543 PCI_MEMORY_RANGE_MASK & 0xffff);
544 pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
545 PCI_PREF_RANGE_MASK & 0xffff);
546 pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
547 PCI_PREF_RANGE_MASK & 0xffff);
549 /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
550 memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
552 pci_set_word(d->wmask + PCI_BRIDGE_CONTROL, 0xffff);
555 static void pci_config_alloc(PCIDevice *pci_dev)
557 int config_size = pci_config_size(pci_dev);
559 pci_dev->config = qemu_mallocz(config_size);
560 pci_dev->cmask = qemu_mallocz(config_size);
561 pci_dev->wmask = qemu_mallocz(config_size);
562 pci_dev->used = qemu_mallocz(config_size);
565 static void pci_config_free(PCIDevice *pci_dev)
567 qemu_free(pci_dev->config);
568 qemu_free(pci_dev->cmask);
569 qemu_free(pci_dev->wmask);
570 qemu_free(pci_dev->used);
573 /* -1 for devfn means auto assign */
574 static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
575 const char *name, int devfn,
576 PCIConfigReadFunc *config_read,
577 PCIConfigWriteFunc *config_write,
578 uint8_t header_type)
580 if (devfn < 0) {
581 for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
582 devfn += 8) {
583 if (!bus->devices[devfn])
584 goto found;
586 qemu_error("PCI: no devfn available for %s, all in use\n", name);
587 return NULL;
588 found: ;
589 } else if (bus->devices[devfn]) {
590 qemu_error("PCI: devfn %d not available for %s, in use by %s\n", devfn,
591 name, bus->devices[devfn]->name);
592 return NULL;
594 pci_dev->bus = bus;
595 pci_dev->devfn = devfn;
596 pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
597 pci_dev->irq_state = 0;
598 pci_config_alloc(pci_dev);
600 header_type &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
601 if (header_type == PCI_HEADER_TYPE_NORMAL) {
602 pci_set_default_subsystem_id(pci_dev);
604 pci_init_cmask(pci_dev);
605 pci_init_wmask(pci_dev);
606 if (header_type == PCI_HEADER_TYPE_BRIDGE) {
607 pci_init_wmask_bridge(pci_dev);
610 if (!config_read)
611 config_read = pci_default_read_config;
612 if (!config_write)
613 config_write = pci_default_write_config;
614 pci_dev->config_read = config_read;
615 pci_dev->config_write = config_write;
616 bus->devices[devfn] = pci_dev;
617 pci_dev->irq = qemu_allocate_irqs(pci_set_irq, pci_dev, PCI_NUM_PINS);
618 pci_dev->version_id = 2; /* Current pci device vmstate version */
619 return pci_dev;
622 PCIDevice *pci_register_device(PCIBus *bus, const char *name,
623 int instance_size, int devfn,
624 PCIConfigReadFunc *config_read,
625 PCIConfigWriteFunc *config_write)
627 PCIDevice *pci_dev;
629 pci_dev = qemu_mallocz(instance_size);
630 pci_dev = do_pci_register_device(pci_dev, bus, name, devfn,
631 config_read, config_write,
632 PCI_HEADER_TYPE_NORMAL);
633 if (pci_dev == NULL) {
634 hw_error("PCI: can't register device\n");
636 return pci_dev;
638 static target_phys_addr_t pci_to_cpu_addr(target_phys_addr_t addr)
640 return addr + pci_mem_base;
643 static void pci_unregister_io_regions(PCIDevice *pci_dev)
645 PCIIORegion *r;
646 int i;
648 for(i = 0; i < PCI_NUM_REGIONS; i++) {
649 r = &pci_dev->io_regions[i];
650 if (!r->size || r->addr == PCI_BAR_UNMAPPED)
651 continue;
652 if (r->type == PCI_BASE_ADDRESS_SPACE_IO) {
653 isa_unassign_ioport(r->addr, r->filtered_size);
654 } else {
655 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
656 r->filtered_size,
657 IO_MEM_UNASSIGNED);
662 static int pci_unregister_device(DeviceState *dev)
664 PCIDevice *pci_dev = DO_UPCAST(PCIDevice, qdev, dev);
665 PCIDeviceInfo *info = DO_UPCAST(PCIDeviceInfo, qdev, dev->info);
666 int ret = 0;
668 if (info->exit)
669 ret = info->exit(pci_dev);
670 if (ret)
671 return ret;
673 pci_unregister_io_regions(pci_dev);
675 qemu_free_irqs(pci_dev->irq);
676 pci_dev->bus->devices[pci_dev->devfn] = NULL;
677 pci_config_free(pci_dev);
678 return 0;
681 void pci_register_bar(PCIDevice *pci_dev, int region_num,
682 pcibus_t size, int type,
683 PCIMapIORegionFunc *map_func)
685 PCIIORegion *r;
686 uint32_t addr;
687 pcibus_t wmask;
689 if ((unsigned int)region_num >= PCI_NUM_REGIONS)
690 return;
692 if (size & (size-1)) {
693 fprintf(stderr, "ERROR: PCI region size must be pow2 "
694 "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
695 exit(1);
698 r = &pci_dev->io_regions[region_num];
699 r->addr = PCI_BAR_UNMAPPED;
700 r->size = size;
701 r->filtered_size = size;
702 r->type = type;
703 r->map_func = map_func;
705 wmask = ~(size - 1);
706 addr = pci_bar(pci_dev, region_num);
707 if (region_num == PCI_ROM_SLOT) {
708 /* ROM enable bit is writeable */
709 wmask |= PCI_ROM_ADDRESS_ENABLE;
711 pci_set_long(pci_dev->config + addr, type);
712 if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
713 r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
714 pci_set_quad(pci_dev->wmask + addr, wmask);
715 pci_set_quad(pci_dev->cmask + addr, ~0ULL);
716 } else {
717 pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
718 pci_set_long(pci_dev->cmask + addr, 0xffffffff);
722 static uint32_t pci_config_get_io_base(PCIDevice *d,
723 uint32_t base, uint32_t base_upper16)
725 uint32_t val;
727 val = ((uint32_t)d->config[base] & PCI_IO_RANGE_MASK) << 8;
728 if (d->config[base] & PCI_IO_RANGE_TYPE_32) {
729 val |= (uint32_t)pci_get_word(d->config + base_upper16) << 16;
731 return val;
734 static pcibus_t pci_config_get_memory_base(PCIDevice *d, uint32_t base)
736 return ((pcibus_t)pci_get_word(d->config + base) & PCI_MEMORY_RANGE_MASK)
737 << 16;
740 static pcibus_t pci_config_get_pref_base(PCIDevice *d,
741 uint32_t base, uint32_t upper)
743 pcibus_t tmp;
744 pcibus_t val;
746 tmp = (pcibus_t)pci_get_word(d->config + base);
747 val = (tmp & PCI_PREF_RANGE_MASK) << 16;
748 if (tmp & PCI_PREF_RANGE_TYPE_64) {
749 val |= (pcibus_t)pci_get_long(d->config + upper) << 32;
751 return val;
754 static pcibus_t pci_bridge_get_base(PCIDevice *bridge, uint8_t type)
756 pcibus_t base;
757 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
758 base = pci_config_get_io_base(bridge,
759 PCI_IO_BASE, PCI_IO_BASE_UPPER16);
760 } else {
761 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
762 base = pci_config_get_pref_base(
763 bridge, PCI_PREF_MEMORY_BASE, PCI_PREF_BASE_UPPER32);
764 } else {
765 base = pci_config_get_memory_base(bridge, PCI_MEMORY_BASE);
769 return base;
772 static pcibus_t pci_bridge_get_limit(PCIDevice *bridge, uint8_t type)
774 pcibus_t limit;
775 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
776 limit = pci_config_get_io_base(bridge,
777 PCI_IO_LIMIT, PCI_IO_LIMIT_UPPER16);
778 limit |= 0xfff; /* PCI bridge spec 3.2.5.6. */
779 } else {
780 if (type & PCI_BASE_ADDRESS_MEM_PREFETCH) {
781 limit = pci_config_get_pref_base(
782 bridge, PCI_PREF_MEMORY_LIMIT, PCI_PREF_LIMIT_UPPER32);
783 } else {
784 limit = pci_config_get_memory_base(bridge, PCI_MEMORY_LIMIT);
786 limit |= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */
788 return limit;
791 static void pci_bridge_filter(PCIDevice *d, pcibus_t *addr, pcibus_t *size,
792 uint8_t type)
794 pcibus_t base = *addr;
795 pcibus_t limit = *addr + *size - 1;
796 PCIDevice *br;
798 for (br = d->bus->parent_dev; br; br = br->bus->parent_dev) {
799 uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
801 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
802 if (!(cmd & PCI_COMMAND_IO)) {
803 goto no_map;
805 } else {
806 if (!(cmd & PCI_COMMAND_MEMORY)) {
807 goto no_map;
811 base = MAX(base, pci_bridge_get_base(br, type));
812 limit = MIN(limit, pci_bridge_get_limit(br, type));
815 if (base > limit) {
816 goto no_map;
818 *addr = base;
819 *size = limit - base + 1;
820 return;
821 no_map:
822 *addr = PCI_BAR_UNMAPPED;
823 *size = 0;
826 static pcibus_t pci_bar_address(PCIDevice *d,
827 int reg, uint8_t type, pcibus_t size)
829 pcibus_t new_addr, last_addr;
830 int bar = pci_bar(d, reg);
831 uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
833 if (type & PCI_BASE_ADDRESS_SPACE_IO) {
834 if (!(cmd & PCI_COMMAND_IO)) {
835 return PCI_BAR_UNMAPPED;
837 new_addr = pci_get_long(d->config + bar) & ~(size - 1);
838 last_addr = new_addr + size - 1;
839 /* NOTE: we have only 64K ioports on PC */
840 if (last_addr <= new_addr || new_addr == 0 || last_addr > UINT16_MAX) {
841 return PCI_BAR_UNMAPPED;
843 return new_addr;
846 if (!(cmd & PCI_COMMAND_MEMORY)) {
847 return PCI_BAR_UNMAPPED;
849 if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
850 new_addr = pci_get_quad(d->config + bar);
851 } else {
852 new_addr = pci_get_long(d->config + bar);
854 /* the ROM slot has a specific enable bit */
855 if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
856 return PCI_BAR_UNMAPPED;
858 new_addr &= ~(size - 1);
859 last_addr = new_addr + size - 1;
860 /* NOTE: we do not support wrapping */
861 /* XXX: as we cannot support really dynamic
862 mappings, we handle specific values as invalid
863 mappings. */
864 if (last_addr <= new_addr || new_addr == 0 ||
865 last_addr == PCI_BAR_UNMAPPED) {
866 return PCI_BAR_UNMAPPED;
869 /* Now pcibus_t is 64bit.
870 * Check if 32 bit BAR wraps around explicitly.
871 * Without this, PC ide doesn't work well.
872 * TODO: remove this work around.
874 if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
875 return PCI_BAR_UNMAPPED;
879 * OS is allowed to set BAR beyond its addressable
880 * bits. For example, 32 bit OS can set 64bit bar
881 * to >4G. Check it. TODO: we might need to support
882 * it in the future for e.g. PAE.
884 if (last_addr >= TARGET_PHYS_ADDR_MAX) {
885 return PCI_BAR_UNMAPPED;
888 return new_addr;
891 static void pci_update_mappings(PCIDevice *d)
893 PCIIORegion *r;
894 int i;
895 pcibus_t new_addr, filtered_size;
897 for(i = 0; i < PCI_NUM_REGIONS; i++) {
898 r = &d->io_regions[i];
900 /* this region isn't registered */
901 if (!r->size)
902 continue;
904 new_addr = pci_bar_address(d, i, r->type, r->size);
906 /* bridge filtering */
907 filtered_size = r->size;
908 if (new_addr != PCI_BAR_UNMAPPED) {
909 pci_bridge_filter(d, &new_addr, &filtered_size, r->type);
912 /* This bar isn't changed */
913 if (new_addr == r->addr && filtered_size == r->filtered_size)
914 continue;
916 /* now do the real mapping */
917 if (r->addr != PCI_BAR_UNMAPPED) {
918 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
919 int class;
920 /* NOTE: specific hack for IDE in PC case:
921 only one byte must be mapped. */
922 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
923 if (class == 0x0101 && r->size == 4) {
924 isa_unassign_ioport(r->addr + 2, 1);
925 } else {
926 isa_unassign_ioport(r->addr, r->filtered_size);
928 } else {
929 cpu_register_physical_memory(pci_to_cpu_addr(r->addr),
930 r->filtered_size,
931 IO_MEM_UNASSIGNED);
932 qemu_unregister_coalesced_mmio(r->addr, r->filtered_size);
935 r->addr = new_addr;
936 r->filtered_size = filtered_size;
937 if (r->addr != PCI_BAR_UNMAPPED) {
939 * TODO: currently almost all the map funcions assumes
940 * filtered_size == size and addr & ~(size - 1) == addr.
941 * However with bridge filtering, they aren't always true.
942 * Teach them such cases, such that filtered_size < size and
943 * addr & (size - 1) != 0.
945 r->map_func(d, i, r->addr, r->filtered_size, r->type);
950 uint32_t pci_default_read_config(PCIDevice *d,
951 uint32_t address, int len)
953 uint32_t val = 0;
954 assert(len == 1 || len == 2 || len == 4);
955 len = MIN(len, pci_config_size(d) - address);
956 memcpy(&val, d->config + address, len);
957 return le32_to_cpu(val);
960 void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
962 int i;
963 uint32_t config_size = pci_config_size(d);
965 for (i = 0; i < l && addr + i < config_size; val >>= 8, ++i) {
966 uint8_t wmask = d->wmask[addr + i];
967 d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
969 if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
970 ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
971 ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
972 range_covers_byte(addr, l, PCI_COMMAND))
973 pci_update_mappings(d);
976 /***********************************************************/
977 /* generic PCI irq support */
979 /* 0 <= irq_num <= 3. level must be 0 or 1 */
980 static void pci_set_irq(void *opaque, int irq_num, int level)
982 PCIDevice *pci_dev = opaque;
983 int change;
985 change = level - pci_irq_state(pci_dev, irq_num);
986 if (!change)
987 return;
989 pci_set_irq_state(pci_dev, irq_num, level);
990 pci_update_irq_status(pci_dev);
991 pci_change_irq_level(pci_dev, irq_num, change);
994 /***********************************************************/
995 /* monitor info on PCI */
997 typedef struct {
998 uint16_t class;
999 const char *desc;
1000 } pci_class_desc;
1002 static const pci_class_desc pci_class_descriptions[] =
1004 { 0x0100, "SCSI controller"},
1005 { 0x0101, "IDE controller"},
1006 { 0x0102, "Floppy controller"},
1007 { 0x0103, "IPI controller"},
1008 { 0x0104, "RAID controller"},
1009 { 0x0106, "SATA controller"},
1010 { 0x0107, "SAS controller"},
1011 { 0x0180, "Storage controller"},
1012 { 0x0200, "Ethernet controller"},
1013 { 0x0201, "Token Ring controller"},
1014 { 0x0202, "FDDI controller"},
1015 { 0x0203, "ATM controller"},
1016 { 0x0280, "Network controller"},
1017 { 0x0300, "VGA controller"},
1018 { 0x0301, "XGA controller"},
1019 { 0x0302, "3D controller"},
1020 { 0x0380, "Display controller"},
1021 { 0x0400, "Video controller"},
1022 { 0x0401, "Audio controller"},
1023 { 0x0402, "Phone"},
1024 { 0x0480, "Multimedia controller"},
1025 { 0x0500, "RAM controller"},
1026 { 0x0501, "Flash controller"},
1027 { 0x0580, "Memory controller"},
1028 { 0x0600, "Host bridge"},
1029 { 0x0601, "ISA bridge"},
1030 { 0x0602, "EISA bridge"},
1031 { 0x0603, "MC bridge"},
1032 { 0x0604, "PCI bridge"},
1033 { 0x0605, "PCMCIA bridge"},
1034 { 0x0606, "NUBUS bridge"},
1035 { 0x0607, "CARDBUS bridge"},
1036 { 0x0608, "RACEWAY bridge"},
1037 { 0x0680, "Bridge"},
1038 { 0x0c03, "USB controller"},
1039 { 0, NULL}
1042 static void pci_info_device(PCIBus *bus, PCIDevice *d)
1044 Monitor *mon = cur_mon;
1045 int i, class;
1046 PCIIORegion *r;
1047 const pci_class_desc *desc;
1049 monitor_printf(mon, " Bus %2d, device %3d, function %d:\n",
1050 pci_bus_num(d->bus),
1051 PCI_SLOT(d->devfn), PCI_FUNC(d->devfn));
1052 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1053 monitor_printf(mon, " ");
1054 desc = pci_class_descriptions;
1055 while (desc->desc && class != desc->class)
1056 desc++;
1057 if (desc->desc) {
1058 monitor_printf(mon, "%s", desc->desc);
1059 } else {
1060 monitor_printf(mon, "Class %04x", class);
1062 monitor_printf(mon, ": PCI device %04x:%04x\n",
1063 pci_get_word(d->config + PCI_VENDOR_ID),
1064 pci_get_word(d->config + PCI_DEVICE_ID));
1066 if (d->config[PCI_INTERRUPT_PIN] != 0) {
1067 monitor_printf(mon, " IRQ %d.\n",
1068 d->config[PCI_INTERRUPT_LINE]);
1070 if (class == 0x0604) {
1071 uint64_t base;
1072 uint64_t limit;
1074 monitor_printf(mon, " BUS %d.\n", d->config[0x19]);
1075 monitor_printf(mon, " secondary bus %d.\n",
1076 d->config[PCI_SECONDARY_BUS]);
1077 monitor_printf(mon, " subordinate bus %d.\n",
1078 d->config[PCI_SUBORDINATE_BUS]);
1080 base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_IO);
1081 limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_IO);
1082 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
1083 base, limit);
1085 base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY);
1086 limit= pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY);
1087 monitor_printf(mon,
1088 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
1089 base, limit);
1091 base = pci_bridge_get_base(d, PCI_BASE_ADDRESS_SPACE_MEMORY |
1092 PCI_BASE_ADDRESS_MEM_PREFETCH);
1093 limit = pci_bridge_get_limit(d, PCI_BASE_ADDRESS_SPACE_MEMORY |
1094 PCI_BASE_ADDRESS_MEM_PREFETCH);
1095 monitor_printf(mon, " prefetchable memory range "
1096 "[0x%08"PRIx64", 0x%08"PRIx64"]\n", base, limit);
1098 for(i = 0;i < PCI_NUM_REGIONS; i++) {
1099 r = &d->io_regions[i];
1100 if (r->size != 0) {
1101 monitor_printf(mon, " BAR%d: ", i);
1102 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
1103 monitor_printf(mon, "I/O at 0x%04"FMT_PCIBUS
1104 " [0x%04"FMT_PCIBUS"].\n",
1105 r->addr, r->addr + r->size - 1);
1106 } else {
1107 const char *type = r->type & PCI_BASE_ADDRESS_MEM_TYPE_64 ?
1108 "64 bit" : "32 bit";
1109 const char *prefetch =
1110 r->type & PCI_BASE_ADDRESS_MEM_PREFETCH ?
1111 " prefetchable" : "";
1113 monitor_printf(mon, "%s%s memory at 0x%08"FMT_PCIBUS
1114 " [0x%08"FMT_PCIBUS"].\n",
1115 type, prefetch,
1116 r->addr, r->addr + r->size - 1);
1120 monitor_printf(mon, " id \"%s\"\n", d->qdev.id ? d->qdev.id : "");
1121 if (class == 0x0604 && d->config[0x19] != 0) {
1122 pci_for_each_device(bus, d->config[0x19], pci_info_device);
1126 static void pci_for_each_device_under_bus(PCIBus *bus,
1127 void (*fn)(PCIBus *b, PCIDevice *d))
1129 PCIDevice *d;
1130 int devfn;
1132 for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
1133 d = bus->devices[devfn];
1134 if (d)
1135 fn(bus, d);
1139 void pci_for_each_device(PCIBus *bus, int bus_num,
1140 void (*fn)(PCIBus *b, PCIDevice *d))
1142 bus = pci_find_bus(bus, bus_num);
1144 if (bus) {
1145 pci_for_each_device_under_bus(bus, fn);
1149 void pci_info(Monitor *mon)
1151 struct PCIHostBus *host;
1152 QLIST_FOREACH(host, &host_buses, next) {
1153 pci_for_each_device(host->bus, 0, pci_info_device);
1157 static const char * const pci_nic_models[] = {
1158 "ne2k_pci",
1159 "i82551",
1160 "i82557b",
1161 "i82559er",
1162 "rtl8139",
1163 "e1000",
1164 "pcnet",
1165 "virtio",
1166 NULL
1169 static const char * const pci_nic_names[] = {
1170 "ne2k_pci",
1171 "i82551",
1172 "i82557b",
1173 "i82559er",
1174 "rtl8139",
1175 "e1000",
1176 "pcnet",
1177 "virtio-net-pci",
1178 NULL
1181 /* Initialize a PCI NIC. */
1182 /* FIXME callers should check for failure, but don't */
1183 PCIDevice *pci_nic_init(NICInfo *nd, const char *default_model,
1184 const char *default_devaddr)
1186 const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
1187 PCIBus *bus;
1188 int devfn;
1189 PCIDevice *pci_dev;
1190 DeviceState *dev;
1191 int i;
1193 i = qemu_find_nic_model(nd, pci_nic_models, default_model);
1194 if (i < 0)
1195 return NULL;
1197 bus = pci_get_bus_devfn(&devfn, devaddr);
1198 if (!bus) {
1199 qemu_error("Invalid PCI device address %s for device %s\n",
1200 devaddr, pci_nic_names[i]);
1201 return NULL;
1204 pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
1205 dev = &pci_dev->qdev;
1206 if (nd->name)
1207 dev->id = qemu_strdup(nd->name);
1208 qdev_set_nic_properties(dev, nd);
1209 if (qdev_init(dev) < 0)
1210 return NULL;
1211 return pci_dev;
1214 PCIDevice *pci_nic_init_nofail(NICInfo *nd, const char *default_model,
1215 const char *default_devaddr)
1217 PCIDevice *res;
1219 if (qemu_show_nic_models(nd->model, pci_nic_models))
1220 exit(0);
1222 res = pci_nic_init(nd, default_model, default_devaddr);
1223 if (!res)
1224 exit(1);
1225 return res;
1228 typedef struct {
1229 PCIDevice dev;
1230 PCIBus bus;
1231 uint32_t vid;
1232 uint32_t did;
1233 } PCIBridge;
1236 static void pci_bridge_update_mappings_fn(PCIBus *b, PCIDevice *d)
1238 pci_update_mappings(d);
1241 static void pci_bridge_update_mappings(PCIBus *b)
1243 PCIBus *child;
1245 pci_for_each_device_under_bus(b, pci_bridge_update_mappings_fn);
1247 QLIST_FOREACH(child, &b->child, sibling) {
1248 pci_bridge_update_mappings(child);
1252 static void pci_bridge_write_config(PCIDevice *d,
1253 uint32_t address, uint32_t val, int len)
1255 pci_default_write_config(d, address, val, len);
1257 if (/* io base/limit */
1258 ranges_overlap(address, len, PCI_IO_BASE, 2) ||
1260 /* memory base/limit, prefetchable base/limit and
1261 io base/limit upper 16 */
1262 ranges_overlap(address, len, PCI_MEMORY_BASE, 20)) {
1263 pci_bridge_update_mappings(d->bus);
1267 PCIBus *pci_find_bus(PCIBus *bus, int bus_num)
1269 PCIBus *sec;
1271 if (!bus)
1272 return NULL;
1274 if (pci_bus_num(bus) == bus_num) {
1275 return bus;
1278 /* try child bus */
1279 QLIST_FOREACH(sec, &bus->child, sibling) {
1281 if (!bus->parent_dev /* pci host bridge */
1282 || (pci_bus_num(sec) <= bus_num &&
1283 bus->parent_dev->config[PCI_SUBORDINATE_BUS])) {
1284 return pci_find_bus(sec, bus_num);
1288 return NULL;
1291 PCIDevice *pci_find_device(PCIBus *bus, int bus_num, int slot, int function)
1293 bus = pci_find_bus(bus, bus_num);
1295 if (!bus)
1296 return NULL;
1298 return bus->devices[PCI_DEVFN(slot, function)];
1301 static int pci_bridge_initfn(PCIDevice *dev)
1303 PCIBridge *s = DO_UPCAST(PCIBridge, dev, dev);
1305 pci_config_set_vendor_id(s->dev.config, s->vid);
1306 pci_config_set_device_id(s->dev.config, s->did);
1308 pci_set_word(dev->config + PCI_STATUS,
1309 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1310 pci_config_set_class(dev->config, PCI_CLASS_BRIDGE_PCI);
1311 dev->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_BRIDGE;
1312 pci_set_word(dev->config + PCI_SEC_STATUS,
1313 PCI_STATUS_66MHZ | PCI_STATUS_FAST_BACK);
1314 return 0;
1317 static int pci_bridge_exitfn(PCIDevice *pci_dev)
1319 PCIBridge *s = DO_UPCAST(PCIBridge, dev, pci_dev);
1320 PCIBus *bus = &s->bus;
1321 pci_unregister_secondary_bus(bus);
1322 return 0;
1325 PCIBus *pci_bridge_init(PCIBus *bus, int devfn, uint16_t vid, uint16_t did,
1326 pci_map_irq_fn map_irq, const char *name)
1328 PCIDevice *dev;
1329 PCIBridge *s;
1331 dev = pci_create(bus, devfn, "pci-bridge");
1332 qdev_prop_set_uint32(&dev->qdev, "vendorid", vid);
1333 qdev_prop_set_uint32(&dev->qdev, "deviceid", did);
1334 qdev_init_nofail(&dev->qdev);
1336 s = DO_UPCAST(PCIBridge, dev, dev);
1337 pci_register_secondary_bus(bus, &s->bus, &s->dev, map_irq, name);
1338 return &s->bus;
1341 PCIDevice *pci_bridge_get_device(PCIBus *bus)
1343 return bus->parent_dev;
1346 static int pci_qdev_init(DeviceState *qdev, DeviceInfo *base)
1348 PCIDevice *pci_dev = (PCIDevice *)qdev;
1349 PCIDeviceInfo *info = container_of(base, PCIDeviceInfo, qdev);
1350 PCIBus *bus;
1351 int devfn, rc;
1353 /* initialize cap_present for pci_is_express() and pci_config_size() */
1354 if (info->is_express) {
1355 pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
1358 bus = FROM_QBUS(PCIBus, qdev_get_parent_bus(qdev));
1359 devfn = pci_dev->devfn;
1360 pci_dev = do_pci_register_device(pci_dev, bus, base->name, devfn,
1361 info->config_read, info->config_write,
1362 info->header_type);
1363 if (pci_dev == NULL)
1364 return -1;
1365 rc = info->init(pci_dev);
1366 if (rc != 0)
1367 return rc;
1369 /* rom loading */
1370 if (pci_dev->romfile == NULL && info->romfile != NULL)
1371 pci_dev->romfile = qemu_strdup(info->romfile);
1372 pci_add_option_rom(pci_dev);
1374 if (qdev->hotplugged)
1375 bus->hotplug(pci_dev, 1);
1376 return 0;
1379 static int pci_unplug_device(DeviceState *qdev)
1381 PCIDevice *dev = DO_UPCAST(PCIDevice, qdev, qdev);
1383 dev->bus->hotplug(dev, 0);
1384 return 0;
1387 void pci_qdev_register(PCIDeviceInfo *info)
1389 info->qdev.init = pci_qdev_init;
1390 info->qdev.unplug = pci_unplug_device;
1391 info->qdev.exit = pci_unregister_device;
1392 info->qdev.bus_info = &pci_bus_info;
1393 qdev_register(&info->qdev);
1396 void pci_qdev_register_many(PCIDeviceInfo *info)
1398 while (info->qdev.name) {
1399 pci_qdev_register(info);
1400 info++;
1404 PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
1406 DeviceState *dev;
1408 dev = qdev_create(&bus->qbus, name);
1409 qdev_prop_set_uint32(dev, "addr", devfn);
1410 return DO_UPCAST(PCIDevice, qdev, dev);
1413 PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
1415 PCIDevice *dev = pci_create(bus, devfn, name);
1416 qdev_init_nofail(&dev->qdev);
1417 return dev;
1420 static int pci_find_space(PCIDevice *pdev, uint8_t size)
1422 int config_size = pci_config_size(pdev);
1423 int offset = PCI_CONFIG_HEADER_SIZE;
1424 int i;
1425 for (i = PCI_CONFIG_HEADER_SIZE; i < config_size; ++i)
1426 if (pdev->used[i])
1427 offset = i + 1;
1428 else if (i - offset + 1 == size)
1429 return offset;
1430 return 0;
1433 static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
1434 uint8_t *prev_p)
1436 uint8_t next, prev;
1438 if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
1439 return 0;
1441 for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
1442 prev = next + PCI_CAP_LIST_NEXT)
1443 if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
1444 break;
1446 if (prev_p)
1447 *prev_p = prev;
1448 return next;
1451 static void pci_map_option_rom(PCIDevice *pdev, int region_num, pcibus_t addr, pcibus_t size, int type)
1453 cpu_register_physical_memory(addr, size, pdev->rom_offset);
1456 /* Add an option rom for the device */
1457 static int pci_add_option_rom(PCIDevice *pdev)
1459 int size;
1460 char *path;
1461 void *ptr;
1463 if (!pdev->romfile)
1464 return 0;
1465 if (strlen(pdev->romfile) == 0)
1466 return 0;
1468 if (!pdev->rom_bar) {
1470 * Load rom via fw_cfg instead of creating a rom bar,
1471 * for 0.11 compatibility.
1473 int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
1474 if (class == 0x0300) {
1475 rom_add_vga(pdev->romfile);
1476 } else {
1477 rom_add_option(pdev->romfile);
1479 return 0;
1482 path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
1483 if (path == NULL) {
1484 path = qemu_strdup(pdev->romfile);
1487 size = get_image_size(path);
1488 if (size < 0) {
1489 qemu_error("%s: failed to find romfile \"%s\"\n", __FUNCTION__,
1490 pdev->romfile);
1491 return -1;
1493 if (size & (size - 1)) {
1494 size = 1 << qemu_fls(size);
1497 pdev->rom_offset = qemu_ram_alloc(size);
1499 ptr = qemu_get_ram_ptr(pdev->rom_offset);
1500 load_image(path, ptr);
1501 qemu_free(path);
1503 pci_register_bar(pdev, PCI_ROM_SLOT, size,
1504 0, pci_map_option_rom);
1506 return 0;
1509 /* Reserve space and add capability to the linked list in pci config space */
1510 int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1512 uint8_t offset = pci_find_space(pdev, size);
1513 uint8_t *config = pdev->config + offset;
1514 if (!offset)
1515 return -ENOSPC;
1516 config[PCI_CAP_LIST_ID] = cap_id;
1517 config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
1518 pdev->config[PCI_CAPABILITY_LIST] = offset;
1519 pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
1520 memset(pdev->used + offset, 0xFF, size);
1521 /* Make capability read-only by default */
1522 memset(pdev->wmask + offset, 0, size);
1523 /* Check capability by default */
1524 memset(pdev->cmask + offset, 0xFF, size);
1525 return offset;
1528 /* Unlink capability from the pci config space. */
1529 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
1531 uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
1532 if (!offset)
1533 return;
1534 pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
1535 /* Make capability writeable again */
1536 memset(pdev->wmask + offset, 0xff, size);
1537 /* Clear cmask as device-specific registers can't be checked */
1538 memset(pdev->cmask + offset, 0, size);
1539 memset(pdev->used + offset, 0, size);
1541 if (!pdev->config[PCI_CAPABILITY_LIST])
1542 pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
1545 /* Reserve space for capability at a known offset (to call after load). */
1546 void pci_reserve_capability(PCIDevice *pdev, uint8_t offset, uint8_t size)
1548 memset(pdev->used + offset, 0xff, size);
1551 uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
1553 return pci_find_capability_list(pdev, cap_id, NULL);
1556 static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1558 PCIDevice *d = (PCIDevice *)dev;
1559 const pci_class_desc *desc;
1560 char ctxt[64];
1561 PCIIORegion *r;
1562 int i, class;
1564 class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1565 desc = pci_class_descriptions;
1566 while (desc->desc && class != desc->class)
1567 desc++;
1568 if (desc->desc) {
1569 snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1570 } else {
1571 snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1574 monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1575 "pci id %04x:%04x (sub %04x:%04x)\n",
1576 indent, "", ctxt,
1577 d->config[PCI_SECONDARY_BUS],
1578 PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1579 pci_get_word(d->config + PCI_VENDOR_ID),
1580 pci_get_word(d->config + PCI_DEVICE_ID),
1581 pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1582 pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1583 for (i = 0; i < PCI_NUM_REGIONS; i++) {
1584 r = &d->io_regions[i];
1585 if (!r->size)
1586 continue;
1587 monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1588 " [0x%"FMT_PCIBUS"]\n",
1589 indent, "",
1590 i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1591 r->addr, r->addr + r->size - 1);
1595 static PCIDeviceInfo bridge_info = {
1596 .qdev.name = "pci-bridge",
1597 .qdev.size = sizeof(PCIBridge),
1598 .init = pci_bridge_initfn,
1599 .exit = pci_bridge_exitfn,
1600 .config_write = pci_bridge_write_config,
1601 .qdev.props = (Property[]) {
1602 DEFINE_PROP_HEX32("vendorid", PCIBridge, vid, 0),
1603 DEFINE_PROP_HEX32("deviceid", PCIBridge, did, 0),
1604 DEFINE_PROP_END_OF_LIST(),
1608 static void pci_register_devices(void)
1610 pci_qdev_register(&bridge_info);
1613 device_init(pci_register_devices)