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 uint32_t config_reg
; /* XXX: suppress */
48 SetIRQFunc
*low_set_irq
;
50 PCIDevice
*devices
[256];
51 PCIDevice
*parent_dev
;
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
[]) {
68 .info
= &qdev_prop_uint32
,
69 .offset
= offsetof(PCIDevice
, devfn
),
70 .defval
= (uint32_t[]) { -1 },
76 static void pci_update_mappings(PCIDevice
*d
);
77 static void pci_set_irq(void *opaque
, int irq_num
, int level
);
79 target_phys_addr_t pci_mem_base
;
80 static uint16_t pci_default_sub_vendor_id
= PCI_SUBVENDOR_ID_REDHAT_QUMRANET
;
81 static uint16_t pci_default_sub_device_id
= PCI_SUBDEVICE_ID_QEMU
;
82 static PCIBus
*first_bus
;
84 static void pcibus_save(QEMUFile
*f
, void *opaque
)
86 PCIBus
*bus
= (PCIBus
*)opaque
;
89 qemu_put_be32(f
, bus
->nirq
);
90 for (i
= 0; i
< bus
->nirq
; i
++)
91 qemu_put_be32(f
, bus
->irq_count
[i
]);
94 static int pcibus_load(QEMUFile
*f
, void *opaque
, int version_id
)
96 PCIBus
*bus
= (PCIBus
*)opaque
;
102 nirq
= qemu_get_be32(f
);
103 if (bus
->nirq
!= nirq
) {
104 fprintf(stderr
, "pcibus_load: nirq mismatch: src=%d dst=%d\n",
109 for (i
= 0; i
< nirq
; i
++)
110 bus
->irq_count
[i
] = qemu_get_be32(f
);
115 static void pci_bus_reset(void *opaque
)
117 PCIBus
*bus
= (PCIBus
*)opaque
;
120 for (i
= 0; i
< bus
->nirq
; i
++) {
121 bus
->irq_count
[i
] = 0;
123 for (i
= 0; i
< 256; i
++) {
125 memset(bus
->devices
[i
]->irq_state
, 0,
126 sizeof(bus
->devices
[i
]->irq_state
));
130 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
131 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
132 qemu_irq
*pic
, int devfn_min
, int nirq
)
137 bus
= FROM_QBUS(PCIBus
, qbus_create(&pci_bus_info
, parent
, name
));
138 bus
->set_irq
= set_irq
;
139 bus
->map_irq
= map_irq
;
140 bus
->irq_opaque
= pic
;
141 bus
->devfn_min
= devfn_min
;
143 bus
->irq_count
= qemu_mallocz(nirq
* sizeof(bus
->irq_count
[0]));
144 bus
->next
= first_bus
;
146 register_savevm("PCIBUS", nbus
++, 1, pcibus_save
, pcibus_load
, bus
);
147 qemu_register_reset(pci_bus_reset
, bus
);
151 static PCIBus
*pci_register_secondary_bus(PCIDevice
*dev
, pci_map_irq_fn map_irq
)
155 bus
= qemu_mallocz(sizeof(PCIBus
));
156 bus
->map_irq
= map_irq
;
157 bus
->parent_dev
= dev
;
158 bus
->next
= dev
->bus
->next
;
159 dev
->bus
->next
= bus
;
163 int pci_bus_num(PCIBus
*s
)
168 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
172 qemu_put_be32(f
, 2); /* PCI device version */
173 qemu_put_buffer(f
, s
->config
, 256);
174 for (i
= 0; i
< 4; i
++)
175 qemu_put_be32(f
, s
->irq_state
[i
]);
178 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
180 uint8_t config
[PCI_CONFIG_SPACE_SIZE
];
184 version_id
= qemu_get_be32(f
);
187 qemu_get_buffer(f
, config
, sizeof config
);
188 for (i
= 0; i
< sizeof config
; ++i
)
189 if ((config
[i
] ^ s
->config
[i
]) & s
->cmask
[i
] & ~s
->wmask
[i
])
191 memcpy(s
->config
, config
, sizeof config
);
193 pci_update_mappings(s
);
196 for (i
= 0; i
< 4; i
++)
197 s
->irq_state
[i
] = qemu_get_be32(f
);
201 static int pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
205 id
= (void*)(&pci_dev
->config
[PCI_SUBVENDOR_ID
]);
206 id
[0] = cpu_to_le16(pci_default_sub_vendor_id
);
207 id
[1] = cpu_to_le16(pci_default_sub_device_id
);
212 * Parse pci address in qemu command
213 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
215 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
220 unsigned long dom
= 0, bus
= 0;
224 val
= strtoul(p
, &e
, 16);
230 val
= strtoul(p
, &e
, 16);
237 val
= strtoul(p
, &e
, 16);
243 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
251 /* Note: QEMU doesn't implement domains other than 0 */
252 if (dom
!= 0 || pci_find_bus(bus
) == NULL
)
262 * Parse device bdf in device assignment command:
264 * -pcidevice host=bus:dev.func
266 * Parse <bus>:<slot>.<func> return -1 on error
268 int pci_parse_host_devaddr(const char *addr
, int *busp
,
269 int *slotp
, int *funcp
)
274 int bus
= 0, slot
= 0, func
= 0;
277 val
= strtoul(p
, &e
, 16);
283 val
= strtoul(p
, &e
, 16);
289 val
= strtoul(p
, &e
, 16);
298 if (bus
> 0xff || slot
> 0x1f || func
> 0x7)
310 int pci_read_devaddr(Monitor
*mon
, const char *addr
, int *domp
, int *busp
,
313 /* strip legacy tag */
314 if (!strncmp(addr
, "pci_addr=", 9)) {
317 if (pci_parse_devaddr(addr
, domp
, busp
, slotp
)) {
318 monitor_printf(mon
, "Invalid pci address\n");
324 PCIBus
*pci_get_bus_devfn(int *devfnp
, const char *devaddr
)
331 return pci_find_bus(0);
334 if (pci_parse_devaddr(devaddr
, &dom
, &bus
, &slot
) < 0) {
339 return pci_find_bus(bus
);
342 static void pci_init_cmask(PCIDevice
*dev
)
344 pci_set_word(dev
->cmask
+ PCI_VENDOR_ID
, 0xffff);
345 pci_set_word(dev
->cmask
+ PCI_DEVICE_ID
, 0xffff);
346 dev
->cmask
[PCI_STATUS
] = PCI_STATUS_CAP_LIST
;
347 dev
->cmask
[PCI_REVISION_ID
] = 0xff;
348 dev
->cmask
[PCI_CLASS_PROG
] = 0xff;
349 pci_set_word(dev
->cmask
+ PCI_CLASS_DEVICE
, 0xffff);
350 dev
->cmask
[PCI_HEADER_TYPE
] = 0xff;
351 dev
->cmask
[PCI_CAPABILITY_LIST
] = 0xff;
354 static void pci_init_wmask(PCIDevice
*dev
)
357 dev
->wmask
[PCI_CACHE_LINE_SIZE
] = 0xff;
358 dev
->wmask
[PCI_INTERRUPT_LINE
] = 0xff;
359 dev
->wmask
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
360 | PCI_COMMAND_MASTER
;
361 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
362 dev
->wmask
[i
] = 0xff;
365 /* -1 for devfn means auto assign */
366 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
367 const char *name
, int devfn
,
368 PCIConfigReadFunc
*config_read
,
369 PCIConfigWriteFunc
*config_write
)
372 for(devfn
= bus
->devfn_min
; devfn
< 256; devfn
+= 8) {
373 if (!bus
->devices
[devfn
])
378 } else if (bus
->devices
[devfn
]) {
382 pci_dev
->devfn
= devfn
;
383 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
384 memset(pci_dev
->irq_state
, 0, sizeof(pci_dev
->irq_state
));
385 pci_set_default_subsystem_id(pci_dev
);
386 pci_init_cmask(pci_dev
);
387 pci_init_wmask(pci_dev
);
390 config_read
= pci_default_read_config
;
392 config_write
= pci_default_write_config
;
393 pci_dev
->config_read
= config_read
;
394 pci_dev
->config_write
= config_write
;
395 bus
->devices
[devfn
] = pci_dev
;
396 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, 4);
400 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
401 int instance_size
, int devfn
,
402 PCIConfigReadFunc
*config_read
,
403 PCIConfigWriteFunc
*config_write
)
407 pci_dev
= qemu_mallocz(instance_size
);
408 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
409 config_read
, config_write
);
412 static target_phys_addr_t
pci_to_cpu_addr(target_phys_addr_t addr
)
414 return addr
+ pci_mem_base
;
417 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
422 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
423 r
= &pci_dev
->io_regions
[i
];
424 if (!r
->size
|| r
->addr
== -1)
426 if (r
->type
== PCI_ADDRESS_SPACE_IO
) {
427 isa_unassign_ioport(r
->addr
, r
->size
);
429 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
436 int pci_unregister_device(PCIDevice
*pci_dev
, int assigned
)
440 if (pci_dev
->unregister
)
441 ret
= pci_dev
->unregister(pci_dev
);
445 pci_unregister_io_regions(pci_dev
);
447 qemu_free_irqs(pci_dev
->irq
);
448 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
453 qdev_free(&pci_dev
->qdev
);
457 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
458 uint32_t size
, int type
,
459 PCIMapIORegionFunc
*map_func
)
465 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
468 if (size
& (size
-1)) {
469 fprintf(stderr
, "ERROR: PCI region size must be pow2 "
470 "type=0x%x, size=0x%x\n", type
, size
);
474 r
= &pci_dev
->io_regions
[region_num
];
478 r
->map_func
= map_func
;
481 if (region_num
== PCI_ROM_SLOT
) {
483 /* ROM enable bit is writeable */
486 addr
= 0x10 + region_num
* 4;
488 *(uint32_t *)(pci_dev
->config
+ addr
) = cpu_to_le32(type
);
489 *(uint32_t *)(pci_dev
->wmask
+ addr
) = cpu_to_le32(wmask
);
490 *(uint32_t *)(pci_dev
->cmask
+ addr
) = 0xffffffff;
493 static void pci_update_mappings(PCIDevice
*d
)
497 uint32_t last_addr
, new_addr
, config_ofs
;
499 cmd
= le16_to_cpu(*(uint16_t *)(d
->config
+ PCI_COMMAND
));
500 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
501 r
= &d
->io_regions
[i
];
502 if (i
== PCI_ROM_SLOT
) {
505 config_ofs
= 0x10 + i
* 4;
508 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
509 if (cmd
& PCI_COMMAND_IO
) {
510 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
512 new_addr
= new_addr
& ~(r
->size
- 1);
513 last_addr
= new_addr
+ r
->size
- 1;
514 /* NOTE: we have only 64K ioports on PC */
515 if (last_addr
<= new_addr
|| new_addr
== 0 ||
516 last_addr
>= 0x10000) {
523 if (cmd
& PCI_COMMAND_MEMORY
) {
524 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
526 /* the ROM slot has a specific enable bit */
527 if (i
== PCI_ROM_SLOT
&& !(new_addr
& 1))
529 new_addr
= new_addr
& ~(r
->size
- 1);
530 last_addr
= new_addr
+ r
->size
- 1;
531 /* NOTE: we do not support wrapping */
532 /* XXX: as we cannot support really dynamic
533 mappings, we handle specific values as invalid
535 if (last_addr
<= new_addr
|| new_addr
== 0 ||
544 /* now do the real mapping */
545 if (new_addr
!= r
->addr
) {
547 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
549 /* NOTE: specific hack for IDE in PC case:
550 only one byte must be mapped. */
551 class = d
->config
[0x0a] | (d
->config
[0x0b] << 8);
552 if (class == 0x0101 && r
->size
== 4) {
553 isa_unassign_ioport(r
->addr
+ 2, 1);
555 isa_unassign_ioport(r
->addr
, r
->size
);
558 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
561 qemu_unregister_coalesced_mmio(r
->addr
, r
->size
);
566 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
573 static uint32_t pci_read_config(PCIDevice
*d
,
574 uint32_t address
, int len
)
581 if (address
<= 0xfc) {
582 val
= le32_to_cpu(*(uint32_t *)(d
->config
+ address
));
587 if (address
<= 0xfe) {
588 val
= le16_to_cpu(*(uint16_t *)(d
->config
+ address
));
593 val
= d
->config
[address
];
599 static void pci_write_config(PCIDevice
*pci_dev
,
600 uint32_t address
, uint32_t val
, int len
)
603 for (i
= 0; i
< len
; i
++) {
604 pci_dev
->config
[address
+ i
] = val
& 0xff;
609 int pci_access_cap_config(PCIDevice
*pci_dev
, uint32_t address
, int len
)
611 if (pci_dev
->cap
.supported
&& address
>= pci_dev
->cap
.start
&&
612 (address
+ len
) < pci_dev
->cap
.start
+ pci_dev
->cap
.length
)
617 uint32_t pci_default_cap_read_config(PCIDevice
*pci_dev
,
618 uint32_t address
, int len
)
620 return pci_read_config(pci_dev
, address
, len
);
623 void pci_default_cap_write_config(PCIDevice
*pci_dev
,
624 uint32_t address
, uint32_t val
, int len
)
626 pci_write_config(pci_dev
, address
, val
, len
);
629 uint32_t pci_default_read_config(PCIDevice
*d
,
630 uint32_t address
, int len
)
632 if (pci_access_cap_config(d
, address
, len
))
633 return d
->cap
.config_read(d
, address
, len
);
635 return pci_read_config(d
, address
, len
);
638 void pci_default_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int l
)
640 uint8_t orig
[PCI_CONFIG_SPACE_SIZE
];
643 if (pci_access_cap_config(d
, addr
, l
)) {
644 d
->cap
.config_write(d
, addr
, val
, l
);
648 /* not efficient, but simple */
649 memcpy(orig
, d
->config
, PCI_CONFIG_SPACE_SIZE
);
650 for(i
= 0; i
< l
&& addr
< PCI_CONFIG_SPACE_SIZE
; val
>>= 8, ++i
, ++addr
) {
651 uint8_t wmask
= d
->wmask
[addr
];
652 d
->config
[addr
] = (d
->config
[addr
] & ~wmask
) | (val
& wmask
);
655 #ifdef USE_KVM_DEVICE_ASSIGNMENT
656 if (kvm_enabled() && qemu_kvm_irqchip_in_kernel() &&
657 addr
>= PIIX_CONFIG_IRQ_ROUTE
&&
658 addr
< PIIX_CONFIG_IRQ_ROUTE
+ 4)
659 assigned_dev_update_irqs();
660 #endif /* USE_KVM_DEVICE_ASSIGNMENT */
662 if (memcmp(orig
+ PCI_BASE_ADDRESS_0
, d
->config
+ PCI_BASE_ADDRESS_0
, 24)
663 || ((orig
[PCI_COMMAND
] ^ d
->config
[PCI_COMMAND
])
664 & (PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
)))
665 pci_update_mappings(d
);
669 void pci_data_write(void *opaque
, uint32_t addr
, uint32_t val
, int len
)
673 int config_addr
, bus_num
;
676 PCI_DPRINTF("pci_data_write: addr=%08x val=%08x len=%d\n",
679 bus_num
= (addr
>> 16) & 0xff;
680 while (s
&& s
->bus_num
!= bus_num
)
684 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
687 config_addr
= addr
& 0xff;
688 PCI_DPRINTF("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
689 pci_dev
->name
, config_addr
, val
, len
);
690 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
693 uint32_t pci_data_read(void *opaque
, uint32_t addr
, int len
)
697 int config_addr
, bus_num
;
700 bus_num
= (addr
>> 16) & 0xff;
701 while (s
&& s
->bus_num
!= bus_num
)
705 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
722 config_addr
= addr
& 0xff;
723 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
724 PCI_DPRINTF("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
725 pci_dev
->name
, config_addr
, val
, len
);
728 PCI_DPRINTF("pci_data_read: addr=%08x val=%08x len=%d\n",
734 /***********************************************************/
735 /* generic PCI irq support */
737 /* 0 <= irq_num <= 3. level must be 0 or 1 */
738 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
740 PCIDevice
*pci_dev
= (PCIDevice
*)opaque
;
744 change
= level
- pci_dev
->irq_state
[irq_num
];
748 pci_dev
->irq_state
[irq_num
] = level
;
750 #if defined(TARGET_IA64)
751 ioapic_set_irq(pci_dev
, irq_num
, level
);
756 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
759 pci_dev
= bus
->parent_dev
;
761 bus
->irq_count
[irq_num
] += change
;
762 bus
->set_irq(bus
->irq_opaque
, irq_num
, bus
->irq_count
[irq_num
] != 0);
765 int pci_map_irq(PCIDevice
*pci_dev
, int pin
)
767 return pci_dev
->bus
->map_irq(pci_dev
, pin
);
770 /***********************************************************/
771 /* monitor info on PCI */
778 static const pci_class_desc pci_class_descriptions
[] =
780 { 0x0100, "SCSI controller"},
781 { 0x0101, "IDE controller"},
782 { 0x0102, "Floppy controller"},
783 { 0x0103, "IPI controller"},
784 { 0x0104, "RAID controller"},
785 { 0x0106, "SATA controller"},
786 { 0x0107, "SAS controller"},
787 { 0x0180, "Storage controller"},
788 { 0x0200, "Ethernet controller"},
789 { 0x0201, "Token Ring controller"},
790 { 0x0202, "FDDI controller"},
791 { 0x0203, "ATM controller"},
792 { 0x0280, "Network controller"},
793 { 0x0300, "VGA controller"},
794 { 0x0301, "XGA controller"},
795 { 0x0302, "3D controller"},
796 { 0x0380, "Display controller"},
797 { 0x0400, "Video controller"},
798 { 0x0401, "Audio controller"},
800 { 0x0480, "Multimedia controller"},
801 { 0x0500, "RAM controller"},
802 { 0x0501, "Flash controller"},
803 { 0x0580, "Memory controller"},
804 { 0x0600, "Host bridge"},
805 { 0x0601, "ISA bridge"},
806 { 0x0602, "EISA bridge"},
807 { 0x0603, "MC bridge"},
808 { 0x0604, "PCI bridge"},
809 { 0x0605, "PCMCIA bridge"},
810 { 0x0606, "NUBUS bridge"},
811 { 0x0607, "CARDBUS bridge"},
812 { 0x0608, "RACEWAY bridge"},
814 { 0x0c03, "USB controller"},
818 static void pci_info_device(PCIDevice
*d
)
820 Monitor
*mon
= cur_mon
;
823 const pci_class_desc
*desc
;
825 monitor_printf(mon
, " Bus %2d, device %3d, function %d:\n",
826 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7);
827 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
828 monitor_printf(mon
, " ");
829 desc
= pci_class_descriptions
;
830 while (desc
->desc
&& class != desc
->class)
833 monitor_printf(mon
, "%s", desc
->desc
);
835 monitor_printf(mon
, "Class %04x", class);
837 monitor_printf(mon
, ": PCI device %04x:%04x\n",
838 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
839 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))));
841 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
842 monitor_printf(mon
, " IRQ %d.\n",
843 d
->config
[PCI_INTERRUPT_LINE
]);
845 if (class == 0x0604) {
846 monitor_printf(mon
, " BUS %d.\n", d
->config
[0x19]);
848 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
849 r
= &d
->io_regions
[i
];
851 monitor_printf(mon
, " BAR%d: ", i
);
852 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
853 monitor_printf(mon
, "I/O at 0x%04x [0x%04x].\n",
854 r
->addr
, r
->addr
+ r
->size
- 1);
856 monitor_printf(mon
, "32 bit memory at 0x%08x [0x%08x].\n",
857 r
->addr
, r
->addr
+ r
->size
- 1);
861 monitor_printf(mon
, " id \"%s\"\n", d
->qdev
.id
? d
->qdev
.id
: "");
862 if (class == 0x0604 && d
->config
[0x19] != 0) {
863 pci_for_each_device(d
->config
[0x19], pci_info_device
);
867 void pci_for_each_device(int bus_num
, void (*fn
)(PCIDevice
*d
))
869 PCIBus
*bus
= first_bus
;
873 while (bus
&& bus
->bus_num
!= bus_num
)
876 for(devfn
= 0; devfn
< 256; devfn
++) {
877 d
= bus
->devices
[devfn
];
884 void pci_info(Monitor
*mon
)
886 pci_for_each_device(0, pci_info_device
);
889 PCIDevice
*pci_create(const char *name
, const char *devaddr
)
895 bus
= pci_get_bus_devfn(&devfn
, devaddr
);
897 fprintf(stderr
, "Invalid PCI device address %s for device %s\n",
902 dev
= qdev_create(&bus
->qbus
, name
);
903 qdev_prop_set_uint32(dev
, "devfn", devfn
);
904 return (PCIDevice
*)dev
;
907 static const char * const pci_nic_models
[] = {
919 static const char * const pci_nic_names
[] = {
931 /* Initialize a PCI NIC. */
932 PCIDevice
*pci_nic_init(NICInfo
*nd
, const char *default_model
,
933 const char *default_devaddr
)
935 const char *devaddr
= nd
->devaddr
? nd
->devaddr
: default_devaddr
;
940 qemu_check_nic_model_list(nd
, pci_nic_models
, default_model
);
942 for (i
= 0; pci_nic_models
[i
]; i
++) {
943 if (strcmp(nd
->model
, pci_nic_models
[i
]) == 0) {
944 pci_dev
= pci_create(pci_nic_names
[i
], devaddr
);
945 dev
= &pci_dev
->qdev
;
947 dev
->id
= qemu_strdup(nd
->id
);
963 static void pci_bridge_write_config(PCIDevice
*d
,
964 uint32_t address
, uint32_t val
, int len
)
966 PCIBridge
*s
= (PCIBridge
*)d
;
968 pci_default_write_config(d
, address
, val
, len
);
969 s
->bus
->bus_num
= d
->config
[PCI_SECONDARY_BUS
];
972 PCIBus
*pci_find_bus(int bus_num
)
974 PCIBus
*bus
= first_bus
;
976 while (bus
&& bus
->bus_num
!= bus_num
)
982 PCIDevice
*pci_find_device(int bus_num
, int slot
, int function
)
984 PCIBus
*bus
= pci_find_bus(bus_num
);
989 return bus
->devices
[PCI_DEVFN(slot
, function
)];
992 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, uint16_t vid
, uint16_t did
,
993 pci_map_irq_fn map_irq
, const char *name
)
996 s
= (PCIBridge
*)pci_register_device(bus
, name
, sizeof(PCIBridge
),
997 devfn
, NULL
, pci_bridge_write_config
);
999 pci_config_set_vendor_id(s
->dev
.config
, vid
);
1000 pci_config_set_device_id(s
->dev
.config
, did
);
1002 s
->dev
.config
[0x04] = 0x06; // command = bus master, pci mem
1003 s
->dev
.config
[0x05] = 0x00;
1004 s
->dev
.config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
1005 s
->dev
.config
[0x07] = 0x00; // status = fast devsel
1006 s
->dev
.config
[0x08] = 0x00; // revision
1007 s
->dev
.config
[0x09] = 0x00; // programming i/f
1008 pci_config_set_class(s
->dev
.config
, PCI_CLASS_BRIDGE_PCI
);
1009 s
->dev
.config
[0x0D] = 0x10; // latency_timer
1010 s
->dev
.config
[PCI_HEADER_TYPE
] =
1011 PCI_HEADER_TYPE_MULTI_FUNCTION
| PCI_HEADER_TYPE_BRIDGE
; // header_type
1012 s
->dev
.config
[0x1E] = 0xa0; // secondary status
1014 s
->bus
= pci_register_secondary_bus(&s
->dev
, map_irq
);
1018 static void pci_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
1020 PCIDevice
*pci_dev
= (PCIDevice
*)qdev
;
1021 PCIDeviceInfo
*info
= container_of(base
, PCIDeviceInfo
, qdev
);
1025 bus
= FROM_QBUS(PCIBus
, qdev_get_parent_bus(qdev
));
1026 devfn
= pci_dev
->devfn
;
1027 pci_dev
= do_pci_register_device(pci_dev
, bus
, base
->name
, devfn
,
1028 info
->config_read
, info
->config_write
);
1030 info
->init(pci_dev
);
1033 void pci_qdev_register(PCIDeviceInfo
*info
)
1035 info
->qdev
.init
= pci_qdev_init
;
1036 info
->qdev
.bus_info
= &pci_bus_info
;
1037 qdev_register(&info
->qdev
);
1040 void pci_qdev_register_many(PCIDeviceInfo
*info
)
1042 while (info
->qdev
.name
) {
1043 pci_qdev_register(info
);
1048 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
1052 dev
= qdev_create(&bus
->qbus
, name
);
1053 qdev_prop_set_uint32(dev
, "devfn", devfn
);
1056 return (PCIDevice
*)dev
;
1059 int pci_enable_capability_support(PCIDevice
*pci_dev
,
1060 uint32_t config_start
,
1061 PCICapConfigReadFunc
*config_read
,
1062 PCICapConfigWriteFunc
*config_write
,
1063 PCICapConfigInitFunc
*config_init
)
1068 pci_dev
->config
[0x06] |= 0x10; // status = capabilities
1070 if (config_start
== 0)
1071 pci_dev
->cap
.start
= PCI_CAPABILITY_CONFIG_DEFAULT_START_ADDR
;
1072 else if (config_start
>= 0x40 && config_start
< 0xff)
1073 pci_dev
->cap
.start
= config_start
;
1078 pci_dev
->cap
.config_read
= config_read
;
1080 pci_dev
->cap
.config_read
= pci_default_cap_read_config
;
1082 pci_dev
->cap
.config_write
= config_write
;
1084 pci_dev
->cap
.config_write
= pci_default_cap_write_config
;
1085 pci_dev
->cap
.supported
= 1;
1086 pci_dev
->config
[PCI_CAPABILITY_LIST
] = pci_dev
->cap
.start
;
1087 return config_init(pci_dev
);
1090 static int pci_find_space(PCIDevice
*pdev
, uint8_t size
)
1092 int offset
= PCI_CONFIG_HEADER_SIZE
;
1094 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
1097 else if (i
- offset
+ 1 == size
)
1102 static uint8_t pci_find_capability_list(PCIDevice
*pdev
, uint8_t cap_id
,
1107 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
))
1110 for (prev
= PCI_CAPABILITY_LIST
; (next
= pdev
->config
[prev
]);
1111 prev
= next
+ PCI_CAP_LIST_NEXT
)
1112 if (pdev
->config
[next
+ PCI_CAP_LIST_ID
] == cap_id
)
1120 /* Reserve space and add capability to the linked list in pci config space */
1121 int pci_add_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1123 uint8_t offset
= pci_find_space(pdev
, size
);
1124 uint8_t *config
= pdev
->config
+ offset
;
1127 config
[PCI_CAP_LIST_ID
] = cap_id
;
1128 config
[PCI_CAP_LIST_NEXT
] = pdev
->config
[PCI_CAPABILITY_LIST
];
1129 pdev
->config
[PCI_CAPABILITY_LIST
] = offset
;
1130 pdev
->config
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
1131 memset(pdev
->used
+ offset
, 0xFF, size
);
1132 /* Make capability read-only by default */
1133 memset(pdev
->wmask
+ offset
, 0, size
);
1134 /* Check capability by default */
1135 memset(pdev
->cmask
+ offset
, 0xFF, size
);
1139 /* Unlink capability from the pci config space. */
1140 void pci_del_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1142 uint8_t prev
, offset
= pci_find_capability_list(pdev
, cap_id
, &prev
);
1145 pdev
->config
[prev
] = pdev
->config
[offset
+ PCI_CAP_LIST_NEXT
];
1146 /* Make capability writeable again */
1147 memset(pdev
->wmask
+ offset
, 0xff, size
);
1148 /* Clear cmask as device-specific registers can't be checked */
1149 memset(pdev
->cmask
+ offset
, 0, size
);
1150 memset(pdev
->used
+ offset
, 0, size
);
1152 if (!pdev
->config
[PCI_CAPABILITY_LIST
])
1153 pdev
->config
[PCI_STATUS
] &= ~PCI_STATUS_CAP_LIST
;
1156 /* Reserve space for capability at a known offset (to call after load). */
1157 void pci_reserve_capability(PCIDevice
*pdev
, uint8_t offset
, uint8_t size
)
1159 memset(pdev
->used
+ offset
, 0xff, size
);
1162 uint8_t pci_find_capability(PCIDevice
*pdev
, uint8_t cap_id
)
1164 return pci_find_capability_list(pdev
, cap_id
, NULL
);
1167 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
1169 PCIDevice
*d
= (PCIDevice
*)dev
;
1170 const pci_class_desc
*desc
;
1175 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
1176 desc
= pci_class_descriptions
;
1177 while (desc
->desc
&& class != desc
->class)
1180 snprintf(ctxt
, sizeof(ctxt
), "%s", desc
->desc
);
1182 snprintf(ctxt
, sizeof(ctxt
), "Class %04x", class);
1185 monitor_printf(mon
, "%*sclass %s, addr %02x:%02x.%x, "
1186 "pci id %04x:%04x (sub %04x:%04x)\n",
1188 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7,
1189 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
1190 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))),
1191 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_SUBSYSTEM_VENDOR_ID
))),
1192 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_SUBSYSTEM_ID
))));
1193 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1194 r
= &d
->io_regions
[i
];
1197 monitor_printf(mon
, "%*sbar %d: %s at 0x%x [0x%x]\n", indent
, "",
1198 i
, r
->type
& PCI_ADDRESS_SPACE_IO
? "i/o" : "mem",
1199 r
->addr
, r
->addr
+ r
->size
- 1);