qemu/hw/isa/piix3.c
<<
>>
Prefs
   1/*
   2 * QEMU PIIX PCI ISA Bridge Emulation
   3 *
   4 * Copyright (c) 2006 Fabrice Bellard
   5 *
   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:
  12 *
  13 * The above copyright notice and this permission notice shall be included in
  14 * all copies or substantial portions of the Software.
  15 *
  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
  22 * THE SOFTWARE.
  23 */
  24
  25#include "qemu/osdep.h"
  26#include "qemu/range.h"
  27#include "qapi/error.h"
  28#include "hw/southbridge/piix.h"
  29#include "hw/irq.h"
  30#include "hw/isa/isa.h"
  31#include "hw/xen/xen.h"
  32#include "sysemu/xen.h"
  33#include "sysemu/reset.h"
  34#include "sysemu/runstate.h"
  35#include "migration/vmstate.h"
  36#include "hw/acpi/acpi_aml_interface.h"
  37
  38#define XEN_PIIX_NUM_PIRQS      128ULL
  39
  40static void piix3_set_irq_pic(PIIX3State *piix3, int pic_irq)
  41{
  42    qemu_set_irq(piix3->pic[pic_irq],
  43                 !!(piix3->pic_levels &
  44                    (((1ULL << PIIX_NUM_PIRQS) - 1) <<
  45                     (pic_irq * PIIX_NUM_PIRQS))));
  46}
  47
  48static void piix3_set_irq_level_internal(PIIX3State *piix3, int pirq, int level)
  49{
  50    int pic_irq;
  51    uint64_t mask;
  52
  53    pic_irq = piix3->dev.config[PIIX_PIRQCA + pirq];
  54    if (pic_irq >= PIIX_NUM_PIC_IRQS) {
  55        return;
  56    }
  57
  58    mask = 1ULL << ((pic_irq * PIIX_NUM_PIRQS) + pirq);
  59    piix3->pic_levels &= ~mask;
  60    piix3->pic_levels |= mask * !!level;
  61}
  62
  63static void piix3_set_irq_level(PIIX3State *piix3, int pirq, int level)
  64{
  65    int pic_irq;
  66
  67    pic_irq = piix3->dev.config[PIIX_PIRQCA + pirq];
  68    if (pic_irq >= PIIX_NUM_PIC_IRQS) {
  69        return;
  70    }
  71
  72    piix3_set_irq_level_internal(piix3, pirq, level);
  73
  74    piix3_set_irq_pic(piix3, pic_irq);
  75}
  76
  77static void piix3_set_irq(void *opaque, int pirq, int level)
  78{
  79    PIIX3State *piix3 = opaque;
  80    piix3_set_irq_level(piix3, pirq, level);
  81}
  82
  83/*
  84 * Return the global irq number corresponding to a given device irq
  85 * pin. We could also use the bus number to have a more precise mapping.
  86 */
  87static int pci_slot_get_pirq(PCIDevice *pci_dev, int pci_intx)
  88{
  89    int slot_addend;
  90    slot_addend = PCI_SLOT(pci_dev->devfn) - 1;
  91    return (pci_intx + slot_addend) & 3;
  92}
  93
  94static PCIINTxRoute piix3_route_intx_pin_to_irq(void *opaque, int pin)
  95{
  96    PIIX3State *piix3 = opaque;
  97    int irq = piix3->dev.config[PIIX_PIRQCA + pin];
  98    PCIINTxRoute route;
  99
 100    if (irq < PIIX_NUM_PIC_IRQS) {
 101        route.mode = PCI_INTX_ENABLED;
 102        route.irq = irq;
 103    } else {
 104        route.mode = PCI_INTX_DISABLED;
 105        route.irq = -1;
 106    }
 107    return route;
 108}
 109
 110/* irq routing is changed. so rebuild bitmap */
 111static void piix3_update_irq_levels(PIIX3State *piix3)
 112{
 113    PCIBus *bus = pci_get_bus(&piix3->dev);
 114    int pirq;
 115
 116    piix3->pic_levels = 0;
 117    for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
 118        piix3_set_irq_level(piix3, pirq, pci_bus_get_irq_level(bus, pirq));
 119    }
 120}
 121
 122static void piix3_write_config(PCIDevice *dev,
 123                               uint32_t address, uint32_t val, int len)
 124{
 125    pci_default_write_config(dev, address, val, len);
 126    if (ranges_overlap(address, len, PIIX_PIRQCA, 4)) {
 127        PIIX3State *piix3 = PIIX3_PCI_DEVICE(dev);
 128        int pic_irq;
 129
 130        pci_bus_fire_intx_routing_notifier(pci_get_bus(&piix3->dev));
 131        piix3_update_irq_levels(piix3);
 132        for (pic_irq = 0; pic_irq < PIIX_NUM_PIC_IRQS; pic_irq++) {
 133            piix3_set_irq_pic(piix3, pic_irq);
 134        }
 135    }
 136}
 137
 138static void piix3_write_config_xen(PCIDevice *dev,
 139                                   uint32_t address, uint32_t val, int len)
 140{
 141    int i;
 142
 143    /* Scan for updates to PCI link routes (0x60-0x63). */
 144    for (i = 0; i < len; i++) {
 145        uint8_t v = (val >> (8 * i)) & 0xff;
 146        if (v & 0x80) {
 147            v = 0;
 148        }
 149        v &= 0xf;
 150        if (((address + i) >= PIIX_PIRQCA) && ((address + i) <= PIIX_PIRQCD)) {
 151            xen_set_pci_link_route(address + i - PIIX_PIRQCA, v);
 152        }
 153    }
 154
 155    piix3_write_config(dev, address, val, len);
 156}
 157
 158static void piix3_reset(void *opaque)
 159{
 160    PIIX3State *d = opaque;
 161    uint8_t *pci_conf = d->dev.config;
 162
 163    pci_conf[0x04] = 0x07; /* master, memory and I/O */
 164    pci_conf[0x05] = 0x00;
 165    pci_conf[0x06] = 0x00;
 166    pci_conf[0x07] = 0x02; /* PCI_status_devsel_medium */
 167    pci_conf[0x4c] = 0x4d;
 168    pci_conf[0x4e] = 0x03;
 169    pci_conf[0x4f] = 0x00;
 170    pci_conf[0x60] = 0x80;
 171    pci_conf[0x61] = 0x80;
 172    pci_conf[0x62] = 0x80;
 173    pci_conf[0x63] = 0x80;
 174    pci_conf[0x69] = 0x02;
 175    pci_conf[0x70] = 0x80;
 176    pci_conf[0x76] = 0x0c;
 177    pci_conf[0x77] = 0x0c;
 178    pci_conf[0x78] = 0x02;
 179    pci_conf[0x79] = 0x00;
 180    pci_conf[0x80] = 0x00;
 181    pci_conf[0x82] = 0x00;
 182    pci_conf[0xa0] = 0x08;
 183    pci_conf[0xa2] = 0x00;
 184    pci_conf[0xa3] = 0x00;
 185    pci_conf[0xa4] = 0x00;
 186    pci_conf[0xa5] = 0x00;
 187    pci_conf[0xa6] = 0x00;
 188    pci_conf[0xa7] = 0x00;
 189    pci_conf[0xa8] = 0x0f;
 190    pci_conf[0xaa] = 0x00;
 191    pci_conf[0xab] = 0x00;
 192    pci_conf[0xac] = 0x00;
 193    pci_conf[0xae] = 0x00;
 194
 195    d->pic_levels = 0;
 196    d->rcr = 0;
 197}
 198
 199static int piix3_post_load(void *opaque, int version_id)
 200{
 201    PIIX3State *piix3 = opaque;
 202    int pirq;
 203
 204    /*
 205     * Because the i8259 has not been deserialized yet, qemu_irq_raise
 206     * might bring the system to a different state than the saved one;
 207     * for example, the interrupt could be masked but the i8259 would
 208     * not know that yet and would trigger an interrupt in the CPU.
 209     *
 210     * Here, we update irq levels without raising the interrupt.
 211     * Interrupt state will be deserialized separately through the i8259.
 212     */
 213    piix3->pic_levels = 0;
 214    for (pirq = 0; pirq < PIIX_NUM_PIRQS; pirq++) {
 215        piix3_set_irq_level_internal(piix3, pirq,
 216            pci_bus_get_irq_level(pci_get_bus(&piix3->dev), pirq));
 217    }
 218    return 0;
 219}
 220
 221static int piix3_pre_save(void *opaque)
 222{
 223    int i;
 224    PIIX3State *piix3 = opaque;
 225
 226    for (i = 0; i < ARRAY_SIZE(piix3->pci_irq_levels_vmstate); i++) {
 227        piix3->pci_irq_levels_vmstate[i] =
 228            pci_bus_get_irq_level(pci_get_bus(&piix3->dev), i);
 229    }
 230
 231    return 0;
 232}
 233
 234static bool piix3_rcr_needed(void *opaque)
 235{
 236    PIIX3State *piix3 = opaque;
 237
 238    return (piix3->rcr != 0);
 239}
 240
 241static const VMStateDescription vmstate_piix3_rcr = {
 242    .name = "PIIX3/rcr",
 243    .version_id = 1,
 244    .minimum_version_id = 1,
 245    .needed = piix3_rcr_needed,
 246    .fields = (VMStateField[]) {
 247        VMSTATE_UINT8(rcr, PIIX3State),
 248        VMSTATE_END_OF_LIST()
 249    }
 250};
 251
 252static const VMStateDescription vmstate_piix3 = {
 253    .name = "PIIX3",
 254    .version_id = 3,
 255    .minimum_version_id = 2,
 256    .post_load = piix3_post_load,
 257    .pre_save = piix3_pre_save,
 258    .fields = (VMStateField[]) {
 259        VMSTATE_PCI_DEVICE(dev, PIIX3State),
 260        VMSTATE_INT32_ARRAY_V(pci_irq_levels_vmstate, PIIX3State,
 261                              PIIX_NUM_PIRQS, 3),
 262        VMSTATE_END_OF_LIST()
 263    },
 264    .subsections = (const VMStateDescription*[]) {
 265        &vmstate_piix3_rcr,
 266        NULL
 267    }
 268};
 269
 270
 271static void rcr_write(void *opaque, hwaddr addr, uint64_t val, unsigned len)
 272{
 273    PIIX3State *d = opaque;
 274
 275    if (val & 4) {
 276        qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
 277        return;
 278    }
 279    d->rcr = val & 2; /* keep System Reset type only */
 280}
 281
 282static uint64_t rcr_read(void *opaque, hwaddr addr, unsigned len)
 283{
 284    PIIX3State *d = opaque;
 285
 286    return d->rcr;
 287}
 288
 289static const MemoryRegionOps rcr_ops = {
 290    .read = rcr_read,
 291    .write = rcr_write,
 292    .endianness = DEVICE_LITTLE_ENDIAN
 293};
 294
 295static void pci_piix3_realize(PCIDevice *dev, Error **errp)
 296{
 297    PIIX3State *d = PIIX3_PCI_DEVICE(dev);
 298
 299    if (!isa_bus_new(DEVICE(d), get_system_memory(),
 300                     pci_address_space_io(dev), errp)) {
 301        return;
 302    }
 303
 304    memory_region_init_io(&d->rcr_mem, OBJECT(dev), &rcr_ops, d,
 305                          "piix3-reset-control", 1);
 306    memory_region_add_subregion_overlap(pci_address_space_io(dev),
 307                                        PIIX_RCR_IOPORT, &d->rcr_mem, 1);
 308
 309    qemu_register_reset(piix3_reset, d);
 310}
 311
 312static void build_pci_isa_aml(AcpiDevAmlIf *adev, Aml *scope)
 313{
 314    BusChild *kid;
 315    BusState *bus = qdev_get_child_bus(DEVICE(adev), "isa.0");
 316
 317    /* PIIX PCI to ISA irq remapping */
 318    aml_append(scope, aml_operation_region("P40C", AML_PCI_CONFIG,
 319                                         aml_int(0x60), 0x04));
 320    QTAILQ_FOREACH(kid, &bus->children, sibling) {
 321        call_dev_aml_func(DEVICE(kid->child), scope);
 322    }
 323}
 324
 325static void pci_piix3_class_init(ObjectClass *klass, void *data)
 326{
 327    DeviceClass *dc = DEVICE_CLASS(klass);
 328    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 329    AcpiDevAmlIfClass *adevc = ACPI_DEV_AML_IF_CLASS(klass);
 330
 331    dc->desc        = "ISA bridge";
 332    dc->vmsd        = &vmstate_piix3;
 333    dc->hotpluggable   = false;
 334    k->vendor_id    = PCI_VENDOR_ID_INTEL;
 335    /* 82371SB PIIX3 PCI-to-ISA bridge (Step A1) */
 336    k->device_id    = PCI_DEVICE_ID_INTEL_82371SB_0;
 337    k->class_id     = PCI_CLASS_BRIDGE_ISA;
 338    /*
 339     * Reason: part of PIIX3 southbridge, needs to be wired up by
 340     * pc_piix.c's pc_init1()
 341     */
 342    dc->user_creatable = false;
 343    adevc->build_dev_aml = build_pci_isa_aml;
 344}
 345
 346static const TypeInfo piix3_pci_type_info = {
 347    .name = TYPE_PIIX3_PCI_DEVICE,
 348    .parent = TYPE_PCI_DEVICE,
 349    .instance_size = sizeof(PIIX3State),
 350    .abstract = true,
 351    .class_init = pci_piix3_class_init,
 352    .interfaces = (InterfaceInfo[]) {
 353        { INTERFACE_CONVENTIONAL_PCI_DEVICE },
 354        { TYPE_ACPI_DEV_AML_IF },
 355        { },
 356    },
 357};
 358
 359static void piix3_realize(PCIDevice *dev, Error **errp)
 360{
 361    ERRP_GUARD();
 362    PIIX3State *piix3 = PIIX3_PCI_DEVICE(dev);
 363    PCIBus *pci_bus = pci_get_bus(dev);
 364
 365    pci_piix3_realize(dev, errp);
 366    if (*errp) {
 367        return;
 368    }
 369
 370    pci_bus_irqs(pci_bus, piix3_set_irq, pci_slot_get_pirq,
 371                 piix3, PIIX_NUM_PIRQS);
 372    pci_bus_set_route_irq_fn(pci_bus, piix3_route_intx_pin_to_irq);
 373};
 374
 375static void piix3_class_init(ObjectClass *klass, void *data)
 376{
 377    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 378
 379    k->config_write = piix3_write_config;
 380    k->realize = piix3_realize;
 381}
 382
 383static const TypeInfo piix3_info = {
 384    .name          = TYPE_PIIX3_DEVICE,
 385    .parent        = TYPE_PIIX3_PCI_DEVICE,
 386    .class_init    = piix3_class_init,
 387};
 388
 389static void piix3_xen_realize(PCIDevice *dev, Error **errp)
 390{
 391    ERRP_GUARD();
 392    PIIX3State *piix3 = PIIX3_PCI_DEVICE(dev);
 393    PCIBus *pci_bus = pci_get_bus(dev);
 394
 395    pci_piix3_realize(dev, errp);
 396    if (*errp) {
 397        return;
 398    }
 399
 400    /*
 401     * Xen supports additional interrupt routes from the PCI devices to
 402     * the IOAPIC: the four pins of each PCI device on the bus are also
 403     * connected to the IOAPIC directly.
 404     * These additional routes can be discovered through ACPI.
 405     */
 406    pci_bus_irqs(pci_bus, xen_piix3_set_irq, xen_pci_slot_get_pirq,
 407                 piix3, XEN_PIIX_NUM_PIRQS);
 408};
 409
 410static void piix3_xen_class_init(ObjectClass *klass, void *data)
 411{
 412    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
 413
 414    k->config_write = piix3_write_config_xen;
 415    k->realize = piix3_xen_realize;
 416};
 417
 418static const TypeInfo piix3_xen_info = {
 419    .name          = TYPE_PIIX3_XEN_DEVICE,
 420    .parent        = TYPE_PIIX3_PCI_DEVICE,
 421    .class_init    = piix3_xen_class_init,
 422};
 423
 424static void piix3_register_types(void)
 425{
 426    type_register_static(&piix3_pci_type_info);
 427    type_register_static(&piix3_info);
 428    type_register_static(&piix3_xen_info);
 429}
 430
 431type_init(piix3_register_types)
 432