Add periodic timer implementation.
[qemu] / hw / ptimer.c
1 /* 
2  * General purpose implementation of a simple periodic countdown timer.
3  *
4  * Copyright (c) 2007 CodeSourcery.
5  *
6  * This code is licenced under the GNU LGPL.
7  */
8 #include "vl.h"
9
10
11 struct ptimer_state
12 {
13     int enabled; /* 0 = disabled, 1 = periodic, 2 = oneshot.  */
14     uint32_t limit;
15     uint32_t delta;
16     uint32_t period_frac;
17     int64_t period;
18     int64_t last_event;
19     int64_t next_event;
20     QEMUBH *bh;
21     QEMUTimer *timer;
22 };
23
24 /* Use a bottom-half routine to avoid reentrancy issues.  */
25 static void ptimer_trigger(ptimer_state *s)
26 {
27     if (s->bh) {
28         qemu_bh_schedule(s->bh);
29     }
30 }
31
32 static void ptimer_reload(ptimer_state *s)
33 {
34     if (s->delta == 0) {
35         ptimer_trigger(s);
36         s->delta = s->limit;
37     }
38     if (s->delta == 0 || s->period == 0) {
39         fprintf(stderr, "Timer with period zero, disabling\n");
40         s->enabled = 0;
41         return;
42     }
43
44     s->last_event = s->next_event;
45     s->next_event = s->last_event + s->delta * s->period;
46     if (s->period_frac) {
47         s->next_event += ((int64_t)s->period_frac * s->delta) >> 32;
48     }
49     qemu_mod_timer(s->timer, s->next_event);
50 }
51
52 static void ptimer_tick(void *opaque)
53 {
54     ptimer_state *s = (ptimer_state *)opaque;
55     ptimer_trigger(s);
56     s->delta = 0;
57     if (s->enabled == 2) {
58         s->enabled = 0;
59     } else {
60         ptimer_reload(s);
61     }
62 }
63
64 uint32_t ptimer_get_count(ptimer_state *s)
65 {
66     int64_t now;
67     uint32_t counter;
68
69     if (s->enabled) {
70         now = qemu_get_clock(vm_clock);
71         /* Figure out the current counter value.  */
72         if (now - s->next_event > 0
73             || s->period == 0) {
74             /* Prevent timer underflowing if it should already have
75                triggered.  */
76             counter = 0;
77         } else {
78             int64_t rem;
79             int64_t div;
80
81             rem = s->next_event - now;
82             div = s->period;
83             counter = rem / div;
84         }
85     } else {
86         counter = s->delta;
87     }
88     return counter;
89 }
90
91 void ptimer_set_count(ptimer_state *s, uint32_t count)
92 {
93     s->delta = count;
94     if (s->enabled) {
95         s->next_event = qemu_get_clock(vm_clock);
96         ptimer_reload(s);
97     }
98 }
99
100 void ptimer_run(ptimer_state *s, int oneshot)
101 {
102     if (s->period == 0) {
103         fprintf(stderr, "Timer with period zero, disabling\n");
104         return;
105     }
106     s->enabled = oneshot ? 2 : 1;
107     s->next_event = qemu_get_clock(vm_clock);
108     ptimer_reload(s);
109 }
110
111 /* Pause a timer.  Note that this may cause it to "loose" time, even if it
112    is immediately restarted.  */
113 void ptimer_stop(ptimer_state *s)
114 {
115     if (!s->enabled)
116         return;
117
118     s->delta = ptimer_get_count(s);
119     qemu_del_timer(s->timer);
120     s->enabled = 0;
121 }
122
123 /* Set counter increment interval in nanoseconds.  */
124 void ptimer_set_period(ptimer_state *s, int64_t period)
125 {
126     if (s->enabled) {
127         fprintf(stderr, "FIXME: ptimer_set_period with running timer");
128     }
129     s->period = period;
130     s->period_frac = 0;
131 }
132
133 /* Set counter frequency in Hz.  */
134 void ptimer_set_freq(ptimer_state *s, uint32_t freq)
135 {
136     if (s->enabled) {
137         fprintf(stderr, "FIXME: ptimer_set_freq with running timer");
138     }
139     s->period = 1000000000ll / freq;
140     s->period_frac = (1000000000ll << 32) / freq;
141 }
142
143 /* Set the initial countdown value.  If reload is nonzero then also set
144    count = limit.  */
145 void ptimer_set_limit(ptimer_state *s, uint32_t limit, int reload)
146 {
147     if (s->enabled) {
148         fprintf(stderr, "FIXME: ptimer_set_limit with running timer");
149     }
150     s->limit = limit;
151     if (reload)
152         s->delta = limit;
153 }
154
155 ptimer_state *ptimer_init(QEMUBH *bh)
156 {
157     ptimer_state *s;
158
159     s = (ptimer_state *)qemu_mallocz(sizeof(ptimer_state));
160     s->bh = bh;
161     s->timer = qemu_new_timer(vm_clock, ptimer_tick, s);
162     return s;
163 }
164