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"
32 static PITState pit_state
;
34 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
);
36 static int pit_get_count(PITChannelState
*s
)
41 d
= muldiv64(qemu_get_clock(vm_clock
) - s
->count_load_time
, PIT_FREQ
, ticks_per_sec
);
47 counter
= (s
->count
- d
) & 0xffff;
50 /* XXX: may be incorrect for odd counts */
51 counter
= s
->count
- ((2 * d
) % s
->count
);
54 counter
= s
->count
- (d
% s
->count
);
60 /* get pit output bit */
61 static int pit_get_out1(PITChannelState
*s
, int64_t current_time
)
66 d
= muldiv64(current_time
- s
->count_load_time
, PIT_FREQ
, ticks_per_sec
);
70 out
= (d
>= s
->count
);
76 if ((d
% s
->count
) == 0 && d
!= 0)
82 out
= (d
% s
->count
) < ((s
->count
+ 1) >> 1);
86 out
= (d
== s
->count
);
92 int pit_get_out(PITState
*pit
, int channel
, int64_t current_time
)
94 PITChannelState
*s
= &pit
->channels
[channel
];
95 return pit_get_out1(s
, current_time
);
98 /* return -1 if no transition will occur. */
99 static int64_t pit_get_next_transition_time(PITChannelState
*s
,
100 int64_t current_time
)
102 uint64_t d
, next_time
, base
;
105 d
= muldiv64(current_time
- s
->count_load_time
, PIT_FREQ
, ticks_per_sec
);
111 next_time
= s
->count
;
116 base
= (d
/ s
->count
) * s
->count
;
117 if ((d
- base
) == 0 && d
!= 0)
118 next_time
= base
+ s
->count
;
120 next_time
= base
+ s
->count
+ 1;
123 base
= (d
/ s
->count
) * s
->count
;
124 period2
= ((s
->count
+ 1) >> 1);
125 if ((d
- base
) < period2
)
126 next_time
= base
+ period2
;
128 next_time
= base
+ s
->count
;
133 next_time
= s
->count
;
134 else if (d
== s
->count
)
135 next_time
= s
->count
+ 1;
140 /* convert to timer units */
141 next_time
= s
->count_load_time
+ muldiv64(next_time
, ticks_per_sec
, PIT_FREQ
);
142 /* fix potential rounding problems */
143 /* XXX: better solution: use a clock at PIT_FREQ Hz */
144 if (next_time
<= current_time
)
145 next_time
= current_time
+ 1;
149 /* val must be 0 or 1 */
150 void pit_set_gate(PITState
*pit
, int channel
, int val
)
152 PITChannelState
*s
= &pit
->channels
[channel
];
158 /* XXX: just disable/enable counting */
163 /* restart counting on rising edge */
164 s
->count_load_time
= qemu_get_clock(vm_clock
);
165 pit_irq_timer_update(s
, s
->count_load_time
);
171 /* restart counting on rising edge */
172 s
->count_load_time
= qemu_get_clock(vm_clock
);
173 pit_irq_timer_update(s
, s
->count_load_time
);
175 /* XXX: disable/enable counting */
181 int pit_get_gate(PITState
*pit
, int channel
)
183 PITChannelState
*s
= &pit
->channels
[channel
];
187 int pit_get_initial_count(PITState
*pit
, int channel
)
189 PITChannelState
*s
= &pit
->channels
[channel
];
193 int pit_get_mode(PITState
*pit
, int channel
)
195 PITChannelState
*s
= &pit
->channels
[channel
];
199 static inline void pit_load_count(PITChannelState
*s
, int val
)
203 s
->count_load_time
= qemu_get_clock(vm_clock
);
205 pit_irq_timer_update(s
, s
->count_load_time
);
208 /* if already latched, do not latch again */
209 static void pit_latch_count(PITChannelState
*s
)
211 if (!s
->count_latched
) {
212 s
->latched_count
= pit_get_count(s
);
213 s
->count_latched
= s
->rw_mode
;
217 static void pit_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
219 PITState
*pit
= opaque
;
227 /* read back command */
228 for(channel
= 0; channel
< 3; channel
++) {
229 s
= &pit
->channels
[channel
];
230 if (val
& (2 << channel
)) {
234 if (!(val
& 0x10) && !s
->status_latched
) {
236 /* XXX: add BCD and null count */
237 s
->status
= (pit_get_out1(s
, qemu_get_clock(vm_clock
)) << 7) |
241 s
->status_latched
= 1;
246 s
= &pit
->channels
[channel
];
247 access
= (val
>> 4) & 3;
252 s
->read_state
= access
;
253 s
->write_state
= access
;
255 s
->mode
= (val
>> 1) & 7;
257 /* XXX: update irq timer ? */
261 s
= &pit
->channels
[addr
];
262 switch(s
->write_state
) {
265 pit_load_count(s
, val
);
268 pit_load_count(s
, val
<< 8);
271 s
->write_latch
= val
;
272 s
->write_state
= RW_STATE_WORD1
;
275 pit_load_count(s
, s
->write_latch
| (val
<< 8));
276 s
->write_state
= RW_STATE_WORD0
;
282 static uint32_t pit_ioport_read(void *opaque
, uint32_t addr
)
284 PITState
*pit
= opaque
;
289 s
= &pit
->channels
[addr
];
290 if (s
->status_latched
) {
291 s
->status_latched
= 0;
293 } else if (s
->count_latched
) {
294 switch(s
->count_latched
) {
297 ret
= s
->latched_count
& 0xff;
298 s
->count_latched
= 0;
301 ret
= s
->latched_count
>> 8;
302 s
->count_latched
= 0;
305 ret
= s
->latched_count
& 0xff;
306 s
->count_latched
= RW_STATE_MSB
;
310 switch(s
->read_state
) {
313 count
= pit_get_count(s
);
317 count
= pit_get_count(s
);
318 ret
= (count
>> 8) & 0xff;
321 count
= pit_get_count(s
);
323 s
->read_state
= RW_STATE_WORD1
;
326 count
= pit_get_count(s
);
327 ret
= (count
>> 8) & 0xff;
328 s
->read_state
= RW_STATE_WORD0
;
335 /* global counters for time-drift fix */
336 int64_t timer_acks
=0, timer_interrupts
=0, timer_ints_to_push
=0;
338 extern int time_drift_fix
;
340 static void pit_irq_timer_update(PITChannelState
*s
, int64_t current_time
)
347 expire_time
= pit_get_next_transition_time(s
, current_time
);
348 irq_level
= pit_get_out1(s
, current_time
);
349 qemu_set_irq(s
->irq
, irq_level
);
350 if (time_drift_fix
&& irq_level
==1) {
351 /* FIXME: fine tune timer_max_fix (max fix per tick).
352 * Should it be 1 (double time), 2 , 4, 10 ?
353 * Currently setting it to 5% of PIT-ticks-per-second (per PIT-tick)
355 const long pit_ticks_per_sec
= (s
->count
>0) ? (PIT_FREQ
/s
->count
) : 0;
356 const long timer_max_fix
= pit_ticks_per_sec
/20;
357 const long delta
= timer_interrupts
- timer_acks
;
358 const long max_delta
= pit_ticks_per_sec
* 60; /* one minute */
359 if ((delta
> max_delta
) && (pit_ticks_per_sec
> 0)) {
360 printf("time drift is too long, %ld seconds were lost\n", delta
/pit_ticks_per_sec
);
361 timer_acks
= timer_interrupts
;
362 timer_ints_to_push
= 0;
363 } else if (delta
> 0) {
364 timer_ints_to_push
= MIN(delta
, timer_max_fix
);
369 printf("irq_level=%d next_delay=%f\n",
371 (double)(expire_time
- current_time
) / ticks_per_sec
);
373 s
->next_transition_time
= expire_time
;
374 if (expire_time
!= -1)
375 qemu_mod_timer(s
->irq_timer
, expire_time
);
377 qemu_del_timer(s
->irq_timer
);
380 static void pit_irq_timer(void *opaque
)
382 PITChannelState
*s
= opaque
;
384 pit_irq_timer_update(s
, s
->next_transition_time
);
387 void pit_save(QEMUFile
*f
, void *opaque
)
389 PITState
*pit
= opaque
;
393 for(i
= 0; i
< 3; i
++) {
394 s
= &pit
->channels
[i
];
395 qemu_put_be32(f
, s
->count
);
396 qemu_put_be16s(f
, &s
->latched_count
);
397 qemu_put_8s(f
, &s
->count_latched
);
398 qemu_put_8s(f
, &s
->status_latched
);
399 qemu_put_8s(f
, &s
->status
);
400 qemu_put_8s(f
, &s
->read_state
);
401 qemu_put_8s(f
, &s
->write_state
);
402 qemu_put_8s(f
, &s
->write_latch
);
403 qemu_put_8s(f
, &s
->rw_mode
);
404 qemu_put_8s(f
, &s
->mode
);
405 qemu_put_8s(f
, &s
->bcd
);
406 qemu_put_8s(f
, &s
->gate
);
407 qemu_put_be64(f
, s
->count_load_time
);
409 qemu_put_be64(f
, s
->next_transition_time
);
410 qemu_put_timer(f
, s
->irq_timer
);
415 int pit_load(QEMUFile
*f
, void *opaque
, int version_id
)
417 PITState
*pit
= opaque
;
424 for(i
= 0; i
< 3; i
++) {
425 s
= &pit
->channels
[i
];
426 s
->count
=qemu_get_be32(f
);
427 qemu_get_be16s(f
, &s
->latched_count
);
428 qemu_get_8s(f
, &s
->count_latched
);
429 qemu_get_8s(f
, &s
->status_latched
);
430 qemu_get_8s(f
, &s
->status
);
431 qemu_get_8s(f
, &s
->read_state
);
432 qemu_get_8s(f
, &s
->write_state
);
433 qemu_get_8s(f
, &s
->write_latch
);
434 qemu_get_8s(f
, &s
->rw_mode
);
435 qemu_get_8s(f
, &s
->mode
);
436 qemu_get_8s(f
, &s
->bcd
);
437 qemu_get_8s(f
, &s
->gate
);
438 s
->count_load_time
=qemu_get_be64(f
);
440 s
->next_transition_time
=qemu_get_be64(f
);
441 qemu_get_timer(f
, s
->irq_timer
);
448 void pit_reset(void *opaque
)
450 PITState
*pit
= opaque
;
454 for(i
= 0;i
< 3; i
++) {
455 s
= &pit
->channels
[i
];
458 pit_load_count(s
, 0);
462 PITState
*pit_init(int base
, qemu_irq irq
)
464 PITState
*pit
= &pit_state
;
467 s
= &pit
->channels
[0];
468 /* the timer 0 is connected to an IRQ */
469 s
->irq_timer
= qemu_new_timer(vm_clock
, pit_irq_timer
, s
);
472 register_savevm(PIT_SAVEVM_NAME
, base
, PIT_SAVEVM_VERSION
,
473 pit_save
, pit_load
, pit
);
475 qemu_register_reset(pit_reset
, pit
);
476 register_ioport_write(base
, 4, 1, pit_ioport_write
, pit
);
477 register_ioport_read(base
, 3, 1, pit_ioport_read
, pit
);