]>
Commit | Line | Data |
---|---|---|
93d89f63 IY |
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/> | |
6b620ca3 PB |
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. | |
93d89f63 | 20 | */ |
83c9f4ca | 21 | #include "hw/hw.h" |
0d09e41a PB |
22 | #include "hw/i386/pc.h" |
23 | #include "hw/isa/apm.h" | |
24 | #include "hw/i2c/pm_smbus.h" | |
83c9f4ca | 25 | #include "hw/pci/pci.h" |
0d09e41a | 26 | #include "hw/acpi/acpi.h" |
9c17d615 | 27 | #include "sysemu/sysemu.h" |
1de7afc9 | 28 | #include "qemu/range.h" |
022c62cb | 29 | #include "exec/ioport.h" |
0d09e41a | 30 | #include "hw/nvram/fw_cfg.h" |
022c62cb | 31 | #include "exec/address-spaces.h" |
277e9340 | 32 | #include "hw/acpi/piix4.h" |
93d89f63 IY |
33 | |
34 | //#define DEBUG | |
35 | ||
50d8ff8b IY |
36 | #ifdef DEBUG |
37 | # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__) | |
38 | #else | |
39 | # define PIIX4_DPRINTF(format, ...) do { } while (0) | |
40 | #endif | |
41 | ||
ac404095 | 42 | #define GPE_BASE 0xafe0 |
23910d3f | 43 | #define GPE_LEN 4 |
c177684c GH |
44 | |
45 | #define PCI_HOTPLUG_ADDR 0xae00 | |
46 | #define PCI_HOTPLUG_SIZE 0x000f | |
ba737541 AW |
47 | #define PCI_UP_BASE 0xae00 |
48 | #define PCI_DOWN_BASE 0xae04 | |
ac404095 | 49 | #define PCI_EJ_BASE 0xae08 |
668643b0 | 50 | #define PCI_RMV_BASE 0xae0c |
ac404095 | 51 | |
b8622725 IM |
52 | #define PIIX4_PROC_BASE 0xaf00 |
53 | #define PIIX4_PROC_LEN 32 | |
54 | ||
4441a287 | 55 | #define PIIX4_PCI_HOTPLUG_STATUS 2 |
b8622725 | 56 | #define PIIX4_CPU_HOTPLUG_STATUS 4 |
4441a287 | 57 | |
ac404095 | 58 | struct pci_status { |
7faa8075 | 59 | uint32_t up; /* deprecated, maintained for migration compatibility */ |
ac404095 IY |
60 | uint32_t down; |
61 | }; | |
62 | ||
b8622725 IM |
63 | typedef struct CPUStatus { |
64 | uint8_t sts[PIIX4_PROC_LEN]; | |
65 | } CPUStatus; | |
66 | ||
93d89f63 | 67 | typedef struct PIIX4PMState { |
6a6b5580 AF |
68 | /*< private >*/ |
69 | PCIDevice parent_obj; | |
70 | /*< public >*/ | |
56e5b2a1 | 71 | |
af11110b | 72 | MemoryRegion io; |
277e9340 MT |
73 | uint32_t io_base; |
74 | ||
b65b93f2 | 75 | MemoryRegion io_gpe; |
c177684c | 76 | MemoryRegion io_pci; |
b8622725 | 77 | MemoryRegion io_cpu; |
355bf2e5 | 78 | ACPIREGS ar; |
93d89f63 IY |
79 | |
80 | APMState apm; | |
81 | ||
93d89f63 | 82 | PMSMBus smb; |
e8ec0571 | 83 | uint32_t smb_io_base; |
93d89f63 IY |
84 | |
85 | qemu_irq irq; | |
93d89f63 IY |
86 | qemu_irq smi_irq; |
87 | int kvm_enabled; | |
6141dbfe | 88 | Notifier machine_ready; |
d010f91c | 89 | Notifier powerdown_notifier; |
ac404095 IY |
90 | |
91 | /* for pci hotplug */ | |
ac404095 | 92 | struct pci_status pci0_status; |
668643b0 | 93 | uint32_t pci0_hotplug_enable; |
7faa8075 | 94 | uint32_t pci0_slot_device_present; |
459ae5ea GN |
95 | |
96 | uint8_t disable_s3; | |
97 | uint8_t disable_s4; | |
98 | uint8_t s4_val; | |
b8622725 IM |
99 | |
100 | CPUStatus gpe_cpu; | |
101 | Notifier cpu_added_notifier; | |
93d89f63 IY |
102 | } PIIX4PMState; |
103 | ||
74e445f6 PC |
104 | #define TYPE_PIIX4_PM "PIIX4_PM" |
105 | ||
106 | #define PIIX4_PM(obj) \ | |
107 | OBJECT_CHECK(PIIX4PMState, (obj), TYPE_PIIX4_PM) | |
108 | ||
56e5b2a1 GH |
109 | static void piix4_acpi_system_hot_add_init(MemoryRegion *parent, |
110 | PCIBus *bus, PIIX4PMState *s); | |
ac404095 | 111 | |
93d89f63 IY |
112 | #define ACPI_ENABLE 0xf1 |
113 | #define ACPI_DISABLE 0xf0 | |
114 | ||
93d89f63 IY |
115 | static void pm_update_sci(PIIX4PMState *s) |
116 | { | |
117 | int sci_level, pmsts; | |
93d89f63 | 118 | |
2886be1b | 119 | pmsts = acpi_pm1_evt_get_sts(&s->ar); |
355bf2e5 | 120 | sci_level = (((pmsts & s->ar.pm1.evt.en) & |
93d89f63 IY |
121 | (ACPI_BITMASK_RT_CLOCK_ENABLE | |
122 | ACPI_BITMASK_POWER_BUTTON_ENABLE | | |
123 | ACPI_BITMASK_GLOBAL_LOCK_ENABLE | | |
633aa0ac | 124 | ACPI_BITMASK_TIMER_ENABLE)) != 0) || |
e4f308bb | 125 | ((s->ar.gpe.sts[0] & s->ar.gpe.en[0]) != 0); |
633aa0ac | 126 | |
93d89f63 IY |
127 | qemu_set_irq(s->irq, sci_level); |
128 | /* schedule a timer interruption if needed */ | |
355bf2e5 | 129 | acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) && |
a54d41a8 | 130 | !(pmsts & ACPI_BITMASK_TIMER_STATUS)); |
93d89f63 IY |
131 | } |
132 | ||
355bf2e5 | 133 | static void pm_tmr_timer(ACPIREGS *ar) |
93d89f63 | 134 | { |
355bf2e5 | 135 | PIIX4PMState *s = container_of(ar, PIIX4PMState, ar); |
93d89f63 IY |
136 | pm_update_sci(s); |
137 | } | |
138 | ||
93d89f63 IY |
139 | static void apm_ctrl_changed(uint32_t val, void *arg) |
140 | { | |
141 | PIIX4PMState *s = arg; | |
6a6b5580 | 142 | PCIDevice *d = PCI_DEVICE(s); |
93d89f63 IY |
143 | |
144 | /* ACPI specs 3.0, 4.7.2.5 */ | |
355bf2e5 | 145 | acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE); |
93d89f63 | 146 | |
6a6b5580 | 147 | if (d->config[0x5b] & (1 << 1)) { |
93d89f63 IY |
148 | if (s->smi_irq) { |
149 | qemu_irq_raise(s->smi_irq); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
93d89f63 IY |
154 | static void pm_io_space_update(PIIX4PMState *s) |
155 | { | |
6a6b5580 | 156 | PCIDevice *d = PCI_DEVICE(s); |
93d89f63 | 157 | |
277e9340 MT |
158 | s->io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x40)); |
159 | s->io_base &= 0xffc0; | |
93d89f63 | 160 | |
af11110b | 161 | memory_region_transaction_begin(); |
6a6b5580 | 162 | memory_region_set_enabled(&s->io, d->config[0x80] & 1); |
277e9340 | 163 | memory_region_set_address(&s->io, s->io_base); |
af11110b | 164 | memory_region_transaction_commit(); |
93d89f63 IY |
165 | } |
166 | ||
24fe083d GH |
167 | static void smbus_io_space_update(PIIX4PMState *s) |
168 | { | |
6a6b5580 AF |
169 | PCIDevice *d = PCI_DEVICE(s); |
170 | ||
171 | s->smb_io_base = le32_to_cpu(*(uint32_t *)(d->config + 0x90)); | |
24fe083d GH |
172 | s->smb_io_base &= 0xffc0; |
173 | ||
174 | memory_region_transaction_begin(); | |
6a6b5580 | 175 | memory_region_set_enabled(&s->smb.io, d->config[0xd2] & 1); |
24fe083d GH |
176 | memory_region_set_address(&s->smb.io, s->smb_io_base); |
177 | memory_region_transaction_commit(); | |
93d89f63 IY |
178 | } |
179 | ||
180 | static void pm_write_config(PCIDevice *d, | |
181 | uint32_t address, uint32_t val, int len) | |
182 | { | |
183 | pci_default_write_config(d, address, val, len); | |
24fe083d GH |
184 | if (range_covers_byte(address, len, 0x80) || |
185 | ranges_overlap(address, len, 0x40, 4)) { | |
93d89f63 | 186 | pm_io_space_update((PIIX4PMState *)d); |
24fe083d GH |
187 | } |
188 | if (range_covers_byte(address, len, 0xd2) || | |
189 | ranges_overlap(address, len, 0x90, 4)) { | |
190 | smbus_io_space_update((PIIX4PMState *)d); | |
191 | } | |
93d89f63 IY |
192 | } |
193 | ||
7faa8075 AW |
194 | static void vmstate_pci_status_pre_save(void *opaque) |
195 | { | |
196 | struct pci_status *pci0_status = opaque; | |
197 | PIIX4PMState *s = container_of(pci0_status, PIIX4PMState, pci0_status); | |
198 | ||
199 | /* We no longer track up, so build a safe value for migrating | |
200 | * to a version that still does... of course these might get lost | |
201 | * by an old buggy implementation, but we try. */ | |
202 | pci0_status->up = s->pci0_slot_device_present & s->pci0_hotplug_enable; | |
203 | } | |
204 | ||
93d89f63 IY |
205 | static int vmstate_acpi_post_load(void *opaque, int version_id) |
206 | { | |
207 | PIIX4PMState *s = opaque; | |
208 | ||
209 | pm_io_space_update(s); | |
210 | return 0; | |
211 | } | |
212 | ||
23910d3f IY |
213 | #define VMSTATE_GPE_ARRAY(_field, _state) \ |
214 | { \ | |
215 | .name = (stringify(_field)), \ | |
216 | .version_id = 0, \ | |
23910d3f IY |
217 | .info = &vmstate_info_uint16, \ |
218 | .size = sizeof(uint16_t), \ | |
b0b873a0 | 219 | .flags = VMS_SINGLE | VMS_POINTER, \ |
23910d3f IY |
220 | .offset = vmstate_offset_pointer(_state, _field, uint8_t), \ |
221 | } | |
222 | ||
4cf3e6f3 AW |
223 | static const VMStateDescription vmstate_gpe = { |
224 | .name = "gpe", | |
225 | .version_id = 1, | |
226 | .minimum_version_id = 1, | |
227 | .minimum_version_id_old = 1, | |
228 | .fields = (VMStateField []) { | |
23910d3f IY |
229 | VMSTATE_GPE_ARRAY(sts, ACPIGPE), |
230 | VMSTATE_GPE_ARRAY(en, ACPIGPE), | |
4cf3e6f3 AW |
231 | VMSTATE_END_OF_LIST() |
232 | } | |
233 | }; | |
234 | ||
235 | static const VMStateDescription vmstate_pci_status = { | |
236 | .name = "pci_status", | |
237 | .version_id = 1, | |
238 | .minimum_version_id = 1, | |
239 | .minimum_version_id_old = 1, | |
7faa8075 | 240 | .pre_save = vmstate_pci_status_pre_save, |
4cf3e6f3 AW |
241 | .fields = (VMStateField []) { |
242 | VMSTATE_UINT32(up, struct pci_status), | |
243 | VMSTATE_UINT32(down, struct pci_status), | |
244 | VMSTATE_END_OF_LIST() | |
245 | } | |
246 | }; | |
247 | ||
b0b873a0 MT |
248 | static int acpi_load_old(QEMUFile *f, void *opaque, int version_id) |
249 | { | |
250 | PIIX4PMState *s = opaque; | |
251 | int ret, i; | |
252 | uint16_t temp; | |
253 | ||
6a6b5580 | 254 | ret = pci_device_load(PCI_DEVICE(s), f); |
b0b873a0 MT |
255 | if (ret < 0) { |
256 | return ret; | |
257 | } | |
258 | qemu_get_be16s(f, &s->ar.pm1.evt.sts); | |
259 | qemu_get_be16s(f, &s->ar.pm1.evt.en); | |
260 | qemu_get_be16s(f, &s->ar.pm1.cnt.cnt); | |
261 | ||
ded67782 | 262 | ret = vmstate_load_state(f, &vmstate_apm, &s->apm, 1); |
b0b873a0 MT |
263 | if (ret) { |
264 | return ret; | |
265 | } | |
266 | ||
40daca54 | 267 | timer_get(f, s->ar.tmr.timer); |
b0b873a0 MT |
268 | qemu_get_sbe64s(f, &s->ar.tmr.overflow_time); |
269 | ||
270 | qemu_get_be16s(f, (uint16_t *)s->ar.gpe.sts); | |
271 | for (i = 0; i < 3; i++) { | |
272 | qemu_get_be16s(f, &temp); | |
273 | } | |
274 | ||
275 | qemu_get_be16s(f, (uint16_t *)s->ar.gpe.en); | |
276 | for (i = 0; i < 3; i++) { | |
277 | qemu_get_be16s(f, &temp); | |
278 | } | |
279 | ||
ded67782 | 280 | ret = vmstate_load_state(f, &vmstate_pci_status, &s->pci0_status, 1); |
b0b873a0 MT |
281 | return ret; |
282 | } | |
283 | ||
284 | /* qemu-kvm 1.2 uses version 3 but advertised as 2 | |
285 | * To support incoming qemu-kvm 1.2 migration, change version_id | |
286 | * and minimum_version_id to 2 below (which breaks migration from | |
287 | * qemu 1.2). | |
288 | * | |
289 | */ | |
93d89f63 IY |
290 | static const VMStateDescription vmstate_acpi = { |
291 | .name = "piix4_pm", | |
b0b873a0 MT |
292 | .version_id = 3, |
293 | .minimum_version_id = 3, | |
93d89f63 | 294 | .minimum_version_id_old = 1, |
b0b873a0 | 295 | .load_state_old = acpi_load_old, |
93d89f63 IY |
296 | .post_load = vmstate_acpi_post_load, |
297 | .fields = (VMStateField []) { | |
6a6b5580 | 298 | VMSTATE_PCI_DEVICE(parent_obj, PIIX4PMState), |
355bf2e5 GH |
299 | VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState), |
300 | VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState), | |
301 | VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState), | |
93d89f63 | 302 | VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState), |
355bf2e5 GH |
303 | VMSTATE_TIMER(ar.tmr.timer, PIIX4PMState), |
304 | VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState), | |
305 | VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE), | |
4cf3e6f3 AW |
306 | VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status, |
307 | struct pci_status), | |
93d89f63 IY |
308 | VMSTATE_END_OF_LIST() |
309 | } | |
310 | }; | |
311 | ||
7faa8075 AW |
312 | static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots) |
313 | { | |
0866aca1 | 314 | BusChild *kid, *next; |
74e445f6 | 315 | BusState *bus = qdev_get_parent_bus(DEVICE(s)); |
7faa8075 | 316 | int slot = ffs(slots) - 1; |
54bfa546 | 317 | bool slot_free = true; |
7faa8075 AW |
318 | |
319 | /* Mark request as complete */ | |
320 | s->pci0_status.down &= ~(1U << slot); | |
321 | ||
0866aca1 AL |
322 | QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) { |
323 | DeviceState *qdev = kid->child; | |
7faa8075 AW |
324 | PCIDevice *dev = PCI_DEVICE(qdev); |
325 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); | |
54bfa546 MT |
326 | if (PCI_SLOT(dev->devfn) == slot) { |
327 | if (pc->no_hotplug) { | |
328 | slot_free = false; | |
329 | } else { | |
02a5c4c9 | 330 | object_unparent(OBJECT(qdev)); |
54bfa546 | 331 | } |
7faa8075 AW |
332 | } |
333 | } | |
54bfa546 MT |
334 | if (slot_free) { |
335 | s->pci0_slot_device_present &= ~(1U << slot); | |
336 | } | |
7faa8075 AW |
337 | } |
338 | ||
668643b0 MT |
339 | static void piix4_update_hotplug(PIIX4PMState *s) |
340 | { | |
74e445f6 | 341 | BusState *bus = qdev_get_parent_bus(DEVICE(s)); |
0866aca1 | 342 | BusChild *kid, *next; |
668643b0 | 343 | |
7faa8075 AW |
344 | /* Execute any pending removes during reset */ |
345 | while (s->pci0_status.down) { | |
346 | acpi_piix_eject_slot(s, s->pci0_status.down); | |
347 | } | |
348 | ||
668643b0 | 349 | s->pci0_hotplug_enable = ~0; |
7faa8075 | 350 | s->pci0_slot_device_present = 0; |
668643b0 | 351 | |
0866aca1 AL |
352 | QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) { |
353 | DeviceState *qdev = kid->child; | |
40021f08 AL |
354 | PCIDevice *pdev = PCI_DEVICE(qdev); |
355 | PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev); | |
668643b0 MT |
356 | int slot = PCI_SLOT(pdev->devfn); |
357 | ||
40021f08 | 358 | if (pc->no_hotplug) { |
7faa8075 | 359 | s->pci0_hotplug_enable &= ~(1U << slot); |
668643b0 | 360 | } |
7faa8075 AW |
361 | |
362 | s->pci0_slot_device_present |= (1U << slot); | |
668643b0 MT |
363 | } |
364 | } | |
365 | ||
93d89f63 IY |
366 | static void piix4_reset(void *opaque) |
367 | { | |
368 | PIIX4PMState *s = opaque; | |
6a6b5580 AF |
369 | PCIDevice *d = PCI_DEVICE(s); |
370 | uint8_t *pci_conf = d->config; | |
93d89f63 IY |
371 | |
372 | pci_conf[0x58] = 0; | |
373 | pci_conf[0x59] = 0; | |
374 | pci_conf[0x5a] = 0; | |
375 | pci_conf[0x5b] = 0; | |
376 | ||
4d09d37c GN |
377 | pci_conf[0x40] = 0x01; /* PM io base read only bit */ |
378 | pci_conf[0x80] = 0; | |
379 | ||
93d89f63 IY |
380 | if (s->kvm_enabled) { |
381 | /* Mark SMM as already inited (until KVM supports SMM). */ | |
382 | pci_conf[0x5B] = 0x02; | |
383 | } | |
c046e8c4 | 384 | pm_io_space_update(s); |
668643b0 | 385 | piix4_update_hotplug(s); |
93d89f63 IY |
386 | } |
387 | ||
d010f91c | 388 | static void piix4_pm_powerdown_req(Notifier *n, void *opaque) |
93d89f63 | 389 | { |
d010f91c | 390 | PIIX4PMState *s = container_of(n, PIIX4PMState, powerdown_notifier); |
93d89f63 | 391 | |
355bf2e5 GH |
392 | assert(s != NULL); |
393 | acpi_pm1_evt_power_down(&s->ar); | |
93d89f63 IY |
394 | } |
395 | ||
9e8dd451 | 396 | static void piix4_pm_machine_ready(Notifier *n, void *opaque) |
6141dbfe PB |
397 | { |
398 | PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready); | |
6a6b5580 AF |
399 | PCIDevice *d = PCI_DEVICE(s); |
400 | MemoryRegion *io_as = pci_address_space_io(d); | |
6141dbfe PB |
401 | uint8_t *pci_conf; |
402 | ||
6a6b5580 | 403 | pci_conf = d->config; |
b6f32962 | 404 | pci_conf[0x5f] = 0x10 | |
3ce10901 | 405 | (memory_region_present(io_as, 0x378) ? 0x80 : 0); |
6141dbfe | 406 | pci_conf[0x63] = 0x60; |
3ce10901 PB |
407 | pci_conf[0x67] = (memory_region_present(io_as, 0x3f8) ? 0x08 : 0) | |
408 | (memory_region_present(io_as, 0x2f8) ? 0x90 : 0); | |
6141dbfe PB |
409 | } |
410 | ||
277e9340 MT |
411 | static void piix4_pm_add_propeties(PIIX4PMState *s) |
412 | { | |
413 | static const uint8_t acpi_enable_cmd = ACPI_ENABLE; | |
414 | static const uint8_t acpi_disable_cmd = ACPI_DISABLE; | |
415 | static const uint32_t gpe0_blk = GPE_BASE; | |
416 | static const uint32_t gpe0_blk_len = GPE_LEN; | |
417 | static const uint16_t sci_int = 9; | |
418 | ||
419 | object_property_add_uint8_ptr(OBJECT(s), ACPI_PM_PROP_ACPI_ENABLE_CMD, | |
420 | &acpi_enable_cmd, NULL); | |
421 | object_property_add_uint8_ptr(OBJECT(s), ACPI_PM_PROP_ACPI_DISABLE_CMD, | |
422 | &acpi_disable_cmd, NULL); | |
423 | object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_GPE0_BLK, | |
424 | &gpe0_blk, NULL); | |
425 | object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_GPE0_BLK_LEN, | |
426 | &gpe0_blk_len, NULL); | |
427 | object_property_add_uint16_ptr(OBJECT(s), ACPI_PM_PROP_SCI_INT, | |
428 | &sci_int, NULL); | |
429 | object_property_add_uint32_ptr(OBJECT(s), ACPI_PM_PROP_PM_IO_BASE, | |
430 | &s->io_base, NULL); | |
431 | } | |
432 | ||
e8ec0571 | 433 | static int piix4_pm_initfn(PCIDevice *dev) |
93d89f63 | 434 | { |
74e445f6 | 435 | PIIX4PMState *s = PIIX4_PM(dev); |
93d89f63 IY |
436 | uint8_t *pci_conf; |
437 | ||
6a6b5580 | 438 | pci_conf = dev->config; |
93d89f63 IY |
439 | pci_conf[0x06] = 0x80; |
440 | pci_conf[0x07] = 0x02; | |
93d89f63 | 441 | pci_conf[0x09] = 0x00; |
93d89f63 IY |
442 | pci_conf[0x3d] = 0x01; // interrupt pin 1 |
443 | ||
93d89f63 | 444 | /* APM */ |
42d8a3cf | 445 | apm_init(dev, &s->apm, apm_ctrl_changed, s); |
93d89f63 | 446 | |
93d89f63 IY |
447 | if (s->kvm_enabled) { |
448 | /* Mark SMM as already inited to prevent SMM from running. KVM does not | |
449 | * support SMM mode. */ | |
450 | pci_conf[0x5B] = 0x02; | |
451 | } | |
452 | ||
453 | /* XXX: which specification is used ? The i82731AB has different | |
454 | mappings */ | |
e8ec0571 IY |
455 | pci_conf[0x90] = s->smb_io_base | 1; |
456 | pci_conf[0x91] = s->smb_io_base >> 8; | |
93d89f63 | 457 | pci_conf[0xd2] = 0x09; |
74e445f6 | 458 | pm_smbus_init(DEVICE(dev), &s->smb); |
24fe083d | 459 | memory_region_set_enabled(&s->smb.io, pci_conf[0xd2] & 1); |
56e5b2a1 GH |
460 | memory_region_add_subregion(pci_address_space_io(dev), |
461 | s->smb_io_base, &s->smb.io); | |
93d89f63 | 462 | |
64bde0f3 | 463 | memory_region_init(&s->io, OBJECT(s), "piix4-pm", 64); |
af11110b | 464 | memory_region_set_enabled(&s->io, false); |
56e5b2a1 GH |
465 | memory_region_add_subregion(pci_address_space_io(dev), |
466 | 0, &s->io); | |
93d89f63 | 467 | |
77d58b1e | 468 | acpi_pm_tmr_init(&s->ar, pm_tmr_timer, &s->io); |
b5a7c024 | 469 | acpi_pm1_evt_init(&s->ar, pm_tmr_timer, &s->io); |
560e6396 | 470 | acpi_pm1_cnt_init(&s->ar, &s->io, s->s4_val); |
355bf2e5 | 471 | acpi_gpe_init(&s->ar, GPE_LEN); |
93d89f63 | 472 | |
d010f91c IM |
473 | s->powerdown_notifier.notify = piix4_pm_powerdown_req; |
474 | qemu_register_powerdown_notifier(&s->powerdown_notifier); | |
93d89f63 | 475 | |
6141dbfe PB |
476 | s->machine_ready.notify = piix4_pm_machine_ready; |
477 | qemu_add_machine_init_done_notifier(&s->machine_ready); | |
e8ec0571 | 478 | qemu_register_reset(piix4_reset, s); |
56e5b2a1 GH |
479 | |
480 | piix4_acpi_system_hot_add_init(pci_address_space_io(dev), dev->bus, s); | |
e8ec0571 | 481 | |
277e9340 | 482 | piix4_pm_add_propeties(s); |
e8ec0571 IY |
483 | return 0; |
484 | } | |
485 | ||
277e9340 MT |
486 | Object *piix4_pm_find(void) |
487 | { | |
488 | bool ambig; | |
489 | Object *o = object_resolve_path_type("", TYPE_PIIX4_PM, &ambig); | |
490 | ||
491 | if (ambig || !o) { | |
492 | return NULL; | |
493 | } | |
494 | return o; | |
495 | } | |
496 | ||
e8ec0571 | 497 | i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base, |
da98c8eb | 498 | qemu_irq sci_irq, qemu_irq smi_irq, |
a88b362c | 499 | int kvm_enabled, FWCfgState *fw_cfg) |
e8ec0571 | 500 | { |
74e445f6 | 501 | DeviceState *dev; |
e8ec0571 IY |
502 | PIIX4PMState *s; |
503 | ||
74e445f6 PC |
504 | dev = DEVICE(pci_create(bus, devfn, TYPE_PIIX4_PM)); |
505 | qdev_prop_set_uint32(dev, "smb_io_base", smb_io_base); | |
93d89f63 | 506 | |
74e445f6 | 507 | s = PIIX4_PM(dev); |
93d89f63 | 508 | s->irq = sci_irq; |
93d89f63 | 509 | s->smi_irq = smi_irq; |
e8ec0571 IY |
510 | s->kvm_enabled = kvm_enabled; |
511 | ||
74e445f6 | 512 | qdev_init_nofail(dev); |
93d89f63 | 513 | |
459ae5ea GN |
514 | if (fw_cfg) { |
515 | uint8_t suspend[6] = {128, 0, 0, 129, 128, 128}; | |
516 | suspend[3] = 1 | ((!s->disable_s3) << 7); | |
517 | suspend[4] = s->s4_val | ((!s->disable_s4) << 7); | |
518 | ||
519 | fw_cfg_add_file(fw_cfg, "etc/system-states", g_memdup(suspend, 6), 6); | |
520 | } | |
521 | ||
93d89f63 IY |
522 | return s->smb.smbus; |
523 | } | |
524 | ||
40021f08 AL |
525 | static Property piix4_pm_properties[] = { |
526 | DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0), | |
f854ecc7 MT |
527 | DEFINE_PROP_UINT8(ACPI_PM_PROP_S3_DISABLED, PIIX4PMState, disable_s3, 0), |
528 | DEFINE_PROP_UINT8(ACPI_PM_PROP_S4_DISABLED, PIIX4PMState, disable_s4, 0), | |
529 | DEFINE_PROP_UINT8(ACPI_PM_PROP_S4_VAL, PIIX4PMState, s4_val, 2), | |
40021f08 AL |
530 | DEFINE_PROP_END_OF_LIST(), |
531 | }; | |
532 | ||
533 | static void piix4_pm_class_init(ObjectClass *klass, void *data) | |
534 | { | |
39bffca2 | 535 | DeviceClass *dc = DEVICE_CLASS(klass); |
40021f08 AL |
536 | PCIDeviceClass *k = PCI_DEVICE_CLASS(klass); |
537 | ||
538 | k->no_hotplug = 1; | |
539 | k->init = piix4_pm_initfn; | |
540 | k->config_write = pm_write_config; | |
541 | k->vendor_id = PCI_VENDOR_ID_INTEL; | |
542 | k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3; | |
543 | k->revision = 0x03; | |
544 | k->class_id = PCI_CLASS_BRIDGE_OTHER; | |
39bffca2 AL |
545 | dc->desc = "PM"; |
546 | dc->no_user = 1; | |
547 | dc->vmsd = &vmstate_acpi; | |
548 | dc->props = piix4_pm_properties; | |
40021f08 AL |
549 | } |
550 | ||
8c43a6f0 | 551 | static const TypeInfo piix4_pm_info = { |
74e445f6 | 552 | .name = TYPE_PIIX4_PM, |
39bffca2 AL |
553 | .parent = TYPE_PCI_DEVICE, |
554 | .instance_size = sizeof(PIIX4PMState), | |
555 | .class_init = piix4_pm_class_init, | |
e8ec0571 IY |
556 | }; |
557 | ||
83f7d43a | 558 | static void piix4_pm_register_types(void) |
e8ec0571 | 559 | { |
39bffca2 | 560 | type_register_static(&piix4_pm_info); |
e8ec0571 IY |
561 | } |
562 | ||
83f7d43a | 563 | type_init(piix4_pm_register_types) |
e8ec0571 | 564 | |
b65b93f2 | 565 | static uint64_t gpe_readb(void *opaque, hwaddr addr, unsigned width) |
93d89f63 | 566 | { |
633aa0ac | 567 | PIIX4PMState *s = opaque; |
355bf2e5 | 568 | uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr); |
93d89f63 | 569 | |
ba275adb | 570 | PIIX4_DPRINTF("gpe read %" HWADDR_PRIx " == %" PRIu32 "\n", addr, val); |
93d89f63 IY |
571 | return val; |
572 | } | |
573 | ||
b65b93f2 GH |
574 | static void gpe_writeb(void *opaque, hwaddr addr, uint64_t val, |
575 | unsigned width) | |
93d89f63 | 576 | { |
633aa0ac | 577 | PIIX4PMState *s = opaque; |
633aa0ac | 578 | |
355bf2e5 | 579 | acpi_gpe_ioport_writeb(&s->ar, addr, val); |
633aa0ac | 580 | pm_update_sci(s); |
93d89f63 | 581 | |
ba275adb | 582 | PIIX4_DPRINTF("gpe write %" HWADDR_PRIx " <== %" PRIu64 "\n", addr, val); |
93d89f63 IY |
583 | } |
584 | ||
b65b93f2 GH |
585 | static const MemoryRegionOps piix4_gpe_ops = { |
586 | .read = gpe_readb, | |
587 | .write = gpe_writeb, | |
588 | .valid.min_access_size = 1, | |
589 | .valid.max_access_size = 4, | |
590 | .impl.min_access_size = 1, | |
591 | .impl.max_access_size = 1, | |
592 | .endianness = DEVICE_LITTLE_ENDIAN, | |
593 | }; | |
594 | ||
c3a29809 | 595 | static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) |
93d89f63 | 596 | { |
ba737541 | 597 | PIIX4PMState *s = opaque; |
c3a29809 HP |
598 | uint32_t val = 0; |
599 | ||
600 | switch (addr) { | |
601 | case PCI_UP_BASE - PCI_HOTPLUG_ADDR: | |
602 | /* Manufacture an "up" value to cause a device check on any hotplug | |
603 | * slot with a device. Extra device checks are harmless. */ | |
604 | val = s->pci0_slot_device_present & s->pci0_hotplug_enable; | |
ba275adb | 605 | PIIX4_DPRINTF("pci_up_read %" PRIu32 "\n", val); |
c3a29809 HP |
606 | break; |
607 | case PCI_DOWN_BASE - PCI_HOTPLUG_ADDR: | |
608 | val = s->pci0_status.down; | |
ba275adb | 609 | PIIX4_DPRINTF("pci_down_read %" PRIu32 "\n", val); |
c3a29809 HP |
610 | break; |
611 | case PCI_EJ_BASE - PCI_HOTPLUG_ADDR: | |
612 | /* No feature defined yet */ | |
ba275adb | 613 | PIIX4_DPRINTF("pci_features_read %" PRIu32 "\n", val); |
c3a29809 HP |
614 | break; |
615 | case PCI_RMV_BASE - PCI_HOTPLUG_ADDR: | |
616 | val = s->pci0_hotplug_enable; | |
617 | break; | |
618 | default: | |
619 | break; | |
620 | } | |
ba737541 | 621 | |
ba737541 | 622 | return val; |
93d89f63 IY |
623 | } |
624 | ||
c3a29809 HP |
625 | static void pci_write(void *opaque, hwaddr addr, uint64_t data, |
626 | unsigned int size) | |
93d89f63 | 627 | { |
c3a29809 HP |
628 | switch (addr) { |
629 | case PCI_EJ_BASE - PCI_HOTPLUG_ADDR: | |
630 | acpi_piix_eject_slot(opaque, (uint32_t)data); | |
ba275adb | 631 | PIIX4_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n", |
c3a29809 HP |
632 | addr, data); |
633 | break; | |
634 | default: | |
635 | break; | |
636 | } | |
668643b0 MT |
637 | } |
638 | ||
c177684c | 639 | static const MemoryRegionOps piix4_pci_ops = { |
c3a29809 HP |
640 | .read = pci_read, |
641 | .write = pci_write, | |
c177684c | 642 | .endianness = DEVICE_LITTLE_ENDIAN, |
c3a29809 HP |
643 | .valid = { |
644 | .min_access_size = 4, | |
645 | .max_access_size = 4, | |
646 | }, | |
c177684c GH |
647 | }; |
648 | ||
b8622725 IM |
649 | static uint64_t cpu_status_read(void *opaque, hwaddr addr, unsigned int size) |
650 | { | |
651 | PIIX4PMState *s = opaque; | |
652 | CPUStatus *cpus = &s->gpe_cpu; | |
653 | uint64_t val = cpus->sts[addr]; | |
654 | ||
655 | return val; | |
656 | } | |
657 | ||
658 | static void cpu_status_write(void *opaque, hwaddr addr, uint64_t data, | |
659 | unsigned int size) | |
660 | { | |
661 | /* TODO: implement VCPU removal on guest signal that CPU can be removed */ | |
662 | } | |
663 | ||
664 | static const MemoryRegionOps cpu_hotplug_ops = { | |
665 | .read = cpu_status_read, | |
666 | .write = cpu_status_write, | |
667 | .endianness = DEVICE_LITTLE_ENDIAN, | |
668 | .valid = { | |
669 | .min_access_size = 1, | |
670 | .max_access_size = 1, | |
671 | }, | |
672 | }; | |
673 | ||
674 | typedef enum { | |
675 | PLUG, | |
676 | UNPLUG, | |
677 | } HotplugEventType; | |
678 | ||
679 | static void piix4_cpu_hotplug_req(PIIX4PMState *s, CPUState *cpu, | |
680 | HotplugEventType action) | |
681 | { | |
682 | CPUStatus *g = &s->gpe_cpu; | |
683 | ACPIGPE *gpe = &s->ar.gpe; | |
684 | CPUClass *k = CPU_GET_CLASS(cpu); | |
685 | int64_t cpu_id; | |
686 | ||
687 | assert(s != NULL); | |
688 | ||
689 | *gpe->sts = *gpe->sts | PIIX4_CPU_HOTPLUG_STATUS; | |
690 | cpu_id = k->get_arch_id(CPU(cpu)); | |
691 | if (action == PLUG) { | |
692 | g->sts[cpu_id / 8] |= (1 << (cpu_id % 8)); | |
693 | } else { | |
694 | g->sts[cpu_id / 8] &= ~(1 << (cpu_id % 8)); | |
695 | } | |
696 | pm_update_sci(s); | |
697 | } | |
698 | ||
699 | static void piix4_cpu_added_req(Notifier *n, void *opaque) | |
700 | { | |
701 | PIIX4PMState *s = container_of(n, PIIX4PMState, cpu_added_notifier); | |
702 | ||
703 | piix4_cpu_hotplug_req(s, CPU(opaque), PLUG); | |
704 | } | |
705 | ||
4cff0a59 MT |
706 | static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev, |
707 | PCIHotplugState state); | |
93d89f63 | 708 | |
56e5b2a1 GH |
709 | static void piix4_acpi_system_hot_add_init(MemoryRegion *parent, |
710 | PCIBus *bus, PIIX4PMState *s) | |
93d89f63 | 711 | { |
38fcbd3f AF |
712 | CPUState *cpu; |
713 | ||
64bde0f3 PB |
714 | memory_region_init_io(&s->io_gpe, OBJECT(s), &piix4_gpe_ops, s, |
715 | "acpi-gpe0", GPE_LEN); | |
56e5b2a1 | 716 | memory_region_add_subregion(parent, GPE_BASE, &s->io_gpe); |
ac404095 | 717 | |
64bde0f3 PB |
718 | memory_region_init_io(&s->io_pci, OBJECT(s), &piix4_pci_ops, s, |
719 | "acpi-pci-hotplug", PCI_HOTPLUG_SIZE); | |
56e5b2a1 | 720 | memory_region_add_subregion(parent, PCI_HOTPLUG_ADDR, |
c177684c | 721 | &s->io_pci); |
74e445f6 | 722 | pci_bus_hotplug(bus, piix4_device_hotplug, DEVICE(s)); |
b8622725 | 723 | |
38fcbd3f AF |
724 | CPU_FOREACH(cpu) { |
725 | CPUClass *cc = CPU_GET_CLASS(cpu); | |
726 | int64_t id = cc->get_arch_id(cpu); | |
727 | ||
728 | g_assert((id / 8) < PIIX4_PROC_LEN); | |
729 | s->gpe_cpu.sts[id / 8] |= (1 << (id % 8)); | |
730 | } | |
64bde0f3 PB |
731 | memory_region_init_io(&s->io_cpu, OBJECT(s), &cpu_hotplug_ops, s, |
732 | "acpi-cpu-hotplug", PIIX4_PROC_LEN); | |
b8622725 IM |
733 | memory_region_add_subregion(parent, PIIX4_PROC_BASE, &s->io_cpu); |
734 | s->cpu_added_notifier.notify = piix4_cpu_added_req; | |
735 | qemu_register_cpu_added_notifier(&s->cpu_added_notifier); | |
93d89f63 IY |
736 | } |
737 | ||
ac404095 | 738 | static void enable_device(PIIX4PMState *s, int slot) |
93d89f63 | 739 | { |
355bf2e5 | 740 | s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS; |
7faa8075 | 741 | s->pci0_slot_device_present |= (1U << slot); |
93d89f63 IY |
742 | } |
743 | ||
ac404095 | 744 | static void disable_device(PIIX4PMState *s, int slot) |
93d89f63 | 745 | { |
355bf2e5 | 746 | s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS; |
7faa8075 | 747 | s->pci0_status.down |= (1U << slot); |
93d89f63 IY |
748 | } |
749 | ||
4cff0a59 MT |
750 | static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev, |
751 | PCIHotplugState state) | |
93d89f63 IY |
752 | { |
753 | int slot = PCI_SLOT(dev->devfn); | |
74e445f6 | 754 | PIIX4PMState *s = PIIX4_PM(qdev); |
93d89f63 | 755 | |
4cff0a59 MT |
756 | /* Don't send event when device is enabled during qemu machine creation: |
757 | * it is present on boot, no hotplug event is necessary. We do send an | |
758 | * event when the device is disabled later. */ | |
759 | if (state == PCI_COLDPLUG_ENABLED) { | |
7faa8075 | 760 | s->pci0_slot_device_present |= (1U << slot); |
5beb8ad5 | 761 | return 0; |
4cff0a59 | 762 | } |
5beb8ad5 | 763 | |
4cff0a59 | 764 | if (state == PCI_HOTPLUG_ENABLED) { |
ac404095 IY |
765 | enable_device(s, slot); |
766 | } else { | |
767 | disable_device(s, slot); | |
768 | } | |
633aa0ac GN |
769 | |
770 | pm_update_sci(s); | |
771 | ||
93d89f63 IY |
772 | return 0; |
773 | } |