2 * Maru power management emulator
3 * Based on qemu/hw/acpi_piix4.c
5 * Copyright (C) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
8 * Hyunjun Son <hj79.son@samsung.com>
9 * GiWoong Kim <giwoong.kim@samsung.com>
10 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
40 #include "tizen/src/debug_ch.h"
45 # define PIIX4_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
47 # define PIIX4_DPRINTF(format, ...) do { } while (0)
50 #define ACPI_DBG_IO_ADDR 0xb044
52 #define GPE_BASE 0xafe0
53 #define PCI_BASE 0xae00
54 #define PCI_EJ_BASE 0xae08
55 #define PCI_RMV_BASE 0xae0c
57 #define PIIX4_PCI_HOTPLUG_STATUS 2
59 /* define debug channel */
60 MULTI_DEBUG_CHANNEL(qemu, tizen_acpi_piix4);
63 uint16_t sts; /* status */
64 uint16_t en; /* enabled */
72 typedef struct PIIX4PMState {
82 int64_t tmr_overflow_time;
94 struct pci_status pci0_status;
95 uint32_t pci0_hotplug_enable;
98 static PIIX4PMState* acpi_state;
99 static int is_suspended;
101 static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s);
103 #define ACPI_ENABLE 0xf1
104 #define ACPI_DISABLE 0xf0
106 static uint32_t get_pmtmr(PIIX4PMState *s)
109 d = muldiv64(qemu_get_clock(vm_clock), PM_TIMER_FREQUENCY, get_ticks_per_sec());
113 static int get_pmsts(PIIX4PMState *s)
117 d = muldiv64(qemu_get_clock(vm_clock), PM_TIMER_FREQUENCY,
118 get_ticks_per_sec());
119 if (d >= s->tmr_overflow_time)
120 s->pmsts |= ACPI_BITMASK_TIMER_STATUS;
124 static void pm_update_sci(PIIX4PMState *s)
126 int sci_level, pmsts;
129 pmsts = get_pmsts(s);
130 sci_level = (((pmsts & s->pmen) &
131 (ACPI_BITMASK_RT_CLOCK_ENABLE |
132 ACPI_BITMASK_POWER_BUTTON_ENABLE |
133 ACPI_BITMASK_GLOBAL_LOCK_ENABLE |
134 ACPI_BITMASK_TIMER_ENABLE)) != 0) ||
135 (((s->gpe.sts & s->gpe.en) & PIIX4_PCI_HOTPLUG_STATUS) != 0);
137 qemu_set_irq(s->irq, sci_level);
138 /* schedule a timer interruption if needed */
139 if ((s->pmen & ACPI_BITMASK_TIMER_ENABLE) &&
140 !(pmsts & ACPI_BITMASK_TIMER_STATUS)) {
141 expire_time = muldiv64(s->tmr_overflow_time, get_ticks_per_sec(),
143 qemu_mod_timer(s->tmr_timer, expire_time);
145 qemu_del_timer(s->tmr_timer);
149 static void pm_tmr_timer(void *opaque)
151 PIIX4PMState *s = opaque;
155 static void pm_ioport_write(IORange *ioport, uint64_t addr, unsigned width,
158 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
161 PIIX4_DPRINTF("PM write port=0x%04x width=%d val=0x%08x\n",
162 (unsigned)addr, width, (unsigned)val);
170 pmsts = get_pmsts(s);
171 if (pmsts & val & ACPI_BITMASK_TIMER_STATUS) {
172 /* if TMRSTS is reset, then compute the new overflow time */
173 d = muldiv64(qemu_get_clock(vm_clock), PM_TIMER_FREQUENCY,
174 get_ticks_per_sec());
175 s->tmr_overflow_time = (d + 0x800000LL) & ~0x7fffffLL;
188 s->pmcntrl = val & ~(ACPI_BITMASK_SLEEP_ENABLE);
189 if (val & ACPI_BITMASK_SLEEP_ENABLE) {
190 /* change suspend type */
191 sus_typ = (val >> 10) & 7;
193 case 0: /* soft power off */
194 qemu_system_shutdown_request();
197 #if 0 // changed suspend operation for emulator
198 /* ACPI_BITMASK_WAKE_STATUS should be set on resume.
199 Pretend that resume was caused by power button */
200 s->pmsts |= (ACPI_BITMASK_WAKE_STATUS |
201 ACPI_BITMASK_POWER_BUTTON_STATUS);
202 qemu_system_reset_request();
204 qemu_irq_raise(s->cmos_s3);
207 INFO( "suspend is requested.\n" );
220 PIIX4_DPRINTF("PM writew port=0x%04x val=0x%04x\n", addr, val);
223 static void pm_ioport_read(IORange *ioport, uint64_t addr, unsigned width,
226 PIIX4PMState *s = container_of(ioport, PIIX4PMState, ioport);
246 PIIX4_DPRINTF("PM readw port=0x%04x val=0x%04x\n", addr, val);
250 static const IORangeOps pm_iorange_ops = {
251 .read = pm_ioport_read,
252 .write = pm_ioport_write,
255 static void apm_ctrl_changed(uint32_t val, void *arg)
257 PIIX4PMState *s = arg;
259 /* ACPI specs 3.0, 4.7.2.5 */
260 if (val == ACPI_ENABLE) {
261 s->pmcntrl |= ACPI_BITMASK_SCI_ENABLE;
262 } else if (val == ACPI_DISABLE) {
263 s->pmcntrl &= ~ACPI_BITMASK_SCI_ENABLE;
266 if (s->dev.config[0x5b] & (1 << 1)) {
268 qemu_irq_raise(s->smi_irq);
273 static void acpi_dbg_writel(void *opaque, uint32_t addr, uint32_t val)
275 PIIX4_DPRINTF("ACPI: DBG: 0x%08x\n", val);
278 static void pm_io_space_update(PIIX4PMState *s)
282 if (s->dev.config[0x80] & 1) {
283 pm_io_base = le32_to_cpu(*(uint32_t *)(s->dev.config + 0x40));
284 pm_io_base &= 0xffc0;
286 /* XXX: need to improve memory and ioport allocation */
287 PIIX4_DPRINTF("PM: mapping to 0x%x\n", pm_io_base);
288 iorange_init(&s->ioport, &pm_iorange_ops, pm_io_base, 64);
289 ioport_register(&s->ioport);
293 static void pm_write_config(PCIDevice *d,
294 uint32_t address, uint32_t val, int len)
296 pci_default_write_config(d, address, val, len);
297 if (range_covers_byte(address, len, 0x80))
298 pm_io_space_update((PIIX4PMState *)d);
301 static int vmstate_acpi_post_load(void *opaque, int version_id)
303 PIIX4PMState *s = opaque;
305 pm_io_space_update(s);
309 static const VMStateDescription vmstate_gpe = {
312 .minimum_version_id = 1,
313 .minimum_version_id_old = 1,
314 .fields = (VMStateField []) {
315 VMSTATE_UINT16(sts, struct gpe_regs),
316 VMSTATE_UINT16(en, struct gpe_regs),
317 VMSTATE_END_OF_LIST()
321 static const VMStateDescription vmstate_pci_status = {
322 .name = "pci_status",
324 .minimum_version_id = 1,
325 .minimum_version_id_old = 1,
326 .fields = (VMStateField []) {
327 VMSTATE_UINT32(up, struct pci_status),
328 VMSTATE_UINT32(down, struct pci_status),
329 VMSTATE_END_OF_LIST()
333 static const VMStateDescription vmstate_acpi = {
336 .minimum_version_id = 1,
337 .minimum_version_id_old = 1,
338 .post_load = vmstate_acpi_post_load,
339 .fields = (VMStateField []) {
340 VMSTATE_PCI_DEVICE(dev, PIIX4PMState),
341 VMSTATE_UINT16(pmsts, PIIX4PMState),
342 VMSTATE_UINT16(pmen, PIIX4PMState),
343 VMSTATE_UINT16(pmcntrl, PIIX4PMState),
344 VMSTATE_STRUCT(apm, PIIX4PMState, 0, vmstate_apm, APMState),
345 VMSTATE_TIMER(tmr_timer, PIIX4PMState),
346 VMSTATE_INT64(tmr_overflow_time, PIIX4PMState),
347 VMSTATE_STRUCT(gpe, PIIX4PMState, 2, vmstate_gpe, struct gpe_regs),
348 VMSTATE_STRUCT(pci0_status, PIIX4PMState, 2, vmstate_pci_status,
350 VMSTATE_END_OF_LIST()
354 static void piix4_update_hotplug(PIIX4PMState *s)
356 PCIDevice *dev = &s->dev;
357 BusState *bus = qdev_get_parent_bus(&dev->qdev);
358 DeviceState *qdev, *next;
360 s->pci0_hotplug_enable = ~0;
362 QLIST_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
363 PCIDeviceInfo *info = container_of(qdev->info, PCIDeviceInfo, qdev);
364 PCIDevice *pdev = DO_UPCAST(PCIDevice, qdev, qdev);
365 int slot = PCI_SLOT(pdev->devfn);
367 if (info->no_hotplug) {
368 s->pci0_hotplug_enable &= ~(1 << slot);
373 static void piix4_reset(void *opaque)
375 PIIX4PMState *s = opaque;
376 uint8_t *pci_conf = s->dev.config;
383 if (s->kvm_enabled) {
384 /* Mark SMM as already inited (until KVM supports SMM). */
385 pci_conf[0x5B] = 0x02;
387 piix4_update_hotplug(s);
390 static void piix4_powerdown(void *opaque, int irq, int power_failing)
392 PIIX4PMState *s = opaque;
395 qemu_system_shutdown_request();
396 } else if (s->pmen & ACPI_BITMASK_POWER_BUTTON_ENABLE) {
397 s->pmsts |= ACPI_BITMASK_POWER_BUTTON_STATUS;
402 static int piix4_pm_initfn(PCIDevice *dev)
404 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev, dev);
407 pci_conf = s->dev.config;
408 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_INTEL);
409 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_INTEL_82371AB_3);
410 pci_conf[0x06] = 0x80;
411 pci_conf[0x07] = 0x02;
412 pci_conf[0x08] = 0x03; // revision number
413 pci_conf[0x09] = 0x00;
414 pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
415 pci_conf[0x3d] = 0x01; // interrupt pin 1
417 pci_conf[0x40] = 0x01; /* PM io base read only bit */
420 apm_init(&s->apm, apm_ctrl_changed, s);
422 register_ioport_write(ACPI_DBG_IO_ADDR, 4, 4, acpi_dbg_writel, s);
424 if (s->kvm_enabled) {
425 /* Mark SMM as already inited to prevent SMM from running. KVM does not
426 * support SMM mode. */
427 pci_conf[0x5B] = 0x02;
430 /* XXX: which specification is used ? The i82731AB has different
432 pci_conf[0x5f] = (parallel_hds[0] != NULL ? 0x80 : 0) | 0x10;
433 pci_conf[0x63] = 0x60;
434 pci_conf[0x67] = (serial_hds[0] != NULL ? 0x08 : 0) |
435 (serial_hds[1] != NULL ? 0x90 : 0);
437 pci_conf[0x90] = s->smb_io_base | 1;
438 pci_conf[0x91] = s->smb_io_base >> 8;
439 pci_conf[0xd2] = 0x09;
440 register_ioport_write(s->smb_io_base, 64, 1, smb_ioport_writeb, &s->smb);
441 register_ioport_read(s->smb_io_base, 64, 1, smb_ioport_readb, &s->smb);
443 s->tmr_timer = qemu_new_timer(vm_clock, pm_tmr_timer, s);
445 qemu_system_powerdown = *qemu_allocate_irqs(piix4_powerdown, s, 1);
447 pm_smbus_init(&s->dev.qdev, &s->smb);
448 qemu_register_reset(piix4_reset, s);
449 piix4_acpi_system_hot_add_init(dev->bus, s);
454 i2c_bus *maru_pm_init(PCIBus *bus, int devfn, uint32_t smb_io_base,
455 qemu_irq sci_irq, qemu_irq cmos_s3, qemu_irq smi_irq,
461 dev = pci_create(bus, devfn, "TIZEN-PIIX4_PM");
462 qdev_prop_set_uint32(&dev->qdev, "smb_io_base", smb_io_base);
464 s = DO_UPCAST(PIIX4PMState, dev, dev);
466 s->cmos_s3 = cmos_s3;
467 s->smi_irq = smi_irq;
468 s->kvm_enabled = kvm_enabled;
472 qdev_init_nofail(&dev->qdev);
477 static PCIDeviceInfo piix4_pm_info = {
478 .qdev.name = "TIZEN-PIIX4_PM",
479 .qdev.desc = "TIZEN PM",
480 .qdev.size = sizeof(PIIX4PMState),
481 .qdev.vmsd = &vmstate_acpi,
484 .init = piix4_pm_initfn,
485 .config_write = pm_write_config,
486 .qdev.props = (Property[]) {
487 DEFINE_PROP_UINT32("smb_io_base", PIIX4PMState, smb_io_base, 0),
488 DEFINE_PROP_END_OF_LIST(),
492 static void piix4_pm_register(void)
494 pci_qdev_register(&piix4_pm_info);
497 device_init(piix4_pm_register);
499 static uint32_t gpe_read_val(uint16_t val, uint32_t addr)
502 return (val >> 8) & 0xff;
506 static uint32_t gpe_readb(void *opaque, uint32_t addr)
509 PIIX4PMState *s = opaque;
510 struct gpe_regs *g = &s->gpe;
515 val = gpe_read_val(g->sts, addr);
519 val = gpe_read_val(g->en, addr);
525 PIIX4_DPRINTF("gpe read %x == %x\n", addr, val);
529 static void gpe_write_val(uint16_t *cur, int addr, uint32_t val)
532 *cur = (*cur & 0xff) | (val << 8);
534 *cur = (*cur & 0xff00) | (val & 0xff);
537 static void gpe_reset_val(uint16_t *cur, int addr, uint32_t val)
539 uint16_t x1, x0 = val & 0xff;
540 int shift = (addr & 1) ? 8 : 0;
542 x1 = (*cur >> shift) & 0xff;
546 *cur = (*cur & (0xff << (8 - shift))) | (x1 << shift);
549 static void gpe_writeb(void *opaque, uint32_t addr, uint32_t val)
551 PIIX4PMState *s = opaque;
552 struct gpe_regs *g = &s->gpe;
557 gpe_reset_val(&g->sts, addr, val);
561 gpe_write_val(&g->en, addr, val);
569 PIIX4_DPRINTF("gpe write %x <== %d\n", addr, val);
572 static uint32_t pcihotplug_read(void *opaque, uint32_t addr)
575 struct pci_status *g = opaque;
587 PIIX4_DPRINTF("pcihotplug read %x == %x\n", addr, val);
591 static void pcihotplug_write(void *opaque, uint32_t addr, uint32_t val)
593 struct pci_status *g = opaque;
603 PIIX4_DPRINTF("pcihotplug write %x <== %d\n", addr, val);
606 static uint32_t pciej_read(void *opaque, uint32_t addr)
608 PIIX4_DPRINTF("pciej read %x\n", addr);
612 static void pciej_write(void *opaque, uint32_t addr, uint32_t val)
614 BusState *bus = opaque;
615 DeviceState *qdev, *next;
617 int slot = ffs(val) - 1;
619 QLIST_FOREACH_SAFE(qdev, &bus->children, sibling, next) {
620 dev = DO_UPCAST(PCIDevice, qdev, qdev);
621 if (PCI_SLOT(dev->devfn) == slot) {
627 PIIX4_DPRINTF("pciej write %x <== %d\n", addr, val);
630 static uint32_t pcirmv_read(void *opaque, uint32_t addr)
632 PIIX4PMState *s = opaque;
634 return s->pci0_hotplug_enable;
637 static void pcirmv_write(void *opaque, uint32_t addr, uint32_t val)
642 static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
643 PCIHotplugState state);
645 static void piix4_acpi_system_hot_add_init(PCIBus *bus, PIIX4PMState *s)
647 struct pci_status *pci0_status = &s->pci0_status;
649 register_ioport_write(GPE_BASE, 4, 1, gpe_writeb, s);
650 register_ioport_read(GPE_BASE, 4, 1, gpe_readb, s);
652 register_ioport_write(PCI_BASE, 8, 4, pcihotplug_write, pci0_status);
653 register_ioport_read(PCI_BASE, 8, 4, pcihotplug_read, pci0_status);
655 register_ioport_write(PCI_EJ_BASE, 4, 4, pciej_write, bus);
656 register_ioport_read(PCI_EJ_BASE, 4, 4, pciej_read, bus);
658 register_ioport_write(PCI_RMV_BASE, 4, 4, pcirmv_write, s);
659 register_ioport_read(PCI_RMV_BASE, 4, 4, pcirmv_read, s);
661 pci_bus_hotplug(bus, piix4_device_hotplug, &s->dev.qdev);
664 static void enable_device(PIIX4PMState *s, int slot)
666 s->gpe.sts |= PIIX4_PCI_HOTPLUG_STATUS;
667 s->pci0_status.up |= (1 << slot);
670 static void disable_device(PIIX4PMState *s, int slot)
672 s->gpe.sts |= PIIX4_PCI_HOTPLUG_STATUS;
673 s->pci0_status.down |= (1 << slot);
676 static int piix4_device_hotplug(DeviceState *qdev, PCIDevice *dev,
677 PCIHotplugState state)
679 int slot = PCI_SLOT(dev->devfn);
680 PIIX4PMState *s = DO_UPCAST(PIIX4PMState, dev,
681 DO_UPCAST(PCIDevice, qdev, qdev));
683 /* Don't send event when device is enabled during qemu machine creation:
684 * it is present on boot, no hotplug event is necessary. We do send an
685 * event when the device is disabled later. */
686 if (state == PCI_COLDPLUG_ENABLED) {
690 s->pci0_status.up = 0;
691 s->pci0_status.down = 0;
692 if (state == PCI_HOTPLUG_ENABLED) {
693 enable_device(s, slot);
695 disable_device(s, slot);
703 void resume( void ) {
709 if ( acpi_state->cmos_s3 ) {
710 acpi_state->pmsts |= ( ACPI_BITMASK_WAKE_STATUS | ACPI_BITMASK_POWER_BUTTON_STATUS );
711 INFO( "raise irq for ACPI wake." );
712 qemu_irq_raise( acpi_state->cmos_s3 );
714 ERR( "acpi cmos_s3 is NULL." );
718 ERR( "acpi state is NULL." );
723 int is_suspended_state( void ) {