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
;
46 PCIDevice
*devices
[256];
47 PCIDevice
*parent_dev
;
48 target_phys_addr_t mem_base
;
50 QLIST_HEAD(, PCIBus
) child
; /* this will be replaced by qdev later */
51 QLIST_ENTRY(PCIBus
) sibling
;/* this will be replaced by qdev later */
53 /* The bus IRQ state is the logical OR of the connected devices.
54 Keep a count of the number of devices with raised IRQs. */
59 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
61 static struct BusInfo pci_bus_info
= {
63 .size
= sizeof(PCIBus
),
64 .print_dev
= pcibus_dev_print
,
65 .props
= (Property
[]) {
66 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice
, devfn
, -1),
67 DEFINE_PROP_STRING("romfile", PCIDevice
, romfile
),
68 DEFINE_PROP_UINT32("rombar", PCIDevice
, rom_bar
, 1),
69 DEFINE_PROP_END_OF_LIST()
73 static void pci_update_mappings(PCIDevice
*d
);
74 static void pci_set_irq(void *opaque
, int irq_num
, int level
);
75 static int pci_add_option_rom(PCIDevice
*pdev
);
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
;
83 QLIST_ENTRY(PCIHostBus
) next
;
85 static QLIST_HEAD(, PCIHostBus
) host_buses
;
87 static const VMStateDescription vmstate_pcibus
= {
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),
99 static int pci_bar(PCIDevice
*d
, int reg
)
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
)
126 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
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
137 static void pci_update_irq_status(PCIDevice
*dev
)
139 if (dev
->irq_state
) {
140 dev
->config
[PCI_STATUS
] |= PCI_STATUS_INTERRUPT
;
142 dev
->config
[PCI_STATUS
] &= ~PCI_STATUS_INTERRUPT
;
146 static void pci_device_reset(PCIDevice
*dev
)
151 pci_update_irq_status(dev
);
152 dev
->config
[PCI_COMMAND
] &= ~(PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
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
) {
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
;
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
;
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
) {
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
;
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
)
220 bus
= qemu_mallocz(sizeof(*bus
));
221 bus
->qbus
.qdev_allocated
= 1;
222 pci_bus_new_inplace(bus
, parent
, name
, devfn_min
);
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
;
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 void pci_bus_set_mem_base(PCIBus
*bus
, target_phys_addr_t base
)
244 bus
->mem_base
= base
;
247 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
248 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
249 void *irq_opaque
, int devfn_min
, int nirq
)
253 bus
= pci_bus_new(parent
, name
, devfn_min
);
254 pci_bus_irqs(bus
, set_irq
, map_irq
, irq_opaque
, nirq
);
258 static void pci_register_secondary_bus(PCIBus
*parent
,
261 pci_map_irq_fn map_irq
,
264 qbus_create_inplace(&bus
->qbus
, &pci_bus_info
, &dev
->qdev
, name
);
265 bus
->map_irq
= map_irq
;
266 bus
->parent_dev
= dev
;
268 QLIST_INIT(&bus
->child
);
269 QLIST_INSERT_HEAD(&parent
->child
, bus
, sibling
);
272 static void pci_unregister_secondary_bus(PCIBus
*bus
)
274 assert(QLIST_EMPTY(&bus
->child
));
275 QLIST_REMOVE(bus
, sibling
);
278 int pci_bus_num(PCIBus
*s
)
281 return 0; /* pci host bridge */
282 return s
->parent_dev
->config
[PCI_SECONDARY_BUS
];
285 static int get_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
287 PCIDevice
*s
= container_of(pv
, PCIDevice
, config
);
291 assert(size
== pci_config_size(s
));
292 config
= qemu_malloc(size
);
294 qemu_get_buffer(f
, config
, size
);
295 for (i
= 0; i
< size
; ++i
) {
296 if ((config
[i
] ^ s
->config
[i
]) & s
->cmask
[i
] & ~s
->wmask
[i
]) {
301 memcpy(s
->config
, config
, size
);
303 pci_update_mappings(s
);
309 /* just put buffer */
310 static void put_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
312 const uint8_t **v
= pv
;
313 assert(size
== pci_config_size(container_of(pv
, PCIDevice
, config
)));
314 qemu_put_buffer(f
, *v
, size
);
317 static VMStateInfo vmstate_info_pci_config
= {
318 .name
= "pci config",
319 .get
= get_pci_config_device
,
320 .put
= put_pci_config_device
,
323 static int get_pci_irq_state(QEMUFile
*f
, void *pv
, size_t size
)
325 PCIDevice
*s
= container_of(pv
, PCIDevice
, config
);
326 uint32_t irq_state
[PCI_NUM_PINS
];
328 for (i
= 0; i
< PCI_NUM_PINS
; ++i
) {
329 irq_state
[i
] = qemu_get_be32(f
);
330 if (irq_state
[i
] != 0x1 && irq_state
[i
] != 0) {
331 fprintf(stderr
, "irq state %d: must be 0 or 1.\n",
337 for (i
= 0; i
< PCI_NUM_PINS
; ++i
) {
338 pci_set_irq_state(s
, i
, irq_state
[i
]);
344 static void put_pci_irq_state(QEMUFile
*f
, void *pv
, size_t size
)
347 PCIDevice
*s
= container_of(pv
, PCIDevice
, config
);
349 for (i
= 0; i
< PCI_NUM_PINS
; ++i
) {
350 qemu_put_be32(f
, pci_irq_state(s
, i
));
354 static VMStateInfo vmstate_info_pci_irq_state
= {
355 .name
= "pci irq state",
356 .get
= get_pci_irq_state
,
357 .put
= put_pci_irq_state
,
360 const VMStateDescription vmstate_pci_device
= {
363 .minimum_version_id
= 1,
364 .minimum_version_id_old
= 1,
365 .fields
= (VMStateField
[]) {
366 VMSTATE_INT32_LE(version_id
, PCIDevice
),
367 VMSTATE_BUFFER_UNSAFE_INFO(config
, PCIDevice
, 0,
368 vmstate_info_pci_config
,
369 PCI_CONFIG_SPACE_SIZE
),
370 VMSTATE_BUFFER_UNSAFE_INFO(irq_state
, PCIDevice
, 2,
371 vmstate_info_pci_irq_state
,
372 PCI_NUM_PINS
* sizeof(int32_t)),
373 VMSTATE_END_OF_LIST()
377 const VMStateDescription vmstate_pcie_device
= {
380 .minimum_version_id
= 1,
381 .minimum_version_id_old
= 1,
382 .fields
= (VMStateField
[]) {
383 VMSTATE_INT32_LE(version_id
, PCIDevice
),
384 VMSTATE_BUFFER_UNSAFE_INFO(config
, PCIDevice
, 0,
385 vmstate_info_pci_config
,
386 PCIE_CONFIG_SPACE_SIZE
),
387 VMSTATE_BUFFER_UNSAFE_INFO(irq_state
, PCIDevice
, 2,
388 vmstate_info_pci_irq_state
,
389 PCI_NUM_PINS
* sizeof(int32_t)),
390 VMSTATE_END_OF_LIST()
394 static inline const VMStateDescription
*pci_get_vmstate(PCIDevice
*s
)
396 return pci_is_express(s
) ? &vmstate_pcie_device
: &vmstate_pci_device
;
399 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
401 /* Clear interrupt status bit: it is implicit
402 * in irq_state which we are saving.
403 * This makes us compatible with old devices
404 * which never set or clear this bit. */
405 s
->config
[PCI_STATUS
] &= ~PCI_STATUS_INTERRUPT
;
406 vmstate_save_state(f
, pci_get_vmstate(s
), s
);
407 /* Restore the interrupt status bit. */
408 pci_update_irq_status(s
);
411 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
414 ret
= vmstate_load_state(f
, pci_get_vmstate(s
), s
, s
->version_id
);
415 /* Restore the interrupt status bit. */
416 pci_update_irq_status(s
);
420 static int pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
424 id
= (void*)(&pci_dev
->config
[PCI_SUBSYSTEM_VENDOR_ID
]);
425 id
[0] = cpu_to_le16(pci_default_sub_vendor_id
);
426 id
[1] = cpu_to_le16(pci_default_sub_device_id
);
431 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
433 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
438 unsigned long dom
= 0, bus
= 0;
442 val
= strtoul(p
, &e
, 16);
448 val
= strtoul(p
, &e
, 16);
455 val
= strtoul(p
, &e
, 16);
461 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
469 /* Note: QEMU doesn't implement domains other than 0 */
470 if (!pci_find_bus(pci_find_root_bus(dom
), bus
))
479 int pci_read_devaddr(Monitor
*mon
, const char *addr
, int *domp
, int *busp
,
482 /* strip legacy tag */
483 if (!strncmp(addr
, "pci_addr=", 9)) {
486 if (pci_parse_devaddr(addr
, domp
, busp
, slotp
)) {
487 monitor_printf(mon
, "Invalid pci address\n");
493 PCIBus
*pci_get_bus_devfn(int *devfnp
, const char *devaddr
)
500 return pci_find_bus(pci_find_root_bus(0), 0);
503 if (pci_parse_devaddr(devaddr
, &dom
, &bus
, &slot
) < 0) {
508 return pci_find_bus(pci_find_root_bus(0), bus
);
511 static void pci_init_cmask(PCIDevice
*dev
)
513 pci_set_word(dev
->cmask
+ PCI_VENDOR_ID
, 0xffff);
514 pci_set_word(dev
->cmask
+ PCI_DEVICE_ID
, 0xffff);
515 dev
->cmask
[PCI_STATUS
] = PCI_STATUS_CAP_LIST
;
516 dev
->cmask
[PCI_REVISION_ID
] = 0xff;
517 dev
->cmask
[PCI_CLASS_PROG
] = 0xff;
518 pci_set_word(dev
->cmask
+ PCI_CLASS_DEVICE
, 0xffff);
519 dev
->cmask
[PCI_HEADER_TYPE
] = 0xff;
520 dev
->cmask
[PCI_CAPABILITY_LIST
] = 0xff;
523 static void pci_init_wmask(PCIDevice
*dev
)
525 int config_size
= pci_config_size(dev
);
527 dev
->wmask
[PCI_CACHE_LINE_SIZE
] = 0xff;
528 dev
->wmask
[PCI_INTERRUPT_LINE
] = 0xff;
529 pci_set_word(dev
->wmask
+ PCI_COMMAND
,
530 PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
|
531 PCI_COMMAND_INTX_DISABLE
);
533 memset(dev
->wmask
+ PCI_CONFIG_HEADER_SIZE
, 0xff,
534 config_size
- PCI_CONFIG_HEADER_SIZE
);
537 static void pci_init_wmask_bridge(PCIDevice
*d
)
539 /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
540 PCI_SEC_LETENCY_TIMER */
541 memset(d
->wmask
+ PCI_PRIMARY_BUS
, 0xff, 4);
544 d
->wmask
[PCI_IO_BASE
] = PCI_IO_RANGE_MASK
& 0xff;
545 d
->wmask
[PCI_IO_LIMIT
] = PCI_IO_RANGE_MASK
& 0xff;
546 pci_set_word(d
->wmask
+ PCI_MEMORY_BASE
,
547 PCI_MEMORY_RANGE_MASK
& 0xffff);
548 pci_set_word(d
->wmask
+ PCI_MEMORY_LIMIT
,
549 PCI_MEMORY_RANGE_MASK
& 0xffff);
550 pci_set_word(d
->wmask
+ PCI_PREF_MEMORY_BASE
,
551 PCI_PREF_RANGE_MASK
& 0xffff);
552 pci_set_word(d
->wmask
+ PCI_PREF_MEMORY_LIMIT
,
553 PCI_PREF_RANGE_MASK
& 0xffff);
555 /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
556 memset(d
->wmask
+ PCI_PREF_BASE_UPPER32
, 0xff, 8);
558 pci_set_word(d
->wmask
+ PCI_BRIDGE_CONTROL
, 0xffff);
561 static void pci_config_alloc(PCIDevice
*pci_dev
)
563 int config_size
= pci_config_size(pci_dev
);
565 pci_dev
->config
= qemu_mallocz(config_size
);
566 pci_dev
->cmask
= qemu_mallocz(config_size
);
567 pci_dev
->wmask
= qemu_mallocz(config_size
);
568 pci_dev
->used
= qemu_mallocz(config_size
);
571 static void pci_config_free(PCIDevice
*pci_dev
)
573 qemu_free(pci_dev
->config
);
574 qemu_free(pci_dev
->cmask
);
575 qemu_free(pci_dev
->wmask
);
576 qemu_free(pci_dev
->used
);
579 /* -1 for devfn means auto assign */
580 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
581 const char *name
, int devfn
,
582 PCIConfigReadFunc
*config_read
,
583 PCIConfigWriteFunc
*config_write
,
587 for(devfn
= bus
->devfn_min
; devfn
< ARRAY_SIZE(bus
->devices
);
589 if (!bus
->devices
[devfn
])
592 qemu_error("PCI: no devfn available for %s, all in use\n", name
);
595 } else if (bus
->devices
[devfn
]) {
596 qemu_error("PCI: devfn %d not available for %s, in use by %s\n", devfn
,
597 name
, bus
->devices
[devfn
]->name
);
601 pci_dev
->devfn
= devfn
;
602 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
603 pci_dev
->irq_state
= 0;
604 pci_config_alloc(pci_dev
);
606 header_type
&= ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
607 if (header_type
== PCI_HEADER_TYPE_NORMAL
) {
608 pci_set_default_subsystem_id(pci_dev
);
610 pci_init_cmask(pci_dev
);
611 pci_init_wmask(pci_dev
);
612 if (header_type
== PCI_HEADER_TYPE_BRIDGE
) {
613 pci_init_wmask_bridge(pci_dev
);
617 config_read
= pci_default_read_config
;
619 config_write
= pci_default_write_config
;
620 pci_dev
->config_read
= config_read
;
621 pci_dev
->config_write
= config_write
;
622 bus
->devices
[devfn
] = pci_dev
;
623 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, PCI_NUM_PINS
);
624 pci_dev
->version_id
= 2; /* Current pci device vmstate version */
628 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
629 int instance_size
, int devfn
,
630 PCIConfigReadFunc
*config_read
,
631 PCIConfigWriteFunc
*config_write
)
635 pci_dev
= qemu_mallocz(instance_size
);
636 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
637 config_read
, config_write
,
638 PCI_HEADER_TYPE_NORMAL
);
639 if (pci_dev
== NULL
) {
640 hw_error("PCI: can't register device\n");
645 static target_phys_addr_t
pci_to_cpu_addr(PCIBus
*bus
,
646 target_phys_addr_t addr
)
648 return addr
+ bus
->mem_base
;
651 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
656 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
657 r
= &pci_dev
->io_regions
[i
];
658 if (!r
->size
|| r
->addr
== PCI_BAR_UNMAPPED
)
660 if (r
->type
== PCI_BASE_ADDRESS_SPACE_IO
) {
661 isa_unassign_ioport(r
->addr
, r
->filtered_size
);
663 cpu_register_physical_memory(pci_to_cpu_addr(pci_dev
->bus
,
671 static int pci_unregister_device(DeviceState
*dev
)
673 PCIDevice
*pci_dev
= DO_UPCAST(PCIDevice
, qdev
, dev
);
674 PCIDeviceInfo
*info
= DO_UPCAST(PCIDeviceInfo
, qdev
, dev
->info
);
678 ret
= info
->exit(pci_dev
);
682 pci_unregister_io_regions(pci_dev
);
684 qemu_free_irqs(pci_dev
->irq
);
685 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
686 pci_config_free(pci_dev
);
690 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
691 pcibus_t size
, int type
,
692 PCIMapIORegionFunc
*map_func
)
698 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
701 if (size
& (size
-1)) {
702 fprintf(stderr
, "ERROR: PCI region size must be pow2 "
703 "type=0x%x, size=0x%"FMT_PCIBUS
"\n", type
, size
);
707 r
= &pci_dev
->io_regions
[region_num
];
708 r
->addr
= PCI_BAR_UNMAPPED
;
710 r
->filtered_size
= size
;
712 r
->map_func
= map_func
;
715 addr
= pci_bar(pci_dev
, region_num
);
716 if (region_num
== PCI_ROM_SLOT
) {
717 /* ROM enable bit is writeable */
718 wmask
|= PCI_ROM_ADDRESS_ENABLE
;
720 pci_set_long(pci_dev
->config
+ addr
, type
);
721 if (!(r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) &&
722 r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
723 pci_set_quad(pci_dev
->wmask
+ addr
, wmask
);
724 pci_set_quad(pci_dev
->cmask
+ addr
, ~0ULL);
726 pci_set_long(pci_dev
->wmask
+ addr
, wmask
& 0xffffffff);
727 pci_set_long(pci_dev
->cmask
+ addr
, 0xffffffff);
731 static uint32_t pci_config_get_io_base(PCIDevice
*d
,
732 uint32_t base
, uint32_t base_upper16
)
736 val
= ((uint32_t)d
->config
[base
] & PCI_IO_RANGE_MASK
) << 8;
737 if (d
->config
[base
] & PCI_IO_RANGE_TYPE_32
) {
738 val
|= (uint32_t)pci_get_word(d
->config
+ base_upper16
) << 16;
743 static pcibus_t
pci_config_get_memory_base(PCIDevice
*d
, uint32_t base
)
745 return ((pcibus_t
)pci_get_word(d
->config
+ base
) & PCI_MEMORY_RANGE_MASK
)
749 static pcibus_t
pci_config_get_pref_base(PCIDevice
*d
,
750 uint32_t base
, uint32_t upper
)
755 tmp
= (pcibus_t
)pci_get_word(d
->config
+ base
);
756 val
= (tmp
& PCI_PREF_RANGE_MASK
) << 16;
757 if (tmp
& PCI_PREF_RANGE_TYPE_64
) {
758 val
|= (pcibus_t
)pci_get_long(d
->config
+ upper
) << 32;
763 static pcibus_t
pci_bridge_get_base(PCIDevice
*bridge
, uint8_t type
)
766 if (type
& PCI_BASE_ADDRESS_SPACE_IO
) {
767 base
= pci_config_get_io_base(bridge
,
768 PCI_IO_BASE
, PCI_IO_BASE_UPPER16
);
770 if (type
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
771 base
= pci_config_get_pref_base(
772 bridge
, PCI_PREF_MEMORY_BASE
, PCI_PREF_BASE_UPPER32
);
774 base
= pci_config_get_memory_base(bridge
, PCI_MEMORY_BASE
);
781 static pcibus_t
pci_bridge_get_limit(PCIDevice
*bridge
, uint8_t type
)
784 if (type
& PCI_BASE_ADDRESS_SPACE_IO
) {
785 limit
= pci_config_get_io_base(bridge
,
786 PCI_IO_LIMIT
, PCI_IO_LIMIT_UPPER16
);
787 limit
|= 0xfff; /* PCI bridge spec 3.2.5.6. */
789 if (type
& PCI_BASE_ADDRESS_MEM_PREFETCH
) {
790 limit
= pci_config_get_pref_base(
791 bridge
, PCI_PREF_MEMORY_LIMIT
, PCI_PREF_LIMIT_UPPER32
);
793 limit
= pci_config_get_memory_base(bridge
, PCI_MEMORY_LIMIT
);
795 limit
|= 0xfffff; /* PCI bridge spec 3.2.5.{1, 8}. */
800 static void pci_bridge_filter(PCIDevice
*d
, pcibus_t
*addr
, pcibus_t
*size
,
803 pcibus_t base
= *addr
;
804 pcibus_t limit
= *addr
+ *size
- 1;
807 for (br
= d
->bus
->parent_dev
; br
; br
= br
->bus
->parent_dev
) {
808 uint16_t cmd
= pci_get_word(d
->config
+ PCI_COMMAND
);
810 if (type
& PCI_BASE_ADDRESS_SPACE_IO
) {
811 if (!(cmd
& PCI_COMMAND_IO
)) {
815 if (!(cmd
& PCI_COMMAND_MEMORY
)) {
820 base
= MAX(base
, pci_bridge_get_base(br
, type
));
821 limit
= MIN(limit
, pci_bridge_get_limit(br
, type
));
828 *size
= limit
- base
+ 1;
831 *addr
= PCI_BAR_UNMAPPED
;
835 static pcibus_t
pci_bar_address(PCIDevice
*d
,
836 int reg
, uint8_t type
, pcibus_t size
)
838 pcibus_t new_addr
, last_addr
;
839 int bar
= pci_bar(d
, reg
);
840 uint16_t cmd
= pci_get_word(d
->config
+ PCI_COMMAND
);
842 if (type
& PCI_BASE_ADDRESS_SPACE_IO
) {
843 if (!(cmd
& PCI_COMMAND_IO
)) {
844 return PCI_BAR_UNMAPPED
;
846 new_addr
= pci_get_long(d
->config
+ bar
) & ~(size
- 1);
847 last_addr
= new_addr
+ size
- 1;
848 /* NOTE: we have only 64K ioports on PC */
849 if (last_addr
<= new_addr
|| new_addr
== 0 || last_addr
> UINT16_MAX
) {
850 return PCI_BAR_UNMAPPED
;
855 if (!(cmd
& PCI_COMMAND_MEMORY
)) {
856 return PCI_BAR_UNMAPPED
;
858 if (type
& PCI_BASE_ADDRESS_MEM_TYPE_64
) {
859 new_addr
= pci_get_quad(d
->config
+ bar
);
861 new_addr
= pci_get_long(d
->config
+ bar
);
863 /* the ROM slot has a specific enable bit */
864 if (reg
== PCI_ROM_SLOT
&& !(new_addr
& PCI_ROM_ADDRESS_ENABLE
)) {
865 return PCI_BAR_UNMAPPED
;
867 new_addr
&= ~(size
- 1);
868 last_addr
= new_addr
+ size
- 1;
869 /* NOTE: we do not support wrapping */
870 /* XXX: as we cannot support really dynamic
871 mappings, we handle specific values as invalid
873 if (last_addr
<= new_addr
|| new_addr
== 0 ||
874 last_addr
== PCI_BAR_UNMAPPED
) {
875 return PCI_BAR_UNMAPPED
;
878 /* Now pcibus_t is 64bit.
879 * Check if 32 bit BAR wraps around explicitly.
880 * Without this, PC ide doesn't work well.
881 * TODO: remove this work around.
883 if (!(type
& PCI_BASE_ADDRESS_MEM_TYPE_64
) && last_addr
>= UINT32_MAX
) {
884 return PCI_BAR_UNMAPPED
;
888 * OS is allowed to set BAR beyond its addressable
889 * bits. For example, 32 bit OS can set 64bit bar
890 * to >4G. Check it. TODO: we might need to support
891 * it in the future for e.g. PAE.
893 if (last_addr
>= TARGET_PHYS_ADDR_MAX
) {
894 return PCI_BAR_UNMAPPED
;
900 static void pci_update_mappings(PCIDevice
*d
)
904 pcibus_t new_addr
, filtered_size
;
906 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
907 r
= &d
->io_regions
[i
];
909 /* this region isn't registered */
913 new_addr
= pci_bar_address(d
, i
, r
->type
, r
->size
);
915 /* bridge filtering */
916 filtered_size
= r
->size
;
917 if (new_addr
!= PCI_BAR_UNMAPPED
) {
918 pci_bridge_filter(d
, &new_addr
, &filtered_size
, r
->type
);
921 /* This bar isn't changed */
922 if (new_addr
== r
->addr
&& filtered_size
== r
->filtered_size
)
925 /* now do the real mapping */
926 if (r
->addr
!= PCI_BAR_UNMAPPED
) {
927 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
929 /* NOTE: specific hack for IDE in PC case:
930 only one byte must be mapped. */
931 class = pci_get_word(d
->config
+ PCI_CLASS_DEVICE
);
932 if (class == 0x0101 && r
->size
== 4) {
933 isa_unassign_ioport(r
->addr
+ 2, 1);
935 isa_unassign_ioport(r
->addr
, r
->filtered_size
);
938 cpu_register_physical_memory(pci_to_cpu_addr(d
->bus
, r
->addr
),
941 qemu_unregister_coalesced_mmio(r
->addr
, r
->filtered_size
);
945 r
->filtered_size
= filtered_size
;
946 if (r
->addr
!= PCI_BAR_UNMAPPED
) {
948 * TODO: currently almost all the map funcions assumes
949 * filtered_size == size and addr & ~(size - 1) == addr.
950 * However with bridge filtering, they aren't always true.
951 * Teach them such cases, such that filtered_size < size and
952 * addr & (size - 1) != 0.
954 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
955 r
->map_func(d
, i
, r
->addr
, r
->filtered_size
, r
->type
);
957 r
->map_func(d
, i
, pci_to_cpu_addr(d
->bus
, r
->addr
),
958 r
->filtered_size
, r
->type
);
964 static inline int pci_irq_disabled(PCIDevice
*d
)
966 return pci_get_word(d
->config
+ PCI_COMMAND
) & PCI_COMMAND_INTX_DISABLE
;
969 /* Called after interrupt disabled field update in config space,
970 * assert/deassert interrupts if necessary.
971 * Gets original interrupt disable bit value (before update). */
972 static void pci_update_irq_disabled(PCIDevice
*d
, int was_irq_disabled
)
974 int i
, disabled
= pci_irq_disabled(d
);
975 if (disabled
== was_irq_disabled
)
977 for (i
= 0; i
< PCI_NUM_PINS
; ++i
) {
978 int state
= pci_irq_state(d
, i
);
979 pci_change_irq_level(d
, i
, disabled
? -state
: state
);
983 uint32_t pci_default_read_config(PCIDevice
*d
,
984 uint32_t address
, int len
)
987 assert(len
== 1 || len
== 2 || len
== 4);
988 len
= MIN(len
, pci_config_size(d
) - address
);
989 memcpy(&val
, d
->config
+ address
, len
);
990 return le32_to_cpu(val
);
993 void pci_default_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int l
)
995 int i
, was_irq_disabled
= pci_irq_disabled(d
);
996 uint32_t config_size
= pci_config_size(d
);
998 for (i
= 0; i
< l
&& addr
+ i
< config_size
; val
>>= 8, ++i
) {
999 uint8_t wmask
= d
->wmask
[addr
+ i
];
1000 d
->config
[addr
+ i
] = (d
->config
[addr
+ i
] & ~wmask
) | (val
& wmask
);
1002 if (ranges_overlap(addr
, l
, PCI_BASE_ADDRESS_0
, 24) ||
1003 ranges_overlap(addr
, l
, PCI_ROM_ADDRESS
, 4) ||
1004 ranges_overlap(addr
, l
, PCI_ROM_ADDRESS1
, 4) ||
1005 range_covers_byte(addr
, l
, PCI_COMMAND
))
1006 pci_update_mappings(d
);
1008 if (range_covers_byte(addr
, l
, PCI_COMMAND
))
1009 pci_update_irq_disabled(d
, was_irq_disabled
);
1012 /***********************************************************/
1013 /* generic PCI irq support */
1015 /* 0 <= irq_num <= 3. level must be 0 or 1 */
1016 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
1018 PCIDevice
*pci_dev
= opaque
;
1021 change
= level
- pci_irq_state(pci_dev
, irq_num
);
1025 pci_set_irq_state(pci_dev
, irq_num
, level
);
1026 pci_update_irq_status(pci_dev
);
1027 if (pci_irq_disabled(pci_dev
))
1029 pci_change_irq_level(pci_dev
, irq_num
, change
);
1032 /***********************************************************/
1033 /* monitor info on PCI */
1040 static const pci_class_desc pci_class_descriptions
[] =
1042 { 0x0100, "SCSI controller"},
1043 { 0x0101, "IDE controller"},
1044 { 0x0102, "Floppy controller"},
1045 { 0x0103, "IPI controller"},
1046 { 0x0104, "RAID controller"},
1047 { 0x0106, "SATA controller"},
1048 { 0x0107, "SAS controller"},
1049 { 0x0180, "Storage controller"},
1050 { 0x0200, "Ethernet controller"},
1051 { 0x0201, "Token Ring controller"},
1052 { 0x0202, "FDDI controller"},
1053 { 0x0203, "ATM controller"},
1054 { 0x0280, "Network controller"},
1055 { 0x0300, "VGA controller"},
1056 { 0x0301, "XGA controller"},
1057 { 0x0302, "3D controller"},
1058 { 0x0380, "Display controller"},
1059 { 0x0400, "Video controller"},
1060 { 0x0401, "Audio controller"},
1062 { 0x0480, "Multimedia controller"},
1063 { 0x0500, "RAM controller"},
1064 { 0x0501, "Flash controller"},
1065 { 0x0580, "Memory controller"},
1066 { 0x0600, "Host bridge"},
1067 { 0x0601, "ISA bridge"},
1068 { 0x0602, "EISA bridge"},
1069 { 0x0603, "MC bridge"},
1070 { 0x0604, "PCI bridge"},
1071 { 0x0605, "PCMCIA bridge"},
1072 { 0x0606, "NUBUS bridge"},
1073 { 0x0607, "CARDBUS bridge"},
1074 { 0x0608, "RACEWAY bridge"},
1075 { 0x0680, "Bridge"},
1076 { 0x0c03, "USB controller"},
1080 static void pci_for_each_device_under_bus(PCIBus
*bus
,
1081 void (*fn
)(PCIBus
*b
, PCIDevice
*d
))
1086 for(devfn
= 0; devfn
< ARRAY_SIZE(bus
->devices
); devfn
++) {
1087 d
= bus
->devices
[devfn
];
1094 void pci_for_each_device(PCIBus
*bus
, int bus_num
,
1095 void (*fn
)(PCIBus
*b
, PCIDevice
*d
))
1097 bus
= pci_find_bus(bus
, bus_num
);
1100 pci_for_each_device_under_bus(bus
, fn
);
1104 static void pci_device_print(Monitor
*mon
, QDict
*device
)
1108 uint64_t addr
, size
;
1110 monitor_printf(mon
, " Bus %2" PRId64
", ", qdict_get_int(device
, "bus"));
1111 monitor_printf(mon
, "device %3" PRId64
", function %" PRId64
":\n",
1112 qdict_get_int(device
, "slot"),
1113 qdict_get_int(device
, "function"));
1114 monitor_printf(mon
, " ");
1116 qdict
= qdict_get_qdict(device
, "class_info");
1117 if (qdict_haskey(qdict
, "desc")) {
1118 monitor_printf(mon
, "%s", qdict_get_str(qdict
, "desc"));
1120 monitor_printf(mon
, "Class %04" PRId64
, qdict_get_int(qdict
, "class"));
1123 qdict
= qdict_get_qdict(device
, "id");
1124 monitor_printf(mon
, ": PCI device %04" PRIx64
":%04" PRIx64
"\n",
1125 qdict_get_int(qdict
, "device"),
1126 qdict_get_int(qdict
, "vendor"));
1128 if (qdict_haskey(device
, "irq")) {
1129 monitor_printf(mon
, " IRQ %" PRId64
".\n",
1130 qdict_get_int(device
, "irq"));
1133 if (qdict_haskey(device
, "pci_bridge")) {
1136 qdict
= qdict_get_qdict(device
, "pci_bridge");
1138 info
= qdict_get_qdict(qdict
, "bus");
1139 monitor_printf(mon
, " BUS %" PRId64
".\n",
1140 qdict_get_int(info
, "number"));
1141 monitor_printf(mon
, " secondary bus %" PRId64
".\n",
1142 qdict_get_int(info
, "secondary"));
1143 monitor_printf(mon
, " subordinate bus %" PRId64
".\n",
1144 qdict_get_int(info
, "subordinate"));
1146 info
= qdict_get_qdict(qdict
, "io_range");
1147 monitor_printf(mon
, " IO range [0x%04"PRIx64
", 0x%04"PRIx64
"]\n",
1148 qdict_get_int(info
, "base"),
1149 qdict_get_int(info
, "limit"));
1151 info
= qdict_get_qdict(qdict
, "memory_range");
1153 " memory range [0x%08"PRIx64
", 0x%08"PRIx64
"]\n",
1154 qdict_get_int(info
, "base"),
1155 qdict_get_int(info
, "limit"));
1157 info
= qdict_get_qdict(qdict
, "prefetchable_range");
1158 monitor_printf(mon
, " prefetchable memory range "
1159 "[0x%08"PRIx64
", 0x%08"PRIx64
"]\n",
1160 qdict_get_int(info
, "base"),
1161 qdict_get_int(info
, "limit"));
1164 QLIST_FOREACH_ENTRY(qdict_get_qlist(device
, "regions"), entry
) {
1165 qdict
= qobject_to_qdict(qlist_entry_obj(entry
));
1166 monitor_printf(mon
, " BAR%d: ", (int) qdict_get_int(qdict
, "bar"));
1168 addr
= qdict_get_int(qdict
, "address");
1169 size
= qdict_get_int(qdict
, "size");
1171 if (!strcmp(qdict_get_str(qdict
, "type"), "io")) {
1172 monitor_printf(mon
, "I/O at 0x%04"FMT_PCIBUS
1173 " [0x%04"FMT_PCIBUS
"].\n",
1174 addr
, addr
+ size
- 1);
1176 monitor_printf(mon
, "%d bit%s memory at 0x%08"FMT_PCIBUS
1177 " [0x%08"FMT_PCIBUS
"].\n",
1178 qdict_get_bool(qdict
, "mem_type_64") ? 64 : 32,
1179 qdict_get_bool(qdict
, "prefetch") ?
1180 " prefetchable" : "", addr
, addr
+ size
- 1);
1184 monitor_printf(mon
, " id \"%s\"\n", qdict_get_str(device
, "qdev_id"));
1186 /* TODO: PCI bridge devices */
1189 void do_pci_info_print(Monitor
*mon
, const QObject
*data
)
1191 QListEntry
*bus
, *dev
;
1193 QLIST_FOREACH_ENTRY(qobject_to_qlist(data
), bus
) {
1194 QDict
*qdict
= qobject_to_qdict(qlist_entry_obj(bus
));
1195 QLIST_FOREACH_ENTRY(qdict_get_qlist(qdict
, "devices"), dev
) {
1196 pci_device_print(mon
, qobject_to_qdict(qlist_entry_obj(dev
)));
1201 static QObject
*pci_get_dev_class(const PCIDevice
*dev
)
1204 const pci_class_desc
*desc
;
1206 class = pci_get_word(dev
->config
+ PCI_CLASS_DEVICE
);
1207 desc
= pci_class_descriptions
;
1208 while (desc
->desc
&& class != desc
->class)
1212 return qobject_from_jsonf("{ 'desc': %s, 'class': %d }",
1215 return qobject_from_jsonf("{ 'class': %d }", class);
1219 static QObject
*pci_get_dev_id(const PCIDevice
*dev
)
1221 return qobject_from_jsonf("{ 'device': %d, 'vendor': %d }",
1222 pci_get_word(dev
->config
+ PCI_VENDOR_ID
),
1223 pci_get_word(dev
->config
+ PCI_DEVICE_ID
));
1226 static QObject
*pci_get_regions_list(const PCIDevice
*dev
)
1229 QList
*regions_list
;
1231 regions_list
= qlist_new();
1233 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1235 const PCIIORegion
*r
= &dev
->io_regions
[i
];
1241 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
1242 obj
= qobject_from_jsonf("{ 'bar': %d, 'type': 'io', "
1243 "'address': %" PRId64
", "
1244 "'size': %" PRId64
" }",
1245 i
, r
->addr
, r
->size
);
1247 int mem_type_64
= r
->type
& PCI_BASE_ADDRESS_MEM_TYPE_64
;
1249 obj
= qobject_from_jsonf("{ 'bar': %d, 'type': 'memory', "
1250 "'mem_type_64': %i, 'prefetch': %i, "
1251 "'address': %" PRId64
", "
1252 "'size': %" PRId64
" }",
1254 r
->type
& PCI_BASE_ADDRESS_MEM_PREFETCH
,
1258 qlist_append_obj(regions_list
, obj
);
1261 return QOBJECT(regions_list
);
1264 static QObject
*pci_get_dev_dict(PCIDevice
*dev
, int bus_num
)
1269 obj
= qobject_from_jsonf("{ 'bus': %d, 'slot': %d, 'function': %d," "'class_info': %p, 'id': %p, 'regions': %p,"
1272 PCI_SLOT(dev
->devfn
), PCI_FUNC(dev
->devfn
),
1273 pci_get_dev_class(dev
), pci_get_dev_id(dev
),
1274 pci_get_regions_list(dev
),
1275 dev
->qdev
.id
? dev
->qdev
.id
: "");
1277 if (dev
->config
[PCI_INTERRUPT_PIN
] != 0) {
1278 QDict
*qdict
= qobject_to_qdict(obj
);
1279 qdict_put(qdict
, "irq", qint_from_int(dev
->config
[PCI_INTERRUPT_LINE
]));
1282 class = pci_get_word(dev
->config
+ PCI_CLASS_DEVICE
);
1283 if (class == 0x0604) {
1285 QObject
*pci_bridge
;
1287 pci_bridge
= qobject_from_jsonf("{ 'bus': "
1288 "{ 'number': %d, 'secondary': %d, 'subordinate': %d }, "
1289 "'io_range': { 'base': %" PRId64
", 'limit': %" PRId64
"}, "
1290 "'memory_range': { 'base': %" PRId64
", 'limit': %" PRId64
"}, "
1291 "'prefetchable_range': { 'base': %" PRId64
", 'limit': %" PRId64
"} }",
1292 dev
->config
[0x19], dev
->config
[PCI_SECONDARY_BUS
],
1293 dev
->config
[PCI_SUBORDINATE_BUS
],
1294 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_IO
),
1295 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_IO
),
1296 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
),
1297 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
),
1298 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
|
1299 PCI_BASE_ADDRESS_MEM_PREFETCH
),
1300 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
|
1301 PCI_BASE_ADDRESS_MEM_PREFETCH
));
1303 qdict
= qobject_to_qdict(obj
);
1304 qdict_put_obj(qdict
, "pci_bridge", pci_bridge
);
1310 static QObject
*pci_get_devices_list(PCIBus
*bus
, int bus_num
)
1316 dev_list
= qlist_new();
1318 for (devfn
= 0; devfn
< ARRAY_SIZE(bus
->devices
); devfn
++) {
1319 dev
= bus
->devices
[devfn
];
1321 qlist_append_obj(dev_list
, pci_get_dev_dict(dev
, bus_num
));
1325 return QOBJECT(dev_list
);
1328 static QObject
*pci_get_bus_dict(PCIBus
*bus
, int bus_num
)
1330 bus
= pci_find_bus(bus
, bus_num
);
1332 return qobject_from_jsonf("{ 'bus': %d, 'devices': %p }",
1333 bus_num
, pci_get_devices_list(bus
, bus_num
));
1340 * do_pci_info(): PCI buses and devices information
1342 * The returned QObject is a QList of all buses. Each bus is
1343 * represented by a QDict, which has a key with a QList of all
1344 * PCI devices attached to it. Each device is represented by
1347 * The bus QDict contains the following:
1349 * - "bus": bus number
1350 * - "devices": a QList of QDicts, each QDict represents a PCI
1353 * The PCI device QDict contains the following:
1355 * - "bus": identical to the parent's bus number
1356 * - "slot": slot number
1357 * - "function": function number
1358 * - "class_info": a QDict containing:
1359 * - "desc": device class description (optional)
1360 * - "class": device class number
1361 * - "id": a QDict containing:
1362 * - "device": device ID
1363 * - "vendor": vendor ID
1364 * - "irq": device's IRQ if assigned (optional)
1365 * - "qdev_id": qdev id string
1366 * - "pci_bridge": It's a QDict, only present if this device is a
1367 * PCI bridge, contains:
1368 * - "bus": bus number
1369 * - "secondary": secondary bus number
1370 * - "subordinate": subordinate bus number
1371 * - "io_range": a QDict with memory range information
1372 * - "memory_range": a QDict with memory range information
1373 * - "prefetchable_range": a QDict with memory range information
1374 * - "regions": a QList of QDicts, each QDict represents a
1375 * memory region of this device
1377 * The memory range QDict contains the following:
1379 * - "base": base memory address
1380 * - "limit": limit value
1382 * The region QDict can be an I/O region or a memory region,
1383 * an I/O region QDict contains the following:
1386 * - "bar": BAR number
1387 * - "address": memory address
1388 * - "size": memory size
1390 * A memory region QDict contains the following:
1392 * - "type": "memory"
1393 * - "bar": BAR number
1394 * - "address": memory address
1395 * - "size": memory size
1396 * - "mem_type_64": true or false
1397 * - "prefetch": true or false
1399 void do_pci_info(Monitor
*mon
, QObject
**ret_data
)
1402 struct PCIHostBus
*host
;
1404 bus_list
= qlist_new();
1406 QLIST_FOREACH(host
, &host_buses
, next
) {
1407 QObject
*obj
= pci_get_bus_dict(host
->bus
, 0);
1409 qlist_append_obj(bus_list
, obj
);
1413 *ret_data
= QOBJECT(bus_list
);
1416 static const char * const pci_nic_models
[] = {
1428 static const char * const pci_nic_names
[] = {
1440 /* Initialize a PCI NIC. */
1441 /* FIXME callers should check for failure, but don't */
1442 PCIDevice
*pci_nic_init(NICInfo
*nd
, const char *default_model
,
1443 const char *default_devaddr
)
1445 const char *devaddr
= nd
->devaddr
? nd
->devaddr
: default_devaddr
;
1452 i
= qemu_find_nic_model(nd
, pci_nic_models
, default_model
);
1456 bus
= pci_get_bus_devfn(&devfn
, devaddr
);
1458 qemu_error("Invalid PCI device address %s for device %s\n",
1459 devaddr
, pci_nic_names
[i
]);
1463 pci_dev
= pci_create(bus
, devfn
, pci_nic_names
[i
]);
1464 dev
= &pci_dev
->qdev
;
1466 dev
->id
= qemu_strdup(nd
->name
);
1467 qdev_set_nic_properties(dev
, nd
);
1468 if (qdev_init(dev
) < 0)
1473 PCIDevice
*pci_nic_init_nofail(NICInfo
*nd
, const char *default_model
,
1474 const char *default_devaddr
)
1478 if (qemu_show_nic_models(nd
->model
, pci_nic_models
))
1481 res
= pci_nic_init(nd
, default_model
, default_devaddr
);
1495 static void pci_bridge_update_mappings_fn(PCIBus
*b
, PCIDevice
*d
)
1497 pci_update_mappings(d
);
1500 static void pci_bridge_update_mappings(PCIBus
*b
)
1504 pci_for_each_device_under_bus(b
, pci_bridge_update_mappings_fn
);
1506 QLIST_FOREACH(child
, &b
->child
, sibling
) {
1507 pci_bridge_update_mappings(child
);
1511 static void pci_bridge_write_config(PCIDevice
*d
,
1512 uint32_t address
, uint32_t val
, int len
)
1514 pci_default_write_config(d
, address
, val
, len
);
1516 if (/* io base/limit */
1517 ranges_overlap(address
, len
, PCI_IO_BASE
, 2) ||
1519 /* memory base/limit, prefetchable base/limit and
1520 io base/limit upper 16 */
1521 ranges_overlap(address
, len
, PCI_MEMORY_BASE
, 20)) {
1522 pci_bridge_update_mappings(d
->bus
);
1526 PCIBus
*pci_find_bus(PCIBus
*bus
, int bus_num
)
1533 if (pci_bus_num(bus
) == bus_num
) {
1538 QLIST_FOREACH(sec
, &bus
->child
, sibling
) {
1540 if (!bus
->parent_dev
/* pci host bridge */
1541 || (pci_bus_num(sec
) <= bus_num
&&
1542 bus
->parent_dev
->config
[PCI_SUBORDINATE_BUS
])) {
1543 return pci_find_bus(sec
, bus_num
);
1550 PCIDevice
*pci_find_device(PCIBus
*bus
, int bus_num
, int slot
, int function
)
1552 bus
= pci_find_bus(bus
, bus_num
);
1557 return bus
->devices
[PCI_DEVFN(slot
, function
)];
1560 static int pci_bridge_initfn(PCIDevice
*dev
)
1562 PCIBridge
*s
= DO_UPCAST(PCIBridge
, dev
, dev
);
1564 pci_config_set_vendor_id(s
->dev
.config
, s
->vid
);
1565 pci_config_set_device_id(s
->dev
.config
, s
->did
);
1567 pci_set_word(dev
->config
+ PCI_STATUS
,
1568 PCI_STATUS_66MHZ
| PCI_STATUS_FAST_BACK
);
1569 pci_config_set_class(dev
->config
, PCI_CLASS_BRIDGE_PCI
);
1570 dev
->config
[PCI_HEADER_TYPE
] = PCI_HEADER_TYPE_BRIDGE
;
1571 pci_set_word(dev
->config
+ PCI_SEC_STATUS
,
1572 PCI_STATUS_66MHZ
| PCI_STATUS_FAST_BACK
);
1576 static int pci_bridge_exitfn(PCIDevice
*pci_dev
)
1578 PCIBridge
*s
= DO_UPCAST(PCIBridge
, dev
, pci_dev
);
1579 PCIBus
*bus
= &s
->bus
;
1580 pci_unregister_secondary_bus(bus
);
1584 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, uint16_t vid
, uint16_t did
,
1585 pci_map_irq_fn map_irq
, const char *name
)
1590 dev
= pci_create(bus
, devfn
, "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
);
1629 if (pci_dev
->romfile
== NULL
&& info
->romfile
!= NULL
)
1630 pci_dev
->romfile
= qemu_strdup(info
->romfile
);
1631 pci_add_option_rom(pci_dev
);
1633 if (qdev
->hotplugged
)
1634 bus
->hotplug(pci_dev
, 1);
1638 static int pci_unplug_device(DeviceState
*qdev
)
1640 PCIDevice
*dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
1642 dev
->bus
->hotplug(dev
, 0);
1646 void pci_qdev_register(PCIDeviceInfo
*info
)
1648 info
->qdev
.init
= pci_qdev_init
;
1649 info
->qdev
.unplug
= pci_unplug_device
;
1650 info
->qdev
.exit
= pci_unregister_device
;
1651 info
->qdev
.bus_info
= &pci_bus_info
;
1652 qdev_register(&info
->qdev
);
1655 void pci_qdev_register_many(PCIDeviceInfo
*info
)
1657 while (info
->qdev
.name
) {
1658 pci_qdev_register(info
);
1663 PCIDevice
*pci_create(PCIBus
*bus
, int devfn
, const char *name
)
1667 dev
= qdev_create(&bus
->qbus
, name
);
1668 qdev_prop_set_uint32(dev
, "addr", devfn
);
1669 return DO_UPCAST(PCIDevice
, qdev
, dev
);
1672 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
1674 PCIDevice
*dev
= pci_create(bus
, devfn
, name
);
1675 qdev_init_nofail(&dev
->qdev
);
1679 static int pci_find_space(PCIDevice
*pdev
, uint8_t size
)
1681 int config_size
= pci_config_size(pdev
);
1682 int offset
= PCI_CONFIG_HEADER_SIZE
;
1684 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< config_size
; ++i
)
1687 else if (i
- offset
+ 1 == size
)
1692 static uint8_t pci_find_capability_list(PCIDevice
*pdev
, uint8_t cap_id
,
1697 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
))
1700 for (prev
= PCI_CAPABILITY_LIST
; (next
= pdev
->config
[prev
]);
1701 prev
= next
+ PCI_CAP_LIST_NEXT
)
1702 if (pdev
->config
[next
+ PCI_CAP_LIST_ID
] == cap_id
)
1710 static void pci_map_option_rom(PCIDevice
*pdev
, int region_num
, pcibus_t addr
, pcibus_t size
, int type
)
1712 cpu_register_physical_memory(addr
, size
, pdev
->rom_offset
);
1715 /* Add an option rom for the device */
1716 static int pci_add_option_rom(PCIDevice
*pdev
)
1724 if (strlen(pdev
->romfile
) == 0)
1727 if (!pdev
->rom_bar
) {
1729 * Load rom via fw_cfg instead of creating a rom bar,
1730 * for 0.11 compatibility.
1732 int class = pci_get_word(pdev
->config
+ PCI_CLASS_DEVICE
);
1733 if (class == 0x0300) {
1734 rom_add_vga(pdev
->romfile
);
1736 rom_add_option(pdev
->romfile
);
1741 path
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, pdev
->romfile
);
1743 path
= qemu_strdup(pdev
->romfile
);
1746 size
= get_image_size(path
);
1748 qemu_error("%s: failed to find romfile \"%s\"\n", __FUNCTION__
,
1752 if (size
& (size
- 1)) {
1753 size
= 1 << qemu_fls(size
);
1756 pdev
->rom_offset
= qemu_ram_alloc(size
);
1758 ptr
= qemu_get_ram_ptr(pdev
->rom_offset
);
1759 load_image(path
, ptr
);
1762 pci_register_bar(pdev
, PCI_ROM_SLOT
, size
,
1763 0, pci_map_option_rom
);
1768 /* Reserve space and add capability to the linked list in pci config space */
1769 int pci_add_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1771 uint8_t offset
= pci_find_space(pdev
, size
);
1772 uint8_t *config
= pdev
->config
+ offset
;
1775 config
[PCI_CAP_LIST_ID
] = cap_id
;
1776 config
[PCI_CAP_LIST_NEXT
] = pdev
->config
[PCI_CAPABILITY_LIST
];
1777 pdev
->config
[PCI_CAPABILITY_LIST
] = offset
;
1778 pdev
->config
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
1779 memset(pdev
->used
+ offset
, 0xFF, size
);
1780 /* Make capability read-only by default */
1781 memset(pdev
->wmask
+ offset
, 0, size
);
1782 /* Check capability by default */
1783 memset(pdev
->cmask
+ offset
, 0xFF, size
);
1787 /* Unlink capability from the pci config space. */
1788 void pci_del_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1790 uint8_t prev
, offset
= pci_find_capability_list(pdev
, cap_id
, &prev
);
1793 pdev
->config
[prev
] = pdev
->config
[offset
+ PCI_CAP_LIST_NEXT
];
1794 /* Make capability writeable again */
1795 memset(pdev
->wmask
+ offset
, 0xff, size
);
1796 /* Clear cmask as device-specific registers can't be checked */
1797 memset(pdev
->cmask
+ offset
, 0, size
);
1798 memset(pdev
->used
+ offset
, 0, size
);
1800 if (!pdev
->config
[PCI_CAPABILITY_LIST
])
1801 pdev
->config
[PCI_STATUS
] &= ~PCI_STATUS_CAP_LIST
;
1804 /* Reserve space for capability at a known offset (to call after load). */
1805 void pci_reserve_capability(PCIDevice
*pdev
, uint8_t offset
, uint8_t size
)
1807 memset(pdev
->used
+ offset
, 0xff, size
);
1810 uint8_t pci_find_capability(PCIDevice
*pdev
, uint8_t cap_id
)
1812 return pci_find_capability_list(pdev
, cap_id
, NULL
);
1815 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
1817 PCIDevice
*d
= (PCIDevice
*)dev
;
1818 const pci_class_desc
*desc
;
1823 class = pci_get_word(d
->config
+ PCI_CLASS_DEVICE
);
1824 desc
= pci_class_descriptions
;
1825 while (desc
->desc
&& class != desc
->class)
1828 snprintf(ctxt
, sizeof(ctxt
), "%s", desc
->desc
);
1830 snprintf(ctxt
, sizeof(ctxt
), "Class %04x", class);
1833 monitor_printf(mon
, "%*sclass %s, addr %02x:%02x.%x, "
1834 "pci id %04x:%04x (sub %04x:%04x)\n",
1836 d
->config
[PCI_SECONDARY_BUS
],
1837 PCI_SLOT(d
->devfn
), PCI_FUNC(d
->devfn
),
1838 pci_get_word(d
->config
+ PCI_VENDOR_ID
),
1839 pci_get_word(d
->config
+ PCI_DEVICE_ID
),
1840 pci_get_word(d
->config
+ PCI_SUBSYSTEM_VENDOR_ID
),
1841 pci_get_word(d
->config
+ PCI_SUBSYSTEM_ID
));
1842 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1843 r
= &d
->io_regions
[i
];
1846 monitor_printf(mon
, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1847 " [0x%"FMT_PCIBUS
"]\n",
1849 i
, r
->type
& PCI_BASE_ADDRESS_SPACE_IO
? "i/o" : "mem",
1850 r
->addr
, r
->addr
+ r
->size
- 1);
1854 static PCIDeviceInfo bridge_info
= {
1855 .qdev
.name
= "pci-bridge",
1856 .qdev
.size
= sizeof(PCIBridge
),
1857 .init
= pci_bridge_initfn
,
1858 .exit
= pci_bridge_exitfn
,
1859 .config_write
= pci_bridge_write_config
,
1860 .qdev
.props
= (Property
[]) {
1861 DEFINE_PROP_HEX32("vendorid", PCIBridge
, vid
, 0),
1862 DEFINE_PROP_HEX32("deviceid", PCIBridge
, did
, 0),
1863 DEFINE_PROP_END_OF_LIST(),
1867 static void pci_register_devices(void)
1869 pci_qdev_register(&bridge_info
);
1872 device_init(pci_register_devices
)