blob: 15275cf3e52d37611ca75c14a6b41eb82f27bdd3 [file] [log] [blame]
Isaku Yamahata93d89f62010-05-14 16:29:02 +09001/*
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 Bonzini6b620ca2012-01-13 17:44:23 +010017 *
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 Yamahata93d89f62010-05-14 16:29:02 +090020 */
21#include "hw.h"
22#include "pc.h"
23#include "apm.h"
24#include "pm_smbus.h"
25#include "pci.h"
Isaku Yamahata93d89f62010-05-14 16:29:02 +090026#include "acpi.h"
Markus Armbruster666daa62010-06-02 18:48:27 +020027#include "sysemu.h"
Blue Swirlbf1b0072010-09-18 05:53:14 +000028#include "range.h"
Paolo Bonzini6141dbf2011-07-15 17:10:15 +020029#include "ioport.h"
Gleb Natapov459ae5e2012-06-04 14:31:55 +030030#include "fw_cfg.h"
Isaku Yamahata93d89f62010-05-14 16:29:02 +090031
32//#define DEBUG
33
Isaku Yamahata50d8ff82010-05-14 16:29:22 +090034#ifdef DEBUG
35# define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
36#else
37# define PIIX4_DPRINTF(format, ...) do { } while (0)
38#endif
39
Isaku Yamahata93d89f62010-05-14 16:29:02 +090040#define ACPI_DBG_IO_ADDR 0xb044
41
Isaku Yamahataac404092010-05-14 16:29:20 +090042#define GPE_BASE 0xafe0
Isaku Yamahata23910d32011-03-25 19:54:41 +090043#define GPE_LEN 4
Alex Williamsonba737542012-04-05 11:07:08 -060044#define PCI_UP_BASE 0xae00
45#define PCI_DOWN_BASE 0xae04
Isaku Yamahataac404092010-05-14 16:29:20 +090046#define PCI_EJ_BASE 0xae08
Marcelo Tosatti668643b2011-01-11 14:20:39 -020047#define PCI_RMV_BASE 0xae0c
Isaku Yamahataac404092010-05-14 16:29:20 +090048
Gleb Natapov4441a282010-10-17 11:45:24 +020049#define PIIX4_PCI_HOTPLUG_STATUS 2
50
Isaku Yamahataac404092010-05-14 16:29:20 +090051struct pci_status {
Alex Williamson7faa8072012-04-05 11:07:15 -060052 uint32_t up; /* deprecated, maintained for migration compatibility */
Isaku Yamahataac404092010-05-14 16:29:20 +090053 uint32_t down;
54};
55
Isaku Yamahata93d89f62010-05-14 16:29:02 +090056typedef struct PIIX4PMState {
57 PCIDevice dev;
Avi Kivity2871a3f2010-11-17 11:50:10 +020058 IORange ioport;
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +010059 ACPIREGS ar;
Isaku Yamahata93d89f62010-05-14 16:29:02 +090060
61 APMState apm;
62
Isaku Yamahata93d89f62010-05-14 16:29:02 +090063 PMSMBus smb;
Isaku Yamahatae8ec0572010-05-14 16:29:18 +090064 uint32_t smb_io_base;
Isaku Yamahata93d89f62010-05-14 16:29:02 +090065
66 qemu_irq irq;
Isaku Yamahata93d89f62010-05-14 16:29:02 +090067 qemu_irq smi_irq;
68 int kvm_enabled;
Paolo Bonzini6141dbf2011-07-15 17:10:15 +020069 Notifier machine_ready;
Igor Mammedovd010f912012-09-05 23:06:22 +020070 Notifier powerdown_notifier;
Isaku Yamahataac404092010-05-14 16:29:20 +090071
72 /* for pci hotplug */
Isaku Yamahataac404092010-05-14 16:29:20 +090073 struct pci_status pci0_status;
Marcelo Tosatti668643b2011-01-11 14:20:39 -020074 uint32_t pci0_hotplug_enable;
Alex Williamson7faa8072012-04-05 11:07:15 -060075 uint32_t pci0_slot_device_present;
Gleb Natapov459ae5e2012-06-04 14:31:55 +030076
77 uint8_t disable_s3;
78 uint8_t disable_s4;
79 uint8_t s4_val;
Isaku Yamahata93d89f62010-05-14 16:29:02 +090080} PIIX4PMState;
81
Isaku Yamahataac404092010-05-14 16:29:20 +090082static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s);
83
Isaku Yamahata93d89f62010-05-14 16:29:02 +090084#define ACPI_ENABLE 0xf1
85#define ACPI_DISABLE 0xf0
86
Isaku Yamahata93d89f62010-05-14 16:29:02 +090087static void pm_update_sci(PIIX4PMState *s)
88{
89 int sci_level, pmsts;
Isaku Yamahata93d89f62010-05-14 16:29:02 +090090
Gerd Hoffmann2886be12012-02-23 13:45:17 +010091 pmsts = acpi_pm1_evt_get_sts(&s->ar);
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +010092 sci_level = (((pmsts & s->ar.pm1.evt.en) &
Isaku Yamahata93d89f62010-05-14 16:29:02 +090093 (ACPI_BITMASK_RT_CLOCK_ENABLE |
94 ACPI_BITMASK_POWER_BUTTON_ENABLE |
95 ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
Gleb Natapov633aa0a2010-10-17 11:45:25 +020096 ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +010097 (((s->ar.gpe.sts[0] & s->ar.gpe.en[0])
98 & PIIX4_PCI_HOTPLUG_STATUS) != 0);
Gleb Natapov633aa0a2010-10-17 11:45:25 +020099
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900100 qemu_set_irq(s->irq, sci_level);
101 /* schedule a timer interruption if needed */
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100102 acpi_pm_tmr_update(&s->ar, (s->ar.pm1.evt.en & ACPI_BITMASK_TIMER_ENABLE) &&
Isaku Yamahataa54d41a2011-03-25 19:54:38 +0900103 !(pmsts & ACPI_BITMASK_TIMER_STATUS));
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900104}
105
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100106static void pm_tmr_timer(ACPIREGS *ar)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900107{
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100108 PIIX4PMState *s = container_of(ar, PIIX4PMState, ar);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900109 pm_update_sci(s);
110}
111
Avi Kivity2871a3f2010-11-17 11:50:10 +0200112static void pm_ioport_write(IORange *ioport, uint64_t addr, unsigned width,
113 uint64_t val)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900114{
Avi Kivity2871a3f2010-11-17 11:50:10 +0200115 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
116
117 if (width != 2) {
118 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
119 (unsigned)addr, width, (unsigned)val);
120 }
121
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900122 switch(addr) {
123 case 0x00:
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100124 acpi_pm1_evt_write_sts(&s->ar, val);
Isaku Yamahata04dc3082011-03-25 19:54:39 +0900125 pm_update_sci(s);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900126 break;
127 case 0x02:
Gerd Hoffmann8283c4f2012-02-23 13:45:18 +0100128 acpi_pm1_evt_write_en(&s->ar, val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900129 pm_update_sci(s);
130 break;
131 case 0x04:
Gleb Natapov459ae5e2012-06-04 14:31:55 +0300132 acpi_pm1_cnt_write(&s->ar, val, s->s4_val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900133 break;
134 default:
135 break;
136 }
Wen Congyang59df4c12011-02-28 10:22:33 +0800137 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr,
138 (unsigned int)val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900139}
140
Avi Kivity2871a3f2010-11-17 11:50:10 +0200141static void pm_ioport_read(IORange *ioport, uint64_t addr, unsigned width,
142 uint64_t *data)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900143{
Avi Kivity2871a3f2010-11-17 11:50:10 +0200144 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900145 uint32_t val;
146
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900147 switch(addr) {
148 case 0x00:
Gerd Hoffmann2886be12012-02-23 13:45:17 +0100149 val = acpi_pm1_evt_get_sts(&s->ar);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900150 break;
151 case 0x02:
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100152 val = s->ar.pm1.evt.en;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900153 break;
154 case 0x04:
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100155 val = s->ar.pm1.cnt.cnt;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900156 break;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900157 case 0x08:
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100158 val = acpi_pm_tmr_get(&s->ar);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900159 break;
160 default:
161 val = 0;
162 break;
163 }
Wen Congyang59df4c12011-02-28 10:22:33 +0800164 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr, val);
Avi Kivity2871a3f2010-11-17 11:50:10 +0200165 *data = val;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900166}
167
Avi Kivity2871a3f2010-11-17 11:50:10 +0200168static const IORangeOps pm_iorange_ops = {
169 .read = pm_ioport_read,
170 .write = pm_ioport_write,
171};
172
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900173static void apm_ctrl_changed(uint32_t val, void *arg)
174{
175 PIIX4PMState *s = arg;
176
177 /* ACPI specs 3.0, 4.7.2.5 */
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100178 acpi_pm1_cnt_update(&s->ar, val == ACPI_ENABLE, val == ACPI_DISABLE);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900179
180 if (s->dev.config[0x5b] & (1 << 1)) {
181 if (s->smi_irq) {
182 qemu_irq_raise(s->smi_irq);
183 }
184 }
185}
186
187static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
188{
Isaku Yamahata50d8ff82010-05-14 16:29:22 +0900189 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900190}
191
192static void pm_io_space_update(PIIX4PMState *s)
193{
194 uint32_t pm_io_base;
195
196 if (s->dev.config[0x80] & 1) {
197 pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
198 pm_io_base &= 0xffc0;
199
200 /* XXX: need to improve memory and ioport allocation */
Isaku Yamahata50d8ff82010-05-14 16:29:22 +0900201 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base);
Avi Kivity2871a3f2010-11-17 11:50:10 +0200202 iorange_init(&s->ioport, &pm_iorange_ops, pm_io_base, 64);
203 ioport_register(&s->ioport);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900204 }
205}
206
207static void pm_write_config(PCIDevice *d,
208 uint32_t address, uint32_t val, int len)
209{
210 pci_default_write_config(d, address, val, len);
211 if (range_covers_byte(address, len, 0x80))
212 pm_io_space_update((PIIX4PMState *)d);
213}
214
Alex Williamson7faa8072012-04-05 11:07:15 -0600215static void vmstate_pci_status_pre_save(void *opaque)
216{
217 struct pci_status *pci0_status = opaque;
218 PIIX4PMState *s = container_of(pci0_status, PIIX4PMState, pci0_status);
219
220 /* We no longer track up, so build a safe value for migrating
221 * to a version that still does... of course these might get lost
222 * by an old buggy implementation, but we try. */
223 pci0_status->up = s->pci0_slot_device_present & s->pci0_hotplug_enable;
224}
225
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900226static int vmstate_acpi_post_load(void *opaque, int version_id)
227{
228 PIIX4PMState *s = opaque;
229
230 pm_io_space_update(s);
231 return 0;
232}
233
Isaku Yamahata23910d32011-03-25 19:54:41 +0900234#define VMSTATE_GPE_ARRAY(_field, _state) \
235 { \
236 .name = (stringify(_field)), \
237 .version_id = 0, \
238 .num = GPE_LEN, \
239 .info = &vmstate_info_uint16, \
240 .size = sizeof(uint16_t), \
241 .flags = VMS_ARRAY | VMS_POINTER, \
242 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
243 }
244
Alex Williamson4cf3e6f2010-06-02 10:58:29 -0600245static const VMStateDescription vmstate_gpe = {
246 .name = "gpe",
247 .version_id = 1,
248 .minimum_version_id = 1,
249 .minimum_version_id_old = 1,
250 .fields = (VMStateField []) {
Isaku Yamahata23910d32011-03-25 19:54:41 +0900251 VMSTATE_GPE_ARRAY(sts, ACPIGPE),
252 VMSTATE_GPE_ARRAY(en, ACPIGPE),
Alex Williamson4cf3e6f2010-06-02 10:58:29 -0600253 VMSTATE_END_OF_LIST()
254 }
255};
256
257static const VMStateDescription vmstate_pci_status = {
258 .name = "pci_status",
259 .version_id = 1,
260 .minimum_version_id = 1,
261 .minimum_version_id_old = 1,
Alex Williamson7faa8072012-04-05 11:07:15 -0600262 .pre_save = vmstate_pci_status_pre_save,
Alex Williamson4cf3e6f2010-06-02 10:58:29 -0600263 .fields = (VMStateField []) {
264 VMSTATE_UINT32(up, struct pci_status),
265 VMSTATE_UINT32(down, struct pci_status),
266 VMSTATE_END_OF_LIST()
267 }
268};
269
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900270static const VMStateDescription vmstate_acpi = {
271 .name = "piix4_pm",
Alex Williamson4cf3e6f2010-06-02 10:58:29 -0600272 .version_id = 2,
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900273 .minimum_version_id = 1,
274 .minimum_version_id_old = 1,
275 .post_load = vmstate_acpi_post_load,
276 .fields = (VMStateField []) {
277 VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100278 VMSTATE_UINT16(ar.pm1.evt.sts, PIIX4PMState),
279 VMSTATE_UINT16(ar.pm1.evt.en, PIIX4PMState),
280 VMSTATE_UINT16(ar.pm1.cnt.cnt, PIIX4PMState),
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900281 VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100282 VMSTATE_TIMER(ar.tmr.timer, PIIX4PMState),
283 VMSTATE_INT64(ar.tmr.overflow_time, PIIX4PMState),
284 VMSTATE_STRUCT(ar.gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
Alex Williamson4cf3e6f2010-06-02 10:58:29 -0600285 VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
286 struct pci_status),
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900287 VMSTATE_END_OF_LIST()
288 }
289};
290
Alex Williamson7faa8072012-04-05 11:07:15 -0600291static void acpi_piix_eject_slot(PIIX4PMState *s, unsigned slots)
292{
Anthony Liguori0866aca2011-12-23 15:34:39 -0600293 BusChild *kid, *next;
Alex Williamson7faa8072012-04-05 11:07:15 -0600294 BusState *bus = qdev_get_parent_bus(&s->dev.qdev);
295 int slot = ffs(slots) - 1;
Michael S. Tsirkin54bfa542012-04-15 12:00:52 +0300296 bool slot_free = true;
Alex Williamson7faa8072012-04-05 11:07:15 -0600297
298 /* Mark request as complete */
299 s->pci0_status.down &= ~(1U << slot);
300
Anthony Liguori0866aca2011-12-23 15:34:39 -0600301 QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
302 DeviceState *qdev = kid->child;
Alex Williamson7faa8072012-04-05 11:07:15 -0600303 PCIDevice *dev = PCI_DEVICE(qdev);
304 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
Michael S. Tsirkin54bfa542012-04-15 12:00:52 +0300305 if (PCI_SLOT(dev->devfn) == slot) {
306 if (pc->no_hotplug) {
307 slot_free = false;
308 } else {
309 qdev_free(qdev);
310 }
Alex Williamson7faa8072012-04-05 11:07:15 -0600311 }
312 }
Michael S. Tsirkin54bfa542012-04-15 12:00:52 +0300313 if (slot_free) {
314 s->pci0_slot_device_present &= ~(1U << slot);
315 }
Alex Williamson7faa8072012-04-05 11:07:15 -0600316}
317
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200318static void piix4_update_hotplug(PIIX4PMState *s)
319{
320 PCIDevice *dev = &s->dev;
321 BusState *bus = qdev_get_parent_bus(&dev->qdev);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600322 BusChild *kid, *next;
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200323
Alex Williamson7faa8072012-04-05 11:07:15 -0600324 /* Execute any pending removes during reset */
325 while (s->pci0_status.down) {
326 acpi_piix_eject_slot(s, s->pci0_status.down);
327 }
328
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200329 s->pci0_hotplug_enable = ~0;
Alex Williamson7faa8072012-04-05 11:07:15 -0600330 s->pci0_slot_device_present = 0;
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200331
Anthony Liguori0866aca2011-12-23 15:34:39 -0600332 QTAILQ_FOREACH_SAFE(kid, &bus->children, sibling, next) {
333 DeviceState *qdev = kid->child;
Anthony Liguori40021f02011-12-04 12:22:06 -0600334 PCIDevice *pdev = PCI_DEVICE(qdev);
335 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev);
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200336 int slot = PCI_SLOT(pdev->devfn);
337
Anthony Liguori40021f02011-12-04 12:22:06 -0600338 if (pc->no_hotplug) {
Alex Williamson7faa8072012-04-05 11:07:15 -0600339 s->pci0_hotplug_enable &= ~(1U << slot);
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200340 }
Alex Williamson7faa8072012-04-05 11:07:15 -0600341
342 s->pci0_slot_device_present |= (1U << slot);
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200343 }
344}
345
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900346static void piix4_reset(void *opaque)
347{
348 PIIX4PMState *s = opaque;
349 uint8_t *pci_conf = s->dev.config;
350
351 pci_conf[0x58] = 0;
352 pci_conf[0x59] = 0;
353 pci_conf[0x5a] = 0;
354 pci_conf[0x5b] = 0;
355
Gleb Natapov4d09d372012-08-07 15:52:03 +0300356 pci_conf[0x40] = 0x01; /* PM io base read only bit */
357 pci_conf[0x80] = 0;
358
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900359 if (s->kvm_enabled) {
360 /* Mark SMM as already inited (until KVM supports SMM). */
361 pci_conf[0x5B] = 0x02;
362 }
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200363 piix4_update_hotplug(s);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900364}
365
Igor Mammedovd010f912012-09-05 23:06:22 +0200366static void piix4_pm_powerdown_req(Notifier *n, void *opaque)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900367{
Igor Mammedovd010f912012-09-05 23:06:22 +0200368 PIIX4PMState *s = container_of(n, PIIX4PMState, powerdown_notifier);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900369
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100370 assert(s != NULL);
371 acpi_pm1_evt_power_down(&s->ar);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900372}
373
Jan Kiszka9e8dd452011-06-20 14:06:26 +0200374static void piix4_pm_machine_ready(Notifier *n, void *opaque)
Paolo Bonzini6141dbf2011-07-15 17:10:15 +0200375{
376 PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
377 uint8_t *pci_conf;
378
379 pci_conf = s->dev.config;
380 pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
381 pci_conf[0x63] = 0x60;
382 pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
383 (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
384
385}
386
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900387static int piix4_pm_initfn(PCIDevice *dev)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900388{
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900389 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900390 uint8_t *pci_conf;
391
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900392 pci_conf = s->dev.config;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900393 pci_conf[0x06] = 0x80;
394 pci_conf[0x07] = 0x02;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900395 pci_conf[0x09] = 0x00;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900396 pci_conf[0x3d] = 0x01; // interrupt pin 1
397
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900398 /* APM */
399 apm_init(&s->apm, apm_ctrl_changed, s);
400
401 register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
402
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900403 if (s->kvm_enabled) {
404 /* Mark SMM as already inited to prevent SMM from running. KVM does not
405 * support SMM mode. */
406 pci_conf[0x5B] = 0x02;
407 }
408
409 /* XXX: which specification is used ? The i82731AB has different
410 mappings */
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900411 pci_conf[0x90] = s->smb_io_base | 1;
412 pci_conf[0x91] = s->smb_io_base >> 8;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900413 pci_conf[0xd2] = 0x09;
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900414 register_ioport_write(s->smb_io_base, 64, 1, smb_ioport_writeb, &s->smb);
415 register_ioport_read(s->smb_io_base, 64, 1, smb_ioport_readb, &s->smb);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900416
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100417 acpi_pm_tmr_init(&s->ar, pm_tmr_timer);
418 acpi_gpe_init(&s->ar, GPE_LEN);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900419
Igor Mammedovd010f912012-09-05 23:06:22 +0200420 s->powerdown_notifier.notify = piix4_pm_powerdown_req;
421 qemu_register_powerdown_notifier(&s->powerdown_notifier);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900422
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900423 pm_smbus_init(&s->dev.qdev, &s->smb);
Paolo Bonzini6141dbf2011-07-15 17:10:15 +0200424 s->machine_ready.notify = piix4_pm_machine_ready;
425 qemu_add_machine_init_done_notifier(&s->machine_ready);
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900426 qemu_register_reset(piix4_reset, s);
Isaku Yamahataac404092010-05-14 16:29:20 +0900427 piix4_acpi_system_hot_add_init(dev->bus, s);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900428
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900429 return 0;
430}
431
432i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
Gerd Hoffmannda98c8e2012-02-23 13:45:20 +0100433 qemu_irq sci_irq, qemu_irq smi_irq,
Gleb Natapov459ae5e2012-06-04 14:31:55 +0300434 int kvm_enabled, void *fw_cfg)
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900435{
436 PCIDevice *dev;
437 PIIX4PMState *s;
438
439 dev = pci_create(bus, devfn, "PIIX4_PM");
440 qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
441
442 s = DO_UPCAST(PIIX4PMState, dev, dev);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900443 s->irq = sci_irq;
Gerd Hoffmannda98c8e2012-02-23 13:45:20 +0100444 acpi_pm1_cnt_init(&s->ar);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900445 s->smi_irq = smi_irq;
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900446 s->kvm_enabled = kvm_enabled;
447
448 qdev_init_nofail(&dev->qdev);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900449
Gleb Natapov459ae5e2012-06-04 14:31:55 +0300450 if (fw_cfg) {
451 uint8_t suspend[6] = {128, 0, 0, 129, 128, 128};
452 suspend[3] = 1 | ((!s->disable_s3) << 7);
453 suspend[4] = s->s4_val | ((!s->disable_s4) << 7);
454
455 fw_cfg_add_file(fw_cfg, "etc/system-states", g_memdup(suspend, 6), 6);
456 }
457
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900458 return s->smb.smbus;
459}
460
Anthony Liguori40021f02011-12-04 12:22:06 -0600461static Property piix4_pm_properties[] = {
462 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
Gleb Natapov459ae5e2012-06-04 14:31:55 +0300463 DEFINE_PROP_UINT8("disable_s3", PIIX4PMState, disable_s3, 0),
464 DEFINE_PROP_UINT8("disable_s4", PIIX4PMState, disable_s4, 0),
465 DEFINE_PROP_UINT8("s4_val", PIIX4PMState, s4_val, 2),
Anthony Liguori40021f02011-12-04 12:22:06 -0600466 DEFINE_PROP_END_OF_LIST(),
467};
468
469static void piix4_pm_class_init(ObjectClass *klass, void *data)
470{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600471 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori40021f02011-12-04 12:22:06 -0600472 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
473
474 k->no_hotplug = 1;
475 k->init = piix4_pm_initfn;
476 k->config_write = pm_write_config;
477 k->vendor_id = PCI_VENDOR_ID_INTEL;
478 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
479 k->revision = 0x03;
480 k->class_id = PCI_CLASS_BRIDGE_OTHER;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600481 dc->desc = "PM";
482 dc->no_user = 1;
483 dc->vmsd = &vmstate_acpi;
484 dc->props = piix4_pm_properties;
Anthony Liguori40021f02011-12-04 12:22:06 -0600485}
486
Anthony Liguori39bffca2011-12-07 21:34:16 -0600487static TypeInfo piix4_pm_info = {
488 .name = "PIIX4_PM",
489 .parent = TYPE_PCI_DEVICE,
490 .instance_size = sizeof(PIIX4PMState),
491 .class_init = piix4_pm_class_init,
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900492};
493
Andreas Färber83f7d432012-02-09 15:20:55 +0100494static void piix4_pm_register_types(void)
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900495{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600496 type_register_static(&piix4_pm_info);
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900497}
498
Andreas Färber83f7d432012-02-09 15:20:55 +0100499type_init(piix4_pm_register_types)
Isaku Yamahatae8ec0572010-05-14 16:29:18 +0900500
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900501static uint32_t gpe_readb(void *opaque, uint32_t addr)
502{
Gleb Natapov633aa0a2010-10-17 11:45:25 +0200503 PIIX4PMState *s = opaque;
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100504 uint32_t val = acpi_gpe_ioport_readb(&s->ar, addr);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900505
Isaku Yamahata50d8ff82010-05-14 16:29:22 +0900506 PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900507 return val;
508}
509
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900510static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
511{
Gleb Natapov633aa0a2010-10-17 11:45:25 +0200512 PIIX4PMState *s = opaque;
Gleb Natapov633aa0a2010-10-17 11:45:25 +0200513
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100514 acpi_gpe_ioport_writeb(&s->ar, addr, val);
Gleb Natapov633aa0a2010-10-17 11:45:25 +0200515 pm_update_sci(s);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900516
Isaku Yamahata50d8ff82010-05-14 16:29:22 +0900517 PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900518}
519
Alex Williamsonba737542012-04-05 11:07:08 -0600520static uint32_t pci_up_read(void *opaque, uint32_t addr)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900521{
Alex Williamsonba737542012-04-05 11:07:08 -0600522 PIIX4PMState *s = opaque;
Alex Williamson7faa8072012-04-05 11:07:15 -0600523 uint32_t val;
524
525 /* Manufacture an "up" value to cause a device check on any hotplug
526 * slot with a device. Extra device checks are harmless. */
527 val = s->pci0_slot_device_present & s->pci0_hotplug_enable;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900528
Alex Williamsonba737542012-04-05 11:07:08 -0600529 PIIX4_DPRINTF("pci_up_read %x\n", val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900530 return val;
531}
532
Alex Williamsonba737542012-04-05 11:07:08 -0600533static uint32_t pci_down_read(void *opaque, uint32_t addr)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900534{
Alex Williamsonba737542012-04-05 11:07:08 -0600535 PIIX4PMState *s = opaque;
536 uint32_t val = s->pci0_status.down;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900537
Alex Williamsonba737542012-04-05 11:07:08 -0600538 PIIX4_DPRINTF("pci_down_read %x\n", val);
539 return val;
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900540}
541
Alex Williamson9290f362012-04-05 11:07:28 -0600542static uint32_t pci_features_read(void *opaque, uint32_t addr)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900543{
Alex Williamson9290f362012-04-05 11:07:28 -0600544 /* No feature defined yet */
545 PIIX4_DPRINTF("pci_features_read %x\n", 0);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900546 return 0;
547}
548
549static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
550{
Alex Williamson7faa8072012-04-05 11:07:15 -0600551 acpi_piix_eject_slot(opaque, val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900552
Isaku Yamahata50d8ff82010-05-14 16:29:22 +0900553 PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900554}
555
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200556static uint32_t pcirmv_read(void *opaque, uint32_t addr)
557{
558 PIIX4PMState *s = opaque;
559
560 return s->pci0_hotplug_enable;
561}
562
Michael S. Tsirkin4cff0a52010-11-12 16:21:35 +0900563static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
564 PCIHotplugState state);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900565
Isaku Yamahataac404092010-05-14 16:29:20 +0900566static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900567{
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900568
Isaku Yamahata23910d32011-03-25 19:54:41 +0900569 register_ioport_write(GPE_BASE, GPE_LEN, 1, gpe_writeb, s);
570 register_ioport_read(GPE_BASE, GPE_LEN, 1, gpe_readb, s);
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100571 acpi_gpe_blk(&s->ar, GPE_BASE);
Isaku Yamahataac404092010-05-14 16:29:20 +0900572
Alex Williamsonba737542012-04-05 11:07:08 -0600573 register_ioport_read(PCI_UP_BASE, 4, 4, pci_up_read, s);
574 register_ioport_read(PCI_DOWN_BASE, 4, 4, pci_down_read, s);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900575
Alex Williamson7faa8072012-04-05 11:07:15 -0600576 register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, s);
Alex Williamson9290f362012-04-05 11:07:28 -0600577 register_ioport_read(PCI_EJ_BASE, 4, 4, pci_features_read, s);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900578
Marcelo Tosatti668643b2011-01-11 14:20:39 -0200579 register_ioport_read(PCI_RMV_BASE, 4, 4, pcirmv_read, s);
580
Isaku Yamahataac404092010-05-14 16:29:20 +0900581 pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900582}
583
Isaku Yamahataac404092010-05-14 16:29:20 +0900584static void enable_device(PIIX4PMState *s, int slot)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900585{
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100586 s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
Alex Williamson7faa8072012-04-05 11:07:15 -0600587 s->pci0_slot_device_present |= (1U << slot);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900588}
589
Isaku Yamahataac404092010-05-14 16:29:20 +0900590static void disable_device(PIIX4PMState *s, int slot)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900591{
Gerd Hoffmann355bf2e2012-02-23 13:45:16 +0100592 s->ar.gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
Alex Williamson7faa8072012-04-05 11:07:15 -0600593 s->pci0_status.down |= (1U << slot);
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900594}
595
Michael S. Tsirkin4cff0a52010-11-12 16:21:35 +0900596static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
597 PCIHotplugState state)
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900598{
599 int slot = PCI_SLOT(dev->devfn);
Isaku Yamahataac404092010-05-14 16:29:20 +0900600 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
Anthony Liguori40021f02011-12-04 12:22:06 -0600601 PCI_DEVICE(qdev));
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900602
Michael S. Tsirkin4cff0a52010-11-12 16:21:35 +0900603 /* Don't send event when device is enabled during qemu machine creation:
604 * it is present on boot, no hotplug event is necessary. We do send an
605 * event when the device is disabled later. */
606 if (state == PCI_COLDPLUG_ENABLED) {
Alex Williamson7faa8072012-04-05 11:07:15 -0600607 s->pci0_slot_device_present |= (1U << slot);
Isaku Yamahata5beb8ad2010-09-06 16:46:18 +0900608 return 0;
Michael S. Tsirkin4cff0a52010-11-12 16:21:35 +0900609 }
Isaku Yamahata5beb8ad2010-09-06 16:46:18 +0900610
Michael S. Tsirkin4cff0a52010-11-12 16:21:35 +0900611 if (state == PCI_HOTPLUG_ENABLED) {
Isaku Yamahataac404092010-05-14 16:29:20 +0900612 enable_device(s, slot);
613 } else {
614 disable_device(s, slot);
615 }
Gleb Natapov633aa0a2010-10-17 11:45:25 +0200616
617 pm_update_sci(s);
618
Isaku Yamahata93d89f62010-05-14 16:29:02 +0900619 return 0;
620}