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/>
19 * Copyright (c) 2009 Isaku Yamahata <yamahata at valinux co jp>
20 * VA Linux Systems Japan K.K.
21 * Copyright (C) 2012 Jason Baron <jbaron@redhat.com>
23 * This is based on acpi.c.
28 #include "qemu-timer.h"
37 #define ICH9_DEBUG(fmt, ...) \
38 do { printf("%s "fmt, __func__, ## __VA_ARGS__); } while (0)
40 #define ICH9_DEBUG(fmt, ...) do { } while (0)
43 static void pm_ioport_write_fallback(void *opaque
, uint32_t addr
, int len
,
45 static uint32_t pm_ioport_read_fallback(void *opaque
, uint32_t addr
, int len
);
47 static void pm_update_sci(ICH9LPCPMRegs
*pm
)
49 int sci_level
, pm1a_sts
;
51 pm1a_sts
= acpi_pm1_evt_get_sts(&pm
->acpi_regs
);
53 sci_level
= (((pm1a_sts
& pm
->acpi_regs
.pm1
.evt
.en
) &
54 (ACPI_BITMASK_RT_CLOCK_ENABLE
|
55 ACPI_BITMASK_POWER_BUTTON_ENABLE
|
56 ACPI_BITMASK_GLOBAL_LOCK_ENABLE
|
57 ACPI_BITMASK_TIMER_ENABLE
)) != 0);
58 qemu_set_irq(pm
->irq
, sci_level
);
60 /* schedule a timer interruption if needed */
61 acpi_pm_tmr_update(&pm
->acpi_regs
,
62 (pm
->acpi_regs
.pm1
.evt
.en
& ACPI_BITMASK_TIMER_ENABLE
) &&
63 !(pm1a_sts
& ACPI_BITMASK_TIMER_STATUS
));
66 static void ich9_pm_update_sci_fn(ACPIREGS
*regs
)
68 ICH9LPCPMRegs
*pm
= container_of(regs
, ICH9LPCPMRegs
, acpi_regs
);
72 static void pm_ioport_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
74 ICH9LPCPMRegs
*pm
= opaque
;
76 switch (addr
& ICH9_PMIO_MASK
) {
77 case ICH9_PMIO_GPE0_STS
... (ICH9_PMIO_GPE0_STS
+ ICH9_PMIO_GPE0_LEN
- 1):
78 acpi_gpe_ioport_writeb(&pm
->acpi_regs
, addr
, val
);
84 ICH9_DEBUG("port=0x%04x val=0x%04x\n", addr
, val
);
87 static uint32_t pm_ioport_readb(void *opaque
, uint32_t addr
)
89 ICH9LPCPMRegs
*pm
= opaque
;
92 switch (addr
& ICH9_PMIO_MASK
) {
93 case ICH9_PMIO_GPE0_STS
... (ICH9_PMIO_GPE0_STS
+ ICH9_PMIO_GPE0_LEN
- 1):
94 val
= acpi_gpe_ioport_readb(&pm
->acpi_regs
, addr
);
100 ICH9_DEBUG("port=0x%04x val=0x%04x\n", addr
, val
);
104 static void pm_ioport_writew(void *opaque
, uint32_t addr
, uint32_t val
)
106 ICH9LPCPMRegs
*pm
= opaque
;
108 switch (addr
& ICH9_PMIO_MASK
) {
109 case ICH9_PMIO_PM1_STS
:
110 acpi_pm1_evt_write_sts(&pm
->acpi_regs
, val
);
113 case ICH9_PMIO_PM1_EN
:
114 pm
->acpi_regs
.pm1
.evt
.en
= val
;
117 case ICH9_PMIO_PM1_CNT
:
118 acpi_pm1_cnt_write(&pm
->acpi_regs
, val
, 0);
121 pm_ioport_write_fallback(opaque
, addr
, 2, val
);
124 ICH9_DEBUG("port=0x%04x val=0x%04x\n", addr
, val
);
127 static uint32_t pm_ioport_readw(void *opaque
, uint32_t addr
)
129 ICH9LPCPMRegs
*pm
= opaque
;
132 switch (addr
& ICH9_PMIO_MASK
) {
133 case ICH9_PMIO_PM1_STS
:
134 val
= acpi_pm1_evt_get_sts(&pm
->acpi_regs
);
136 case ICH9_PMIO_PM1_EN
:
137 val
= pm
->acpi_regs
.pm1
.evt
.en
;
139 case ICH9_PMIO_PM1_CNT
:
140 val
= pm
->acpi_regs
.pm1
.cnt
.cnt
;
143 val
= pm_ioport_read_fallback(opaque
, addr
, 2);
146 ICH9_DEBUG("port=0x%04x val=0x%04x\n", addr
, val
);
150 static void pm_ioport_writel(void *opaque
, uint32_t addr
, uint32_t val
)
152 ICH9LPCPMRegs
*pm
= opaque
;
154 switch (addr
& ICH9_PMIO_MASK
) {
155 case ICH9_PMIO_SMI_EN
:
159 pm_ioport_write_fallback(opaque
, addr
, 4, val
);
162 ICH9_DEBUG("port=0x%04x val=0x%08x\n", addr
, val
);
165 static uint32_t pm_ioport_readl(void *opaque
, uint32_t addr
)
167 ICH9LPCPMRegs
*pm
= opaque
;
170 switch (addr
& ICH9_PMIO_MASK
) {
171 case ICH9_PMIO_PM1_TMR
:
172 val
= acpi_pm_tmr_get(&pm
->acpi_regs
);
174 case ICH9_PMIO_SMI_EN
:
179 val
= pm_ioport_read_fallback(opaque
, addr
, 4);
182 ICH9_DEBUG("port=0x%04x val=0x%08x\n", addr
, val
);
186 static void pm_ioport_write_fallback(void *opaque
, uint32_t addr
, int len
,
189 int subsize
= (len
== 4) ? 2 : 1;
190 IOPortWriteFunc
*ioport_write
=
191 (subsize
== 2) ? pm_ioport_writew
: pm_ioport_writeb
;
195 for (i
= 0; i
< len
; i
+= subsize
) {
196 ioport_write(opaque
, addr
, val
);
201 static uint32_t pm_ioport_read_fallback(void *opaque
, uint32_t addr
, int len
)
203 int subsize
= (len
== 4) ? 2 : 1;
204 IOPortReadFunc
*ioport_read
=
205 (subsize
== 2) ? pm_ioport_readw
: pm_ioport_readb
;
211 for (i
= 0; i
< len
; i
+= subsize
) {
213 val
|= ioport_read(opaque
, addr
);
219 void ich9_pm_iospace_update(ICH9LPCPMRegs
*pm
, uint32_t pm_io_base
)
221 ICH9_DEBUG("to 0x%x\n", pm_io_base
);
223 assert((pm_io_base
& ICH9_PMIO_MASK
) == 0);
225 if (pm
->pm_io_base
!= 0) {
226 isa_unassign_ioport(pm
->pm_io_base
, ICH9_PMIO_SIZE
);
230 if (pm_io_base
== 0) {
234 register_ioport_write(pm_io_base
, ICH9_PMIO_SIZE
, 1, pm_ioport_writeb
, pm
);
235 register_ioport_read(pm_io_base
, ICH9_PMIO_SIZE
, 1, pm_ioport_readb
, pm
);
236 register_ioport_write(pm_io_base
, ICH9_PMIO_SIZE
, 2, pm_ioport_writew
, pm
);
237 register_ioport_read(pm_io_base
, ICH9_PMIO_SIZE
, 2, pm_ioport_readw
, pm
);
238 register_ioport_write(pm_io_base
, ICH9_PMIO_SIZE
, 4, pm_ioport_writel
, pm
);
239 register_ioport_read(pm_io_base
, ICH9_PMIO_SIZE
, 4, pm_ioport_readl
, pm
);
241 pm
->pm_io_base
= pm_io_base
;
242 acpi_gpe_blk(&pm
->acpi_regs
, pm_io_base
+ ICH9_PMIO_GPE0_STS
);
245 static int ich9_pm_post_load(void *opaque
, int version_id
)
247 ICH9LPCPMRegs
*pm
= opaque
;
248 uint32_t pm_io_base
= pm
->pm_io_base
;
250 ich9_pm_iospace_update(pm
, pm_io_base
);
254 #define VMSTATE_GPE_ARRAY(_field, _state) \
256 .name = (stringify(_field)), \
258 .num = ICH9_PMIO_GPE0_LEN, \
259 .info = &vmstate_info_uint8, \
260 .size = sizeof(uint8_t), \
261 .flags = VMS_ARRAY | VMS_POINTER, \
262 .offset = vmstate_offset_pointer(_state, _field, uint8_t), \
265 const VMStateDescription vmstate_ich9_pm
= {
268 .minimum_version_id
= 1,
269 .minimum_version_id_old
= 1,
270 .post_load
= ich9_pm_post_load
,
271 .fields
= (VMStateField
[]) {
272 VMSTATE_UINT16(acpi_regs
.pm1
.evt
.sts
, ICH9LPCPMRegs
),
273 VMSTATE_UINT16(acpi_regs
.pm1
.evt
.en
, ICH9LPCPMRegs
),
274 VMSTATE_UINT16(acpi_regs
.pm1
.cnt
.cnt
, ICH9LPCPMRegs
),
275 VMSTATE_TIMER(acpi_regs
.tmr
.timer
, ICH9LPCPMRegs
),
276 VMSTATE_INT64(acpi_regs
.tmr
.overflow_time
, ICH9LPCPMRegs
),
277 VMSTATE_GPE_ARRAY(acpi_regs
.gpe
.sts
, ICH9LPCPMRegs
),
278 VMSTATE_GPE_ARRAY(acpi_regs
.gpe
.en
, ICH9LPCPMRegs
),
279 VMSTATE_UINT32(smi_en
, ICH9LPCPMRegs
),
280 VMSTATE_UINT32(smi_sts
, ICH9LPCPMRegs
),
281 VMSTATE_END_OF_LIST()
285 static void pm_reset(void *opaque
)
287 ICH9LPCPMRegs
*pm
= opaque
;
288 ich9_pm_iospace_update(pm
, 0);
290 acpi_pm1_evt_reset(&pm
->acpi_regs
);
291 acpi_pm1_cnt_reset(&pm
->acpi_regs
);
292 acpi_pm_tmr_reset(&pm
->acpi_regs
);
293 acpi_gpe_reset(&pm
->acpi_regs
);
298 static void pm_powerdown_req(Notifier
*n
, void *opaque
)
300 ICH9LPCPMRegs
*pm
= container_of(n
, ICH9LPCPMRegs
, powerdown_notifier
);
302 acpi_pm1_evt_power_down(&pm
->acpi_regs
);
305 void ich9_pm_init(ICH9LPCPMRegs
*pm
, qemu_irq sci_irq
, qemu_irq cmos_s3
)
307 acpi_pm_tmr_init(&pm
->acpi_regs
, ich9_pm_update_sci_fn
);
308 acpi_pm1_cnt_init(&pm
->acpi_regs
);
309 acpi_gpe_init(&pm
->acpi_regs
, ICH9_PMIO_GPE0_LEN
);
312 qemu_register_reset(pm_reset
, pm
);
313 pm
->powerdown_notifier
.notify
= pm_powerdown_req
;
314 qemu_register_powerdown_notifier(&pm
->powerdown_notifier
);