SlavIO Counter-Timers fix, by Aurelien Jarno.
[qemu] / hw / slavio_timer.c
1 /*
2  * QEMU Sparc SLAVIO timer controller emulation
3  *
4  * Copyright (c) 2003-2005 Fabrice Bellard
5  * 
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  */
24 #include "vl.h"
25
26 //#define DEBUG_TIMER
27
28 #ifdef DEBUG_TIMER
29 #define DPRINTF(fmt, args...) \
30 do { printf("TIMER: " fmt , ##args); } while (0)
31 #else
32 #define DPRINTF(fmt, args...)
33 #endif
34
35 /*
36  * Registers of hardware timer in sun4m.
37  *
38  * This is the timer/counter part of chip STP2001 (Slave I/O), also
39  * produced as NCR89C105. See
40  * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
41  * 
42  * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
43  * are zero. Bit 31 is 1 when count has been reached.
44  *
45  * Per-CPU timers interrupt local CPU, system timer uses normal
46  * interrupt routing.
47  *
48  */
49
50 typedef struct SLAVIO_TIMERState {
51     uint32_t limit, count, counthigh;
52     int64_t count_load_time;
53     int64_t expire_time;
54     int64_t stop_time, tick_offset;
55     QEMUTimer *irq_timer;
56     int irq;
57     int reached, stopped;
58     int mode; // 0 = processor, 1 = user, 2 = system
59     unsigned int cpu;
60 } SLAVIO_TIMERState;
61
62 #define TIMER_MAXADDR 0x1f
63 #define CNT_FREQ 2000000
64
65 // Update count, set irq, update expire_time
66 static void slavio_timer_get_out(SLAVIO_TIMERState *s)
67 {
68     int out;
69     int64_t diff, ticks, count;
70     uint32_t limit;
71
72     // There are three clock tick units: CPU ticks, register units
73     // (nanoseconds), and counter ticks (500 ns).
74     if (s->mode == 1 && s->stopped)
75         ticks = s->stop_time;
76     else
77         ticks = qemu_get_clock(vm_clock) - s->tick_offset;
78
79     out = (ticks > s->expire_time);
80     if (out)
81         s->reached = 0x80000000;
82     if (!s->limit)
83         limit = 0x7fffffff;
84     else
85         limit = s->limit;
86
87     // Convert register units to counter ticks
88     limit = limit >> 9;
89
90     // Convert cpu ticks to counter ticks
91     diff = muldiv64(ticks - s->count_load_time, CNT_FREQ, ticks_per_sec);
92
93     // Calculate what the counter should be, convert to register
94     // units
95     count = diff % limit;
96     s->count = count << 9;
97     s->counthigh = count >> 22;
98
99     // Expire time: CPU ticks left to next interrupt
100     // Convert remaining counter ticks to CPU ticks
101     s->expire_time = ticks + muldiv64(limit - count, ticks_per_sec, CNT_FREQ);
102
103     DPRINTF("irq %d limit %d reached %d d %" PRId64 " count %d s->c %x diff %" PRId64 " stopped %d mode %d\n", s->irq, limit, s->reached?1:0, (ticks-s->count_load_time), count, s->count, s->expire_time - ticks, s->stopped, s->mode);
104
105     if (s->mode != 1)
106         pic_set_irq_cpu(s->irq, out, s->cpu);
107 }
108
109 // timer callback
110 static void slavio_timer_irq(void *opaque)
111 {
112     SLAVIO_TIMERState *s = opaque;
113
114     if (!s->irq_timer)
115         return;
116     slavio_timer_get_out(s);
117     if (s->mode != 1)
118         qemu_mod_timer(s->irq_timer, s->expire_time);
119 }
120
121 static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
122 {
123     SLAVIO_TIMERState *s = opaque;
124     uint32_t saddr;
125
126     saddr = (addr & TIMER_MAXADDR) >> 2;
127     switch (saddr) {
128     case 0:
129         // read limit (system counter mode) or read most signifying
130         // part of counter (user mode)
131         if (s->mode != 1) {
132             // clear irq
133             pic_set_irq_cpu(s->irq, 0, s->cpu);
134             s->reached = 0;
135             return s->limit;
136         }
137         else {
138             slavio_timer_get_out(s);
139             return s->counthigh & 0x7fffffff;
140         }
141     case 1:
142         // read counter and reached bit (system mode) or read lsbits
143         // of counter (user mode)
144         slavio_timer_get_out(s);
145         if (s->mode != 1)
146             return (s->count & 0x7fffffff) | s->reached;
147         else
148             return s->count;
149     case 3:
150         // read start/stop status
151         return s->stopped;
152     case 4:
153         // read user/system mode
154         return s->mode & 1;
155     default:
156         return 0;
157     }
158 }
159
160 static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
161 {
162     SLAVIO_TIMERState *s = opaque;
163     uint32_t saddr;
164
165     saddr = (addr & TIMER_MAXADDR) >> 2;
166     switch (saddr) {
167     case 0:
168         // set limit, reset counter
169         s->count_load_time = qemu_get_clock(vm_clock);
170         // fall through
171     case 2:
172         // set limit without resetting counter
173         if (!val)
174             s->limit = 0x7fffffff;
175         else
176             s->limit = val & 0x7fffffff;
177         slavio_timer_irq(s);
178         break;
179     case 3:
180         // start/stop user counter
181         if (s->mode == 1) {
182             if (val & 1) {
183                 s->stop_time = qemu_get_clock(vm_clock);
184                 s->stopped = 1;
185             }
186             else {
187                 if (s->stopped)
188                     s->tick_offset += qemu_get_clock(vm_clock) - s->stop_time;
189                 s->stopped = 0;
190             }
191         }
192         break;
193     case 4:
194         // bit 0: user (1) or system (0) counter mode
195         if (s->mode == 0 || s->mode == 1)
196             s->mode = val & 1;
197         break;
198     default:
199         break;
200     }
201 }
202
203 static CPUReadMemoryFunc *slavio_timer_mem_read[3] = {
204     slavio_timer_mem_readl,
205     slavio_timer_mem_readl,
206     slavio_timer_mem_readl,
207 };
208
209 static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = {
210     slavio_timer_mem_writel,
211     slavio_timer_mem_writel,
212     slavio_timer_mem_writel,
213 };
214
215 static void slavio_timer_save(QEMUFile *f, void *opaque)
216 {
217     SLAVIO_TIMERState *s = opaque;
218
219     qemu_put_be32s(f, &s->limit);
220     qemu_put_be32s(f, &s->count);
221     qemu_put_be32s(f, &s->counthigh);
222     qemu_put_be64s(f, &s->count_load_time);
223     qemu_put_be64s(f, &s->expire_time);
224     qemu_put_be64s(f, &s->stop_time);
225     qemu_put_be64s(f, &s->tick_offset);
226     qemu_put_be32s(f, &s->irq);
227     qemu_put_be32s(f, &s->reached);
228     qemu_put_be32s(f, &s->stopped);
229     qemu_put_be32s(f, &s->mode);
230 }
231
232 static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id)
233 {
234     SLAVIO_TIMERState *s = opaque;
235     
236     if (version_id != 1)
237         return -EINVAL;
238
239     qemu_get_be32s(f, &s->limit);
240     qemu_get_be32s(f, &s->count);
241     qemu_get_be32s(f, &s->counthigh);
242     qemu_get_be64s(f, &s->count_load_time);
243     qemu_get_be64s(f, &s->expire_time);
244     qemu_get_be64s(f, &s->stop_time);
245     qemu_get_be64s(f, &s->tick_offset);
246     qemu_get_be32s(f, &s->irq);
247     qemu_get_be32s(f, &s->reached);
248     qemu_get_be32s(f, &s->stopped);
249     qemu_get_be32s(f, &s->mode);
250     return 0;
251 }
252
253 static void slavio_timer_reset(void *opaque)
254 {
255     SLAVIO_TIMERState *s = opaque;
256
257     s->limit = 0;
258     s->count = 0;
259     s->count_load_time = qemu_get_clock(vm_clock);;
260     s->stop_time = s->count_load_time;
261     s->tick_offset = 0;
262     s->reached = 0;
263     s->mode &= 2;
264     s->stopped = 1;
265     slavio_timer_get_out(s);
266 }
267
268 void slavio_timer_init(uint32_t addr, int irq, int mode, unsigned int cpu)
269 {
270     int slavio_timer_io_memory;
271     SLAVIO_TIMERState *s;
272
273     s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
274     if (!s)
275         return;
276     s->irq = irq;
277     s->mode = mode;
278     s->cpu = cpu;
279     s->irq_timer = qemu_new_timer(vm_clock, slavio_timer_irq, s);
280
281     slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
282                                                     slavio_timer_mem_write, s);
283     cpu_register_physical_memory(addr, TIMER_MAXADDR, slavio_timer_io_memory);
284     register_savevm("slavio_timer", addr, 1, slavio_timer_save, slavio_timer_load, s);
285     qemu_register_reset(slavio_timer_reset, s);
286     slavio_timer_reset(s);
287 }