xref: /qemu/hw/isa/piix.c (revision 2a62c47926420128bb23edd2ba7d4b339db86660)
114a026ddSPhilippe Mathieu-Daudé /*
214a026ddSPhilippe Mathieu-Daudé  * QEMU PIIX PCI ISA Bridge Emulation
314a026ddSPhilippe Mathieu-Daudé  *
414a026ddSPhilippe Mathieu-Daudé  * Copyright (c) 2006 Fabrice Bellard
516971899SBernhard Beschow  * Copyright (c) 2018 Hervé Poussineau
614a026ddSPhilippe Mathieu-Daudé  *
714a026ddSPhilippe Mathieu-Daudé  * Permission is hereby granted, free of charge, to any person obtaining a copy
814a026ddSPhilippe Mathieu-Daudé  * of this software and associated documentation files (the "Software"), to deal
914a026ddSPhilippe Mathieu-Daudé  * in the Software without restriction, including without limitation the rights
1014a026ddSPhilippe Mathieu-Daudé  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1114a026ddSPhilippe Mathieu-Daudé  * copies of the Software, and to permit persons to whom the Software is
1214a026ddSPhilippe Mathieu-Daudé  * furnished to do so, subject to the following conditions:
1314a026ddSPhilippe Mathieu-Daudé  *
1414a026ddSPhilippe Mathieu-Daudé  * The above copyright notice and this permission notice shall be included in
1514a026ddSPhilippe Mathieu-Daudé  * all copies or substantial portions of the Software.
1614a026ddSPhilippe Mathieu-Daudé  *
1714a026ddSPhilippe Mathieu-Daudé  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1814a026ddSPhilippe Mathieu-Daudé  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1914a026ddSPhilippe Mathieu-Daudé  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2014a026ddSPhilippe Mathieu-Daudé  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2114a026ddSPhilippe Mathieu-Daudé  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2214a026ddSPhilippe Mathieu-Daudé  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2314a026ddSPhilippe Mathieu-Daudé  * THE SOFTWARE.
2414a026ddSPhilippe Mathieu-Daudé  */
2514a026ddSPhilippe Mathieu-Daudé 
2614a026ddSPhilippe Mathieu-Daudé #include "qemu/osdep.h"
2714a026ddSPhilippe Mathieu-Daudé #include "qemu/range.h"
28fe3055d2SBernhard Beschow #include "qapi/error.h"
29503a35e7SBernhard Beschow #include "hw/dma/i8257.h"
3014a026ddSPhilippe Mathieu-Daudé #include "hw/southbridge/piix.h"
3116971899SBernhard Beschow #include "hw/timer/i8254.h"
3214a026ddSPhilippe Mathieu-Daudé #include "hw/irq.h"
33f0bc6bf7SBernhard Beschow #include "hw/qdev-properties.h"
34e47e5a5bSBernhard Beschow #include "hw/ide/piix.h"
3516971899SBernhard Beschow #include "hw/intc/i8259.h"
3614a026ddSPhilippe Mathieu-Daudé #include "hw/isa/isa.h"
3714a026ddSPhilippe Mathieu-Daudé #include "sysemu/runstate.h"
3814a026ddSPhilippe Mathieu-Daudé #include "migration/vmstate.h"
3992ea7fb3SIgor Mammedov #include "hw/acpi/acpi_aml_interface.h"
4014a026ddSPhilippe Mathieu-Daudé 
41*2a62c479SBernhard Beschow static void piix_set_irq_pic(PIIXState *s, int pic_irq)
4214a026ddSPhilippe Mathieu-Daudé {
43*2a62c479SBernhard Beschow     qemu_set_irq(s->isa_irqs_in[pic_irq],
44*2a62c479SBernhard Beschow                  !!(s->pic_levels &
4514a026ddSPhilippe Mathieu-Daudé                     (((1ULL << PIIX_NUM_PIRQS) - 1) <<
4614a026ddSPhilippe Mathieu-Daudé                      (pic_irq * PIIX_NUM_PIRQS))));
4714a026ddSPhilippe Mathieu-Daudé }
4814a026ddSPhilippe Mathieu-Daudé 
49*2a62c479SBernhard Beschow static void piix_set_pci_irq_level_internal(PIIXState *s, int pirq, int level)
5014a026ddSPhilippe Mathieu-Daudé {
5114a026ddSPhilippe Mathieu-Daudé     int pic_irq;
5214a026ddSPhilippe Mathieu-Daudé     uint64_t mask;
5314a026ddSPhilippe Mathieu-Daudé 
54*2a62c479SBernhard Beschow     pic_irq = s->dev.config[PIIX_PIRQCA + pirq];
5532f29b26SBernhard Beschow     if (pic_irq >= ISA_NUM_IRQS) {
5614a026ddSPhilippe Mathieu-Daudé         return;
5714a026ddSPhilippe Mathieu-Daudé     }
5814a026ddSPhilippe Mathieu-Daudé 
5914a026ddSPhilippe Mathieu-Daudé     mask = 1ULL << ((pic_irq * PIIX_NUM_PIRQS) + pirq);
60*2a62c479SBernhard Beschow     s->pic_levels &= ~mask;
61*2a62c479SBernhard Beschow     s->pic_levels |= mask * !!level;
6214a026ddSPhilippe Mathieu-Daudé }
6314a026ddSPhilippe Mathieu-Daudé 
64*2a62c479SBernhard Beschow static void piix_set_pci_irq_level(PIIXState *s, int pirq, int level)
6514a026ddSPhilippe Mathieu-Daudé {
6614a026ddSPhilippe Mathieu-Daudé     int pic_irq;
6714a026ddSPhilippe Mathieu-Daudé 
68*2a62c479SBernhard Beschow     pic_irq = s->dev.config[PIIX_PIRQCA + pirq];
6932f29b26SBernhard Beschow     if (pic_irq >= ISA_NUM_IRQS) {
7014a026ddSPhilippe Mathieu-Daudé         return;
7114a026ddSPhilippe Mathieu-Daudé     }
7214a026ddSPhilippe Mathieu-Daudé 
73*2a62c479SBernhard Beschow     piix_set_pci_irq_level_internal(s, pirq, level);
7414a026ddSPhilippe Mathieu-Daudé 
75*2a62c479SBernhard Beschow     piix_set_irq_pic(s, pic_irq);
7614a026ddSPhilippe Mathieu-Daudé }
7714a026ddSPhilippe Mathieu-Daudé 
78*2a62c479SBernhard Beschow static void piix_set_pci_irq(void *opaque, int pirq, int level)
7914a026ddSPhilippe Mathieu-Daudé {
80*2a62c479SBernhard Beschow     PIIXState *s = opaque;
81*2a62c479SBernhard Beschow     piix_set_pci_irq_level(s, pirq, level);
8214a026ddSPhilippe Mathieu-Daudé }
8314a026ddSPhilippe Mathieu-Daudé 
8416971899SBernhard Beschow static void piix4_set_irq(void *opaque, int irq_num, int level)
8516971899SBernhard Beschow {
8616971899SBernhard Beschow     int i, pic_irq, pic_level;
877d6f2659SBernhard Beschow     PIIXState *s = opaque;
8816971899SBernhard Beschow     PCIBus *bus = pci_get_bus(&s->dev);
8916971899SBernhard Beschow 
9016971899SBernhard Beschow     /* now we change the pic irq level according to the piix irq mappings */
9116971899SBernhard Beschow     /* XXX: optimize */
9216971899SBernhard Beschow     pic_irq = s->dev.config[PIIX_PIRQCA + irq_num];
9316971899SBernhard Beschow     if (pic_irq < ISA_NUM_IRQS) {
9416971899SBernhard Beschow         /* The pic level is the logical OR of all the PCI irqs mapped to it. */
9516971899SBernhard Beschow         pic_level = 0;
9616971899SBernhard Beschow         for (i = 0; i < PIIX_NUM_PIRQS; i++) {
9716971899SBernhard Beschow             if (pic_irq == s->dev.config[PIIX_PIRQCA + i]) {
9816971899SBernhard Beschow                 pic_level |= pci_bus_get_irq_level(bus, i);
9916971899SBernhard Beschow             }
10016971899SBernhard Beschow         }
10116971899SBernhard Beschow         qemu_set_irq(s->isa_irqs_in[pic_irq], pic_level);
10216971899SBernhard Beschow     }
10316971899SBernhard Beschow }
10416971899SBernhard Beschow 
1052d7630f5SBernhard Beschow static void piix_request_i8259_irq(void *opaque, int irq, int level)
10616971899SBernhard Beschow {
1077d6f2659SBernhard Beschow     PIIXState *s = opaque;
10816971899SBernhard Beschow     qemu_set_irq(s->cpu_intr, level);
10916971899SBernhard Beschow }
11016971899SBernhard Beschow 
111*2a62c479SBernhard Beschow static PCIINTxRoute piix_route_intx_pin_to_irq(void *opaque, int pin)
11214a026ddSPhilippe Mathieu-Daudé {
113*2a62c479SBernhard Beschow     PCIDevice *pci_dev = opaque;
114*2a62c479SBernhard Beschow     int irq = pci_dev->config[PIIX_PIRQCA + pin];
11514a026ddSPhilippe Mathieu-Daudé     PCIINTxRoute route;
11614a026ddSPhilippe Mathieu-Daudé 
11732f29b26SBernhard Beschow     if (irq < ISA_NUM_IRQS) {
11814a026ddSPhilippe Mathieu-Daudé         route.mode = PCI_INTX_ENABLED;
11914a026ddSPhilippe Mathieu-Daudé         route.irq = irq;
12014a026ddSPhilippe Mathieu-Daudé     } else {
12114a026ddSPhilippe Mathieu-Daudé         route.mode = PCI_INTX_DISABLED;
12214a026ddSPhilippe Mathieu-Daudé         route.irq = -1;
12314a026ddSPhilippe Mathieu-Daudé     }
12414a026ddSPhilippe Mathieu-Daudé     return route;
12514a026ddSPhilippe Mathieu-Daudé }
12614a026ddSPhilippe Mathieu-Daudé 
12714a026ddSPhilippe Mathieu-Daudé /* irq routing is changed. so rebuild bitmap */
128*2a62c479SBernhard Beschow static void piix_update_pci_irq_levels(PIIXState *s)
12914a026ddSPhilippe Mathieu-Daudé {
130*2a62c479SBernhard Beschow     PCIBus *bus = pci_get_bus(&s->dev);
13114a026ddSPhilippe Mathieu-Daudé     int pirq;
13214a026ddSPhilippe Mathieu-Daudé 
133*2a62c479SBernhard Beschow     s->pic_levels = 0;
13414a026ddSPhilippe Mathieu-Daudé     for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
135*2a62c479SBernhard Beschow         piix_set_pci_irq_level(s, pirq, pci_bus_get_irq_level(bus, pirq));
13614a026ddSPhilippe Mathieu-Daudé     }
13714a026ddSPhilippe Mathieu-Daudé }
13814a026ddSPhilippe Mathieu-Daudé 
139*2a62c479SBernhard Beschow static void piix_write_config(PCIDevice *dev, uint32_t address, uint32_t val,
140*2a62c479SBernhard Beschow                               int len)
14114a026ddSPhilippe Mathieu-Daudé {
14214a026ddSPhilippe Mathieu-Daudé     pci_default_write_config(dev, address, val, len);
14314a026ddSPhilippe Mathieu-Daudé     if (ranges_overlap(address, len, PIIX_PIRQCA, 4)) {
144*2a62c479SBernhard Beschow         PIIXState *s = PIIX_PCI_DEVICE(dev);
14514a026ddSPhilippe Mathieu-Daudé         int pic_irq;
14614a026ddSPhilippe Mathieu-Daudé 
147*2a62c479SBernhard Beschow         pci_bus_fire_intx_routing_notifier(pci_get_bus(&s->dev));
148*2a62c479SBernhard Beschow         piix_update_pci_irq_levels(s);
14932f29b26SBernhard Beschow         for (pic_irq = 0; pic_irq < ISA_NUM_IRQS; pic_irq++) {
150*2a62c479SBernhard Beschow             piix_set_irq_pic(s, pic_irq);
15114a026ddSPhilippe Mathieu-Daudé         }
15214a026ddSPhilippe Mathieu-Daudé     }
15314a026ddSPhilippe Mathieu-Daudé }
15414a026ddSPhilippe Mathieu-Daudé 
1557d6f2659SBernhard Beschow static void piix_reset(DeviceState *dev)
15614a026ddSPhilippe Mathieu-Daudé {
1577d6f2659SBernhard Beschow     PIIXState *d = PIIX_PCI_DEVICE(dev);
15814a026ddSPhilippe Mathieu-Daudé     uint8_t *pci_conf = d->dev.config;
15914a026ddSPhilippe Mathieu-Daudé 
16014a026ddSPhilippe Mathieu-Daudé     pci_conf[0x04] = 0x07; /* master, memory and I/O */
16114a026ddSPhilippe Mathieu-Daudé     pci_conf[0x05] = 0x00;
16214a026ddSPhilippe Mathieu-Daudé     pci_conf[0x06] = 0x00;
16314a026ddSPhilippe Mathieu-Daudé     pci_conf[0x07] = 0x02; /* PCI_status_devsel_medium */
16414a026ddSPhilippe Mathieu-Daudé     pci_conf[0x4c] = 0x4d;
16514a026ddSPhilippe Mathieu-Daudé     pci_conf[0x4e] = 0x03;
16614a026ddSPhilippe Mathieu-Daudé     pci_conf[0x4f] = 0x00;
16714a026ddSPhilippe Mathieu-Daudé     pci_conf[0x60] = 0x80;
16814a026ddSPhilippe Mathieu-Daudé     pci_conf[0x61] = 0x80;
16914a026ddSPhilippe Mathieu-Daudé     pci_conf[0x62] = 0x80;
17014a026ddSPhilippe Mathieu-Daudé     pci_conf[0x63] = 0x80;
17114a026ddSPhilippe Mathieu-Daudé     pci_conf[0x69] = 0x02;
17214a026ddSPhilippe Mathieu-Daudé     pci_conf[0x70] = 0x80;
17314a026ddSPhilippe Mathieu-Daudé     pci_conf[0x76] = 0x0c;
17414a026ddSPhilippe Mathieu-Daudé     pci_conf[0x77] = 0x0c;
17514a026ddSPhilippe Mathieu-Daudé     pci_conf[0x78] = 0x02;
17614a026ddSPhilippe Mathieu-Daudé     pci_conf[0x79] = 0x00;
17714a026ddSPhilippe Mathieu-Daudé     pci_conf[0x80] = 0x00;
17814a026ddSPhilippe Mathieu-Daudé     pci_conf[0x82] = 0x00;
17914a026ddSPhilippe Mathieu-Daudé     pci_conf[0xa0] = 0x08;
18014a026ddSPhilippe Mathieu-Daudé     pci_conf[0xa2] = 0x00;
18114a026ddSPhilippe Mathieu-Daudé     pci_conf[0xa3] = 0x00;
18214a026ddSPhilippe Mathieu-Daudé     pci_conf[0xa4] = 0x00;
18314a026ddSPhilippe Mathieu-Daudé     pci_conf[0xa5] = 0x00;
18414a026ddSPhilippe Mathieu-Daudé     pci_conf[0xa6] = 0x00;
18514a026ddSPhilippe Mathieu-Daudé     pci_conf[0xa7] = 0x00;
18614a026ddSPhilippe Mathieu-Daudé     pci_conf[0xa8] = 0x0f;
18714a026ddSPhilippe Mathieu-Daudé     pci_conf[0xaa] = 0x00;
18814a026ddSPhilippe Mathieu-Daudé     pci_conf[0xab] = 0x00;
18914a026ddSPhilippe Mathieu-Daudé     pci_conf[0xac] = 0x00;
19014a026ddSPhilippe Mathieu-Daudé     pci_conf[0xae] = 0x00;
19114a026ddSPhilippe Mathieu-Daudé 
19214a026ddSPhilippe Mathieu-Daudé     d->pic_levels = 0;
19314a026ddSPhilippe Mathieu-Daudé     d->rcr = 0;
19414a026ddSPhilippe Mathieu-Daudé }
19514a026ddSPhilippe Mathieu-Daudé 
196*2a62c479SBernhard Beschow static int piix_post_load(void *opaque, int version_id)
19714a026ddSPhilippe Mathieu-Daudé {
198*2a62c479SBernhard Beschow     PIIXState *s = opaque;
19914a026ddSPhilippe Mathieu-Daudé     int pirq;
20014a026ddSPhilippe Mathieu-Daudé 
20114a026ddSPhilippe Mathieu-Daudé     /*
20214a026ddSPhilippe Mathieu-Daudé      * Because the i8259 has not been deserialized yet, qemu_irq_raise
20314a026ddSPhilippe Mathieu-Daudé      * might bring the system to a different state than the saved one;
20414a026ddSPhilippe Mathieu-Daudé      * for example, the interrupt could be masked but the i8259 would
20514a026ddSPhilippe Mathieu-Daudé      * not know that yet and would trigger an interrupt in the CPU.
20614a026ddSPhilippe Mathieu-Daudé      *
20714a026ddSPhilippe Mathieu-Daudé      * Here, we update irq levels without raising the interrupt.
20814a026ddSPhilippe Mathieu-Daudé      * Interrupt state will be deserialized separately through the i8259.
20914a026ddSPhilippe Mathieu-Daudé      */
210*2a62c479SBernhard Beschow     s->pic_levels = 0;
21114a026ddSPhilippe Mathieu-Daudé     for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
212*2a62c479SBernhard Beschow         piix_set_pci_irq_level_internal(s, pirq,
213*2a62c479SBernhard Beschow             pci_bus_get_irq_level(pci_get_bus(&s->dev), pirq));
21414a026ddSPhilippe Mathieu-Daudé     }
21514a026ddSPhilippe Mathieu-Daudé     return 0;
21614a026ddSPhilippe Mathieu-Daudé }
21714a026ddSPhilippe Mathieu-Daudé 
21816971899SBernhard Beschow static int piix4_post_load(void *opaque, int version_id)
21916971899SBernhard Beschow {
2207d6f2659SBernhard Beschow     PIIXState *s = opaque;
22116971899SBernhard Beschow 
22216971899SBernhard Beschow     if (version_id == 2) {
22316971899SBernhard Beschow         s->rcr = 0;
22416971899SBernhard Beschow     }
22516971899SBernhard Beschow 
22616971899SBernhard Beschow     return 0;
22716971899SBernhard Beschow }
22816971899SBernhard Beschow 
22914a026ddSPhilippe Mathieu-Daudé static int piix3_pre_save(void *opaque)
23014a026ddSPhilippe Mathieu-Daudé {
23114a026ddSPhilippe Mathieu-Daudé     int i;
2329769cfc3SBernhard Beschow     PIIXState *piix3 = opaque;
23314a026ddSPhilippe Mathieu-Daudé 
23414a026ddSPhilippe Mathieu-Daudé     for (i = 0; i < ARRAY_SIZE(piix3->pci_irq_levels_vmstate); i++) {
23514a026ddSPhilippe Mathieu-Daudé         piix3->pci_irq_levels_vmstate[i] =
23614a026ddSPhilippe Mathieu-Daudé             pci_bus_get_irq_level(pci_get_bus(&piix3->dev), i);
23714a026ddSPhilippe Mathieu-Daudé     }
23814a026ddSPhilippe Mathieu-Daudé 
23914a026ddSPhilippe Mathieu-Daudé     return 0;
24014a026ddSPhilippe Mathieu-Daudé }
24114a026ddSPhilippe Mathieu-Daudé 
24214a026ddSPhilippe Mathieu-Daudé static bool piix3_rcr_needed(void *opaque)
24314a026ddSPhilippe Mathieu-Daudé {
2449769cfc3SBernhard Beschow     PIIXState *piix3 = opaque;
24514a026ddSPhilippe Mathieu-Daudé 
24614a026ddSPhilippe Mathieu-Daudé     return (piix3->rcr != 0);
24714a026ddSPhilippe Mathieu-Daudé }
24814a026ddSPhilippe Mathieu-Daudé 
24914a026ddSPhilippe Mathieu-Daudé static const VMStateDescription vmstate_piix3_rcr = {
25014a026ddSPhilippe Mathieu-Daudé     .name = "PIIX3/rcr",
25114a026ddSPhilippe Mathieu-Daudé     .version_id = 1,
25214a026ddSPhilippe Mathieu-Daudé     .minimum_version_id = 1,
25314a026ddSPhilippe Mathieu-Daudé     .needed = piix3_rcr_needed,
25414a026ddSPhilippe Mathieu-Daudé     .fields = (VMStateField[]) {
2559769cfc3SBernhard Beschow         VMSTATE_UINT8(rcr, PIIXState),
25614a026ddSPhilippe Mathieu-Daudé         VMSTATE_END_OF_LIST()
25714a026ddSPhilippe Mathieu-Daudé     }
25814a026ddSPhilippe Mathieu-Daudé };
25914a026ddSPhilippe Mathieu-Daudé 
26014a026ddSPhilippe Mathieu-Daudé static const VMStateDescription vmstate_piix3 = {
26114a026ddSPhilippe Mathieu-Daudé     .name = "PIIX3",
26214a026ddSPhilippe Mathieu-Daudé     .version_id = 3,
26314a026ddSPhilippe Mathieu-Daudé     .minimum_version_id = 2,
264*2a62c479SBernhard Beschow     .post_load = piix_post_load,
26514a026ddSPhilippe Mathieu-Daudé     .pre_save = piix3_pre_save,
26614a026ddSPhilippe Mathieu-Daudé     .fields = (VMStateField[]) {
2679769cfc3SBernhard Beschow         VMSTATE_PCI_DEVICE(dev, PIIXState),
2689769cfc3SBernhard Beschow         VMSTATE_INT32_ARRAY_V(pci_irq_levels_vmstate, PIIXState,
26914a026ddSPhilippe Mathieu-Daudé                               PIIX_NUM_PIRQS, 3),
27014a026ddSPhilippe Mathieu-Daudé         VMSTATE_END_OF_LIST()
27114a026ddSPhilippe Mathieu-Daudé     },
27214a026ddSPhilippe Mathieu-Daudé     .subsections = (const VMStateDescription*[]) {
27314a026ddSPhilippe Mathieu-Daudé         &vmstate_piix3_rcr,
27414a026ddSPhilippe Mathieu-Daudé         NULL
27514a026ddSPhilippe Mathieu-Daudé     }
27614a026ddSPhilippe Mathieu-Daudé };
27714a026ddSPhilippe Mathieu-Daudé 
27816971899SBernhard Beschow static const VMStateDescription vmstate_piix4 = {
27916971899SBernhard Beschow     .name = "PIIX4",
28016971899SBernhard Beschow     .version_id = 3,
28116971899SBernhard Beschow     .minimum_version_id = 2,
28216971899SBernhard Beschow     .post_load = piix4_post_load,
28316971899SBernhard Beschow     .fields = (VMStateField[]) {
2847d6f2659SBernhard Beschow         VMSTATE_PCI_DEVICE(dev, PIIXState),
2857d6f2659SBernhard Beschow         VMSTATE_UINT8_V(rcr, PIIXState, 3),
28616971899SBernhard Beschow         VMSTATE_END_OF_LIST()
28716971899SBernhard Beschow     }
28816971899SBernhard Beschow };
28914a026ddSPhilippe Mathieu-Daudé 
29014a026ddSPhilippe Mathieu-Daudé static void rcr_write(void *opaque, hwaddr addr, uint64_t val, unsigned len)
29114a026ddSPhilippe Mathieu-Daudé {
2929769cfc3SBernhard Beschow     PIIXState *d = opaque;
29314a026ddSPhilippe Mathieu-Daudé 
29414a026ddSPhilippe Mathieu-Daudé     if (val & 4) {
29514a026ddSPhilippe Mathieu-Daudé         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
29614a026ddSPhilippe Mathieu-Daudé         return;
29714a026ddSPhilippe Mathieu-Daudé     }
29814a026ddSPhilippe Mathieu-Daudé     d->rcr = val & 2; /* keep System Reset type only */
29914a026ddSPhilippe Mathieu-Daudé }
30014a026ddSPhilippe Mathieu-Daudé 
30114a026ddSPhilippe Mathieu-Daudé static uint64_t rcr_read(void *opaque, hwaddr addr, unsigned len)
30214a026ddSPhilippe Mathieu-Daudé {
3039769cfc3SBernhard Beschow     PIIXState *d = opaque;
30414a026ddSPhilippe Mathieu-Daudé 
30514a026ddSPhilippe Mathieu-Daudé     return d->rcr;
30614a026ddSPhilippe Mathieu-Daudé }
30714a026ddSPhilippe Mathieu-Daudé 
30814a026ddSPhilippe Mathieu-Daudé static const MemoryRegionOps rcr_ops = {
30914a026ddSPhilippe Mathieu-Daudé     .read = rcr_read,
31014a026ddSPhilippe Mathieu-Daudé     .write = rcr_write,
3113ee15e80SBernhard Beschow     .endianness = DEVICE_LITTLE_ENDIAN,
3123ee15e80SBernhard Beschow     .impl = {
3133ee15e80SBernhard Beschow         .min_access_size = 1,
3143ee15e80SBernhard Beschow         .max_access_size = 1,
3153ee15e80SBernhard Beschow     },
31614a026ddSPhilippe Mathieu-Daudé };
31714a026ddSPhilippe Mathieu-Daudé 
3182922dbc2SBernhard Beschow static void pci_piix_realize(PCIDevice *dev, const char *uhci_type,
3192922dbc2SBernhard Beschow                              Error **errp)
32014a026ddSPhilippe Mathieu-Daudé {
3219769cfc3SBernhard Beschow     PIIXState *d = PIIX_PCI_DEVICE(dev);
322e47e5a5bSBernhard Beschow     PCIBus *pci_bus = pci_get_bus(dev);
323503a35e7SBernhard Beschow     ISABus *isa_bus;
32456b1f50eSBernhard Beschow     uint32_t irq;
32514a026ddSPhilippe Mathieu-Daudé 
32657654b8eSBernhard Beschow     isa_bus = isa_bus_new(DEVICE(d), pci_address_space(dev),
327503a35e7SBernhard Beschow                           pci_address_space_io(dev), errp);
328503a35e7SBernhard Beschow     if (!isa_bus) {
32914a026ddSPhilippe Mathieu-Daudé         return;
33014a026ddSPhilippe Mathieu-Daudé     }
33114a026ddSPhilippe Mathieu-Daudé 
33214a026ddSPhilippe Mathieu-Daudé     memory_region_init_io(&d->rcr_mem, OBJECT(dev), &rcr_ops, d,
333f97479caSBernhard Beschow                           "piix-reset-control", 1);
33414a026ddSPhilippe Mathieu-Daudé     memory_region_add_subregion_overlap(pci_address_space_io(dev),
33514a026ddSPhilippe Mathieu-Daudé                                         PIIX_RCR_IOPORT, &d->rcr_mem, 1);
33614a026ddSPhilippe Mathieu-Daudé 
3372d7630f5SBernhard Beschow     /* PIC */
3382d7630f5SBernhard Beschow     if (d->has_pic) {
3392d7630f5SBernhard Beschow         qemu_irq *i8259_out_irq = qemu_allocate_irqs(piix_request_i8259_irq, d,
3402d7630f5SBernhard Beschow                                                      1);
3412d7630f5SBernhard Beschow         qemu_irq *i8259 = i8259_init(isa_bus, *i8259_out_irq);
3422d7630f5SBernhard Beschow         size_t i;
3432d7630f5SBernhard Beschow 
3442d7630f5SBernhard Beschow         for (i = 0; i < ISA_NUM_IRQS; i++) {
3452d7630f5SBernhard Beschow             d->isa_irqs_in[i] = i8259[i];
3462d7630f5SBernhard Beschow         }
3472d7630f5SBernhard Beschow 
3482d7630f5SBernhard Beschow         g_free(i8259);
3492d7630f5SBernhard Beschow 
3502d7630f5SBernhard Beschow         qdev_init_gpio_out_named(DEVICE(dev), &d->cpu_intr, "intr", 1);
3512d7630f5SBernhard Beschow     }
3522d7630f5SBernhard Beschow 
35364127940SBernhard Beschow     isa_bus_register_input_irqs(isa_bus, d->isa_irqs_in);
35464127940SBernhard Beschow 
355ac433035SBernhard Beschow     /* PIT */
356ac433035SBernhard Beschow     if (d->has_pit) {
357ac433035SBernhard Beschow         i8254_pit_init(isa_bus, 0x40, 0, NULL);
358ac433035SBernhard Beschow     }
359ac433035SBernhard Beschow 
360503a35e7SBernhard Beschow     i8257_dma_init(isa_bus, 0);
361f0bc6bf7SBernhard Beschow 
362f0bc6bf7SBernhard Beschow     /* RTC */
363f0bc6bf7SBernhard Beschow     qdev_prop_set_int32(DEVICE(&d->rtc), "base_year", 2000);
364f0bc6bf7SBernhard Beschow     if (!qdev_realize(DEVICE(&d->rtc), BUS(isa_bus), errp)) {
365f0bc6bf7SBernhard Beschow         return;
366f0bc6bf7SBernhard Beschow     }
36756b1f50eSBernhard Beschow     irq = object_property_get_uint(OBJECT(&d->rtc), "irq", &error_fatal);
36856b1f50eSBernhard Beschow     isa_connect_gpio_out(ISA_DEVICE(&d->rtc), 0, irq);
369e47e5a5bSBernhard Beschow 
370e47e5a5bSBernhard Beschow     /* IDE */
371e47e5a5bSBernhard Beschow     qdev_prop_set_int32(DEVICE(&d->ide), "addr", dev->devfn + 1);
372e47e5a5bSBernhard Beschow     if (!qdev_realize(DEVICE(&d->ide), BUS(pci_bus), errp)) {
373e47e5a5bSBernhard Beschow         return;
374e47e5a5bSBernhard Beschow     }
3756fe4464cSBernhard Beschow 
3766fe4464cSBernhard Beschow     /* USB */
3776fe4464cSBernhard Beschow     if (d->has_usb) {
3782922dbc2SBernhard Beschow         object_initialize_child(OBJECT(dev), "uhci", &d->uhci, uhci_type);
3796fe4464cSBernhard Beschow         qdev_prop_set_int32(DEVICE(&d->uhci), "addr", dev->devfn + 2);
3806fe4464cSBernhard Beschow         if (!qdev_realize(DEVICE(&d->uhci), BUS(pci_bus), errp)) {
3816fe4464cSBernhard Beschow             return;
3826fe4464cSBernhard Beschow         }
3836fe4464cSBernhard Beschow     }
3840a15cf08SBernhard Beschow 
3850a15cf08SBernhard Beschow     /* Power Management */
3860a15cf08SBernhard Beschow     if (d->has_acpi) {
3870a15cf08SBernhard Beschow         object_initialize_child(OBJECT(d), "pm", &d->pm, TYPE_PIIX4_PM);
3880a15cf08SBernhard Beschow         qdev_prop_set_int32(DEVICE(&d->pm), "addr", dev->devfn + 3);
3890a15cf08SBernhard Beschow         qdev_prop_set_uint32(DEVICE(&d->pm), "smb_io_base", d->smb_io_base);
3900a15cf08SBernhard Beschow         qdev_prop_set_bit(DEVICE(&d->pm), "smm-enabled", d->smm_enabled);
3910a15cf08SBernhard Beschow         if (!qdev_realize(DEVICE(&d->pm), BUS(pci_bus), errp)) {
3920a15cf08SBernhard Beschow             return;
3930a15cf08SBernhard Beschow         }
3940a15cf08SBernhard Beschow         qdev_connect_gpio_out(DEVICE(&d->pm), 0, d->isa_irqs_in[9]);
3950a15cf08SBernhard Beschow     }
39614a026ddSPhilippe Mathieu-Daudé }
39714a026ddSPhilippe Mathieu-Daudé 
39892ea7fb3SIgor Mammedov static void build_pci_isa_aml(AcpiDevAmlIf *adev, Aml *scope)
39992ea7fb3SIgor Mammedov {
40047a373faSIgor Mammedov     Aml *field;
4014fd75ce0SIgor Mammedov     Aml *sb_scope = aml_scope("\\_SB");
40292ea7fb3SIgor Mammedov     BusState *bus = qdev_get_child_bus(DEVICE(adev), "isa.0");
40392ea7fb3SIgor Mammedov 
40492ea7fb3SIgor Mammedov     /* PIIX PCI to ISA irq remapping */
40592ea7fb3SIgor Mammedov     aml_append(scope, aml_operation_region("P40C", AML_PCI_CONFIG,
40692ea7fb3SIgor Mammedov                                            aml_int(0x60), 0x04));
40747a373faSIgor Mammedov     /* Fields declarion has to happen *after* operation region */
4084fd75ce0SIgor Mammedov     field = aml_field("PCI0.S08.P40C", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
40947a373faSIgor Mammedov     aml_append(field, aml_named_field("PRQ0", 8));
41047a373faSIgor Mammedov     aml_append(field, aml_named_field("PRQ1", 8));
41147a373faSIgor Mammedov     aml_append(field, aml_named_field("PRQ2", 8));
41247a373faSIgor Mammedov     aml_append(field, aml_named_field("PRQ3", 8));
4134fd75ce0SIgor Mammedov     aml_append(sb_scope, field);
4144fd75ce0SIgor Mammedov     aml_append(scope, sb_scope);
41547a373faSIgor Mammedov 
4169c6c0aeaSBernhard Beschow     qbus_build_aml(bus, scope);
41792ea7fb3SIgor Mammedov }
41892ea7fb3SIgor Mammedov 
4197d6f2659SBernhard Beschow static void pci_piix_init(Object *obj)
420f0bc6bf7SBernhard Beschow {
4219769cfc3SBernhard Beschow     PIIXState *d = PIIX_PCI_DEVICE(obj);
422f0bc6bf7SBernhard Beschow 
42340f70623SBernhard Beschow     qdev_init_gpio_out_named(DEVICE(obj), d->isa_irqs_in, "isa-irqs",
42440f70623SBernhard Beschow                              ISA_NUM_IRQS);
425001cb25fSBernhard Beschow 
426f0bc6bf7SBernhard Beschow     object_initialize_child(obj, "rtc", &d->rtc, TYPE_MC146818_RTC);
427f0bc6bf7SBernhard Beschow }
428f0bc6bf7SBernhard Beschow 
4292922dbc2SBernhard Beschow static Property pci_piix_props[] = {
4309769cfc3SBernhard Beschow     DEFINE_PROP_UINT32("smb_io_base", PIIXState, smb_io_base, 0),
4319769cfc3SBernhard Beschow     DEFINE_PROP_BOOL("has-acpi", PIIXState, has_acpi, true),
4322d7630f5SBernhard Beschow     DEFINE_PROP_BOOL("has-pic", PIIXState, has_pic, true),
433ac433035SBernhard Beschow     DEFINE_PROP_BOOL("has-pit", PIIXState, has_pit, true),
4349769cfc3SBernhard Beschow     DEFINE_PROP_BOOL("has-usb", PIIXState, has_usb, true),
4359769cfc3SBernhard Beschow     DEFINE_PROP_BOOL("smm-enabled", PIIXState, smm_enabled, false),
4366fe4464cSBernhard Beschow     DEFINE_PROP_END_OF_LIST(),
4376fe4464cSBernhard Beschow };
4386fe4464cSBernhard Beschow 
4397d6f2659SBernhard Beschow static void pci_piix_class_init(ObjectClass *klass, void *data)
44014a026ddSPhilippe Mathieu-Daudé {
44114a026ddSPhilippe Mathieu-Daudé     DeviceClass *dc = DEVICE_CLASS(klass);
44214a026ddSPhilippe Mathieu-Daudé     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
44392ea7fb3SIgor Mammedov     AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass);
44414a026ddSPhilippe Mathieu-Daudé 
4457d6f2659SBernhard Beschow     dc->reset       = piix_reset;
44614a026ddSPhilippe Mathieu-Daudé     dc->desc        = "ISA bridge";
44714a026ddSPhilippe Mathieu-Daudé     dc->hotpluggable   = false;
44814a026ddSPhilippe Mathieu-Daudé     k->vendor_id    = PCI_VENDOR_ID_INTEL;
44914a026ddSPhilippe Mathieu-Daudé     k->class_id     = PCI_CLASS_BRIDGE_ISA;
45014a026ddSPhilippe Mathieu-Daudé     /*
4517d6f2659SBernhard Beschow      * Reason: part of PIIX southbridge, needs to be wired up by e.g.
45214a026ddSPhilippe Mathieu-Daudé      * pc_piix.c's pc_init1()
45314a026ddSPhilippe Mathieu-Daudé      */
45414a026ddSPhilippe Mathieu-Daudé     dc->user_creatable = false;
4552922dbc2SBernhard Beschow     device_class_set_props(dc, pci_piix_props);
45692ea7fb3SIgor Mammedov     adevc->build_dev_aml = build_pci_isa_aml;
45714a026ddSPhilippe Mathieu-Daudé }
45814a026ddSPhilippe Mathieu-Daudé 
4599769cfc3SBernhard Beschow static const TypeInfo piix_pci_type_info = {
4609769cfc3SBernhard Beschow     .name = TYPE_PIIX_PCI_DEVICE,
46114a026ddSPhilippe Mathieu-Daudé     .parent = TYPE_PCI_DEVICE,
4629769cfc3SBernhard Beschow     .instance_size = sizeof(PIIXState),
4637d6f2659SBernhard Beschow     .instance_init = pci_piix_init,
46414a026ddSPhilippe Mathieu-Daudé     .abstract = true,
4657d6f2659SBernhard Beschow     .class_init = pci_piix_class_init,
46614a026ddSPhilippe Mathieu-Daudé     .interfaces = (InterfaceInfo[]) {
46714a026ddSPhilippe Mathieu-Daudé         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
46892ea7fb3SIgor Mammedov         { TYPE_ACPI_DEV_AML_IF },
46914a026ddSPhilippe Mathieu-Daudé         { },
47014a026ddSPhilippe Mathieu-Daudé     },
47114a026ddSPhilippe Mathieu-Daudé };
47214a026ddSPhilippe Mathieu-Daudé 
473fe3055d2SBernhard Beschow static void piix3_realize(PCIDevice *dev, Error **errp)
474fe3055d2SBernhard Beschow {
475fe3055d2SBernhard Beschow     ERRP_GUARD();
4769769cfc3SBernhard Beschow     PIIXState *piix3 = PIIX_PCI_DEVICE(dev);
477fe3055d2SBernhard Beschow     PCIBus *pci_bus = pci_get_bus(dev);
478fe3055d2SBernhard Beschow 
4792922dbc2SBernhard Beschow     pci_piix_realize(dev, TYPE_PIIX3_USB_UHCI, errp);
480fe3055d2SBernhard Beschow     if (*errp) {
481fe3055d2SBernhard Beschow         return;
482fe3055d2SBernhard Beschow     }
483fe3055d2SBernhard Beschow 
484*2a62c479SBernhard Beschow     pci_bus_irqs(pci_bus, piix_set_pci_irq, piix3, PIIX_NUM_PIRQS);
485*2a62c479SBernhard Beschow     pci_bus_set_route_irq_fn(pci_bus, piix_route_intx_pin_to_irq);
48605c049f1SBernhard Beschow }
487fe3055d2SBernhard Beschow 
4887d6f2659SBernhard Beschow static void piix3_init(Object *obj)
4897d6f2659SBernhard Beschow {
4907d6f2659SBernhard Beschow     PIIXState *d = PIIX_PCI_DEVICE(obj);
4917d6f2659SBernhard Beschow 
4927d6f2659SBernhard Beschow     object_initialize_child(obj, "ide", &d->ide, TYPE_PIIX3_IDE);
4937d6f2659SBernhard Beschow }
4947d6f2659SBernhard Beschow 
49514a026ddSPhilippe Mathieu-Daudé static void piix3_class_init(ObjectClass *klass, void *data)
49614a026ddSPhilippe Mathieu-Daudé {
4977d6f2659SBernhard Beschow     DeviceClass *dc = DEVICE_CLASS(klass);
49814a026ddSPhilippe Mathieu-Daudé     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
49914a026ddSPhilippe Mathieu-Daudé 
500*2a62c479SBernhard Beschow     k->config_write = piix_write_config;
501fe3055d2SBernhard Beschow     k->realize = piix3_realize;
5027d6f2659SBernhard Beschow     /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
5037d6f2659SBernhard Beschow     k->device_id = PCI_DEVICE_ID_INTEL_82371SB_0;
5047d6f2659SBernhard Beschow     dc->vmsd = &vmstate_piix3;
50514a026ddSPhilippe Mathieu-Daudé }
50614a026ddSPhilippe Mathieu-Daudé 
50714a026ddSPhilippe Mathieu-Daudé static const TypeInfo piix3_info = {
50814a026ddSPhilippe Mathieu-Daudé     .name          = TYPE_PIIX3_DEVICE,
5099769cfc3SBernhard Beschow     .parent        = TYPE_PIIX_PCI_DEVICE,
5107d6f2659SBernhard Beschow     .instance_init = piix3_init,
51114a026ddSPhilippe Mathieu-Daudé     .class_init    = piix3_class_init,
51214a026ddSPhilippe Mathieu-Daudé };
51314a026ddSPhilippe Mathieu-Daudé 
51416971899SBernhard Beschow static void piix4_realize(PCIDevice *dev, Error **errp)
51516971899SBernhard Beschow {
5162922dbc2SBernhard Beschow     ERRP_GUARD();
5177d6f2659SBernhard Beschow     PIIXState *s = PIIX_PCI_DEVICE(dev);
51816971899SBernhard Beschow     PCIBus *pci_bus = pci_get_bus(dev);
51916971899SBernhard Beschow 
5202922dbc2SBernhard Beschow     pci_piix_realize(dev, TYPE_PIIX4_USB_UHCI, errp);
5212922dbc2SBernhard Beschow     if (*errp) {
52216971899SBernhard Beschow         return;
52316971899SBernhard Beschow     }
52416971899SBernhard Beschow 
52516971899SBernhard Beschow     pci_bus_irqs(pci_bus, piix4_set_irq, s, PIIX_NUM_PIRQS);
52616971899SBernhard Beschow }
52716971899SBernhard Beschow 
52816971899SBernhard Beschow static void piix4_init(Object *obj)
52916971899SBernhard Beschow {
5307d6f2659SBernhard Beschow     PIIXState *s = PIIX_PCI_DEVICE(obj);
53116971899SBernhard Beschow 
53216971899SBernhard Beschow     object_initialize_child(obj, "ide", &s->ide, TYPE_PIIX4_IDE);
53316971899SBernhard Beschow }
53416971899SBernhard Beschow 
53516971899SBernhard Beschow static void piix4_class_init(ObjectClass *klass, void *data)
53616971899SBernhard Beschow {
53716971899SBernhard Beschow     DeviceClass *dc = DEVICE_CLASS(klass);
53816971899SBernhard Beschow     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
53916971899SBernhard Beschow 
54016971899SBernhard Beschow     k->realize = piix4_realize;
54116971899SBernhard Beschow     k->device_id = PCI_DEVICE_ID_INTEL_82371AB_0;
54216971899SBernhard Beschow     dc->vmsd = &vmstate_piix4;
54316971899SBernhard Beschow }
54416971899SBernhard Beschow 
54516971899SBernhard Beschow static const TypeInfo piix4_info = {
54616971899SBernhard Beschow     .name          = TYPE_PIIX4_PCI_DEVICE,
5477d6f2659SBernhard Beschow     .parent        = TYPE_PIIX_PCI_DEVICE,
54816971899SBernhard Beschow     .instance_init = piix4_init,
54916971899SBernhard Beschow     .class_init    = piix4_class_init,
55016971899SBernhard Beschow };
55116971899SBernhard Beschow 
55214a026ddSPhilippe Mathieu-Daudé static void piix3_register_types(void)
55314a026ddSPhilippe Mathieu-Daudé {
5549769cfc3SBernhard Beschow     type_register_static(&piix_pci_type_info);
55514a026ddSPhilippe Mathieu-Daudé     type_register_static(&piix3_info);
55616971899SBernhard Beschow     type_register_static(&piix4_info);
55714a026ddSPhilippe Mathieu-Daudé }
55814a026ddSPhilippe Mathieu-Daudé 
55914a026ddSPhilippe Mathieu-Daudé type_init(piix3_register_types)
560