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
31 #include "device-assignment.h"
35 # define PCI_DPRINTF(format, ...) printf(format, __VA_ARGS__)
37 # define PCI_DPRINTF(format, ...) do { } while (0)
44 pci_set_irq_fn set_irq
;
45 pci_map_irq_fn map_irq
;
46 pci_hotplug_fn hotplug
;
47 uint32_t config_reg
; /* XXX: suppress */
49 PCIDevice
*devices
[256];
50 PCIDevice
*parent_dev
;
52 /* The bus IRQ state is the logical OR of the connected devices.
53 Keep a count of the number of devices with raised IRQs. */
58 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
60 static struct BusInfo pci_bus_info
= {
62 .size
= sizeof(PCIBus
),
63 .print_dev
= pcibus_dev_print
,
64 .props
= (Property
[]) {
65 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice
, devfn
, -1),
66 DEFINE_PROP_END_OF_LIST()
70 static void pci_update_mappings(PCIDevice
*d
);
71 static void pci_set_irq(void *opaque
, int irq_num
, int level
);
73 target_phys_addr_t pci_mem_base
;
74 static uint16_t pci_default_sub_vendor_id
= PCI_SUBVENDOR_ID_REDHAT_QUMRANET
;
75 static uint16_t pci_default_sub_device_id
= PCI_SUBDEVICE_ID_QEMU
;
76 static PCIBus
*first_bus
;
78 static const VMStateDescription vmstate_pcibus
= {
81 .minimum_version_id
= 1,
82 .minimum_version_id_old
= 1,
83 .fields
= (VMStateField
[]) {
84 VMSTATE_INT32_EQUAL(nirq
, PCIBus
),
85 VMSTATE_INT32_VARRAY(irq_count
, PCIBus
, nirq
),
90 static inline int pci_bar(int reg
)
92 return reg
== PCI_ROM_SLOT
? PCI_ROM_ADDRESS
: PCI_BASE_ADDRESS_0
+ reg
* 4;
95 static void pci_device_reset(PCIDevice
*dev
)
97 memset(dev
->irq_state
, 0, sizeof dev
->irq_state
);
100 static void pci_bus_reset(void *opaque
)
102 PCIBus
*bus
= opaque
;
105 for (i
= 0; i
< bus
->nirq
; i
++) {
106 bus
->irq_count
[i
] = 0;
108 for (i
= 0; i
< ARRAY_SIZE(bus
->devices
); ++i
) {
109 if (bus
->devices
[i
]) {
110 pci_device_reset(bus
->devices
[i
]);
115 void pci_bus_new_inplace(PCIBus
*bus
, DeviceState
*parent
,
116 const char *name
, int devfn_min
)
120 qbus_create_inplace(&bus
->qbus
, &pci_bus_info
, parent
, name
);
121 bus
->devfn_min
= devfn_min
;
122 bus
->next
= first_bus
;
124 vmstate_register(nbus
++, &vmstate_pcibus
, bus
);
125 qemu_register_reset(pci_bus_reset
, bus
);
128 PCIBus
*pci_bus_new(DeviceState
*parent
, const char *name
, int devfn_min
)
132 bus
= qemu_mallocz(sizeof(*bus
));
133 bus
->qbus
.qdev_allocated
= 1;
134 pci_bus_new_inplace(bus
, parent
, name
, devfn_min
);
138 void pci_bus_irqs(PCIBus
*bus
, pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
139 void *irq_opaque
, int nirq
)
141 bus
->set_irq
= set_irq
;
142 bus
->map_irq
= map_irq
;
143 bus
->irq_opaque
= irq_opaque
;
145 bus
->irq_count
= qemu_mallocz(nirq
* sizeof(bus
->irq_count
[0]));
148 void pci_bus_hotplug(PCIBus
*bus
, pci_hotplug_fn hotplug
)
150 bus
->qbus
.allow_hotplug
= 1;
151 bus
->hotplug
= hotplug
;
154 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
155 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
156 void *irq_opaque
, int devfn_min
, int nirq
)
160 bus
= pci_bus_new(parent
, name
, devfn_min
);
161 pci_bus_irqs(bus
, set_irq
, map_irq
, irq_opaque
, nirq
);
165 static void pci_register_secondary_bus(PCIBus
*bus
,
167 pci_map_irq_fn map_irq
,
170 qbus_create_inplace(&bus
->qbus
, &pci_bus_info
, &dev
->qdev
, name
);
171 bus
->map_irq
= map_irq
;
172 bus
->parent_dev
= dev
;
173 bus
->next
= dev
->bus
->next
;
174 dev
->bus
->next
= bus
;
177 int pci_bus_num(PCIBus
*s
)
182 static int get_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
184 PCIDevice
*s
= container_of(pv
, PCIDevice
, config
);
185 uint8_t config
[PCI_CONFIG_SPACE_SIZE
];
188 assert(size
== sizeof config
);
189 qemu_get_buffer(f
, config
, sizeof config
);
190 for (i
= 0; i
< sizeof config
; ++i
)
191 if ((config
[i
] ^ s
->config
[i
]) & s
->cmask
[i
] & ~s
->wmask
[i
])
193 memcpy(s
->config
, config
, sizeof config
);
195 pci_update_mappings(s
);
200 /* just put buffer */
201 static void put_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
203 const uint8_t *v
= pv
;
204 qemu_put_buffer(f
, v
, size
);
207 static VMStateInfo vmstate_info_pci_config
= {
208 .name
= "pci config",
209 .get
= get_pci_config_device
,
210 .put
= put_pci_config_device
,
213 const VMStateDescription vmstate_pci_device
= {
216 .minimum_version_id
= 1,
217 .minimum_version_id_old
= 1,
218 .fields
= (VMStateField
[]) {
219 VMSTATE_INT32_LE(version_id
, PCIDevice
),
220 VMSTATE_SINGLE(config
, PCIDevice
, 0, vmstate_info_pci_config
,
221 typeof_field(PCIDevice
,config
)),
222 VMSTATE_INT32_ARRAY_V(irq_state
, PCIDevice
, 4, 2),
223 VMSTATE_END_OF_LIST()
227 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
229 vmstate_save_state(f
, &vmstate_pci_device
, s
);
232 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
234 return vmstate_load_state(f
, &vmstate_pci_device
, s
, s
->version_id
);
237 static int pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
241 id
= (void*)(&pci_dev
->config
[PCI_SUBVENDOR_ID
]);
242 id
[0] = cpu_to_le16(pci_default_sub_vendor_id
);
243 id
[1] = cpu_to_le16(pci_default_sub_device_id
);
248 * Parse pci address in qemu command
249 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
251 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
256 unsigned long dom
= 0, bus
= 0;
260 val
= strtoul(p
, &e
, 16);
266 val
= strtoul(p
, &e
, 16);
273 val
= strtoul(p
, &e
, 16);
279 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
287 /* Note: QEMU doesn't implement domains other than 0 */
288 if (dom
!= 0 || pci_find_bus(bus
) == NULL
)
298 * Parse device bdf in device assignment command:
300 * -pcidevice host=bus:dev.func
302 * Parse <bus>:<slot>.<func> return -1 on error
304 int pci_parse_host_devaddr(const char *addr
, int *busp
,
305 int *slotp
, int *funcp
)
310 int bus
= 0, slot
= 0, func
= 0;
313 val
= strtoul(p
, &e
, 16);
319 val
= strtoul(p
, &e
, 16);
325 val
= strtoul(p
, &e
, 16);
334 if (bus
> 0xff || slot
> 0x1f || func
> 0x7)
346 int pci_read_devaddr(Monitor
*mon
, const char *addr
, int *domp
, int *busp
,
349 /* strip legacy tag */
350 if (!strncmp(addr
, "pci_addr=", 9)) {
353 if (pci_parse_devaddr(addr
, domp
, busp
, slotp
)) {
354 monitor_printf(mon
, "Invalid pci address\n");
360 PCIBus
*pci_get_bus_devfn(int *devfnp
, const char *devaddr
)
367 return pci_find_bus(0);
370 if (pci_parse_devaddr(devaddr
, &dom
, &bus
, &slot
) < 0) {
375 return pci_find_bus(bus
);
378 static void pci_init_cmask(PCIDevice
*dev
)
380 pci_set_word(dev
->cmask
+ PCI_VENDOR_ID
, 0xffff);
381 pci_set_word(dev
->cmask
+ PCI_DEVICE_ID
, 0xffff);
382 dev
->cmask
[PCI_STATUS
] = PCI_STATUS_CAP_LIST
;
383 dev
->cmask
[PCI_REVISION_ID
] = 0xff;
384 dev
->cmask
[PCI_CLASS_PROG
] = 0xff;
385 pci_set_word(dev
->cmask
+ PCI_CLASS_DEVICE
, 0xffff);
386 dev
->cmask
[PCI_HEADER_TYPE
] = 0xff;
387 dev
->cmask
[PCI_CAPABILITY_LIST
] = 0xff;
390 static void pci_init_wmask(PCIDevice
*dev
)
393 dev
->wmask
[PCI_CACHE_LINE_SIZE
] = 0xff;
394 dev
->wmask
[PCI_INTERRUPT_LINE
] = 0xff;
395 dev
->wmask
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
396 | PCI_COMMAND_MASTER
;
397 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
398 dev
->wmask
[i
] = 0xff;
401 /* -1 for devfn means auto assign */
402 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
403 const char *name
, int devfn
,
404 PCIConfigReadFunc
*config_read
,
405 PCIConfigWriteFunc
*config_write
)
408 for(devfn
= bus
->devfn_min
; devfn
< 256; devfn
+= 8) {
409 if (!bus
->devices
[devfn
])
414 } else if (bus
->devices
[devfn
]) {
418 pci_dev
->devfn
= devfn
;
419 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
420 memset(pci_dev
->irq_state
, 0, sizeof(pci_dev
->irq_state
));
421 pci_set_default_subsystem_id(pci_dev
);
422 pci_init_cmask(pci_dev
);
423 pci_init_wmask(pci_dev
);
426 config_read
= pci_default_read_config
;
428 config_write
= pci_default_write_config
;
429 pci_dev
->config_read
= config_read
;
430 pci_dev
->config_write
= config_write
;
431 bus
->devices
[devfn
] = pci_dev
;
432 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, 4);
433 pci_dev
->version_id
= 2; /* Current pci device vmstate version */
437 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
438 int instance_size
, int devfn
,
439 PCIConfigReadFunc
*config_read
,
440 PCIConfigWriteFunc
*config_write
)
444 pci_dev
= qemu_mallocz(instance_size
);
445 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
446 config_read
, config_write
);
449 static target_phys_addr_t
pci_to_cpu_addr(target_phys_addr_t addr
)
451 return addr
+ pci_mem_base
;
454 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
459 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
460 r
= &pci_dev
->io_regions
[i
];
461 if (!r
->size
|| r
->addr
== -1)
463 if (r
->type
== PCI_ADDRESS_SPACE_IO
) {
464 isa_unassign_ioport(r
->addr
, r
->size
);
466 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
473 static int pci_unregister_device(DeviceState
*dev
)
475 PCIDevice
*pci_dev
= DO_UPCAST(PCIDevice
, qdev
, dev
);
476 PCIDeviceInfo
*info
= DO_UPCAST(PCIDeviceInfo
, qdev
, dev
->info
);
480 ret
= info
->exit(pci_dev
);
484 pci_unregister_io_regions(pci_dev
);
486 qemu_free_irqs(pci_dev
->irq
);
487 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
491 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
492 uint32_t size
, int type
,
493 PCIMapIORegionFunc
*map_func
)
499 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
502 if (size
& (size
-1)) {
503 fprintf(stderr
, "ERROR: PCI region size must be pow2 "
504 "type=0x%x, size=0x%x\n", type
, size
);
508 r
= &pci_dev
->io_regions
[region_num
];
512 r
->map_func
= map_func
;
515 addr
= pci_bar(region_num
);
516 if (region_num
== PCI_ROM_SLOT
) {
517 /* ROM enable bit is writeable */
518 wmask
|= PCI_ROM_ADDRESS_ENABLE
;
520 *(uint32_t *)(pci_dev
->config
+ addr
) = cpu_to_le32(type
);
521 *(uint32_t *)(pci_dev
->wmask
+ addr
) = cpu_to_le32(wmask
);
522 *(uint32_t *)(pci_dev
->cmask
+ addr
) = 0xffffffff;
525 static void pci_update_mappings(PCIDevice
*d
)
529 uint32_t last_addr
, new_addr
;
531 cmd
= le16_to_cpu(*(uint16_t *)(d
->config
+ PCI_COMMAND
));
532 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
533 r
= &d
->io_regions
[i
];
535 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
536 if (cmd
& PCI_COMMAND_IO
) {
537 new_addr
= pci_get_long(d
->config
+ pci_bar(i
));
538 new_addr
= new_addr
& ~(r
->size
- 1);
539 last_addr
= new_addr
+ r
->size
- 1;
540 /* NOTE: we have only 64K ioports on PC */
541 if (last_addr
<= new_addr
|| new_addr
== 0 ||
542 last_addr
>= 0x10000) {
549 if (cmd
& PCI_COMMAND_MEMORY
) {
550 new_addr
= pci_get_long(d
->config
+ pci_bar(i
));
551 /* the ROM slot has a specific enable bit */
552 if (i
== PCI_ROM_SLOT
&& !(new_addr
& PCI_ROM_ADDRESS_ENABLE
))
554 new_addr
= new_addr
& ~(r
->size
- 1);
555 last_addr
= new_addr
+ r
->size
- 1;
556 /* NOTE: we do not support wrapping */
557 /* XXX: as we cannot support really dynamic
558 mappings, we handle specific values as invalid
560 if (last_addr
<= new_addr
|| new_addr
== 0 ||
569 /* now do the real mapping */
570 if (new_addr
!= r
->addr
) {
572 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
574 /* NOTE: specific hack for IDE in PC case:
575 only one byte must be mapped. */
576 class = pci_get_word(d
->config
+ PCI_CLASS_DEVICE
);
577 if (class == 0x0101 && r
->size
== 4) {
578 isa_unassign_ioport(r
->addr
+ 2, 1);
580 isa_unassign_ioport(r
->addr
, r
->size
);
583 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
586 qemu_unregister_coalesced_mmio(r
->addr
, r
->size
);
591 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
598 static uint32_t pci_read_config(PCIDevice
*d
,
599 uint32_t address
, int len
)
606 if (address
<= 0xfc) {
607 val
= le32_to_cpu(*(uint32_t *)(d
->config
+ address
));
612 if (address
<= 0xfe) {
613 val
= le16_to_cpu(*(uint16_t *)(d
->config
+ address
));
618 val
= d
->config
[address
];
624 static void pci_write_config(PCIDevice
*pci_dev
,
625 uint32_t address
, uint32_t val
, int len
)
628 for (i
= 0; i
< len
; i
++) {
629 pci_dev
->config
[address
+ i
] = val
& 0xff;
634 int pci_access_cap_config(PCIDevice
*pci_dev
, uint32_t address
, int len
)
636 if (pci_dev
->cap
.supported
&& address
>= pci_dev
->cap
.start
&&
637 (address
+ len
) < pci_dev
->cap
.start
+ pci_dev
->cap
.length
)
642 uint32_t pci_default_cap_read_config(PCIDevice
*pci_dev
,
643 uint32_t address
, int len
)
645 return pci_read_config(pci_dev
, address
, len
);
648 void pci_default_cap_write_config(PCIDevice
*pci_dev
,
649 uint32_t address
, uint32_t val
, int len
)
651 pci_write_config(pci_dev
, address
, val
, len
);
654 uint32_t pci_default_read_config(PCIDevice
*d
,
655 uint32_t address
, int len
)
657 if (pci_access_cap_config(d
, address
, len
))
658 return d
->cap
.config_read(d
, address
, len
);
660 return pci_read_config(d
, address
, len
);
663 void pci_default_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int l
)
665 uint8_t orig
[PCI_CONFIG_SPACE_SIZE
];
668 if (pci_access_cap_config(d
, addr
, l
)) {
669 d
->cap
.config_write(d
, addr
, val
, l
);
673 /* not efficient, but simple */
674 memcpy(orig
, d
->config
, PCI_CONFIG_SPACE_SIZE
);
675 for(i
= 0; i
< l
&& addr
< PCI_CONFIG_SPACE_SIZE
; val
>>= 8, ++i
, ++addr
) {
676 uint8_t wmask
= d
->wmask
[addr
];
677 d
->config
[addr
] = (d
->config
[addr
] & ~wmask
) | (val
& wmask
);
680 #ifdef CONFIG_KVM_DEVICE_ASSIGNMENT
681 if (kvm_enabled() && kvm_irqchip_in_kernel() &&
682 addr
>= PIIX_CONFIG_IRQ_ROUTE
&&
683 addr
< PIIX_CONFIG_IRQ_ROUTE
+ 4)
684 assigned_dev_update_irqs();
685 #endif /* CONFIG_KVM_DEVICE_ASSIGNMENT */
687 if (memcmp(orig
+ PCI_BASE_ADDRESS_0
, d
->config
+ PCI_BASE_ADDRESS_0
, 24)
688 || ((orig
[PCI_COMMAND
] ^ d
->config
[PCI_COMMAND
])
689 & (PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
)))
690 pci_update_mappings(d
);
694 void pci_data_write(void *opaque
, uint32_t addr
, uint32_t val
, int len
)
698 int config_addr
, bus_num
;
701 PCI_DPRINTF("pci_data_write: addr=%08x val=%08x len=%d\n",
704 bus_num
= (addr
>> 16) & 0xff;
705 while (s
&& s
->bus_num
!= bus_num
)
709 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
712 config_addr
= addr
& 0xff;
713 PCI_DPRINTF("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
714 pci_dev
->name
, config_addr
, val
, len
);
715 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
718 uint32_t pci_data_read(void *opaque
, uint32_t addr
, int len
)
722 int config_addr
, bus_num
;
725 bus_num
= (addr
>> 16) & 0xff;
726 while (s
&& s
->bus_num
!= bus_num
)
730 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
747 config_addr
= addr
& 0xff;
748 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
749 PCI_DPRINTF("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
750 pci_dev
->name
, config_addr
, val
, len
);
753 PCI_DPRINTF("pci_data_read: addr=%08x val=%08x len=%d\n",
759 /***********************************************************/
760 /* generic PCI irq support */
762 /* 0 <= irq_num <= 3. level must be 0 or 1 */
763 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
765 PCIDevice
*pci_dev
= opaque
;
769 change
= level
- pci_dev
->irq_state
[irq_num
];
773 pci_dev
->irq_state
[irq_num
] = level
;
775 #if defined(TARGET_IA64)
776 ioapic_set_irq(pci_dev
, irq_num
, level
);
781 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
784 pci_dev
= bus
->parent_dev
;
786 bus
->irq_count
[irq_num
] += change
;
787 bus
->set_irq(bus
->irq_opaque
, irq_num
, bus
->irq_count
[irq_num
] != 0);
790 int pci_map_irq(PCIDevice
*pci_dev
, int pin
)
792 return pci_dev
->bus
->map_irq(pci_dev
, pin
);
795 /***********************************************************/
796 /* monitor info on PCI */
803 static const pci_class_desc pci_class_descriptions
[] =
805 { 0x0100, "SCSI controller"},
806 { 0x0101, "IDE controller"},
807 { 0x0102, "Floppy controller"},
808 { 0x0103, "IPI controller"},
809 { 0x0104, "RAID controller"},
810 { 0x0106, "SATA controller"},
811 { 0x0107, "SAS controller"},
812 { 0x0180, "Storage controller"},
813 { 0x0200, "Ethernet controller"},
814 { 0x0201, "Token Ring controller"},
815 { 0x0202, "FDDI controller"},
816 { 0x0203, "ATM controller"},
817 { 0x0280, "Network controller"},
818 { 0x0300, "VGA controller"},
819 { 0x0301, "XGA controller"},
820 { 0x0302, "3D controller"},
821 { 0x0380, "Display controller"},
822 { 0x0400, "Video controller"},
823 { 0x0401, "Audio controller"},
825 { 0x0480, "Multimedia controller"},
826 { 0x0500, "RAM controller"},
827 { 0x0501, "Flash controller"},
828 { 0x0580, "Memory controller"},
829 { 0x0600, "Host bridge"},
830 { 0x0601, "ISA bridge"},
831 { 0x0602, "EISA bridge"},
832 { 0x0603, "MC bridge"},
833 { 0x0604, "PCI bridge"},
834 { 0x0605, "PCMCIA bridge"},
835 { 0x0606, "NUBUS bridge"},
836 { 0x0607, "CARDBUS bridge"},
837 { 0x0608, "RACEWAY bridge"},
839 { 0x0c03, "USB controller"},
843 static void pci_info_device(PCIDevice
*d
)
845 Monitor
*mon
= cur_mon
;
848 const pci_class_desc
*desc
;
850 monitor_printf(mon
, " Bus %2d, device %3d, function %d:\n",
851 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7);
852 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
853 monitor_printf(mon
, " ");
854 desc
= pci_class_descriptions
;
855 while (desc
->desc
&& class != desc
->class)
858 monitor_printf(mon
, "%s", desc
->desc
);
860 monitor_printf(mon
, "Class %04x", class);
862 monitor_printf(mon
, ": PCI device %04x:%04x\n",
863 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
864 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))));
866 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
867 monitor_printf(mon
, " IRQ %d.\n",
868 d
->config
[PCI_INTERRUPT_LINE
]);
870 if (class == 0x0604) {
871 monitor_printf(mon
, " BUS %d.\n", d
->config
[0x19]);
873 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
874 r
= &d
->io_regions
[i
];
876 monitor_printf(mon
, " BAR%d: ", i
);
877 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
878 monitor_printf(mon
, "I/O at 0x%04x [0x%04x].\n",
879 r
->addr
, r
->addr
+ r
->size
- 1);
881 monitor_printf(mon
, "32 bit memory at 0x%08x [0x%08x].\n",
882 r
->addr
, r
->addr
+ r
->size
- 1);
886 monitor_printf(mon
, " id \"%s\"\n", d
->qdev
.id
? d
->qdev
.id
: "");
887 if (class == 0x0604 && d
->config
[0x19] != 0) {
888 pci_for_each_device(d
->config
[0x19], pci_info_device
);
892 void pci_for_each_device(int bus_num
, void (*fn
)(PCIDevice
*d
))
894 PCIBus
*bus
= first_bus
;
898 while (bus
&& bus
->bus_num
!= bus_num
)
901 for(devfn
= 0; devfn
< 256; devfn
++) {
902 d
= bus
->devices
[devfn
];
909 void pci_info(Monitor
*mon
)
911 pci_for_each_device(0, pci_info_device
);
914 static const char * const pci_nic_models
[] = {
926 static const char * const pci_nic_names
[] = {
938 /* Initialize a PCI NIC. */
939 /* FIXME callers should check for failure, but don't */
940 PCIDevice
*pci_nic_init(NICInfo
*nd
, const char *default_model
,
941 const char *default_devaddr
)
943 const char *devaddr
= nd
->devaddr
? nd
->devaddr
: default_devaddr
;
950 i
= qemu_find_nic_model(nd
, pci_nic_models
, default_model
);
954 bus
= pci_get_bus_devfn(&devfn
, devaddr
);
956 qemu_error("Invalid PCI device address %s for device %s\n",
957 devaddr
, pci_nic_names
[i
]);
961 pci_dev
= pci_create(bus
, devfn
, pci_nic_names
[i
]);
962 dev
= &pci_dev
->qdev
;
964 dev
->id
= qemu_strdup(nd
->name
);
965 if (qdev_prop_exists(dev
, "mac")) {
967 qdev_set_nic_properties(dev
, nd
);
973 if (qdev_init(dev
) < 0)
978 PCIDevice
*pci_nic_init_nofail(NICInfo
*nd
, const char *default_model
,
979 const char *default_devaddr
)
983 if (qemu_show_nic_models(nd
->model
, pci_nic_models
))
986 res
= pci_nic_init(nd
, default_model
, default_devaddr
);
999 static void pci_bridge_write_config(PCIDevice
*d
,
1000 uint32_t address
, uint32_t val
, int len
)
1002 PCIBridge
*s
= (PCIBridge
*)d
;
1004 pci_default_write_config(d
, address
, val
, len
);
1005 s
->bus
.bus_num
= d
->config
[PCI_SECONDARY_BUS
];
1008 PCIBus
*pci_find_bus(int bus_num
)
1010 PCIBus
*bus
= first_bus
;
1012 while (bus
&& bus
->bus_num
!= bus_num
)
1018 PCIDevice
*pci_find_device(int bus_num
, int slot
, int function
)
1020 PCIBus
*bus
= pci_find_bus(bus_num
);
1025 return bus
->devices
[PCI_DEVFN(slot
, function
)];
1028 static int pci_bridge_initfn(PCIDevice
*dev
)
1030 PCIBridge
*s
= DO_UPCAST(PCIBridge
, dev
, dev
);
1032 pci_config_set_vendor_id(s
->dev
.config
, s
->vid
);
1033 pci_config_set_device_id(s
->dev
.config
, s
->did
);
1035 s
->dev
.config
[0x04] = 0x06; // command = bus master, pci mem
1036 s
->dev
.config
[0x05] = 0x00;
1037 s
->dev
.config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
1038 s
->dev
.config
[0x07] = 0x00; // status = fast devsel
1039 s
->dev
.config
[0x08] = 0x00; // revision
1040 s
->dev
.config
[0x09] = 0x00; // programming i/f
1041 pci_config_set_class(s
->dev
.config
, PCI_CLASS_BRIDGE_PCI
);
1042 s
->dev
.config
[0x0D] = 0x10; // latency_timer
1043 s
->dev
.config
[PCI_HEADER_TYPE
] =
1044 PCI_HEADER_TYPE_MULTI_FUNCTION
| PCI_HEADER_TYPE_BRIDGE
; // header_type
1045 s
->dev
.config
[0x1E] = 0xa0; // secondary status
1049 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, uint16_t vid
, uint16_t did
,
1050 pci_map_irq_fn map_irq
, const char *name
)
1055 dev
= pci_create(bus
, devfn
, "pci-bridge");
1056 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", vid
);
1057 qdev_prop_set_uint32(&dev
->qdev
, "deviceid", did
);
1058 qdev_init_nofail(&dev
->qdev
);
1060 s
= DO_UPCAST(PCIBridge
, dev
, dev
);
1061 pci_register_secondary_bus(&s
->bus
, &s
->dev
, map_irq
, name
);
1065 static int pci_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
1067 PCIDevice
*pci_dev
= (PCIDevice
*)qdev
;
1068 PCIDeviceInfo
*info
= container_of(base
, PCIDeviceInfo
, qdev
);
1072 bus
= FROM_QBUS(PCIBus
, qdev_get_parent_bus(qdev
));
1073 devfn
= pci_dev
->devfn
;
1074 pci_dev
= do_pci_register_device(pci_dev
, bus
, base
->name
, devfn
,
1075 info
->config_read
, info
->config_write
);
1077 rc
= info
->init(pci_dev
);
1080 if (qdev
->hotplugged
)
1081 bus
->hotplug(pci_dev
, 1);
1085 static int pci_unplug_device(DeviceState
*qdev
)
1087 PCIDevice
*dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
1089 dev
->bus
->hotplug(dev
, 0);
1093 void pci_qdev_register(PCIDeviceInfo
*info
)
1095 info
->qdev
.init
= pci_qdev_init
;
1096 info
->qdev
.unplug
= pci_unplug_device
;
1097 info
->qdev
.exit
= pci_unregister_device
;
1098 info
->qdev
.bus_info
= &pci_bus_info
;
1099 qdev_register(&info
->qdev
);
1102 void pci_qdev_register_many(PCIDeviceInfo
*info
)
1104 while (info
->qdev
.name
) {
1105 pci_qdev_register(info
);
1110 PCIDevice
*pci_create(PCIBus
*bus
, int devfn
, const char *name
)
1114 dev
= qdev_create(&bus
->qbus
, name
);
1115 qdev_prop_set_uint32(dev
, "addr", devfn
);
1116 return DO_UPCAST(PCIDevice
, qdev
, dev
);
1119 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
1121 PCIDevice
*dev
= pci_create(bus
, devfn
, name
);
1122 qdev_init_nofail(&dev
->qdev
);
1126 int pci_enable_capability_support(PCIDevice
*pci_dev
,
1127 uint32_t config_start
,
1128 PCICapConfigReadFunc
*config_read
,
1129 PCICapConfigWriteFunc
*config_write
,
1130 PCICapConfigInitFunc
*config_init
)
1135 pci_dev
->config
[0x06] |= 0x10; // status = capabilities
1137 if (config_start
== 0)
1138 pci_dev
->cap
.start
= PCI_CAPABILITY_CONFIG_DEFAULT_START_ADDR
;
1139 else if (config_start
>= 0x40 && config_start
< 0xff)
1140 pci_dev
->cap
.start
= config_start
;
1145 pci_dev
->cap
.config_read
= config_read
;
1147 pci_dev
->cap
.config_read
= pci_default_cap_read_config
;
1149 pci_dev
->cap
.config_write
= config_write
;
1151 pci_dev
->cap
.config_write
= pci_default_cap_write_config
;
1152 pci_dev
->cap
.supported
= 1;
1153 pci_dev
->config
[PCI_CAPABILITY_LIST
] = pci_dev
->cap
.start
;
1154 return config_init(pci_dev
);
1157 static int pci_find_space(PCIDevice
*pdev
, uint8_t size
)
1159 int offset
= PCI_CONFIG_HEADER_SIZE
;
1161 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
1164 else if (i
- offset
+ 1 == size
)
1169 static uint8_t pci_find_capability_list(PCIDevice
*pdev
, uint8_t cap_id
,
1174 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
))
1177 for (prev
= PCI_CAPABILITY_LIST
; (next
= pdev
->config
[prev
]);
1178 prev
= next
+ PCI_CAP_LIST_NEXT
)
1179 if (pdev
->config
[next
+ PCI_CAP_LIST_ID
] == cap_id
)
1187 /* Reserve space and add capability to the linked list in pci config space */
1188 int pci_add_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1190 uint8_t offset
= pci_find_space(pdev
, size
);
1191 uint8_t *config
= pdev
->config
+ offset
;
1194 config
[PCI_CAP_LIST_ID
] = cap_id
;
1195 config
[PCI_CAP_LIST_NEXT
] = pdev
->config
[PCI_CAPABILITY_LIST
];
1196 pdev
->config
[PCI_CAPABILITY_LIST
] = offset
;
1197 pdev
->config
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
1198 memset(pdev
->used
+ offset
, 0xFF, size
);
1199 /* Make capability read-only by default */
1200 memset(pdev
->wmask
+ offset
, 0, size
);
1201 /* Check capability by default */
1202 memset(pdev
->cmask
+ offset
, 0xFF, size
);
1206 /* Unlink capability from the pci config space. */
1207 void pci_del_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1209 uint8_t prev
, offset
= pci_find_capability_list(pdev
, cap_id
, &prev
);
1212 pdev
->config
[prev
] = pdev
->config
[offset
+ PCI_CAP_LIST_NEXT
];
1213 /* Make capability writeable again */
1214 memset(pdev
->wmask
+ offset
, 0xff, size
);
1215 /* Clear cmask as device-specific registers can't be checked */
1216 memset(pdev
->cmask
+ offset
, 0, size
);
1217 memset(pdev
->used
+ offset
, 0, size
);
1219 if (!pdev
->config
[PCI_CAPABILITY_LIST
])
1220 pdev
->config
[PCI_STATUS
] &= ~PCI_STATUS_CAP_LIST
;
1223 /* Reserve space for capability at a known offset (to call after load). */
1224 void pci_reserve_capability(PCIDevice
*pdev
, uint8_t offset
, uint8_t size
)
1226 memset(pdev
->used
+ offset
, 0xff, size
);
1229 uint8_t pci_find_capability(PCIDevice
*pdev
, uint8_t cap_id
)
1231 return pci_find_capability_list(pdev
, cap_id
, NULL
);
1234 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
1236 PCIDevice
*d
= (PCIDevice
*)dev
;
1237 const pci_class_desc
*desc
;
1242 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
1243 desc
= pci_class_descriptions
;
1244 while (desc
->desc
&& class != desc
->class)
1247 snprintf(ctxt
, sizeof(ctxt
), "%s", desc
->desc
);
1249 snprintf(ctxt
, sizeof(ctxt
), "Class %04x", class);
1252 monitor_printf(mon
, "%*sclass %s, addr %02x:%02x.%x, "
1253 "pci id %04x:%04x (sub %04x:%04x)\n",
1255 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7,
1256 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
1257 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))),
1258 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_SUBSYSTEM_VENDOR_ID
))),
1259 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_SUBSYSTEM_ID
))));
1260 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1261 r
= &d
->io_regions
[i
];
1264 monitor_printf(mon
, "%*sbar %d: %s at 0x%x [0x%x]\n", indent
, "",
1265 i
, r
->type
& PCI_ADDRESS_SPACE_IO
? "i/o" : "mem",
1266 r
->addr
, r
->addr
+ r
->size
- 1);
1270 static PCIDeviceInfo bridge_info
= {
1271 .qdev
.name
= "pci-bridge",
1272 .qdev
.size
= sizeof(PCIBridge
),
1273 .init
= pci_bridge_initfn
,
1274 .config_write
= pci_bridge_write_config
,
1275 .qdev
.props
= (Property
[]) {
1276 DEFINE_PROP_HEX32("vendorid", PCIBridge
, vid
, 0),
1277 DEFINE_PROP_HEX32("deviceid", PCIBridge
, did
, 0),
1278 DEFINE_PROP_END_OF_LIST(),
1282 static void pci_register_devices(void)
1284 pci_qdev_register(&bridge_info
);
1287 device_init(pci_register_devices
)