Change ptimer API to use 64-bit values, add save and load methods
[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     ptimer_state *timer;
52     uint32_t count, counthigh, reached;
53     uint64_t limit;
54     int irq;
55     int stopped;
56     int mode; // 0 = processor, 1 = user, 2 = system
57     unsigned int cpu;
58     void *intctl;
59 } SLAVIO_TIMERState;
60
61 #define TIMER_MAXADDR 0x1f
62
63 // Update count, set irq, update expire_time
64 // Convert from ptimer countdown units
65 static void slavio_timer_get_out(SLAVIO_TIMERState *s)
66 {
67     uint64_t count;
68
69     count = s->limit - (ptimer_get_count(s->timer) << 9);
70     DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", s->limit, s->counthigh,
71             s->count);
72     s->count = count & 0xfffffe00;
73     s->counthigh = count >> 32;
74 }
75
76 // timer callback
77 static void slavio_timer_irq(void *opaque)
78 {
79     SLAVIO_TIMERState *s = opaque;
80
81     slavio_timer_get_out(s);
82     DPRINTF("callback: count %x%08x\n", s->counthigh, s->count);
83     s->reached = 0x80000000;
84     if (s->mode != 1)
85         pic_set_irq_cpu(s->intctl, s->irq, 1, s->cpu);
86 }
87
88 static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
89 {
90     SLAVIO_TIMERState *s = opaque;
91     uint32_t saddr, ret;
92
93     saddr = (addr & TIMER_MAXADDR) >> 2;
94     switch (saddr) {
95     case 0:
96         // read limit (system counter mode) or read most signifying
97         // part of counter (user mode)
98         if (s->mode != 1) {
99             // clear irq
100             pic_set_irq_cpu(s->intctl, s->irq, 0, s->cpu);
101             s->reached = 0;
102             ret = s->limit & 0x7fffffff;
103         }
104         else {
105             slavio_timer_get_out(s);
106             ret = s->counthigh & 0x7fffffff;
107         }
108         break;
109     case 1:
110         // read counter and reached bit (system mode) or read lsbits
111         // of counter (user mode)
112         slavio_timer_get_out(s);
113         if (s->mode != 1)
114             ret = (s->count & 0x7fffffff) | s->reached;
115         else
116             ret = s->count;
117         break;
118     case 3:
119         // read start/stop status
120         ret = s->stopped;
121         break;
122     case 4:
123         // read user/system mode
124         ret = s->mode & 1;
125         break;
126     default:
127         ret = 0;
128         break;
129     }
130     DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret);
131
132     return ret;
133 }
134
135 static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
136 {
137     SLAVIO_TIMERState *s = opaque;
138     uint32_t saddr;
139     int reload = 0;
140
141     DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val);
142     saddr = (addr & TIMER_MAXADDR) >> 2;
143     switch (saddr) {
144     case 0:
145         // set limit, reset counter
146         reload = 1;
147         pic_set_irq_cpu(s->intctl, s->irq, 0, s->cpu);
148         // fall through
149     case 2:
150         // set limit without resetting counter
151         s->limit = val & 0x7ffffe00ULL;
152         if (!s->limit)
153             s->limit = 0x7ffffe00ULL;
154         ptimer_set_limit(s->timer, s->limit >> 9, reload);
155         break;
156     case 3:
157         // start/stop user counter
158         if (s->mode == 1) {
159             if (val & 1) {
160                 ptimer_stop(s->timer);
161                 s->stopped = 1;
162             }
163             else {
164                 ptimer_run(s->timer, 0);
165                 s->stopped = 0;
166             }
167         }
168         break;
169     case 4:
170         // bit 0: user (1) or system (0) counter mode
171         if (s->mode == 0 || s->mode == 1)
172             s->mode = val & 1;
173         if (s->mode == 1) {
174             pic_set_irq_cpu(s->intctl, s->irq, 0, s->cpu);
175             s->limit = -1ULL;
176         }
177         ptimer_set_limit(s->timer, s->limit >> 9, 1);
178         break;
179     default:
180         break;
181     }
182 }
183
184 static CPUReadMemoryFunc *slavio_timer_mem_read[3] = {
185     slavio_timer_mem_readl,
186     slavio_timer_mem_readl,
187     slavio_timer_mem_readl,
188 };
189
190 static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = {
191     slavio_timer_mem_writel,
192     slavio_timer_mem_writel,
193     slavio_timer_mem_writel,
194 };
195
196 static void slavio_timer_save(QEMUFile *f, void *opaque)
197 {
198     SLAVIO_TIMERState *s = opaque;
199
200     qemu_put_be64s(f, &s->limit);
201     qemu_put_be32s(f, &s->count);
202     qemu_put_be32s(f, &s->counthigh);
203     qemu_put_be32s(f, &s->irq);
204     qemu_put_be32s(f, &s->reached);
205     qemu_put_be32s(f, &s->stopped);
206     qemu_put_be32s(f, &s->mode);
207     qemu_put_ptimer(f, s->timer);
208 }
209
210 static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id)
211 {
212     SLAVIO_TIMERState *s = opaque;
213     
214     if (version_id != 2)
215         return -EINVAL;
216
217     qemu_get_be64s(f, &s->limit);
218     qemu_get_be32s(f, &s->count);
219     qemu_get_be32s(f, &s->counthigh);
220     qemu_get_be32s(f, &s->irq);
221     qemu_get_be32s(f, &s->reached);
222     qemu_get_be32s(f, &s->stopped);
223     qemu_get_be32s(f, &s->mode);
224     qemu_get_ptimer(f, s->timer);
225
226     return 0;
227 }
228
229 static void slavio_timer_reset(void *opaque)
230 {
231     SLAVIO_TIMERState *s = opaque;
232
233     s->limit = 0x7ffffe00ULL;
234     s->count = 0;
235     s->reached = 0;
236     s->mode &= 2;
237     ptimer_set_limit(s->timer, s->limit >> 9, 1);
238     ptimer_run(s->timer, 0);
239     s->stopped = 1;
240     slavio_timer_irq(s);
241 }
242
243 void slavio_timer_init(target_phys_addr_t addr, int irq, int mode,
244                        unsigned int cpu, void *intctl)
245 {
246     int slavio_timer_io_memory;
247     SLAVIO_TIMERState *s;
248     QEMUBH *bh;
249
250     s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
251     if (!s)
252         return;
253     s->irq = irq;
254     s->mode = mode;
255     s->cpu = cpu;
256     bh = qemu_bh_new(slavio_timer_irq, s);
257     s->timer = ptimer_init(bh);
258     ptimer_set_period(s->timer, 500ULL);
259     s->intctl = intctl;
260
261     slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
262                                                     slavio_timer_mem_write, s);
263     cpu_register_physical_memory(addr, TIMER_MAXADDR, slavio_timer_io_memory);
264     register_savevm("slavio_timer", addr, 2, slavio_timer_save, slavio_timer_load, s);
265     qemu_register_reset(slavio_timer_reset, s);
266     slavio_timer_reset(s);
267 }