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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "qemu-timer.h"
32 /* i82731AB (PIIX4) compatible power management function */
33 #define PM_FREQ 3579545
35 #define ACPI_DBG_IO_ADDR 0xb044
37 typedef struct PIIX4PMState
{
45 int64_t tmr_overflow_time
;
58 #define RTC_EN (1 << 10)
59 #define PWRBTN_EN (1 << 8)
60 #define GBL_EN (1 << 5)
61 #define TMROF_EN (1 << 0)
63 #define SCI_EN (1 << 0)
65 #define SUS_EN (1 << 13)
67 #define ACPI_ENABLE 0xf1
68 #define ACPI_DISABLE 0xf0
70 #define SMBHSTSTS 0x00
71 #define SMBHSTCNT 0x02
72 #define SMBHSTCMD 0x03
73 #define SMBHSTADD 0x04
74 #define SMBHSTDAT0 0x05
75 #define SMBHSTDAT1 0x06
76 #define SMBBLKDAT 0x07
78 static PIIX4PMState
*pm_state
;
80 static uint32_t get_pmtmr(PIIX4PMState
*s
)
83 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, ticks_per_sec
);
87 static int get_pmsts(PIIX4PMState
*s
)
92 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, ticks_per_sec
);
93 if (d
>= s
->tmr_overflow_time
)
98 static void pm_update_sci(PIIX4PMState
*s
)
100 int sci_level
, pmsts
;
103 pmsts
= get_pmsts(s
);
104 sci_level
= (((pmsts
& s
->pmen
) &
105 (RTC_EN
| PWRBTN_EN
| GBL_EN
| TMROF_EN
)) != 0);
106 qemu_set_irq(s
->irq
, sci_level
);
107 /* schedule a timer interruption if needed */
108 if ((s
->pmen
& TMROF_EN
) && !(pmsts
& TMROF_EN
)) {
109 expire_time
= muldiv64(s
->tmr_overflow_time
, ticks_per_sec
, PM_FREQ
);
110 qemu_mod_timer(s
->tmr_timer
, expire_time
);
112 qemu_del_timer(s
->tmr_timer
);
116 static void pm_tmr_timer(void *opaque
)
118 PIIX4PMState
*s
= opaque
;
122 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
124 PIIX4PMState
*s
= opaque
;
131 pmsts
= get_pmsts(s
);
132 if (pmsts
& val
& TMROF_EN
) {
133 /* if TMRSTS is reset, then compute the new overflow time */
134 d
= muldiv64(qemu_get_clock(vm_clock
), PM_FREQ
, ticks_per_sec
);
135 s
->tmr_overflow_time
= (d
+ 0x800000LL
) & ~0x7fffffLL
;
148 s
->pmcntrl
= val
& ~(SUS_EN
);
150 /* change suspend type */
151 sus_typ
= (val
>> 10) & 7;
153 case 0: /* soft power off */
154 qemu_system_shutdown_request();
166 printf("PM writew port=0x%04x val=0x%04x\n", addr
, val
);
170 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
172 PIIX4PMState
*s
= opaque
;
191 printf("PM readw port=0x%04x val=0x%04x\n", addr
, val
);
196 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
198 // PIIX4PMState *s = opaque;
201 printf("PM writel port=0x%04x val=0x%08x\n", addr
, val
);
205 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
207 PIIX4PMState
*s
= opaque
;
220 printf("PM readl port=0x%04x val=0x%08x\n", addr
, val
);
225 static void pm_smi_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
227 PIIX4PMState
*s
= opaque
;
230 printf("pm_smi_writeb addr=0x%x val=0x%02x\n", addr
, val
);
235 /* ACPI specs 3.0, 4.7.2.5 */
236 if (val
== ACPI_ENABLE
) {
237 s
->pmcntrl
|= SCI_EN
;
238 } else if (val
== ACPI_DISABLE
) {
239 s
->pmcntrl
&= ~SCI_EN
;
242 if (s
->dev
.config
[0x5b] & (1 << 1)) {
243 cpu_interrupt(first_cpu
, CPU_INTERRUPT_SMI
);
250 static uint32_t pm_smi_readb(void *opaque
, uint32_t addr
)
252 PIIX4PMState
*s
= opaque
;
262 printf("pm_smi_readb addr=0x%x val=0x%02x\n", addr
, val
);
267 static void acpi_dbg_writel(void *opaque
, uint32_t addr
, uint32_t val
)
270 printf("ACPI: DBG: 0x%08x\n", val
);
274 static void smb_transaction(PIIX4PMState
*s
)
276 uint8_t prot
= (s
->smb_ctl
>> 2) & 0x07;
277 uint8_t read
= s
->smb_addr
& 0x01;
278 uint8_t cmd
= s
->smb_cmd
;
279 uint8_t addr
= s
->smb_addr
>> 1;
280 i2c_bus
*bus
= s
->smbus
;
283 printf("SMBus trans addr=0x%02x prot=0x%02x\n", addr
, prot
);
287 smbus_quick_command(bus
, addr
, read
);
291 s
->smb_data0
= smbus_receive_byte(bus
, addr
);
293 smbus_send_byte(bus
, addr
, cmd
);
298 s
->smb_data0
= smbus_read_byte(bus
, addr
, cmd
);
300 smbus_write_byte(bus
, addr
, cmd
, s
->smb_data0
);
306 val
= smbus_read_word(bus
, addr
, cmd
);
308 s
->smb_data1
= val
>> 8;
310 smbus_write_word(bus
, addr
, cmd
, (s
->smb_data1
<< 8) | s
->smb_data0
);
315 s
->smb_data0
= smbus_read_block(bus
, addr
, cmd
, s
->smb_data
);
317 smbus_write_block(bus
, addr
, cmd
, s
->smb_data
, s
->smb_data0
);
329 static void smb_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
331 PIIX4PMState
*s
= opaque
;
334 printf("SMB writeb port=0x%04x val=0x%02x\n", addr
, val
);
359 s
->smb_data
[s
->smb_index
++] = val
;
360 if (s
->smb_index
> 31)
368 static uint32_t smb_ioport_readb(void *opaque
, uint32_t addr
)
370 PIIX4PMState
*s
= opaque
;
380 val
= s
->smb_ctl
& 0x1f;
395 val
= s
->smb_data
[s
->smb_index
++];
396 if (s
->smb_index
> 31)
404 printf("SMB readb port=0x%04x val=0x%02x\n", addr
, val
);
409 static void pm_io_space_update(PIIX4PMState
*s
)
413 if (s
->dev
.config
[0x80] & 1) {
414 pm_io_base
= le32_to_cpu(*(uint32_t *)(s
->dev
.config
+ 0x40));
415 pm_io_base
&= 0xffc0;
417 /* XXX: need to improve memory and ioport allocation */
419 printf("PM: mapping to 0x%x\n", pm_io_base
);
421 register_ioport_write(pm_io_base
, 64, 2, pm_ioport_writew
, s
);
422 register_ioport_read(pm_io_base
, 64, 2, pm_ioport_readw
, s
);
423 register_ioport_write(pm_io_base
, 64, 4, pm_ioport_writel
, s
);
424 register_ioport_read(pm_io_base
, 64, 4, pm_ioport_readl
, s
);
428 static void pm_write_config(PCIDevice
*d
,
429 uint32_t address
, uint32_t val
, int len
)
431 pci_default_write_config(d
, address
, val
, len
);
433 pm_io_space_update((PIIX4PMState
*)d
);
436 static void pm_save(QEMUFile
* f
,void *opaque
)
438 PIIX4PMState
*s
= opaque
;
440 pci_device_save(&s
->dev
, f
);
442 qemu_put_be16s(f
, &s
->pmsts
);
443 qemu_put_be16s(f
, &s
->pmen
);
444 qemu_put_be16s(f
, &s
->pmcntrl
);
445 qemu_put_8s(f
, &s
->apmc
);
446 qemu_put_8s(f
, &s
->apms
);
447 qemu_put_timer(f
, s
->tmr_timer
);
448 qemu_put_be64(f
, s
->tmr_overflow_time
);
451 static int pm_load(QEMUFile
* f
,void* opaque
,int version_id
)
453 PIIX4PMState
*s
= opaque
;
459 ret
= pci_device_load(&s
->dev
, f
);
463 qemu_get_be16s(f
, &s
->pmsts
);
464 qemu_get_be16s(f
, &s
->pmen
);
465 qemu_get_be16s(f
, &s
->pmcntrl
);
466 qemu_get_8s(f
, &s
->apmc
);
467 qemu_get_8s(f
, &s
->apms
);
468 qemu_get_timer(f
, s
->tmr_timer
);
469 s
->tmr_overflow_time
=qemu_get_be64(f
);
471 pm_io_space_update(s
);
476 i2c_bus
*piix4_pm_init(PCIBus
*bus
, int devfn
, uint32_t smb_io_base
,
482 s
= (PIIX4PMState
*)pci_register_device(bus
,
483 "PM", sizeof(PIIX4PMState
),
484 devfn
, NULL
, pm_write_config
);
486 pci_conf
= s
->dev
.config
;
487 pci_conf
[0x00] = 0x86;
488 pci_conf
[0x01] = 0x80;
489 pci_conf
[0x02] = 0x13;
490 pci_conf
[0x03] = 0x71;
491 pci_conf
[0x06] = 0x80;
492 pci_conf
[0x07] = 0x02;
493 pci_conf
[0x08] = 0x03; // revision number
494 pci_conf
[0x09] = 0x00;
495 pci_conf
[0x0a] = 0x80; // other bridge device
496 pci_conf
[0x0b] = 0x06; // bridge device
497 pci_conf
[0x0e] = 0x00; // header_type
498 pci_conf
[0x3d] = 0x01; // interrupt pin 1
500 pci_conf
[0x40] = 0x01; /* PM io base read only bit */
502 #if defined(TARGET_IA64)
503 pci_conf
[0x40] = 0x41; /* PM io base read only bit */
504 pci_conf
[0x41] = 0x1f;
505 pm_write_config(s
, 0x80, 0x01, 1); /*Set default pm_io_base 0x1f40*/
509 register_ioport_write(0xb2, 2, 1, pm_smi_writeb
, s
);
510 register_ioport_read(0xb2, 2, 1, pm_smi_readb
, s
);
512 register_ioport_write(ACPI_DBG_IO_ADDR
, 4, 4, acpi_dbg_writel
, s
);
515 /* Mark SMM as already inited to prevent SMM from running. KVM does not
516 * support SMM mode. */
517 pci_conf
[0x5B] = 0x02;
520 /* XXX: which specification is used ? The i82731AB has different
522 pci_conf
[0x5f] = (parallel_hds
[0] != NULL
? 0x80 : 0) | 0x10;
523 pci_conf
[0x63] = 0x60;
524 pci_conf
[0x67] = (serial_hds
[0] != NULL
? 0x08 : 0) |
525 (serial_hds
[1] != NULL
? 0x90 : 0);
527 pci_conf
[0x90] = smb_io_base
| 1;
528 pci_conf
[0x91] = smb_io_base
>> 8;
529 pci_conf
[0xd2] = 0x09;
530 register_ioport_write(smb_io_base
, 64, 1, smb_ioport_writeb
, s
);
531 register_ioport_read(smb_io_base
, 64, 1, smb_ioport_readb
, s
);
533 s
->tmr_timer
= qemu_new_timer(vm_clock
, pm_tmr_timer
, s
);
535 register_savevm("piix4_pm", 0, 1, pm_save
, pm_load
, s
);
537 s
->smbus
= i2c_init_bus();
542 #if defined(TARGET_I386)
543 void qemu_system_powerdown(void)
546 qemu_system_shutdown_request();
547 } else if (pm_state
->pmen
& PWRBTN_EN
) {
548 pm_state
->pmsts
|= PWRBTN_EN
;
549 pm_update_sci(pm_state
);
553 #define GPE_BASE 0xafe0
554 #define PROC_BASE 0xaf00
555 #define PCI_BASE 0xae00
556 #define PCI_EJ_BASE 0xae08
559 uint16_t sts
; /* status */
560 uint16_t en
; /* enabled */
570 static struct gpe_regs gpe
;
571 static struct pci_status pci0_status
;
573 static uint32_t gpe_readb(void *opaque
, uint32_t addr
)
576 struct gpe_regs
*g
= opaque
;
589 val
= (g
->sts
>> 8) & 0xFF;
595 val
= (g
->en
>> 8) & 0xFF;
602 printf("gpe read %x == %x\n", addr
, val
);
607 static void gpe_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
609 struct gpe_regs
*g
= opaque
;
619 g
->sts
= (g
->sts
& ~0xFFFF) | (val
& 0xFFFF);
622 g
->sts
= (g
->sts
& 0xFFFF) | (val
<< 8);
625 g
->en
= (g
->en
& ~0xFFFF) | (val
& 0xFFFF);
628 g
->en
= (g
->en
& 0xFFFF) | (val
<< 8);
635 printf("gpe write %x <== %d\n", addr
, val
);
639 static uint32_t pcihotplug_read(void *opaque
, uint32_t addr
)
642 struct pci_status
*g
= opaque
;
655 printf("pcihotplug read %x == %x\n", addr
, val
);
660 static void pcihotplug_write(void *opaque
, uint32_t addr
, uint32_t val
)
662 struct pci_status
*g
= opaque
;
673 printf("pcihotplug write %x <== %d\n", addr
, val
);
677 static uint32_t pciej_read(void *opaque
, uint32_t addr
)
680 printf("pciej read %x\n", addr
);
685 static void pciej_write(void *opaque
, uint32_t addr
, uint32_t val
)
687 int slot
= ffs(val
) - 1;
689 device_hot_remove_success(0, slot
);
692 printf("pciej write %x <== %d\n", addr
, val
);
696 static const char *model
;
698 void qemu_system_hot_add_init(const char *cpu_model
)
700 register_ioport_write(GPE_BASE
, 4, 1, gpe_writeb
, &gpe
);
701 register_ioport_read(GPE_BASE
, 4, 1, gpe_readb
, &gpe
);
703 register_ioport_write(PROC_BASE
, 4, 1, gpe_writeb
, &gpe
);
704 register_ioport_read(PROC_BASE
, 4, 1, gpe_readb
, &gpe
);
706 register_ioport_write(PCI_BASE
, 8, 4, pcihotplug_write
, &pci0_status
);
707 register_ioport_read(PCI_BASE
, 8, 4, pcihotplug_read
, &pci0_status
);
709 register_ioport_write(PCI_EJ_BASE
, 4, 4, pciej_write
, NULL
);
710 register_ioport_read(PCI_EJ_BASE
, 4, 4, pciej_read
, NULL
);
715 static void enable_processor(struct gpe_regs
*g
, int cpu
)
722 static void disable_processor(struct gpe_regs
*g
, int cpu
)
726 g
->down
|= (1 << cpu
);
729 #if defined(TARGET_I386) || defined(TARGET_X86_64)
731 static CPUState
*qemu_kvm_cpu_env(int index
)
738 if (penv
->cpu_index
== index
)
740 penv
= (CPUState
*)penv
->next_cpu
;
748 void qemu_system_cpu_hot_add(int cpu
, int state
)
754 && (!qemu_kvm_cpu_env(cpu
))
757 env
= pc_new_cpu(cpu
, model
, 1);
759 fprintf(stderr
, "cpu %d creation failed\n", cpu
);
767 qemu_set_irq(pm_state
->irq
, 1);
771 enable_processor(&gpe
, cpu
);
773 disable_processor(&gpe
, cpu
);
774 qemu_set_irq(pm_state
->irq
, 0);
778 static void enable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
782 p
->up
|= (1 << slot
);
785 static void disable_device(struct pci_status
*p
, struct gpe_regs
*g
, int slot
)
789 p
->down
|= (1 << slot
);
792 void qemu_system_device_hot_add(int pcibus
, int slot
, int state
)
794 qemu_set_irq(pm_state
->irq
, 1);
796 pci0_status
.down
= 0;
798 enable_device(&pci0_status
, &gpe
, slot
);
800 disable_device(&pci0_status
, &gpe
, slot
);
801 qemu_set_irq(pm_state
->irq
, 0);