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
30 #include "qemu-objects.h"
34 # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
36 # define PCI_DPRINTF(format, ...) do { } while (0)
42 pci_set_irq_fn set_irq
;
43 pci_map_irq_fn map_irq
;
44 pci_hotplug_fn hotplug
;
45 DeviceState
*hotplug_qdev
;
47 PCIDevice
*devices
[256];
48 PCIDevice
*parent_dev
;
49 target_phys_addr_t mem_base
;
51 QLIST_HEAD(, PCIBus
) child
; /* this will be replaced by qdev later */
52 QLIST_ENTRY(PCIBus
) sibling
;/* this will be replaced by qdev later */
54 /* The bus IRQ state is the logical OR of the connected devices.
55 Keep a count of the number of devices with raised IRQs. */
60 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
61 static char *pcibus_get_dev_path(DeviceState
*dev
);
63 static struct BusInfo pci_bus_info
= {
65 .size
= sizeof(PCIBus
),
66 .print_dev
= pcibus_dev_print
,
67 .get_dev_path
= pcibus_get_dev_path
,
68 .props
= (Property
[]) {
69 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice
, devfn
, -1),
70 DEFINE_PROP_STRING("romfile", PCIDevice
, romfile
),
71 DEFINE_PROP_UINT32("rombar", PCIDevice
, rom_bar
, 1),
72 DEFINE_PROP_BIT("multifunction", PCIDevice
, cap_present
,
73 QEMU_PCI_CAP_MULTIFUNCTION_BITNR
, false),
74 DEFINE_PROP_END_OF_LIST()
78 static void pci_update_mappings(PCIDevice
*d
);
79 static void pci_set_irq(void *opaque
, int irq_num
, int level
);
80 static int pci_add_option_rom(PCIDevice
*pdev
);
81 static void pci_del_option_rom(PCIDevice
*pdev
);
83 static uint16_t pci_default_sub_vendor_id
= PCI_SUBVENDOR_ID_REDHAT_QUMRANET
;
84 static uint16_t pci_default_sub_device_id
= PCI_SUBDEVICE_ID_QEMU
;
89 QLIST_ENTRY(PCIHostBus
) next
;
91 static QLIST_HEAD(, PCIHostBus
) host_buses
;
93 static const VMStateDescription vmstate_pcibus
= {
96 .minimum_version_id
= 1,
97 .minimum_version_id_old
= 1,
98 .fields
= (VMStateField
[]) {
99 VMSTATE_INT32_EQUAL(nirq
, PCIBus
),
100 VMSTATE_VARRAY_INT32(irq_count
, PCIBus
, nirq
, 0, vmstate_info_int32
, int32_t),
101 VMSTATE_END_OF_LIST()
105 static int pci_bar(PCIDevice
*d
, int reg
)
109 if (reg
!= PCI_ROM_SLOT
)
110 return PCI_BASE_ADDRESS_0
+ reg
* 4;
112 type
= d
->config
[PCI_HEADER_TYPE
] & ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
113 return type
== PCI_HEADER_TYPE_BRIDGE
? PCI_ROM_ADDRESS1
: PCI_ROM_ADDRESS
;
116 static inline int pci_irq_state(PCIDevice
*d
, int irq_num
)
118 return (d
->irq_state
>> irq_num
) & 0x1;
121 static inline void pci_set_irq_state(PCIDevice
*d
, int irq_num
, int level
)
123 d
->irq_state
&= ~(0x1 << irq_num
);
124 d
->irq_state
|= level
<< irq_num
;
127 static void pci_change_irq_level(PCIDevice
*pci_dev
, int irq_num
, int change
)
132 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
135 pci_dev
= bus
->parent_dev
;
137 bus
->irq_count
[irq_num
] += change
;
138 bus
->set_irq(bus
->irq_opaque
, irq_num
, bus
->irq_count
[irq_num
] != 0);
141 /* Update interrupt status bit in config space on interrupt
143 static void pci_update_irq_status(PCIDevice
*dev
)
145 if (dev
->irq_state
) {
146 dev
->config
[PCI_STATUS
] |= PCI_STATUS_INTERRUPT
;
148 dev
->config
[PCI_STATUS
] &= ~PCI_STATUS_INTERRUPT
;
152 static void pci_device_reset(PCIDevice
*dev
)
157 pci_update_irq_status(dev
);
158 dev
->config
[PCI_COMMAND
] &= ~(PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
160 dev
->config
[PCI_CACHE_LINE_SIZE
] = 0x0;
161 dev
->config
[PCI_INTERRUPT_LINE
] = 0x0;
162 for (r
= 0; r
< PCI_NUM_REGIONS
; ++r
) {
163 if (!dev
->io_regions
[r
].size
) {
166 pci_set_long(dev
->config
+ pci_bar(dev
, r
), dev
->io_regions
[r
].type
);
168 pci_update_mappings(dev
);
171 static void pci_bus_reset(void *opaque
)
173 PCIBus
*bus
= opaque
;
176 for (i
= 0; i
< bus
->nirq
; i
++) {
177 bus
->irq_count
[i
] = 0;
179 for (i
= 0; i
< ARRAY_SIZE(bus
->devices
); ++i
) {
180 if (bus
->devices
[i
]) {
181 pci_device_reset(bus
->devices
[i
]);
186 static void pci_host_bus_register(int domain
, PCIBus
*bus
)
188 struct PCIHostBus
*host
;
189 host
= qemu_mallocz(sizeof(*host
));
190 host
->domain
= domain
;
192 QLIST_INSERT_HEAD(&host_buses
, host
, next
);
195 PCIBus
*pci_find_root_bus(int domain
)
197 struct PCIHostBus
*host
;
199 QLIST_FOREACH(host
, &host_buses
, next
) {
200 if (host
->domain
== domain
) {
208 int pci_find_domain(const PCIBus
*bus
)
211 struct PCIHostBus
*host
;
213 /* obtain root bus */
214 while ((d
= bus
->parent_dev
) != NULL
) {
218 QLIST_FOREACH(host
, &host_buses
, next
) {
219 if (host
->bus
== bus
) {
224 abort(); /* should not be reached */
228 void pci_bus_new_inplace(PCIBus
*bus
, DeviceState
*parent
,
229 const char *name
, int devfn_min
)
231 qbus_create_inplace(&bus
->qbus
, &pci_bus_info
, parent
, name
);
232 assert(PCI_FUNC(devfn_min
) == 0);
233 bus
->devfn_min
= devfn_min
;
236 QLIST_INIT(&bus
->child
);
237 pci_host_bus_register(0, bus
); /* for now only pci domain 0 is supported */
239 vmstate_register(NULL
, -1, &vmstate_pcibus
, bus
);
240 qemu_register_reset(pci_bus_reset
, bus
);
243 PCIBus
*pci_bus_new(DeviceState
*parent
, const char *name
, int devfn_min
)
247 bus
= qemu_mallocz(sizeof(*bus
));
248 bus
->qbus
.qdev_allocated
= 1;
249 pci_bus_new_inplace(bus
, parent
, name
, devfn_min
);
253 void pci_bus_irqs(PCIBus
*bus
, pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
254 void *irq_opaque
, int nirq
)
256 bus
->set_irq
= set_irq
;
257 bus
->map_irq
= map_irq
;
258 bus
->irq_opaque
= irq_opaque
;
260 bus
->irq_count
= qemu_mallocz(nirq
* sizeof(bus
->irq_count
[0]));
263 void pci_bus_hotplug(PCIBus
*bus
, pci_hotplug_fn hotplug
, DeviceState
*qdev
)
265 bus
->qbus
.allow_hotplug
= 1;
266 bus
->hotplug
= hotplug
;
267 bus
->hotplug_qdev
= qdev
;
270 void pci_bus_set_mem_base(PCIBus
*bus
, target_phys_addr_t base
)
272 bus
->mem_base
= base
;
275 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
276 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
277 void *irq_opaque
, int devfn_min
, int nirq
)
281 bus
= pci_bus_new(parent
, name
, devfn_min
);
282 pci_bus_irqs(bus
, set_irq
, map_irq
, irq_opaque
, nirq
);
286 static void pci_register_secondary_bus(PCIBus
*parent
,
289 pci_map_irq_fn map_irq
,
292 qbus_create_inplace(&bus
->qbus
, &pci_bus_info
, &dev
->qdev
, name
);
293 bus
->map_irq
= map_irq
;
294 bus
->parent_dev
= dev
;
296 QLIST_INIT(&bus
->child
);
297 QLIST_INSERT_HEAD(&parent
->child
, bus
, sibling
);
300 static void pci_unregister_secondary_bus(PCIBus
*bus
)
302 assert(QLIST_EMPTY(&bus
->child
));
303 QLIST_REMOVE(bus
, sibling
);
306 int pci_bus_num(PCIBus
*s
)
309 return 0; /* pci host bridge */
310 return s
->parent_dev
->config
[PCI_SECONDARY_BUS
];
313 static int get_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
315 PCIDevice
*s
= container_of(pv
, PCIDevice
, config
);
319 assert(size
== pci_config_size(s
));
320 config
= qemu_malloc(size
);
322 qemu_get_buffer(f
, config
, size
);
323 for (i
= 0; i
< size
; ++i
) {
324 if ((config
[i
] ^ s
->config
[i
]) & s
->cmask
[i
] & ~s
->wmask
[i
]) {
329 memcpy(s
->config
, config
, size
);
331 pci_update_mappings(s
);
337 /* just put buffer */
338 static void put_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
340 const uint8_t **v
= pv
;
341 assert(size
== pci_config_size(container_of(pv
, PCIDevice
, config
)));
342 qemu_put_buffer(f
, *v
, size
);
345 static VMStateInfo vmstate_info_pci_config
= {
346 .name
= "pci config",
347 .get
= get_pci_config_device
,
348 .put
= put_pci_config_device
,
351 static int get_pci_irq_state(QEMUFile
*f
, void *pv
, size_t size
)
353 PCIDevice
*s
= container_of(pv
, PCIDevice
, irq_state
);
354 uint32_t irq_state
[PCI_NUM_PINS
];
356 for (i
= 0; i
< PCI_NUM_PINS
; ++i
) {
357 irq_state
[i
] = qemu_get_be32(f
);
358 if (irq_state
[i
] != 0x1 && irq_state
[i
] != 0) {
359 fprintf(stderr
, "irq state %d: must be 0 or 1.\n",
365 for (i
= 0; i
< PCI_NUM_PINS
; ++i
) {
366 pci_set_irq_state(s
, i
, irq_state
[i
]);
372 static void put_pci_irq_state(QEMUFile
*f
, void *pv
, size_t size
)
375 PCIDevice
*s
= container_of(pv
, PCIDevice
, irq_state
);
377 for (i
= 0; i
< PCI_NUM_PINS
; ++i
) {
378 qemu_put_be32(f
, pci_irq_state(s
, i
));
382 static VMStateInfo vmstate_info_pci_irq_state
= {
383 .name
= "pci irq state",
384 .get
= get_pci_irq_state
,
385 .put
= put_pci_irq_state
,
388 const VMStateDescription vmstate_pci_device
= {
391 .minimum_version_id
= 1,
392 .minimum_version_id_old
= 1,
393 .fields
= (VMStateField
[]) {
394 VMSTATE_INT32_LE(version_id
, PCIDevice
),
395 VMSTATE_BUFFER_UNSAFE_INFO(config
, PCIDevice
, 0,
396 vmstate_info_pci_config
,
397 PCI_CONFIG_SPACE_SIZE
),
398 VMSTATE_BUFFER_UNSAFE_INFO(irq_state
, PCIDevice
, 2,
399 vmstate_info_pci_irq_state
,
400 PCI_NUM_PINS
* sizeof(int32_t)),
401 VMSTATE_END_OF_LIST()
405 const VMStateDescription vmstate_pcie_device
= {
408 .minimum_version_id
= 1,
409 .minimum_version_id_old
= 1,
410 .fields
= (VMStateField
[]) {
411 VMSTATE_INT32_LE(version_id
, PCIDevice
),
412 VMSTATE_BUFFER_UNSAFE_INFO(config
, PCIDevice
, 0,
413 vmstate_info_pci_config
,
414 PCIE_CONFIG_SPACE_SIZE
),
415 VMSTATE_BUFFER_UNSAFE_INFO(irq_state
, PCIDevice
, 2,
416 vmstate_info_pci_irq_state
,
417 PCI_NUM_PINS
* sizeof(int32_t)),
418 VMSTATE_END_OF_LIST()
422 static inline const VMStateDescription
*pci_get_vmstate(PCIDevice
*s
)
424 return pci_is_express(s
) ? &vmstate_pcie_device
: &vmstate_pci_device
;
427 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
429 /* Clear interrupt status bit: it is implicit
430 * in irq_state which we are saving.
431 * This makes us compatible with old devices
432 * which never set or clear this bit. */
433 s
->config
[PCI_STATUS
] &= ~PCI_STATUS_INTERRUPT
;
434 vmstate_save_state(f
, pci_get_vmstate(s
), s
);
435 /* Restore the interrupt status bit. */
436 pci_update_irq_status(s
);
439 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
442 ret
= vmstate_load_state(f
, pci_get_vmstate(s
), s
, s
->version_id
);
443 /* Restore the interrupt status bit. */
444 pci_update_irq_status(s
);
448 static void pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
450 pci_set_word(pci_dev
->config
+ PCI_SUBSYSTEM_VENDOR_ID
,
451 pci_default_sub_vendor_id
);
452 pci_set_word(pci_dev
->config
+ PCI_SUBSYSTEM_ID
,
453 pci_default_sub_device_id
);
457 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
459 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
464 unsigned long dom
= 0, bus
= 0;
468 val
= strtoul(p
, &e
, 16);
474 val
= strtoul(p
, &e
, 16);
481 val
= strtoul(p
, &e
, 16);
487 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
495 /* Note: QEMU doesn't implement domains other than 0 */
496 if (!pci_find_bus(pci_find_root_bus(dom
), bus
))
505 int pci_read_devaddr(Monitor
*mon
, const char *addr
, int *domp
, int *busp
,
508 /* strip legacy tag */
509 if (!strncmp(addr
, "pci_addr=", 9)) {
512 if (pci_parse_devaddr(addr
, domp
, busp
, slotp
)) {
513 monitor_printf(mon
, "Invalid pci address\n");
519 PCIBus
*pci_get_bus_devfn(int *devfnp
, const char *devaddr
)
526 return pci_find_bus(pci_find_root_bus(0), 0);
529 if (pci_parse_devaddr(devaddr
, &dom
, &bus
, &slot
) < 0) {
534 return pci_find_bus(pci_find_root_bus(dom
), bus
);
537 static void pci_init_cmask(PCIDevice
*dev
)
539 pci_set_word(dev
->cmask
+ PCI_VENDOR_ID
, 0xffff);
540 pci_set_word(dev
->cmask
+ PCI_DEVICE_ID
, 0xffff);
541 dev
->cmask
[PCI_STATUS
] = PCI_STATUS_CAP_LIST
;
542 dev
->cmask
[PCI_REVISION_ID
] = 0xff;
543 dev
->cmask
[PCI_CLASS_PROG
] = 0xff;
544 pci_set_word(dev
->cmask
+ PCI_CLASS_DEVICE
, 0xffff);
545 dev
->cmask
[PCI_HEADER_TYPE
] = 0xff;
546 dev
->cmask
[PCI_CAPABILITY_LIST
] = 0xff;
549 static void pci_init_wmask(PCIDevice
*dev
)
551 int config_size
= pci_config_size(dev
);
553 dev
->wmask
[PCI_CACHE_LINE_SIZE
] = 0xff;
554 dev
->wmask
[PCI_INTERRUPT_LINE
] = 0xff;
555 pci_set_word(dev
->wmask
+ PCI_COMMAND
,
556 PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
|
557 PCI_COMMAND_INTX_DISABLE
);
559 memset(dev
->wmask
+ PCI_CONFIG_HEADER_SIZE
, 0xff,
560 config_size
- PCI_CONFIG_HEADER_SIZE
);
563 static void pci_init_wmask_bridge(PCIDevice
*d
)
565 /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
566 PCI_SEC_LETENCY_TIMER */
567 memset(d
->wmask
+ PCI_PRIMARY_BUS
, 0xff, 4);
570 d
->wmask
[PCI_IO_BASE
] = PCI_IO_RANGE_MASK
& 0xff;
571 d
->wmask
[PCI_IO_LIMIT
] = PCI_IO_RANGE_MASK
& 0xff;
572 pci_set_word(d
->wmask
+ PCI_MEMORY_BASE
,
573 PCI_MEMORY_RANGE_MASK
& 0xffff);
574 pci_set_word(d
->wmask
+ PCI_MEMORY_LIMIT
,
575 PCI_MEMORY_RANGE_MASK
& 0xffff);
576 pci_set_word(d
->wmask
+ PCI_PREF_MEMORY_BASE
,
577 PCI_PREF_RANGE_MASK
& 0xffff);
578 pci_set_word(d
->wmask
+ PCI_PREF_MEMORY_LIMIT
,
579 PCI_PREF_RANGE_MASK
& 0xffff);
581 /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
582 memset(d
->wmask
+ PCI_PREF_BASE_UPPER32
, 0xff, 8);
584 pci_set_word(d
->wmask
+ PCI_BRIDGE_CONTROL
, 0xffff);
587 static void pci_config_alloc(PCIDevice
*pci_dev
)
589 int config_size
= pci_config_size(pci_dev
);
591 pci_dev
->config
= qemu_mallocz(config_size
);
592 pci_dev
->cmask
= qemu_mallocz(config_size
);
593 pci_dev
->wmask
= qemu_mallocz(config_size
);
594 pci_dev
->used
= qemu_mallocz(config_size
);
597 static void pci_config_free(PCIDevice
*pci_dev
)
599 qemu_free(pci_dev
->config
);
600 qemu_free(pci_dev
->cmask
);
601 qemu_free(pci_dev
->wmask
);
602 qemu_free(pci_dev
->used
);
605 /* -1 for devfn means auto assign */
606 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
607 const char *name
, int devfn
,
608 PCIConfigReadFunc
*config_read
,
609 PCIConfigWriteFunc
*config_write
,
613 for(devfn
= bus
->devfn_min
; devfn
< ARRAY_SIZE(bus
->devices
);
614 devfn
+= PCI_FUNC_MAX
) {
615 if (!bus
->devices
[devfn
])
618 error_report("PCI: no slot/function available for %s, all in use", name
);
621 } else if (bus
->devices
[devfn
]) {
622 error_report("PCI: slot %d function %d not available for %s, in use by %s",
623 PCI_SLOT(devfn
), PCI_FUNC(devfn
), name
, bus
->devices
[devfn
]->name
);
627 pci_dev
->devfn
= devfn
;
628 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
629 pci_dev
->irq_state
= 0;
630 pci_config_alloc(pci_dev
);
633 pci_set_default_subsystem_id(pci_dev
);
635 pci_init_cmask(pci_dev
);
636 pci_init_wmask(pci_dev
);
638 pci_init_wmask_bridge(pci_dev
);
642 config_read
= pci_default_read_config
;
644 config_write
= pci_default_write_config
;
645 pci_dev
->config_read
= config_read
;
646 pci_dev
->config_write
= config_write
;
647 bus
->devices
[devfn
] = pci_dev
;
648 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, PCI_NUM_PINS
);
649 pci_dev
->version_id
= 2; /* Current pci device vmstate version */
653 static void do_pci_unregister_device(PCIDevice
*pci_dev
)
655 qemu_free_irqs(pci_dev
->irq
);
656 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
657 pci_config_free(pci_dev
);
660 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
661 int instance_size
, int devfn
,
662 PCIConfigReadFunc
*config_read
,
663 PCIConfigWriteFunc
*config_write
)
667 pci_dev
= qemu_mallocz(instance_size
);
668 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
669 config_read
, config_write
,
670 PCI_HEADER_TYPE_NORMAL
);
671 if (pci_dev
== NULL
) {
672 hw_error("PCI: can't register device\n");
677 static target_phys_addr_t
pci_to_cpu_addr(PCIBus
*bus
,
678 target_phys_addr_t addr
)
680 return addr
+ bus
->mem_base
;
683 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
688 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
689 r
= &pci_dev
->io_regions
[i
];
690 if (!r
->size
|| r
->addr
== PCI_BAR_UNMAPPED
)
692 if (r
->type
== PCI_BASE_ADDRESS_SPACE_IO
) {
693 isa_unassign_ioport(r
->addr
, r
->filtered_size
);
695 cpu_register_physical_memory(pci_to_cpu_addr(pci_dev
->bus
,
703 static int pci_unregister_device(DeviceState
*dev
)
705 PCIDevice
*pci_dev
= DO_UPCAST(PCIDevice
, qdev
, dev
);
706 PCIDeviceInfo
*info
= DO_UPCAST(PCIDeviceInfo
, qdev
, dev
->info
);
710 ret
= info
->exit(pci_dev
);
714 pci_unregister_io_regions(pci_dev
);
715 pci_del_option_rom(pci_dev
);
716 do_pci_unregister_device(pci_dev
);
720 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
721 pcibus_t size
, int type
,
722 PCIMapIORegionFunc
*map_func
)
728 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
731 if (size
& (size
-1)) {
732 fprintf(stderr
, "ERROR: PCI region size must be pow2 "
733 "type=0x%x, size=0x%"FMT_PCIBUS
"\n", type
, size
);
737 r
= &pci_dev
->io_regions
[region_num
];
738 r
->addr
= PCI_BAR_UNMAPPED
;
740 r
->filtered_size
= size
;
742 r
->map_func
= map_func
;
745 addr
= pci_bar(pci_dev
, region_num
);
746 if (region_num
== PCI_ROM_SLOT
) {
747 /* ROM enable bit is writeable */
748 wmask
|= PCI_ROM_ADDRESS_ENABLE
;
750 pci_set_long(pci_dev
->config
+ addr
, type
);
751 if (!(r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) &&
752 r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
753 pci_set_quad(pci_dev
->wmask
+ addr
, wmask
);
754 pci_set_quad(pci_dev
->cmask
+ addr
, ~0ULL);
756 pci_set_long(pci_dev
->wmask
+ addr
, wmask
& 0xffffffff);
757 pci_set_long(pci_dev
->cmask
+ addr
, 0xffffffff);
761 static uint32_t pci_config_get_io_base(PCIDevice
*d
,
762 uint32_t base
, uint32_t base_upper16
)
766 val
= ((uint32_t)d
->config
[base
] & PCI_IO_RANGE_MASK
) << 8;
767 if (d
->config
[base
] & PCI_IO_RANGE_TYPE_32
) {
768 val
|= (uint32_t)pci_get_word(d
->config
+ base_upper16
) << 16;
773 static pcibus_t
pci_config_get_memory_base(PCIDevice
*d
, uint32_t base
)
775 return ((pcibus_t
)pci_get_word(d
->config
+ base
) & PCI_MEMORY_RANGE_MASK
)
779 static pcibus_t
pci_config_get_pref_base(PCIDevice
*d
,
780 uint32_t base
, uint32_t upper
)
785 tmp
= (pcibus_t
)pci_get_word(d
->config
+ base
);
786 val
= (tmp
& PCI_PREF_RANGE_MASK
) << 16;
787 if (tmp
& PCI_PREF_RANGE_TYPE_64
) {
788 val
|= (pcibus_t
)pci_get_long(d
->config
+ upper
) << 32;
793 static pcibus_t
pci_bridge_get_base(PCIDevice
*bridge
, uint8_t type
)
796 if (type
& PCI_BASE_ADDRESS_SPACE_IO
) {
797 base
= pci_config_get_io_base(bridge
,
798 PCI_IO_BASE
, PCI_IO_BASE_UPPER16
);
800 if (type
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
801 base
= pci_config_get_pref_base(
802 bridge
, PCI_PREF_MEMORY_BASE
, PCI_PREF_BASE_UPPER32
);
804 base
= pci_config_get_memory_base(bridge
, PCI_MEMORY_BASE
);
811 static pcibus_t
pci_bridge_get_limit(PCIDevice
*bridge
, uint8_t type
)
814 if (type
& PCI_BASE_ADDRESS_SPACE_IO
) {
815 limit
= pci_config_get_io_base(bridge
,
816 PCI_IO_LIMIT
, PCI_IO_LIMIT_UPPER16
);
817 limit
|= 0xfff; /* PCI bridge spec 3.2.5.6. */
819 if (type
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
820 limit
= pci_config_get_pref_base(
821 bridge
, PCI_PREF_MEMORY_LIMIT
, PCI_PREF_LIMIT_UPPER32
);
823 limit
= pci_config_get_memory_base(bridge
, PCI_MEMORY_LIMIT
);
825 limit
|= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */
830 static void pci_bridge_filter(PCIDevice
*d
, pcibus_t
*addr
, pcibus_t
*size
,
833 pcibus_t base
= *addr
;
834 pcibus_t limit
= *addr
+ *size
- 1;
837 for (br
= d
->bus
->parent_dev
; br
; br
= br
->bus
->parent_dev
) {
838 uint16_t cmd
= pci_get_word(d
->config
+ PCI_COMMAND
);
840 if (type
& PCI_BASE_ADDRESS_SPACE_IO
) {
841 if (!(cmd
& PCI_COMMAND_IO
)) {
845 if (!(cmd
& PCI_COMMAND_MEMORY
)) {
850 base
= MAX(base
, pci_bridge_get_base(br
, type
));
851 limit
= MIN(limit
, pci_bridge_get_limit(br
, type
));
858 *size
= limit
- base
+ 1;
861 *addr
= PCI_BAR_UNMAPPED
;
865 static pcibus_t
pci_bar_address(PCIDevice
*d
,
866 int reg
, uint8_t type
, pcibus_t size
)
868 pcibus_t new_addr
, last_addr
;
869 int bar
= pci_bar(d
, reg
);
870 uint16_t cmd
= pci_get_word(d
->config
+ PCI_COMMAND
);
872 if (type
& PCI_BASE_ADDRESS_SPACE_IO
) {
873 if (!(cmd
& PCI_COMMAND_IO
)) {
874 return PCI_BAR_UNMAPPED
;
876 new_addr
= pci_get_long(d
->config
+ bar
) & ~(size
- 1);
877 last_addr
= new_addr
+ size
- 1;
878 /* NOTE: we have only 64K ioports on PC */
879 if (last_addr
<= new_addr
|| new_addr
== 0 || last_addr
> UINT16_MAX
) {
880 return PCI_BAR_UNMAPPED
;
885 if (!(cmd
& PCI_COMMAND_MEMORY
)) {
886 return PCI_BAR_UNMAPPED
;
888 if (type
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
889 new_addr
= pci_get_quad(d
->config
+ bar
);
891 new_addr
= pci_get_long(d
->config
+ bar
);
893 /* the ROM slot has a specific enable bit */
894 if (reg
== PCI_ROM_SLOT
&& !(new_addr
& PCI_ROM_ADDRESS_ENABLE
)) {
895 return PCI_BAR_UNMAPPED
;
897 new_addr
&= ~(size
- 1);
898 last_addr
= new_addr
+ size
- 1;
899 /* NOTE: we do not support wrapping */
900 /* XXX: as we cannot support really dynamic
901 mappings, we handle specific values as invalid
903 if (last_addr
<= new_addr
|| new_addr
== 0 ||
904 last_addr
== PCI_BAR_UNMAPPED
) {
905 return PCI_BAR_UNMAPPED
;
908 /* Now pcibus_t is 64bit.
909 * Check if 32 bit BAR wraps around explicitly.
910 * Without this, PC ide doesn't work well.
911 * TODO: remove this work around.
913 if (!(type
& PCI_BASE_ADDRESS_MEM_TYPE_64
) && last_addr
>= UINT32_MAX
) {
914 return PCI_BAR_UNMAPPED
;
918 * OS is allowed to set BAR beyond its addressable
919 * bits. For example, 32 bit OS can set 64bit bar
920 * to >4G. Check it. TODO: we might need to support
921 * it in the future for e.g. PAE.
923 if (last_addr
>= TARGET_PHYS_ADDR_MAX
) {
924 return PCI_BAR_UNMAPPED
;
930 static void pci_update_mappings(PCIDevice
*d
)
934 pcibus_t new_addr
, filtered_size
;
936 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
937 r
= &d
->io_regions
[i
];
939 /* this region isn't registered */
943 new_addr
= pci_bar_address(d
, i
, r
->type
, r
->size
);
945 /* bridge filtering */
946 filtered_size
= r
->size
;
947 if (new_addr
!= PCI_BAR_UNMAPPED
) {
948 pci_bridge_filter(d
, &new_addr
, &filtered_size
, r
->type
);
951 /* This bar isn't changed */
952 if (new_addr
== r
->addr
&& filtered_size
== r
->filtered_size
)
955 /* now do the real mapping */
956 if (r
->addr
!= PCI_BAR_UNMAPPED
) {
957 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
959 /* NOTE: specific hack for IDE in PC case:
960 only one byte must be mapped. */
961 class = pci_get_word(d
->config
+ PCI_CLASS_DEVICE
);
962 if (class == 0x0101 && r
->size
== 4) {
963 isa_unassign_ioport(r
->addr
+ 2, 1);
965 isa_unassign_ioport(r
->addr
, r
->filtered_size
);
968 cpu_register_physical_memory(pci_to_cpu_addr(d
->bus
, r
->addr
),
971 qemu_unregister_coalesced_mmio(r
->addr
, r
->filtered_size
);
975 r
->filtered_size
= filtered_size
;
976 if (r
->addr
!= PCI_BAR_UNMAPPED
) {
978 * TODO: currently almost all the map funcions assumes
979 * filtered_size == size and addr & ~(size - 1) == addr.
980 * However with bridge filtering, they aren't always true.
981 * Teach them such cases, such that filtered_size < size and
982 * addr & (size - 1) != 0.
984 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
985 r
->map_func(d
, i
, r
->addr
, r
->filtered_size
, r
->type
);
987 r
->map_func(d
, i
, pci_to_cpu_addr(d
->bus
, r
->addr
),
988 r
->filtered_size
, r
->type
);
994 static inline int pci_irq_disabled(PCIDevice
*d
)
996 return pci_get_word(d
->config
+ PCI_COMMAND
) & PCI_COMMAND_INTX_DISABLE
;
999 /* Called after interrupt disabled field update in config space,
1000 * assert/deassert interrupts if necessary.
1001 * Gets original interrupt disable bit value (before update). */
1002 static void pci_update_irq_disabled(PCIDevice
*d
, int was_irq_disabled
)
1004 int i
, disabled
= pci_irq_disabled(d
);
1005 if (disabled
== was_irq_disabled
)
1007 for (i
= 0; i
< PCI_NUM_PINS
; ++i
) {
1008 int state
= pci_irq_state(d
, i
);
1009 pci_change_irq_level(d
, i
, disabled
? -state
: state
);
1013 uint32_t pci_default_read_config(PCIDevice
*d
,
1014 uint32_t address
, int len
)
1017 assert(len
== 1 || len
== 2 || len
== 4);
1018 len
= MIN(len
, pci_config_size(d
) - address
);
1019 memcpy(&val
, d
->config
+ address
, len
);
1020 return le32_to_cpu(val
);
1023 void pci_default_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int l
)
1025 int i
, was_irq_disabled
= pci_irq_disabled(d
);
1026 uint32_t config_size
= pci_config_size(d
);
1028 for (i
= 0; i
< l
&& addr
+ i
< config_size
; val
>>= 8, ++i
) {
1029 uint8_t wmask
= d
->wmask
[addr
+ i
];
1030 d
->config
[addr
+ i
] = (d
->config
[addr
+ i
] & ~wmask
) | (val
& wmask
);
1032 if (ranges_overlap(addr
, l
, PCI_BASE_ADDRESS_0
, 24) ||
1033 ranges_overlap(addr
, l
, PCI_ROM_ADDRESS
, 4) ||
1034 ranges_overlap(addr
, l
, PCI_ROM_ADDRESS1
, 4) ||
1035 range_covers_byte(addr
, l
, PCI_COMMAND
))
1036 pci_update_mappings(d
);
1038 if (range_covers_byte(addr
, l
, PCI_COMMAND
))
1039 pci_update_irq_disabled(d
, was_irq_disabled
);
1042 /***********************************************************/
1043 /* generic PCI irq support */
1045 /* 0 <= irq_num <= 3. level must be 0 or 1 */
1046 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
1048 PCIDevice
*pci_dev
= opaque
;
1051 change
= level
- pci_irq_state(pci_dev
, irq_num
);
1055 pci_set_irq_state(pci_dev
, irq_num
, level
);
1056 pci_update_irq_status(pci_dev
);
1057 if (pci_irq_disabled(pci_dev
))
1059 pci_change_irq_level(pci_dev
, irq_num
, change
);
1062 /***********************************************************/
1063 /* monitor info on PCI */
1070 static const pci_class_desc pci_class_descriptions
[] =
1072 { 0x0100, "SCSI controller"},
1073 { 0x0101, "IDE controller"},
1074 { 0x0102, "Floppy controller"},
1075 { 0x0103, "IPI controller"},
1076 { 0x0104, "RAID controller"},
1077 { 0x0106, "SATA controller"},
1078 { 0x0107, "SAS controller"},
1079 { 0x0180, "Storage controller"},
1080 { 0x0200, "Ethernet controller"},
1081 { 0x0201, "Token Ring controller"},
1082 { 0x0202, "FDDI controller"},
1083 { 0x0203, "ATM controller"},
1084 { 0x0280, "Network controller"},
1085 { 0x0300, "VGA controller"},
1086 { 0x0301, "XGA controller"},
1087 { 0x0302, "3D controller"},
1088 { 0x0380, "Display controller"},
1089 { 0x0400, "Video controller"},
1090 { 0x0401, "Audio controller"},
1092 { 0x0480, "Multimedia controller"},
1093 { 0x0500, "RAM controller"},
1094 { 0x0501, "Flash controller"},
1095 { 0x0580, "Memory controller"},
1096 { 0x0600, "Host bridge"},
1097 { 0x0601, "ISA bridge"},
1098 { 0x0602, "EISA bridge"},
1099 { 0x0603, "MC bridge"},
1100 { 0x0604, "PCI bridge"},
1101 { 0x0605, "PCMCIA bridge"},
1102 { 0x0606, "NUBUS bridge"},
1103 { 0x0607, "CARDBUS bridge"},
1104 { 0x0608, "RACEWAY bridge"},
1105 { 0x0680, "Bridge"},
1106 { 0x0c03, "USB controller"},
1110 static void pci_for_each_device_under_bus(PCIBus
*bus
,
1111 void (*fn
)(PCIBus
*b
, PCIDevice
*d
))
1116 for(devfn
= 0; devfn
< ARRAY_SIZE(bus
->devices
); devfn
++) {
1117 d
= bus
->devices
[devfn
];
1124 void pci_for_each_device(PCIBus
*bus
, int bus_num
,
1125 void (*fn
)(PCIBus
*b
, PCIDevice
*d
))
1127 bus
= pci_find_bus(bus
, bus_num
);
1130 pci_for_each_device_under_bus(bus
, fn
);
1134 static void pci_device_print(Monitor
*mon
, QDict
*device
)
1138 uint64_t addr
, size
;
1140 monitor_printf(mon
, " Bus %2" PRId64
", ", qdict_get_int(device
, "bus"));
1141 monitor_printf(mon
, "device %3" PRId64
", function %" PRId64
":\n",
1142 qdict_get_int(device
, "slot"),
1143 qdict_get_int(device
, "function"));
1144 monitor_printf(mon
, " ");
1146 qdict
= qdict_get_qdict(device
, "class_info");
1147 if (qdict_haskey(qdict
, "desc")) {
1148 monitor_printf(mon
, "%s", qdict_get_str(qdict
, "desc"));
1150 monitor_printf(mon
, "Class %04" PRId64
, qdict_get_int(qdict
, "class"));
1153 qdict
= qdict_get_qdict(device
, "id");
1154 monitor_printf(mon
, ": PCI device %04" PRIx64
":%04" PRIx64
"\n",
1155 qdict_get_int(qdict
, "device"),
1156 qdict_get_int(qdict
, "vendor"));
1158 if (qdict_haskey(device
, "irq")) {
1159 monitor_printf(mon
, " IRQ %" PRId64
".\n",
1160 qdict_get_int(device
, "irq"));
1163 if (qdict_haskey(device
, "pci_bridge")) {
1166 qdict
= qdict_get_qdict(device
, "pci_bridge");
1168 info
= qdict_get_qdict(qdict
, "bus");
1169 monitor_printf(mon
, " BUS %" PRId64
".\n",
1170 qdict_get_int(info
, "number"));
1171 monitor_printf(mon
, " secondary bus %" PRId64
".\n",
1172 qdict_get_int(info
, "secondary"));
1173 monitor_printf(mon
, " subordinate bus %" PRId64
".\n",
1174 qdict_get_int(info
, "subordinate"));
1176 info
= qdict_get_qdict(qdict
, "io_range");
1177 monitor_printf(mon
, " IO range [0x%04"PRIx64
", 0x%04"PRIx64
"]\n",
1178 qdict_get_int(info
, "base"),
1179 qdict_get_int(info
, "limit"));
1181 info
= qdict_get_qdict(qdict
, "memory_range");
1183 " memory range [0x%08"PRIx64
", 0x%08"PRIx64
"]\n",
1184 qdict_get_int(info
, "base"),
1185 qdict_get_int(info
, "limit"));
1187 info
= qdict_get_qdict(qdict
, "prefetchable_range");
1188 monitor_printf(mon
, " prefetchable memory range "
1189 "[0x%08"PRIx64
", 0x%08"PRIx64
"]\n",
1190 qdict_get_int(info
, "base"),
1191 qdict_get_int(info
, "limit"));
1194 QLIST_FOREACH_ENTRY(qdict_get_qlist(device
, "regions"), entry
) {
1195 qdict
= qobject_to_qdict(qlist_entry_obj(entry
));
1196 monitor_printf(mon
, " BAR%d: ", (int) qdict_get_int(qdict
, "bar"));
1198 addr
= qdict_get_int(qdict
, "address");
1199 size
= qdict_get_int(qdict
, "size");
1201 if (!strcmp(qdict_get_str(qdict
, "type"), "io")) {
1202 monitor_printf(mon
, "I/O at 0x%04"FMT_PCIBUS
1203 " [0x%04"FMT_PCIBUS
"].\n",
1204 addr
, addr
+ size
- 1);
1206 monitor_printf(mon
, "%d bit%s memory at 0x%08"FMT_PCIBUS
1207 " [0x%08"FMT_PCIBUS
"].\n",
1208 qdict_get_bool(qdict
, "mem_type_64") ? 64 : 32,
1209 qdict_get_bool(qdict
, "prefetch") ?
1210 " prefetchable" : "", addr
, addr
+ size
- 1);
1214 monitor_printf(mon
, " id \"%s\"\n", qdict_get_str(device
, "qdev_id"));
1216 if (qdict_haskey(device
, "pci_bridge")) {
1217 qdict
= qdict_get_qdict(device
, "pci_bridge");
1218 if (qdict_haskey(qdict
, "devices")) {
1220 QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict
, "devices"), dev
) {
1221 pci_device_print(mon
, qobject_to_qdict(qlist_entry_obj(dev
)));
1227 void do_pci_info_print(Monitor
*mon
, const QObject
*data
)
1229 QListEntry
*bus
, *dev
;
1231 QLIST_FOREACH_ENTRY(qobject_to_qlist(data
), bus
) {
1232 QDict
*qdict
= qobject_to_qdict(qlist_entry_obj(bus
));
1233 QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict
, "devices"), dev
) {
1234 pci_device_print(mon
, qobject_to_qdict(qlist_entry_obj(dev
)));
1239 static QObject
*pci_get_dev_class(const PCIDevice
*dev
)
1242 const pci_class_desc
*desc
;
1244 class = pci_get_word(dev
->config
+ PCI_CLASS_DEVICE
);
1245 desc
= pci_class_descriptions
;
1246 while (desc
->desc
&& class != desc
->class)
1250 return qobject_from_jsonf("{ 'desc': %s, 'class': %d }",
1253 return qobject_from_jsonf("{ 'class': %d }", class);
1257 static QObject
*pci_get_dev_id(const PCIDevice
*dev
)
1259 return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }",
1260 pci_get_word(dev
->config
+ PCI_VENDOR_ID
),
1261 pci_get_word(dev
->config
+ PCI_DEVICE_ID
));
1264 static QObject
*pci_get_regions_list(const PCIDevice
*dev
)
1267 QList
*regions_list
;
1269 regions_list
= qlist_new();
1271 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1273 const PCIIORegion
*r
= &dev
->io_regions
[i
];
1279 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
1280 obj
= qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
1281 "'address': %" PRId64
", "
1282 "'size': %" PRId64
" }",
1283 i
, r
->addr
, r
->size
);
1285 int mem_type_64
= r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
;
1287 obj
= qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
1288 "'mem_type_64': %i, 'prefetch': %i, "
1289 "'address': %" PRId64
", "
1290 "'size': %" PRId64
" }",
1292 r
->type
& PCI_BASE_ADDRESS_MEM_PREFETCH
,
1296 qlist_append_obj(regions_list
, obj
);
1299 return QOBJECT(regions_list
);
1302 static QObject
*pci_get_devices_list(PCIBus
*bus
, int bus_num
);
1304 static QObject
*pci_get_dev_dict(PCIDevice
*dev
, PCIBus
*bus
, int bus_num
)
1309 obj
= qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d," "'class_info': %p, 'id': %p, 'regions': %p,"
1312 PCI_SLOT(dev
->devfn
), PCI_FUNC(dev
->devfn
),
1313 pci_get_dev_class(dev
), pci_get_dev_id(dev
),
1314 pci_get_regions_list(dev
),
1315 dev
->qdev
.id
? dev
->qdev
.id
: "");
1317 if (dev
->config
[PCI_INTERRUPT_PIN
] != 0) {
1318 QDict
*qdict
= qobject_to_qdict(obj
);
1319 qdict_put(qdict
, "irq", qint_from_int(dev
->config
[PCI_INTERRUPT_LINE
]));
1322 type
= dev
->config
[PCI_HEADER_TYPE
] & ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
1323 if (type
== PCI_HEADER_TYPE_BRIDGE
) {
1325 QObject
*pci_bridge
;
1327 pci_bridge
= qobject_from_jsonf("{ 'bus': "
1328 "{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
1329 "'io_range': { 'base': %" PRId64
", 'limit': %" PRId64
"}, "
1330 "'memory_range': { 'base': %" PRId64
", 'limit': %" PRId64
"}, "
1331 "'prefetchable_range': { 'base': %" PRId64
", 'limit': %" PRId64
"} }",
1332 dev
->config
[PCI_PRIMARY_BUS
], dev
->config
[PCI_SECONDARY_BUS
],
1333 dev
->config
[PCI_SUBORDINATE_BUS
],
1334 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_IO
),
1335 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_IO
),
1336 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
),
1337 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
),
1338 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
|
1339 PCI_BASE_ADDRESS_MEM_PREFETCH
),
1340 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
|
1341 PCI_BASE_ADDRESS_MEM_PREFETCH
));
1343 if (dev
->config
[PCI_SECONDARY_BUS
] != 0) {
1344 PCIBus
*child_bus
= pci_find_bus(bus
, dev
->config
[PCI_SECONDARY_BUS
]);
1347 qdict
= qobject_to_qdict(pci_bridge
);
1348 qdict_put_obj(qdict
, "devices",
1349 pci_get_devices_list(child_bus
,
1350 dev
->config
[PCI_SECONDARY_BUS
]));
1353 qdict
= qobject_to_qdict(obj
);
1354 qdict_put_obj(qdict
, "pci_bridge", pci_bridge
);
1360 static QObject
*pci_get_devices_list(PCIBus
*bus
, int bus_num
)
1366 dev_list
= qlist_new();
1368 for (devfn
= 0; devfn
< ARRAY_SIZE(bus
->devices
); devfn
++) {
1369 dev
= bus
->devices
[devfn
];
1371 qlist_append_obj(dev_list
, pci_get_dev_dict(dev
, bus
, bus_num
));
1375 return QOBJECT(dev_list
);
1378 static QObject
*pci_get_bus_dict(PCIBus
*bus
, int bus_num
)
1380 bus
= pci_find_bus(bus
, bus_num
);
1382 return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }",
1383 bus_num
, pci_get_devices_list(bus
, bus_num
));
1389 void do_pci_info(Monitor
*mon
, QObject
**ret_data
)
1392 struct PCIHostBus
*host
;
1394 bus_list
= qlist_new();
1396 QLIST_FOREACH(host
, &host_buses
, next
) {
1397 QObject
*obj
= pci_get_bus_dict(host
->bus
, 0);
1399 qlist_append_obj(bus_list
, obj
);
1403 *ret_data
= QOBJECT(bus_list
);
1406 static const char * const pci_nic_models
[] = {
1418 static const char * const pci_nic_names
[] = {
1430 /* Initialize a PCI NIC. */
1431 /* FIXME callers should check for failure, but don't */
1432 PCIDevice
*pci_nic_init(NICInfo
*nd
, const char *default_model
,
1433 const char *default_devaddr
)
1435 const char *devaddr
= nd
->devaddr
? nd
->devaddr
: default_devaddr
;
1442 i
= qemu_find_nic_model(nd
, pci_nic_models
, default_model
);
1446 bus
= pci_get_bus_devfn(&devfn
, devaddr
);
1448 error_report("Invalid PCI device address %s for device %s",
1449 devaddr
, pci_nic_names
[i
]);
1453 pci_dev
= pci_create(bus
, devfn
, pci_nic_names
[i
]);
1454 dev
= &pci_dev
->qdev
;
1455 qdev_set_nic_properties(dev
, nd
);
1456 if (qdev_init(dev
) < 0)
1461 PCIDevice
*pci_nic_init_nofail(NICInfo
*nd
, const char *default_model
,
1462 const char *default_devaddr
)
1466 if (qemu_show_nic_models(nd
->model
, pci_nic_models
))
1469 res
= pci_nic_init(nd
, default_model
, default_devaddr
);
1483 static void pci_bridge_update_mappings_fn(PCIBus
*b
, PCIDevice
*d
)
1485 pci_update_mappings(d
);
1488 static void pci_bridge_update_mappings(PCIBus
*b
)
1492 pci_for_each_device_under_bus(b
, pci_bridge_update_mappings_fn
);
1494 QLIST_FOREACH(child
, &b
->child
, sibling
) {
1495 pci_bridge_update_mappings(child
);
1499 static void pci_bridge_write_config(PCIDevice
*d
,
1500 uint32_t address
, uint32_t val
, int len
)
1502 pci_default_write_config(d
, address
, val
, len
);
1504 if (/* io base/limit */
1505 ranges_overlap(address
, len
, PCI_IO_BASE
, 2) ||
1507 /* memory base/limit, prefetchable base/limit and
1508 io base/limit upper 16 */
1509 ranges_overlap(address
, len
, PCI_MEMORY_BASE
, 20)) {
1510 pci_bridge_update_mappings(d
->bus
);
1514 PCIBus
*pci_find_bus(PCIBus
*bus
, int bus_num
)
1522 if (pci_bus_num(bus
) == bus_num
) {
1527 if (!bus
->parent_dev
/* host pci bridge */ ||
1528 (bus
->parent_dev
->config
[PCI_SECONDARY_BUS
] < bus_num
&&
1529 bus_num
<= bus
->parent_dev
->config
[PCI_SUBORDINATE_BUS
])) {
1530 for (; bus
; bus
= sec
) {
1531 QLIST_FOREACH(sec
, &bus
->child
, sibling
) {
1532 assert(sec
->parent_dev
);
1533 if (sec
->parent_dev
->config
[PCI_SECONDARY_BUS
] == bus_num
) {
1536 if (sec
->parent_dev
->config
[PCI_SECONDARY_BUS
] < bus_num
&&
1537 bus_num
<= sec
->parent_dev
->config
[PCI_SUBORDINATE_BUS
]) {
1547 PCIDevice
*pci_find_device(PCIBus
*bus
, int bus_num
, int slot
, int function
)
1549 bus
= pci_find_bus(bus
, bus_num
);
1554 return bus
->devices
[PCI_DEVFN(slot
, function
)];
1557 static int pci_bridge_initfn(PCIDevice
*dev
)
1559 PCIBridge
*s
= DO_UPCAST(PCIBridge
, dev
, dev
);
1561 pci_config_set_vendor_id(s
->dev
.config
, s
->vid
);
1562 pci_config_set_device_id(s
->dev
.config
, s
->did
);
1564 pci_set_word(dev
->config
+ PCI_STATUS
,
1565 PCI_STATUS_66MHZ
| PCI_STATUS_FAST_BACK
);
1566 pci_config_set_class(dev
->config
, PCI_CLASS_BRIDGE_PCI
);
1567 dev
->config
[PCI_HEADER_TYPE
] =
1568 (dev
->config
[PCI_HEADER_TYPE
] & PCI_HEADER_TYPE_MULTI_FUNCTION
) |
1569 PCI_HEADER_TYPE_BRIDGE
;
1570 pci_set_word(dev
->config
+ PCI_SEC_STATUS
,
1571 PCI_STATUS_66MHZ
| PCI_STATUS_FAST_BACK
);
1575 static int pci_bridge_exitfn(PCIDevice
*pci_dev
)
1577 PCIBridge
*s
= DO_UPCAST(PCIBridge
, dev
, pci_dev
);
1578 PCIBus
*bus
= &s
->bus
;
1579 pci_unregister_secondary_bus(bus
);
1583 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, bool multifunction
,
1584 uint16_t vid
, uint16_t did
,
1585 pci_map_irq_fn map_irq
, const char *name
)
1590 dev
= pci_create_multifunction(bus
, devfn
, multifunction
, "pci-bridge");
1591 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", vid
);
1592 qdev_prop_set_uint32(&dev
->qdev
, "deviceid", did
);
1593 qdev_init_nofail(&dev
->qdev
);
1595 s
= DO_UPCAST(PCIBridge
, dev
, dev
);
1596 pci_register_secondary_bus(bus
, &s
->bus
, &s
->dev
, map_irq
, name
);
1600 PCIDevice
*pci_bridge_get_device(PCIBus
*bus
)
1602 return bus
->parent_dev
;
1605 static int pci_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
1607 PCIDevice
*pci_dev
= (PCIDevice
*)qdev
;
1608 PCIDeviceInfo
*info
= container_of(base
, PCIDeviceInfo
, qdev
);
1612 /* initialize cap_present for pci_is_express() and pci_config_size() */
1613 if (info
->is_express
) {
1614 pci_dev
->cap_present
|= QEMU_PCI_CAP_EXPRESS
;
1617 bus
= FROM_QBUS(PCIBus
, qdev_get_parent_bus(qdev
));
1618 devfn
= pci_dev
->devfn
;
1619 pci_dev
= do_pci_register_device(pci_dev
, bus
, base
->name
, devfn
,
1620 info
->config_read
, info
->config_write
,
1622 if (pci_dev
== NULL
)
1624 rc
= info
->init(pci_dev
);
1626 do_pci_unregister_device(pci_dev
);
1631 if (pci_dev
->romfile
== NULL
&& info
->romfile
!= NULL
)
1632 pci_dev
->romfile
= qemu_strdup(info
->romfile
);
1633 pci_add_option_rom(pci_dev
);
1635 if (qdev
->hotplugged
)
1636 bus
->hotplug(bus
->hotplug_qdev
, pci_dev
, 1);
1640 static int pci_unplug_device(DeviceState
*qdev
)
1642 PCIDevice
*dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
1644 dev
->bus
->hotplug(dev
->bus
->hotplug_qdev
, dev
, 0);
1648 void pci_qdev_register(PCIDeviceInfo
*info
)
1650 info
->qdev
.init
= pci_qdev_init
;
1651 info
->qdev
.unplug
= pci_unplug_device
;
1652 info
->qdev
.exit
= pci_unregister_device
;
1653 info
->qdev
.bus_info
= &pci_bus_info
;
1654 qdev_register(&info
->qdev
);
1657 void pci_qdev_register_many(PCIDeviceInfo
*info
)
1659 while (info
->qdev
.name
) {
1660 pci_qdev_register(info
);
1665 PCIDevice
*pci_create_multifunction(PCIBus
*bus
, int devfn
, bool multifunction
,
1670 dev
= qdev_create(&bus
->qbus
, name
);
1671 qdev_prop_set_uint32(dev
, "addr", devfn
);
1672 qdev_prop_set_bit(dev
, "multifunction", multifunction
);
1673 return DO_UPCAST(PCIDevice
, qdev
, dev
);
1676 PCIDevice
*pci_create_simple_multifunction(PCIBus
*bus
, int devfn
,
1680 PCIDevice
*dev
= pci_create_multifunction(bus
, devfn
, multifunction
, name
);
1681 qdev_init_nofail(&dev
->qdev
);
1685 PCIDevice
*pci_create(PCIBus
*bus
, int devfn
, const char *name
)
1687 return pci_create_multifunction(bus
, devfn
, false, name
);
1690 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
1692 return pci_create_simple_multifunction(bus
, devfn
, false, name
);
1695 static int pci_find_space(PCIDevice
*pdev
, uint8_t size
)
1697 int config_size
= pci_config_size(pdev
);
1698 int offset
= PCI_CONFIG_HEADER_SIZE
;
1700 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< config_size
; ++i
)
1703 else if (i
- offset
+ 1 == size
)
1708 static uint8_t pci_find_capability_list(PCIDevice
*pdev
, uint8_t cap_id
,
1713 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
))
1716 for (prev
= PCI_CAPABILITY_LIST
; (next
= pdev
->config
[prev
]);
1717 prev
= next
+ PCI_CAP_LIST_NEXT
)
1718 if (pdev
->config
[next
+ PCI_CAP_LIST_ID
] == cap_id
)
1726 static void pci_map_option_rom(PCIDevice
*pdev
, int region_num
, pcibus_t addr
, pcibus_t size
, int type
)
1728 cpu_register_physical_memory(addr
, size
, pdev
->rom_offset
);
1731 /* Add an option rom for the device */
1732 static int pci_add_option_rom(PCIDevice
*pdev
)
1741 if (strlen(pdev
->romfile
) == 0)
1744 if (!pdev
->rom_bar
) {
1746 * Load rom via fw_cfg instead of creating a rom bar,
1747 * for 0.11 compatibility.
1749 int class = pci_get_word(pdev
->config
+ PCI_CLASS_DEVICE
);
1750 if (class == 0x0300) {
1751 rom_add_vga(pdev
->romfile
);
1753 rom_add_option(pdev
->romfile
);
1758 path
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, pdev
->romfile
);
1760 path
= qemu_strdup(pdev
->romfile
);
1763 size
= get_image_size(path
);
1765 error_report("%s: failed to find romfile \"%s\"",
1766 __FUNCTION__
, pdev
->romfile
);
1769 if (size
& (size
- 1)) {
1770 size
= 1 << qemu_fls(size
);
1773 if (pdev
->qdev
.info
->vmsd
)
1774 snprintf(name
, sizeof(name
), "%s.rom", pdev
->qdev
.info
->vmsd
->name
);
1776 snprintf(name
, sizeof(name
), "%s.rom", pdev
->qdev
.info
->name
);
1777 pdev
->rom_offset
= qemu_ram_alloc(&pdev
->qdev
, name
, size
);
1779 ptr
= qemu_get_ram_ptr(pdev
->rom_offset
);
1780 load_image(path
, ptr
);
1783 pci_register_bar(pdev
, PCI_ROM_SLOT
, size
,
1784 0, pci_map_option_rom
);
1789 static void pci_del_option_rom(PCIDevice
*pdev
)
1791 if (!pdev
->rom_offset
)
1794 qemu_ram_free(pdev
->rom_offset
);
1795 pdev
->rom_offset
= 0;
1798 /* Reserve space and add capability to the linked list in pci config space */
1799 int pci_add_capability_at_offset(PCIDevice
*pdev
, uint8_t cap_id
,
1800 uint8_t offset
, uint8_t size
)
1802 uint8_t *config
= pdev
->config
+ offset
;
1803 config
[PCI_CAP_LIST_ID
] = cap_id
;
1804 config
[PCI_CAP_LIST_NEXT
] = pdev
->config
[PCI_CAPABILITY_LIST
];
1805 pdev
->config
[PCI_CAPABILITY_LIST
] = offset
;
1806 pdev
->config
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
1807 memset(pdev
->used
+ offset
, 0xFF, size
);
1808 /* Make capability read-only by default */
1809 memset(pdev
->wmask
+ offset
, 0, size
);
1810 /* Check capability by default */
1811 memset(pdev
->cmask
+ offset
, 0xFF, size
);
1815 /* Find and reserve space and add capability to the linked list
1816 * in pci config space */
1817 int pci_add_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1819 uint8_t offset
= pci_find_space(pdev
, size
);
1823 return pci_add_capability_at_offset(pdev
, cap_id
, offset
, size
);
1826 /* Unlink capability from the pci config space. */
1827 void pci_del_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1829 uint8_t prev
, offset
= pci_find_capability_list(pdev
, cap_id
, &prev
);
1832 pdev
->config
[prev
] = pdev
->config
[offset
+ PCI_CAP_LIST_NEXT
];
1833 /* Make capability writeable again */
1834 memset(pdev
->wmask
+ offset
, 0xff, size
);
1835 /* Clear cmask as device-specific registers can't be checked */
1836 memset(pdev
->cmask
+ offset
, 0, size
);
1837 memset(pdev
->used
+ offset
, 0, size
);
1839 if (!pdev
->config
[PCI_CAPABILITY_LIST
])
1840 pdev
->config
[PCI_STATUS
] &= ~PCI_STATUS_CAP_LIST
;
1843 /* Reserve space for capability at a known offset (to call after load). */
1844 void pci_reserve_capability(PCIDevice
*pdev
, uint8_t offset
, uint8_t size
)
1846 memset(pdev
->used
+ offset
, 0xff, size
);
1849 uint8_t pci_find_capability(PCIDevice
*pdev
, uint8_t cap_id
)
1851 return pci_find_capability_list(pdev
, cap_id
, NULL
);
1854 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
1856 PCIDevice
*d
= (PCIDevice
*)dev
;
1857 const pci_class_desc
*desc
;
1862 class = pci_get_word(d
->config
+ PCI_CLASS_DEVICE
);
1863 desc
= pci_class_descriptions
;
1864 while (desc
->desc
&& class != desc
->class)
1867 snprintf(ctxt
, sizeof(ctxt
), "%s", desc
->desc
);
1869 snprintf(ctxt
, sizeof(ctxt
), "Class %04x", class);
1872 monitor_printf(mon
, "%*sclass %s, addr %02x:%02x.%x, "
1873 "pci id %04x:%04x (sub %04x:%04x)\n",
1875 d
->config
[PCI_SECONDARY_BUS
],
1876 PCI_SLOT(d
->devfn
), PCI_FUNC(d
->devfn
),
1877 pci_get_word(d
->config
+ PCI_VENDOR_ID
),
1878 pci_get_word(d
->config
+ PCI_DEVICE_ID
),
1879 pci_get_word(d
->config
+ PCI_SUBSYSTEM_VENDOR_ID
),
1880 pci_get_word(d
->config
+ PCI_SUBSYSTEM_ID
));
1881 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1882 r
= &d
->io_regions
[i
];
1885 monitor_printf(mon
, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1886 " [0x%"FMT_PCIBUS
"]\n",
1888 i
, r
->type
& PCI_BASE_ADDRESS_SPACE_IO
? "i/o" : "mem",
1889 r
->addr
, r
->addr
+ r
->size
- 1);
1893 static char *pcibus_get_dev_path(DeviceState
*dev
)
1895 PCIDevice
*d
= (PCIDevice
*)dev
;
1898 snprintf(path
, sizeof(path
), "%04x:%02x:%02x.%x",
1899 pci_find_domain(d
->bus
), d
->config
[PCI_SECONDARY_BUS
],
1900 PCI_SLOT(d
->devfn
), PCI_FUNC(d
->devfn
));
1902 return strdup(path
);
1905 static PCIDeviceInfo bridge_info
= {
1906 .qdev
.name
= "pci-bridge",
1907 .qdev
.size
= sizeof(PCIBridge
),
1908 .init
= pci_bridge_initfn
,
1909 .exit
= pci_bridge_exitfn
,
1910 .config_write
= pci_bridge_write_config
,
1912 .qdev
.props
= (Property
[]) {
1913 DEFINE_PROP_HEX32("vendorid", PCIBridge
, vid
, 0),
1914 DEFINE_PROP_HEX32("deviceid", PCIBridge
, did
, 0),
1915 DEFINE_PROP_END_OF_LIST(),
1919 static void pci_register_devices(void)
1921 pci_qdev_register(&bridge_info
);
1924 device_init(pci_register_devices
)