Use full 36-bit physical address space on SS10
[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     void *intctl;
61 } SLAVIO_TIMERState;
62
63 #define TIMER_MAXADDR 0x1f
64 #define CNT_FREQ 2000000
65
66 // Update count, set irq, update expire_time
67 static void slavio_timer_get_out(SLAVIO_TIMERState *s)
68 {
69     int out;
70     int64_t diff, ticks, count;
71     uint32_t limit;
72
73     // There are three clock tick units: CPU ticks, register units
74     // (nanoseconds), and counter ticks (500 ns).
75     if (s->mode == 1 && s->stopped)
76         ticks = s->stop_time;
77     else
78         ticks = qemu_get_clock(vm_clock) - s->tick_offset;
79
80     out = (ticks > s->expire_time);
81     if (out)
82         s->reached = 0x80000000;
83     // Convert register units to counter ticks
84     limit = s->limit >> 9;
85
86     if (!limit)
87         limit = 0x7fffffff >> 9;
88
89     // Convert cpu ticks to counter ticks
90     diff = muldiv64(ticks - s->count_load_time, CNT_FREQ, ticks_per_sec);
91
92     // Calculate what the counter should be, convert to register
93     // units
94     count = diff % limit;
95     s->count = count << 9;
96     s->counthigh = count >> 22;
97
98     // Expire time: CPU ticks left to next interrupt
99     // Convert remaining counter ticks to CPU ticks
100     s->expire_time = ticks + muldiv64(limit - count, ticks_per_sec, CNT_FREQ);
101
102     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);
103
104     if (s->mode != 1)
105         pic_set_irq_cpu(s->intctl, s->irq, out, s->cpu);
106 }
107
108 // timer callback
109 static void slavio_timer_irq(void *opaque)
110 {
111     SLAVIO_TIMERState *s = opaque;
112
113     if (!s->irq_timer)
114         return;
115     slavio_timer_get_out(s);
116     if (s->mode != 1)
117         qemu_mod_timer(s->irq_timer, s->expire_time);
118 }
119
120 static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
121 {
122     SLAVIO_TIMERState *s = opaque;
123     uint32_t saddr;
124
125     saddr = (addr & TIMER_MAXADDR) >> 2;
126     switch (saddr) {
127     case 0:
128         // read limit (system counter mode) or read most signifying
129         // part of counter (user mode)
130         if (s->mode != 1) {
131             // clear irq
132             pic_set_irq_cpu(s->intctl, s->irq, 0, s->cpu);
133             s->reached = 0;
134             return s->limit;
135         }
136         else {
137             slavio_timer_get_out(s);
138             return s->counthigh & 0x7fffffff;
139         }
140     case 1:
141         // read counter and reached bit (system mode) or read lsbits
142         // of counter (user mode)
143         slavio_timer_get_out(s);
144         if (s->mode != 1)
145             return (s->count & 0x7fffffff) | s->reached;
146         else
147             return s->count;
148     case 3:
149         // read start/stop status
150         return s->stopped;
151     case 4:
152         // read user/system mode
153         return s->mode & 1;
154     default:
155         return 0;
156     }
157 }
158
159 static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
160 {
161     SLAVIO_TIMERState *s = opaque;
162     uint32_t saddr;
163
164     saddr = (addr & TIMER_MAXADDR) >> 2;
165     switch (saddr) {
166     case 0:
167         // set limit, reset counter
168         s->count_load_time = qemu_get_clock(vm_clock);
169         // fall through
170     case 2:
171         // set limit without resetting counter
172         if (!val)
173             s->limit = 0x7fffffff;
174         else
175             s->limit = val & 0x7fffffff;
176         slavio_timer_irq(s);
177         break;
178     case 3:
179         // start/stop user counter
180         if (s->mode == 1) {
181             if (val & 1) {
182                 s->stop_time = qemu_get_clock(vm_clock);
183                 s->stopped = 1;
184             }
185             else {
186                 if (s->stopped)
187                     s->tick_offset += qemu_get_clock(vm_clock) - s->stop_time;
188                 s->stopped = 0;
189             }
190         }
191         break;
192     case 4:
193         // bit 0: user (1) or system (0) counter mode
194         if (s->mode == 0 || s->mode == 1)
195             s->mode = val & 1;
196         break;
197     default:
198         break;
199     }
200 }
201
202 static CPUReadMemoryFunc *slavio_timer_mem_read[3] = {
203     slavio_timer_mem_readl,
204     slavio_timer_mem_readl,
205     slavio_timer_mem_readl,
206 };
207
208 static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = {
209     slavio_timer_mem_writel,
210     slavio_timer_mem_writel,
211     slavio_timer_mem_writel,
212 };
213
214 static void slavio_timer_save(QEMUFile *f, void *opaque)
215 {
216     SLAVIO_TIMERState *s = opaque;
217
218     qemu_put_be32s(f, &s->limit);
219     qemu_put_be32s(f, &s->count);
220     qemu_put_be32s(f, &s->counthigh);
221     qemu_put_be64s(f, &s->count_load_time);
222     qemu_put_be64s(f, &s->expire_time);
223     qemu_put_be64s(f, &s->stop_time);
224     qemu_put_be64s(f, &s->tick_offset);
225     qemu_put_be32s(f, &s->irq);
226     qemu_put_be32s(f, &s->reached);
227     qemu_put_be32s(f, &s->stopped);
228     qemu_put_be32s(f, &s->mode);
229 }
230
231 static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id)
232 {
233     SLAVIO_TIMERState *s = opaque;
234     
235     if (version_id != 1)
236         return -EINVAL;
237
238     qemu_get_be32s(f, &s->limit);
239     qemu_get_be32s(f, &s->count);
240     qemu_get_be32s(f, &s->counthigh);
241     qemu_get_be64s(f, &s->count_load_time);
242     qemu_get_be64s(f, &s->expire_time);
243     qemu_get_be64s(f, &s->stop_time);
244     qemu_get_be64s(f, &s->tick_offset);
245     qemu_get_be32s(f, &s->irq);
246     qemu_get_be32s(f, &s->reached);
247     qemu_get_be32s(f, &s->stopped);
248     qemu_get_be32s(f, &s->mode);
249     return 0;
250 }
251
252 static void slavio_timer_reset(void *opaque)
253 {
254     SLAVIO_TIMERState *s = opaque;
255
256     s->limit = 0;
257     s->count = 0;
258     s->count_load_time = qemu_get_clock(vm_clock);;
259     s->stop_time = s->count_load_time;
260     s->tick_offset = 0;
261     s->reached = 0;
262     s->mode &= 2;
263     s->stopped = 1;
264     slavio_timer_irq(s);
265 }
266
267 void slavio_timer_init(target_phys_addr_t addr, int irq, int mode,
268                        unsigned int cpu, void *intctl)
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     s->intctl = intctl;
281
282     slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
283                                                     slavio_timer_mem_write, s);
284     cpu_register_physical_memory(addr, TIMER_MAXADDR, slavio_timer_io_memory);
285     register_savevm("slavio_timer", addr, 1, slavio_timer_save, slavio_timer_load, s);
286     qemu_register_reset(slavio_timer_reset, s);
287     slavio_timer_reset(s);
288 }