2 * QEMU ICH9 TCO emulation
4 * Copyright (c) 2015 Paulo Alcantara <pcacjr@zytor.com>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "sysemu/watchdog.h"
12 #include "hw/i386/ich9.h"
13 #include "migration/vmstate.h"
15 #include "hw/acpi/ich9_tco.h"
19 TCO_RLD_DEFAULT
= 0x0000,
20 TCO_DAT_IN_DEFAULT
= 0x00,
21 TCO_DAT_OUT_DEFAULT
= 0x00,
22 TCO1_STS_DEFAULT
= 0x0000,
23 TCO2_STS_DEFAULT
= 0x0000,
24 TCO1_CNT_DEFAULT
= 0x0000,
25 TCO2_CNT_DEFAULT
= 0x0008,
26 TCO_MESSAGE1_DEFAULT
= 0x00,
27 TCO_MESSAGE2_DEFAULT
= 0x00,
28 TCO_WDCNT_DEFAULT
= 0x00,
29 TCO_TMR_DEFAULT
= 0x0004,
30 SW_IRQ_GEN_DEFAULT
= 0x03,
33 static inline void tco_timer_reload(TCOIORegs
*tr
)
35 int ticks
= tr
->tco
.tmr
& TCO_TMR_MASK
;
36 int64_t nsec
= (int64_t)ticks
* TCO_TICK_NSEC
;
38 trace_tco_timer_reload(ticks
, nsec
/ 1000000);
39 tr
->expire_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + nsec
;
40 timer_mod(tr
->tco_timer
, tr
->expire_time
);
43 static inline void tco_timer_stop(TCOIORegs
*tr
)
46 timer_del(tr
->tco_timer
);
49 static void tco_timer_expired(void *opaque
)
51 TCOIORegs
*tr
= opaque
;
52 ICH9LPCPMRegs
*pm
= container_of(tr
, ICH9LPCPMRegs
, tco_regs
);
53 ICH9LPCState
*lpc
= container_of(pm
, ICH9LPCState
, pm
);
54 uint32_t gcs
= pci_get_long(lpc
->chip_config
+ ICH9_CC_GCS
);
56 trace_tco_timer_expired(tr
->timeouts_no
,
57 lpc
->pin_strap
.spkr_hi
,
58 !!(gcs
& ICH9_CC_GCS_NO_REBOOT
));
60 tr
->tco
.sts1
|= TCO_TIMEOUT
;
61 if (++tr
->timeouts_no
== 2) {
62 tr
->tco
.sts2
|= TCO_SECOND_TO_STS
;
63 tr
->tco
.sts2
|= TCO_BOOT_STS
;
66 if (!lpc
->pin_strap
.spkr_hi
&& !(gcs
& ICH9_CC_GCS_NO_REBOOT
)) {
67 watchdog_perform_action();
73 if (pm
->smi_en
& ICH9_PMIO_SMI_EN_TCO_EN
) {
76 tr
->tco
.rld
= tr
->tco
.tmr
;
80 /* NOTE: values of 0 or 1 will be ignored by ICH */
81 static inline int can_start_tco_timer(TCOIORegs
*tr
)
83 return !(tr
->tco
.cnt1
& TCO_TMR_HLT
) && tr
->tco
.tmr
> 1;
86 static uint32_t tco_ioport_readw(TCOIORegs
*tr
, uint32_t addr
)
93 if (tr
->expire_time
!= -1) {
94 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
95 int64_t elapsed
= (tr
->expire_time
- now
) / TCO_TICK_NSEC
;
96 rld
= (uint16_t)elapsed
| (tr
->tco
.rld
& ~TCO_RLD_MASK
);
133 ret
= tr
->sw_irq_gen
;
136 trace_tco_io_read(addr
, ret
);
140 static void tco_ioport_writew(TCOIORegs
*tr
, uint32_t addr
, uint32_t val
)
142 trace_tco_io_write(addr
, val
);
146 if (can_start_tco_timer(tr
)) {
147 tr
->tco
.rld
= tr
->tco
.tmr
;
148 tco_timer_reload(tr
);
155 tr
->tco
.sts1
|= SW_TCO_SMI
;
160 tr
->tco
.sts1
|= TCO_INT_STS
;
161 /* TODO: cause an interrupt, as selected by the TCO_INT_SEL bits */
164 tr
->tco
.sts1
= val
& TCO1_STS_MASK
;
167 tr
->tco
.sts2
= val
& TCO2_STS_MASK
;
170 val
&= TCO1_CNT_MASK
;
172 * once TCO_LOCK bit is set, it can not be cleared by software. a reset
173 * is required to change this bit from 1 to 0 -- it defaults to 0.
175 tr
->tco
.cnt1
= val
| (tr
->tco
.cnt1
& TCO_LOCK
);
176 if (can_start_tco_timer(tr
)) {
177 tr
->tco
.rld
= tr
->tco
.tmr
;
178 tco_timer_reload(tr
);
199 tr
->sw_irq_gen
= val
;
204 static uint64_t tco_io_readw(void *opaque
, hwaddr addr
, unsigned width
)
206 TCOIORegs
*tr
= opaque
;
207 return tco_ioport_readw(tr
, addr
);
210 static void tco_io_writew(void *opaque
, hwaddr addr
, uint64_t val
,
213 TCOIORegs
*tr
= opaque
;
214 tco_ioport_writew(tr
, addr
, val
);
217 static const MemoryRegionOps tco_io_ops
= {
218 .read
= tco_io_readw
,
219 .write
= tco_io_writew
,
220 .valid
.min_access_size
= 1,
221 .valid
.max_access_size
= 4,
222 .impl
.min_access_size
= 1,
223 .impl
.max_access_size
= 2,
224 .endianness
= DEVICE_LITTLE_ENDIAN
,
227 void acpi_pm_tco_init(TCOIORegs
*tr
, MemoryRegion
*parent
)
231 .rld
= TCO_RLD_DEFAULT
,
232 .din
= TCO_DAT_IN_DEFAULT
,
233 .dout
= TCO_DAT_OUT_DEFAULT
,
234 .sts1
= TCO1_STS_DEFAULT
,
235 .sts2
= TCO2_STS_DEFAULT
,
236 .cnt1
= TCO1_CNT_DEFAULT
,
237 .cnt2
= TCO2_CNT_DEFAULT
,
238 .msg1
= TCO_MESSAGE1_DEFAULT
,
239 .msg2
= TCO_MESSAGE2_DEFAULT
,
240 .wdcnt
= TCO_WDCNT_DEFAULT
,
241 .tmr
= TCO_TMR_DEFAULT
,
243 .sw_irq_gen
= SW_IRQ_GEN_DEFAULT
,
244 .tco_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, tco_timer_expired
, tr
),
248 memory_region_init_io(&tr
->io
, memory_region_owner(parent
),
249 &tco_io_ops
, tr
, "sm-tco", ICH9_PMIO_TCO_LEN
);
250 memory_region_add_subregion(parent
, ICH9_PMIO_TCO_RLD
, &tr
->io
);
253 const VMStateDescription vmstate_tco_io_sts
= {
254 .name
= "tco io device status",
256 .minimum_version_id
= 1,
257 .fields
= (VMStateField
[]) {
258 VMSTATE_UINT16(tco
.rld
, TCOIORegs
),
259 VMSTATE_UINT8(tco
.din
, TCOIORegs
),
260 VMSTATE_UINT8(tco
.dout
, TCOIORegs
),
261 VMSTATE_UINT16(tco
.sts1
, TCOIORegs
),
262 VMSTATE_UINT16(tco
.sts2
, TCOIORegs
),
263 VMSTATE_UINT16(tco
.cnt1
, TCOIORegs
),
264 VMSTATE_UINT16(tco
.cnt2
, TCOIORegs
),
265 VMSTATE_UINT8(tco
.msg1
, TCOIORegs
),
266 VMSTATE_UINT8(tco
.msg2
, TCOIORegs
),
267 VMSTATE_UINT8(tco
.wdcnt
, TCOIORegs
),
268 VMSTATE_UINT16(tco
.tmr
, TCOIORegs
),
269 VMSTATE_UINT8(sw_irq_gen
, TCOIORegs
),
270 VMSTATE_TIMER_PTR(tco_timer
, TCOIORegs
),
271 VMSTATE_INT64(expire_time
, TCOIORegs
),
272 VMSTATE_UINT8(timeouts_no
, TCOIORegs
),
273 VMSTATE_END_OF_LIST()