xref: /openbmc/qemu/hw/isa/piix.c (revision 28ae3179fc52d2e4d870b635c4a412aab99759e7)
116971899SBernhard Beschow /*
216971899SBernhard Beschow  * QEMU PIIX PCI ISA Bridge Emulation
316971899SBernhard Beschow  *
416971899SBernhard Beschow  * Copyright (c) 2006 Fabrice Bellard
516971899SBernhard Beschow  * Copyright (c) 2018 Hervé Poussineau
616971899SBernhard Beschow  *
716971899SBernhard Beschow  * Permission is hereby granted, free of charge, to any person obtaining a copy
816971899SBernhard Beschow  * of this software and associated documentation files (the "Software"), to deal
916971899SBernhard Beschow  * in the Software without restriction, including without limitation the rights
1016971899SBernhard Beschow  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1116971899SBernhard Beschow  * copies of the Software, and to permit persons to whom the Software is
1216971899SBernhard Beschow  * furnished to do so, subject to the following conditions:
1316971899SBernhard Beschow  *
1416971899SBernhard Beschow  * The above copyright notice and this permission notice shall be included in
1516971899SBernhard Beschow  * all copies or substantial portions of the Software.
1616971899SBernhard Beschow  *
1716971899SBernhard Beschow  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1816971899SBernhard Beschow  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1916971899SBernhard Beschow  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2016971899SBernhard Beschow  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2116971899SBernhard Beschow  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2216971899SBernhard Beschow  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2316971899SBernhard Beschow  * THE SOFTWARE.
2416971899SBernhard Beschow  */
2516971899SBernhard Beschow 
2616971899SBernhard Beschow #include "qemu/osdep.h"
2716971899SBernhard Beschow #include "qemu/range.h"
2816971899SBernhard Beschow #include "qapi/error.h"
2916971899SBernhard Beschow #include "hw/dma/i8257.h"
3016971899SBernhard Beschow #include "hw/southbridge/piix.h"
3116971899SBernhard Beschow #include "hw/timer/i8254.h"
3216971899SBernhard Beschow #include "hw/irq.h"
3316971899SBernhard Beschow #include "hw/qdev-properties.h"
3416971899SBernhard Beschow #include "hw/ide/piix.h"
3516971899SBernhard Beschow #include "hw/intc/i8259.h"
3616971899SBernhard Beschow #include "hw/isa/isa.h"
3716971899SBernhard Beschow #include "sysemu/runstate.h"
3816971899SBernhard Beschow #include "migration/vmstate.h"
3916971899SBernhard Beschow #include "hw/acpi/acpi_aml_interface.h"
4016971899SBernhard Beschow 
piix_set_irq_pic(PIIXState * s,int pic_irq)412a62c479SBernhard Beschow static void piix_set_irq_pic(PIIXState *s, int pic_irq)
4216971899SBernhard Beschow {
432a62c479SBernhard Beschow     qemu_set_irq(s->isa_irqs_in[pic_irq],
442a62c479SBernhard Beschow                  !!(s->pic_levels &
4516971899SBernhard Beschow                     (((1ULL << PIIX_NUM_PIRQS) - 1) <<
4616971899SBernhard Beschow                      (pic_irq * PIIX_NUM_PIRQS))));
4716971899SBernhard Beschow }
4816971899SBernhard Beschow 
piix_set_pci_irq_level_internal(PIIXState * s,int pirq,int level)492a62c479SBernhard Beschow static void piix_set_pci_irq_level_internal(PIIXState *s, int pirq, int level)
5016971899SBernhard Beschow {
5116971899SBernhard Beschow     int pic_irq;
5216971899SBernhard Beschow     uint64_t mask;
5316971899SBernhard Beschow 
542a62c479SBernhard Beschow     pic_irq = s->dev.config[PIIX_PIRQCA + pirq];
5516971899SBernhard Beschow     if (pic_irq >= ISA_NUM_IRQS) {
5616971899SBernhard Beschow         return;
5716971899SBernhard Beschow     }
5816971899SBernhard Beschow 
5916971899SBernhard Beschow     mask = 1ULL << ((pic_irq * PIIX_NUM_PIRQS) + pirq);
602a62c479SBernhard Beschow     s->pic_levels &= ~mask;
612a62c479SBernhard Beschow     s->pic_levels |= mask * !!level;
6216971899SBernhard Beschow }
6316971899SBernhard Beschow 
piix_set_pci_irq_level(PIIXState * s,int pirq,int level)642a62c479SBernhard Beschow static void piix_set_pci_irq_level(PIIXState *s, int pirq, int level)
6516971899SBernhard Beschow {
6616971899SBernhard Beschow     int pic_irq;
6716971899SBernhard Beschow 
682a62c479SBernhard Beschow     pic_irq = s->dev.config[PIIX_PIRQCA + pirq];
6916971899SBernhard Beschow     if (pic_irq >= ISA_NUM_IRQS) {
7016971899SBernhard Beschow         return;
7116971899SBernhard Beschow     }
7216971899SBernhard Beschow 
732a62c479SBernhard Beschow     piix_set_pci_irq_level_internal(s, pirq, level);
7416971899SBernhard Beschow 
752a62c479SBernhard Beschow     piix_set_irq_pic(s, pic_irq);
7616971899SBernhard Beschow }
7716971899SBernhard Beschow 
piix_set_pci_irq(void * opaque,int pirq,int level)782a62c479SBernhard Beschow static void piix_set_pci_irq(void *opaque, int pirq, int level)
7916971899SBernhard Beschow {
802a62c479SBernhard Beschow     PIIXState *s = opaque;
812a62c479SBernhard Beschow     piix_set_pci_irq_level(s, pirq, level);
8216971899SBernhard Beschow }
8316971899SBernhard Beschow 
piix_request_i8259_irq(void * opaque,int irq,int level)842d7630f5SBernhard Beschow static void piix_request_i8259_irq(void *opaque, int irq, int level)
8516971899SBernhard Beschow {
867d6f2659SBernhard Beschow     PIIXState *s = opaque;
8716971899SBernhard Beschow     qemu_set_irq(s->cpu_intr, level);
8816971899SBernhard Beschow }
8916971899SBernhard Beschow 
piix_route_intx_pin_to_irq(void * opaque,int pin)902a62c479SBernhard Beschow static PCIINTxRoute piix_route_intx_pin_to_irq(void *opaque, int pin)
9116971899SBernhard Beschow {
922a62c479SBernhard Beschow     PCIDevice *pci_dev = opaque;
932a62c479SBernhard Beschow     int irq = pci_dev->config[PIIX_PIRQCA + pin];
9416971899SBernhard Beschow     PCIINTxRoute route;
9516971899SBernhard Beschow 
9616971899SBernhard Beschow     if (irq < ISA_NUM_IRQS) {
9716971899SBernhard Beschow         route.mode = PCI_INTX_ENABLED;
9816971899SBernhard Beschow         route.irq = irq;
9916971899SBernhard Beschow     } else {
10016971899SBernhard Beschow         route.mode = PCI_INTX_DISABLED;
10116971899SBernhard Beschow         route.irq = -1;
10216971899SBernhard Beschow     }
10316971899SBernhard Beschow     return route;
10416971899SBernhard Beschow }
10516971899SBernhard Beschow 
10616971899SBernhard Beschow /* irq routing is changed. so rebuild bitmap */
piix_update_pci_irq_levels(PIIXState * s)1072a62c479SBernhard Beschow static void piix_update_pci_irq_levels(PIIXState *s)
10816971899SBernhard Beschow {
1092a62c479SBernhard Beschow     PCIBus *bus = pci_get_bus(&s->dev);
11016971899SBernhard Beschow     int pirq;
11116971899SBernhard Beschow 
1122a62c479SBernhard Beschow     s->pic_levels = 0;
11316971899SBernhard Beschow     for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
1142a62c479SBernhard Beschow         piix_set_pci_irq_level(s, pirq, pci_bus_get_irq_level(bus, pirq));
11516971899SBernhard Beschow     }
11616971899SBernhard Beschow }
11716971899SBernhard Beschow 
piix_write_config(PCIDevice * dev,uint32_t address,uint32_t val,int len)1182a62c479SBernhard Beschow static void piix_write_config(PCIDevice *dev, uint32_t address, uint32_t val,
1192a62c479SBernhard Beschow                               int len)
12016971899SBernhard Beschow {
12116971899SBernhard Beschow     pci_default_write_config(dev, address, val, len);
12216971899SBernhard Beschow     if (ranges_overlap(address, len, PIIX_PIRQCA, 4)) {
1232a62c479SBernhard Beschow         PIIXState *s = PIIX_PCI_DEVICE(dev);
12416971899SBernhard Beschow         int pic_irq;
12516971899SBernhard Beschow 
1262a62c479SBernhard Beschow         pci_bus_fire_intx_routing_notifier(pci_get_bus(&s->dev));
1272a62c479SBernhard Beschow         piix_update_pci_irq_levels(s);
12816971899SBernhard Beschow         for (pic_irq = 0; pic_irq < ISA_NUM_IRQS; pic_irq++) {
1292a62c479SBernhard Beschow             piix_set_irq_pic(s, pic_irq);
13016971899SBernhard Beschow         }
13116971899SBernhard Beschow     }
13216971899SBernhard Beschow }
13316971899SBernhard Beschow 
piix_reset(DeviceState * dev)1347d6f2659SBernhard Beschow static void piix_reset(DeviceState *dev)
13516971899SBernhard Beschow {
1367d6f2659SBernhard Beschow     PIIXState *d = PIIX_PCI_DEVICE(dev);
13716971899SBernhard Beschow     uint8_t *pci_conf = d->dev.config;
13816971899SBernhard Beschow 
13916971899SBernhard Beschow     pci_conf[0x04] = 0x07; /* master, memory and I/O */
14016971899SBernhard Beschow     pci_conf[0x05] = 0x00;
14116971899SBernhard Beschow     pci_conf[0x06] = 0x00;
14216971899SBernhard Beschow     pci_conf[0x07] = 0x02; /* PCI_status_devsel_medium */
14316971899SBernhard Beschow     pci_conf[0x4c] = 0x4d;
14416971899SBernhard Beschow     pci_conf[0x4e] = 0x03;
14516971899SBernhard Beschow     pci_conf[0x4f] = 0x00;
14616971899SBernhard Beschow     pci_conf[0x60] = 0x80;
14716971899SBernhard Beschow     pci_conf[0x61] = 0x80;
14816971899SBernhard Beschow     pci_conf[0x62] = 0x80;
14916971899SBernhard Beschow     pci_conf[0x63] = 0x80;
15016971899SBernhard Beschow     pci_conf[0x69] = 0x02;
15116971899SBernhard Beschow     pci_conf[0x70] = 0x80;
15216971899SBernhard Beschow     pci_conf[0x76] = 0x0c;
15316971899SBernhard Beschow     pci_conf[0x77] = 0x0c;
15416971899SBernhard Beschow     pci_conf[0x78] = 0x02;
15516971899SBernhard Beschow     pci_conf[0x79] = 0x00;
15616971899SBernhard Beschow     pci_conf[0x80] = 0x00;
15716971899SBernhard Beschow     pci_conf[0x82] = 0x00;
15816971899SBernhard Beschow     pci_conf[0xa0] = 0x08;
15916971899SBernhard Beschow     pci_conf[0xa2] = 0x00;
16016971899SBernhard Beschow     pci_conf[0xa3] = 0x00;
16116971899SBernhard Beschow     pci_conf[0xa4] = 0x00;
16216971899SBernhard Beschow     pci_conf[0xa5] = 0x00;
16316971899SBernhard Beschow     pci_conf[0xa6] = 0x00;
16416971899SBernhard Beschow     pci_conf[0xa7] = 0x00;
16516971899SBernhard Beschow     pci_conf[0xa8] = 0x0f;
16616971899SBernhard Beschow     pci_conf[0xaa] = 0x00;
16716971899SBernhard Beschow     pci_conf[0xab] = 0x00;
16816971899SBernhard Beschow     pci_conf[0xac] = 0x00;
16916971899SBernhard Beschow     pci_conf[0xae] = 0x00;
17016971899SBernhard Beschow 
17116971899SBernhard Beschow     d->pic_levels = 0;
17216971899SBernhard Beschow     d->rcr = 0;
17316971899SBernhard Beschow }
17416971899SBernhard Beschow 
piix_post_load(void * opaque,int version_id)1752a62c479SBernhard Beschow static int piix_post_load(void *opaque, int version_id)
17616971899SBernhard Beschow {
1772a62c479SBernhard Beschow     PIIXState *s = opaque;
17816971899SBernhard Beschow     int pirq;
17916971899SBernhard Beschow 
18016971899SBernhard Beschow     /*
18116971899SBernhard Beschow      * Because the i8259 has not been deserialized yet, qemu_irq_raise
18216971899SBernhard Beschow      * might bring the system to a different state than the saved one;
18316971899SBernhard Beschow      * for example, the interrupt could be masked but the i8259 would
18416971899SBernhard Beschow      * not know that yet and would trigger an interrupt in the CPU.
18516971899SBernhard Beschow      *
18616971899SBernhard Beschow      * Here, we update irq levels without raising the interrupt.
18716971899SBernhard Beschow      * Interrupt state will be deserialized separately through the i8259.
18816971899SBernhard Beschow      */
1892a62c479SBernhard Beschow     s->pic_levels = 0;
19016971899SBernhard Beschow     for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
1912a62c479SBernhard Beschow         piix_set_pci_irq_level_internal(s, pirq,
1922a62c479SBernhard Beschow             pci_bus_get_irq_level(pci_get_bus(&s->dev), pirq));
19316971899SBernhard Beschow     }
19416971899SBernhard Beschow     return 0;
19516971899SBernhard Beschow }
19616971899SBernhard Beschow 
piix4_post_load(void * opaque,int version_id)19716971899SBernhard Beschow static int piix4_post_load(void *opaque, int version_id)
19816971899SBernhard Beschow {
1997d6f2659SBernhard Beschow     PIIXState *s = opaque;
20016971899SBernhard Beschow 
20116971899SBernhard Beschow     if (version_id == 2) {
20216971899SBernhard Beschow         s->rcr = 0;
20316971899SBernhard Beschow     }
20416971899SBernhard Beschow 
2050c9fd5a3SBernhard Beschow     return piix_post_load(opaque, version_id);
20616971899SBernhard Beschow }
20716971899SBernhard Beschow 
piix3_pre_save(void * opaque)20816971899SBernhard Beschow static int piix3_pre_save(void *opaque)
20916971899SBernhard Beschow {
21016971899SBernhard Beschow     int i;
21116971899SBernhard Beschow     PIIXState *piix3 = opaque;
21216971899SBernhard Beschow 
21316971899SBernhard Beschow     for (i = 0; i < ARRAY_SIZE(piix3->pci_irq_levels_vmstate); i++) {
21416971899SBernhard Beschow         piix3->pci_irq_levels_vmstate[i] =
21516971899SBernhard Beschow             pci_bus_get_irq_level(pci_get_bus(&piix3->dev), i);
21616971899SBernhard Beschow     }
21716971899SBernhard Beschow 
21816971899SBernhard Beschow     return 0;
21916971899SBernhard Beschow }
22016971899SBernhard Beschow 
piix3_rcr_needed(void * opaque)22116971899SBernhard Beschow static bool piix3_rcr_needed(void *opaque)
22216971899SBernhard Beschow {
22316971899SBernhard Beschow     PIIXState *piix3 = opaque;
22416971899SBernhard Beschow 
22516971899SBernhard Beschow     return (piix3->rcr != 0);
22616971899SBernhard Beschow }
22716971899SBernhard Beschow 
22816971899SBernhard Beschow static const VMStateDescription vmstate_piix3_rcr = {
22916971899SBernhard Beschow     .name = "PIIX3/rcr",
23016971899SBernhard Beschow     .version_id = 1,
23116971899SBernhard Beschow     .minimum_version_id = 1,
23216971899SBernhard Beschow     .needed = piix3_rcr_needed,
233cbf19506SRichard Henderson     .fields = (const VMStateField[]) {
23416971899SBernhard Beschow         VMSTATE_UINT8(rcr, PIIXState),
23516971899SBernhard Beschow         VMSTATE_END_OF_LIST()
23616971899SBernhard Beschow     }
23716971899SBernhard Beschow };
23816971899SBernhard Beschow 
23916971899SBernhard Beschow static const VMStateDescription vmstate_piix3 = {
24016971899SBernhard Beschow     .name = "PIIX3",
24116971899SBernhard Beschow     .version_id = 3,
24216971899SBernhard Beschow     .minimum_version_id = 2,
2432a62c479SBernhard Beschow     .post_load = piix_post_load,
24416971899SBernhard Beschow     .pre_save = piix3_pre_save,
245cbf19506SRichard Henderson     .fields = (const VMStateField[]) {
24616971899SBernhard Beschow         VMSTATE_PCI_DEVICE(dev, PIIXState),
24716971899SBernhard Beschow         VMSTATE_INT32_ARRAY_V(pci_irq_levels_vmstate, PIIXState,
24816971899SBernhard Beschow                               PIIX_NUM_PIRQS, 3),
24916971899SBernhard Beschow         VMSTATE_END_OF_LIST()
25016971899SBernhard Beschow     },
251cbf19506SRichard Henderson     .subsections = (const VMStateDescription * const []) {
25216971899SBernhard Beschow         &vmstate_piix3_rcr,
25316971899SBernhard Beschow         NULL
25416971899SBernhard Beschow     }
25516971899SBernhard Beschow };
25616971899SBernhard Beschow 
25716971899SBernhard Beschow static const VMStateDescription vmstate_piix4 = {
25816971899SBernhard Beschow     .name = "PIIX4",
25916971899SBernhard Beschow     .version_id = 3,
26016971899SBernhard Beschow     .minimum_version_id = 2,
26116971899SBernhard Beschow     .post_load = piix4_post_load,
262cbf19506SRichard Henderson     .fields = (const VMStateField[]) {
2637d6f2659SBernhard Beschow         VMSTATE_PCI_DEVICE(dev, PIIXState),
2647d6f2659SBernhard Beschow         VMSTATE_UINT8_V(rcr, PIIXState, 3),
26516971899SBernhard Beschow         VMSTATE_END_OF_LIST()
26616971899SBernhard Beschow     }
26716971899SBernhard Beschow };
26816971899SBernhard Beschow 
rcr_write(void * opaque,hwaddr addr,uint64_t val,unsigned len)26916971899SBernhard Beschow static void rcr_write(void *opaque, hwaddr addr, uint64_t val, unsigned len)
27016971899SBernhard Beschow {
27116971899SBernhard Beschow     PIIXState *d = opaque;
27216971899SBernhard Beschow 
27316971899SBernhard Beschow     if (val & 4) {
27416971899SBernhard Beschow         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
27516971899SBernhard Beschow         return;
27616971899SBernhard Beschow     }
27716971899SBernhard Beschow     d->rcr = val & 2; /* keep System Reset type only */
27816971899SBernhard Beschow }
27916971899SBernhard Beschow 
rcr_read(void * opaque,hwaddr addr,unsigned len)28016971899SBernhard Beschow static uint64_t rcr_read(void *opaque, hwaddr addr, unsigned len)
28116971899SBernhard Beschow {
28216971899SBernhard Beschow     PIIXState *d = opaque;
28316971899SBernhard Beschow 
28416971899SBernhard Beschow     return d->rcr;
28516971899SBernhard Beschow }
28616971899SBernhard Beschow 
28716971899SBernhard Beschow static const MemoryRegionOps rcr_ops = {
28816971899SBernhard Beschow     .read = rcr_read,
28916971899SBernhard Beschow     .write = rcr_write,
29016971899SBernhard Beschow     .endianness = DEVICE_LITTLE_ENDIAN,
29116971899SBernhard Beschow     .impl = {
29216971899SBernhard Beschow         .min_access_size = 1,
29316971899SBernhard Beschow         .max_access_size = 1,
29416971899SBernhard Beschow     },
29516971899SBernhard Beschow };
29616971899SBernhard Beschow 
pci_piix_realize(PCIDevice * dev,const char * uhci_type,Error ** errp)2972922dbc2SBernhard Beschow static void pci_piix_realize(PCIDevice *dev, const char *uhci_type,
2982922dbc2SBernhard Beschow                              Error **errp)
29916971899SBernhard Beschow {
30016971899SBernhard Beschow     PIIXState *d = PIIX_PCI_DEVICE(dev);
30116971899SBernhard Beschow     PCIBus *pci_bus = pci_get_bus(dev);
30216971899SBernhard Beschow     ISABus *isa_bus;
30316971899SBernhard Beschow     uint32_t irq;
30416971899SBernhard Beschow 
30516971899SBernhard Beschow     isa_bus = isa_bus_new(DEVICE(d), pci_address_space(dev),
30616971899SBernhard Beschow                           pci_address_space_io(dev), errp);
30716971899SBernhard Beschow     if (!isa_bus) {
30816971899SBernhard Beschow         return;
30916971899SBernhard Beschow     }
31016971899SBernhard Beschow 
31116971899SBernhard Beschow     memory_region_init_io(&d->rcr_mem, OBJECT(dev), &rcr_ops, d,
312f97479caSBernhard Beschow                           "piix-reset-control", 1);
31316971899SBernhard Beschow     memory_region_add_subregion_overlap(pci_address_space_io(dev),
31416971899SBernhard Beschow                                         PIIX_RCR_IOPORT, &d->rcr_mem, 1);
31516971899SBernhard Beschow 
3162d7630f5SBernhard Beschow     /* PIC */
3172d7630f5SBernhard Beschow     if (d->has_pic) {
3182d7630f5SBernhard Beschow         qemu_irq *i8259_out_irq = qemu_allocate_irqs(piix_request_i8259_irq, d,
3192d7630f5SBernhard Beschow                                                      1);
3202d7630f5SBernhard Beschow         qemu_irq *i8259 = i8259_init(isa_bus, *i8259_out_irq);
3212d7630f5SBernhard Beschow         size_t i;
3222d7630f5SBernhard Beschow 
3232d7630f5SBernhard Beschow         for (i = 0; i < ISA_NUM_IRQS; i++) {
3242d7630f5SBernhard Beschow             d->isa_irqs_in[i] = i8259[i];
3252d7630f5SBernhard Beschow         }
3262d7630f5SBernhard Beschow 
3272d7630f5SBernhard Beschow         g_free(i8259);
3282d7630f5SBernhard Beschow 
3292d7630f5SBernhard Beschow         qdev_init_gpio_out_named(DEVICE(dev), &d->cpu_intr, "intr", 1);
3302d7630f5SBernhard Beschow     }
3312d7630f5SBernhard Beschow 
33216971899SBernhard Beschow     isa_bus_register_input_irqs(isa_bus, d->isa_irqs_in);
33316971899SBernhard Beschow 
334ac433035SBernhard Beschow     /* PIT */
335ac433035SBernhard Beschow     if (d->has_pit) {
336ac433035SBernhard Beschow         i8254_pit_init(isa_bus, 0x40, 0, NULL);
337ac433035SBernhard Beschow     }
338ac433035SBernhard Beschow 
3395e37bc49SPhilippe Mathieu-Daudé     i8257_dma_init(OBJECT(dev), isa_bus, 0);
34016971899SBernhard Beschow 
34116971899SBernhard Beschow     /* RTC */
34216971899SBernhard Beschow     qdev_prop_set_int32(DEVICE(&d->rtc), "base_year", 2000);
34316971899SBernhard Beschow     if (!qdev_realize(DEVICE(&d->rtc), BUS(isa_bus), errp)) {
34416971899SBernhard Beschow         return;
34516971899SBernhard Beschow     }
34616971899SBernhard Beschow     irq = object_property_get_uint(OBJECT(&d->rtc), "irq", &error_fatal);
34716971899SBernhard Beschow     isa_connect_gpio_out(ISA_DEVICE(&d->rtc), 0, irq);
34816971899SBernhard Beschow 
34916971899SBernhard Beschow     /* IDE */
35016971899SBernhard Beschow     qdev_prop_set_int32(DEVICE(&d->ide), "addr", dev->devfn + 1);
35116971899SBernhard Beschow     if (!qdev_realize(DEVICE(&d->ide), BUS(pci_bus), errp)) {
35216971899SBernhard Beschow         return;
35316971899SBernhard Beschow     }
35416971899SBernhard Beschow 
35516971899SBernhard Beschow     /* USB */
35616971899SBernhard Beschow     if (d->has_usb) {
3572922dbc2SBernhard Beschow         object_initialize_child(OBJECT(dev), "uhci", &d->uhci, uhci_type);
35816971899SBernhard Beschow         qdev_prop_set_int32(DEVICE(&d->uhci), "addr", dev->devfn + 2);
35916971899SBernhard Beschow         if (!qdev_realize(DEVICE(&d->uhci), BUS(pci_bus), errp)) {
36016971899SBernhard Beschow             return;
36116971899SBernhard Beschow         }
36216971899SBernhard Beschow     }
36316971899SBernhard Beschow 
36416971899SBernhard Beschow     /* Power Management */
36516971899SBernhard Beschow     if (d->has_acpi) {
36616971899SBernhard Beschow         object_initialize_child(OBJECT(d), "pm", &d->pm, TYPE_PIIX4_PM);
36716971899SBernhard Beschow         qdev_prop_set_int32(DEVICE(&d->pm), "addr", dev->devfn + 3);
36816971899SBernhard Beschow         qdev_prop_set_uint32(DEVICE(&d->pm), "smb_io_base", d->smb_io_base);
36916971899SBernhard Beschow         qdev_prop_set_bit(DEVICE(&d->pm), "smm-enabled", d->smm_enabled);
37016971899SBernhard Beschow         if (!qdev_realize(DEVICE(&d->pm), BUS(pci_bus), errp)) {
37116971899SBernhard Beschow             return;
37216971899SBernhard Beschow         }
37316971899SBernhard Beschow         qdev_connect_gpio_out(DEVICE(&d->pm), 0, d->isa_irqs_in[9]);
37416971899SBernhard Beschow     }
375a203cc53SBernhard Beschow 
376a203cc53SBernhard Beschow     pci_bus_irqs(pci_bus, piix_set_pci_irq, d, PIIX_NUM_PIRQS);
37712cecd45SBernhard Beschow     pci_bus_set_route_irq_fn(pci_bus, piix_route_intx_pin_to_irq);
37816971899SBernhard Beschow }
37916971899SBernhard Beschow 
build_pci_isa_aml(AcpiDevAmlIf * adev,Aml * scope)38016971899SBernhard Beschow static void build_pci_isa_aml(AcpiDevAmlIf *adev, Aml *scope)
38116971899SBernhard Beschow {
38216971899SBernhard Beschow     Aml *field;
38316971899SBernhard Beschow     Aml *sb_scope = aml_scope("\\_SB");
38416971899SBernhard Beschow     BusState *bus = qdev_get_child_bus(DEVICE(adev), "isa.0");
38516971899SBernhard Beschow 
38616971899SBernhard Beschow     /* PIIX PCI to ISA irq remapping */
38716971899SBernhard Beschow     aml_append(scope, aml_operation_region("P40C", AML_PCI_CONFIG,
38816971899SBernhard Beschow                                            aml_int(0x60), 0x04));
38916971899SBernhard Beschow     /* Fields declarion has to happen *after* operation region */
39016971899SBernhard Beschow     field = aml_field("PCI0.S08.P40C", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
39116971899SBernhard Beschow     aml_append(field, aml_named_field("PRQ0", 8));
39216971899SBernhard Beschow     aml_append(field, aml_named_field("PRQ1", 8));
39316971899SBernhard Beschow     aml_append(field, aml_named_field("PRQ2", 8));
39416971899SBernhard Beschow     aml_append(field, aml_named_field("PRQ3", 8));
39516971899SBernhard Beschow     aml_append(sb_scope, field);
39616971899SBernhard Beschow     aml_append(scope, sb_scope);
39716971899SBernhard Beschow 
39816971899SBernhard Beschow     qbus_build_aml(bus, scope);
39916971899SBernhard Beschow }
40016971899SBernhard Beschow 
pci_piix_init(Object * obj)4017d6f2659SBernhard Beschow static void pci_piix_init(Object *obj)
40216971899SBernhard Beschow {
40316971899SBernhard Beschow     PIIXState *d = PIIX_PCI_DEVICE(obj);
40416971899SBernhard Beschow 
40516971899SBernhard Beschow     qdev_init_gpio_out_named(DEVICE(obj), d->isa_irqs_in, "isa-irqs",
40616971899SBernhard Beschow                              ISA_NUM_IRQS);
40716971899SBernhard Beschow 
40816971899SBernhard Beschow     object_initialize_child(obj, "rtc", &d->rtc, TYPE_MC146818_RTC);
40916971899SBernhard Beschow }
41016971899SBernhard Beschow 
4112922dbc2SBernhard Beschow static Property pci_piix_props[] = {
41216971899SBernhard Beschow     DEFINE_PROP_UINT32("smb_io_base", PIIXState, smb_io_base, 0),
41316971899SBernhard Beschow     DEFINE_PROP_BOOL("has-acpi", PIIXState, has_acpi, true),
4142d7630f5SBernhard Beschow     DEFINE_PROP_BOOL("has-pic", PIIXState, has_pic, true),
415ac433035SBernhard Beschow     DEFINE_PROP_BOOL("has-pit", PIIXState, has_pit, true),
41616971899SBernhard Beschow     DEFINE_PROP_BOOL("has-usb", PIIXState, has_usb, true),
41716971899SBernhard Beschow     DEFINE_PROP_BOOL("smm-enabled", PIIXState, smm_enabled, false),
41816971899SBernhard Beschow     DEFINE_PROP_END_OF_LIST(),
41916971899SBernhard Beschow };
42016971899SBernhard Beschow 
pci_piix_class_init(ObjectClass * klass,void * data)4217d6f2659SBernhard Beschow static void pci_piix_class_init(ObjectClass *klass, void *data)
42216971899SBernhard Beschow {
42316971899SBernhard Beschow     DeviceClass *dc = DEVICE_CLASS(klass);
42416971899SBernhard Beschow     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
42516971899SBernhard Beschow     AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass);
42616971899SBernhard Beschow 
4270c9fd5a3SBernhard Beschow     k->config_write = piix_write_config;
428*e3d08143SPeter Maydell     device_class_set_legacy_reset(dc, piix_reset);
42916971899SBernhard Beschow     dc->desc        = "ISA bridge";
43016971899SBernhard Beschow     dc->hotpluggable   = false;
43116971899SBernhard Beschow     k->vendor_id    = PCI_VENDOR_ID_INTEL;
43216971899SBernhard Beschow     k->class_id     = PCI_CLASS_BRIDGE_ISA;
43316971899SBernhard Beschow     /*
4347d6f2659SBernhard Beschow      * Reason: part of PIIX southbridge, needs to be wired up by e.g.
43516971899SBernhard Beschow      * pc_piix.c's pc_init1()
43616971899SBernhard Beschow      */
43716971899SBernhard Beschow     dc->user_creatable = false;
4382922dbc2SBernhard Beschow     device_class_set_props(dc, pci_piix_props);
43916971899SBernhard Beschow     adevc->build_dev_aml = build_pci_isa_aml;
44016971899SBernhard Beschow }
44116971899SBernhard Beschow 
44216971899SBernhard Beschow static const TypeInfo piix_pci_type_info = {
44316971899SBernhard Beschow     .name = TYPE_PIIX_PCI_DEVICE,
44416971899SBernhard Beschow     .parent = TYPE_PCI_DEVICE,
44516971899SBernhard Beschow     .instance_size = sizeof(PIIXState),
4467d6f2659SBernhard Beschow     .instance_init = pci_piix_init,
44716971899SBernhard Beschow     .abstract = true,
4487d6f2659SBernhard Beschow     .class_init = pci_piix_class_init,
44916971899SBernhard Beschow     .interfaces = (InterfaceInfo[]) {
45016971899SBernhard Beschow         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
45116971899SBernhard Beschow         { TYPE_ACPI_DEV_AML_IF },
45216971899SBernhard Beschow         { },
45316971899SBernhard Beschow     },
45416971899SBernhard Beschow };
45516971899SBernhard Beschow 
piix3_realize(PCIDevice * dev,Error ** errp)45616971899SBernhard Beschow static void piix3_realize(PCIDevice *dev, Error **errp)
45716971899SBernhard Beschow {
4582922dbc2SBernhard Beschow     pci_piix_realize(dev, TYPE_PIIX3_USB_UHCI, errp);
45916971899SBernhard Beschow }
46016971899SBernhard Beschow 
piix3_init(Object * obj)4617d6f2659SBernhard Beschow static void piix3_init(Object *obj)
4627d6f2659SBernhard Beschow {
4637d6f2659SBernhard Beschow     PIIXState *d = PIIX_PCI_DEVICE(obj);
4647d6f2659SBernhard Beschow 
4657d6f2659SBernhard Beschow     object_initialize_child(obj, "ide", &d->ide, TYPE_PIIX3_IDE);
4667d6f2659SBernhard Beschow }
4677d6f2659SBernhard Beschow 
piix3_class_init(ObjectClass * klass,void * data)46816971899SBernhard Beschow static void piix3_class_init(ObjectClass *klass, void *data)
46916971899SBernhard Beschow {
4707d6f2659SBernhard Beschow     DeviceClass *dc = DEVICE_CLASS(klass);
47116971899SBernhard Beschow     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
47216971899SBernhard Beschow 
47316971899SBernhard Beschow     k->realize = piix3_realize;
4747d6f2659SBernhard Beschow     /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
4757d6f2659SBernhard Beschow     k->device_id = PCI_DEVICE_ID_INTEL_82371SB_0;
4767d6f2659SBernhard Beschow     dc->vmsd = &vmstate_piix3;
47716971899SBernhard Beschow }
47816971899SBernhard Beschow 
47916971899SBernhard Beschow static const TypeInfo piix3_info = {
48016971899SBernhard Beschow     .name          = TYPE_PIIX3_DEVICE,
48116971899SBernhard Beschow     .parent        = TYPE_PIIX_PCI_DEVICE,
4827d6f2659SBernhard Beschow     .instance_init = piix3_init,
48316971899SBernhard Beschow     .class_init    = piix3_class_init,
48416971899SBernhard Beschow };
48516971899SBernhard Beschow 
piix4_realize(PCIDevice * dev,Error ** errp)48616971899SBernhard Beschow static void piix4_realize(PCIDevice *dev, Error **errp)
48716971899SBernhard Beschow {
4882922dbc2SBernhard Beschow     pci_piix_realize(dev, TYPE_PIIX4_USB_UHCI, errp);
48916971899SBernhard Beschow }
49016971899SBernhard Beschow 
piix4_init(Object * obj)49116971899SBernhard Beschow static void piix4_init(Object *obj)
49216971899SBernhard Beschow {
4937d6f2659SBernhard Beschow     PIIXState *s = PIIX_PCI_DEVICE(obj);
49416971899SBernhard Beschow 
49516971899SBernhard Beschow     object_initialize_child(obj, "ide", &s->ide, TYPE_PIIX4_IDE);
49616971899SBernhard Beschow }
49716971899SBernhard Beschow 
piix4_class_init(ObjectClass * klass,void * data)49816971899SBernhard Beschow static void piix4_class_init(ObjectClass *klass, void *data)
49916971899SBernhard Beschow {
50016971899SBernhard Beschow     DeviceClass *dc = DEVICE_CLASS(klass);
50116971899SBernhard Beschow     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
50216971899SBernhard Beschow 
50316971899SBernhard Beschow     k->realize = piix4_realize;
50416971899SBernhard Beschow     k->device_id = PCI_DEVICE_ID_INTEL_82371AB_0;
50516971899SBernhard Beschow     dc->vmsd = &vmstate_piix4;
50616971899SBernhard Beschow }
50716971899SBernhard Beschow 
50816971899SBernhard Beschow static const TypeInfo piix4_info = {
50916971899SBernhard Beschow     .name          = TYPE_PIIX4_PCI_DEVICE,
5107d6f2659SBernhard Beschow     .parent        = TYPE_PIIX_PCI_DEVICE,
51116971899SBernhard Beschow     .instance_init = piix4_init,
51216971899SBernhard Beschow     .class_init    = piix4_class_init,
51316971899SBernhard Beschow };
51416971899SBernhard Beschow 
piix3_register_types(void)51516971899SBernhard Beschow static void piix3_register_types(void)
51616971899SBernhard Beschow {
51716971899SBernhard Beschow     type_register_static(&piix_pci_type_info);
51816971899SBernhard Beschow     type_register_static(&piix3_info);
51916971899SBernhard Beschow     type_register_static(&piix4_info);
52016971899SBernhard Beschow }
52116971899SBernhard Beschow 
52216971899SBernhard Beschow type_init(piix3_register_types)
523