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
32 # define PCI_DPRINTF(format, ...) printf(format, __VA_ARGS__)
34 # define PCI_DPRINTF(format, ...) do { } while (0)
41 pci_set_irq_fn set_irq
;
42 pci_map_irq_fn map_irq
;
43 uint32_t config_reg
; /* XXX: suppress */
45 SetIRQFunc
*low_set_irq
;
47 PCIDevice
*devices
[256];
48 PCIDevice
*parent_dev
;
50 /* The bus IRQ state is the logical OR of the connected devices.
51 Keep a count of the number of devices with raised IRQs. */
56 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
58 static struct BusInfo pci_bus_info
= {
60 .size
= sizeof(PCIBus
),
61 .print_dev
= pcibus_dev_print
,
62 .props
= (Property
[]) {
63 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice
, devfn
, -1),
64 DEFINE_PROP_END_OF_LIST()
68 static void pci_update_mappings(PCIDevice
*d
);
69 static void pci_set_irq(void *opaque
, int irq_num
, int level
);
71 target_phys_addr_t pci_mem_base
;
72 static uint16_t pci_default_sub_vendor_id
= PCI_SUBVENDOR_ID_REDHAT_QUMRANET
;
73 static uint16_t pci_default_sub_device_id
= PCI_SUBDEVICE_ID_QEMU
;
74 static PCIBus
*first_bus
;
76 static const VMStateDescription vmstate_pcibus
= {
79 .minimum_version_id
= 1,
80 .minimum_version_id_old
= 1,
81 .fields
= (VMStateField
[]) {
82 VMSTATE_INT32_EQUAL(nirq
, PCIBus
),
83 VMSTATE_INT32_VARRAY(irq_count
, PCIBus
, nirq
),
88 static void pci_bus_reset(void *opaque
)
90 PCIBus
*bus
= (PCIBus
*)opaque
;
93 for (i
= 0; i
< bus
->nirq
; i
++) {
94 bus
->irq_count
[i
] = 0;
96 for (i
= 0; i
< 256; i
++) {
98 memset(bus
->devices
[i
]->irq_state
, 0,
99 sizeof(bus
->devices
[i
]->irq_state
));
103 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
104 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
105 qemu_irq
*pic
, int devfn_min
, int nirq
)
110 bus
= FROM_QBUS(PCIBus
, qbus_create(&pci_bus_info
, parent
, name
));
111 bus
->set_irq
= set_irq
;
112 bus
->map_irq
= map_irq
;
113 bus
->irq_opaque
= pic
;
114 bus
->devfn_min
= devfn_min
;
116 bus
->irq_count
= qemu_mallocz(nirq
* sizeof(bus
->irq_count
[0]));
117 bus
->next
= first_bus
;
119 vmstate_register(nbus
++, &vmstate_pcibus
, bus
);
120 qemu_register_reset(pci_bus_reset
, bus
);
124 static PCIBus
*pci_register_secondary_bus(PCIDevice
*dev
,
125 pci_map_irq_fn map_irq
,
130 bus
= FROM_QBUS(PCIBus
, qbus_create(&pci_bus_info
, &dev
->qdev
, name
));
131 bus
->map_irq
= map_irq
;
132 bus
->parent_dev
= dev
;
133 bus
->next
= dev
->bus
->next
;
134 dev
->bus
->next
= bus
;
138 int pci_bus_num(PCIBus
*s
)
143 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
147 qemu_put_be32(f
, s
->version_id
); /* PCI device version */
148 qemu_put_buffer(f
, s
->config
, 256);
149 for (i
= 0; i
< 4; i
++)
150 qemu_put_be32(f
, s
->irq_state
[i
]);
153 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
155 uint8_t config
[PCI_CONFIG_SPACE_SIZE
];
159 version_id
= qemu_get_be32(f
);
162 qemu_get_buffer(f
, config
, sizeof config
);
163 for (i
= 0; i
< sizeof config
; ++i
)
164 if ((config
[i
] ^ s
->config
[i
]) & s
->cmask
[i
] & ~s
->wmask
[i
])
166 memcpy(s
->config
, config
, sizeof config
);
168 pci_update_mappings(s
);
171 for (i
= 0; i
< 4; i
++)
172 s
->irq_state
[i
] = qemu_get_be32(f
);
176 static int pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
180 id
= (void*)(&pci_dev
->config
[PCI_SUBVENDOR_ID
]);
181 id
[0] = cpu_to_le16(pci_default_sub_vendor_id
);
182 id
[1] = cpu_to_le16(pci_default_sub_device_id
);
187 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
189 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
194 unsigned long dom
= 0, bus
= 0;
198 val
= strtoul(p
, &e
, 16);
204 val
= strtoul(p
, &e
, 16);
211 val
= strtoul(p
, &e
, 16);
217 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
225 /* Note: QEMU doesn't implement domains other than 0 */
226 if (dom
!= 0 || pci_find_bus(bus
) == NULL
)
235 int pci_read_devaddr(Monitor
*mon
, const char *addr
, int *domp
, int *busp
,
238 /* strip legacy tag */
239 if (!strncmp(addr
, "pci_addr=", 9)) {
242 if (pci_parse_devaddr(addr
, domp
, busp
, slotp
)) {
243 monitor_printf(mon
, "Invalid pci address\n");
249 static PCIBus
*pci_get_bus_devfn(int *devfnp
, const char *devaddr
)
256 return pci_find_bus(0);
259 if (pci_parse_devaddr(devaddr
, &dom
, &bus
, &slot
) < 0) {
264 return pci_find_bus(bus
);
267 static void pci_init_cmask(PCIDevice
*dev
)
269 pci_set_word(dev
->cmask
+ PCI_VENDOR_ID
, 0xffff);
270 pci_set_word(dev
->cmask
+ PCI_DEVICE_ID
, 0xffff);
271 dev
->cmask
[PCI_STATUS
] = PCI_STATUS_CAP_LIST
;
272 dev
->cmask
[PCI_REVISION_ID
] = 0xff;
273 dev
->cmask
[PCI_CLASS_PROG
] = 0xff;
274 pci_set_word(dev
->cmask
+ PCI_CLASS_DEVICE
, 0xffff);
275 dev
->cmask
[PCI_HEADER_TYPE
] = 0xff;
276 dev
->cmask
[PCI_CAPABILITY_LIST
] = 0xff;
279 static void pci_init_wmask(PCIDevice
*dev
)
282 dev
->wmask
[PCI_CACHE_LINE_SIZE
] = 0xff;
283 dev
->wmask
[PCI_INTERRUPT_LINE
] = 0xff;
284 dev
->wmask
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
285 | PCI_COMMAND_MASTER
;
286 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
287 dev
->wmask
[i
] = 0xff;
290 /* -1 for devfn means auto assign */
291 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
292 const char *name
, int devfn
,
293 PCIConfigReadFunc
*config_read
,
294 PCIConfigWriteFunc
*config_write
)
297 for(devfn
= bus
->devfn_min
; devfn
< 256; devfn
+= 8) {
298 if (!bus
->devices
[devfn
])
303 } else if (bus
->devices
[devfn
]) {
307 pci_dev
->devfn
= devfn
;
308 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
309 memset(pci_dev
->irq_state
, 0, sizeof(pci_dev
->irq_state
));
310 pci_set_default_subsystem_id(pci_dev
);
311 pci_init_cmask(pci_dev
);
312 pci_init_wmask(pci_dev
);
315 config_read
= pci_default_read_config
;
317 config_write
= pci_default_write_config
;
318 pci_dev
->config_read
= config_read
;
319 pci_dev
->config_write
= config_write
;
320 bus
->devices
[devfn
] = pci_dev
;
321 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, 4);
322 pci_dev
->version_id
= 2; /* Current pci device vmstate version */
326 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
327 int instance_size
, int devfn
,
328 PCIConfigReadFunc
*config_read
,
329 PCIConfigWriteFunc
*config_write
)
333 pci_dev
= qemu_mallocz(instance_size
);
334 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
335 config_read
, config_write
);
338 static target_phys_addr_t
pci_to_cpu_addr(target_phys_addr_t addr
)
340 return addr
+ pci_mem_base
;
343 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
348 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
349 r
= &pci_dev
->io_regions
[i
];
350 if (!r
->size
|| r
->addr
== -1)
352 if (r
->type
== PCI_ADDRESS_SPACE_IO
) {
353 isa_unassign_ioport(r
->addr
, r
->size
);
355 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
362 int pci_unregister_device(PCIDevice
*pci_dev
)
366 if (pci_dev
->unregister
)
367 ret
= pci_dev
->unregister(pci_dev
);
371 pci_unregister_io_regions(pci_dev
);
373 qemu_free_irqs(pci_dev
->irq
);
374 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
375 qdev_free(&pci_dev
->qdev
);
379 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
380 uint32_t size
, int type
,
381 PCIMapIORegionFunc
*map_func
)
387 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
390 if (size
& (size
-1)) {
391 fprintf(stderr
, "ERROR: PCI region size must be pow2 "
392 "type=0x%x, size=0x%x\n", type
, size
);
396 r
= &pci_dev
->io_regions
[region_num
];
400 r
->map_func
= map_func
;
403 if (region_num
== PCI_ROM_SLOT
) {
405 /* ROM enable bit is writeable */
408 addr
= 0x10 + region_num
* 4;
410 *(uint32_t *)(pci_dev
->config
+ addr
) = cpu_to_le32(type
);
411 *(uint32_t *)(pci_dev
->wmask
+ addr
) = cpu_to_le32(wmask
);
412 *(uint32_t *)(pci_dev
->cmask
+ addr
) = 0xffffffff;
415 static void pci_update_mappings(PCIDevice
*d
)
419 uint32_t last_addr
, new_addr
, config_ofs
;
421 cmd
= le16_to_cpu(*(uint16_t *)(d
->config
+ PCI_COMMAND
));
422 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
423 r
= &d
->io_regions
[i
];
424 if (i
== PCI_ROM_SLOT
) {
427 config_ofs
= 0x10 + i
* 4;
430 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
431 if (cmd
& PCI_COMMAND_IO
) {
432 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
434 new_addr
= new_addr
& ~(r
->size
- 1);
435 last_addr
= new_addr
+ r
->size
- 1;
436 /* NOTE: we have only 64K ioports on PC */
437 if (last_addr
<= new_addr
|| new_addr
== 0 ||
438 last_addr
>= 0x10000) {
445 if (cmd
& PCI_COMMAND_MEMORY
) {
446 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
448 /* the ROM slot has a specific enable bit */
449 if (i
== PCI_ROM_SLOT
&& !(new_addr
& 1))
451 new_addr
= new_addr
& ~(r
->size
- 1);
452 last_addr
= new_addr
+ r
->size
- 1;
453 /* NOTE: we do not support wrapping */
454 /* XXX: as we cannot support really dynamic
455 mappings, we handle specific values as invalid
457 if (last_addr
<= new_addr
|| new_addr
== 0 ||
466 /* now do the real mapping */
467 if (new_addr
!= r
->addr
) {
469 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
471 /* NOTE: specific hack for IDE in PC case:
472 only one byte must be mapped. */
473 class = d
->config
[0x0a] | (d
->config
[0x0b] << 8);
474 if (class == 0x0101 && r
->size
== 4) {
475 isa_unassign_ioport(r
->addr
+ 2, 1);
477 isa_unassign_ioport(r
->addr
, r
->size
);
480 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
483 qemu_unregister_coalesced_mmio(r
->addr
, r
->size
);
488 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
495 uint32_t pci_default_read_config(PCIDevice
*d
,
496 uint32_t address
, int len
)
503 if (address
<= 0xfc) {
504 val
= le32_to_cpu(*(uint32_t *)(d
->config
+ address
));
509 if (address
<= 0xfe) {
510 val
= le16_to_cpu(*(uint16_t *)(d
->config
+ address
));
515 val
= d
->config
[address
];
521 void pci_default_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int l
)
523 uint8_t orig
[PCI_CONFIG_SPACE_SIZE
];
526 /* not efficient, but simple */
527 memcpy(orig
, d
->config
, PCI_CONFIG_SPACE_SIZE
);
528 for(i
= 0; i
< l
&& addr
< PCI_CONFIG_SPACE_SIZE
; val
>>= 8, ++i
, ++addr
) {
529 uint8_t wmask
= d
->wmask
[addr
];
530 d
->config
[addr
] = (d
->config
[addr
] & ~wmask
) | (val
& wmask
);
532 if (memcmp(orig
+ PCI_BASE_ADDRESS_0
, d
->config
+ PCI_BASE_ADDRESS_0
, 24)
533 || ((orig
[PCI_COMMAND
] ^ d
->config
[PCI_COMMAND
])
534 & (PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
)))
535 pci_update_mappings(d
);
538 void pci_data_write(void *opaque
, uint32_t addr
, uint32_t val
, int len
)
542 int config_addr
, bus_num
;
545 PCI_DPRINTF("pci_data_write: addr=%08x val=%08x len=%d\n",
548 bus_num
= (addr
>> 16) & 0xff;
549 while (s
&& s
->bus_num
!= bus_num
)
553 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
556 config_addr
= addr
& 0xff;
557 PCI_DPRINTF("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
558 pci_dev
->name
, config_addr
, val
, len
);
559 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
562 uint32_t pci_data_read(void *opaque
, uint32_t addr
, int len
)
566 int config_addr
, bus_num
;
569 bus_num
= (addr
>> 16) & 0xff;
570 while (s
&& s
->bus_num
!= bus_num
)
574 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
591 config_addr
= addr
& 0xff;
592 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
593 PCI_DPRINTF("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
594 pci_dev
->name
, config_addr
, val
, len
);
597 PCI_DPRINTF("pci_data_read: addr=%08x val=%08x len=%d\n",
603 /***********************************************************/
604 /* generic PCI irq support */
606 /* 0 <= irq_num <= 3. level must be 0 or 1 */
607 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
609 PCIDevice
*pci_dev
= (PCIDevice
*)opaque
;
613 change
= level
- pci_dev
->irq_state
[irq_num
];
617 pci_dev
->irq_state
[irq_num
] = level
;
620 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
623 pci_dev
= bus
->parent_dev
;
625 bus
->irq_count
[irq_num
] += change
;
626 bus
->set_irq(bus
->irq_opaque
, irq_num
, bus
->irq_count
[irq_num
] != 0);
629 /***********************************************************/
630 /* monitor info on PCI */
637 static const pci_class_desc pci_class_descriptions
[] =
639 { 0x0100, "SCSI controller"},
640 { 0x0101, "IDE controller"},
641 { 0x0102, "Floppy controller"},
642 { 0x0103, "IPI controller"},
643 { 0x0104, "RAID controller"},
644 { 0x0106, "SATA controller"},
645 { 0x0107, "SAS controller"},
646 { 0x0180, "Storage controller"},
647 { 0x0200, "Ethernet controller"},
648 { 0x0201, "Token Ring controller"},
649 { 0x0202, "FDDI controller"},
650 { 0x0203, "ATM controller"},
651 { 0x0280, "Network controller"},
652 { 0x0300, "VGA controller"},
653 { 0x0301, "XGA controller"},
654 { 0x0302, "3D controller"},
655 { 0x0380, "Display controller"},
656 { 0x0400, "Video controller"},
657 { 0x0401, "Audio controller"},
659 { 0x0480, "Multimedia controller"},
660 { 0x0500, "RAM controller"},
661 { 0x0501, "Flash controller"},
662 { 0x0580, "Memory controller"},
663 { 0x0600, "Host bridge"},
664 { 0x0601, "ISA bridge"},
665 { 0x0602, "EISA bridge"},
666 { 0x0603, "MC bridge"},
667 { 0x0604, "PCI bridge"},
668 { 0x0605, "PCMCIA bridge"},
669 { 0x0606, "NUBUS bridge"},
670 { 0x0607, "CARDBUS bridge"},
671 { 0x0608, "RACEWAY bridge"},
673 { 0x0c03, "USB controller"},
677 static void pci_info_device(PCIDevice
*d
)
679 Monitor
*mon
= cur_mon
;
682 const pci_class_desc
*desc
;
684 monitor_printf(mon
, " Bus %2d, device %3d, function %d:\n",
685 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7);
686 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
687 monitor_printf(mon
, " ");
688 desc
= pci_class_descriptions
;
689 while (desc
->desc
&& class != desc
->class)
692 monitor_printf(mon
, "%s", desc
->desc
);
694 monitor_printf(mon
, "Class %04x", class);
696 monitor_printf(mon
, ": PCI device %04x:%04x\n",
697 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
698 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))));
700 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
701 monitor_printf(mon
, " IRQ %d.\n",
702 d
->config
[PCI_INTERRUPT_LINE
]);
704 if (class == 0x0604) {
705 monitor_printf(mon
, " BUS %d.\n", d
->config
[0x19]);
707 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
708 r
= &d
->io_regions
[i
];
710 monitor_printf(mon
, " BAR%d: ", i
);
711 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
712 monitor_printf(mon
, "I/O at 0x%04x [0x%04x].\n",
713 r
->addr
, r
->addr
+ r
->size
- 1);
715 monitor_printf(mon
, "32 bit memory at 0x%08x [0x%08x].\n",
716 r
->addr
, r
->addr
+ r
->size
- 1);
720 monitor_printf(mon
, " id \"%s\"\n", d
->qdev
.id
? d
->qdev
.id
: "");
721 if (class == 0x0604 && d
->config
[0x19] != 0) {
722 pci_for_each_device(d
->config
[0x19], pci_info_device
);
726 void pci_for_each_device(int bus_num
, void (*fn
)(PCIDevice
*d
))
728 PCIBus
*bus
= first_bus
;
732 while (bus
&& bus
->bus_num
!= bus_num
)
735 for(devfn
= 0; devfn
< 256; devfn
++) {
736 d
= bus
->devices
[devfn
];
743 void pci_info(Monitor
*mon
)
745 pci_for_each_device(0, pci_info_device
);
748 PCIDevice
*pci_create(const char *name
, const char *devaddr
)
754 bus
= pci_get_bus_devfn(&devfn
, devaddr
);
756 fprintf(stderr
, "Invalid PCI device address %s for device %s\n",
761 dev
= qdev_create(&bus
->qbus
, name
);
762 qdev_prop_set_uint32(dev
, "addr", devfn
);
763 return (PCIDevice
*)dev
;
766 static const char * const pci_nic_models
[] = {
778 static const char * const pci_nic_names
[] = {
790 /* Initialize a PCI NIC. */
791 PCIDevice
*pci_nic_init(NICInfo
*nd
, const char *default_model
,
792 const char *default_devaddr
)
794 const char *devaddr
= nd
->devaddr
? nd
->devaddr
: default_devaddr
;
799 qemu_check_nic_model_list(nd
, pci_nic_models
, default_model
);
801 for (i
= 0; pci_nic_models
[i
]; i
++) {
802 if (strcmp(nd
->model
, pci_nic_models
[i
]) == 0) {
803 pci_dev
= pci_create(pci_nic_names
[i
], devaddr
);
804 dev
= &pci_dev
->qdev
;
806 dev
->id
= qemu_strdup(nd
->id
);
822 static void pci_bridge_write_config(PCIDevice
*d
,
823 uint32_t address
, uint32_t val
, int len
)
825 PCIBridge
*s
= (PCIBridge
*)d
;
827 pci_default_write_config(d
, address
, val
, len
);
828 s
->bus
->bus_num
= d
->config
[PCI_SECONDARY_BUS
];
831 PCIBus
*pci_find_bus(int bus_num
)
833 PCIBus
*bus
= first_bus
;
835 while (bus
&& bus
->bus_num
!= bus_num
)
841 PCIDevice
*pci_find_device(int bus_num
, int slot
, int function
)
843 PCIBus
*bus
= pci_find_bus(bus_num
);
848 return bus
->devices
[PCI_DEVFN(slot
, function
)];
851 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, uint16_t vid
, uint16_t did
,
852 pci_map_irq_fn map_irq
, const char *name
)
855 s
= (PCIBridge
*)pci_register_device(bus
, name
, sizeof(PCIBridge
),
856 devfn
, NULL
, pci_bridge_write_config
);
858 pci_config_set_vendor_id(s
->dev
.config
, vid
);
859 pci_config_set_device_id(s
->dev
.config
, did
);
861 s
->dev
.config
[0x04] = 0x06; // command = bus master, pci mem
862 s
->dev
.config
[0x05] = 0x00;
863 s
->dev
.config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
864 s
->dev
.config
[0x07] = 0x00; // status = fast devsel
865 s
->dev
.config
[0x08] = 0x00; // revision
866 s
->dev
.config
[0x09] = 0x00; // programming i/f
867 pci_config_set_class(s
->dev
.config
, PCI_CLASS_BRIDGE_PCI
);
868 s
->dev
.config
[0x0D] = 0x10; // latency_timer
869 s
->dev
.config
[PCI_HEADER_TYPE
] =
870 PCI_HEADER_TYPE_MULTI_FUNCTION
| PCI_HEADER_TYPE_BRIDGE
; // header_type
871 s
->dev
.config
[0x1E] = 0xa0; // secondary status
873 s
->bus
= pci_register_secondary_bus(&s
->dev
, map_irq
, name
);
877 static void pci_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
879 PCIDevice
*pci_dev
= (PCIDevice
*)qdev
;
880 PCIDeviceInfo
*info
= container_of(base
, PCIDeviceInfo
, qdev
);
884 bus
= FROM_QBUS(PCIBus
, qdev_get_parent_bus(qdev
));
885 devfn
= pci_dev
->devfn
;
886 pci_dev
= do_pci_register_device(pci_dev
, bus
, base
->name
, devfn
,
887 info
->config_read
, info
->config_write
);
892 void pci_qdev_register(PCIDeviceInfo
*info
)
894 info
->qdev
.init
= pci_qdev_init
;
895 info
->qdev
.bus_info
= &pci_bus_info
;
896 qdev_register(&info
->qdev
);
899 void pci_qdev_register_many(PCIDeviceInfo
*info
)
901 while (info
->qdev
.name
) {
902 pci_qdev_register(info
);
907 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
911 dev
= qdev_create(&bus
->qbus
, name
);
912 qdev_prop_set_uint32(dev
, "addr", devfn
);
915 return (PCIDevice
*)dev
;
918 static int pci_find_space(PCIDevice
*pdev
, uint8_t size
)
920 int offset
= PCI_CONFIG_HEADER_SIZE
;
922 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
925 else if (i
- offset
+ 1 == size
)
930 static uint8_t pci_find_capability_list(PCIDevice
*pdev
, uint8_t cap_id
,
935 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
))
938 for (prev
= PCI_CAPABILITY_LIST
; (next
= pdev
->config
[prev
]);
939 prev
= next
+ PCI_CAP_LIST_NEXT
)
940 if (pdev
->config
[next
+ PCI_CAP_LIST_ID
] == cap_id
)
948 /* Reserve space and add capability to the linked list in pci config space */
949 int pci_add_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
951 uint8_t offset
= pci_find_space(pdev
, size
);
952 uint8_t *config
= pdev
->config
+ offset
;
955 config
[PCI_CAP_LIST_ID
] = cap_id
;
956 config
[PCI_CAP_LIST_NEXT
] = pdev
->config
[PCI_CAPABILITY_LIST
];
957 pdev
->config
[PCI_CAPABILITY_LIST
] = offset
;
958 pdev
->config
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
959 memset(pdev
->used
+ offset
, 0xFF, size
);
960 /* Make capability read-only by default */
961 memset(pdev
->wmask
+ offset
, 0, size
);
962 /* Check capability by default */
963 memset(pdev
->cmask
+ offset
, 0xFF, size
);
967 /* Unlink capability from the pci config space. */
968 void pci_del_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
970 uint8_t prev
, offset
= pci_find_capability_list(pdev
, cap_id
, &prev
);
973 pdev
->config
[prev
] = pdev
->config
[offset
+ PCI_CAP_LIST_NEXT
];
974 /* Make capability writeable again */
975 memset(pdev
->wmask
+ offset
, 0xff, size
);
976 /* Clear cmask as device-specific registers can't be checked */
977 memset(pdev
->cmask
+ offset
, 0, size
);
978 memset(pdev
->used
+ offset
, 0, size
);
980 if (!pdev
->config
[PCI_CAPABILITY_LIST
])
981 pdev
->config
[PCI_STATUS
] &= ~PCI_STATUS_CAP_LIST
;
984 /* Reserve space for capability at a known offset (to call after load). */
985 void pci_reserve_capability(PCIDevice
*pdev
, uint8_t offset
, uint8_t size
)
987 memset(pdev
->used
+ offset
, 0xff, size
);
990 uint8_t pci_find_capability(PCIDevice
*pdev
, uint8_t cap_id
)
992 return pci_find_capability_list(pdev
, cap_id
, NULL
);
995 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
997 PCIDevice
*d
= (PCIDevice
*)dev
;
998 const pci_class_desc
*desc
;
1003 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
1004 desc
= pci_class_descriptions
;
1005 while (desc
->desc
&& class != desc
->class)
1008 snprintf(ctxt
, sizeof(ctxt
), "%s", desc
->desc
);
1010 snprintf(ctxt
, sizeof(ctxt
), "Class %04x", class);
1013 monitor_printf(mon
, "%*sclass %s, addr %02x:%02x.%x, "
1014 "pci id %04x:%04x (sub %04x:%04x)\n",
1016 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7,
1017 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
1018 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))),
1019 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_SUBSYSTEM_VENDOR_ID
))),
1020 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_SUBSYSTEM_ID
))));
1021 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1022 r
= &d
->io_regions
[i
];
1025 monitor_printf(mon
, "%*sbar %d: %s at 0x%x [0x%x]\n", indent
, "",
1026 i
, r
->type
& PCI_ADDRESS_SPACE_IO
? "i/o" : "mem",
1027 r
->addr
, r
->addr
+ r
->size
- 1);