root / hw / slavio_timer.c @ eae6bcbf
History | View | Annotate | Download (13.5 kB)
1 | e80cfcfc | bellard | /*
|
---|---|---|---|
2 | e80cfcfc | bellard | * QEMU Sparc SLAVIO timer controller emulation
|
3 | e80cfcfc | bellard | *
|
4 | 66321a11 | bellard | * Copyright (c) 2003-2005 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | e80cfcfc | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | e80cfcfc | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | e80cfcfc | bellard | * in the Software without restriction, including without limitation the rights
|
9 | e80cfcfc | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | e80cfcfc | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | e80cfcfc | bellard | * furnished to do so, subject to the following conditions:
|
12 | e80cfcfc | bellard | *
|
13 | e80cfcfc | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | e80cfcfc | bellard | * all copies or substantial portions of the Software.
|
15 | e80cfcfc | bellard | *
|
16 | e80cfcfc | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | e80cfcfc | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | e80cfcfc | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | e80cfcfc | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | e80cfcfc | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | e80cfcfc | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | e80cfcfc | bellard | * THE SOFTWARE.
|
23 | e80cfcfc | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "sun4m.h" |
26 | 87ecb68b | pbrook | #include "qemu-timer.h" |
27 | e80cfcfc | bellard | |
28 | e80cfcfc | bellard | //#define DEBUG_TIMER
|
29 | e80cfcfc | bellard | |
30 | 66321a11 | bellard | #ifdef DEBUG_TIMER
|
31 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) \
|
32 | 001faf32 | Blue Swirl | do { printf("TIMER: " fmt , ## __VA_ARGS__); } while (0) |
33 | 66321a11 | bellard | #else
|
34 | 001faf32 | Blue Swirl | #define DPRINTF(fmt, ...) do {} while (0) |
35 | 66321a11 | bellard | #endif
|
36 | 66321a11 | bellard | |
37 | e80cfcfc | bellard | /*
|
38 | e80cfcfc | bellard | * Registers of hardware timer in sun4m.
|
39 | e80cfcfc | bellard | *
|
40 | e80cfcfc | bellard | * This is the timer/counter part of chip STP2001 (Slave I/O), also
|
41 | e80cfcfc | bellard | * produced as NCR89C105. See
|
42 | e80cfcfc | bellard | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
|
43 | 5fafdf24 | ths | *
|
44 | e80cfcfc | bellard | * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
|
45 | e80cfcfc | bellard | * are zero. Bit 31 is 1 when count has been reached.
|
46 | e80cfcfc | bellard | *
|
47 | ba3c64fb | bellard | * Per-CPU timers interrupt local CPU, system timer uses normal
|
48 | ba3c64fb | bellard | * interrupt routing.
|
49 | ba3c64fb | bellard | *
|
50 | e80cfcfc | bellard | */
|
51 | e80cfcfc | bellard | |
52 | 81732d19 | blueswir1 | #define MAX_CPUS 16 |
53 | 81732d19 | blueswir1 | |
54 | e80cfcfc | bellard | typedef struct SLAVIO_TIMERState { |
55 | d7edfd27 | blueswir1 | qemu_irq irq; |
56 | 8d05ea8a | blueswir1 | ptimer_state *timer; |
57 | 8d05ea8a | blueswir1 | uint32_t count, counthigh, reached; |
58 | 8d05ea8a | blueswir1 | uint64_t limit; |
59 | 115646b6 | blueswir1 | // processor only
|
60 | 22548760 | blueswir1 | uint32_t running; |
61 | 115646b6 | blueswir1 | struct SLAVIO_TIMERState *master;
|
62 | 22548760 | blueswir1 | uint32_t slave_index; |
63 | 115646b6 | blueswir1 | // system only
|
64 | 22548760 | blueswir1 | uint32_t num_slaves; |
65 | 81732d19 | blueswir1 | struct SLAVIO_TIMERState *slave[MAX_CPUS];
|
66 | 81732d19 | blueswir1 | uint32_t slave_mode; |
67 | e80cfcfc | bellard | } SLAVIO_TIMERState; |
68 | e80cfcfc | bellard | |
69 | 115646b6 | blueswir1 | #define SYS_TIMER_SIZE 0x14 |
70 | 81732d19 | blueswir1 | #define CPU_TIMER_SIZE 0x10 |
71 | e80cfcfc | bellard | |
72 | d2c38b24 | blueswir1 | #define SYS_TIMER_OFFSET 0x10000ULL |
73 | d2c38b24 | blueswir1 | #define CPU_TIMER_OFFSET(cpu) (0x1000ULL * cpu) |
74 | d2c38b24 | blueswir1 | |
75 | d2c38b24 | blueswir1 | #define TIMER_LIMIT 0 |
76 | d2c38b24 | blueswir1 | #define TIMER_COUNTER 1 |
77 | d2c38b24 | blueswir1 | #define TIMER_COUNTER_NORST 2 |
78 | d2c38b24 | blueswir1 | #define TIMER_STATUS 3 |
79 | d2c38b24 | blueswir1 | #define TIMER_MODE 4 |
80 | d2c38b24 | blueswir1 | |
81 | d2c38b24 | blueswir1 | #define TIMER_COUNT_MASK32 0xfffffe00 |
82 | d2c38b24 | blueswir1 | #define TIMER_LIMIT_MASK32 0x7fffffff |
83 | d2c38b24 | blueswir1 | #define TIMER_MAX_COUNT64 0x7ffffffffffffe00ULL |
84 | d2c38b24 | blueswir1 | #define TIMER_MAX_COUNT32 0x7ffffe00ULL |
85 | d2c38b24 | blueswir1 | #define TIMER_REACHED 0x80000000 |
86 | d2c38b24 | blueswir1 | #define TIMER_PERIOD 500ULL // 500ns |
87 | d2c38b24 | blueswir1 | #define LIMIT_TO_PERIODS(l) ((l) >> 9) |
88 | d2c38b24 | blueswir1 | #define PERIODS_TO_LIMIT(l) ((l) << 9) |
89 | d2c38b24 | blueswir1 | |
90 | 115646b6 | blueswir1 | static int slavio_timer_is_user(SLAVIO_TIMERState *s) |
91 | 115646b6 | blueswir1 | { |
92 | 115646b6 | blueswir1 | return s->master && (s->master->slave_mode & (1 << s->slave_index)); |
93 | 115646b6 | blueswir1 | } |
94 | 115646b6 | blueswir1 | |
95 | e80cfcfc | bellard | // Update count, set irq, update expire_time
|
96 | 8d05ea8a | blueswir1 | // Convert from ptimer countdown units
|
97 | e80cfcfc | bellard | static void slavio_timer_get_out(SLAVIO_TIMERState *s) |
98 | e80cfcfc | bellard | { |
99 | bd7e2875 | blueswir1 | uint64_t count, limit; |
100 | e80cfcfc | bellard | |
101 | bd7e2875 | blueswir1 | if (s->limit == 0) /* free-run processor or system counter */ |
102 | bd7e2875 | blueswir1 | limit = TIMER_MAX_COUNT32; |
103 | bd7e2875 | blueswir1 | else
|
104 | bd7e2875 | blueswir1 | limit = s->limit; |
105 | bd7e2875 | blueswir1 | |
106 | 85e3023e | blueswir1 | if (s->timer)
|
107 | 85e3023e | blueswir1 | count = limit - PERIODS_TO_LIMIT(ptimer_get_count(s->timer)); |
108 | 85e3023e | blueswir1 | else
|
109 | 85e3023e | blueswir1 | count = 0;
|
110 | 85e3023e | blueswir1 | |
111 | d2c38b24 | blueswir1 | DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", s->limit, |
112 | d2c38b24 | blueswir1 | s->counthigh, s->count); |
113 | d2c38b24 | blueswir1 | s->count = count & TIMER_COUNT_MASK32; |
114 | 8d05ea8a | blueswir1 | s->counthigh = count >> 32;
|
115 | e80cfcfc | bellard | } |
116 | e80cfcfc | bellard | |
117 | e80cfcfc | bellard | // timer callback
|
118 | e80cfcfc | bellard | static void slavio_timer_irq(void *opaque) |
119 | e80cfcfc | bellard | { |
120 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
121 | e80cfcfc | bellard | |
122 | e80cfcfc | bellard | slavio_timer_get_out(s); |
123 | 8d05ea8a | blueswir1 | DPRINTF("callback: count %x%08x\n", s->counthigh, s->count);
|
124 | e1cb9502 | blueswir1 | s->reached = TIMER_REACHED; |
125 | e1cb9502 | blueswir1 | if (!slavio_timer_is_user(s))
|
126 | f930d07e | blueswir1 | qemu_irq_raise(s->irq); |
127 | e80cfcfc | bellard | } |
128 | e80cfcfc | bellard | |
129 | e80cfcfc | bellard | static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr) |
130 | e80cfcfc | bellard | { |
131 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
132 | 8d05ea8a | blueswir1 | uint32_t saddr, ret; |
133 | e80cfcfc | bellard | |
134 | e64d7d59 | blueswir1 | saddr = addr >> 2;
|
135 | e80cfcfc | bellard | switch (saddr) {
|
136 | d2c38b24 | blueswir1 | case TIMER_LIMIT:
|
137 | f930d07e | blueswir1 | // read limit (system counter mode) or read most signifying
|
138 | f930d07e | blueswir1 | // part of counter (user mode)
|
139 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) {
|
140 | 115646b6 | blueswir1 | // read user timer MSW
|
141 | 115646b6 | blueswir1 | slavio_timer_get_out(s); |
142 | e1cb9502 | blueswir1 | ret = s->counthigh | s->reached; |
143 | 115646b6 | blueswir1 | } else {
|
144 | 115646b6 | blueswir1 | // read limit
|
145 | f930d07e | blueswir1 | // clear irq
|
146 | d7edfd27 | blueswir1 | qemu_irq_lower(s->irq); |
147 | f930d07e | blueswir1 | s->reached = 0;
|
148 | d2c38b24 | blueswir1 | ret = s->limit & TIMER_LIMIT_MASK32; |
149 | f930d07e | blueswir1 | } |
150 | 8d05ea8a | blueswir1 | break;
|
151 | d2c38b24 | blueswir1 | case TIMER_COUNTER:
|
152 | f930d07e | blueswir1 | // read counter and reached bit (system mode) or read lsbits
|
153 | f930d07e | blueswir1 | // of counter (user mode)
|
154 | f930d07e | blueswir1 | slavio_timer_get_out(s); |
155 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) // read user timer LSW |
156 | e1cb9502 | blueswir1 | ret = s->count & TIMER_MAX_COUNT64; |
157 | 115646b6 | blueswir1 | else // read limit |
158 | d2c38b24 | blueswir1 | ret = (s->count & TIMER_MAX_COUNT32) | s->reached; |
159 | 8d05ea8a | blueswir1 | break;
|
160 | d2c38b24 | blueswir1 | case TIMER_STATUS:
|
161 | 115646b6 | blueswir1 | // only available in processor counter/timer
|
162 | f930d07e | blueswir1 | // read start/stop status
|
163 | 115646b6 | blueswir1 | ret = s->running; |
164 | 8d05ea8a | blueswir1 | break;
|
165 | d2c38b24 | blueswir1 | case TIMER_MODE:
|
166 | 115646b6 | blueswir1 | // only available in system counter
|
167 | f930d07e | blueswir1 | // read user/system mode
|
168 | 81732d19 | blueswir1 | ret = s->slave_mode; |
169 | 8d05ea8a | blueswir1 | break;
|
170 | e80cfcfc | bellard | default:
|
171 | 115646b6 | blueswir1 | DPRINTF("invalid read address " TARGET_FMT_plx "\n", addr); |
172 | 8d05ea8a | blueswir1 | ret = 0;
|
173 | 8d05ea8a | blueswir1 | break;
|
174 | e80cfcfc | bellard | } |
175 | 8d05ea8a | blueswir1 | DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret); |
176 | 8d05ea8a | blueswir1 | |
177 | 8d05ea8a | blueswir1 | return ret;
|
178 | e80cfcfc | bellard | } |
179 | e80cfcfc | bellard | |
180 | d2c38b24 | blueswir1 | static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr, |
181 | d2c38b24 | blueswir1 | uint32_t val) |
182 | e80cfcfc | bellard | { |
183 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
184 | e80cfcfc | bellard | uint32_t saddr; |
185 | e80cfcfc | bellard | |
186 | 8d05ea8a | blueswir1 | DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val); |
187 | e64d7d59 | blueswir1 | saddr = addr >> 2;
|
188 | e80cfcfc | bellard | switch (saddr) {
|
189 | d2c38b24 | blueswir1 | case TIMER_LIMIT:
|
190 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) {
|
191 | e1cb9502 | blueswir1 | uint64_t count; |
192 | e1cb9502 | blueswir1 | |
193 | 115646b6 | blueswir1 | // set user counter MSW, reset counter
|
194 | d2c38b24 | blueswir1 | s->limit = TIMER_MAX_COUNT64; |
195 | e1cb9502 | blueswir1 | s->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
|
196 | e1cb9502 | blueswir1 | s->reached = 0;
|
197 | e1cb9502 | blueswir1 | count = ((uint64_t)s->counthigh << 32) | s->count;
|
198 | e1cb9502 | blueswir1 | DPRINTF("processor %d user timer set to %016llx\n", s->slave_index,
|
199 | e1cb9502 | blueswir1 | count); |
200 | 67e42751 | blueswir1 | if (s->timer)
|
201 | e1cb9502 | blueswir1 | ptimer_set_count(s->timer, LIMIT_TO_PERIODS(s->limit - count)); |
202 | 115646b6 | blueswir1 | } else {
|
203 | 115646b6 | blueswir1 | // set limit, reset counter
|
204 | 115646b6 | blueswir1 | qemu_irq_lower(s->irq); |
205 | d2c38b24 | blueswir1 | s->limit = val & TIMER_MAX_COUNT32; |
206 | 85e3023e | blueswir1 | if (s->timer) {
|
207 | 85e3023e | blueswir1 | if (s->limit == 0) /* free-run */ |
208 | 77f193da | blueswir1 | ptimer_set_limit(s->timer, |
209 | 77f193da | blueswir1 | LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
|
210 | 85e3023e | blueswir1 | else
|
211 | 85e3023e | blueswir1 | ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
|
212 | 85e3023e | blueswir1 | } |
213 | 81732d19 | blueswir1 | } |
214 | 115646b6 | blueswir1 | break;
|
215 | d2c38b24 | blueswir1 | case TIMER_COUNTER:
|
216 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) {
|
217 | e1cb9502 | blueswir1 | uint64_t count; |
218 | e1cb9502 | blueswir1 | |
219 | 115646b6 | blueswir1 | // set user counter LSW, reset counter
|
220 | d2c38b24 | blueswir1 | s->limit = TIMER_MAX_COUNT64; |
221 | e1cb9502 | blueswir1 | s->count = val & TIMER_MAX_COUNT64; |
222 | e1cb9502 | blueswir1 | s->reached = 0;
|
223 | e1cb9502 | blueswir1 | count = ((uint64_t)s->counthigh) << 32 | s->count;
|
224 | e1cb9502 | blueswir1 | DPRINTF("processor %d user timer set to %016llx\n", s->slave_index,
|
225 | e1cb9502 | blueswir1 | count); |
226 | 67e42751 | blueswir1 | if (s->timer)
|
227 | e1cb9502 | blueswir1 | ptimer_set_count(s->timer, LIMIT_TO_PERIODS(s->limit - count)); |
228 | 115646b6 | blueswir1 | } else
|
229 | 115646b6 | blueswir1 | DPRINTF("not user timer\n");
|
230 | 115646b6 | blueswir1 | break;
|
231 | d2c38b24 | blueswir1 | case TIMER_COUNTER_NORST:
|
232 | f930d07e | blueswir1 | // set limit without resetting counter
|
233 | d2c38b24 | blueswir1 | s->limit = val & TIMER_MAX_COUNT32; |
234 | 85e3023e | blueswir1 | if (s->timer) {
|
235 | 85e3023e | blueswir1 | if (s->limit == 0) /* free-run */ |
236 | 77f193da | blueswir1 | ptimer_set_limit(s->timer, |
237 | 77f193da | blueswir1 | LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
|
238 | 85e3023e | blueswir1 | else
|
239 | 85e3023e | blueswir1 | ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 0);
|
240 | 85e3023e | blueswir1 | } |
241 | f930d07e | blueswir1 | break;
|
242 | d2c38b24 | blueswir1 | case TIMER_STATUS:
|
243 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) {
|
244 | 115646b6 | blueswir1 | // start/stop user counter
|
245 | 115646b6 | blueswir1 | if ((val & 1) && !s->running) { |
246 | 115646b6 | blueswir1 | DPRINTF("processor %d user timer started\n", s->slave_index);
|
247 | 85e3023e | blueswir1 | if (s->timer)
|
248 | 85e3023e | blueswir1 | ptimer_run(s->timer, 0);
|
249 | 115646b6 | blueswir1 | s->running = 1;
|
250 | 115646b6 | blueswir1 | } else if (!(val & 1) && s->running) { |
251 | 115646b6 | blueswir1 | DPRINTF("processor %d user timer stopped\n", s->slave_index);
|
252 | 85e3023e | blueswir1 | if (s->timer)
|
253 | 85e3023e | blueswir1 | ptimer_stop(s->timer); |
254 | 115646b6 | blueswir1 | s->running = 0;
|
255 | f930d07e | blueswir1 | } |
256 | f930d07e | blueswir1 | } |
257 | f930d07e | blueswir1 | break;
|
258 | d2c38b24 | blueswir1 | case TIMER_MODE:
|
259 | 115646b6 | blueswir1 | if (s->master == NULL) { |
260 | 81732d19 | blueswir1 | unsigned int i; |
261 | 81732d19 | blueswir1 | |
262 | 19f8e5dd | blueswir1 | for (i = 0; i < s->num_slaves; i++) { |
263 | 67e42751 | blueswir1 | unsigned int processor = 1 << i; |
264 | 67e42751 | blueswir1 | |
265 | 67e42751 | blueswir1 | // check for a change in timer mode for this processor
|
266 | 67e42751 | blueswir1 | if ((val & processor) != (s->slave_mode & processor)) {
|
267 | 67e42751 | blueswir1 | if (val & processor) { // counter -> user timer |
268 | 67e42751 | blueswir1 | qemu_irq_lower(s->slave[i]->irq); |
269 | 67e42751 | blueswir1 | // counters are always running
|
270 | 67e42751 | blueswir1 | ptimer_stop(s->slave[i]->timer); |
271 | 67e42751 | blueswir1 | s->slave[i]->running = 0;
|
272 | 67e42751 | blueswir1 | // user timer limit is always the same
|
273 | 67e42751 | blueswir1 | s->slave[i]->limit = TIMER_MAX_COUNT64; |
274 | 67e42751 | blueswir1 | ptimer_set_limit(s->slave[i]->timer, |
275 | 77f193da | blueswir1 | LIMIT_TO_PERIODS(s->slave[i]->limit), |
276 | 77f193da | blueswir1 | 1);
|
277 | 67e42751 | blueswir1 | // set this processors user timer bit in config
|
278 | 67e42751 | blueswir1 | // register
|
279 | 67e42751 | blueswir1 | s->slave_mode |= processor; |
280 | 67e42751 | blueswir1 | DPRINTF("processor %d changed from counter to user "
|
281 | 67e42751 | blueswir1 | "timer\n", s->slave[i]->slave_index);
|
282 | 67e42751 | blueswir1 | } else { // user timer -> counter |
283 | 67e42751 | blueswir1 | // stop the user timer if it is running
|
284 | 67e42751 | blueswir1 | if (s->slave[i]->running)
|
285 | 67e42751 | blueswir1 | ptimer_stop(s->slave[i]->timer); |
286 | 67e42751 | blueswir1 | // start the counter
|
287 | 67e42751 | blueswir1 | ptimer_run(s->slave[i]->timer, 0);
|
288 | 67e42751 | blueswir1 | s->slave[i]->running = 1;
|
289 | 67e42751 | blueswir1 | // clear this processors user timer bit in config
|
290 | 67e42751 | blueswir1 | // register
|
291 | 67e42751 | blueswir1 | s->slave_mode &= ~processor; |
292 | 67e42751 | blueswir1 | DPRINTF("processor %d changed from user timer to "
|
293 | 67e42751 | blueswir1 | "counter\n", s->slave[i]->slave_index);
|
294 | 67e42751 | blueswir1 | } |
295 | 115646b6 | blueswir1 | } |
296 | 81732d19 | blueswir1 | } |
297 | 115646b6 | blueswir1 | } else
|
298 | 115646b6 | blueswir1 | DPRINTF("not system timer\n");
|
299 | f930d07e | blueswir1 | break;
|
300 | e80cfcfc | bellard | default:
|
301 | 115646b6 | blueswir1 | DPRINTF("invalid write address " TARGET_FMT_plx "\n", addr); |
302 | f930d07e | blueswir1 | break;
|
303 | e80cfcfc | bellard | } |
304 | e80cfcfc | bellard | } |
305 | e80cfcfc | bellard | |
306 | e80cfcfc | bellard | static CPUReadMemoryFunc *slavio_timer_mem_read[3] = { |
307 | 7c560456 | blueswir1 | NULL,
|
308 | 7c560456 | blueswir1 | NULL,
|
309 | e80cfcfc | bellard | slavio_timer_mem_readl, |
310 | e80cfcfc | bellard | }; |
311 | e80cfcfc | bellard | |
312 | e80cfcfc | bellard | static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = { |
313 | 7c560456 | blueswir1 | NULL,
|
314 | 7c560456 | blueswir1 | NULL,
|
315 | e80cfcfc | bellard | slavio_timer_mem_writel, |
316 | e80cfcfc | bellard | }; |
317 | e80cfcfc | bellard | |
318 | e80cfcfc | bellard | static void slavio_timer_save(QEMUFile *f, void *opaque) |
319 | e80cfcfc | bellard | { |
320 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
321 | e80cfcfc | bellard | |
322 | 8d05ea8a | blueswir1 | qemu_put_be64s(f, &s->limit); |
323 | e80cfcfc | bellard | qemu_put_be32s(f, &s->count); |
324 | e80cfcfc | bellard | qemu_put_be32s(f, &s->counthigh); |
325 | e80cfcfc | bellard | qemu_put_be32s(f, &s->reached); |
326 | 115646b6 | blueswir1 | qemu_put_be32s(f, &s->running); |
327 | 85e3023e | blueswir1 | if (s->timer)
|
328 | 85e3023e | blueswir1 | qemu_put_ptimer(f, s->timer); |
329 | e80cfcfc | bellard | } |
330 | e80cfcfc | bellard | |
331 | e80cfcfc | bellard | static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id) |
332 | e80cfcfc | bellard | { |
333 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
334 | 3b46e624 | ths | |
335 | 85e3023e | blueswir1 | if (version_id != 3) |
336 | e80cfcfc | bellard | return -EINVAL;
|
337 | e80cfcfc | bellard | |
338 | 8d05ea8a | blueswir1 | qemu_get_be64s(f, &s->limit); |
339 | e80cfcfc | bellard | qemu_get_be32s(f, &s->count); |
340 | e80cfcfc | bellard | qemu_get_be32s(f, &s->counthigh); |
341 | e80cfcfc | bellard | qemu_get_be32s(f, &s->reached); |
342 | 115646b6 | blueswir1 | qemu_get_be32s(f, &s->running); |
343 | 85e3023e | blueswir1 | if (s->timer)
|
344 | 85e3023e | blueswir1 | qemu_get_ptimer(f, s->timer); |
345 | 8d05ea8a | blueswir1 | |
346 | e80cfcfc | bellard | return 0; |
347 | e80cfcfc | bellard | } |
348 | e80cfcfc | bellard | |
349 | e80cfcfc | bellard | static void slavio_timer_reset(void *opaque) |
350 | e80cfcfc | bellard | { |
351 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
352 | e80cfcfc | bellard | |
353 | 3b4aa426 | blueswir1 | s->limit = 0;
|
354 | e80cfcfc | bellard | s->count = 0;
|
355 | e80cfcfc | bellard | s->reached = 0;
|
356 | 3b4aa426 | blueswir1 | s->slave_mode = 0;
|
357 | 85e3023e | blueswir1 | if (!s->master || s->slave_index < s->master->num_slaves) {
|
358 | 85e3023e | blueswir1 | ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
|
359 | 85e3023e | blueswir1 | ptimer_run(s->timer, 0);
|
360 | 85e3023e | blueswir1 | } |
361 | 115646b6 | blueswir1 | s->running = 1;
|
362 | d7edfd27 | blueswir1 | qemu_irq_lower(s->irq); |
363 | e80cfcfc | bellard | } |
364 | e80cfcfc | bellard | |
365 | 81732d19 | blueswir1 | static SLAVIO_TIMERState *slavio_timer_init(target_phys_addr_t addr,
|
366 | 115646b6 | blueswir1 | qemu_irq irq, |
367 | 115646b6 | blueswir1 | SLAVIO_TIMERState *master, |
368 | 22548760 | blueswir1 | uint32_t slave_index) |
369 | e80cfcfc | bellard | { |
370 | e80cfcfc | bellard | int slavio_timer_io_memory;
|
371 | e80cfcfc | bellard | SLAVIO_TIMERState *s; |
372 | 8d05ea8a | blueswir1 | QEMUBH *bh; |
373 | e80cfcfc | bellard | |
374 | e80cfcfc | bellard | s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
|
375 | e80cfcfc | bellard | s->irq = irq; |
376 | 115646b6 | blueswir1 | s->master = master; |
377 | 115646b6 | blueswir1 | s->slave_index = slave_index; |
378 | 85e3023e | blueswir1 | if (!master || slave_index < master->num_slaves) {
|
379 | 85e3023e | blueswir1 | bh = qemu_bh_new(slavio_timer_irq, s); |
380 | 85e3023e | blueswir1 | s->timer = ptimer_init(bh); |
381 | 85e3023e | blueswir1 | ptimer_set_period(s->timer, TIMER_PERIOD); |
382 | 85e3023e | blueswir1 | } |
383 | e80cfcfc | bellard | |
384 | 1eed09cb | Avi Kivity | slavio_timer_io_memory = cpu_register_io_memory(slavio_timer_mem_read, |
385 | f930d07e | blueswir1 | slavio_timer_mem_write, s); |
386 | 115646b6 | blueswir1 | if (master)
|
387 | d2c38b24 | blueswir1 | cpu_register_physical_memory(addr, CPU_TIMER_SIZE, |
388 | d2c38b24 | blueswir1 | slavio_timer_io_memory); |
389 | 81732d19 | blueswir1 | else
|
390 | d2c38b24 | blueswir1 | cpu_register_physical_memory(addr, SYS_TIMER_SIZE, |
391 | d2c38b24 | blueswir1 | slavio_timer_io_memory); |
392 | 85e3023e | blueswir1 | register_savevm("slavio_timer", addr, 3, slavio_timer_save, |
393 | d2c38b24 | blueswir1 | slavio_timer_load, s); |
394 | 8217606e | Jan Kiszka | qemu_register_reset(slavio_timer_reset, 0, s);
|
395 | e80cfcfc | bellard | slavio_timer_reset(s); |
396 | 81732d19 | blueswir1 | |
397 | 81732d19 | blueswir1 | return s;
|
398 | 81732d19 | blueswir1 | } |
399 | 81732d19 | blueswir1 | |
400 | 81732d19 | blueswir1 | void slavio_timer_init_all(target_phys_addr_t base, qemu_irq master_irq,
|
401 | 19f8e5dd | blueswir1 | qemu_irq *cpu_irqs, unsigned int num_cpus) |
402 | 81732d19 | blueswir1 | { |
403 | 81732d19 | blueswir1 | SLAVIO_TIMERState *master; |
404 | 81732d19 | blueswir1 | unsigned int i; |
405 | 81732d19 | blueswir1 | |
406 | d2c38b24 | blueswir1 | master = slavio_timer_init(base + SYS_TIMER_OFFSET, master_irq, NULL, 0); |
407 | 81732d19 | blueswir1 | |
408 | 19f8e5dd | blueswir1 | master->num_slaves = num_cpus; |
409 | 19f8e5dd | blueswir1 | |
410 | 81732d19 | blueswir1 | for (i = 0; i < MAX_CPUS; i++) { |
411 | 81732d19 | blueswir1 | master->slave[i] = slavio_timer_init(base + (target_phys_addr_t) |
412 | d2c38b24 | blueswir1 | CPU_TIMER_OFFSET(i), |
413 | 115646b6 | blueswir1 | cpu_irqs[i], master, i); |
414 | 81732d19 | blueswir1 | } |
415 | e80cfcfc | bellard | } |