2 * QEMU 8253/8254 interval timer emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "qemu-timer.h"
31 #define RW_STATE_LSB 1
32 #define RW_STATE_MSB 2
33 #define RW_STATE_WORD0 3
34 #define RW_STATE_WORD1 4
36 typedef struct PITChannelState
{
37 int count
; /* can be 65536 */
38 uint16_t latched_count
;
39 uint8_t count_latched
;
40 uint8_t status_latched
;
47 uint8_t bcd
; /* not supported */
48 uint8_t gate
; /* timer start */
49 int64_t count_load_time
;
51 int64_t next_transition_time
;
56 typedef struct PITState
{
60 PITChannelState channels
[3];
63 static PITState pit_state
;
65 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
);
67 static int pit_get_count(PITChannelState
*s
)
72 d
= muldiv64(qemu_get_clock(vm_clock
) - s
->count_load_time
, PIT_FREQ
,
79 counter
= (s
->count
- d
) & 0xffff;
82 /* XXX: may be incorrect for odd counts */
83 counter
= s
->count
- ((2 * d
) % s
->count
);
86 counter
= s
->count
- (d
% s
->count
);
92 /* get pit output bit */
93 static int pit_get_out1(PITChannelState
*s
, int64_t current_time
)
98 d
= muldiv64(current_time
- s
->count_load_time
, PIT_FREQ
,
103 out
= (d
>= s
->count
);
106 out
= (d
< s
->count
);
109 if ((d
% s
->count
) == 0 && d
!= 0)
115 out
= (d
% s
->count
) < ((s
->count
+ 1) >> 1);
119 out
= (d
== s
->count
);
125 int pit_get_out(ISADevice
*dev
, int channel
, int64_t current_time
)
127 PITState
*pit
= DO_UPCAST(PITState
, dev
, dev
);
128 PITChannelState
*s
= &pit
->channels
[channel
];
129 return pit_get_out1(s
, current_time
);
132 /* return -1 if no transition will occur. */
133 static int64_t pit_get_next_transition_time(PITChannelState
*s
,
134 int64_t current_time
)
136 uint64_t d
, next_time
, base
;
139 d
= muldiv64(current_time
- s
->count_load_time
, PIT_FREQ
,
140 get_ticks_per_sec());
146 next_time
= s
->count
;
151 base
= (d
/ s
->count
) * s
->count
;
152 if ((d
- base
) == 0 && d
!= 0)
153 next_time
= base
+ s
->count
;
155 next_time
= base
+ s
->count
+ 1;
158 base
= (d
/ s
->count
) * s
->count
;
159 period2
= ((s
->count
+ 1) >> 1);
160 if ((d
- base
) < period2
)
161 next_time
= base
+ period2
;
163 next_time
= base
+ s
->count
;
168 next_time
= s
->count
;
169 else if (d
== s
->count
)
170 next_time
= s
->count
+ 1;
175 /* convert to timer units */
176 next_time
= s
->count_load_time
+ muldiv64(next_time
, get_ticks_per_sec(),
178 /* fix potential rounding problems */
179 /* XXX: better solution: use a clock at PIT_FREQ Hz */
180 if (next_time
<= current_time
)
181 next_time
= current_time
+ 1;
185 /* val must be 0 or 1 */
186 void pit_set_gate(ISADevice
*dev
, int channel
, int val
)
188 PITState
*pit
= DO_UPCAST(PITState
, dev
, dev
);
189 PITChannelState
*s
= &pit
->channels
[channel
];
195 /* XXX: just disable/enable counting */
200 /* restart counting on rising edge */
201 s
->count_load_time
= qemu_get_clock(vm_clock
);
202 pit_irq_timer_update(s
, s
->count_load_time
);
208 /* restart counting on rising edge */
209 s
->count_load_time
= qemu_get_clock(vm_clock
);
210 pit_irq_timer_update(s
, s
->count_load_time
);
212 /* XXX: disable/enable counting */
218 int pit_get_gate(ISADevice
*dev
, int channel
)
220 PITState
*pit
= DO_UPCAST(PITState
, dev
, dev
);
221 PITChannelState
*s
= &pit
->channels
[channel
];
225 int pit_get_initial_count(ISADevice
*dev
, int channel
)
227 PITState
*pit
= DO_UPCAST(PITState
, dev
, dev
);
228 PITChannelState
*s
= &pit
->channels
[channel
];
232 int pit_get_mode(ISADevice
*dev
, int channel
)
234 PITState
*pit
= DO_UPCAST(PITState
, dev
, dev
);
235 PITChannelState
*s
= &pit
->channels
[channel
];
239 static inline void pit_load_count(PITChannelState
*s
, int val
)
243 s
->count_load_time
= qemu_get_clock(vm_clock
);
245 pit_irq_timer_update(s
, s
->count_load_time
);
248 /* if already latched, do not latch again */
249 static void pit_latch_count(PITChannelState
*s
)
251 if (!s
->count_latched
) {
252 s
->latched_count
= pit_get_count(s
);
253 s
->count_latched
= s
->rw_mode
;
257 static void pit_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
259 PITState
*pit
= opaque
;
267 /* read back command */
268 for(channel
= 0; channel
< 3; channel
++) {
269 s
= &pit
->channels
[channel
];
270 if (val
& (2 << channel
)) {
274 if (!(val
& 0x10) && !s
->status_latched
) {
276 /* XXX: add BCD and null count */
277 s
->status
= (pit_get_out1(s
, qemu_get_clock(vm_clock
)) << 7) |
281 s
->status_latched
= 1;
286 s
= &pit
->channels
[channel
];
287 access
= (val
>> 4) & 3;
292 s
->read_state
= access
;
293 s
->write_state
= access
;
295 s
->mode
= (val
>> 1) & 7;
297 /* XXX: update irq timer ? */
301 s
= &pit
->channels
[addr
];
302 switch(s
->write_state
) {
305 pit_load_count(s
, val
);
308 pit_load_count(s
, val
<< 8);
311 s
->write_latch
= val
;
312 s
->write_state
= RW_STATE_WORD1
;
315 pit_load_count(s
, s
->write_latch
| (val
<< 8));
316 s
->write_state
= RW_STATE_WORD0
;
322 static uint32_t pit_ioport_read(void *opaque
, uint32_t addr
)
324 PITState
*pit
= opaque
;
329 s
= &pit
->channels
[addr
];
330 if (s
->status_latched
) {
331 s
->status_latched
= 0;
333 } else if (s
->count_latched
) {
334 switch(s
->count_latched
) {
337 ret
= s
->latched_count
& 0xff;
338 s
->count_latched
= 0;
341 ret
= s
->latched_count
>> 8;
342 s
->count_latched
= 0;
345 ret
= s
->latched_count
& 0xff;
346 s
->count_latched
= RW_STATE_MSB
;
350 switch(s
->read_state
) {
353 count
= pit_get_count(s
);
357 count
= pit_get_count(s
);
358 ret
= (count
>> 8) & 0xff;
361 count
= pit_get_count(s
);
363 s
->read_state
= RW_STATE_WORD1
;
366 count
= pit_get_count(s
);
367 ret
= (count
>> 8) & 0xff;
368 s
->read_state
= RW_STATE_WORD0
;
375 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
)
382 expire_time
= pit_get_next_transition_time(s
, current_time
);
383 irq_level
= pit_get_out1(s
, current_time
);
384 qemu_set_irq(s
->irq
, irq_level
);
386 printf("irq_level=%d next_delay=%f\n",
388 (double)(expire_time
- current_time
) / get_ticks_per_sec());
390 s
->next_transition_time
= expire_time
;
391 if (expire_time
!= -1)
392 qemu_mod_timer(s
->irq_timer
, expire_time
);
394 qemu_del_timer(s
->irq_timer
);
397 static void pit_irq_timer(void *opaque
)
399 PITChannelState
*s
= opaque
;
401 pit_irq_timer_update(s
, s
->next_transition_time
);
404 static const VMStateDescription vmstate_pit_channel
= {
405 .name
= "pit channel",
407 .minimum_version_id
= 2,
408 .minimum_version_id_old
= 2,
409 .fields
= (VMStateField
[]) {
410 VMSTATE_INT32(count
, PITChannelState
),
411 VMSTATE_UINT16(latched_count
, PITChannelState
),
412 VMSTATE_UINT8(count_latched
, PITChannelState
),
413 VMSTATE_UINT8(status_latched
, PITChannelState
),
414 VMSTATE_UINT8(status
, PITChannelState
),
415 VMSTATE_UINT8(read_state
, PITChannelState
),
416 VMSTATE_UINT8(write_state
, PITChannelState
),
417 VMSTATE_UINT8(write_latch
, PITChannelState
),
418 VMSTATE_UINT8(rw_mode
, PITChannelState
),
419 VMSTATE_UINT8(mode
, PITChannelState
),
420 VMSTATE_UINT8(bcd
, PITChannelState
),
421 VMSTATE_UINT8(gate
, PITChannelState
),
422 VMSTATE_INT64(count_load_time
, PITChannelState
),
423 VMSTATE_INT64(next_transition_time
, PITChannelState
),
424 VMSTATE_END_OF_LIST()
428 static int pit_load_old(QEMUFile
*f
, void *opaque
, int version_id
)
430 PITState
*pit
= opaque
;
437 for(i
= 0; i
< 3; i
++) {
438 s
= &pit
->channels
[i
];
439 s
->count
=qemu_get_be32(f
);
440 qemu_get_be16s(f
, &s
->latched_count
);
441 qemu_get_8s(f
, &s
->count_latched
);
442 qemu_get_8s(f
, &s
->status_latched
);
443 qemu_get_8s(f
, &s
->status
);
444 qemu_get_8s(f
, &s
->read_state
);
445 qemu_get_8s(f
, &s
->write_state
);
446 qemu_get_8s(f
, &s
->write_latch
);
447 qemu_get_8s(f
, &s
->rw_mode
);
448 qemu_get_8s(f
, &s
->mode
);
449 qemu_get_8s(f
, &s
->bcd
);
450 qemu_get_8s(f
, &s
->gate
);
451 s
->count_load_time
=qemu_get_be64(f
);
453 s
->next_transition_time
=qemu_get_be64(f
);
454 qemu_get_timer(f
, s
->irq_timer
);
460 static const VMStateDescription vmstate_pit
= {
463 .minimum_version_id
= 2,
464 .minimum_version_id_old
= 1,
465 .load_state_old
= pit_load_old
,
466 .fields
= (VMStateField
[]) {
467 VMSTATE_STRUCT_ARRAY(channels
, PITState
, 3, 2, vmstate_pit_channel
, PITChannelState
),
468 VMSTATE_TIMER(channels
[0].irq_timer
, PITState
),
469 VMSTATE_END_OF_LIST()
473 static void pit_reset(DeviceState
*dev
)
475 PITState
*pit
= container_of(dev
, PITState
, dev
.qdev
);
479 for(i
= 0;i
< 3; i
++) {
480 s
= &pit
->channels
[i
];
483 pit_load_count(s
, 0);
487 /* When HPET is operating in legacy mode, i8254 timer0 is disabled */
488 void hpet_pit_disable(void) {
490 s
= &pit_state
.channels
[0];
492 qemu_del_timer(s
->irq_timer
);
495 /* When HPET is reset or leaving legacy mode, it must reenable i8254
499 void hpet_pit_enable(void)
501 PITState
*pit
= &pit_state
;
503 s
= &pit
->channels
[0];
506 pit_load_count(s
, 0);
509 static int pit_initfn(ISADevice
*dev
)
511 PITState
*pit
= DO_UPCAST(PITState
, dev
, dev
);
514 s
= &pit
->channels
[0];
515 /* the timer 0 is connected to an IRQ */
516 s
->irq_timer
= qemu_new_timer(vm_clock
, pit_irq_timer
, s
);
517 s
->irq
= isa_get_irq(pit
->irq
);
519 register_ioport_write(pit
->iobase
, 4, 1, pit_ioport_write
, pit
);
520 register_ioport_read(pit
->iobase
, 3, 1, pit_ioport_read
, pit
);
521 isa_init_ioport(dev
, pit
->iobase
);
526 static ISADeviceInfo pit_info
= {
527 .qdev
.name
= "isa-pit",
528 .qdev
.size
= sizeof(PITState
),
529 .qdev
.vmsd
= &vmstate_pit
,
530 .qdev
.reset
= pit_reset
,
533 .qdev
.props
= (Property
[]) {
534 DEFINE_PROP_UINT32("irq", PITState
, irq
, -1),
535 DEFINE_PROP_HEX32("iobase", PITState
, iobase
, -1),
536 DEFINE_PROP_END_OF_LIST(),
540 static void pit_register(void)
542 isa_qdev_register(&pit_info
);
544 device_init(pit_register
)