Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * VT82C686B south bridge support |
| 3 | * |
| 4 | * Copyright (c) 2008 yajin (yajin@vm-kernel.org) |
| 5 | * Copyright (c) 2009 chenming (chenming@rdc.faw.com.cn) |
| 6 | * Copyright (c) 2010 Huacai Chen (zltjiangshi@gmail.com) |
| 7 | * This code is licensed under the GNU GPL v2. |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 8 | * |
| 9 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 10 | * GNU GPL, version 2 or (at your option) any later version. |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 11 | * |
| 12 | * VT8231 south bridge support and general clean up to allow it |
| 13 | * Copyright (c) 2018-2020 BALATON Zoltan |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 14 | */ |
| 15 | |
Peter Maydell | 0430891 | 2016-01-26 18:17:30 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 17 | #include "hw/isa/vt82c686.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 18 | #include "hw/pci/pci.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 19 | #include "hw/qdev-properties.h" |
Bernhard Beschow | 9eb6abb | 2022-06-13 19:24:55 +0200 | [diff] [blame] | 20 | #include "hw/ide/pci.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 21 | #include "hw/isa/isa.h" |
Philippe Mathieu-Daudé | 98cf824 | 2018-03-08 23:39:40 +0100 | [diff] [blame] | 22 | #include "hw/isa/superio.h" |
BALATON Zoltan | 3dc31cb | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 23 | #include "hw/intc/i8259.h" |
| 24 | #include "hw/irq.h" |
| 25 | #include "hw/dma/i8257.h" |
Bernhard Beschow | 1a99ddb | 2022-09-01 13:41:22 +0200 | [diff] [blame] | 26 | #include "hw/usb/hcd-uhci.h" |
BALATON Zoltan | 3dc31cb | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 27 | #include "hw/timer/i8254.h" |
| 28 | #include "hw/rtc/mc146818rtc.h" |
Markus Armbruster | d645427 | 2019-08-12 07:23:45 +0200 | [diff] [blame] | 29 | #include "migration/vmstate.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 30 | #include "hw/isa/apm.h" |
| 31 | #include "hw/acpi/acpi.h" |
| 32 | #include "hw/i2c/pm_smbus.h" |
Markus Armbruster | 9307d06 | 2020-06-10 07:32:04 +0200 | [diff] [blame] | 33 | #include "qapi/error.h" |
BALATON Zoltan | 2c4c556 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 34 | #include "qemu/log.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 35 | #include "qemu/module.h" |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 36 | #include "qemu/range.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 37 | #include "qemu/timer.h" |
BALATON Zoltan | ff413a1 | 2021-01-02 11:43:35 +0100 | [diff] [blame] | 38 | #include "trace.h" |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 39 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 40 | #define TYPE_VIA_PM "via-pm" |
| 41 | OBJECT_DECLARE_SIMPLE_TYPE(ViaPMState, VIA_PM) |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 42 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 43 | struct ViaPMState { |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 44 | PCIDevice dev; |
Gerd Hoffmann | a290282 | 2012-11-23 08:29:27 +0100 | [diff] [blame] | 45 | MemoryRegion io; |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 46 | ACPIREGS ar; |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 47 | APMState apm; |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 48 | PMSMBus smb; |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 49 | }; |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 50 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 51 | static void pm_io_space_update(ViaPMState *s) |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 52 | { |
BALATON Zoltan | 3ab1eea | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 53 | uint32_t pmbase = pci_get_long(s->dev.config + 0x48) & 0xff80UL; |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 54 | |
Gerd Hoffmann | a290282 | 2012-11-23 08:29:27 +0100 | [diff] [blame] | 55 | memory_region_transaction_begin(); |
BALATON Zoltan | 3ab1eea | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 56 | memory_region_set_address(&s->io, pmbase); |
| 57 | memory_region_set_enabled(&s->io, s->dev.config[0x41] & BIT(7)); |
Gerd Hoffmann | a290282 | 2012-11-23 08:29:27 +0100 | [diff] [blame] | 58 | memory_region_transaction_commit(); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 59 | } |
| 60 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 61 | static void smb_io_space_update(ViaPMState *s) |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 62 | { |
| 63 | uint32_t smbase = pci_get_long(s->dev.config + 0x90) & 0xfff0UL; |
| 64 | |
| 65 | memory_region_transaction_begin(); |
| 66 | memory_region_set_address(&s->smb.io, smbase); |
| 67 | memory_region_set_enabled(&s->smb.io, s->dev.config[0xd2] & BIT(0)); |
| 68 | memory_region_transaction_commit(); |
| 69 | } |
| 70 | |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 71 | static int vmstate_acpi_post_load(void *opaque, int version_id) |
| 72 | { |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 73 | ViaPMState *s = opaque; |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 74 | |
| 75 | pm_io_space_update(s); |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 76 | smb_io_space_update(s); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static const VMStateDescription vmstate_acpi = { |
| 81 | .name = "vt82c686b_pm", |
| 82 | .version_id = 1, |
| 83 | .minimum_version_id = 1, |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 84 | .post_load = vmstate_acpi_post_load, |
Juan Quintela | d49805a | 2014-04-16 15:32:32 +0200 | [diff] [blame] | 85 | .fields = (VMStateField[]) { |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 86 | VMSTATE_PCI_DEVICE(dev, ViaPMState), |
| 87 | VMSTATE_UINT16(ar.pm1.evt.sts, ViaPMState), |
| 88 | VMSTATE_UINT16(ar.pm1.evt.en, ViaPMState), |
| 89 | VMSTATE_UINT16(ar.pm1.cnt.cnt, ViaPMState), |
| 90 | VMSTATE_STRUCT(apm, ViaPMState, 0, vmstate_apm, APMState), |
| 91 | VMSTATE_TIMER_PTR(ar.tmr.timer, ViaPMState), |
| 92 | VMSTATE_INT64(ar.tmr.overflow_time, ViaPMState), |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 93 | VMSTATE_END_OF_LIST() |
| 94 | } |
| 95 | }; |
| 96 | |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 97 | static void pm_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int len) |
| 98 | { |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 99 | ViaPMState *s = VIA_PM(d); |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 100 | |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 101 | trace_via_pm_write(addr, val, len); |
| 102 | pci_default_write_config(d, addr, val, len); |
BALATON Zoltan | 3ab1eea | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 103 | if (ranges_overlap(addr, len, 0x48, 4)) { |
| 104 | uint32_t v = pci_get_long(s->dev.config + 0x48); |
| 105 | pci_set_long(s->dev.config + 0x48, (v & 0xff80UL) | 1); |
| 106 | } |
| 107 | if (range_covers_byte(addr, len, 0x41)) { |
| 108 | pm_io_space_update(s); |
| 109 | } |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 110 | if (ranges_overlap(addr, len, 0x90, 4)) { |
| 111 | uint32_t v = pci_get_long(s->dev.config + 0x90); |
| 112 | pci_set_long(s->dev.config + 0x90, (v & 0xfff0UL) | 1); |
| 113 | } |
| 114 | if (range_covers_byte(addr, len, 0xd2)) { |
| 115 | s->dev.config[0xd2] &= 0xf; |
| 116 | smb_io_space_update(s); |
| 117 | } |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 118 | } |
| 119 | |
BALATON Zoltan | 35e360e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 120 | static void pm_io_write(void *op, hwaddr addr, uint64_t data, unsigned size) |
| 121 | { |
| 122 | trace_via_pm_io_write(addr, data, size); |
| 123 | } |
| 124 | |
| 125 | static uint64_t pm_io_read(void *op, hwaddr addr, unsigned size) |
| 126 | { |
| 127 | trace_via_pm_io_read(addr, 0, size); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static const MemoryRegionOps pm_io_ops = { |
| 132 | .read = pm_io_read, |
| 133 | .write = pm_io_write, |
| 134 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 135 | .impl = { |
| 136 | .min_access_size = 1, |
| 137 | .max_access_size = 1, |
| 138 | }, |
| 139 | }; |
| 140 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 141 | static void pm_update_sci(ViaPMState *s) |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 142 | { |
| 143 | int sci_level, pmsts; |
| 144 | |
| 145 | pmsts = acpi_pm1_evt_get_sts(&s->ar); |
| 146 | sci_level = (((pmsts & s->ar.pm1.evt.en) & |
| 147 | (ACPI_BITMASK_RT_CLOCK_ENABLE | |
| 148 | ACPI_BITMASK_POWER_BUTTON_ENABLE | |
| 149 | ACPI_BITMASK_GLOBAL_LOCK_ENABLE | |
| 150 | ACPI_BITMASK_TIMER_ENABLE)) != 0); |
Isaku Yamahata | 0fae92a | 2021-03-23 13:52:25 -0700 | [diff] [blame] | 151 | if (pci_get_byte(s->dev.config + PCI_INTERRUPT_PIN)) { |
| 152 | /* |
| 153 | * FIXME: |
| 154 | * Fix device model that realizes this PM device and remove |
| 155 | * this work around. |
| 156 | * The device model should wire SCI and setup |
| 157 | * PCI_INTERRUPT_PIN properly. |
| 158 | * If PIN# = 0(interrupt pin isn't used), don't raise SCI as |
| 159 | * work around. |
| 160 | */ |
| 161 | pci_set_irq(&s->dev, sci_level); |
| 162 | } |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 163 | /* schedule a timer interruption if needed */ |
| 164 | acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) && |
| 165 | !(pmsts & ACPI_BITMASK_TIMER_STATUS)); |
| 166 | } |
| 167 | |
| 168 | static void pm_tmr_timer(ACPIREGS *ar) |
| 169 | { |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 170 | ViaPMState *s = container_of(ar, ViaPMState, ar); |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 171 | pm_update_sci(s); |
| 172 | } |
| 173 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 174 | static void via_pm_reset(DeviceState *d) |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 175 | { |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 176 | ViaPMState *s = VIA_PM(d); |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 177 | |
BALATON Zoltan | 9af8e52 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 178 | memset(s->dev.config + PCI_CONFIG_HEADER_SIZE, 0, |
| 179 | PCI_CONFIG_SPACE_SIZE - PCI_CONFIG_HEADER_SIZE); |
| 180 | /* Power Management IO base */ |
| 181 | pci_set_long(s->dev.config + 0x48, 1); |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 182 | /* SMBus IO base */ |
| 183 | pci_set_long(s->dev.config + 0x90, 1); |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 184 | |
Isaku Yamahata | 44421c6 | 2021-03-23 13:52:26 -0700 | [diff] [blame] | 185 | acpi_pm1_evt_reset(&s->ar); |
| 186 | acpi_pm1_cnt_reset(&s->ar); |
| 187 | acpi_pm_tmr_reset(&s->ar); |
| 188 | pm_update_sci(s); |
| 189 | |
BALATON Zoltan | 3ab1eea | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 190 | pm_io_space_update(s); |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 191 | smb_io_space_update(s); |
| 192 | } |
| 193 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 194 | static void via_pm_realize(PCIDevice *dev, Error **errp) |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 195 | { |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 196 | ViaPMState *s = VIA_PM(dev); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 197 | |
BALATON Zoltan | 3ab1eea | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 198 | pci_set_word(dev->config + PCI_STATUS, PCI_STATUS_FAST_BACK | |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 199 | PCI_STATUS_DEVSEL_MEDIUM); |
| 200 | |
Philippe Mathieu-Daudé | a30c34d | 2019-05-28 18:40:17 +0200 | [diff] [blame] | 201 | pm_smbus_init(DEVICE(s), &s->smb, false); |
BALATON Zoltan | 911629e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 202 | memory_region_add_subregion(pci_address_space_io(dev), 0, &s->smb.io); |
| 203 | memory_region_set_enabled(&s->smb.io, false); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 204 | |
Julien Grall | 42d8a3c | 2012-09-19 12:50:03 +0100 | [diff] [blame] | 205 | apm_init(dev, &s->apm, NULL, s); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 206 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 207 | memory_region_init_io(&s->io, OBJECT(dev), &pm_io_ops, s, "via-pm", 128); |
BALATON Zoltan | 35e360e | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 208 | memory_region_add_subregion(pci_address_space_io(dev), 0, &s->io); |
Gerd Hoffmann | a290282 | 2012-11-23 08:29:27 +0100 | [diff] [blame] | 209 | memory_region_set_enabled(&s->io, false); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 210 | |
Gerd Hoffmann | 77d58b1 | 2012-11-22 12:12:30 +0100 | [diff] [blame] | 211 | acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io); |
Gerd Hoffmann | b5a7c02 | 2012-11-22 13:25:10 +0100 | [diff] [blame] | 212 | acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io); |
Isaku Yamahata | 6be8cf5 | 2021-02-17 21:51:12 -0800 | [diff] [blame] | 213 | acpi_pm1_cnt_init(&s->ar, &s->io, false, false, 2, false); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 214 | } |
| 215 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 216 | typedef struct via_pm_init_info { |
| 217 | uint16_t device_id; |
| 218 | } ViaPMInitInfo; |
| 219 | |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 220 | static void via_pm_class_init(ObjectClass *klass, void *data) |
| 221 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 222 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 223 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 224 | ViaPMInitInfo *info = data; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 225 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 226 | k->realize = via_pm_realize; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 227 | k->config_write = pm_write_config; |
| 228 | k->vendor_id = PCI_VENDOR_ID_VIA; |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 229 | k->device_id = info->device_id; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 230 | k->class_id = PCI_CLASS_BRIDGE_OTHER; |
| 231 | k->revision = 0x40; |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 232 | dc->reset = via_pm_reset; |
BALATON Zoltan | 084bf4b | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 233 | /* Reason: part of VIA south bridge, does not exist stand alone */ |
| 234 | dc->user_creatable = false; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 235 | dc->vmsd = &vmstate_acpi; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 236 | } |
| 237 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 238 | static const TypeInfo via_pm_info = { |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 239 | .name = TYPE_VIA_PM, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 240 | .parent = TYPE_PCI_DEVICE, |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 241 | .instance_size = sizeof(ViaPMState), |
| 242 | .abstract = true, |
Eduardo Habkost | fd3b02c | 2017-09-27 16:56:34 -0300 | [diff] [blame] | 243 | .interfaces = (InterfaceInfo[]) { |
| 244 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, |
| 245 | { }, |
| 246 | }, |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 247 | }; |
| 248 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 249 | static const ViaPMInitInfo vt82c686b_pm_init_info = { |
| 250 | .device_id = PCI_DEVICE_ID_VIA_82C686B_PM, |
| 251 | }; |
| 252 | |
Bernhard Beschow | d105377 | 2022-09-01 13:41:23 +0200 | [diff] [blame] | 253 | #define TYPE_VT82C686B_PM "vt82c686b-pm" |
| 254 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 255 | static const TypeInfo vt82c686b_pm_info = { |
| 256 | .name = TYPE_VT82C686B_PM, |
| 257 | .parent = TYPE_VIA_PM, |
| 258 | .class_init = via_pm_class_init, |
| 259 | .class_data = (void *)&vt82c686b_pm_init_info, |
| 260 | }; |
| 261 | |
| 262 | static const ViaPMInitInfo vt8231_pm_init_info = { |
| 263 | .device_id = PCI_DEVICE_ID_VIA_8231_PM, |
| 264 | }; |
| 265 | |
Bernhard Beschow | d105377 | 2022-09-01 13:41:23 +0200 | [diff] [blame] | 266 | #define TYPE_VT8231_PM "vt8231-pm" |
| 267 | |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 268 | static const TypeInfo vt8231_pm_info = { |
| 269 | .name = TYPE_VT8231_PM, |
| 270 | .parent = TYPE_VIA_PM, |
| 271 | .class_init = via_pm_class_init, |
| 272 | .class_data = (void *)&vt8231_pm_init_info, |
| 273 | }; |
| 274 | |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 275 | |
BALATON Zoltan | f028c2d | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 276 | #define TYPE_VIA_SUPERIO "via-superio" |
| 277 | OBJECT_DECLARE_SIMPLE_TYPE(ViaSuperIOState, VIA_SUPERIO) |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 278 | |
BALATON Zoltan | f028c2d | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 279 | struct ViaSuperIOState { |
| 280 | ISASuperIODevice superio; |
| 281 | uint8_t regs[0x100]; |
| 282 | const MemoryRegionOps *io_ops; |
| 283 | MemoryRegion io; |
| 284 | }; |
| 285 | |
| 286 | static inline void via_superio_io_enable(ViaSuperIOState *s, bool enable) |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 287 | { |
BALATON Zoltan | f028c2d | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 288 | memory_region_set_enabled(&s->io, enable); |
| 289 | } |
| 290 | |
| 291 | static void via_superio_realize(DeviceState *d, Error **errp) |
| 292 | { |
| 293 | ViaSuperIOState *s = VIA_SUPERIO(d); |
| 294 | ISASuperIOClass *ic = ISA_SUPERIO_GET_CLASS(s); |
| 295 | Error *local_err = NULL; |
| 296 | |
| 297 | assert(s->io_ops); |
| 298 | ic->parent_realize(d, &local_err); |
| 299 | if (local_err) { |
| 300 | error_propagate(errp, local_err); |
| 301 | return; |
| 302 | } |
| 303 | memory_region_init_io(&s->io, OBJECT(d), s->io_ops, s, "via-superio", 2); |
| 304 | memory_region_set_enabled(&s->io, false); |
| 305 | /* The floppy also uses 0x3f0 and 0x3f1 but this seems to work anyway */ |
| 306 | memory_region_add_subregion(isa_address_space_io(ISA_DEVICE(s)), 0x3f0, |
| 307 | &s->io); |
| 308 | } |
| 309 | |
| 310 | static uint64_t via_superio_cfg_read(void *opaque, hwaddr addr, unsigned size) |
| 311 | { |
| 312 | ViaSuperIOState *sc = opaque; |
| 313 | uint8_t idx = sc->regs[0]; |
| 314 | uint8_t val = sc->regs[idx]; |
| 315 | |
| 316 | if (addr == 0) { |
| 317 | return idx; |
| 318 | } |
| 319 | if (addr == 1 && idx == 0) { |
| 320 | val = 0; /* reading reg 0 where we store index value */ |
| 321 | } |
| 322 | trace_via_superio_read(idx, val); |
| 323 | return val; |
| 324 | } |
| 325 | |
| 326 | static void via_superio_class_init(ObjectClass *klass, void *data) |
| 327 | { |
| 328 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 329 | ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass); |
| 330 | |
| 331 | sc->parent_realize = dc->realize; |
| 332 | dc->realize = via_superio_realize; |
| 333 | } |
| 334 | |
| 335 | static const TypeInfo via_superio_info = { |
| 336 | .name = TYPE_VIA_SUPERIO, |
| 337 | .parent = TYPE_ISA_SUPERIO, |
| 338 | .instance_size = sizeof(ViaSuperIOState), |
| 339 | .class_size = sizeof(ISASuperIOClass), |
| 340 | .class_init = via_superio_class_init, |
| 341 | .abstract = true, |
| 342 | }; |
| 343 | |
| 344 | #define TYPE_VT82C686B_SUPERIO "vt82c686b-superio" |
| 345 | |
| 346 | static void vt82c686b_superio_cfg_write(void *opaque, hwaddr addr, |
| 347 | uint64_t data, unsigned size) |
| 348 | { |
| 349 | ViaSuperIOState *sc = opaque; |
BALATON Zoltan | c953bf7 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 350 | uint8_t idx = sc->regs[0]; |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 351 | |
BALATON Zoltan | cc2b455 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 352 | if (addr == 0) { /* config index register */ |
| 353 | sc->regs[0] = data; |
BALATON Zoltan | 2b98dca | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 354 | return; |
| 355 | } |
BALATON Zoltan | cc2b455 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 356 | |
| 357 | /* config data register */ |
| 358 | trace_via_superio_write(idx, data); |
BALATON Zoltan | 2b98dca | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 359 | switch (idx) { |
| 360 | case 0x00 ... 0xdf: |
| 361 | case 0xe4: |
| 362 | case 0xe5: |
| 363 | case 0xe9 ... 0xed: |
| 364 | case 0xf3: |
| 365 | case 0xf5: |
| 366 | case 0xf7: |
| 367 | case 0xf9 ... 0xfb: |
| 368 | case 0xfd ... 0xff: |
BALATON Zoltan | b7741b7 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 369 | /* ignore write to read only registers */ |
| 370 | return; |
BALATON Zoltan | 2b98dca | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 371 | /* case 0xe6 ... 0xe8: Should set base port of parallel and serial */ |
| 372 | default: |
BALATON Zoltan | 2c4c556 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 373 | qemu_log_mask(LOG_UNIMP, |
| 374 | "via_superio_cfg: unimplemented register 0x%x\n", idx); |
BALATON Zoltan | 2b98dca | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 375 | break; |
| 376 | } |
BALATON Zoltan | cc2b455 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 377 | sc->regs[idx] = data; |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 378 | } |
| 379 | |
BALATON Zoltan | f028c2d | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 380 | static const MemoryRegionOps vt82c686b_superio_cfg_ops = { |
| 381 | .read = via_superio_cfg_read, |
| 382 | .write = vt82c686b_superio_cfg_write, |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 383 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 384 | .impl = { |
| 385 | .min_access_size = 1, |
| 386 | .max_access_size = 1, |
| 387 | }, |
| 388 | }; |
| 389 | |
BALATON Zoltan | f028c2d | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 390 | static void vt82c686b_superio_reset(DeviceState *dev) |
| 391 | { |
| 392 | ViaSuperIOState *s = VIA_SUPERIO(dev); |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 393 | |
BALATON Zoltan | f028c2d | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 394 | memset(s->regs, 0, sizeof(s->regs)); |
| 395 | /* Device ID */ |
| 396 | vt82c686b_superio_cfg_write(s, 0, 0xe0, 1); |
| 397 | vt82c686b_superio_cfg_write(s, 1, 0x3c, 1); |
| 398 | /* Function select - all disabled */ |
| 399 | vt82c686b_superio_cfg_write(s, 0, 0xe2, 1); |
| 400 | vt82c686b_superio_cfg_write(s, 1, 0x03, 1); |
| 401 | /* Floppy ctrl base addr 0x3f0-7 */ |
| 402 | vt82c686b_superio_cfg_write(s, 0, 0xe3, 1); |
| 403 | vt82c686b_superio_cfg_write(s, 1, 0xfc, 1); |
| 404 | /* Parallel port base addr 0x378-f */ |
| 405 | vt82c686b_superio_cfg_write(s, 0, 0xe6, 1); |
| 406 | vt82c686b_superio_cfg_write(s, 1, 0xde, 1); |
| 407 | /* Serial port 1 base addr 0x3f8-f */ |
| 408 | vt82c686b_superio_cfg_write(s, 0, 0xe7, 1); |
| 409 | vt82c686b_superio_cfg_write(s, 1, 0xfe, 1); |
| 410 | /* Serial port 2 base addr 0x2f8-f */ |
| 411 | vt82c686b_superio_cfg_write(s, 0, 0xe8, 1); |
| 412 | vt82c686b_superio_cfg_write(s, 1, 0xbe, 1); |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 413 | |
BALATON Zoltan | f028c2d | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 414 | vt82c686b_superio_cfg_write(s, 0, 0, 1); |
| 415 | } |
| 416 | |
| 417 | static void vt82c686b_superio_init(Object *obj) |
| 418 | { |
| 419 | VIA_SUPERIO(obj)->io_ops = &vt82c686b_superio_cfg_ops; |
| 420 | } |
| 421 | |
| 422 | static void vt82c686b_superio_class_init(ObjectClass *klass, void *data) |
| 423 | { |
| 424 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 425 | ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass); |
| 426 | |
| 427 | dc->reset = vt82c686b_superio_reset; |
| 428 | sc->serial.count = 2; |
| 429 | sc->parallel.count = 1; |
| 430 | sc->ide.count = 0; /* emulated by via-ide */ |
| 431 | sc->floppy.count = 1; |
| 432 | } |
| 433 | |
| 434 | static const TypeInfo vt82c686b_superio_info = { |
| 435 | .name = TYPE_VT82C686B_SUPERIO, |
| 436 | .parent = TYPE_VIA_SUPERIO, |
| 437 | .instance_size = sizeof(ViaSuperIOState), |
| 438 | .instance_init = vt82c686b_superio_init, |
| 439 | .class_size = sizeof(ISASuperIOClass), |
| 440 | .class_init = vt82c686b_superio_class_init, |
| 441 | }; |
| 442 | |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 443 | |
BALATON Zoltan | ab74864 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 444 | #define TYPE_VT8231_SUPERIO "vt8231-superio" |
| 445 | |
| 446 | static void vt8231_superio_cfg_write(void *opaque, hwaddr addr, |
| 447 | uint64_t data, unsigned size) |
| 448 | { |
| 449 | ViaSuperIOState *sc = opaque; |
| 450 | uint8_t idx = sc->regs[0]; |
| 451 | |
| 452 | if (addr == 0) { /* config index register */ |
| 453 | sc->regs[0] = data; |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | /* config data register */ |
| 458 | trace_via_superio_write(idx, data); |
| 459 | switch (idx) { |
| 460 | case 0x00 ... 0xdf: |
| 461 | case 0xe7 ... 0xef: |
| 462 | case 0xf0 ... 0xf1: |
| 463 | case 0xf5: |
| 464 | case 0xf8: |
| 465 | case 0xfd: |
| 466 | /* ignore write to read only registers */ |
| 467 | return; |
| 468 | default: |
| 469 | qemu_log_mask(LOG_UNIMP, |
| 470 | "via_superio_cfg: unimplemented register 0x%x\n", idx); |
| 471 | break; |
| 472 | } |
| 473 | sc->regs[idx] = data; |
| 474 | } |
| 475 | |
| 476 | static const MemoryRegionOps vt8231_superio_cfg_ops = { |
| 477 | .read = via_superio_cfg_read, |
| 478 | .write = vt8231_superio_cfg_write, |
| 479 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 480 | .impl = { |
| 481 | .min_access_size = 1, |
| 482 | .max_access_size = 1, |
| 483 | }, |
| 484 | }; |
| 485 | |
| 486 | static void vt8231_superio_reset(DeviceState *dev) |
| 487 | { |
| 488 | ViaSuperIOState *s = VIA_SUPERIO(dev); |
| 489 | |
| 490 | memset(s->regs, 0, sizeof(s->regs)); |
| 491 | /* Device ID */ |
| 492 | s->regs[0xf0] = 0x3c; |
| 493 | /* Device revision */ |
| 494 | s->regs[0xf1] = 0x01; |
| 495 | /* Function select - all disabled */ |
| 496 | vt8231_superio_cfg_write(s, 0, 0xf2, 1); |
| 497 | vt8231_superio_cfg_write(s, 1, 0x03, 1); |
| 498 | /* Serial port base addr */ |
| 499 | vt8231_superio_cfg_write(s, 0, 0xf4, 1); |
| 500 | vt8231_superio_cfg_write(s, 1, 0xfe, 1); |
| 501 | /* Parallel port base addr */ |
| 502 | vt8231_superio_cfg_write(s, 0, 0xf6, 1); |
| 503 | vt8231_superio_cfg_write(s, 1, 0xde, 1); |
| 504 | /* Floppy ctrl base addr */ |
| 505 | vt8231_superio_cfg_write(s, 0, 0xf7, 1); |
| 506 | vt8231_superio_cfg_write(s, 1, 0xfc, 1); |
| 507 | |
| 508 | vt8231_superio_cfg_write(s, 0, 0, 1); |
| 509 | } |
| 510 | |
| 511 | static void vt8231_superio_init(Object *obj) |
| 512 | { |
| 513 | VIA_SUPERIO(obj)->io_ops = &vt8231_superio_cfg_ops; |
| 514 | } |
| 515 | |
| 516 | static uint16_t vt8231_superio_serial_iobase(ISASuperIODevice *sio, |
| 517 | uint8_t index) |
| 518 | { |
| 519 | return 0x2f8; /* FIXME: This should be settable via registers f2-f4 */ |
| 520 | } |
| 521 | |
| 522 | static void vt8231_superio_class_init(ObjectClass *klass, void *data) |
| 523 | { |
| 524 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 525 | ISASuperIOClass *sc = ISA_SUPERIO_CLASS(klass); |
| 526 | |
| 527 | dc->reset = vt8231_superio_reset; |
| 528 | sc->serial.count = 1; |
| 529 | sc->serial.get_iobase = vt8231_superio_serial_iobase; |
| 530 | sc->parallel.count = 1; |
| 531 | sc->ide.count = 0; /* emulated by via-ide */ |
| 532 | sc->floppy.count = 1; |
| 533 | } |
| 534 | |
| 535 | static const TypeInfo vt8231_superio_info = { |
| 536 | .name = TYPE_VT8231_SUPERIO, |
| 537 | .parent = TYPE_VIA_SUPERIO, |
| 538 | .instance_size = sizeof(ViaSuperIOState), |
| 539 | .instance_init = vt8231_superio_init, |
| 540 | .class_size = sizeof(ISASuperIOClass), |
| 541 | .class_init = vt8231_superio_class_init, |
| 542 | }; |
| 543 | |
| 544 | |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 545 | #define TYPE_VIA_ISA "via-isa" |
| 546 | OBJECT_DECLARE_SIMPLE_TYPE(ViaISAState, VIA_ISA) |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 547 | |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 548 | struct ViaISAState { |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 549 | PCIDevice dev; |
BALATON Zoltan | 3dc31cb | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 550 | qemu_irq cpu_intr; |
Philippe Mathieu-Daudé | bb98e0f | 2023-02-09 10:38:42 +0100 | [diff] [blame] | 551 | qemu_irq *isa_irqs_in; |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 552 | ViaSuperIOState via_sio; |
Philippe Mathieu-Daudé | 8df7129 | 2023-02-11 00:17:03 +0100 | [diff] [blame] | 553 | MC146818RtcState rtc; |
Bernhard Beschow | 9eb6abb | 2022-06-13 19:24:55 +0200 | [diff] [blame] | 554 | PCIIDEState ide; |
Bernhard Beschow | 1a99ddb | 2022-09-01 13:41:22 +0200 | [diff] [blame] | 555 | UHCIState uhci[2]; |
Bernhard Beschow | d105377 | 2022-09-01 13:41:23 +0200 | [diff] [blame] | 556 | ViaPMState pm; |
BALATON Zoltan | eb60441 | 2022-01-23 21:40:42 +0100 | [diff] [blame] | 557 | ViaAC97State ac97; |
Bernhard Beschow | 0a8d405 | 2022-09-01 13:41:24 +0200 | [diff] [blame] | 558 | PCIDevice mc97; |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 559 | }; |
| 560 | |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 561 | static const VMStateDescription vmstate_via = { |
| 562 | .name = "via-isa", |
| 563 | .version_id = 1, |
| 564 | .minimum_version_id = 1, |
| 565 | .fields = (VMStateField[]) { |
| 566 | VMSTATE_PCI_DEVICE(dev, ViaISAState), |
| 567 | VMSTATE_END_OF_LIST() |
| 568 | } |
| 569 | }; |
| 570 | |
Bernhard Beschow | 9eb6abb | 2022-06-13 19:24:55 +0200 | [diff] [blame] | 571 | static void via_isa_init(Object *obj) |
| 572 | { |
| 573 | ViaISAState *s = VIA_ISA(obj); |
| 574 | |
Bernhard Beschow | 3ecb2e6 | 2022-09-01 13:41:26 +0200 | [diff] [blame] | 575 | object_initialize_child(obj, "rtc", &s->rtc, TYPE_MC146818_RTC); |
Bernhard Beschow | 9eb6abb | 2022-06-13 19:24:55 +0200 | [diff] [blame] | 576 | object_initialize_child(obj, "ide", &s->ide, TYPE_VIA_IDE); |
Bernhard Beschow | 1a99ddb | 2022-09-01 13:41:22 +0200 | [diff] [blame] | 577 | object_initialize_child(obj, "uhci1", &s->uhci[0], TYPE_VT82C686B_USB_UHCI); |
| 578 | object_initialize_child(obj, "uhci2", &s->uhci[1], TYPE_VT82C686B_USB_UHCI); |
Bernhard Beschow | 0a8d405 | 2022-09-01 13:41:24 +0200 | [diff] [blame] | 579 | object_initialize_child(obj, "ac97", &s->ac97, TYPE_VIA_AC97); |
| 580 | object_initialize_child(obj, "mc97", &s->mc97, TYPE_VIA_MC97); |
Bernhard Beschow | 9eb6abb | 2022-06-13 19:24:55 +0200 | [diff] [blame] | 581 | } |
| 582 | |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 583 | static const TypeInfo via_isa_info = { |
| 584 | .name = TYPE_VIA_ISA, |
| 585 | .parent = TYPE_PCI_DEVICE, |
| 586 | .instance_size = sizeof(ViaISAState), |
Bernhard Beschow | 9eb6abb | 2022-06-13 19:24:55 +0200 | [diff] [blame] | 587 | .instance_init = via_isa_init, |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 588 | .abstract = true, |
| 589 | .interfaces = (InterfaceInfo[]) { |
| 590 | { INTERFACE_CONVENTIONAL_PCI_DEVICE }, |
| 591 | { }, |
| 592 | }, |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 593 | }; |
| 594 | |
BALATON Zoltan | a4d65b7 | 2021-10-15 03:06:20 +0200 | [diff] [blame] | 595 | void via_isa_set_irq(PCIDevice *d, int n, int level) |
| 596 | { |
| 597 | ViaISAState *s = VIA_ISA(d); |
Philippe Mathieu-Daudé | bb98e0f | 2023-02-09 10:38:42 +0100 | [diff] [blame] | 598 | qemu_set_irq(s->isa_irqs_in[n], level); |
BALATON Zoltan | 3dc31cb | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 599 | } |
| 600 | |
BALATON Zoltan | 3820001 | 2023-03-01 01:17:08 +0100 | [diff] [blame] | 601 | static void via_isa_request_i8259_irq(void *opaque, int irq, int level) |
| 602 | { |
| 603 | ViaISAState *s = opaque; |
| 604 | qemu_set_irq(s->cpu_intr, level); |
| 605 | } |
| 606 | |
BALATON Zoltan | 2fdadd0 | 2023-02-16 21:21:35 +0100 | [diff] [blame] | 607 | static int via_isa_get_pci_irq(const ViaISAState *s, int irq_num) |
| 608 | { |
| 609 | switch (irq_num) { |
| 610 | case 0: |
| 611 | return s->dev.config[0x55] >> 4; |
| 612 | case 1: |
| 613 | return s->dev.config[0x56] & 0xf; |
| 614 | case 2: |
| 615 | return s->dev.config[0x56] >> 4; |
| 616 | case 3: |
| 617 | return s->dev.config[0x57] >> 4; |
| 618 | } |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static void via_isa_set_pci_irq(void *opaque, int irq_num, int level) |
| 623 | { |
| 624 | ViaISAState *s = opaque; |
| 625 | PCIBus *bus = pci_get_bus(&s->dev); |
| 626 | int i, pic_level, pic_irq = via_isa_get_pci_irq(s, irq_num); |
| 627 | |
| 628 | /* IRQ 0: disabled, IRQ 2,8,13: reserved */ |
| 629 | if (!pic_irq) { |
| 630 | return; |
| 631 | } |
| 632 | if (unlikely(pic_irq == 2 || pic_irq == 8 || pic_irq == 13)) { |
| 633 | qemu_log_mask(LOG_GUEST_ERROR, "Invalid ISA IRQ routing"); |
| 634 | } |
| 635 | |
| 636 | /* The pic level is the logical OR of all the PCI irqs mapped to it. */ |
| 637 | pic_level = 0; |
| 638 | for (i = 0; i < PCI_NUM_PINS; i++) { |
| 639 | if (pic_irq == via_isa_get_pci_irq(s, i)) { |
| 640 | pic_level |= pci_bus_get_irq_level(bus, i); |
| 641 | } |
| 642 | } |
| 643 | /* Now we change the pic irq level according to the via irq mappings. */ |
| 644 | qemu_set_irq(s->isa_irqs_in[pic_irq], pic_level); |
| 645 | } |
| 646 | |
BALATON Zoltan | 3a2f166 | 2021-10-15 03:06:20 +0200 | [diff] [blame] | 647 | static void via_isa_realize(PCIDevice *d, Error **errp) |
| 648 | { |
| 649 | ViaISAState *s = VIA_ISA(d); |
| 650 | DeviceState *dev = DEVICE(d); |
Bernhard Beschow | 9eb6abb | 2022-06-13 19:24:55 +0200 | [diff] [blame] | 651 | PCIBus *pci_bus = pci_get_bus(d); |
BALATON Zoltan | 3820001 | 2023-03-01 01:17:08 +0100 | [diff] [blame] | 652 | qemu_irq *isa_irq; |
Bernhard Beschow | 91ba92d | 2022-09-01 13:41:16 +0200 | [diff] [blame] | 653 | ISABus *isa_bus; |
BALATON Zoltan | 3a2f166 | 2021-10-15 03:06:20 +0200 | [diff] [blame] | 654 | int i; |
| 655 | |
| 656 | qdev_init_gpio_out(dev, &s->cpu_intr, 1); |
BALATON Zoltan | 3820001 | 2023-03-01 01:17:08 +0100 | [diff] [blame] | 657 | isa_irq = qemu_allocate_irqs(via_isa_request_i8259_irq, s, 1); |
Bernhard Beschow | dd28cc8 | 2022-09-01 13:41:17 +0200 | [diff] [blame] | 658 | isa_bus = isa_bus_new(dev, pci_address_space(d), pci_address_space_io(d), |
Bernhard Beschow | c1561d1 | 2022-09-01 13:41:18 +0200 | [diff] [blame] | 659 | errp); |
| 660 | |
| 661 | if (!isa_bus) { |
| 662 | return; |
| 663 | } |
| 664 | |
BALATON Zoltan | 3820001 | 2023-03-01 01:17:08 +0100 | [diff] [blame] | 665 | s->isa_irqs_in = i8259_init(isa_bus, *isa_irq); |
Philippe Mathieu-Daudé | 7067887 | 2023-02-09 13:32:18 +0100 | [diff] [blame] | 666 | isa_bus_register_input_irqs(isa_bus, s->isa_irqs_in); |
Bernhard Beschow | 91ba92d | 2022-09-01 13:41:16 +0200 | [diff] [blame] | 667 | i8254_pit_init(isa_bus, 0x40, 0, NULL); |
| 668 | i8257_dma_init(isa_bus, 0); |
Bernhard Beschow | 3ecb2e6 | 2022-09-01 13:41:26 +0200 | [diff] [blame] | 669 | |
BALATON Zoltan | 2fdadd0 | 2023-02-16 21:21:35 +0100 | [diff] [blame] | 670 | qdev_init_gpio_in_named(dev, via_isa_set_pci_irq, "pirq", PCI_NUM_PINS); |
| 671 | |
Bernhard Beschow | 3ecb2e6 | 2022-09-01 13:41:26 +0200 | [diff] [blame] | 672 | /* RTC */ |
| 673 | qdev_prop_set_int32(DEVICE(&s->rtc), "base_year", 2000); |
| 674 | if (!qdev_realize(DEVICE(&s->rtc), BUS(isa_bus), errp)) { |
| 675 | return; |
| 676 | } |
Bernhard Beschow | 3ecb2e6 | 2022-09-01 13:41:26 +0200 | [diff] [blame] | 677 | isa_connect_gpio_out(ISA_DEVICE(&s->rtc), 0, s->rtc.isairq); |
BALATON Zoltan | 3a2f166 | 2021-10-15 03:06:20 +0200 | [diff] [blame] | 678 | |
| 679 | for (i = 0; i < PCI_CONFIG_HEADER_SIZE; i++) { |
| 680 | if (i < PCI_COMMAND || i >= PCI_REVISION_ID) { |
| 681 | d->wmask[i] = 0; |
| 682 | } |
| 683 | } |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 684 | |
| 685 | /* Super I/O */ |
Bernhard Beschow | 91ba92d | 2022-09-01 13:41:16 +0200 | [diff] [blame] | 686 | if (!qdev_realize(DEVICE(&s->via_sio), BUS(isa_bus), errp)) { |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 687 | return; |
| 688 | } |
Bernhard Beschow | 9eb6abb | 2022-06-13 19:24:55 +0200 | [diff] [blame] | 689 | |
| 690 | /* Function 1: IDE */ |
| 691 | qdev_prop_set_int32(DEVICE(&s->ide), "addr", d->devfn + 1); |
| 692 | if (!qdev_realize(DEVICE(&s->ide), BUS(pci_bus), errp)) { |
| 693 | return; |
| 694 | } |
Bernhard Beschow | 1a99ddb | 2022-09-01 13:41:22 +0200 | [diff] [blame] | 695 | |
| 696 | /* Functions 2-3: USB Ports */ |
| 697 | for (i = 0; i < ARRAY_SIZE(s->uhci); i++) { |
| 698 | qdev_prop_set_int32(DEVICE(&s->uhci[i]), "addr", d->devfn + 2 + i); |
| 699 | if (!qdev_realize(DEVICE(&s->uhci[i]), BUS(pci_bus), errp)) { |
| 700 | return; |
| 701 | } |
| 702 | } |
Bernhard Beschow | d105377 | 2022-09-01 13:41:23 +0200 | [diff] [blame] | 703 | |
| 704 | /* Function 4: Power Management */ |
| 705 | qdev_prop_set_int32(DEVICE(&s->pm), "addr", d->devfn + 4); |
| 706 | if (!qdev_realize(DEVICE(&s->pm), BUS(pci_bus), errp)) { |
| 707 | return; |
| 708 | } |
Bernhard Beschow | 0a8d405 | 2022-09-01 13:41:24 +0200 | [diff] [blame] | 709 | |
| 710 | /* Function 5: AC97 Audio */ |
| 711 | qdev_prop_set_int32(DEVICE(&s->ac97), "addr", d->devfn + 5); |
| 712 | if (!qdev_realize(DEVICE(&s->ac97), BUS(pci_bus), errp)) { |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | /* Function 6: MC97 Modem */ |
| 717 | qdev_prop_set_int32(DEVICE(&s->mc97), "addr", d->devfn + 6); |
| 718 | if (!qdev_realize(DEVICE(&s->mc97), BUS(pci_bus), errp)) { |
| 719 | return; |
| 720 | } |
BALATON Zoltan | 3a2f166 | 2021-10-15 03:06:20 +0200 | [diff] [blame] | 721 | } |
| 722 | |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 723 | /* TYPE_VT82C686B_ISA */ |
| 724 | |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 725 | static void vt82c686b_write_config(PCIDevice *d, uint32_t addr, |
| 726 | uint32_t val, int len) |
| 727 | { |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 728 | ViaISAState *s = VIA_ISA(d); |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 729 | |
| 730 | trace_via_isa_write(addr, val, len); |
| 731 | pci_default_write_config(d, addr, val, len); |
| 732 | if (addr == 0x85) { |
| 733 | /* BIT(1): enable or disable superio config io ports */ |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 734 | via_superio_io_enable(&s->via_sio, val & BIT(1)); |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 738 | static void vt82c686b_isa_reset(DeviceState *dev) |
| 739 | { |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 740 | ViaISAState *s = VIA_ISA(dev); |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 741 | uint8_t *pci_conf = s->dev.config; |
| 742 | |
| 743 | pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0); |
| 744 | pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY | |
| 745 | PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL); |
| 746 | pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM); |
| 747 | |
| 748 | pci_conf[0x48] = 0x01; /* Miscellaneous Control 3 */ |
| 749 | pci_conf[0x4a] = 0x04; /* IDE interrupt Routing */ |
| 750 | pci_conf[0x4f] = 0x03; /* DMA/Master Mem Access Control 3 */ |
| 751 | pci_conf[0x50] = 0x2d; /* PnP DMA Request Control */ |
| 752 | pci_conf[0x59] = 0x04; |
| 753 | pci_conf[0x5a] = 0x04; /* KBC/RTC Control*/ |
| 754 | pci_conf[0x5f] = 0x04; |
| 755 | pci_conf[0x77] = 0x10; /* GPIO Control 1/2/3/4 */ |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 758 | static void vt82c686b_init(Object *obj) |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 759 | { |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 760 | ViaISAState *s = VIA_ISA(obj); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 761 | |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 762 | object_initialize_child(obj, "sio", &s->via_sio, TYPE_VT82C686B_SUPERIO); |
Bernhard Beschow | d105377 | 2022-09-01 13:41:23 +0200 | [diff] [blame] | 763 | object_initialize_child(obj, "pm", &s->pm, TYPE_VT82C686B_PM); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 764 | } |
| 765 | |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 766 | static void vt82c686b_class_init(ObjectClass *klass, void *data) |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 767 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 768 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 769 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
| 770 | |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 771 | k->realize = via_isa_realize; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 772 | k->config_write = vt82c686b_write_config; |
| 773 | k->vendor_id = PCI_VENDOR_ID_VIA; |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 774 | k->device_id = PCI_DEVICE_ID_VIA_82C686B_ISA; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 775 | k->class_id = PCI_CLASS_BRIDGE_ISA; |
| 776 | k->revision = 0x40; |
Philippe Mathieu-Daudé | 9dc1a76 | 2019-10-10 15:15:25 +0200 | [diff] [blame] | 777 | dc->reset = vt82c686b_isa_reset; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 778 | dc->desc = "ISA bridge"; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 779 | dc->vmsd = &vmstate_via; |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 780 | /* Reason: part of VIA VT82C686 southbridge, needs to be wired up */ |
Eduardo Habkost | e90f2a8 | 2017-05-03 17:35:44 -0300 | [diff] [blame] | 781 | dc->user_creatable = false; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 782 | } |
| 783 | |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 784 | static const TypeInfo vt82c686b_isa_info = { |
BALATON Zoltan | 0f79846 | 2021-01-02 11:43:35 +0100 | [diff] [blame] | 785 | .name = TYPE_VT82C686B_ISA, |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 786 | .parent = TYPE_VIA_ISA, |
| 787 | .instance_size = sizeof(ViaISAState), |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 788 | .instance_init = vt82c686b_init, |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 789 | .class_init = vt82c686b_class_init, |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 790 | }; |
| 791 | |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 792 | /* TYPE_VT8231_ISA */ |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 793 | |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 794 | static void vt8231_write_config(PCIDevice *d, uint32_t addr, |
| 795 | uint32_t val, int len) |
Philippe Mathieu-Daudé | 98cf824 | 2018-03-08 23:39:40 +0100 | [diff] [blame] | 796 | { |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 797 | ViaISAState *s = VIA_ISA(d); |
Philippe Mathieu-Daudé | 98cf824 | 2018-03-08 23:39:40 +0100 | [diff] [blame] | 798 | |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 799 | trace_via_isa_write(addr, val, len); |
| 800 | pci_default_write_config(d, addr, val, len); |
| 801 | if (addr == 0x50) { |
| 802 | /* BIT(2): enable or disable superio config io ports */ |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 803 | via_superio_io_enable(&s->via_sio, val & BIT(2)); |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 804 | } |
Philippe Mathieu-Daudé | 98cf824 | 2018-03-08 23:39:40 +0100 | [diff] [blame] | 805 | } |
| 806 | |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 807 | static void vt8231_isa_reset(DeviceState *dev) |
| 808 | { |
| 809 | ViaISAState *s = VIA_ISA(dev); |
| 810 | uint8_t *pci_conf = s->dev.config; |
| 811 | |
| 812 | pci_set_long(pci_conf + PCI_CAPABILITY_LIST, 0x000000c0); |
| 813 | pci_set_word(pci_conf + PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY | |
| 814 | PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL); |
| 815 | pci_set_word(pci_conf + PCI_STATUS, PCI_STATUS_DEVSEL_MEDIUM); |
| 816 | |
| 817 | pci_conf[0x58] = 0x40; /* Miscellaneous Control 0 */ |
| 818 | pci_conf[0x67] = 0x08; /* Fast IR Config */ |
| 819 | pci_conf[0x6b] = 0x01; /* Fast IR I/O Base */ |
| 820 | } |
| 821 | |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 822 | static void vt8231_init(Object *obj) |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 823 | { |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 824 | ViaISAState *s = VIA_ISA(obj); |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 825 | |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 826 | object_initialize_child(obj, "sio", &s->via_sio, TYPE_VT8231_SUPERIO); |
Bernhard Beschow | d105377 | 2022-09-01 13:41:23 +0200 | [diff] [blame] | 827 | object_initialize_child(obj, "pm", &s->pm, TYPE_VT8231_PM); |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | static void vt8231_class_init(ObjectClass *klass, void *data) |
| 831 | { |
| 832 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 833 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
| 834 | |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 835 | k->realize = via_isa_realize; |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 836 | k->config_write = vt8231_write_config; |
| 837 | k->vendor_id = PCI_VENDOR_ID_VIA; |
| 838 | k->device_id = PCI_DEVICE_ID_VIA_8231_ISA; |
| 839 | k->class_id = PCI_CLASS_BRIDGE_ISA; |
| 840 | k->revision = 0x10; |
| 841 | dc->reset = vt8231_isa_reset; |
| 842 | dc->desc = "ISA bridge"; |
| 843 | dc->vmsd = &vmstate_via; |
| 844 | /* Reason: part of VIA VT8231 southbridge, needs to be wired up */ |
| 845 | dc->user_creatable = false; |
| 846 | } |
| 847 | |
| 848 | static const TypeInfo vt8231_isa_info = { |
| 849 | .name = TYPE_VT8231_ISA, |
| 850 | .parent = TYPE_VIA_ISA, |
| 851 | .instance_size = sizeof(ViaISAState), |
Bernhard Beschow | 8e4022a | 2022-09-01 13:41:15 +0200 | [diff] [blame] | 852 | .instance_init = vt8231_init, |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 853 | .class_init = vt8231_class_init, |
Philippe Mathieu-Daudé | 98cf824 | 2018-03-08 23:39:40 +0100 | [diff] [blame] | 854 | }; |
| 855 | |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 856 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 857 | static void vt82c686b_register_types(void) |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 858 | { |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 859 | type_register_static(&via_pm_info); |
BALATON Zoltan | e1a6973 | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 860 | type_register_static(&vt82c686b_pm_info); |
| 861 | type_register_static(&vt8231_pm_info); |
BALATON Zoltan | 94349bf | 2021-01-09 21:16:36 +0100 | [diff] [blame] | 862 | type_register_static(&via_superio_info); |
BALATON Zoltan | f028c2d | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 863 | type_register_static(&vt82c686b_superio_info); |
BALATON Zoltan | ab74864 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 864 | type_register_static(&vt8231_superio_info); |
BALATON Zoltan | 2e84e10 | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 865 | type_register_static(&via_isa_info); |
| 866 | type_register_static(&vt82c686b_isa_info); |
BALATON Zoltan | f9f0c9e | 2021-03-25 14:50:39 +0100 | [diff] [blame] | 867 | type_register_static(&vt8231_isa_info); |
Huacai Chen | edf79e6 | 2010-06-29 10:49:29 +0800 | [diff] [blame] | 868 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 869 | |
| 870 | type_init(vt82c686b_register_types) |