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 PCIDevice
*devices
[256];
46 PCIDevice
*parent_dev
;
48 /* The bus IRQ state is the logical OR of the connected devices.
49 Keep a count of the number of devices with raised IRQs. */
54 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
56 static struct BusInfo pci_bus_info
= {
58 .size
= sizeof(PCIBus
),
59 .print_dev
= pcibus_dev_print
,
60 .props
= (Property
[]) {
61 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice
, devfn
, -1),
62 DEFINE_PROP_END_OF_LIST()
66 static void pci_update_mappings(PCIDevice
*d
);
67 static void pci_set_irq(void *opaque
, int irq_num
, int level
);
69 target_phys_addr_t pci_mem_base
;
70 static uint16_t pci_default_sub_vendor_id
= PCI_SUBVENDOR_ID_REDHAT_QUMRANET
;
71 static uint16_t pci_default_sub_device_id
= PCI_SUBDEVICE_ID_QEMU
;
72 static PCIBus
*first_bus
;
74 static const VMStateDescription vmstate_pcibus
= {
77 .minimum_version_id
= 1,
78 .minimum_version_id_old
= 1,
79 .fields
= (VMStateField
[]) {
80 VMSTATE_INT32_EQUAL(nirq
, PCIBus
),
81 VMSTATE_INT32_VARRAY(irq_count
, PCIBus
, nirq
),
86 static void pci_bus_reset(void *opaque
)
91 for (i
= 0; i
< bus
->nirq
; i
++) {
92 bus
->irq_count
[i
] = 0;
94 for (i
= 0; i
< 256; i
++) {
96 memset(bus
->devices
[i
]->irq_state
, 0,
97 sizeof(bus
->devices
[i
]->irq_state
));
101 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
102 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
103 void *irq_opaque
, int devfn_min
, int nirq
)
108 bus
= FROM_QBUS(PCIBus
, qbus_create(&pci_bus_info
, parent
, name
));
109 bus
->set_irq
= set_irq
;
110 bus
->map_irq
= map_irq
;
111 bus
->irq_opaque
= irq_opaque
;
112 bus
->devfn_min
= devfn_min
;
114 bus
->irq_count
= qemu_mallocz(nirq
* sizeof(bus
->irq_count
[0]));
115 bus
->next
= first_bus
;
117 vmstate_register(nbus
++, &vmstate_pcibus
, bus
);
118 qemu_register_reset(pci_bus_reset
, bus
);
122 static PCIBus
*pci_register_secondary_bus(PCIDevice
*dev
,
123 pci_map_irq_fn map_irq
,
128 bus
= FROM_QBUS(PCIBus
, qbus_create(&pci_bus_info
, &dev
->qdev
, name
));
129 bus
->map_irq
= map_irq
;
130 bus
->parent_dev
= dev
;
131 bus
->next
= dev
->bus
->next
;
132 dev
->bus
->next
= bus
;
136 int pci_bus_num(PCIBus
*s
)
141 static int get_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
143 PCIDevice
*s
= container_of(pv
, PCIDevice
, config
);
144 uint8_t config
[size
];
147 qemu_get_buffer(f
, config
, size
);
148 for (i
= 0; i
< size
; ++i
)
149 if ((config
[i
] ^ s
->config
[i
]) & s
->cmask
[i
] & ~s
->wmask
[i
])
151 memcpy(s
->config
, config
, size
);
153 pci_update_mappings(s
);
158 /* just put buffer */
159 static void put_pci_config_device(QEMUFile
*f
, const void *pv
, size_t size
)
161 const uint8_t *v
= pv
;
162 qemu_put_buffer(f
, v
, size
);
165 static VMStateInfo vmstate_info_pci_config
= {
166 .name
= "pci config",
167 .get
= get_pci_config_device
,
168 .put
= put_pci_config_device
,
171 const VMStateDescription vmstate_pci_device
= {
174 .minimum_version_id
= 1,
175 .minimum_version_id_old
= 1,
176 .fields
= (VMStateField
[]) {
177 VMSTATE_INT32_LE(version_id
, PCIDevice
),
178 VMSTATE_SINGLE(config
, PCIDevice
, 0, vmstate_info_pci_config
,
179 typeof_field(PCIDevice
,config
)),
180 VMSTATE_INT32_ARRAY_V(irq_state
, PCIDevice
, 4, 2),
181 VMSTATE_END_OF_LIST()
185 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
187 vmstate_save_state(f
, &vmstate_pci_device
, s
);
190 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
192 return vmstate_load_state(f
, &vmstate_pci_device
, s
, s
->version_id
);
195 static int pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
199 id
= (void*)(&pci_dev
->config
[PCI_SUBVENDOR_ID
]);
200 id
[0] = cpu_to_le16(pci_default_sub_vendor_id
);
201 id
[1] = cpu_to_le16(pci_default_sub_device_id
);
206 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
208 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
213 unsigned long dom
= 0, bus
= 0;
217 val
= strtoul(p
, &e
, 16);
223 val
= strtoul(p
, &e
, 16);
230 val
= strtoul(p
, &e
, 16);
236 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
244 /* Note: QEMU doesn't implement domains other than 0 */
245 if (dom
!= 0 || pci_find_bus(bus
) == NULL
)
254 int pci_read_devaddr(Monitor
*mon
, const char *addr
, int *domp
, int *busp
,
257 /* strip legacy tag */
258 if (!strncmp(addr
, "pci_addr=", 9)) {
261 if (pci_parse_devaddr(addr
, domp
, busp
, slotp
)) {
262 monitor_printf(mon
, "Invalid pci address\n");
268 static PCIBus
*pci_get_bus_devfn(int *devfnp
, const char *devaddr
)
275 return pci_find_bus(0);
278 if (pci_parse_devaddr(devaddr
, &dom
, &bus
, &slot
) < 0) {
283 return pci_find_bus(bus
);
286 static void pci_init_cmask(PCIDevice
*dev
)
288 pci_set_word(dev
->cmask
+ PCI_VENDOR_ID
, 0xffff);
289 pci_set_word(dev
->cmask
+ PCI_DEVICE_ID
, 0xffff);
290 dev
->cmask
[PCI_STATUS
] = PCI_STATUS_CAP_LIST
;
291 dev
->cmask
[PCI_REVISION_ID
] = 0xff;
292 dev
->cmask
[PCI_CLASS_PROG
] = 0xff;
293 pci_set_word(dev
->cmask
+ PCI_CLASS_DEVICE
, 0xffff);
294 dev
->cmask
[PCI_HEADER_TYPE
] = 0xff;
295 dev
->cmask
[PCI_CAPABILITY_LIST
] = 0xff;
298 static void pci_init_wmask(PCIDevice
*dev
)
301 dev
->wmask
[PCI_CACHE_LINE_SIZE
] = 0xff;
302 dev
->wmask
[PCI_INTERRUPT_LINE
] = 0xff;
303 dev
->wmask
[PCI_COMMAND
] = PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
304 | PCI_COMMAND_MASTER
;
305 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
306 dev
->wmask
[i
] = 0xff;
309 /* -1 for devfn means auto assign */
310 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
311 const char *name
, int devfn
,
312 PCIConfigReadFunc
*config_read
,
313 PCIConfigWriteFunc
*config_write
)
316 for(devfn
= bus
->devfn_min
; devfn
< 256; devfn
+= 8) {
317 if (!bus
->devices
[devfn
])
322 } else if (bus
->devices
[devfn
]) {
326 pci_dev
->devfn
= devfn
;
327 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
328 memset(pci_dev
->irq_state
, 0, sizeof(pci_dev
->irq_state
));
329 pci_set_default_subsystem_id(pci_dev
);
330 pci_init_cmask(pci_dev
);
331 pci_init_wmask(pci_dev
);
334 config_read
= pci_default_read_config
;
336 config_write
= pci_default_write_config
;
337 pci_dev
->config_read
= config_read
;
338 pci_dev
->config_write
= config_write
;
339 bus
->devices
[devfn
] = pci_dev
;
340 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, 4);
341 pci_dev
->version_id
= 2; /* Current pci device vmstate version */
345 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
346 int instance_size
, int devfn
,
347 PCIConfigReadFunc
*config_read
,
348 PCIConfigWriteFunc
*config_write
)
352 pci_dev
= qemu_mallocz(instance_size
);
353 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
354 config_read
, config_write
);
357 static target_phys_addr_t
pci_to_cpu_addr(target_phys_addr_t addr
)
359 return addr
+ pci_mem_base
;
362 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
367 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
368 r
= &pci_dev
->io_regions
[i
];
369 if (!r
->size
|| r
->addr
== -1)
371 if (r
->type
== PCI_ADDRESS_SPACE_IO
) {
372 isa_unassign_ioport(r
->addr
, r
->size
);
374 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
381 int pci_unregister_device(PCIDevice
*pci_dev
)
385 if (pci_dev
->unregister
)
386 ret
= pci_dev
->unregister(pci_dev
);
390 pci_unregister_io_regions(pci_dev
);
392 qemu_free_irqs(pci_dev
->irq
);
393 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
394 qdev_free(&pci_dev
->qdev
);
398 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
399 uint32_t size
, int type
,
400 PCIMapIORegionFunc
*map_func
)
406 if ((unsigned int)region_num
>= PCI_NUM_REGIONS
)
409 if (size
& (size
-1)) {
410 fprintf(stderr
, "ERROR: PCI region size must be pow2 "
411 "type=0x%x, size=0x%x\n", type
, size
);
415 r
= &pci_dev
->io_regions
[region_num
];
419 r
->map_func
= map_func
;
422 if (region_num
== PCI_ROM_SLOT
) {
424 /* ROM enable bit is writeable */
427 addr
= 0x10 + region_num
* 4;
429 *(uint32_t *)(pci_dev
->config
+ addr
) = cpu_to_le32(type
);
430 *(uint32_t *)(pci_dev
->wmask
+ addr
) = cpu_to_le32(wmask
);
431 *(uint32_t *)(pci_dev
->cmask
+ addr
) = 0xffffffff;
434 static void pci_update_mappings(PCIDevice
*d
)
438 uint32_t last_addr
, new_addr
, config_ofs
;
440 cmd
= le16_to_cpu(*(uint16_t *)(d
->config
+ PCI_COMMAND
));
441 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
442 r
= &d
->io_regions
[i
];
443 if (i
== PCI_ROM_SLOT
) {
446 config_ofs
= 0x10 + i
* 4;
449 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
450 if (cmd
& PCI_COMMAND_IO
) {
451 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
453 new_addr
= new_addr
& ~(r
->size
- 1);
454 last_addr
= new_addr
+ r
->size
- 1;
455 /* NOTE: we have only 64K ioports on PC */
456 if (last_addr
<= new_addr
|| new_addr
== 0 ||
457 last_addr
>= 0x10000) {
464 if (cmd
& PCI_COMMAND_MEMORY
) {
465 new_addr
= le32_to_cpu(*(uint32_t *)(d
->config
+
467 /* the ROM slot has a specific enable bit */
468 if (i
== PCI_ROM_SLOT
&& !(new_addr
& 1))
470 new_addr
= new_addr
& ~(r
->size
- 1);
471 last_addr
= new_addr
+ r
->size
- 1;
472 /* NOTE: we do not support wrapping */
473 /* XXX: as we cannot support really dynamic
474 mappings, we handle specific values as invalid
476 if (last_addr
<= new_addr
|| new_addr
== 0 ||
485 /* now do the real mapping */
486 if (new_addr
!= r
->addr
) {
488 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
490 /* NOTE: specific hack for IDE in PC case:
491 only one byte must be mapped. */
492 class = d
->config
[0x0a] | (d
->config
[0x0b] << 8);
493 if (class == 0x0101 && r
->size
== 4) {
494 isa_unassign_ioport(r
->addr
+ 2, 1);
496 isa_unassign_ioport(r
->addr
, r
->size
);
499 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
502 qemu_unregister_coalesced_mmio(r
->addr
, r
->size
);
507 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
514 uint32_t pci_default_read_config(PCIDevice
*d
,
515 uint32_t address
, int len
)
522 if (address
<= 0xfc) {
523 val
= le32_to_cpu(*(uint32_t *)(d
->config
+ address
));
528 if (address
<= 0xfe) {
529 val
= le16_to_cpu(*(uint16_t *)(d
->config
+ address
));
534 val
= d
->config
[address
];
540 void pci_default_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int l
)
542 uint8_t orig
[PCI_CONFIG_SPACE_SIZE
];
545 /* not efficient, but simple */
546 memcpy(orig
, d
->config
, PCI_CONFIG_SPACE_SIZE
);
547 for(i
= 0; i
< l
&& addr
< PCI_CONFIG_SPACE_SIZE
; val
>>= 8, ++i
, ++addr
) {
548 uint8_t wmask
= d
->wmask
[addr
];
549 d
->config
[addr
] = (d
->config
[addr
] & ~wmask
) | (val
& wmask
);
551 if (memcmp(orig
+ PCI_BASE_ADDRESS_0
, d
->config
+ PCI_BASE_ADDRESS_0
, 24)
552 || ((orig
[PCI_COMMAND
] ^ d
->config
[PCI_COMMAND
])
553 & (PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
)))
554 pci_update_mappings(d
);
557 void pci_data_write(void *opaque
, uint32_t addr
, uint32_t val
, int len
)
561 int config_addr
, bus_num
;
564 PCI_DPRINTF("pci_data_write: addr=%08x val=%08x len=%d\n",
567 bus_num
= (addr
>> 16) & 0xff;
568 while (s
&& s
->bus_num
!= bus_num
)
572 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
575 config_addr
= addr
& 0xff;
576 PCI_DPRINTF("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
577 pci_dev
->name
, config_addr
, val
, len
);
578 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
581 uint32_t pci_data_read(void *opaque
, uint32_t addr
, int len
)
585 int config_addr
, bus_num
;
588 bus_num
= (addr
>> 16) & 0xff;
589 while (s
&& s
->bus_num
!= bus_num
)
593 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
610 config_addr
= addr
& 0xff;
611 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
612 PCI_DPRINTF("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
613 pci_dev
->name
, config_addr
, val
, len
);
616 PCI_DPRINTF("pci_data_read: addr=%08x val=%08x len=%d\n",
622 /***********************************************************/
623 /* generic PCI irq support */
625 /* 0 <= irq_num <= 3. level must be 0 or 1 */
626 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
628 PCIDevice
*pci_dev
= opaque
;
632 change
= level
- pci_dev
->irq_state
[irq_num
];
636 pci_dev
->irq_state
[irq_num
] = level
;
639 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
642 pci_dev
= bus
->parent_dev
;
644 bus
->irq_count
[irq_num
] += change
;
645 bus
->set_irq(bus
->irq_opaque
, irq_num
, bus
->irq_count
[irq_num
] != 0);
648 /***********************************************************/
649 /* monitor info on PCI */
656 static const pci_class_desc pci_class_descriptions
[] =
658 { 0x0100, "SCSI controller"},
659 { 0x0101, "IDE controller"},
660 { 0x0102, "Floppy controller"},
661 { 0x0103, "IPI controller"},
662 { 0x0104, "RAID controller"},
663 { 0x0106, "SATA controller"},
664 { 0x0107, "SAS controller"},
665 { 0x0180, "Storage controller"},
666 { 0x0200, "Ethernet controller"},
667 { 0x0201, "Token Ring controller"},
668 { 0x0202, "FDDI controller"},
669 { 0x0203, "ATM controller"},
670 { 0x0280, "Network controller"},
671 { 0x0300, "VGA controller"},
672 { 0x0301, "XGA controller"},
673 { 0x0302, "3D controller"},
674 { 0x0380, "Display controller"},
675 { 0x0400, "Video controller"},
676 { 0x0401, "Audio controller"},
678 { 0x0480, "Multimedia controller"},
679 { 0x0500, "RAM controller"},
680 { 0x0501, "Flash controller"},
681 { 0x0580, "Memory controller"},
682 { 0x0600, "Host bridge"},
683 { 0x0601, "ISA bridge"},
684 { 0x0602, "EISA bridge"},
685 { 0x0603, "MC bridge"},
686 { 0x0604, "PCI bridge"},
687 { 0x0605, "PCMCIA bridge"},
688 { 0x0606, "NUBUS bridge"},
689 { 0x0607, "CARDBUS bridge"},
690 { 0x0608, "RACEWAY bridge"},
692 { 0x0c03, "USB controller"},
696 static void pci_info_device(PCIDevice
*d
)
698 Monitor
*mon
= cur_mon
;
701 const pci_class_desc
*desc
;
703 monitor_printf(mon
, " Bus %2d, device %3d, function %d:\n",
704 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7);
705 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
706 monitor_printf(mon
, " ");
707 desc
= pci_class_descriptions
;
708 while (desc
->desc
&& class != desc
->class)
711 monitor_printf(mon
, "%s", desc
->desc
);
713 monitor_printf(mon
, "Class %04x", class);
715 monitor_printf(mon
, ": PCI device %04x:%04x\n",
716 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
717 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))));
719 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
720 monitor_printf(mon
, " IRQ %d.\n",
721 d
->config
[PCI_INTERRUPT_LINE
]);
723 if (class == 0x0604) {
724 monitor_printf(mon
, " BUS %d.\n", d
->config
[0x19]);
726 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
727 r
= &d
->io_regions
[i
];
729 monitor_printf(mon
, " BAR%d: ", i
);
730 if (r
->type
& PCI_ADDRESS_SPACE_IO
) {
731 monitor_printf(mon
, "I/O at 0x%04x [0x%04x].\n",
732 r
->addr
, r
->addr
+ r
->size
- 1);
734 monitor_printf(mon
, "32 bit memory at 0x%08x [0x%08x].\n",
735 r
->addr
, r
->addr
+ r
->size
- 1);
739 monitor_printf(mon
, " id \"%s\"\n", d
->qdev
.id
? d
->qdev
.id
: "");
740 if (class == 0x0604 && d
->config
[0x19] != 0) {
741 pci_for_each_device(d
->config
[0x19], pci_info_device
);
745 void pci_for_each_device(int bus_num
, void (*fn
)(PCIDevice
*d
))
747 PCIBus
*bus
= first_bus
;
751 while (bus
&& bus
->bus_num
!= bus_num
)
754 for(devfn
= 0; devfn
< 256; devfn
++) {
755 d
= bus
->devices
[devfn
];
762 void pci_info(Monitor
*mon
)
764 pci_for_each_device(0, pci_info_device
);
767 PCIDevice
*pci_create(const char *name
, const char *devaddr
)
773 bus
= pci_get_bus_devfn(&devfn
, devaddr
);
775 fprintf(stderr
, "Invalid PCI device address %s for device %s\n",
780 dev
= qdev_create(&bus
->qbus
, name
);
781 qdev_prop_set_uint32(dev
, "addr", devfn
);
782 return (PCIDevice
*)dev
;
785 static const char * const pci_nic_models
[] = {
797 static const char * const pci_nic_names
[] = {
809 /* Initialize a PCI NIC. */
810 PCIDevice
*pci_nic_init(NICInfo
*nd
, const char *default_model
,
811 const char *default_devaddr
)
813 const char *devaddr
= nd
->devaddr
? nd
->devaddr
: default_devaddr
;
818 qemu_check_nic_model_list(nd
, pci_nic_models
, default_model
);
820 for (i
= 0; pci_nic_models
[i
]; i
++) {
821 if (strcmp(nd
->model
, pci_nic_models
[i
]) == 0) {
822 pci_dev
= pci_create(pci_nic_names
[i
], devaddr
);
823 dev
= &pci_dev
->qdev
;
825 dev
->id
= qemu_strdup(nd
->id
);
841 static void pci_bridge_write_config(PCIDevice
*d
,
842 uint32_t address
, uint32_t val
, int len
)
844 PCIBridge
*s
= (PCIBridge
*)d
;
846 pci_default_write_config(d
, address
, val
, len
);
847 s
->bus
->bus_num
= d
->config
[PCI_SECONDARY_BUS
];
850 PCIBus
*pci_find_bus(int bus_num
)
852 PCIBus
*bus
= first_bus
;
854 while (bus
&& bus
->bus_num
!= bus_num
)
860 PCIDevice
*pci_find_device(int bus_num
, int slot
, int function
)
862 PCIBus
*bus
= pci_find_bus(bus_num
);
867 return bus
->devices
[PCI_DEVFN(slot
, function
)];
870 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, uint16_t vid
, uint16_t did
,
871 pci_map_irq_fn map_irq
, const char *name
)
874 s
= (PCIBridge
*)pci_register_device(bus
, name
, sizeof(PCIBridge
),
875 devfn
, NULL
, pci_bridge_write_config
);
877 pci_config_set_vendor_id(s
->dev
.config
, vid
);
878 pci_config_set_device_id(s
->dev
.config
, did
);
880 s
->dev
.config
[0x04] = 0x06; // command = bus master, pci mem
881 s
->dev
.config
[0x05] = 0x00;
882 s
->dev
.config
[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
883 s
->dev
.config
[0x07] = 0x00; // status = fast devsel
884 s
->dev
.config
[0x08] = 0x00; // revision
885 s
->dev
.config
[0x09] = 0x00; // programming i/f
886 pci_config_set_class(s
->dev
.config
, PCI_CLASS_BRIDGE_PCI
);
887 s
->dev
.config
[0x0D] = 0x10; // latency_timer
888 s
->dev
.config
[PCI_HEADER_TYPE
] =
889 PCI_HEADER_TYPE_MULTI_FUNCTION
| PCI_HEADER_TYPE_BRIDGE
; // header_type
890 s
->dev
.config
[0x1E] = 0xa0; // secondary status
892 s
->bus
= pci_register_secondary_bus(&s
->dev
, map_irq
, name
);
896 static int pci_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
898 PCIDevice
*pci_dev
= (PCIDevice
*)qdev
;
899 PCIDeviceInfo
*info
= container_of(base
, PCIDeviceInfo
, qdev
);
903 bus
= FROM_QBUS(PCIBus
, qdev_get_parent_bus(qdev
));
904 devfn
= pci_dev
->devfn
;
905 pci_dev
= do_pci_register_device(pci_dev
, bus
, base
->name
, devfn
,
906 info
->config_read
, info
->config_write
);
908 return info
->init(pci_dev
);
911 void pci_qdev_register(PCIDeviceInfo
*info
)
913 info
->qdev
.init
= pci_qdev_init
;
914 info
->qdev
.bus_info
= &pci_bus_info
;
915 qdev_register(&info
->qdev
);
918 void pci_qdev_register_many(PCIDeviceInfo
*info
)
920 while (info
->qdev
.name
) {
921 pci_qdev_register(info
);
926 PCIDevice
*pci_create_noinit(PCIBus
*bus
, int devfn
, const char *name
)
930 dev
= qdev_create(&bus
->qbus
, name
);
931 qdev_prop_set_uint32(dev
, "addr", devfn
);
932 return DO_UPCAST(PCIDevice
, qdev
, dev
);
935 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
937 PCIDevice
*dev
= pci_create_noinit(bus
, devfn
, name
);
938 qdev_init(&dev
->qdev
);
942 static int pci_find_space(PCIDevice
*pdev
, uint8_t size
)
944 int offset
= PCI_CONFIG_HEADER_SIZE
;
946 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
949 else if (i
- offset
+ 1 == size
)
954 static uint8_t pci_find_capability_list(PCIDevice
*pdev
, uint8_t cap_id
,
959 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
))
962 for (prev
= PCI_CAPABILITY_LIST
; (next
= pdev
->config
[prev
]);
963 prev
= next
+ PCI_CAP_LIST_NEXT
)
964 if (pdev
->config
[next
+ PCI_CAP_LIST_ID
] == cap_id
)
972 /* Reserve space and add capability to the linked list in pci config space */
973 int pci_add_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
975 uint8_t offset
= pci_find_space(pdev
, size
);
976 uint8_t *config
= pdev
->config
+ offset
;
979 config
[PCI_CAP_LIST_ID
] = cap_id
;
980 config
[PCI_CAP_LIST_NEXT
] = pdev
->config
[PCI_CAPABILITY_LIST
];
981 pdev
->config
[PCI_CAPABILITY_LIST
] = offset
;
982 pdev
->config
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
983 memset(pdev
->used
+ offset
, 0xFF, size
);
984 /* Make capability read-only by default */
985 memset(pdev
->wmask
+ offset
, 0, size
);
986 /* Check capability by default */
987 memset(pdev
->cmask
+ offset
, 0xFF, size
);
991 /* Unlink capability from the pci config space. */
992 void pci_del_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
994 uint8_t prev
, offset
= pci_find_capability_list(pdev
, cap_id
, &prev
);
997 pdev
->config
[prev
] = pdev
->config
[offset
+ PCI_CAP_LIST_NEXT
];
998 /* Make capability writeable again */
999 memset(pdev
->wmask
+ offset
, 0xff, size
);
1000 /* Clear cmask as device-specific registers can't be checked */
1001 memset(pdev
->cmask
+ offset
, 0, size
);
1002 memset(pdev
->used
+ offset
, 0, size
);
1004 if (!pdev
->config
[PCI_CAPABILITY_LIST
])
1005 pdev
->config
[PCI_STATUS
] &= ~PCI_STATUS_CAP_LIST
;
1008 /* Reserve space for capability at a known offset (to call after load). */
1009 void pci_reserve_capability(PCIDevice
*pdev
, uint8_t offset
, uint8_t size
)
1011 memset(pdev
->used
+ offset
, 0xff, size
);
1014 uint8_t pci_find_capability(PCIDevice
*pdev
, uint8_t cap_id
)
1016 return pci_find_capability_list(pdev
, cap_id
, NULL
);
1019 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
1021 PCIDevice
*d
= (PCIDevice
*)dev
;
1022 const pci_class_desc
*desc
;
1027 class = le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_CLASS_DEVICE
)));
1028 desc
= pci_class_descriptions
;
1029 while (desc
->desc
&& class != desc
->class)
1032 snprintf(ctxt
, sizeof(ctxt
), "%s", desc
->desc
);
1034 snprintf(ctxt
, sizeof(ctxt
), "Class %04x", class);
1037 monitor_printf(mon
, "%*sclass %s, addr %02x:%02x.%x, "
1038 "pci id %04x:%04x (sub %04x:%04x)\n",
1040 d
->bus
->bus_num
, d
->devfn
>> 3, d
->devfn
& 7,
1041 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_VENDOR_ID
))),
1042 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_DEVICE_ID
))),
1043 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_SUBSYSTEM_VENDOR_ID
))),
1044 le16_to_cpu(*((uint16_t *)(d
->config
+ PCI_SUBSYSTEM_ID
))));
1045 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1046 r
= &d
->io_regions
[i
];
1049 monitor_printf(mon
, "%*sbar %d: %s at 0x%x [0x%x]\n", indent
, "",
1050 i
, r
->type
& PCI_ADDRESS_SPACE_IO
? "i/o" : "mem",
1051 r
->addr
, r
->addr
+ r
->size
- 1);