ARM: Samsung exynos4210-based boards emulation
[qemu/cris-port.git] / hw / acpi_piix4.c
blobd959f4907bea152d61da615490186c9c2c2ff8a0
1 /*
2 * ACPI implementation
4 * Copyright (c) 2006 Fabrice Bellard
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.
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.
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/>
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.
21 #include "hw.h"
22 #include "pc.h"
23 #include "apm.h"
24 #include "pm_smbus.h"
25 #include "pci.h"
26 #include "acpi.h"
27 #include "sysemu.h"
28 #include "range.h"
29 #include "ioport.h"
31 //#define DEBUG
33 #ifdef DEBUG
34 # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
35 #else
36 # define PIIX4_DPRINTF(format, ...) do { } while (0)
37 #endif
39 #define ACPI_DBG_IO_ADDR 0xb044
41 #define GPE_BASE 0xafe0
42 #define GPE_LEN 4
43 #define PCI_BASE 0xae00
44 #define PCI_EJ_BASE 0xae08
45 #define PCI_RMV_BASE 0xae0c
47 #define PIIX4_PCI_HOTPLUG_STATUS 2
49 struct pci_status {
50 uint32_t up;
51 uint32_t down;
54 typedef struct PIIX4PMState {
55 PCIDevice dev;
56 IORange ioport;
57 ACPIPM1EVT pm1a;
58 ACPIPM1CNT pm1_cnt;
60 APMState apm;
62 ACPIPMTimer tmr;
64 PMSMBus smb;
65 uint32_t smb_io_base;
67 qemu_irq irq;
68 qemu_irq smi_irq;
69 int kvm_enabled;
70 Notifier machine_ready;
72 /* for pci hotplug */
73 ACPIGPE gpe;
74 struct pci_status pci0_status;
75 uint32_t pci0_hotplug_enable;
76 } PIIX4PMState;
78 static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s);
80 #define ACPI_ENABLE 0xf1
81 #define ACPI_DISABLE 0xf0
83 static void pm_update_sci(PIIX4PMState *s)
85 int sci_level, pmsts;
87 pmsts = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
88 sci_level = (((pmsts & s->pm1a.en) &
89 (ACPI_BITMASK_RT_CLOCK_ENABLE |
90 ACPI_BITMASK_POWER_BUTTON_ENABLE |
91 ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
92 ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
93 (((s->gpe.sts[0] & s->gpe.en[0]) & PIIX4_PCI_HOTPLUG_STATUS) != 0);
95 qemu_set_irq(s->irq, sci_level);
96 /* schedule a timer interruption if needed */
97 acpi_pm_tmr_update(&s->tmr, (s->pm1a.en & ACPI_BITMASK_TIMER_ENABLE) &&
98 !(pmsts & ACPI_BITMASK_TIMER_STATUS));
101 static void pm_tmr_timer(ACPIPMTimer *tmr)
103 PIIX4PMState *s = container_of(tmr, PIIX4PMState, tmr);
104 pm_update_sci(s);
107 static void pm_ioport_write(IORange *ioport, uint64_t addr, unsigned width,
108 uint64_t val)
110 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
112 if (width != 2) {
113 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
114 (unsigned)addr, width, (unsigned)val);
117 switch(addr) {
118 case 0x00:
119 acpi_pm1_evt_write_sts(&s->pm1a, &s->tmr, val);
120 pm_update_sci(s);
121 break;
122 case 0x02:
123 s->pm1a.en = val;
124 pm_update_sci(s);
125 break;
126 case 0x04:
127 acpi_pm1_cnt_write(&s->pm1a, &s->pm1_cnt, val);
128 break;
129 default:
130 break;
132 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", (unsigned int)addr,
133 (unsigned int)val);
136 static void pm_ioport_read(IORange *ioport, uint64_t addr, unsigned width,
137 uint64_t *data)
139 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
140 uint32_t val;
142 switch(addr) {
143 case 0x00:
144 val = acpi_pm1_evt_get_sts(&s->pm1a, s->tmr.overflow_time);
145 break;
146 case 0x02:
147 val = s->pm1a.en;
148 break;
149 case 0x04:
150 val = s->pm1_cnt.cnt;
151 break;
152 case 0x08:
153 val = acpi_pm_tmr_get(&s->tmr);
154 break;
155 default:
156 val = 0;
157 break;
159 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", (unsigned int)addr, val);
160 *data = val;
163 static const IORangeOps pm_iorange_ops = {
164 .read = pm_ioport_read,
165 .write = pm_ioport_write,
168 static void apm_ctrl_changed(uint32_t val, void *arg)
170 PIIX4PMState *s = arg;
172 /* ACPI specs 3.0, 4.7.2.5 */
173 acpi_pm1_cnt_update(&s->pm1_cnt, val == ACPI_ENABLE, val == ACPI_DISABLE);
175 if (s->dev.config[0x5b] & (1 << 1)) {
176 if (s->smi_irq) {
177 qemu_irq_raise(s->smi_irq);
182 static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
184 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val);
187 static void pm_io_space_update(PIIX4PMState *s)
189 uint32_t pm_io_base;
191 if (s->dev.config[0x80] & 1) {
192 pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
193 pm_io_base &= 0xffc0;
195 /* XXX: need to improve memory and ioport allocation */
196 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base);
197 iorange_init(&s->ioport, &pm_iorange_ops, pm_io_base, 64);
198 ioport_register(&s->ioport);
202 static void pm_write_config(PCIDevice *d,
203 uint32_t address, uint32_t val, int len)
205 pci_default_write_config(d, address, val, len);
206 if (range_covers_byte(address, len, 0x80))
207 pm_io_space_update((PIIX4PMState *)d);
210 static int vmstate_acpi_post_load(void *opaque, int version_id)
212 PIIX4PMState *s = opaque;
214 pm_io_space_update(s);
215 return 0;
218 #define VMSTATE_GPE_ARRAY(_field, _state) \
220 .name = (stringify(_field)), \
221 .version_id = 0, \
222 .num = GPE_LEN, \
223 .info = &vmstate_info_uint16, \
224 .size = sizeof(uint16_t), \
225 .flags = VMS_ARRAY | VMS_POINTER, \
226 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
229 static const VMStateDescription vmstate_gpe = {
230 .name = "gpe",
231 .version_id = 1,
232 .minimum_version_id = 1,
233 .minimum_version_id_old = 1,
234 .fields = (VMStateField []) {
235 VMSTATE_GPE_ARRAY(sts, ACPIGPE),
236 VMSTATE_GPE_ARRAY(en, ACPIGPE),
237 VMSTATE_END_OF_LIST()
241 static const VMStateDescription vmstate_pci_status = {
242 .name = "pci_status",
243 .version_id = 1,
244 .minimum_version_id = 1,
245 .minimum_version_id_old = 1,
246 .fields = (VMStateField []) {
247 VMSTATE_UINT32(up, struct pci_status),
248 VMSTATE_UINT32(down, struct pci_status),
249 VMSTATE_END_OF_LIST()
253 static const VMStateDescription vmstate_acpi = {
254 .name = "piix4_pm",
255 .version_id = 2,
256 .minimum_version_id = 1,
257 .minimum_version_id_old = 1,
258 .post_load = vmstate_acpi_post_load,
259 .fields = (VMStateField []) {
260 VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
261 VMSTATE_UINT16(pm1a.sts, PIIX4PMState),
262 VMSTATE_UINT16(pm1a.en, PIIX4PMState),
263 VMSTATE_UINT16(pm1_cnt.cnt, PIIX4PMState),
264 VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
265 VMSTATE_TIMER(tmr.timer, PIIX4PMState),
266 VMSTATE_INT64(tmr.overflow_time, PIIX4PMState),
267 VMSTATE_STRUCT(gpe, PIIX4PMState, 2, vmstate_gpe, ACPIGPE),
268 VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
269 struct pci_status),
270 VMSTATE_END_OF_LIST()
274 static void piix4_update_hotplug(PIIX4PMState *s)
276 PCIDevice *dev = &s->dev;
277 BusState *bus = qdev_get_parent_bus(&dev->qdev);
278 DeviceState *qdev, *next;
280 s->pci0_hotplug_enable = ~0;
282 QTAILQ_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
283 PCIDevice *pdev = PCI_DEVICE(qdev);
284 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pdev);
285 int slot = PCI_SLOT(pdev->devfn);
287 if (pc->no_hotplug) {
288 s->pci0_hotplug_enable &= ~(1 << slot);
293 static void piix4_reset(void *opaque)
295 PIIX4PMState *s = opaque;
296 uint8_t *pci_conf = s->dev.config;
298 pci_conf[0x58] = 0;
299 pci_conf[0x59] = 0;
300 pci_conf[0x5a] = 0;
301 pci_conf[0x5b] = 0;
303 if (s->kvm_enabled) {
304 /* Mark SMM as already inited (until KVM supports SMM). */
305 pci_conf[0x5B] = 0x02;
307 piix4_update_hotplug(s);
310 static void piix4_powerdown(void *opaque, int irq, int power_failing)
312 PIIX4PMState *s = opaque;
313 ACPIPM1EVT *pm1a = s? &s->pm1a: NULL;
314 ACPIPMTimer *tmr = s? &s->tmr: NULL;
316 acpi_pm1_evt_power_down(pm1a, tmr);
319 static void piix4_pm_machine_ready(Notifier *n, void *opaque)
321 PIIX4PMState *s = container_of(n, PIIX4PMState, machine_ready);
322 uint8_t *pci_conf;
324 pci_conf = s->dev.config;
325 pci_conf[0x5f] = (isa_is_ioport_assigned(0x378) ? 0x80 : 0) | 0x10;
326 pci_conf[0x63] = 0x60;
327 pci_conf[0x67] = (isa_is_ioport_assigned(0x3f8) ? 0x08 : 0) |
328 (isa_is_ioport_assigned(0x2f8) ? 0x90 : 0);
332 static int piix4_pm_initfn(PCIDevice *dev)
334 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
335 uint8_t *pci_conf;
337 pci_conf = s->dev.config;
338 pci_conf[0x06] = 0x80;
339 pci_conf[0x07] = 0x02;
340 pci_conf[0x09] = 0x00;
341 pci_conf[0x3d] = 0x01; // interrupt pin 1
343 pci_conf[0x40] = 0x01; /* PM io base read only bit */
345 /* APM */
346 apm_init(&s->apm, apm_ctrl_changed, s);
348 register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
350 if (s->kvm_enabled) {
351 /* Mark SMM as already inited to prevent SMM from running. KVM does not
352 * support SMM mode. */
353 pci_conf[0x5B] = 0x02;
356 /* XXX: which specification is used ? The i82731AB has different
357 mappings */
358 pci_conf[0x90] = s->smb_io_base | 1;
359 pci_conf[0x91] = s->smb_io_base >> 8;
360 pci_conf[0xd2] = 0x09;
361 register_ioport_write(s->smb_io_base, 64, 1, smb_ioport_writeb, &s->smb);
362 register_ioport_read(s->smb_io_base, 64, 1, smb_ioport_readb, &s->smb);
364 acpi_pm_tmr_init(&s->tmr, pm_tmr_timer);
365 acpi_gpe_init(&s->gpe, GPE_LEN);
367 qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
369 pm_smbus_init(&s->dev.qdev, &s->smb);
370 s->machine_ready.notify = piix4_pm_machine_ready;
371 qemu_add_machine_init_done_notifier(&s->machine_ready);
372 qemu_register_reset(piix4_reset, s);
373 piix4_acpi_system_hot_add_init(dev->bus, s);
375 return 0;
378 i2c_bus *piix4_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
379 qemu_irq sci_irq, qemu_irq cmos_s3, qemu_irq smi_irq,
380 int kvm_enabled)
382 PCIDevice *dev;
383 PIIX4PMState *s;
385 dev = pci_create(bus, devfn, "PIIX4_PM");
386 qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
388 s = DO_UPCAST(PIIX4PMState, dev, dev);
389 s->irq = sci_irq;
390 acpi_pm1_cnt_init(&s->pm1_cnt, cmos_s3);
391 s->smi_irq = smi_irq;
392 s->kvm_enabled = kvm_enabled;
394 qdev_init_nofail(&dev->qdev);
396 return s->smb.smbus;
399 static Property piix4_pm_properties[] = {
400 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
401 DEFINE_PROP_END_OF_LIST(),
404 static void piix4_pm_class_init(ObjectClass *klass, void *data)
406 DeviceClass *dc = DEVICE_CLASS(klass);
407 PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
409 k->no_hotplug = 1;
410 k->init = piix4_pm_initfn;
411 k->config_write = pm_write_config;
412 k->vendor_id = PCI_VENDOR_ID_INTEL;
413 k->device_id = PCI_DEVICE_ID_INTEL_82371AB_3;
414 k->revision = 0x03;
415 k->class_id = PCI_CLASS_BRIDGE_OTHER;
416 dc->desc = "PM";
417 dc->no_user = 1;
418 dc->vmsd = &vmstate_acpi;
419 dc->props = piix4_pm_properties;
422 static TypeInfo piix4_pm_info = {
423 .name = "PIIX4_PM",
424 .parent = TYPE_PCI_DEVICE,
425 .instance_size = sizeof(PIIX4PMState),
426 .class_init = piix4_pm_class_init,
429 static void piix4_pm_register_types(void)
431 type_register_static(&piix4_pm_info);
434 type_init(piix4_pm_register_types)
436 static uint32_t gpe_readb(void *opaque, uint32_t addr)
438 PIIX4PMState *s = opaque;
439 uint32_t val = acpi_gpe_ioport_readb(&s->gpe, addr);
441 PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
442 return val;
445 static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
447 PIIX4PMState *s = opaque;
449 acpi_gpe_ioport_writeb(&s->gpe, addr, val);
450 pm_update_sci(s);
452 PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
455 static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
457 uint32_t val = 0;
458 struct pci_status *g = opaque;
459 switch (addr) {
460 case PCI_BASE:
461 val = g->up;
462 break;
463 case PCI_BASE + 4:
464 val = g->down;
465 break;
466 default:
467 break;
470 PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr, val);
471 return val;
474 static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
476 struct pci_status *g = opaque;
477 switch (addr) {
478 case PCI_BASE:
479 g->up = val;
480 break;
481 case PCI_BASE + 4:
482 g->down = val;
483 break;
486 PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr, val);
489 static uint32_t pciej_read(void *opaque, uint32_t addr)
491 PIIX4_DPRINTF("pciej read %x\n", addr);
492 return 0;
495 static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
497 BusState *bus = opaque;
498 DeviceState *qdev, *next;
499 int slot = ffs(val) - 1;
501 QTAILQ_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
502 PCIDevice *dev = PCI_DEVICE(qdev);
503 PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
504 if (PCI_SLOT(dev->devfn) == slot && !pc->no_hotplug) {
505 qdev_free(qdev);
510 PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val);
513 static uint32_t pcirmv_read(void *opaque, uint32_t addr)
515 PIIX4PMState *s = opaque;
517 return s->pci0_hotplug_enable;
520 static void pcirmv_write(void *opaque, uint32_t addr, uint32_t val)
522 return;
525 static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
526 PCIHotplugState state);
528 static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s)
530 struct pci_status *pci0_status = &s->pci0_status;
532 register_ioport_write(GPE_BASE, GPE_LEN, 1, gpe_writeb, s);
533 register_ioport_read(GPE_BASE, GPE_LEN, 1, gpe_readb, s);
534 acpi_gpe_blk(&s->gpe, GPE_BASE);
536 register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, pci0_status);
537 register_ioport_read(PCI_BASE, 8, 4, pcihotplug_read, pci0_status);
539 register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, bus);
540 register_ioport_read(PCI_EJ_BASE, 4, 4, pciej_read, bus);
542 register_ioport_write(PCI_RMV_BASE, 4, 4, pcirmv_write, s);
543 register_ioport_read(PCI_RMV_BASE, 4, 4, pcirmv_read, s);
545 pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
548 static void enable_device(PIIX4PMState *s, int slot)
550 s->gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
551 s->pci0_status.up |= (1 << slot);
554 static void disable_device(PIIX4PMState *s, int slot)
556 s->gpe.sts[0] |= PIIX4_PCI_HOTPLUG_STATUS;
557 s->pci0_status.down |= (1 << slot);
560 static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
561 PCIHotplugState state)
563 int slot = PCI_SLOT(dev->devfn);
564 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
565 PCI_DEVICE(qdev));
567 /* Don't send event when device is enabled during qemu machine creation:
568 * it is present on boot, no hotplug event is necessary. We do send an
569 * event when the device is disabled later. */
570 if (state == PCI_COLDPLUG_ENABLED) {
571 return 0;
574 s->pci0_status.up = 0;
575 s->pci0_status.down = 0;
576 if (state == PCI_HOTPLUG_ENABLED) {
577 enable_device(s, slot);
578 } else {
579 disable_device(s, slot);
582 pm_update_sci(s);
584 return 0;