root / hw / slavio_timer.c @ a245f2e7
History | View | Annotate | Download (13.6 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 | 66321a11 | bellard | #define DPRINTF(fmt, args...) \
|
32 | 66321a11 | bellard | do { printf("TIMER: " fmt , ##args); } while (0) |
33 | 66321a11 | bellard | #else
|
34 | 22548760 | blueswir1 | #define DPRINTF(fmt, args...) 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 | e80cfcfc | bellard | #define TIMER_MAXADDR 0x1f |
70 | 115646b6 | blueswir1 | #define SYS_TIMER_SIZE 0x14 |
71 | 81732d19 | blueswir1 | #define CPU_TIMER_SIZE 0x10 |
72 | e80cfcfc | bellard | |
73 | d2c38b24 | blueswir1 | #define SYS_TIMER_OFFSET 0x10000ULL |
74 | d2c38b24 | blueswir1 | #define CPU_TIMER_OFFSET(cpu) (0x1000ULL * cpu) |
75 | d2c38b24 | blueswir1 | |
76 | d2c38b24 | blueswir1 | #define TIMER_LIMIT 0 |
77 | d2c38b24 | blueswir1 | #define TIMER_COUNTER 1 |
78 | d2c38b24 | blueswir1 | #define TIMER_COUNTER_NORST 2 |
79 | d2c38b24 | blueswir1 | #define TIMER_STATUS 3 |
80 | d2c38b24 | blueswir1 | #define TIMER_MODE 4 |
81 | d2c38b24 | blueswir1 | |
82 | d2c38b24 | blueswir1 | #define TIMER_COUNT_MASK32 0xfffffe00 |
83 | d2c38b24 | blueswir1 | #define TIMER_LIMIT_MASK32 0x7fffffff |
84 | d2c38b24 | blueswir1 | #define TIMER_MAX_COUNT64 0x7ffffffffffffe00ULL |
85 | d2c38b24 | blueswir1 | #define TIMER_MAX_COUNT32 0x7ffffe00ULL |
86 | d2c38b24 | blueswir1 | #define TIMER_REACHED 0x80000000 |
87 | d2c38b24 | blueswir1 | #define TIMER_PERIOD 500ULL // 500ns |
88 | d2c38b24 | blueswir1 | #define LIMIT_TO_PERIODS(l) ((l) >> 9) |
89 | d2c38b24 | blueswir1 | #define PERIODS_TO_LIMIT(l) ((l) << 9) |
90 | d2c38b24 | blueswir1 | |
91 | 115646b6 | blueswir1 | static int slavio_timer_is_user(SLAVIO_TIMERState *s) |
92 | 115646b6 | blueswir1 | { |
93 | 115646b6 | blueswir1 | return s->master && (s->master->slave_mode & (1 << s->slave_index)); |
94 | 115646b6 | blueswir1 | } |
95 | 115646b6 | blueswir1 | |
96 | e80cfcfc | bellard | // Update count, set irq, update expire_time
|
97 | 8d05ea8a | blueswir1 | // Convert from ptimer countdown units
|
98 | e80cfcfc | bellard | static void slavio_timer_get_out(SLAVIO_TIMERState *s) |
99 | e80cfcfc | bellard | { |
100 | bd7e2875 | blueswir1 | uint64_t count, limit; |
101 | e80cfcfc | bellard | |
102 | bd7e2875 | blueswir1 | if (s->limit == 0) /* free-run processor or system counter */ |
103 | bd7e2875 | blueswir1 | limit = TIMER_MAX_COUNT32; |
104 | bd7e2875 | blueswir1 | else
|
105 | bd7e2875 | blueswir1 | limit = s->limit; |
106 | bd7e2875 | blueswir1 | |
107 | 85e3023e | blueswir1 | if (s->timer)
|
108 | 85e3023e | blueswir1 | count = limit - PERIODS_TO_LIMIT(ptimer_get_count(s->timer)); |
109 | 85e3023e | blueswir1 | else
|
110 | 85e3023e | blueswir1 | count = 0;
|
111 | 85e3023e | blueswir1 | |
112 | d2c38b24 | blueswir1 | DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", s->limit, |
113 | d2c38b24 | blueswir1 | s->counthigh, s->count); |
114 | d2c38b24 | blueswir1 | s->count = count & TIMER_COUNT_MASK32; |
115 | 8d05ea8a | blueswir1 | s->counthigh = count >> 32;
|
116 | e80cfcfc | bellard | } |
117 | e80cfcfc | bellard | |
118 | e80cfcfc | bellard | // timer callback
|
119 | e80cfcfc | bellard | static void slavio_timer_irq(void *opaque) |
120 | e80cfcfc | bellard | { |
121 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
122 | e80cfcfc | bellard | |
123 | e80cfcfc | bellard | slavio_timer_get_out(s); |
124 | 8d05ea8a | blueswir1 | DPRINTF("callback: count %x%08x\n", s->counthigh, s->count);
|
125 | e1cb9502 | blueswir1 | s->reached = TIMER_REACHED; |
126 | e1cb9502 | blueswir1 | if (!slavio_timer_is_user(s))
|
127 | f930d07e | blueswir1 | qemu_irq_raise(s->irq); |
128 | e80cfcfc | bellard | } |
129 | e80cfcfc | bellard | |
130 | e80cfcfc | bellard | static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr) |
131 | e80cfcfc | bellard | { |
132 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
133 | 8d05ea8a | blueswir1 | uint32_t saddr, ret; |
134 | e80cfcfc | bellard | |
135 | e80cfcfc | bellard | saddr = (addr & TIMER_MAXADDR) >> 2;
|
136 | e80cfcfc | bellard | switch (saddr) {
|
137 | d2c38b24 | blueswir1 | case TIMER_LIMIT:
|
138 | f930d07e | blueswir1 | // read limit (system counter mode) or read most signifying
|
139 | f930d07e | blueswir1 | // part of counter (user mode)
|
140 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) {
|
141 | 115646b6 | blueswir1 | // read user timer MSW
|
142 | 115646b6 | blueswir1 | slavio_timer_get_out(s); |
143 | e1cb9502 | blueswir1 | ret = s->counthigh | s->reached; |
144 | 115646b6 | blueswir1 | } else {
|
145 | 115646b6 | blueswir1 | // read limit
|
146 | f930d07e | blueswir1 | // clear irq
|
147 | d7edfd27 | blueswir1 | qemu_irq_lower(s->irq); |
148 | f930d07e | blueswir1 | s->reached = 0;
|
149 | d2c38b24 | blueswir1 | ret = s->limit & TIMER_LIMIT_MASK32; |
150 | f930d07e | blueswir1 | } |
151 | 8d05ea8a | blueswir1 | break;
|
152 | d2c38b24 | blueswir1 | case TIMER_COUNTER:
|
153 | f930d07e | blueswir1 | // read counter and reached bit (system mode) or read lsbits
|
154 | f930d07e | blueswir1 | // of counter (user mode)
|
155 | f930d07e | blueswir1 | slavio_timer_get_out(s); |
156 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) // read user timer LSW |
157 | e1cb9502 | blueswir1 | ret = s->count & TIMER_MAX_COUNT64; |
158 | 115646b6 | blueswir1 | else // read limit |
159 | d2c38b24 | blueswir1 | ret = (s->count & TIMER_MAX_COUNT32) | s->reached; |
160 | 8d05ea8a | blueswir1 | break;
|
161 | d2c38b24 | blueswir1 | case TIMER_STATUS:
|
162 | 115646b6 | blueswir1 | // only available in processor counter/timer
|
163 | f930d07e | blueswir1 | // read start/stop status
|
164 | 115646b6 | blueswir1 | ret = s->running; |
165 | 8d05ea8a | blueswir1 | break;
|
166 | d2c38b24 | blueswir1 | case TIMER_MODE:
|
167 | 115646b6 | blueswir1 | // only available in system counter
|
168 | f930d07e | blueswir1 | // read user/system mode
|
169 | 81732d19 | blueswir1 | ret = s->slave_mode; |
170 | 8d05ea8a | blueswir1 | break;
|
171 | e80cfcfc | bellard | default:
|
172 | 115646b6 | blueswir1 | DPRINTF("invalid read address " TARGET_FMT_plx "\n", addr); |
173 | 8d05ea8a | blueswir1 | ret = 0;
|
174 | 8d05ea8a | blueswir1 | break;
|
175 | e80cfcfc | bellard | } |
176 | 8d05ea8a | blueswir1 | DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret); |
177 | 8d05ea8a | blueswir1 | |
178 | 8d05ea8a | blueswir1 | return ret;
|
179 | e80cfcfc | bellard | } |
180 | e80cfcfc | bellard | |
181 | d2c38b24 | blueswir1 | static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr, |
182 | d2c38b24 | blueswir1 | uint32_t val) |
183 | e80cfcfc | bellard | { |
184 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
185 | e80cfcfc | bellard | uint32_t saddr; |
186 | e80cfcfc | bellard | |
187 | 8d05ea8a | blueswir1 | DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val); |
188 | e80cfcfc | bellard | saddr = (addr & TIMER_MAXADDR) >> 2;
|
189 | e80cfcfc | bellard | switch (saddr) {
|
190 | d2c38b24 | blueswir1 | case TIMER_LIMIT:
|
191 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) {
|
192 | e1cb9502 | blueswir1 | uint64_t count; |
193 | e1cb9502 | blueswir1 | |
194 | 115646b6 | blueswir1 | // set user counter MSW, reset counter
|
195 | d2c38b24 | blueswir1 | s->limit = TIMER_MAX_COUNT64; |
196 | e1cb9502 | blueswir1 | s->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
|
197 | e1cb9502 | blueswir1 | s->reached = 0;
|
198 | e1cb9502 | blueswir1 | count = ((uint64_t)s->counthigh << 32) | s->count;
|
199 | e1cb9502 | blueswir1 | DPRINTF("processor %d user timer set to %016llx\n", s->slave_index,
|
200 | e1cb9502 | blueswir1 | count); |
201 | 67e42751 | blueswir1 | if (s->timer)
|
202 | e1cb9502 | blueswir1 | ptimer_set_count(s->timer, LIMIT_TO_PERIODS(s->limit - count)); |
203 | 115646b6 | blueswir1 | } else {
|
204 | 115646b6 | blueswir1 | // set limit, reset counter
|
205 | 115646b6 | blueswir1 | qemu_irq_lower(s->irq); |
206 | d2c38b24 | blueswir1 | s->limit = val & TIMER_MAX_COUNT32; |
207 | 85e3023e | blueswir1 | if (s->timer) {
|
208 | 85e3023e | blueswir1 | if (s->limit == 0) /* free-run */ |
209 | 77f193da | blueswir1 | ptimer_set_limit(s->timer, |
210 | 77f193da | blueswir1 | LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
|
211 | 85e3023e | blueswir1 | else
|
212 | 85e3023e | blueswir1 | ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
|
213 | 85e3023e | blueswir1 | } |
214 | 81732d19 | blueswir1 | } |
215 | 115646b6 | blueswir1 | break;
|
216 | d2c38b24 | blueswir1 | case TIMER_COUNTER:
|
217 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) {
|
218 | e1cb9502 | blueswir1 | uint64_t count; |
219 | e1cb9502 | blueswir1 | |
220 | 115646b6 | blueswir1 | // set user counter LSW, reset counter
|
221 | d2c38b24 | blueswir1 | s->limit = TIMER_MAX_COUNT64; |
222 | e1cb9502 | blueswir1 | s->count = val & TIMER_MAX_COUNT64; |
223 | e1cb9502 | blueswir1 | s->reached = 0;
|
224 | e1cb9502 | blueswir1 | count = ((uint64_t)s->counthigh) << 32 | s->count;
|
225 | e1cb9502 | blueswir1 | DPRINTF("processor %d user timer set to %016llx\n", s->slave_index,
|
226 | e1cb9502 | blueswir1 | count); |
227 | 67e42751 | blueswir1 | if (s->timer)
|
228 | e1cb9502 | blueswir1 | ptimer_set_count(s->timer, LIMIT_TO_PERIODS(s->limit - count)); |
229 | 115646b6 | blueswir1 | } else
|
230 | 115646b6 | blueswir1 | DPRINTF("not user timer\n");
|
231 | 115646b6 | blueswir1 | break;
|
232 | d2c38b24 | blueswir1 | case TIMER_COUNTER_NORST:
|
233 | f930d07e | blueswir1 | // set limit without resetting counter
|
234 | d2c38b24 | blueswir1 | s->limit = val & TIMER_MAX_COUNT32; |
235 | 85e3023e | blueswir1 | if (s->timer) {
|
236 | 85e3023e | blueswir1 | if (s->limit == 0) /* free-run */ |
237 | 77f193da | blueswir1 | ptimer_set_limit(s->timer, |
238 | 77f193da | blueswir1 | LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
|
239 | 85e3023e | blueswir1 | else
|
240 | 85e3023e | blueswir1 | ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 0);
|
241 | 85e3023e | blueswir1 | } |
242 | f930d07e | blueswir1 | break;
|
243 | d2c38b24 | blueswir1 | case TIMER_STATUS:
|
244 | 115646b6 | blueswir1 | if (slavio_timer_is_user(s)) {
|
245 | 115646b6 | blueswir1 | // start/stop user counter
|
246 | 115646b6 | blueswir1 | if ((val & 1) && !s->running) { |
247 | 115646b6 | blueswir1 | DPRINTF("processor %d user timer started\n", s->slave_index);
|
248 | 85e3023e | blueswir1 | if (s->timer)
|
249 | 85e3023e | blueswir1 | ptimer_run(s->timer, 0);
|
250 | 115646b6 | blueswir1 | s->running = 1;
|
251 | 115646b6 | blueswir1 | } else if (!(val & 1) && s->running) { |
252 | 115646b6 | blueswir1 | DPRINTF("processor %d user timer stopped\n", s->slave_index);
|
253 | 85e3023e | blueswir1 | if (s->timer)
|
254 | 85e3023e | blueswir1 | ptimer_stop(s->timer); |
255 | 115646b6 | blueswir1 | s->running = 0;
|
256 | f930d07e | blueswir1 | } |
257 | f930d07e | blueswir1 | } |
258 | f930d07e | blueswir1 | break;
|
259 | d2c38b24 | blueswir1 | case TIMER_MODE:
|
260 | 115646b6 | blueswir1 | if (s->master == NULL) { |
261 | 81732d19 | blueswir1 | unsigned int i; |
262 | 81732d19 | blueswir1 | |
263 | 19f8e5dd | blueswir1 | for (i = 0; i < s->num_slaves; i++) { |
264 | 67e42751 | blueswir1 | unsigned int processor = 1 << i; |
265 | 67e42751 | blueswir1 | |
266 | 67e42751 | blueswir1 | // check for a change in timer mode for this processor
|
267 | 67e42751 | blueswir1 | if ((val & processor) != (s->slave_mode & processor)) {
|
268 | 67e42751 | blueswir1 | if (val & processor) { // counter -> user timer |
269 | 67e42751 | blueswir1 | qemu_irq_lower(s->slave[i]->irq); |
270 | 67e42751 | blueswir1 | // counters are always running
|
271 | 67e42751 | blueswir1 | ptimer_stop(s->slave[i]->timer); |
272 | 67e42751 | blueswir1 | s->slave[i]->running = 0;
|
273 | 67e42751 | blueswir1 | // user timer limit is always the same
|
274 | 67e42751 | blueswir1 | s->slave[i]->limit = TIMER_MAX_COUNT64; |
275 | 67e42751 | blueswir1 | ptimer_set_limit(s->slave[i]->timer, |
276 | 77f193da | blueswir1 | LIMIT_TO_PERIODS(s->slave[i]->limit), |
277 | 77f193da | blueswir1 | 1);
|
278 | 67e42751 | blueswir1 | // set this processors user timer bit in config
|
279 | 67e42751 | blueswir1 | // register
|
280 | 67e42751 | blueswir1 | s->slave_mode |= processor; |
281 | 67e42751 | blueswir1 | DPRINTF("processor %d changed from counter to user "
|
282 | 67e42751 | blueswir1 | "timer\n", s->slave[i]->slave_index);
|
283 | 67e42751 | blueswir1 | } else { // user timer -> counter |
284 | 67e42751 | blueswir1 | // stop the user timer if it is running
|
285 | 67e42751 | blueswir1 | if (s->slave[i]->running)
|
286 | 67e42751 | blueswir1 | ptimer_stop(s->slave[i]->timer); |
287 | 67e42751 | blueswir1 | // start the counter
|
288 | 67e42751 | blueswir1 | ptimer_run(s->slave[i]->timer, 0);
|
289 | 67e42751 | blueswir1 | s->slave[i]->running = 1;
|
290 | 67e42751 | blueswir1 | // clear this processors user timer bit in config
|
291 | 67e42751 | blueswir1 | // register
|
292 | 67e42751 | blueswir1 | s->slave_mode &= ~processor; |
293 | 67e42751 | blueswir1 | DPRINTF("processor %d changed from user timer to "
|
294 | 67e42751 | blueswir1 | "counter\n", s->slave[i]->slave_index);
|
295 | 67e42751 | blueswir1 | } |
296 | 115646b6 | blueswir1 | } |
297 | 81732d19 | blueswir1 | } |
298 | 115646b6 | blueswir1 | } else
|
299 | 115646b6 | blueswir1 | DPRINTF("not system timer\n");
|
300 | f930d07e | blueswir1 | break;
|
301 | e80cfcfc | bellard | default:
|
302 | 115646b6 | blueswir1 | DPRINTF("invalid write address " TARGET_FMT_plx "\n", addr); |
303 | f930d07e | blueswir1 | break;
|
304 | e80cfcfc | bellard | } |
305 | e80cfcfc | bellard | } |
306 | e80cfcfc | bellard | |
307 | e80cfcfc | bellard | static CPUReadMemoryFunc *slavio_timer_mem_read[3] = { |
308 | 7c560456 | blueswir1 | NULL,
|
309 | 7c560456 | blueswir1 | NULL,
|
310 | e80cfcfc | bellard | slavio_timer_mem_readl, |
311 | e80cfcfc | bellard | }; |
312 | e80cfcfc | bellard | |
313 | e80cfcfc | bellard | static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = { |
314 | 7c560456 | blueswir1 | NULL,
|
315 | 7c560456 | blueswir1 | NULL,
|
316 | e80cfcfc | bellard | slavio_timer_mem_writel, |
317 | e80cfcfc | bellard | }; |
318 | e80cfcfc | bellard | |
319 | e80cfcfc | bellard | static void slavio_timer_save(QEMUFile *f, void *opaque) |
320 | e80cfcfc | bellard | { |
321 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
322 | e80cfcfc | bellard | |
323 | 8d05ea8a | blueswir1 | qemu_put_be64s(f, &s->limit); |
324 | e80cfcfc | bellard | qemu_put_be32s(f, &s->count); |
325 | e80cfcfc | bellard | qemu_put_be32s(f, &s->counthigh); |
326 | e80cfcfc | bellard | qemu_put_be32s(f, &s->reached); |
327 | 115646b6 | blueswir1 | qemu_put_be32s(f, &s->running); |
328 | 85e3023e | blueswir1 | if (s->timer)
|
329 | 85e3023e | blueswir1 | qemu_put_ptimer(f, s->timer); |
330 | e80cfcfc | bellard | } |
331 | e80cfcfc | bellard | |
332 | e80cfcfc | bellard | static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id) |
333 | e80cfcfc | bellard | { |
334 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
335 | 3b46e624 | ths | |
336 | 85e3023e | blueswir1 | if (version_id != 3) |
337 | e80cfcfc | bellard | return -EINVAL;
|
338 | e80cfcfc | bellard | |
339 | 8d05ea8a | blueswir1 | qemu_get_be64s(f, &s->limit); |
340 | e80cfcfc | bellard | qemu_get_be32s(f, &s->count); |
341 | e80cfcfc | bellard | qemu_get_be32s(f, &s->counthigh); |
342 | e80cfcfc | bellard | qemu_get_be32s(f, &s->reached); |
343 | 115646b6 | blueswir1 | qemu_get_be32s(f, &s->running); |
344 | 85e3023e | blueswir1 | if (s->timer)
|
345 | 85e3023e | blueswir1 | qemu_get_ptimer(f, s->timer); |
346 | 8d05ea8a | blueswir1 | |
347 | e80cfcfc | bellard | return 0; |
348 | e80cfcfc | bellard | } |
349 | e80cfcfc | bellard | |
350 | e80cfcfc | bellard | static void slavio_timer_reset(void *opaque) |
351 | e80cfcfc | bellard | { |
352 | e80cfcfc | bellard | SLAVIO_TIMERState *s = opaque; |
353 | e80cfcfc | bellard | |
354 | 3b4aa426 | blueswir1 | s->limit = 0;
|
355 | e80cfcfc | bellard | s->count = 0;
|
356 | e80cfcfc | bellard | s->reached = 0;
|
357 | 3b4aa426 | blueswir1 | s->slave_mode = 0;
|
358 | 85e3023e | blueswir1 | if (!s->master || s->slave_index < s->master->num_slaves) {
|
359 | 85e3023e | blueswir1 | ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
|
360 | 85e3023e | blueswir1 | ptimer_run(s->timer, 0);
|
361 | 85e3023e | blueswir1 | } |
362 | 115646b6 | blueswir1 | s->running = 1;
|
363 | d7edfd27 | blueswir1 | qemu_irq_lower(s->irq); |
364 | e80cfcfc | bellard | } |
365 | e80cfcfc | bellard | |
366 | 81732d19 | blueswir1 | static SLAVIO_TIMERState *slavio_timer_init(target_phys_addr_t addr,
|
367 | 115646b6 | blueswir1 | qemu_irq irq, |
368 | 115646b6 | blueswir1 | SLAVIO_TIMERState *master, |
369 | 22548760 | blueswir1 | uint32_t slave_index) |
370 | e80cfcfc | bellard | { |
371 | e80cfcfc | bellard | int slavio_timer_io_memory;
|
372 | e80cfcfc | bellard | SLAVIO_TIMERState *s; |
373 | 8d05ea8a | blueswir1 | QEMUBH *bh; |
374 | e80cfcfc | bellard | |
375 | e80cfcfc | bellard | s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
|
376 | e80cfcfc | bellard | if (!s)
|
377 | 81732d19 | blueswir1 | return s;
|
378 | e80cfcfc | bellard | s->irq = irq; |
379 | 115646b6 | blueswir1 | s->master = master; |
380 | 115646b6 | blueswir1 | s->slave_index = slave_index; |
381 | 85e3023e | blueswir1 | if (!master || slave_index < master->num_slaves) {
|
382 | 85e3023e | blueswir1 | bh = qemu_bh_new(slavio_timer_irq, s); |
383 | 85e3023e | blueswir1 | s->timer = ptimer_init(bh); |
384 | 85e3023e | blueswir1 | ptimer_set_period(s->timer, TIMER_PERIOD); |
385 | 85e3023e | blueswir1 | } |
386 | e80cfcfc | bellard | |
387 | e80cfcfc | bellard | slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
|
388 | f930d07e | blueswir1 | slavio_timer_mem_write, s); |
389 | 115646b6 | blueswir1 | if (master)
|
390 | d2c38b24 | blueswir1 | cpu_register_physical_memory(addr, CPU_TIMER_SIZE, |
391 | d2c38b24 | blueswir1 | slavio_timer_io_memory); |
392 | 81732d19 | blueswir1 | else
|
393 | d2c38b24 | blueswir1 | cpu_register_physical_memory(addr, SYS_TIMER_SIZE, |
394 | d2c38b24 | blueswir1 | slavio_timer_io_memory); |
395 | 85e3023e | blueswir1 | register_savevm("slavio_timer", addr, 3, slavio_timer_save, |
396 | d2c38b24 | blueswir1 | slavio_timer_load, s); |
397 | e80cfcfc | bellard | qemu_register_reset(slavio_timer_reset, s); |
398 | e80cfcfc | bellard | slavio_timer_reset(s); |
399 | 81732d19 | blueswir1 | |
400 | 81732d19 | blueswir1 | return s;
|
401 | 81732d19 | blueswir1 | } |
402 | 81732d19 | blueswir1 | |
403 | 81732d19 | blueswir1 | void slavio_timer_init_all(target_phys_addr_t base, qemu_irq master_irq,
|
404 | 19f8e5dd | blueswir1 | qemu_irq *cpu_irqs, unsigned int num_cpus) |
405 | 81732d19 | blueswir1 | { |
406 | 81732d19 | blueswir1 | SLAVIO_TIMERState *master; |
407 | 81732d19 | blueswir1 | unsigned int i; |
408 | 81732d19 | blueswir1 | |
409 | d2c38b24 | blueswir1 | master = slavio_timer_init(base + SYS_TIMER_OFFSET, master_irq, NULL, 0); |
410 | 81732d19 | blueswir1 | |
411 | 19f8e5dd | blueswir1 | master->num_slaves = num_cpus; |
412 | 19f8e5dd | blueswir1 | |
413 | 81732d19 | blueswir1 | for (i = 0; i < MAX_CPUS; i++) { |
414 | 81732d19 | blueswir1 | master->slave[i] = slavio_timer_init(base + (target_phys_addr_t) |
415 | d2c38b24 | blueswir1 | CPU_TIMER_OFFSET(i), |
416 | 115646b6 | blueswir1 | cpu_irqs[i], master, i); |
417 | 81732d19 | blueswir1 | } |
418 | e80cfcfc | bellard | } |