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 pci_hotplug_fn hotplug
;
44 uint32_t config_reg
; /* XXX: suppress */
46 PCIDevice
*devices
[256];
47 PCIDevice
*parent_dev
;
49 /* The bus IRQ state is the logical OR of the connected devices.
50 Keep a count of the number of devices with raised IRQs. */
55 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
);
57 static struct BusInfo pci_bus_info
= {
59 .size
= sizeof(PCIBus
),
60 .print_dev
= pcibus_dev_print
,
61 .props
= (Property
[]) {
62 DEFINE_PROP_PCI_DEVFN("addr", PCIDevice
, devfn
, -1),
63 DEFINE_PROP_END_OF_LIST()
67 static void pci_update_mappings(PCIDevice
*d
);
68 static void pci_set_irq(void *opaque
, int irq_num
, int level
);
70 target_phys_addr_t pci_mem_base
;
71 static uint16_t pci_default_sub_vendor_id
= PCI_SUBVENDOR_ID_REDHAT_QUMRANET
;
72 static uint16_t pci_default_sub_device_id
= PCI_SUBDEVICE_ID_QEMU
;
73 static PCIBus
*first_bus
;
75 static const VMStateDescription vmstate_pcibus
= {
78 .minimum_version_id
= 1,
79 .minimum_version_id_old
= 1,
80 .fields
= (VMStateField
[]) {
81 VMSTATE_INT32_EQUAL(nirq
, PCIBus
),
82 VMSTATE_VARRAY_INT32(irq_count
, PCIBus
, nirq
, 0, vmstate_info_int32
, int32_t),
87 static int pci_bar(PCIDevice
*d
, int reg
)
91 if (reg
!= PCI_ROM_SLOT
)
92 return PCI_BASE_ADDRESS_0
+ reg
* 4;
94 type
= d
->config
[PCI_HEADER_TYPE
] & ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
95 return type
== PCI_HEADER_TYPE_BRIDGE
? PCI_ROM_ADDRESS1
: PCI_ROM_ADDRESS
;
98 static void pci_device_reset(PCIDevice
*dev
)
102 memset(dev
->irq_state
, 0, sizeof dev
->irq_state
);
103 dev
->config
[PCI_COMMAND
] &= ~(PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
|
105 dev
->config
[PCI_CACHE_LINE_SIZE
] = 0x0;
106 dev
->config
[PCI_INTERRUPT_LINE
] = 0x0;
107 for (r
= 0; r
< PCI_NUM_REGIONS
; ++r
) {
108 if (!dev
->io_regions
[r
].size
) {
111 pci_set_long(dev
->config
+ pci_bar(dev
, r
), dev
->io_regions
[r
].type
);
113 pci_update_mappings(dev
);
116 static void pci_bus_reset(void *opaque
)
118 PCIBus
*bus
= opaque
;
121 for (i
= 0; i
< bus
->nirq
; i
++) {
122 bus
->irq_count
[i
] = 0;
124 for (i
= 0; i
< ARRAY_SIZE(bus
->devices
); ++i
) {
125 if (bus
->devices
[i
]) {
126 pci_device_reset(bus
->devices
[i
]);
131 void pci_bus_new_inplace(PCIBus
*bus
, DeviceState
*parent
,
132 const char *name
, int devfn_min
)
136 qbus_create_inplace(&bus
->qbus
, &pci_bus_info
, parent
, name
);
137 bus
->devfn_min
= devfn_min
;
138 bus
->next
= first_bus
;
140 vmstate_register(nbus
++, &vmstate_pcibus
, bus
);
141 qemu_register_reset(pci_bus_reset
, bus
);
144 PCIBus
*pci_bus_new(DeviceState
*parent
, const char *name
, int devfn_min
)
148 bus
= qemu_mallocz(sizeof(*bus
));
149 bus
->qbus
.qdev_allocated
= 1;
150 pci_bus_new_inplace(bus
, parent
, name
, devfn_min
);
154 void pci_bus_irqs(PCIBus
*bus
, pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
155 void *irq_opaque
, int nirq
)
157 bus
->set_irq
= set_irq
;
158 bus
->map_irq
= map_irq
;
159 bus
->irq_opaque
= irq_opaque
;
161 bus
->irq_count
= qemu_mallocz(nirq
* sizeof(bus
->irq_count
[0]));
164 void pci_bus_hotplug(PCIBus
*bus
, pci_hotplug_fn hotplug
)
166 bus
->qbus
.allow_hotplug
= 1;
167 bus
->hotplug
= hotplug
;
170 PCIBus
*pci_register_bus(DeviceState
*parent
, const char *name
,
171 pci_set_irq_fn set_irq
, pci_map_irq_fn map_irq
,
172 void *irq_opaque
, int devfn_min
, int nirq
)
176 bus
= pci_bus_new(parent
, name
, devfn_min
);
177 pci_bus_irqs(bus
, set_irq
, map_irq
, irq_opaque
, nirq
);
181 static void pci_register_secondary_bus(PCIBus
*bus
,
183 pci_map_irq_fn map_irq
,
186 qbus_create_inplace(&bus
->qbus
, &pci_bus_info
, &dev
->qdev
, name
);
187 bus
->map_irq
= map_irq
;
188 bus
->parent_dev
= dev
;
189 bus
->next
= dev
->bus
->next
;
190 dev
->bus
->next
= bus
;
193 int pci_bus_num(PCIBus
*s
)
198 static int get_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
200 PCIDevice
*s
= container_of(pv
, PCIDevice
, config
);
201 uint8_t config
[PCI_CONFIG_SPACE_SIZE
];
204 assert(size
== sizeof config
);
205 qemu_get_buffer(f
, config
, sizeof config
);
206 for (i
= 0; i
< sizeof config
; ++i
)
207 if ((config
[i
] ^ s
->config
[i
]) & s
->cmask
[i
] & ~s
->wmask
[i
])
209 memcpy(s
->config
, config
, sizeof config
);
211 pci_update_mappings(s
);
216 /* just put buffer */
217 static void put_pci_config_device(QEMUFile
*f
, void *pv
, size_t size
)
219 const uint8_t *v
= pv
;
220 qemu_put_buffer(f
, v
, size
);
223 static VMStateInfo vmstate_info_pci_config
= {
224 .name
= "pci config",
225 .get
= get_pci_config_device
,
226 .put
= put_pci_config_device
,
229 const VMStateDescription vmstate_pci_device
= {
232 .minimum_version_id
= 1,
233 .minimum_version_id_old
= 1,
234 .fields
= (VMStateField
[]) {
235 VMSTATE_INT32_LE(version_id
, PCIDevice
),
236 VMSTATE_SINGLE(config
, PCIDevice
, 0, vmstate_info_pci_config
,
237 typeof_field(PCIDevice
,config
)),
238 VMSTATE_INT32_ARRAY_V(irq_state
, PCIDevice
, PCI_NUM_PINS
, 2),
239 VMSTATE_END_OF_LIST()
243 void pci_device_save(PCIDevice
*s
, QEMUFile
*f
)
245 vmstate_save_state(f
, &vmstate_pci_device
, s
);
248 int pci_device_load(PCIDevice
*s
, QEMUFile
*f
)
250 return vmstate_load_state(f
, &vmstate_pci_device
, s
, s
->version_id
);
253 static int pci_set_default_subsystem_id(PCIDevice
*pci_dev
)
257 id
= (void*)(&pci_dev
->config
[PCI_SUBVENDOR_ID
]);
258 id
[0] = cpu_to_le16(pci_default_sub_vendor_id
);
259 id
[1] = cpu_to_le16(pci_default_sub_device_id
);
264 * Parse [[<domain>:]<bus>:]<slot>, return -1 on error
266 static int pci_parse_devaddr(const char *addr
, int *domp
, int *busp
, unsigned *slotp
)
271 unsigned long dom
= 0, bus
= 0;
275 val
= strtoul(p
, &e
, 16);
281 val
= strtoul(p
, &e
, 16);
288 val
= strtoul(p
, &e
, 16);
294 if (dom
> 0xffff || bus
> 0xff || val
> 0x1f)
302 /* Note: QEMU doesn't implement domains other than 0 */
303 if (dom
!= 0 || pci_find_bus(bus
) == NULL
)
312 int pci_read_devaddr(Monitor
*mon
, const char *addr
, int *domp
, int *busp
,
315 /* strip legacy tag */
316 if (!strncmp(addr
, "pci_addr=", 9)) {
319 if (pci_parse_devaddr(addr
, domp
, busp
, slotp
)) {
320 monitor_printf(mon
, "Invalid pci address\n");
326 PCIBus
*pci_get_bus_devfn(int *devfnp
, const char *devaddr
)
333 return pci_find_bus(0);
336 if (pci_parse_devaddr(devaddr
, &dom
, &bus
, &slot
) < 0) {
341 return pci_find_bus(bus
);
344 static void pci_init_cmask(PCIDevice
*dev
)
346 pci_set_word(dev
->cmask
+ PCI_VENDOR_ID
, 0xffff);
347 pci_set_word(dev
->cmask
+ PCI_DEVICE_ID
, 0xffff);
348 dev
->cmask
[PCI_STATUS
] = PCI_STATUS_CAP_LIST
;
349 dev
->cmask
[PCI_REVISION_ID
] = 0xff;
350 dev
->cmask
[PCI_CLASS_PROG
] = 0xff;
351 pci_set_word(dev
->cmask
+ PCI_CLASS_DEVICE
, 0xffff);
352 dev
->cmask
[PCI_HEADER_TYPE
] = 0xff;
353 dev
->cmask
[PCI_CAPABILITY_LIST
] = 0xff;
356 static void pci_init_wmask(PCIDevice
*dev
)
359 dev
->wmask
[PCI_CACHE_LINE_SIZE
] = 0xff;
360 dev
->wmask
[PCI_INTERRUPT_LINE
] = 0xff;
361 pci_set_word(dev
->wmask
+ PCI_COMMAND
,
362 PCI_COMMAND_IO
| PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
363 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
364 dev
->wmask
[i
] = 0xff;
367 /* -1 for devfn means auto assign */
368 static PCIDevice
*do_pci_register_device(PCIDevice
*pci_dev
, PCIBus
*bus
,
369 const char *name
, int devfn
,
370 PCIConfigReadFunc
*config_read
,
371 PCIConfigWriteFunc
*config_write
)
374 for(devfn
= bus
->devfn_min
; devfn
< 256; devfn
+= 8) {
375 if (!bus
->devices
[devfn
])
380 } else if (bus
->devices
[devfn
]) {
384 pci_dev
->devfn
= devfn
;
385 pstrcpy(pci_dev
->name
, sizeof(pci_dev
->name
), name
);
386 memset(pci_dev
->irq_state
, 0, sizeof(pci_dev
->irq_state
));
387 pci_set_default_subsystem_id(pci_dev
);
388 pci_init_cmask(pci_dev
);
389 pci_init_wmask(pci_dev
);
392 config_read
= pci_default_read_config
;
394 config_write
= pci_default_write_config
;
395 pci_dev
->config_read
= config_read
;
396 pci_dev
->config_write
= config_write
;
397 bus
->devices
[devfn
] = pci_dev
;
398 pci_dev
->irq
= qemu_allocate_irqs(pci_set_irq
, pci_dev
, PCI_NUM_PINS
);
399 pci_dev
->version_id
= 2; /* Current pci device vmstate version */
403 PCIDevice
*pci_register_device(PCIBus
*bus
, const char *name
,
404 int instance_size
, int devfn
,
405 PCIConfigReadFunc
*config_read
,
406 PCIConfigWriteFunc
*config_write
)
410 pci_dev
= qemu_mallocz(instance_size
);
411 pci_dev
= do_pci_register_device(pci_dev
, bus
, name
, devfn
,
412 config_read
, config_write
);
415 static target_phys_addr_t
pci_to_cpu_addr(target_phys_addr_t addr
)
417 return addr
+ pci_mem_base
;
420 static void pci_unregister_io_regions(PCIDevice
*pci_dev
)
425 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
426 r
= &pci_dev
->io_regions
[i
];
427 if (!r
->size
|| r
->addr
== PCI_BAR_UNMAPPED
)
429 if (r
->type
== PCI_BASE_ADDRESS_SPACE_IO
) {
430 isa_unassign_ioport(r
->addr
, r
->size
);
432 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
439 static int pci_unregister_device(DeviceState
*dev
)
441 PCIDevice
*pci_dev
= DO_UPCAST(PCIDevice
, qdev
, dev
);
442 PCIDeviceInfo
*info
= DO_UPCAST(PCIDeviceInfo
, qdev
, dev
->info
);
446 ret
= info
->exit(pci_dev
);
450 pci_unregister_io_regions(pci_dev
);
452 qemu_free_irqs(pci_dev
->irq
);
453 pci_dev
->bus
->devices
[pci_dev
->devfn
] = NULL
;
457 void pci_register_bar(PCIDevice
*pci_dev
, int region_num
,
458 pcibus_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
];
475 r
->addr
= PCI_BAR_UNMAPPED
;
478 r
->map_func
= map_func
;
481 addr
= pci_bar(pci_dev
, region_num
);
482 if (region_num
== PCI_ROM_SLOT
) {
483 /* ROM enable bit is writeable */
484 wmask
|= PCI_ROM_ADDRESS_ENABLE
;
486 pci_set_long(pci_dev
->config
+ addr
, type
);
487 pci_set_long(pci_dev
->wmask
+ addr
, wmask
);
488 pci_set_long(pci_dev
->cmask
+ addr
, 0xffffffff);
491 static void pci_update_mappings(PCIDevice
*d
)
495 pcibus_t last_addr
, new_addr
;
497 cmd
= pci_get_word(d
->config
+ PCI_COMMAND
);
498 for(i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
499 r
= &d
->io_regions
[i
];
501 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
502 if (cmd
& PCI_COMMAND_IO
) {
503 new_addr
= pci_get_long(d
->config
+ pci_bar(d
, i
));
504 new_addr
= new_addr
& ~(r
->size
- 1);
505 last_addr
= new_addr
+ r
->size
- 1;
506 /* NOTE: we have only 64K ioports on PC */
507 if (last_addr
<= new_addr
|| new_addr
== 0 ||
508 last_addr
>= 0x10000) {
509 new_addr
= PCI_BAR_UNMAPPED
;
512 new_addr
= PCI_BAR_UNMAPPED
;
515 if (cmd
& PCI_COMMAND_MEMORY
) {
516 new_addr
= pci_get_long(d
->config
+ pci_bar(d
, i
));
517 /* the ROM slot has a specific enable bit */
518 if (i
== PCI_ROM_SLOT
&& !(new_addr
& PCI_ROM_ADDRESS_ENABLE
))
520 new_addr
= new_addr
& ~(r
->size
- 1);
521 last_addr
= new_addr
+ r
->size
- 1;
522 /* NOTE: we do not support wrapping */
523 /* XXX: as we cannot support really dynamic
524 mappings, we handle specific values as invalid
526 if (last_addr
<= new_addr
|| new_addr
== 0 ||
527 last_addr
== PCI_BAR_UNMAPPED
) {
528 new_addr
= PCI_BAR_UNMAPPED
;
532 new_addr
= PCI_BAR_UNMAPPED
;
535 /* now do the real mapping */
536 if (new_addr
!= r
->addr
) {
537 if (r
->addr
!= PCI_BAR_UNMAPPED
) {
538 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
540 /* NOTE: specific hack for IDE in PC case:
541 only one byte must be mapped. */
542 class = pci_get_word(d
->config
+ PCI_CLASS_DEVICE
);
543 if (class == 0x0101 && r
->size
== 4) {
544 isa_unassign_ioport(r
->addr
+ 2, 1);
546 isa_unassign_ioport(r
->addr
, r
->size
);
549 cpu_register_physical_memory(pci_to_cpu_addr(r
->addr
),
552 qemu_unregister_coalesced_mmio(r
->addr
, r
->size
);
556 if (r
->addr
!= PCI_BAR_UNMAPPED
) {
557 r
->map_func(d
, i
, r
->addr
, r
->size
, r
->type
);
564 uint32_t pci_default_read_config(PCIDevice
*d
,
565 uint32_t address
, int len
)
568 assert(len
== 1 || len
== 2 || len
== 4);
569 len
= MIN(len
, PCI_CONFIG_SPACE_SIZE
- address
);
570 memcpy(&val
, d
->config
+ address
, len
);
571 return le32_to_cpu(val
);
574 void pci_default_write_config(PCIDevice
*d
, uint32_t addr
, uint32_t val
, int l
)
576 uint8_t orig
[PCI_CONFIG_SPACE_SIZE
];
579 /* not efficient, but simple */
580 memcpy(orig
, d
->config
, PCI_CONFIG_SPACE_SIZE
);
581 for(i
= 0; i
< l
&& addr
< PCI_CONFIG_SPACE_SIZE
; val
>>= 8, ++i
, ++addr
) {
582 uint8_t wmask
= d
->wmask
[addr
];
583 d
->config
[addr
] = (d
->config
[addr
] & ~wmask
) | (val
& wmask
);
585 if (memcmp(orig
+ PCI_BASE_ADDRESS_0
, d
->config
+ PCI_BASE_ADDRESS_0
, 24)
586 || ((orig
[PCI_COMMAND
] ^ d
->config
[PCI_COMMAND
])
587 & (PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
)))
588 pci_update_mappings(d
);
591 void pci_data_write(void *opaque
, uint32_t addr
, uint32_t val
, int len
)
595 int config_addr
, bus_num
;
598 PCI_DPRINTF("pci_data_write: addr=%08x val=%08x len=%d\n",
601 bus_num
= (addr
>> 16) & 0xff;
602 while (s
&& s
->bus_num
!= bus_num
)
606 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
609 config_addr
= addr
& 0xff;
610 PCI_DPRINTF("pci_config_write: %s: addr=%02x val=%08x len=%d\n",
611 pci_dev
->name
, config_addr
, val
, len
);
612 pci_dev
->config_write(pci_dev
, config_addr
, val
, len
);
615 uint32_t pci_data_read(void *opaque
, uint32_t addr
, int len
)
619 int config_addr
, bus_num
;
622 bus_num
= (addr
>> 16) & 0xff;
623 while (s
&& s
->bus_num
!= bus_num
)
627 pci_dev
= s
->devices
[(addr
>> 8) & 0xff];
644 config_addr
= addr
& 0xff;
645 val
= pci_dev
->config_read(pci_dev
, config_addr
, len
);
646 PCI_DPRINTF("pci_config_read: %s: addr=%02x val=%08x len=%d\n",
647 pci_dev
->name
, config_addr
, val
, len
);
650 PCI_DPRINTF("pci_data_read: addr=%08x val=%08x len=%d\n",
656 /***********************************************************/
657 /* generic PCI irq support */
659 /* 0 <= irq_num <= 3. level must be 0 or 1 */
660 static void pci_set_irq(void *opaque
, int irq_num
, int level
)
662 PCIDevice
*pci_dev
= opaque
;
666 change
= level
- pci_dev
->irq_state
[irq_num
];
670 pci_dev
->irq_state
[irq_num
] = level
;
673 irq_num
= bus
->map_irq(pci_dev
, irq_num
);
676 pci_dev
= bus
->parent_dev
;
678 bus
->irq_count
[irq_num
] += change
;
679 bus
->set_irq(bus
->irq_opaque
, irq_num
, bus
->irq_count
[irq_num
] != 0);
682 /***********************************************************/
683 /* monitor info on PCI */
690 static const pci_class_desc pci_class_descriptions
[] =
692 { 0x0100, "SCSI controller"},
693 { 0x0101, "IDE controller"},
694 { 0x0102, "Floppy controller"},
695 { 0x0103, "IPI controller"},
696 { 0x0104, "RAID controller"},
697 { 0x0106, "SATA controller"},
698 { 0x0107, "SAS controller"},
699 { 0x0180, "Storage controller"},
700 { 0x0200, "Ethernet controller"},
701 { 0x0201, "Token Ring controller"},
702 { 0x0202, "FDDI controller"},
703 { 0x0203, "ATM controller"},
704 { 0x0280, "Network controller"},
705 { 0x0300, "VGA controller"},
706 { 0x0301, "XGA controller"},
707 { 0x0302, "3D controller"},
708 { 0x0380, "Display controller"},
709 { 0x0400, "Video controller"},
710 { 0x0401, "Audio controller"},
712 { 0x0480, "Multimedia controller"},
713 { 0x0500, "RAM controller"},
714 { 0x0501, "Flash controller"},
715 { 0x0580, "Memory controller"},
716 { 0x0600, "Host bridge"},
717 { 0x0601, "ISA bridge"},
718 { 0x0602, "EISA bridge"},
719 { 0x0603, "MC bridge"},
720 { 0x0604, "PCI bridge"},
721 { 0x0605, "PCMCIA bridge"},
722 { 0x0606, "NUBUS bridge"},
723 { 0x0607, "CARDBUS bridge"},
724 { 0x0608, "RACEWAY bridge"},
726 { 0x0c03, "USB controller"},
730 static void pci_info_device(PCIDevice
*d
)
732 Monitor
*mon
= cur_mon
;
735 const pci_class_desc
*desc
;
737 monitor_printf(mon
, " Bus %2d, device %3d, function %d:\n",
738 d
->bus
->bus_num
, PCI_SLOT(d
->devfn
), PCI_FUNC(d
->devfn
));
739 class = pci_get_word(d
->config
+ PCI_CLASS_DEVICE
);
740 monitor_printf(mon
, " ");
741 desc
= pci_class_descriptions
;
742 while (desc
->desc
&& class != desc
->class)
745 monitor_printf(mon
, "%s", desc
->desc
);
747 monitor_printf(mon
, "Class %04x", class);
749 monitor_printf(mon
, ": PCI device %04x:%04x\n",
750 pci_get_word(d
->config
+ PCI_VENDOR_ID
),
751 pci_get_word(d
->config
+ PCI_DEVICE_ID
));
753 if (d
->config
[PCI_INTERRUPT_PIN
] != 0) {
754 monitor_printf(mon
, " IRQ %d.\n",
755 d
->config
[PCI_INTERRUPT_LINE
]);
757 if (class == 0x0604) {
758 monitor_printf(mon
, " BUS %d.\n", d
->config
[0x19]);
760 for(i
= 0;i
< PCI_NUM_REGIONS
; i
++) {
761 r
= &d
->io_regions
[i
];
763 monitor_printf(mon
, " BAR%d: ", i
);
764 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
765 monitor_printf(mon
, "I/O at 0x%04x [0x%04x].\n",
766 r
->addr
, r
->addr
+ r
->size
- 1);
768 monitor_printf(mon
, "32 bit memory at 0x%08x [0x%08x].\n",
769 r
->addr
, r
->addr
+ r
->size
- 1);
773 monitor_printf(mon
, " id \"%s\"\n", d
->qdev
.id
? d
->qdev
.id
: "");
774 if (class == 0x0604 && d
->config
[0x19] != 0) {
775 pci_for_each_device(d
->config
[0x19], pci_info_device
);
779 void pci_for_each_device(int bus_num
, void (*fn
)(PCIDevice
*d
))
781 PCIBus
*bus
= first_bus
;
785 while (bus
&& bus
->bus_num
!= bus_num
)
788 for(devfn
= 0; devfn
< 256; devfn
++) {
789 d
= bus
->devices
[devfn
];
796 void pci_info(Monitor
*mon
)
798 pci_for_each_device(0, pci_info_device
);
801 static const char * const pci_nic_models
[] = {
813 static const char * const pci_nic_names
[] = {
825 /* Initialize a PCI NIC. */
826 /* FIXME callers should check for failure, but don't */
827 PCIDevice
*pci_nic_init(NICInfo
*nd
, const char *default_model
,
828 const char *default_devaddr
)
830 const char *devaddr
= nd
->devaddr
? nd
->devaddr
: default_devaddr
;
837 i
= qemu_find_nic_model(nd
, pci_nic_models
, default_model
);
841 bus
= pci_get_bus_devfn(&devfn
, devaddr
);
843 qemu_error("Invalid PCI device address %s for device %s\n",
844 devaddr
, pci_nic_names
[i
]);
848 pci_dev
= pci_create(bus
, devfn
, pci_nic_names
[i
]);
849 dev
= &pci_dev
->qdev
;
851 dev
->id
= qemu_strdup(nd
->name
);
852 qdev_set_nic_properties(dev
, nd
);
853 if (qdev_init(dev
) < 0)
858 PCIDevice
*pci_nic_init_nofail(NICInfo
*nd
, const char *default_model
,
859 const char *default_devaddr
)
863 if (qemu_show_nic_models(nd
->model
, pci_nic_models
))
866 res
= pci_nic_init(nd
, default_model
, default_devaddr
);
879 static void pci_bridge_write_config(PCIDevice
*d
,
880 uint32_t address
, uint32_t val
, int len
)
882 PCIBridge
*s
= (PCIBridge
*)d
;
884 pci_default_write_config(d
, address
, val
, len
);
885 s
->bus
.bus_num
= d
->config
[PCI_SECONDARY_BUS
];
888 PCIBus
*pci_find_bus(int bus_num
)
890 PCIBus
*bus
= first_bus
;
892 while (bus
&& bus
->bus_num
!= bus_num
)
898 PCIDevice
*pci_find_device(int bus_num
, int slot
, int function
)
900 PCIBus
*bus
= pci_find_bus(bus_num
);
905 return bus
->devices
[PCI_DEVFN(slot
, function
)];
908 static int pci_bridge_initfn(PCIDevice
*dev
)
910 PCIBridge
*s
= DO_UPCAST(PCIBridge
, dev
, dev
);
912 pci_config_set_vendor_id(s
->dev
.config
, s
->vid
);
913 pci_config_set_device_id(s
->dev
.config
, s
->did
);
915 /* TODO: intial value
917 * According to PCI bridge spec, after reset
918 * bus master bit is off
919 * memory space enable bit is off
920 * According to manual (805-1251.pdf).(See abp_pbi.c for its links.)
921 * the reset value should be zero unless the boot pin is tied high
922 * (which is tru) and thus it should be PCI_COMMAND_MEMORY.
924 * For now, don't touch the value.
925 * Later command register will be set to zero and apb_pci.c will
926 * override the value.
927 * Same for latency timer, and multi function bit of header type.
929 pci_set_word(dev
->config
+ PCI_COMMAND
,
930 PCI_COMMAND_MEMORY
| PCI_COMMAND_MASTER
);
932 pci_set_word(dev
->config
+ PCI_STATUS
,
933 PCI_STATUS_66MHZ
| PCI_STATUS_FAST_BACK
);
934 pci_config_set_class(dev
->config
, PCI_CLASS_BRIDGE_PCI
);
935 dev
->config
[PCI_LATENCY_TIMER
] = 0x10;
936 dev
->config
[PCI_HEADER_TYPE
] =
937 PCI_HEADER_TYPE_MULTI_FUNCTION
| PCI_HEADER_TYPE_BRIDGE
;
938 pci_set_word(dev
->config
+ PCI_SEC_STATUS
,
939 PCI_STATUS_66MHZ
| PCI_STATUS_FAST_BACK
);
943 PCIBus
*pci_bridge_init(PCIBus
*bus
, int devfn
, uint16_t vid
, uint16_t did
,
944 pci_map_irq_fn map_irq
, const char *name
)
949 dev
= pci_create(bus
, devfn
, "pci-bridge");
950 qdev_prop_set_uint32(&dev
->qdev
, "vendorid", vid
);
951 qdev_prop_set_uint32(&dev
->qdev
, "deviceid", did
);
952 qdev_init_nofail(&dev
->qdev
);
954 s
= DO_UPCAST(PCIBridge
, dev
, dev
);
955 pci_register_secondary_bus(&s
->bus
, &s
->dev
, map_irq
, name
);
959 static int pci_qdev_init(DeviceState
*qdev
, DeviceInfo
*base
)
961 PCIDevice
*pci_dev
= (PCIDevice
*)qdev
;
962 PCIDeviceInfo
*info
= container_of(base
, PCIDeviceInfo
, qdev
);
966 bus
= FROM_QBUS(PCIBus
, qdev_get_parent_bus(qdev
));
967 devfn
= pci_dev
->devfn
;
968 pci_dev
= do_pci_register_device(pci_dev
, bus
, base
->name
, devfn
,
969 info
->config_read
, info
->config_write
);
971 rc
= info
->init(pci_dev
);
974 if (qdev
->hotplugged
)
975 bus
->hotplug(pci_dev
, 1);
979 static int pci_unplug_device(DeviceState
*qdev
)
981 PCIDevice
*dev
= DO_UPCAST(PCIDevice
, qdev
, qdev
);
983 dev
->bus
->hotplug(dev
, 0);
987 void pci_qdev_register(PCIDeviceInfo
*info
)
989 info
->qdev
.init
= pci_qdev_init
;
990 info
->qdev
.unplug
= pci_unplug_device
;
991 info
->qdev
.exit
= pci_unregister_device
;
992 info
->qdev
.bus_info
= &pci_bus_info
;
993 qdev_register(&info
->qdev
);
996 void pci_qdev_register_many(PCIDeviceInfo
*info
)
998 while (info
->qdev
.name
) {
999 pci_qdev_register(info
);
1004 PCIDevice
*pci_create(PCIBus
*bus
, int devfn
, const char *name
)
1008 dev
= qdev_create(&bus
->qbus
, name
);
1009 qdev_prop_set_uint32(dev
, "addr", devfn
);
1010 return DO_UPCAST(PCIDevice
, qdev
, dev
);
1013 PCIDevice
*pci_create_simple(PCIBus
*bus
, int devfn
, const char *name
)
1015 PCIDevice
*dev
= pci_create(bus
, devfn
, name
);
1016 qdev_init_nofail(&dev
->qdev
);
1020 static int pci_find_space(PCIDevice
*pdev
, uint8_t size
)
1022 int offset
= PCI_CONFIG_HEADER_SIZE
;
1024 for (i
= PCI_CONFIG_HEADER_SIZE
; i
< PCI_CONFIG_SPACE_SIZE
; ++i
)
1027 else if (i
- offset
+ 1 == size
)
1032 static uint8_t pci_find_capability_list(PCIDevice
*pdev
, uint8_t cap_id
,
1037 if (!(pdev
->config
[PCI_STATUS
] & PCI_STATUS_CAP_LIST
))
1040 for (prev
= PCI_CAPABILITY_LIST
; (next
= pdev
->config
[prev
]);
1041 prev
= next
+ PCI_CAP_LIST_NEXT
)
1042 if (pdev
->config
[next
+ PCI_CAP_LIST_ID
] == cap_id
)
1050 /* Reserve space and add capability to the linked list in pci config space */
1051 int pci_add_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1053 uint8_t offset
= pci_find_space(pdev
, size
);
1054 uint8_t *config
= pdev
->config
+ offset
;
1057 config
[PCI_CAP_LIST_ID
] = cap_id
;
1058 config
[PCI_CAP_LIST_NEXT
] = pdev
->config
[PCI_CAPABILITY_LIST
];
1059 pdev
->config
[PCI_CAPABILITY_LIST
] = offset
;
1060 pdev
->config
[PCI_STATUS
] |= PCI_STATUS_CAP_LIST
;
1061 memset(pdev
->used
+ offset
, 0xFF, size
);
1062 /* Make capability read-only by default */
1063 memset(pdev
->wmask
+ offset
, 0, size
);
1064 /* Check capability by default */
1065 memset(pdev
->cmask
+ offset
, 0xFF, size
);
1069 /* Unlink capability from the pci config space. */
1070 void pci_del_capability(PCIDevice
*pdev
, uint8_t cap_id
, uint8_t size
)
1072 uint8_t prev
, offset
= pci_find_capability_list(pdev
, cap_id
, &prev
);
1075 pdev
->config
[prev
] = pdev
->config
[offset
+ PCI_CAP_LIST_NEXT
];
1076 /* Make capability writeable again */
1077 memset(pdev
->wmask
+ offset
, 0xff, size
);
1078 /* Clear cmask as device-specific registers can't be checked */
1079 memset(pdev
->cmask
+ offset
, 0, size
);
1080 memset(pdev
->used
+ offset
, 0, size
);
1082 if (!pdev
->config
[PCI_CAPABILITY_LIST
])
1083 pdev
->config
[PCI_STATUS
] &= ~PCI_STATUS_CAP_LIST
;
1086 /* Reserve space for capability at a known offset (to call after load). */
1087 void pci_reserve_capability(PCIDevice
*pdev
, uint8_t offset
, uint8_t size
)
1089 memset(pdev
->used
+ offset
, 0xff, size
);
1092 uint8_t pci_find_capability(PCIDevice
*pdev
, uint8_t cap_id
)
1094 return pci_find_capability_list(pdev
, cap_id
, NULL
);
1097 static void pcibus_dev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
1099 PCIDevice
*d
= (PCIDevice
*)dev
;
1100 const pci_class_desc
*desc
;
1105 class = pci_get_word(d
->config
+ PCI_CLASS_DEVICE
);
1106 desc
= pci_class_descriptions
;
1107 while (desc
->desc
&& class != desc
->class)
1110 snprintf(ctxt
, sizeof(ctxt
), "%s", desc
->desc
);
1112 snprintf(ctxt
, sizeof(ctxt
), "Class %04x", class);
1115 monitor_printf(mon
, "%*sclass %s, addr %02x:%02x.%x, "
1116 "pci id %04x:%04x (sub %04x:%04x)\n",
1118 d
->bus
->bus_num
, PCI_SLOT(d
->devfn
), PCI_FUNC(d
->devfn
),
1119 pci_get_word(d
->config
+ PCI_VENDOR_ID
),
1120 pci_get_word(d
->config
+ PCI_DEVICE_ID
),
1121 pci_get_word(d
->config
+ PCI_SUBSYSTEM_VENDOR_ID
),
1122 pci_get_word(d
->config
+ PCI_SUBSYSTEM_ID
));
1123 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
1124 r
= &d
->io_regions
[i
];
1127 monitor_printf(mon
, "%*sbar %d: %s at 0x%x [0x%x]\n", indent
, "",
1128 i
, r
->type
& PCI_BASE_ADDRESS_SPACE_IO
? "i/o" : "mem",
1129 r
->addr
, r
->addr
+ r
->size
- 1);
1133 static PCIDeviceInfo bridge_info
= {
1134 .qdev
.name
= "pci-bridge",
1135 .qdev
.size
= sizeof(PCIBridge
),
1136 .init
= pci_bridge_initfn
,
1137 .config_write
= pci_bridge_write_config
,
1138 .qdev
.props
= (Property
[]) {
1139 DEFINE_PROP_HEX32("vendorid", PCIBridge
, vid
, 0),
1140 DEFINE_PROP_HEX32("deviceid", PCIBridge
, did
, 0),
1141 DEFINE_PROP_END_OF_LIST(),
1145 static void pci_register_devices(void)
1147 pci_qdev_register(&bridge_info
);
1150 device_init(pci_register_devices
)