Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 1 | /* |
| 2 | * ACPI implementation |
| 3 | * |
| 4 | * Copyright (c) 2006 Fabrice Bellard |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, see <http://www.gnu.org/licenses/> |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 17 | * |
| 18 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 19 | * GNU GPL, version 2 or (at your option) any later version. |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 20 | */ |
| 21 | #include "hw.h" |
| 22 | #include "pc.h" |
| 23 | #include "apm.h" |
| 24 | #include "pm_smbus.h" |
Michael S. Tsirkin | a2cb15b | 2012-12-12 14:24:50 +0200 | [diff] [blame] | 25 | #include "pci/pci.h" |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 26 | #include "acpi.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 27 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 28 | #include "qemu/range.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 29 | #include "exec/ioport.h" |
Gleb Natapov | 459ae5e | 2012-06-04 14:31:55 +0300 | [diff] [blame] | 30 | #include "fw_cfg.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 31 | #include "exec/address-spaces.h" |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 32 | |
| 33 | //#define DEBUG |
| 34 | |
Isaku Yamahata | 50d8ff8 | 2010-05-14 16:29:22 +0900 | [diff] [blame] | 35 | #ifdef DEBUG |
| 36 | # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) |
| 37 | #else |
| 38 | # define PIIX4_DPRINTF(format, ...) do { } while (0) |
| 39 | #endif |
| 40 | |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 41 | #define GPE_BASE 0xafe0 |
Isaku Yamahata | 23910d3 | 2011-03-25 19:54:41 +0900 | [diff] [blame] | 42 | #define GPE_LEN 4 |
Gerd Hoffmann | c177684 | 2012-11-23 16:03:19 +0100 | [diff] [blame] | 43 | |
| 44 | #define PCI_HOTPLUG_ADDR 0xae00 |
| 45 | #define PCI_HOTPLUG_SIZE 0x000f |
Alex Williamson | ba73754 | 2012-04-05 11:07:08 -0600 | [diff] [blame] | 46 | #define PCI_UP_BASE 0xae00 |
| 47 | #define PCI_DOWN_BASE 0xae04 |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 48 | #define PCI_EJ_BASE 0xae08 |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 49 | #define PCI_RMV_BASE 0xae0c |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 50 | |
Gleb Natapov | 4441a28 | 2010-10-17 11:45:24 +0200 | [diff] [blame] | 51 | #define PIIX4_PCI_HOTPLUG_STATUS 2 |
| 52 | |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 53 | struct pci_status { |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 54 | uint32_t up; /* deprecated, maintained for migration compatibility */ |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 55 | uint32_t down; |
| 56 | }; |
| 57 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 58 | typedef struct PIIX4PMState { |
| 59 | PCIDevice dev; |
Gerd Hoffmann | 56e5b2a | 2012-12-11 09:40:45 +0100 | [diff] [blame] | 60 | |
Gerd Hoffmann | af11110 | 2012-11-22 11:34:44 +0100 | [diff] [blame] | 61 | MemoryRegion io; |
Gerd Hoffmann | b65b93f | 2012-11-23 15:35:13 +0100 | [diff] [blame] | 62 | MemoryRegion io_gpe; |
Gerd Hoffmann | c177684 | 2012-11-23 16:03:19 +0100 | [diff] [blame] | 63 | MemoryRegion io_pci; |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 64 | ACPIREGS ar; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 65 | |
| 66 | APMState apm; |
| 67 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 68 | PMSMBus smb; |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 69 | uint32_t smb_io_base; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 70 | |
| 71 | qemu_irq irq; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 72 | qemu_irq smi_irq; |
| 73 | int kvm_enabled; |
Paolo Bonzini | 6141dbf | 2011-07-15 17:10:15 +0200 | [diff] [blame] | 74 | Notifier machine_ready; |
Igor Mammedov | d010f91 | 2012-09-05 23:06:22 +0200 | [diff] [blame] | 75 | Notifier powerdown_notifier; |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 76 | |
| 77 | /* for pci hotplug */ |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 78 | struct pci_status pci0_status; |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 79 | uint32_t pci0_hotplug_enable; |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 80 | uint32_t pci0_slot_device_present; |
Gleb Natapov | 459ae5e | 2012-06-04 14:31:55 +0300 | [diff] [blame] | 81 | |
| 82 | uint8_t disable_s3; |
| 83 | uint8_t disable_s4; |
| 84 | uint8_t s4_val; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 85 | } PIIX4PMState; |
| 86 | |
Gerd Hoffmann | 56e5b2a | 2012-12-11 09:40:45 +0100 | [diff] [blame] | 87 | static void piix4_acpi_system_hot_add_init(MemoryRegion *parent, |
| 88 | PCIBus *bus, PIIX4PMState *s); |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 89 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 90 | #define ACPI_ENABLE 0xf1 |
| 91 | #define ACPI_DISABLE 0xf0 |
| 92 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 93 | static void pm_update_sci(PIIX4PMState *s) |
| 94 | { |
| 95 | int sci_level, pmsts; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 96 | |
Gerd Hoffmann | 2886be1 | 2012-02-23 13:45:17 +0100 | [diff] [blame] | 97 | pmsts = acpi_pm1_evt_get_sts(&s->ar); |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 98 | sci_level = (((pmsts & s->ar.pm1.evt.en) & |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 99 | (ACPI_BITMASK_RT_CLOCK_ENABLE | |
| 100 | ACPI_BITMASK_POWER_BUTTON_ENABLE | |
| 101 | ACPI_BITMASK_GLOBAL_LOCK_ENABLE | |
Gleb Natapov | 633aa0a | 2010-10-17 11:45:25 +0200 | [diff] [blame] | 102 | ACPI_BITMASK_TIMER_ENABLE)) != 0) || |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 103 | (((s->ar.gpe.sts[0] & s->ar.gpe.en[0]) |
| 104 | & PIIX4_PCI_HOTPLUG_STATUS) != 0); |
Gleb Natapov | 633aa0a | 2010-10-17 11:45:25 +0200 | [diff] [blame] | 105 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 106 | qemu_set_irq(s->irq, sci_level); |
| 107 | /* schedule a timer interruption if needed */ |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 108 | acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) && |
Isaku Yamahata | a54d41a | 2011-03-25 19:54:38 +0900 | [diff] [blame] | 109 | !(pmsts & ACPI_BITMASK_TIMER_STATUS)); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 112 | static void pm_tmr_timer(ACPIREGS *ar) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 113 | { |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 114 | PIIX4PMState *s = container_of(ar, PIIX4PMState, ar); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 115 | pm_update_sci(s); |
| 116 | } |
| 117 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 118 | static void apm_ctrl_changed(uint32_t val, void *arg) |
| 119 | { |
| 120 | PIIX4PMState *s = arg; |
| 121 | |
| 122 | /* ACPI specs 3.0, 4.7.2.5 */ |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 123 | acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 124 | |
| 125 | if (s->dev.config[0x5b] & (1 << 1)) { |
| 126 | if (s->smi_irq) { |
| 127 | qemu_irq_raise(s->smi_irq); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 132 | static void pm_io_space_update(PIIX4PMState *s) |
| 133 | { |
| 134 | uint32_t pm_io_base; |
| 135 | |
Gerd Hoffmann | af11110 | 2012-11-22 11:34:44 +0100 | [diff] [blame] | 136 | pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40)); |
| 137 | pm_io_base &= 0xffc0; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 138 | |
Gerd Hoffmann | af11110 | 2012-11-22 11:34:44 +0100 | [diff] [blame] | 139 | memory_region_transaction_begin(); |
| 140 | memory_region_set_enabled(&s->io, s->dev.config[0x80] & 1); |
| 141 | memory_region_set_address(&s->io, pm_io_base); |
| 142 | memory_region_transaction_commit(); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Gerd Hoffmann | 24fe083 | 2012-11-23 14:58:04 +0100 | [diff] [blame] | 145 | static void smbus_io_space_update(PIIX4PMState *s) |
| 146 | { |
| 147 | s->smb_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x90)); |
| 148 | s->smb_io_base &= 0xffc0; |
| 149 | |
| 150 | memory_region_transaction_begin(); |
| 151 | memory_region_set_enabled(&s->smb.io, s->dev.config[0xd2] & 1); |
| 152 | memory_region_set_address(&s->smb.io, s->smb_io_base); |
| 153 | memory_region_transaction_commit(); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void pm_write_config(PCIDevice *d, |
| 157 | uint32_t address, uint32_t val, int len) |
| 158 | { |
| 159 | pci_default_write_config(d, address, val, len); |
Gerd Hoffmann | 24fe083 | 2012-11-23 14:58:04 +0100 | [diff] [blame] | 160 | if (range_covers_byte(address, len, 0x80) || |
| 161 | ranges_overlap(address, len, 0x40, 4)) { |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 162 | pm_io_space_update((PIIX4PMState *)d); |
Gerd Hoffmann | 24fe083 | 2012-11-23 14:58:04 +0100 | [diff] [blame] | 163 | } |
| 164 | if (range_covers_byte(address, len, 0xd2) || |
| 165 | ranges_overlap(address, len, 0x90, 4)) { |
| 166 | smbus_io_space_update((PIIX4PMState *)d); |
| 167 | } |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 168 | } |
| 169 | |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 170 | static void vmstate_pci_status_pre_save(void *opaque) |
| 171 | { |
| 172 | struct pci_status *pci0_status = opaque; |
| 173 | PIIX4PMState *s = container_of(pci0_status, PIIX4PMState, pci0_status); |
| 174 | |
| 175 | /* We no longer track up, so build a safe value for migrating |
| 176 | * to a version that still does... of course these might get lost |
| 177 | * by an old buggy implementation, but we try. */ |
| 178 | pci0_status->up = s->pci0_slot_device_present & s->pci0_hotplug_enable; |
| 179 | } |
| 180 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 181 | static int vmstate_acpi_post_load(void *opaque, int version_id) |
| 182 | { |
| 183 | PIIX4PMState *s = opaque; |
| 184 | |
| 185 | pm_io_space_update(s); |
| 186 | return 0; |
| 187 | } |
| 188 | |
Isaku Yamahata | 23910d3 | 2011-03-25 19:54:41 +0900 | [diff] [blame] | 189 | #define VMSTATE_GPE_ARRAY(_field, _state) \ |
| 190 | { \ |
| 191 | .name = (stringify(_field)), \ |
| 192 | .version_id = 0, \ |
Isaku Yamahata | 23910d3 | 2011-03-25 19:54:41 +0900 | [diff] [blame] | 193 | .info = &vmstate_info_uint16, \ |
| 194 | .size = sizeof(uint16_t), \ |
Marcelo Tosatti | b0b873a | 2012-10-30 21:14:19 -0200 | [diff] [blame] | 195 | .flags = VMS_SINGLE | VMS_POINTER, \ |
Isaku Yamahata | 23910d3 | 2011-03-25 19:54:41 +0900 | [diff] [blame] | 196 | .offset = vmstate_offset_pointer(_state, _field, uint8_t), \ |
| 197 | } |
| 198 | |
Alex Williamson | 4cf3e6f | 2010-06-02 10:58:29 -0600 | [diff] [blame] | 199 | static const VMStateDescription vmstate_gpe = { |
| 200 | .name = "gpe", |
| 201 | .version_id = 1, |
| 202 | .minimum_version_id = 1, |
| 203 | .minimum_version_id_old = 1, |
| 204 | .fields = (VMStateField []) { |
Isaku Yamahata | 23910d3 | 2011-03-25 19:54:41 +0900 | [diff] [blame] | 205 | VMSTATE_GPE_ARRAY(sts, ACPIGPE), |
| 206 | VMSTATE_GPE_ARRAY(en, ACPIGPE), |
Alex Williamson | 4cf3e6f | 2010-06-02 10:58:29 -0600 | [diff] [blame] | 207 | VMSTATE_END_OF_LIST() |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | static const VMStateDescription vmstate_pci_status = { |
| 212 | .name = "pci_status", |
| 213 | .version_id = 1, |
| 214 | .minimum_version_id = 1, |
| 215 | .minimum_version_id_old = 1, |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 216 | .pre_save = vmstate_pci_status_pre_save, |
Alex Williamson | 4cf3e6f | 2010-06-02 10:58:29 -0600 | [diff] [blame] | 217 | .fields = (VMStateField []) { |
| 218 | VMSTATE_UINT32(up, struct pci_status), |
| 219 | VMSTATE_UINT32(down, struct pci_status), |
| 220 | VMSTATE_END_OF_LIST() |
| 221 | } |
| 222 | }; |
| 223 | |
Marcelo Tosatti | b0b873a | 2012-10-30 21:14:19 -0200 | [diff] [blame] | 224 | static int acpi_load_old(QEMUFile *f, void *opaque, int version_id) |
| 225 | { |
| 226 | PIIX4PMState *s = opaque; |
| 227 | int ret, i; |
| 228 | uint16_t temp; |
| 229 | |
| 230 | ret = pci_device_load(&s->dev, f); |
| 231 | if (ret < 0) { |
| 232 | return ret; |
| 233 | } |
| 234 | qemu_get_be16s(f, &s->ar.pm1.evt.sts); |
| 235 | qemu_get_be16s(f, &s->ar.pm1.evt.en); |
| 236 | qemu_get_be16s(f, &s->ar.pm1.cnt.cnt); |
| 237 | |
| 238 | ret = vmstate_load_state(f, &vmstate_apm, opaque, 1); |
| 239 | if (ret) { |
| 240 | return ret; |
| 241 | } |
| 242 | |
| 243 | qemu_get_timer(f, s->ar.tmr.timer); |
| 244 | qemu_get_sbe64s(f, &s->ar.tmr.overflow_time); |
| 245 | |
| 246 | qemu_get_be16s(f, (uint16_t *)s->ar.gpe.sts); |
| 247 | for (i = 0; i < 3; i++) { |
| 248 | qemu_get_be16s(f, &temp); |
| 249 | } |
| 250 | |
| 251 | qemu_get_be16s(f, (uint16_t *)s->ar.gpe.en); |
| 252 | for (i = 0; i < 3; i++) { |
| 253 | qemu_get_be16s(f, &temp); |
| 254 | } |
| 255 | |
| 256 | ret = vmstate_load_state(f, &vmstate_pci_status, opaque, 1); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | /* qemu-kvm 1.2 uses version 3 but advertised as 2 |
| 261 | * To support incoming qemu-kvm 1.2 migration, change version_id |
| 262 | * and minimum_version_id to 2 below (which breaks migration from |
| 263 | * qemu 1.2). |
| 264 | * |
| 265 | */ |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 266 | static const VMStateDescription vmstate_acpi = { |
| 267 | .name = "piix4_pm", |
Marcelo Tosatti | b0b873a | 2012-10-30 21:14:19 -0200 | [diff] [blame] | 268 | .version_id = 3, |
| 269 | .minimum_version_id = 3, |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 270 | .minimum_version_id_old = 1, |
Marcelo Tosatti | b0b873a | 2012-10-30 21:14:19 -0200 | [diff] [blame] | 271 | .load_state_old = acpi_load_old, |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 272 | .post_load = vmstate_acpi_post_load, |
| 273 | .fields = (VMStateField []) { |
| 274 | VMSTATE_PCI_DEVICE(dev, PIIX4PMState), |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 275 | VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState), |
| 276 | VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState), |
| 277 | VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState), |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 278 | VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState), |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 279 | VMSTATE_TIMER(ar.tmr.timer, PIIX4PMState), |
| 280 | VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState), |
| 281 | VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE), |
Alex Williamson | 4cf3e6f | 2010-06-02 10:58:29 -0600 | [diff] [blame] | 282 | VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status, |
| 283 | struct pci_status), |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 284 | VMSTATE_END_OF_LIST() |
| 285 | } |
| 286 | }; |
| 287 | |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 288 | static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots) |
| 289 | { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 290 | BusChild *kid, *next; |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 291 | BusState *bus = qdev_get_parent_bus(&s->dev.qdev); |
| 292 | int slot = ffs(slots) - 1; |
Michael S. Tsirkin | 54bfa54 | 2012-04-15 12:00:52 +0300 | [diff] [blame] | 293 | bool slot_free = true; |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 294 | |
| 295 | /* Mark request as complete */ |
| 296 | s->pci0_status.down &= ~(1U << slot); |
| 297 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 298 | QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) { |
| 299 | DeviceState *qdev = kid->child; |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 300 | PCIDevice *dev = PCI_DEVICE(qdev); |
| 301 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); |
Michael S. Tsirkin | 54bfa54 | 2012-04-15 12:00:52 +0300 | [diff] [blame] | 302 | if (PCI_SLOT(dev->devfn) == slot) { |
| 303 | if (pc->no_hotplug) { |
| 304 | slot_free = false; |
| 305 | } else { |
| 306 | qdev_free(qdev); |
| 307 | } |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 308 | } |
| 309 | } |
Michael S. Tsirkin | 54bfa54 | 2012-04-15 12:00:52 +0300 | [diff] [blame] | 310 | if (slot_free) { |
| 311 | s->pci0_slot_device_present &= ~(1U << slot); |
| 312 | } |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 313 | } |
| 314 | |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 315 | static void piix4_update_hotplug(PIIX4PMState *s) |
| 316 | { |
| 317 | PCIDevice *dev = &s->dev; |
| 318 | BusState *bus = qdev_get_parent_bus(&dev->qdev); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 319 | BusChild *kid, *next; |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 320 | |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 321 | /* Execute any pending removes during reset */ |
| 322 | while (s->pci0_status.down) { |
| 323 | acpi_piix_eject_slot(s, s->pci0_status.down); |
| 324 | } |
| 325 | |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 326 | s->pci0_hotplug_enable = ~0; |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 327 | s->pci0_slot_device_present = 0; |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 328 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 329 | QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) { |
| 330 | DeviceState *qdev = kid->child; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 331 | PCIDevice *pdev = PCI_DEVICE(qdev); |
| 332 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev); |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 333 | int slot = PCI_SLOT(pdev->devfn); |
| 334 | |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 335 | if (pc->no_hotplug) { |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 336 | s->pci0_hotplug_enable &= ~(1U << slot); |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 337 | } |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 338 | |
| 339 | s->pci0_slot_device_present |= (1U << slot); |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 343 | static void piix4_reset(void *opaque) |
| 344 | { |
| 345 | PIIX4PMState *s = opaque; |
| 346 | uint8_t *pci_conf = s->dev.config; |
| 347 | |
| 348 | pci_conf[0x58] = 0; |
| 349 | pci_conf[0x59] = 0; |
| 350 | pci_conf[0x5a] = 0; |
| 351 | pci_conf[0x5b] = 0; |
| 352 | |
Gleb Natapov | 4d09d37 | 2012-08-07 15:52:03 +0300 | [diff] [blame] | 353 | pci_conf[0x40] = 0x01; /* PM io base read only bit */ |
| 354 | pci_conf[0x80] = 0; |
| 355 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 356 | if (s->kvm_enabled) { |
| 357 | /* Mark SMM as already inited (until KVM supports SMM). */ |
| 358 | pci_conf[0x5B] = 0x02; |
| 359 | } |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 360 | piix4_update_hotplug(s); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 361 | } |
| 362 | |
Igor Mammedov | d010f91 | 2012-09-05 23:06:22 +0200 | [diff] [blame] | 363 | static void piix4_pm_powerdown_req(Notifier *n, void *opaque) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 364 | { |
Igor Mammedov | d010f91 | 2012-09-05 23:06:22 +0200 | [diff] [blame] | 365 | PIIX4PMState *s = container_of(n, PIIX4PMState, powerdown_notifier); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 366 | |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 367 | assert(s != NULL); |
| 368 | acpi_pm1_evt_power_down(&s->ar); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 369 | } |
| 370 | |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 371 | static void piix4_pm_machine_ready(Notifier *n, void *opaque) |
Paolo Bonzini | 6141dbf | 2011-07-15 17:10:15 +0200 | [diff] [blame] | 372 | { |
| 373 | PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready); |
| 374 | uint8_t *pci_conf; |
| 375 | |
| 376 | pci_conf = s->dev.config; |
| 377 | pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10; |
| 378 | pci_conf[0x63] = 0x60; |
| 379 | pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) | |
| 380 | (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0); |
| 381 | |
| 382 | } |
| 383 | |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 384 | static int piix4_pm_initfn(PCIDevice *dev) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 385 | { |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 386 | PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 387 | uint8_t *pci_conf; |
| 388 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 389 | pci_conf = s->dev.config; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 390 | pci_conf[0x06] = 0x80; |
| 391 | pci_conf[0x07] = 0x02; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 392 | pci_conf[0x09] = 0x00; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 393 | pci_conf[0x3d] = 0x01; // interrupt pin 1 |
| 394 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 395 | /* APM */ |
Julien Grall | 42d8a3c | 2012-09-19 12:50:03 +0100 | [diff] [blame] | 396 | apm_init(dev, &s->apm, apm_ctrl_changed, s); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 397 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 398 | if (s->kvm_enabled) { |
| 399 | /* Mark SMM as already inited to prevent SMM from running. KVM does not |
| 400 | * support SMM mode. */ |
| 401 | pci_conf[0x5B] = 0x02; |
| 402 | } |
| 403 | |
| 404 | /* XXX: which specification is used ? The i82731AB has different |
| 405 | mappings */ |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 406 | pci_conf[0x90] = s->smb_io_base | 1; |
| 407 | pci_conf[0x91] = s->smb_io_base >> 8; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 408 | pci_conf[0xd2] = 0x09; |
Gerd Hoffmann | 798512e | 2012-11-23 14:57:01 +0100 | [diff] [blame] | 409 | pm_smbus_init(&s->dev.qdev, &s->smb); |
Gerd Hoffmann | 24fe083 | 2012-11-23 14:58:04 +0100 | [diff] [blame] | 410 | memory_region_set_enabled(&s->smb.io, pci_conf[0xd2] & 1); |
Gerd Hoffmann | 56e5b2a | 2012-12-11 09:40:45 +0100 | [diff] [blame] | 411 | memory_region_add_subregion(pci_address_space_io(dev), |
| 412 | s->smb_io_base, &s->smb.io); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 413 | |
Gerd Hoffmann | ca5d64b | 2012-11-22 13:27:04 +0100 | [diff] [blame] | 414 | memory_region_init(&s->io, "piix4-pm", 64); |
Gerd Hoffmann | af11110 | 2012-11-22 11:34:44 +0100 | [diff] [blame] | 415 | memory_region_set_enabled(&s->io, false); |
Gerd Hoffmann | 56e5b2a | 2012-12-11 09:40:45 +0100 | [diff] [blame] | 416 | memory_region_add_subregion(pci_address_space_io(dev), |
| 417 | 0, &s->io); |
Gerd Hoffmann | af11110 | 2012-11-22 11:34:44 +0100 | [diff] [blame] | 418 | |
Gerd Hoffmann | 77d58b1 | 2012-11-22 12:12:30 +0100 | [diff] [blame] | 419 | acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io); |
Gerd Hoffmann | b5a7c02 | 2012-11-22 13:25:10 +0100 | [diff] [blame] | 420 | acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io); |
Gerd Hoffmann | afafe4b | 2012-11-22 13:17:57 +0100 | [diff] [blame] | 421 | acpi_pm1_cnt_init(&s->ar, &s->io); |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 422 | acpi_gpe_init(&s->ar, GPE_LEN); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 423 | |
Igor Mammedov | d010f91 | 2012-09-05 23:06:22 +0200 | [diff] [blame] | 424 | s->powerdown_notifier.notify = piix4_pm_powerdown_req; |
| 425 | qemu_register_powerdown_notifier(&s->powerdown_notifier); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 426 | |
Paolo Bonzini | 6141dbf | 2011-07-15 17:10:15 +0200 | [diff] [blame] | 427 | s->machine_ready.notify = piix4_pm_machine_ready; |
| 428 | qemu_add_machine_init_done_notifier(&s->machine_ready); |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 429 | qemu_register_reset(piix4_reset, s); |
Gerd Hoffmann | 56e5b2a | 2012-12-11 09:40:45 +0100 | [diff] [blame] | 430 | |
| 431 | piix4_acpi_system_hot_add_init(pci_address_space_io(dev), dev->bus, s); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 432 | |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base, |
Gerd Hoffmann | da98c8e | 2012-02-23 13:45:20 +0100 | [diff] [blame] | 437 | qemu_irq sci_irq, qemu_irq smi_irq, |
Gleb Natapov | 459ae5e | 2012-06-04 14:31:55 +0300 | [diff] [blame] | 438 | int kvm_enabled, void *fw_cfg) |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 439 | { |
| 440 | PCIDevice *dev; |
| 441 | PIIX4PMState *s; |
| 442 | |
| 443 | dev = pci_create(bus, devfn, "PIIX4_PM"); |
| 444 | qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base); |
| 445 | |
| 446 | s = DO_UPCAST(PIIX4PMState, dev, dev); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 447 | s->irq = sci_irq; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 448 | s->smi_irq = smi_irq; |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 449 | s->kvm_enabled = kvm_enabled; |
| 450 | |
| 451 | qdev_init_nofail(&dev->qdev); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 452 | |
Gleb Natapov | 459ae5e | 2012-06-04 14:31:55 +0300 | [diff] [blame] | 453 | if (fw_cfg) { |
| 454 | uint8_t suspend[6] = {128, 0, 0, 129, 128, 128}; |
| 455 | suspend[3] = 1 | ((!s->disable_s3) << 7); |
| 456 | suspend[4] = s->s4_val | ((!s->disable_s4) << 7); |
| 457 | |
| 458 | fw_cfg_add_file(fw_cfg, "etc/system-states", g_memdup(suspend, 6), 6); |
| 459 | } |
| 460 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 461 | return s->smb.smbus; |
| 462 | } |
| 463 | |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 464 | static Property piix4_pm_properties[] = { |
| 465 | DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0), |
Gleb Natapov | 459ae5e | 2012-06-04 14:31:55 +0300 | [diff] [blame] | 466 | DEFINE_PROP_UINT8("disable_s3", PIIX4PMState, disable_s3, 0), |
| 467 | DEFINE_PROP_UINT8("disable_s4", PIIX4PMState, disable_s4, 0), |
| 468 | DEFINE_PROP_UINT8("s4_val", PIIX4PMState, s4_val, 2), |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 469 | DEFINE_PROP_END_OF_LIST(), |
| 470 | }; |
| 471 | |
| 472 | static void piix4_pm_class_init(ObjectClass *klass, void *data) |
| 473 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 474 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 475 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
| 476 | |
| 477 | k->no_hotplug = 1; |
| 478 | k->init = piix4_pm_initfn; |
| 479 | k->config_write = pm_write_config; |
| 480 | k->vendor_id = PCI_VENDOR_ID_INTEL; |
| 481 | k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3; |
| 482 | k->revision = 0x03; |
| 483 | k->class_id = PCI_CLASS_BRIDGE_OTHER; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 484 | dc->desc = "PM"; |
| 485 | dc->no_user = 1; |
| 486 | dc->vmsd = &vmstate_acpi; |
| 487 | dc->props = piix4_pm_properties; |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 488 | } |
| 489 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 490 | static TypeInfo piix4_pm_info = { |
| 491 | .name = "PIIX4_PM", |
| 492 | .parent = TYPE_PCI_DEVICE, |
| 493 | .instance_size = sizeof(PIIX4PMState), |
| 494 | .class_init = piix4_pm_class_init, |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 495 | }; |
| 496 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 497 | static void piix4_pm_register_types(void) |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 498 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 499 | type_register_static(&piix4_pm_info); |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 500 | } |
| 501 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 502 | type_init(piix4_pm_register_types) |
Isaku Yamahata | e8ec057 | 2010-05-14 16:29:18 +0900 | [diff] [blame] | 503 | |
Gerd Hoffmann | b65b93f | 2012-11-23 15:35:13 +0100 | [diff] [blame] | 504 | static uint64_t gpe_readb(void *opaque, hwaddr addr, unsigned width) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 505 | { |
Gleb Natapov | 633aa0a | 2010-10-17 11:45:25 +0200 | [diff] [blame] | 506 | PIIX4PMState *s = opaque; |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 507 | uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 508 | |
Isaku Yamahata | 50d8ff8 | 2010-05-14 16:29:22 +0900 | [diff] [blame] | 509 | PIIX4_DPRINTF("gpe read %x == %x\n", addr, val); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 510 | return val; |
| 511 | } |
| 512 | |
Gerd Hoffmann | b65b93f | 2012-11-23 15:35:13 +0100 | [diff] [blame] | 513 | static void gpe_writeb(void *opaque, hwaddr addr, uint64_t val, |
| 514 | unsigned width) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 515 | { |
Gleb Natapov | 633aa0a | 2010-10-17 11:45:25 +0200 | [diff] [blame] | 516 | PIIX4PMState *s = opaque; |
Gleb Natapov | 633aa0a | 2010-10-17 11:45:25 +0200 | [diff] [blame] | 517 | |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 518 | acpi_gpe_ioport_writeb(&s->ar, addr, val); |
Gleb Natapov | 633aa0a | 2010-10-17 11:45:25 +0200 | [diff] [blame] | 519 | pm_update_sci(s); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 520 | |
Isaku Yamahata | 50d8ff8 | 2010-05-14 16:29:22 +0900 | [diff] [blame] | 521 | PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 522 | } |
| 523 | |
Gerd Hoffmann | b65b93f | 2012-11-23 15:35:13 +0100 | [diff] [blame] | 524 | static const MemoryRegionOps piix4_gpe_ops = { |
| 525 | .read = gpe_readb, |
| 526 | .write = gpe_writeb, |
| 527 | .valid.min_access_size = 1, |
| 528 | .valid.max_access_size = 4, |
| 529 | .impl.min_access_size = 1, |
| 530 | .impl.max_access_size = 1, |
| 531 | .endianness = DEVICE_LITTLE_ENDIAN, |
| 532 | }; |
| 533 | |
Alex Williamson | ba73754 | 2012-04-05 11:07:08 -0600 | [diff] [blame] | 534 | static uint32_t pci_up_read(void *opaque, uint32_t addr) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 535 | { |
Alex Williamson | ba73754 | 2012-04-05 11:07:08 -0600 | [diff] [blame] | 536 | PIIX4PMState *s = opaque; |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 537 | uint32_t val; |
| 538 | |
| 539 | /* Manufacture an "up" value to cause a device check on any hotplug |
| 540 | * slot with a device. Extra device checks are harmless. */ |
| 541 | val = s->pci0_slot_device_present & s->pci0_hotplug_enable; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 542 | |
Alex Williamson | ba73754 | 2012-04-05 11:07:08 -0600 | [diff] [blame] | 543 | PIIX4_DPRINTF("pci_up_read %x\n", val); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 544 | return val; |
| 545 | } |
| 546 | |
Alex Williamson | ba73754 | 2012-04-05 11:07:08 -0600 | [diff] [blame] | 547 | static uint32_t pci_down_read(void *opaque, uint32_t addr) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 548 | { |
Alex Williamson | ba73754 | 2012-04-05 11:07:08 -0600 | [diff] [blame] | 549 | PIIX4PMState *s = opaque; |
| 550 | uint32_t val = s->pci0_status.down; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 551 | |
Alex Williamson | ba73754 | 2012-04-05 11:07:08 -0600 | [diff] [blame] | 552 | PIIX4_DPRINTF("pci_down_read %x\n", val); |
| 553 | return val; |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 554 | } |
| 555 | |
Alex Williamson | 9290f36 | 2012-04-05 11:07:28 -0600 | [diff] [blame] | 556 | static uint32_t pci_features_read(void *opaque, uint32_t addr) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 557 | { |
Alex Williamson | 9290f36 | 2012-04-05 11:07:28 -0600 | [diff] [blame] | 558 | /* No feature defined yet */ |
| 559 | PIIX4_DPRINTF("pci_features_read %x\n", 0); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static void pciej_write(void *opaque, uint32_t addr, uint32_t val) |
| 564 | { |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 565 | acpi_piix_eject_slot(opaque, val); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 566 | |
Isaku Yamahata | 50d8ff8 | 2010-05-14 16:29:22 +0900 | [diff] [blame] | 567 | PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 568 | } |
| 569 | |
Marcelo Tosatti | 668643b | 2011-01-11 14:20:39 -0200 | [diff] [blame] | 570 | static uint32_t pcirmv_read(void *opaque, uint32_t addr) |
| 571 | { |
| 572 | PIIX4PMState *s = opaque; |
| 573 | |
| 574 | return s->pci0_hotplug_enable; |
| 575 | } |
| 576 | |
Gerd Hoffmann | c177684 | 2012-11-23 16:03:19 +0100 | [diff] [blame] | 577 | static const MemoryRegionOps piix4_pci_ops = { |
| 578 | .old_portio = (MemoryRegionPortio[]) { |
| 579 | { |
| 580 | .offset = PCI_UP_BASE - PCI_HOTPLUG_ADDR, .len = 4, .size = 4, |
| 581 | .read = pci_up_read, |
| 582 | },{ |
| 583 | .offset = PCI_DOWN_BASE - PCI_HOTPLUG_ADDR, .len = 4, .size = 4, |
| 584 | .read = pci_down_read, |
| 585 | },{ |
| 586 | .offset = PCI_EJ_BASE - PCI_HOTPLUG_ADDR, .len = 4, .size = 4, |
| 587 | .read = pci_features_read, |
| 588 | .write = pciej_write, |
| 589 | },{ |
| 590 | .offset = PCI_RMV_BASE - PCI_HOTPLUG_ADDR, .len = 4, .size = 4, |
| 591 | .read = pcirmv_read, |
| 592 | }, |
| 593 | PORTIO_END_OF_LIST() |
| 594 | }, |
| 595 | .endianness = DEVICE_LITTLE_ENDIAN, |
| 596 | }; |
| 597 | |
Michael S. Tsirkin | 4cff0a5 | 2010-11-12 16:21:35 +0900 | [diff] [blame] | 598 | static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev, |
| 599 | PCIHotplugState state); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 600 | |
Gerd Hoffmann | 56e5b2a | 2012-12-11 09:40:45 +0100 | [diff] [blame] | 601 | static void piix4_acpi_system_hot_add_init(MemoryRegion *parent, |
| 602 | PCIBus *bus, PIIX4PMState *s) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 603 | { |
Gerd Hoffmann | b65b93f | 2012-11-23 15:35:13 +0100 | [diff] [blame] | 604 | memory_region_init_io(&s->io_gpe, &piix4_gpe_ops, s, "apci-gpe0", |
| 605 | GPE_LEN); |
Gerd Hoffmann | 56e5b2a | 2012-12-11 09:40:45 +0100 | [diff] [blame] | 606 | memory_region_add_subregion(parent, GPE_BASE, &s->io_gpe); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 607 | |
Gerd Hoffmann | c177684 | 2012-11-23 16:03:19 +0100 | [diff] [blame] | 608 | memory_region_init_io(&s->io_pci, &piix4_pci_ops, s, "apci-pci-hotplug", |
| 609 | PCI_HOTPLUG_SIZE); |
Gerd Hoffmann | 56e5b2a | 2012-12-11 09:40:45 +0100 | [diff] [blame] | 610 | memory_region_add_subregion(parent, PCI_HOTPLUG_ADDR, |
Gerd Hoffmann | c177684 | 2012-11-23 16:03:19 +0100 | [diff] [blame] | 611 | &s->io_pci); |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 612 | pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 613 | } |
| 614 | |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 615 | static void enable_device(PIIX4PMState *s, int slot) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 616 | { |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 617 | s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS; |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 618 | s->pci0_slot_device_present |= (1U << slot); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 619 | } |
| 620 | |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 621 | static void disable_device(PIIX4PMState *s, int slot) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 622 | { |
Gerd Hoffmann | 355bf2e | 2012-02-23 13:45:16 +0100 | [diff] [blame] | 623 | s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS; |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 624 | s->pci0_status.down |= (1U << slot); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 625 | } |
| 626 | |
Michael S. Tsirkin | 4cff0a5 | 2010-11-12 16:21:35 +0900 | [diff] [blame] | 627 | static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev, |
| 628 | PCIHotplugState state) |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 629 | { |
| 630 | int slot = PCI_SLOT(dev->devfn); |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 631 | PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, |
Anthony Liguori | 40021f0 | 2011-12-04 12:22:06 -0600 | [diff] [blame] | 632 | PCI_DEVICE(qdev)); |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 633 | |
Michael S. Tsirkin | 4cff0a5 | 2010-11-12 16:21:35 +0900 | [diff] [blame] | 634 | /* Don't send event when device is enabled during qemu machine creation: |
| 635 | * it is present on boot, no hotplug event is necessary. We do send an |
| 636 | * event when the device is disabled later. */ |
| 637 | if (state == PCI_COLDPLUG_ENABLED) { |
Alex Williamson | 7faa807 | 2012-04-05 11:07:15 -0600 | [diff] [blame] | 638 | s->pci0_slot_device_present |= (1U << slot); |
Isaku Yamahata | 5beb8ad | 2010-09-06 16:46:18 +0900 | [diff] [blame] | 639 | return 0; |
Michael S. Tsirkin | 4cff0a5 | 2010-11-12 16:21:35 +0900 | [diff] [blame] | 640 | } |
Isaku Yamahata | 5beb8ad | 2010-09-06 16:46:18 +0900 | [diff] [blame] | 641 | |
Michael S. Tsirkin | 4cff0a5 | 2010-11-12 16:21:35 +0900 | [diff] [blame] | 642 | if (state == PCI_HOTPLUG_ENABLED) { |
Isaku Yamahata | ac40409 | 2010-05-14 16:29:20 +0900 | [diff] [blame] | 643 | enable_device(s, slot); |
| 644 | } else { |
| 645 | disable_device(s, slot); |
| 646 | } |
Gleb Natapov | 633aa0a | 2010-10-17 11:45:25 +0200 | [diff] [blame] | 647 | |
| 648 | pm_update_sci(s); |
| 649 | |
Isaku Yamahata | 93d89f6 | 2010-05-14 16:29:02 +0900 | [diff] [blame] | 650 | return 0; |
| 651 | } |