2 * QEMU PIIX4 PCI Bridge Emulation
4 * Copyright (c) 2006 Fabrice Bellard
5 * Copyright (c) 2018 Hervé Poussineau
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
29 #include "hw/southbridge/piix.h"
30 #include "hw/pci/pci.h"
31 #include "hw/isa/isa.h"
32 #include "hw/intc/i8259.h"
33 #include "hw/dma/i8257.h"
34 #include "hw/timer/i8254.h"
35 #include "hw/rtc/mc146818rtc.h"
36 #include "hw/ide/pci.h"
37 #include "migration/vmstate.h"
38 #include "sysemu/reset.h"
39 #include "sysemu/runstate.h"
40 #include "qom/object.h"
50 /* Reset Control Register */
55 OBJECT_DECLARE_SIMPLE_TYPE(PIIX4State
, PIIX4_PCI_DEVICE
)
57 static void piix4_set_irq(void *opaque
, int irq_num
, int level
)
59 int i
, pic_irq
, pic_level
;
60 PIIX4State
*s
= opaque
;
61 PCIBus
*bus
= pci_get_bus(piix4_dev
);
63 /* now we change the pic irq level according to the piix irq mappings */
65 pic_irq
= piix4_dev
->config
[PIIX_PIRQCA
+ irq_num
];
67 /* The pic level is the logical OR of all the PCI irqs mapped to it. */
69 for (i
= 0; i
< 4; i
++) {
70 if (pic_irq
== piix4_dev
->config
[PIIX_PIRQCA
+ i
]) {
71 pic_level
|= pci_bus_get_irq_level(bus
, i
);
74 qemu_set_irq(s
->isa
[pic_irq
], pic_level
);
78 static void piix4_isa_reset(DeviceState
*dev
)
80 PIIX4State
*d
= PIIX4_PCI_DEVICE(dev
);
81 uint8_t *pci_conf
= d
->dev
.config
;
83 pci_conf
[0x04] = 0x07; // master, memory and I/O
84 pci_conf
[0x05] = 0x00;
85 pci_conf
[0x06] = 0x00;
86 pci_conf
[0x07] = 0x02; // PCI_status_devsel_medium
87 pci_conf
[0x4c] = 0x4d;
88 pci_conf
[0x4e] = 0x03;
89 pci_conf
[0x4f] = 0x00;
90 pci_conf
[0x60] = 0x0a; // PCI A -> IRQ 10
91 pci_conf
[0x61] = 0x0a; // PCI B -> IRQ 10
92 pci_conf
[0x62] = 0x0b; // PCI C -> IRQ 11
93 pci_conf
[0x63] = 0x0b; // PCI D -> IRQ 11
94 pci_conf
[0x69] = 0x02;
95 pci_conf
[0x70] = 0x80;
96 pci_conf
[0x76] = 0x0c;
97 pci_conf
[0x77] = 0x0c;
98 pci_conf
[0x78] = 0x02;
99 pci_conf
[0x79] = 0x00;
100 pci_conf
[0x80] = 0x00;
101 pci_conf
[0x82] = 0x00;
102 pci_conf
[0xa0] = 0x08;
103 pci_conf
[0xa2] = 0x00;
104 pci_conf
[0xa3] = 0x00;
105 pci_conf
[0xa4] = 0x00;
106 pci_conf
[0xa5] = 0x00;
107 pci_conf
[0xa6] = 0x00;
108 pci_conf
[0xa7] = 0x00;
109 pci_conf
[0xa8] = 0x0f;
110 pci_conf
[0xaa] = 0x00;
111 pci_conf
[0xab] = 0x00;
112 pci_conf
[0xac] = 0x00;
113 pci_conf
[0xae] = 0x00;
116 static int piix4_ide_post_load(void *opaque
, int version_id
)
118 PIIX4State
*s
= opaque
;
120 if (version_id
== 2) {
127 static const VMStateDescription vmstate_piix4
= {
130 .minimum_version_id
= 2,
131 .post_load
= piix4_ide_post_load
,
132 .fields
= (VMStateField
[]) {
133 VMSTATE_PCI_DEVICE(dev
, PIIX4State
),
134 VMSTATE_UINT8_V(rcr
, PIIX4State
, 3),
135 VMSTATE_END_OF_LIST()
139 static void piix4_request_i8259_irq(void *opaque
, int irq
, int level
)
141 PIIX4State
*s
= opaque
;
142 qemu_set_irq(s
->cpu_intr
, level
);
145 static void piix4_set_i8259_irq(void *opaque
, int irq
, int level
)
147 PIIX4State
*s
= opaque
;
148 qemu_set_irq(s
->isa
[irq
], level
);
151 static void piix4_rcr_write(void *opaque
, hwaddr addr
, uint64_t val
,
154 PIIX4State
*s
= opaque
;
157 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
161 s
->rcr
= val
& 2; /* keep System Reset type only */
164 static uint64_t piix4_rcr_read(void *opaque
, hwaddr addr
, unsigned int len
)
166 PIIX4State
*s
= opaque
;
171 static const MemoryRegionOps piix4_rcr_ops
= {
172 .read
= piix4_rcr_read
,
173 .write
= piix4_rcr_write
,
174 .endianness
= DEVICE_LITTLE_ENDIAN
,
176 .min_access_size
= 1,
177 .max_access_size
= 1,
181 static void piix4_realize(PCIDevice
*dev
, Error
**errp
)
183 PIIX4State
*s
= PIIX4_PCI_DEVICE(dev
);
185 qemu_irq
*i8259_out_irq
;
187 isa_bus
= isa_bus_new(DEVICE(dev
), pci_address_space(dev
),
188 pci_address_space_io(dev
), errp
);
193 qdev_init_gpio_in_named(DEVICE(dev
), piix4_set_i8259_irq
,
194 "isa", ISA_NUM_IRQS
);
195 qdev_init_gpio_out_named(DEVICE(dev
), &s
->cpu_intr
,
198 memory_region_init_io(&s
->rcr_mem
, OBJECT(dev
), &piix4_rcr_ops
, s
,
200 memory_region_add_subregion_overlap(pci_address_space_io(dev
),
201 PIIX_RCR_IOPORT
, &s
->rcr_mem
, 1);
203 /* initialize i8259 pic */
204 i8259_out_irq
= qemu_allocate_irqs(piix4_request_i8259_irq
, s
, 1);
205 s
->isa
= i8259_init(isa_bus
, *i8259_out_irq
);
207 /* initialize ISA irqs */
208 isa_bus_irqs(isa_bus
, s
->isa
);
211 i8254_pit_init(isa_bus
, 0x40, 0, NULL
);
214 i8257_dma_init(isa_bus
, 0);
217 qdev_prop_set_int32(DEVICE(&s
->rtc
), "base_year", 2000);
218 if (!qdev_realize(DEVICE(&s
->rtc
), BUS(isa_bus
), errp
)) {
221 isa_init_irq(ISA_DEVICE(&s
->rtc
), &s
->rtc
.irq
, RTC_ISA_IRQ
);
226 static void piix4_init(Object
*obj
)
228 PIIX4State
*s
= PIIX4_PCI_DEVICE(obj
);
230 object_initialize(&s
->rtc
, sizeof(s
->rtc
), TYPE_MC146818_RTC
);
233 static void piix4_class_init(ObjectClass
*klass
, void *data
)
235 DeviceClass
*dc
= DEVICE_CLASS(klass
);
236 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
238 k
->realize
= piix4_realize
;
239 k
->vendor_id
= PCI_VENDOR_ID_INTEL
;
240 k
->device_id
= PCI_DEVICE_ID_INTEL_82371AB_0
;
241 k
->class_id
= PCI_CLASS_BRIDGE_ISA
;
242 dc
->reset
= piix4_isa_reset
;
243 dc
->desc
= "ISA bridge";
244 dc
->vmsd
= &vmstate_piix4
;
246 * Reason: part of PIIX4 southbridge, needs to be wired up,
247 * e.g. by mips_malta_init()
249 dc
->user_creatable
= false;
250 dc
->hotpluggable
= false;
253 static const TypeInfo piix4_info
= {
254 .name
= TYPE_PIIX4_PCI_DEVICE
,
255 .parent
= TYPE_PCI_DEVICE
,
256 .instance_size
= sizeof(PIIX4State
),
257 .instance_init
= piix4_init
,
258 .class_init
= piix4_class_init
,
259 .interfaces
= (InterfaceInfo
[]) {
260 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
265 static void piix4_register_types(void)
267 type_register_static(&piix4_info
);
270 type_init(piix4_register_types
)
272 static int pci_slot_get_pirq(PCIDevice
*pci_dev
, int irq_num
)
276 slot
= PCI_SLOT(pci_dev
->devfn
);
282 /* AMD 79C973 Ethernet */
285 /* Crystal 4281 Sound */
288 /* PCI slot 1 to 4 */
290 return ((slot
- 18) + irq_num
) & 0x03;
291 /* Unknown device, don't do any translation */
297 DeviceState
*piix4_create(PCIBus
*pci_bus
, ISABus
**isa_bus
, I2CBus
**smbus
)
302 int devfn
= PCI_DEVFN(10, 0);
304 pci
= pci_create_simple_multifunction(pci_bus
, devfn
, true,
305 TYPE_PIIX4_PCI_DEVICE
);
307 s
= PIIX4_PCI_DEVICE(pci
);
309 *isa_bus
= ISA_BUS(qdev_get_child_bus(dev
, "isa.0"));
312 pci
= pci_create_simple(pci_bus
, devfn
+ 1, "piix4-ide");
313 pci_ide_create_devs(pci
);
315 pci_create_simple(pci_bus
, devfn
+ 2, "piix4-usb-uhci");
317 *smbus
= piix4_pm_init(pci_bus
, devfn
+ 3, 0x1100,
318 qdev_get_gpio_in_named(dev
, "isa", 9),
322 pci_bus_irqs(pci_bus
, piix4_set_irq
, pci_slot_get_pirq
, s
, 4);