]>
Commit | Line | Data |
---|---|---|
a541f297 | 1 | /* |
e9df014c | 2 | * QEMU generic PowerPC hardware System Emulator |
5fafdf24 | 3 | * |
76a66253 | 4 | * Copyright (c) 2003-2007 Jocelyn Mayer |
5fafdf24 | 5 | * |
a541f297 FB |
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 | */ | |
87ecb68b PB |
24 | #include "hw.h" |
25 | #include "ppc.h" | |
26 | #include "qemu-timer.h" | |
27 | #include "sysemu.h" | |
28 | #include "nvram.h" | |
3b3fb322 | 29 | #include "qemu-log.h" |
ca20cf32 | 30 | #include "loader.h" |
fc87e185 AG |
31 | #include "kvm.h" |
32 | #include "kvm_ppc.h" | |
a541f297 | 33 | |
e9df014c | 34 | //#define PPC_DEBUG_IRQ |
4b6d0a4c | 35 | //#define PPC_DEBUG_TB |
e9df014c | 36 | |
d12d51d5 | 37 | #ifdef PPC_DEBUG_IRQ |
93fcfe39 | 38 | # define LOG_IRQ(...) qemu_log_mask(CPU_LOG_INT, ## __VA_ARGS__) |
d12d51d5 AL |
39 | #else |
40 | # define LOG_IRQ(...) do { } while (0) | |
41 | #endif | |
42 | ||
43 | ||
44 | #ifdef PPC_DEBUG_TB | |
93fcfe39 | 45 | # define LOG_TB(...) qemu_log(__VA_ARGS__) |
d12d51d5 AL |
46 | #else |
47 | # define LOG_TB(...) do { } while (0) | |
48 | #endif | |
49 | ||
dbdd2506 JM |
50 | static void cpu_ppc_tb_stop (CPUState *env); |
51 | static void cpu_ppc_tb_start (CPUState *env); | |
52 | ||
00af685f | 53 | static void ppc_set_irq (CPUState *env, int n_IRQ, int level) |
47103572 | 54 | { |
fc87e185 AG |
55 | unsigned int old_pending = env->pending_interrupts; |
56 | ||
47103572 JM |
57 | if (level) { |
58 | env->pending_interrupts |= 1 << n_IRQ; | |
59 | cpu_interrupt(env, CPU_INTERRUPT_HARD); | |
60 | } else { | |
61 | env->pending_interrupts &= ~(1 << n_IRQ); | |
62 | if (env->pending_interrupts == 0) | |
63 | cpu_reset_interrupt(env, CPU_INTERRUPT_HARD); | |
64 | } | |
fc87e185 AG |
65 | |
66 | if (old_pending != env->pending_interrupts) { | |
67 | #ifdef CONFIG_KVM | |
68 | kvmppc_set_interrupt(env, n_IRQ, level); | |
69 | #endif | |
70 | } | |
71 | ||
d12d51d5 | 72 | LOG_IRQ("%s: %p n_IRQ %d level %d => pending %08" PRIx32 |
aae9366a | 73 | "req %08x\n", __func__, env, n_IRQ, level, |
a496775f | 74 | env->pending_interrupts, env->interrupt_request); |
47103572 JM |
75 | } |
76 | ||
e9df014c JM |
77 | /* PowerPC 6xx / 7xx internal IRQ controller */ |
78 | static void ppc6xx_set_irq (void *opaque, int pin, int level) | |
d537cf6c | 79 | { |
e9df014c JM |
80 | CPUState *env = opaque; |
81 | int cur_level; | |
d537cf6c | 82 | |
d12d51d5 | 83 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, |
a496775f | 84 | env, pin, level); |
e9df014c JM |
85 | cur_level = (env->irq_input_state >> pin) & 1; |
86 | /* Don't generate spurious events */ | |
24be5ae3 | 87 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { |
e9df014c | 88 | switch (pin) { |
dbdd2506 JM |
89 | case PPC6xx_INPUT_TBEN: |
90 | /* Level sensitive - active high */ | |
d12d51d5 | 91 | LOG_IRQ("%s: %s the time base\n", |
dbdd2506 | 92 | __func__, level ? "start" : "stop"); |
dbdd2506 JM |
93 | if (level) { |
94 | cpu_ppc_tb_start(env); | |
95 | } else { | |
96 | cpu_ppc_tb_stop(env); | |
97 | } | |
24be5ae3 JM |
98 | case PPC6xx_INPUT_INT: |
99 | /* Level sensitive - active high */ | |
d12d51d5 | 100 | LOG_IRQ("%s: set the external IRQ state to %d\n", |
a496775f | 101 | __func__, level); |
e9df014c JM |
102 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); |
103 | break; | |
24be5ae3 | 104 | case PPC6xx_INPUT_SMI: |
e9df014c | 105 | /* Level sensitive - active high */ |
d12d51d5 | 106 | LOG_IRQ("%s: set the SMI IRQ state to %d\n", |
a496775f | 107 | __func__, level); |
e9df014c JM |
108 | ppc_set_irq(env, PPC_INTERRUPT_SMI, level); |
109 | break; | |
24be5ae3 | 110 | case PPC6xx_INPUT_MCP: |
e9df014c JM |
111 | /* Negative edge sensitive */ |
112 | /* XXX: TODO: actual reaction may depends on HID0 status | |
113 | * 603/604/740/750: check HID0[EMCP] | |
114 | */ | |
115 | if (cur_level == 1 && level == 0) { | |
d12d51d5 | 116 | LOG_IRQ("%s: raise machine check state\n", |
a496775f | 117 | __func__); |
e9df014c JM |
118 | ppc_set_irq(env, PPC_INTERRUPT_MCK, 1); |
119 | } | |
120 | break; | |
24be5ae3 | 121 | case PPC6xx_INPUT_CKSTP_IN: |
e9df014c JM |
122 | /* Level sensitive - active low */ |
123 | /* XXX: TODO: relay the signal to CKSTP_OUT pin */ | |
e63ecc6f | 124 | /* XXX: Note that the only way to restart the CPU is to reset it */ |
e9df014c | 125 | if (level) { |
d12d51d5 | 126 | LOG_IRQ("%s: stop the CPU\n", __func__); |
e9df014c | 127 | env->halted = 1; |
e9df014c JM |
128 | } |
129 | break; | |
24be5ae3 | 130 | case PPC6xx_INPUT_HRESET: |
e9df014c JM |
131 | /* Level sensitive - active low */ |
132 | if (level) { | |
d12d51d5 | 133 | LOG_IRQ("%s: reset the CPU\n", __func__); |
ef397e88 JM |
134 | env->interrupt_request |= CPU_INTERRUPT_EXITTB; |
135 | /* XXX: TOFIX */ | |
136 | #if 0 | |
d84bda46 | 137 | cpu_reset(env); |
ef397e88 JM |
138 | #else |
139 | qemu_system_reset_request(); | |
e9df014c JM |
140 | #endif |
141 | } | |
142 | break; | |
24be5ae3 | 143 | case PPC6xx_INPUT_SRESET: |
d12d51d5 | 144 | LOG_IRQ("%s: set the RESET IRQ state to %d\n", |
a496775f | 145 | __func__, level); |
e9df014c JM |
146 | ppc_set_irq(env, PPC_INTERRUPT_RESET, level); |
147 | break; | |
148 | default: | |
149 | /* Unknown pin - do nothing */ | |
d12d51d5 | 150 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); |
e9df014c JM |
151 | return; |
152 | } | |
153 | if (level) | |
154 | env->irq_input_state |= 1 << pin; | |
155 | else | |
156 | env->irq_input_state &= ~(1 << pin); | |
d537cf6c PB |
157 | } |
158 | } | |
159 | ||
e9df014c | 160 | void ppc6xx_irq_init (CPUState *env) |
47103572 | 161 | { |
7b62a955 JM |
162 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, env, |
163 | PPC6xx_INPUT_NB); | |
47103572 JM |
164 | } |
165 | ||
00af685f | 166 | #if defined(TARGET_PPC64) |
d0dfae6e JM |
167 | /* PowerPC 970 internal IRQ controller */ |
168 | static void ppc970_set_irq (void *opaque, int pin, int level) | |
169 | { | |
170 | CPUState *env = opaque; | |
171 | int cur_level; | |
172 | ||
d12d51d5 | 173 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, |
d0dfae6e | 174 | env, pin, level); |
d0dfae6e JM |
175 | cur_level = (env->irq_input_state >> pin) & 1; |
176 | /* Don't generate spurious events */ | |
177 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { | |
178 | switch (pin) { | |
179 | case PPC970_INPUT_INT: | |
180 | /* Level sensitive - active high */ | |
d12d51d5 | 181 | LOG_IRQ("%s: set the external IRQ state to %d\n", |
d0dfae6e | 182 | __func__, level); |
d0dfae6e JM |
183 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); |
184 | break; | |
185 | case PPC970_INPUT_THINT: | |
186 | /* Level sensitive - active high */ | |
d12d51d5 | 187 | LOG_IRQ("%s: set the SMI IRQ state to %d\n", __func__, |
d0dfae6e | 188 | level); |
d0dfae6e JM |
189 | ppc_set_irq(env, PPC_INTERRUPT_THERM, level); |
190 | break; | |
191 | case PPC970_INPUT_MCP: | |
192 | /* Negative edge sensitive */ | |
193 | /* XXX: TODO: actual reaction may depends on HID0 status | |
194 | * 603/604/740/750: check HID0[EMCP] | |
195 | */ | |
196 | if (cur_level == 1 && level == 0) { | |
d12d51d5 | 197 | LOG_IRQ("%s: raise machine check state\n", |
d0dfae6e | 198 | __func__); |
d0dfae6e JM |
199 | ppc_set_irq(env, PPC_INTERRUPT_MCK, 1); |
200 | } | |
201 | break; | |
202 | case PPC970_INPUT_CKSTP: | |
203 | /* Level sensitive - active low */ | |
204 | /* XXX: TODO: relay the signal to CKSTP_OUT pin */ | |
205 | if (level) { | |
d12d51d5 | 206 | LOG_IRQ("%s: stop the CPU\n", __func__); |
d0dfae6e JM |
207 | env->halted = 1; |
208 | } else { | |
d12d51d5 | 209 | LOG_IRQ("%s: restart the CPU\n", __func__); |
d0dfae6e | 210 | env->halted = 0; |
94ad5b00 | 211 | qemu_cpu_kick(env); |
d0dfae6e JM |
212 | } |
213 | break; | |
214 | case PPC970_INPUT_HRESET: | |
215 | /* Level sensitive - active low */ | |
216 | if (level) { | |
217 | #if 0 // XXX: TOFIX | |
d12d51d5 | 218 | LOG_IRQ("%s: reset the CPU\n", __func__); |
d0dfae6e JM |
219 | cpu_reset(env); |
220 | #endif | |
221 | } | |
222 | break; | |
223 | case PPC970_INPUT_SRESET: | |
d12d51d5 | 224 | LOG_IRQ("%s: set the RESET IRQ state to %d\n", |
d0dfae6e | 225 | __func__, level); |
d0dfae6e JM |
226 | ppc_set_irq(env, PPC_INTERRUPT_RESET, level); |
227 | break; | |
228 | case PPC970_INPUT_TBEN: | |
d12d51d5 | 229 | LOG_IRQ("%s: set the TBEN state to %d\n", __func__, |
d0dfae6e | 230 | level); |
d0dfae6e JM |
231 | /* XXX: TODO */ |
232 | break; | |
233 | default: | |
234 | /* Unknown pin - do nothing */ | |
d12d51d5 | 235 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); |
d0dfae6e JM |
236 | return; |
237 | } | |
238 | if (level) | |
239 | env->irq_input_state |= 1 << pin; | |
240 | else | |
241 | env->irq_input_state &= ~(1 << pin); | |
242 | } | |
243 | } | |
244 | ||
245 | void ppc970_irq_init (CPUState *env) | |
246 | { | |
7b62a955 JM |
247 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppc970_set_irq, env, |
248 | PPC970_INPUT_NB); | |
d0dfae6e | 249 | } |
00af685f | 250 | #endif /* defined(TARGET_PPC64) */ |
d0dfae6e | 251 | |
4e290a0b JM |
252 | /* PowerPC 40x internal IRQ controller */ |
253 | static void ppc40x_set_irq (void *opaque, int pin, int level) | |
24be5ae3 JM |
254 | { |
255 | CPUState *env = opaque; | |
256 | int cur_level; | |
257 | ||
d12d51d5 | 258 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, |
8ecc7913 | 259 | env, pin, level); |
24be5ae3 JM |
260 | cur_level = (env->irq_input_state >> pin) & 1; |
261 | /* Don't generate spurious events */ | |
262 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { | |
263 | switch (pin) { | |
4e290a0b | 264 | case PPC40x_INPUT_RESET_SYS: |
8ecc7913 | 265 | if (level) { |
d12d51d5 | 266 | LOG_IRQ("%s: reset the PowerPC system\n", |
8ecc7913 | 267 | __func__); |
8ecc7913 JM |
268 | ppc40x_system_reset(env); |
269 | } | |
270 | break; | |
4e290a0b | 271 | case PPC40x_INPUT_RESET_CHIP: |
8ecc7913 | 272 | if (level) { |
d12d51d5 | 273 | LOG_IRQ("%s: reset the PowerPC chip\n", __func__); |
8ecc7913 JM |
274 | ppc40x_chip_reset(env); |
275 | } | |
276 | break; | |
4e290a0b | 277 | case PPC40x_INPUT_RESET_CORE: |
24be5ae3 JM |
278 | /* XXX: TODO: update DBSR[MRR] */ |
279 | if (level) { | |
d12d51d5 | 280 | LOG_IRQ("%s: reset the PowerPC core\n", __func__); |
8ecc7913 | 281 | ppc40x_core_reset(env); |
24be5ae3 JM |
282 | } |
283 | break; | |
4e290a0b | 284 | case PPC40x_INPUT_CINT: |
24be5ae3 | 285 | /* Level sensitive - active high */ |
d12d51d5 | 286 | LOG_IRQ("%s: set the critical IRQ state to %d\n", |
8ecc7913 | 287 | __func__, level); |
4e290a0b | 288 | ppc_set_irq(env, PPC_INTERRUPT_CEXT, level); |
24be5ae3 | 289 | break; |
4e290a0b | 290 | case PPC40x_INPUT_INT: |
24be5ae3 | 291 | /* Level sensitive - active high */ |
d12d51d5 | 292 | LOG_IRQ("%s: set the external IRQ state to %d\n", |
a496775f | 293 | __func__, level); |
24be5ae3 JM |
294 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); |
295 | break; | |
4e290a0b | 296 | case PPC40x_INPUT_HALT: |
24be5ae3 JM |
297 | /* Level sensitive - active low */ |
298 | if (level) { | |
d12d51d5 | 299 | LOG_IRQ("%s: stop the CPU\n", __func__); |
24be5ae3 JM |
300 | env->halted = 1; |
301 | } else { | |
d12d51d5 | 302 | LOG_IRQ("%s: restart the CPU\n", __func__); |
24be5ae3 | 303 | env->halted = 0; |
94ad5b00 | 304 | qemu_cpu_kick(env); |
24be5ae3 JM |
305 | } |
306 | break; | |
4e290a0b | 307 | case PPC40x_INPUT_DEBUG: |
24be5ae3 | 308 | /* Level sensitive - active high */ |
d12d51d5 | 309 | LOG_IRQ("%s: set the debug pin state to %d\n", |
a496775f | 310 | __func__, level); |
a750fc0b | 311 | ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level); |
24be5ae3 JM |
312 | break; |
313 | default: | |
314 | /* Unknown pin - do nothing */ | |
d12d51d5 | 315 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); |
24be5ae3 JM |
316 | return; |
317 | } | |
318 | if (level) | |
319 | env->irq_input_state |= 1 << pin; | |
320 | else | |
321 | env->irq_input_state &= ~(1 << pin); | |
322 | } | |
323 | } | |
324 | ||
4e290a0b | 325 | void ppc40x_irq_init (CPUState *env) |
24be5ae3 | 326 | { |
4e290a0b JM |
327 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppc40x_set_irq, |
328 | env, PPC40x_INPUT_NB); | |
24be5ae3 JM |
329 | } |
330 | ||
9fdc60bf AJ |
331 | /* PowerPC E500 internal IRQ controller */ |
332 | static void ppce500_set_irq (void *opaque, int pin, int level) | |
333 | { | |
334 | CPUState *env = opaque; | |
335 | int cur_level; | |
336 | ||
337 | LOG_IRQ("%s: env %p pin %d level %d\n", __func__, | |
338 | env, pin, level); | |
339 | cur_level = (env->irq_input_state >> pin) & 1; | |
340 | /* Don't generate spurious events */ | |
341 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { | |
342 | switch (pin) { | |
343 | case PPCE500_INPUT_MCK: | |
344 | if (level) { | |
345 | LOG_IRQ("%s: reset the PowerPC system\n", | |
346 | __func__); | |
347 | qemu_system_reset_request(); | |
348 | } | |
349 | break; | |
350 | case PPCE500_INPUT_RESET_CORE: | |
351 | if (level) { | |
352 | LOG_IRQ("%s: reset the PowerPC core\n", __func__); | |
353 | ppc_set_irq(env, PPC_INTERRUPT_MCK, level); | |
354 | } | |
355 | break; | |
356 | case PPCE500_INPUT_CINT: | |
357 | /* Level sensitive - active high */ | |
358 | LOG_IRQ("%s: set the critical IRQ state to %d\n", | |
359 | __func__, level); | |
360 | ppc_set_irq(env, PPC_INTERRUPT_CEXT, level); | |
361 | break; | |
362 | case PPCE500_INPUT_INT: | |
363 | /* Level sensitive - active high */ | |
364 | LOG_IRQ("%s: set the core IRQ state to %d\n", | |
365 | __func__, level); | |
366 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); | |
367 | break; | |
368 | case PPCE500_INPUT_DEBUG: | |
369 | /* Level sensitive - active high */ | |
370 | LOG_IRQ("%s: set the debug pin state to %d\n", | |
371 | __func__, level); | |
372 | ppc_set_irq(env, PPC_INTERRUPT_DEBUG, level); | |
373 | break; | |
374 | default: | |
375 | /* Unknown pin - do nothing */ | |
376 | LOG_IRQ("%s: unknown IRQ pin %d\n", __func__, pin); | |
377 | return; | |
378 | } | |
379 | if (level) | |
380 | env->irq_input_state |= 1 << pin; | |
381 | else | |
382 | env->irq_input_state &= ~(1 << pin); | |
383 | } | |
384 | } | |
385 | ||
386 | void ppce500_irq_init (CPUState *env) | |
387 | { | |
388 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppce500_set_irq, | |
389 | env, PPCE500_INPUT_NB); | |
390 | } | |
9fddaa0c | 391 | /*****************************************************************************/ |
e9df014c | 392 | /* PowerPC time base and decrementer emulation */ |
c227f099 | 393 | struct ppc_tb_t { |
9fddaa0c | 394 | /* Time base management */ |
dbdd2506 JM |
395 | int64_t tb_offset; /* Compensation */ |
396 | int64_t atb_offset; /* Compensation */ | |
397 | uint32_t tb_freq; /* TB frequency */ | |
9fddaa0c | 398 | /* Decrementer management */ |
dbdd2506 JM |
399 | uint64_t decr_next; /* Tick for next decr interrupt */ |
400 | uint32_t decr_freq; /* decrementer frequency */ | |
9fddaa0c | 401 | struct QEMUTimer *decr_timer; |
58a7d328 JM |
402 | /* Hypervisor decrementer management */ |
403 | uint64_t hdecr_next; /* Tick for next hdecr interrupt */ | |
404 | struct QEMUTimer *hdecr_timer; | |
405 | uint64_t purr_load; | |
406 | uint64_t purr_start; | |
47103572 | 407 | void *opaque; |
9fddaa0c FB |
408 | }; |
409 | ||
c227f099 | 410 | static inline uint64_t cpu_ppc_get_tb(ppc_tb_t *tb_env, uint64_t vmclk, |
636aa200 | 411 | int64_t tb_offset) |
9fddaa0c FB |
412 | { |
413 | /* TB time in tb periods */ | |
6ee093c9 | 414 | return muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()) + tb_offset; |
9fddaa0c FB |
415 | } |
416 | ||
e3ea6529 | 417 | uint64_t cpu_ppc_load_tbl (CPUState *env) |
9fddaa0c | 418 | { |
c227f099 | 419 | ppc_tb_t *tb_env = env->tb_env; |
9fddaa0c FB |
420 | uint64_t tb; |
421 | ||
74475455 | 422 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset); |
d12d51d5 | 423 | LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); |
9fddaa0c | 424 | |
e3ea6529 | 425 | return tb; |
9fddaa0c FB |
426 | } |
427 | ||
636aa200 | 428 | static inline uint32_t _cpu_ppc_load_tbu(CPUState *env) |
9fddaa0c | 429 | { |
c227f099 | 430 | ppc_tb_t *tb_env = env->tb_env; |
9fddaa0c FB |
431 | uint64_t tb; |
432 | ||
74475455 | 433 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset); |
d12d51d5 | 434 | LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); |
76a66253 | 435 | |
9fddaa0c FB |
436 | return tb >> 32; |
437 | } | |
438 | ||
8a84de23 JM |
439 | uint32_t cpu_ppc_load_tbu (CPUState *env) |
440 | { | |
441 | return _cpu_ppc_load_tbu(env); | |
442 | } | |
443 | ||
c227f099 | 444 | static inline void cpu_ppc_store_tb(ppc_tb_t *tb_env, uint64_t vmclk, |
636aa200 | 445 | int64_t *tb_offsetp, uint64_t value) |
9fddaa0c | 446 | { |
6ee093c9 | 447 | *tb_offsetp = value - muldiv64(vmclk, tb_env->tb_freq, get_ticks_per_sec()); |
d12d51d5 | 448 | LOG_TB("%s: tb %016" PRIx64 " offset %08" PRIx64 "\n", |
aae9366a | 449 | __func__, value, *tb_offsetp); |
9fddaa0c FB |
450 | } |
451 | ||
a062e36c JM |
452 | void cpu_ppc_store_tbl (CPUState *env, uint32_t value) |
453 | { | |
c227f099 | 454 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c JM |
455 | uint64_t tb; |
456 | ||
74475455 | 457 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset); |
a062e36c | 458 | tb &= 0xFFFFFFFF00000000ULL; |
74475455 | 459 | cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock), |
dbdd2506 | 460 | &tb_env->tb_offset, tb | (uint64_t)value); |
a062e36c JM |
461 | } |
462 | ||
636aa200 | 463 | static inline void _cpu_ppc_store_tbu(CPUState *env, uint32_t value) |
9fddaa0c | 464 | { |
c227f099 | 465 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c | 466 | uint64_t tb; |
9fddaa0c | 467 | |
74475455 | 468 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->tb_offset); |
a062e36c | 469 | tb &= 0x00000000FFFFFFFFULL; |
74475455 | 470 | cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock), |
dbdd2506 | 471 | &tb_env->tb_offset, ((uint64_t)value << 32) | tb); |
9fddaa0c FB |
472 | } |
473 | ||
8a84de23 JM |
474 | void cpu_ppc_store_tbu (CPUState *env, uint32_t value) |
475 | { | |
476 | _cpu_ppc_store_tbu(env, value); | |
477 | } | |
478 | ||
b711de95 | 479 | uint64_t cpu_ppc_load_atbl (CPUState *env) |
a062e36c | 480 | { |
c227f099 | 481 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c JM |
482 | uint64_t tb; |
483 | ||
74475455 | 484 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset); |
d12d51d5 | 485 | LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); |
a062e36c | 486 | |
b711de95 | 487 | return tb; |
a062e36c JM |
488 | } |
489 | ||
490 | uint32_t cpu_ppc_load_atbu (CPUState *env) | |
491 | { | |
c227f099 | 492 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c JM |
493 | uint64_t tb; |
494 | ||
74475455 | 495 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset); |
d12d51d5 | 496 | LOG_TB("%s: tb %016" PRIx64 "\n", __func__, tb); |
a062e36c JM |
497 | |
498 | return tb >> 32; | |
499 | } | |
500 | ||
501 | void cpu_ppc_store_atbl (CPUState *env, uint32_t value) | |
502 | { | |
c227f099 | 503 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c JM |
504 | uint64_t tb; |
505 | ||
74475455 | 506 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset); |
a062e36c | 507 | tb &= 0xFFFFFFFF00000000ULL; |
74475455 | 508 | cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock), |
dbdd2506 | 509 | &tb_env->atb_offset, tb | (uint64_t)value); |
a062e36c JM |
510 | } |
511 | ||
512 | void cpu_ppc_store_atbu (CPUState *env, uint32_t value) | |
9fddaa0c | 513 | { |
c227f099 | 514 | ppc_tb_t *tb_env = env->tb_env; |
a062e36c | 515 | uint64_t tb; |
9fddaa0c | 516 | |
74475455 | 517 | tb = cpu_ppc_get_tb(tb_env, qemu_get_clock_ns(vm_clock), tb_env->atb_offset); |
a062e36c | 518 | tb &= 0x00000000FFFFFFFFULL; |
74475455 | 519 | cpu_ppc_store_tb(tb_env, qemu_get_clock_ns(vm_clock), |
dbdd2506 JM |
520 | &tb_env->atb_offset, ((uint64_t)value << 32) | tb); |
521 | } | |
522 | ||
523 | static void cpu_ppc_tb_stop (CPUState *env) | |
524 | { | |
c227f099 | 525 | ppc_tb_t *tb_env = env->tb_env; |
dbdd2506 JM |
526 | uint64_t tb, atb, vmclk; |
527 | ||
528 | /* If the time base is already frozen, do nothing */ | |
529 | if (tb_env->tb_freq != 0) { | |
74475455 | 530 | vmclk = qemu_get_clock_ns(vm_clock); |
dbdd2506 JM |
531 | /* Get the time base */ |
532 | tb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->tb_offset); | |
533 | /* Get the alternate time base */ | |
534 | atb = cpu_ppc_get_tb(tb_env, vmclk, tb_env->atb_offset); | |
535 | /* Store the time base value (ie compute the current offset) */ | |
536 | cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb); | |
537 | /* Store the alternate time base value (compute the current offset) */ | |
538 | cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb); | |
539 | /* Set the time base frequency to zero */ | |
540 | tb_env->tb_freq = 0; | |
541 | /* Now, the time bases are frozen to tb_offset / atb_offset value */ | |
542 | } | |
543 | } | |
544 | ||
545 | static void cpu_ppc_tb_start (CPUState *env) | |
546 | { | |
c227f099 | 547 | ppc_tb_t *tb_env = env->tb_env; |
dbdd2506 | 548 | uint64_t tb, atb, vmclk; |
aae9366a | 549 | |
dbdd2506 JM |
550 | /* If the time base is not frozen, do nothing */ |
551 | if (tb_env->tb_freq == 0) { | |
74475455 | 552 | vmclk = qemu_get_clock_ns(vm_clock); |
dbdd2506 JM |
553 | /* Get the time base from tb_offset */ |
554 | tb = tb_env->tb_offset; | |
555 | /* Get the alternate time base from atb_offset */ | |
556 | atb = tb_env->atb_offset; | |
557 | /* Restore the tb frequency from the decrementer frequency */ | |
558 | tb_env->tb_freq = tb_env->decr_freq; | |
559 | /* Store the time base value */ | |
560 | cpu_ppc_store_tb(tb_env, vmclk, &tb_env->tb_offset, tb); | |
561 | /* Store the alternate time base value */ | |
562 | cpu_ppc_store_tb(tb_env, vmclk, &tb_env->atb_offset, atb); | |
563 | } | |
9fddaa0c FB |
564 | } |
565 | ||
636aa200 | 566 | static inline uint32_t _cpu_ppc_load_decr(CPUState *env, uint64_t next) |
9fddaa0c | 567 | { |
c227f099 | 568 | ppc_tb_t *tb_env = env->tb_env; |
9fddaa0c | 569 | uint32_t decr; |
4e588a4d | 570 | int64_t diff; |
9fddaa0c | 571 | |
74475455 | 572 | diff = next - qemu_get_clock_ns(vm_clock); |
4e588a4d | 573 | if (diff >= 0) |
6ee093c9 | 574 | decr = muldiv64(diff, tb_env->decr_freq, get_ticks_per_sec()); |
4e588a4d | 575 | else |
6ee093c9 | 576 | decr = -muldiv64(-diff, tb_env->decr_freq, get_ticks_per_sec()); |
d12d51d5 | 577 | LOG_TB("%s: %08" PRIx32 "\n", __func__, decr); |
76a66253 | 578 | |
9fddaa0c FB |
579 | return decr; |
580 | } | |
581 | ||
58a7d328 JM |
582 | uint32_t cpu_ppc_load_decr (CPUState *env) |
583 | { | |
c227f099 | 584 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 | 585 | |
f55e9d9a | 586 | return _cpu_ppc_load_decr(env, tb_env->decr_next); |
58a7d328 JM |
587 | } |
588 | ||
58a7d328 JM |
589 | uint32_t cpu_ppc_load_hdecr (CPUState *env) |
590 | { | |
c227f099 | 591 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 | 592 | |
f55e9d9a | 593 | return _cpu_ppc_load_decr(env, tb_env->hdecr_next); |
58a7d328 JM |
594 | } |
595 | ||
596 | uint64_t cpu_ppc_load_purr (CPUState *env) | |
597 | { | |
c227f099 | 598 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 JM |
599 | uint64_t diff; |
600 | ||
74475455 | 601 | diff = qemu_get_clock_ns(vm_clock) - tb_env->purr_start; |
b33c17e1 | 602 | |
6ee093c9 | 603 | return tb_env->purr_load + muldiv64(diff, tb_env->tb_freq, get_ticks_per_sec()); |
58a7d328 | 604 | } |
58a7d328 | 605 | |
9fddaa0c FB |
606 | /* When decrementer expires, |
607 | * all we need to do is generate or queue a CPU exception | |
608 | */ | |
636aa200 | 609 | static inline void cpu_ppc_decr_excp(CPUState *env) |
9fddaa0c FB |
610 | { |
611 | /* Raise it */ | |
d12d51d5 | 612 | LOG_TB("raise decrementer exception\n"); |
47103572 | 613 | ppc_set_irq(env, PPC_INTERRUPT_DECR, 1); |
9fddaa0c FB |
614 | } |
615 | ||
636aa200 | 616 | static inline void cpu_ppc_hdecr_excp(CPUState *env) |
58a7d328 JM |
617 | { |
618 | /* Raise it */ | |
d12d51d5 | 619 | LOG_TB("raise decrementer exception\n"); |
58a7d328 JM |
620 | ppc_set_irq(env, PPC_INTERRUPT_HDECR, 1); |
621 | } | |
622 | ||
623 | static void __cpu_ppc_store_decr (CPUState *env, uint64_t *nextp, | |
b33c17e1 JM |
624 | struct QEMUTimer *timer, |
625 | void (*raise_excp)(CPUState *), | |
626 | uint32_t decr, uint32_t value, | |
627 | int is_excp) | |
9fddaa0c | 628 | { |
c227f099 | 629 | ppc_tb_t *tb_env = env->tb_env; |
9fddaa0c FB |
630 | uint64_t now, next; |
631 | ||
d12d51d5 | 632 | LOG_TB("%s: %08" PRIx32 " => %08" PRIx32 "\n", __func__, |
aae9366a | 633 | decr, value); |
74475455 | 634 | now = qemu_get_clock_ns(vm_clock); |
6ee093c9 | 635 | next = now + muldiv64(value, get_ticks_per_sec(), tb_env->decr_freq); |
9fddaa0c | 636 | if (is_excp) |
58a7d328 | 637 | next += *nextp - now; |
9fddaa0c | 638 | if (next == now) |
76a66253 | 639 | next++; |
58a7d328 | 640 | *nextp = next; |
9fddaa0c | 641 | /* Adjust timer */ |
58a7d328 | 642 | qemu_mod_timer(timer, next); |
9fddaa0c FB |
643 | /* If we set a negative value and the decrementer was positive, |
644 | * raise an exception. | |
645 | */ | |
646 | if ((value & 0x80000000) && !(decr & 0x80000000)) | |
58a7d328 JM |
647 | (*raise_excp)(env); |
648 | } | |
649 | ||
636aa200 BS |
650 | static inline void _cpu_ppc_store_decr(CPUState *env, uint32_t decr, |
651 | uint32_t value, int is_excp) | |
58a7d328 | 652 | { |
c227f099 | 653 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 JM |
654 | |
655 | __cpu_ppc_store_decr(env, &tb_env->decr_next, tb_env->decr_timer, | |
656 | &cpu_ppc_decr_excp, decr, value, is_excp); | |
9fddaa0c FB |
657 | } |
658 | ||
659 | void cpu_ppc_store_decr (CPUState *env, uint32_t value) | |
660 | { | |
661 | _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0); | |
662 | } | |
663 | ||
664 | static void cpu_ppc_decr_cb (void *opaque) | |
665 | { | |
666 | _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1); | |
667 | } | |
668 | ||
636aa200 BS |
669 | static inline void _cpu_ppc_store_hdecr(CPUState *env, uint32_t hdecr, |
670 | uint32_t value, int is_excp) | |
58a7d328 | 671 | { |
c227f099 | 672 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 | 673 | |
b172c56a JM |
674 | if (tb_env->hdecr_timer != NULL) { |
675 | __cpu_ppc_store_decr(env, &tb_env->hdecr_next, tb_env->hdecr_timer, | |
676 | &cpu_ppc_hdecr_excp, hdecr, value, is_excp); | |
677 | } | |
58a7d328 JM |
678 | } |
679 | ||
680 | void cpu_ppc_store_hdecr (CPUState *env, uint32_t value) | |
681 | { | |
682 | _cpu_ppc_store_hdecr(env, cpu_ppc_load_hdecr(env), value, 0); | |
683 | } | |
684 | ||
685 | static void cpu_ppc_hdecr_cb (void *opaque) | |
686 | { | |
687 | _cpu_ppc_store_hdecr(opaque, 0x00000000, 0xFFFFFFFF, 1); | |
688 | } | |
689 | ||
690 | void cpu_ppc_store_purr (CPUState *env, uint64_t value) | |
691 | { | |
c227f099 | 692 | ppc_tb_t *tb_env = env->tb_env; |
58a7d328 JM |
693 | |
694 | tb_env->purr_load = value; | |
74475455 | 695 | tb_env->purr_start = qemu_get_clock_ns(vm_clock); |
58a7d328 | 696 | } |
58a7d328 | 697 | |
8ecc7913 JM |
698 | static void cpu_ppc_set_tb_clk (void *opaque, uint32_t freq) |
699 | { | |
700 | CPUState *env = opaque; | |
c227f099 | 701 | ppc_tb_t *tb_env = env->tb_env; |
8ecc7913 JM |
702 | |
703 | tb_env->tb_freq = freq; | |
dbdd2506 | 704 | tb_env->decr_freq = freq; |
8ecc7913 JM |
705 | /* There is a bug in Linux 2.4 kernels: |
706 | * if a decrementer exception is pending when it enables msr_ee at startup, | |
707 | * it's not ready to handle it... | |
708 | */ | |
709 | _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0); | |
58a7d328 JM |
710 | _cpu_ppc_store_hdecr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0); |
711 | cpu_ppc_store_purr(env, 0x0000000000000000ULL); | |
8ecc7913 JM |
712 | } |
713 | ||
9fddaa0c | 714 | /* Set up (once) timebase frequency (in Hz) */ |
8ecc7913 | 715 | clk_setup_cb cpu_ppc_tb_init (CPUState *env, uint32_t freq) |
9fddaa0c | 716 | { |
c227f099 | 717 | ppc_tb_t *tb_env; |
9fddaa0c | 718 | |
c227f099 | 719 | tb_env = qemu_mallocz(sizeof(ppc_tb_t)); |
9fddaa0c | 720 | env->tb_env = tb_env; |
8ecc7913 | 721 | /* Create new timer */ |
74475455 | 722 | tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &cpu_ppc_decr_cb, env); |
b172c56a JM |
723 | if (0) { |
724 | /* XXX: find a suitable condition to enable the hypervisor decrementer | |
725 | */ | |
74475455 | 726 | tb_env->hdecr_timer = qemu_new_timer_ns(vm_clock, &cpu_ppc_hdecr_cb, env); |
b172c56a JM |
727 | } else { |
728 | tb_env->hdecr_timer = NULL; | |
729 | } | |
8ecc7913 | 730 | cpu_ppc_set_tb_clk(env, freq); |
9fddaa0c | 731 | |
8ecc7913 | 732 | return &cpu_ppc_set_tb_clk; |
9fddaa0c FB |
733 | } |
734 | ||
76a66253 | 735 | /* Specific helpers for POWER & PowerPC 601 RTC */ |
b1d8e52e BS |
736 | #if 0 |
737 | static clk_setup_cb cpu_ppc601_rtc_init (CPUState *env) | |
76a66253 JM |
738 | { |
739 | return cpu_ppc_tb_init(env, 7812500); | |
740 | } | |
b1d8e52e | 741 | #endif |
76a66253 JM |
742 | |
743 | void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value) | |
8a84de23 JM |
744 | { |
745 | _cpu_ppc_store_tbu(env, value); | |
746 | } | |
76a66253 JM |
747 | |
748 | uint32_t cpu_ppc601_load_rtcu (CPUState *env) | |
8a84de23 JM |
749 | { |
750 | return _cpu_ppc_load_tbu(env); | |
751 | } | |
76a66253 JM |
752 | |
753 | void cpu_ppc601_store_rtcl (CPUState *env, uint32_t value) | |
754 | { | |
755 | cpu_ppc_store_tbl(env, value & 0x3FFFFF80); | |
756 | } | |
757 | ||
758 | uint32_t cpu_ppc601_load_rtcl (CPUState *env) | |
759 | { | |
760 | return cpu_ppc_load_tbl(env) & 0x3FFFFF80; | |
761 | } | |
762 | ||
636aaad7 | 763 | /*****************************************************************************/ |
76a66253 | 764 | /* Embedded PowerPC timers */ |
636aaad7 JM |
765 | |
766 | /* PIT, FIT & WDT */ | |
c227f099 AL |
767 | typedef struct ppcemb_timer_t ppcemb_timer_t; |
768 | struct ppcemb_timer_t { | |
636aaad7 JM |
769 | uint64_t pit_reload; /* PIT auto-reload value */ |
770 | uint64_t fit_next; /* Tick for next FIT interrupt */ | |
771 | struct QEMUTimer *fit_timer; | |
772 | uint64_t wdt_next; /* Tick for next WDT interrupt */ | |
773 | struct QEMUTimer *wdt_timer; | |
d63cb48d EI |
774 | |
775 | /* 405 have the PIT, 440 have a DECR. */ | |
776 | unsigned int decr_excp; | |
636aaad7 | 777 | }; |
3b46e624 | 778 | |
636aaad7 JM |
779 | /* Fixed interval timer */ |
780 | static void cpu_4xx_fit_cb (void *opaque) | |
781 | { | |
782 | CPUState *env; | |
c227f099 AL |
783 | ppc_tb_t *tb_env; |
784 | ppcemb_timer_t *ppcemb_timer; | |
636aaad7 JM |
785 | uint64_t now, next; |
786 | ||
787 | env = opaque; | |
788 | tb_env = env->tb_env; | |
789 | ppcemb_timer = tb_env->opaque; | |
74475455 | 790 | now = qemu_get_clock_ns(vm_clock); |
636aaad7 JM |
791 | switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) { |
792 | case 0: | |
793 | next = 1 << 9; | |
794 | break; | |
795 | case 1: | |
796 | next = 1 << 13; | |
797 | break; | |
798 | case 2: | |
799 | next = 1 << 17; | |
800 | break; | |
801 | case 3: | |
802 | next = 1 << 21; | |
803 | break; | |
804 | default: | |
805 | /* Cannot occur, but makes gcc happy */ | |
806 | return; | |
807 | } | |
6ee093c9 | 808 | next = now + muldiv64(next, get_ticks_per_sec(), tb_env->tb_freq); |
636aaad7 JM |
809 | if (next == now) |
810 | next++; | |
811 | qemu_mod_timer(ppcemb_timer->fit_timer, next); | |
636aaad7 JM |
812 | env->spr[SPR_40x_TSR] |= 1 << 26; |
813 | if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) | |
814 | ppc_set_irq(env, PPC_INTERRUPT_FIT, 1); | |
90e189ec BS |
815 | LOG_TB("%s: ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__, |
816 | (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1), | |
817 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); | |
636aaad7 JM |
818 | } |
819 | ||
820 | /* Programmable interval timer */ | |
c227f099 | 821 | static void start_stop_pit (CPUState *env, ppc_tb_t *tb_env, int is_excp) |
76a66253 | 822 | { |
c227f099 | 823 | ppcemb_timer_t *ppcemb_timer; |
636aaad7 JM |
824 | uint64_t now, next; |
825 | ||
636aaad7 | 826 | ppcemb_timer = tb_env->opaque; |
4b6d0a4c JM |
827 | if (ppcemb_timer->pit_reload <= 1 || |
828 | !((env->spr[SPR_40x_TCR] >> 26) & 0x1) || | |
829 | (is_excp && !((env->spr[SPR_40x_TCR] >> 22) & 0x1))) { | |
830 | /* Stop PIT */ | |
d12d51d5 | 831 | LOG_TB("%s: stop PIT\n", __func__); |
4b6d0a4c JM |
832 | qemu_del_timer(tb_env->decr_timer); |
833 | } else { | |
d12d51d5 | 834 | LOG_TB("%s: start PIT %016" PRIx64 "\n", |
4b6d0a4c | 835 | __func__, ppcemb_timer->pit_reload); |
74475455 | 836 | now = qemu_get_clock_ns(vm_clock); |
636aaad7 | 837 | next = now + muldiv64(ppcemb_timer->pit_reload, |
6ee093c9 | 838 | get_ticks_per_sec(), tb_env->decr_freq); |
4b6d0a4c JM |
839 | if (is_excp) |
840 | next += tb_env->decr_next - now; | |
636aaad7 JM |
841 | if (next == now) |
842 | next++; | |
843 | qemu_mod_timer(tb_env->decr_timer, next); | |
844 | tb_env->decr_next = next; | |
845 | } | |
4b6d0a4c JM |
846 | } |
847 | ||
848 | static void cpu_4xx_pit_cb (void *opaque) | |
849 | { | |
850 | CPUState *env; | |
c227f099 AL |
851 | ppc_tb_t *tb_env; |
852 | ppcemb_timer_t *ppcemb_timer; | |
4b6d0a4c JM |
853 | |
854 | env = opaque; | |
855 | tb_env = env->tb_env; | |
856 | ppcemb_timer = tb_env->opaque; | |
636aaad7 JM |
857 | env->spr[SPR_40x_TSR] |= 1 << 27; |
858 | if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) | |
d63cb48d | 859 | ppc_set_irq(env, ppcemb_timer->decr_excp, 1); |
4b6d0a4c | 860 | start_stop_pit(env, tb_env, 1); |
90e189ec BS |
861 | LOG_TB("%s: ar %d ir %d TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx " " |
862 | "%016" PRIx64 "\n", __func__, | |
863 | (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1), | |
864 | (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1), | |
865 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR], | |
866 | ppcemb_timer->pit_reload); | |
636aaad7 JM |
867 | } |
868 | ||
869 | /* Watchdog timer */ | |
870 | static void cpu_4xx_wdt_cb (void *opaque) | |
871 | { | |
872 | CPUState *env; | |
c227f099 AL |
873 | ppc_tb_t *tb_env; |
874 | ppcemb_timer_t *ppcemb_timer; | |
636aaad7 JM |
875 | uint64_t now, next; |
876 | ||
877 | env = opaque; | |
878 | tb_env = env->tb_env; | |
879 | ppcemb_timer = tb_env->opaque; | |
74475455 | 880 | now = qemu_get_clock_ns(vm_clock); |
636aaad7 JM |
881 | switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) { |
882 | case 0: | |
883 | next = 1 << 17; | |
884 | break; | |
885 | case 1: | |
886 | next = 1 << 21; | |
887 | break; | |
888 | case 2: | |
889 | next = 1 << 25; | |
890 | break; | |
891 | case 3: | |
892 | next = 1 << 29; | |
893 | break; | |
894 | default: | |
895 | /* Cannot occur, but makes gcc happy */ | |
896 | return; | |
897 | } | |
6ee093c9 | 898 | next = now + muldiv64(next, get_ticks_per_sec(), tb_env->decr_freq); |
636aaad7 JM |
899 | if (next == now) |
900 | next++; | |
90e189ec BS |
901 | LOG_TB("%s: TCR " TARGET_FMT_lx " TSR " TARGET_FMT_lx "\n", __func__, |
902 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); | |
636aaad7 JM |
903 | switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) { |
904 | case 0x0: | |
905 | case 0x1: | |
906 | qemu_mod_timer(ppcemb_timer->wdt_timer, next); | |
907 | ppcemb_timer->wdt_next = next; | |
908 | env->spr[SPR_40x_TSR] |= 1 << 31; | |
909 | break; | |
910 | case 0x2: | |
911 | qemu_mod_timer(ppcemb_timer->wdt_timer, next); | |
912 | ppcemb_timer->wdt_next = next; | |
913 | env->spr[SPR_40x_TSR] |= 1 << 30; | |
914 | if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) | |
915 | ppc_set_irq(env, PPC_INTERRUPT_WDT, 1); | |
916 | break; | |
917 | case 0x3: | |
918 | env->spr[SPR_40x_TSR] &= ~0x30000000; | |
919 | env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000; | |
920 | switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) { | |
921 | case 0x0: | |
922 | /* No reset */ | |
923 | break; | |
924 | case 0x1: /* Core reset */ | |
8ecc7913 JM |
925 | ppc40x_core_reset(env); |
926 | break; | |
636aaad7 | 927 | case 0x2: /* Chip reset */ |
8ecc7913 JM |
928 | ppc40x_chip_reset(env); |
929 | break; | |
636aaad7 | 930 | case 0x3: /* System reset */ |
8ecc7913 JM |
931 | ppc40x_system_reset(env); |
932 | break; | |
636aaad7 JM |
933 | } |
934 | } | |
76a66253 JM |
935 | } |
936 | ||
937 | void store_40x_pit (CPUState *env, target_ulong val) | |
938 | { | |
c227f099 AL |
939 | ppc_tb_t *tb_env; |
940 | ppcemb_timer_t *ppcemb_timer; | |
636aaad7 JM |
941 | |
942 | tb_env = env->tb_env; | |
943 | ppcemb_timer = tb_env->opaque; | |
90e189ec | 944 | LOG_TB("%s val" TARGET_FMT_lx "\n", __func__, val); |
636aaad7 | 945 | ppcemb_timer->pit_reload = val; |
4b6d0a4c | 946 | start_stop_pit(env, tb_env, 0); |
76a66253 JM |
947 | } |
948 | ||
636aaad7 | 949 | target_ulong load_40x_pit (CPUState *env) |
76a66253 | 950 | { |
636aaad7 | 951 | return cpu_ppc_load_decr(env); |
76a66253 JM |
952 | } |
953 | ||
954 | void store_booke_tsr (CPUState *env, target_ulong val) | |
955 | { | |
d63cb48d EI |
956 | ppc_tb_t *tb_env = env->tb_env; |
957 | ppcemb_timer_t *ppcemb_timer; | |
958 | ||
959 | ppcemb_timer = tb_env->opaque; | |
960 | ||
90e189ec | 961 | LOG_TB("%s: val " TARGET_FMT_lx "\n", __func__, val); |
4b6d0a4c JM |
962 | env->spr[SPR_40x_TSR] &= ~(val & 0xFC000000); |
963 | if (val & 0x80000000) | |
d63cb48d | 964 | ppc_set_irq(env, ppcemb_timer->decr_excp, 0); |
636aaad7 JM |
965 | } |
966 | ||
967 | void store_booke_tcr (CPUState *env, target_ulong val) | |
968 | { | |
c227f099 | 969 | ppc_tb_t *tb_env; |
4b6d0a4c JM |
970 | |
971 | tb_env = env->tb_env; | |
90e189ec | 972 | LOG_TB("%s: val " TARGET_FMT_lx "\n", __func__, val); |
4b6d0a4c JM |
973 | env->spr[SPR_40x_TCR] = val & 0xFFC00000; |
974 | start_stop_pit(env, tb_env, 1); | |
8ecc7913 | 975 | cpu_4xx_wdt_cb(env); |
636aaad7 JM |
976 | } |
977 | ||
4b6d0a4c JM |
978 | static void ppc_emb_set_tb_clk (void *opaque, uint32_t freq) |
979 | { | |
980 | CPUState *env = opaque; | |
c227f099 | 981 | ppc_tb_t *tb_env = env->tb_env; |
4b6d0a4c | 982 | |
d12d51d5 | 983 | LOG_TB("%s set new frequency to %" PRIu32 "\n", __func__, |
aae9366a | 984 | freq); |
4b6d0a4c | 985 | tb_env->tb_freq = freq; |
dbdd2506 | 986 | tb_env->decr_freq = freq; |
4b6d0a4c JM |
987 | /* XXX: we should also update all timers */ |
988 | } | |
989 | ||
d63cb48d EI |
990 | clk_setup_cb ppc_emb_timers_init (CPUState *env, uint32_t freq, |
991 | unsigned int decr_excp) | |
636aaad7 | 992 | { |
c227f099 AL |
993 | ppc_tb_t *tb_env; |
994 | ppcemb_timer_t *ppcemb_timer; | |
636aaad7 | 995 | |
c227f099 | 996 | tb_env = qemu_mallocz(sizeof(ppc_tb_t)); |
8ecc7913 | 997 | env->tb_env = tb_env; |
c227f099 | 998 | ppcemb_timer = qemu_mallocz(sizeof(ppcemb_timer_t)); |
8ecc7913 | 999 | tb_env->tb_freq = freq; |
dbdd2506 | 1000 | tb_env->decr_freq = freq; |
636aaad7 | 1001 | tb_env->opaque = ppcemb_timer; |
d12d51d5 | 1002 | LOG_TB("%s freq %" PRIu32 "\n", __func__, freq); |
636aaad7 JM |
1003 | if (ppcemb_timer != NULL) { |
1004 | /* We use decr timer for PIT */ | |
74475455 | 1005 | tb_env->decr_timer = qemu_new_timer_ns(vm_clock, &cpu_4xx_pit_cb, env); |
636aaad7 | 1006 | ppcemb_timer->fit_timer = |
74475455 | 1007 | qemu_new_timer_ns(vm_clock, &cpu_4xx_fit_cb, env); |
636aaad7 | 1008 | ppcemb_timer->wdt_timer = |
74475455 | 1009 | qemu_new_timer_ns(vm_clock, &cpu_4xx_wdt_cb, env); |
d63cb48d | 1010 | ppcemb_timer->decr_excp = decr_excp; |
636aaad7 | 1011 | } |
8ecc7913 | 1012 | |
4b6d0a4c | 1013 | return &ppc_emb_set_tb_clk; |
76a66253 JM |
1014 | } |
1015 | ||
2e719ba3 JM |
1016 | /*****************************************************************************/ |
1017 | /* Embedded PowerPC Device Control Registers */ | |
c227f099 AL |
1018 | typedef struct ppc_dcrn_t ppc_dcrn_t; |
1019 | struct ppc_dcrn_t { | |
2e719ba3 JM |
1020 | dcr_read_cb dcr_read; |
1021 | dcr_write_cb dcr_write; | |
1022 | void *opaque; | |
1023 | }; | |
1024 | ||
a750fc0b JM |
1025 | /* XXX: on 460, DCR addresses are 32 bits wide, |
1026 | * using DCRIPR to get the 22 upper bits of the DCR address | |
1027 | */ | |
2e719ba3 | 1028 | #define DCRN_NB 1024 |
c227f099 AL |
1029 | struct ppc_dcr_t { |
1030 | ppc_dcrn_t dcrn[DCRN_NB]; | |
2e719ba3 JM |
1031 | int (*read_error)(int dcrn); |
1032 | int (*write_error)(int dcrn); | |
1033 | }; | |
1034 | ||
73b01960 | 1035 | int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, uint32_t *valp) |
2e719ba3 | 1036 | { |
c227f099 | 1037 | ppc_dcrn_t *dcr; |
2e719ba3 JM |
1038 | |
1039 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
1040 | goto error; | |
1041 | dcr = &dcr_env->dcrn[dcrn]; | |
1042 | if (dcr->dcr_read == NULL) | |
1043 | goto error; | |
1044 | *valp = (*dcr->dcr_read)(dcr->opaque, dcrn); | |
1045 | ||
1046 | return 0; | |
1047 | ||
1048 | error: | |
1049 | if (dcr_env->read_error != NULL) | |
1050 | return (*dcr_env->read_error)(dcrn); | |
1051 | ||
1052 | return -1; | |
1053 | } | |
1054 | ||
73b01960 | 1055 | int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, uint32_t val) |
2e719ba3 | 1056 | { |
c227f099 | 1057 | ppc_dcrn_t *dcr; |
2e719ba3 JM |
1058 | |
1059 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
1060 | goto error; | |
1061 | dcr = &dcr_env->dcrn[dcrn]; | |
1062 | if (dcr->dcr_write == NULL) | |
1063 | goto error; | |
1064 | (*dcr->dcr_write)(dcr->opaque, dcrn, val); | |
1065 | ||
1066 | return 0; | |
1067 | ||
1068 | error: | |
1069 | if (dcr_env->write_error != NULL) | |
1070 | return (*dcr_env->write_error)(dcrn); | |
1071 | ||
1072 | return -1; | |
1073 | } | |
1074 | ||
1075 | int ppc_dcr_register (CPUState *env, int dcrn, void *opaque, | |
1076 | dcr_read_cb dcr_read, dcr_write_cb dcr_write) | |
1077 | { | |
c227f099 AL |
1078 | ppc_dcr_t *dcr_env; |
1079 | ppc_dcrn_t *dcr; | |
2e719ba3 JM |
1080 | |
1081 | dcr_env = env->dcr_env; | |
1082 | if (dcr_env == NULL) | |
1083 | return -1; | |
1084 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
1085 | return -1; | |
1086 | dcr = &dcr_env->dcrn[dcrn]; | |
1087 | if (dcr->opaque != NULL || | |
1088 | dcr->dcr_read != NULL || | |
1089 | dcr->dcr_write != NULL) | |
1090 | return -1; | |
1091 | dcr->opaque = opaque; | |
1092 | dcr->dcr_read = dcr_read; | |
1093 | dcr->dcr_write = dcr_write; | |
1094 | ||
1095 | return 0; | |
1096 | } | |
1097 | ||
1098 | int ppc_dcr_init (CPUState *env, int (*read_error)(int dcrn), | |
1099 | int (*write_error)(int dcrn)) | |
1100 | { | |
c227f099 | 1101 | ppc_dcr_t *dcr_env; |
2e719ba3 | 1102 | |
c227f099 | 1103 | dcr_env = qemu_mallocz(sizeof(ppc_dcr_t)); |
2e719ba3 JM |
1104 | dcr_env->read_error = read_error; |
1105 | dcr_env->write_error = write_error; | |
1106 | env->dcr_env = dcr_env; | |
1107 | ||
1108 | return 0; | |
1109 | } | |
1110 | ||
64201201 FB |
1111 | /*****************************************************************************/ |
1112 | /* Debug port */ | |
fd0bbb12 | 1113 | void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val) |
64201201 FB |
1114 | { |
1115 | addr &= 0xF; | |
1116 | switch (addr) { | |
1117 | case 0: | |
1118 | printf("%c", val); | |
1119 | break; | |
1120 | case 1: | |
1121 | printf("\n"); | |
1122 | fflush(stdout); | |
1123 | break; | |
1124 | case 2: | |
aae9366a | 1125 | printf("Set loglevel to %04" PRIx32 "\n", val); |
fd0bbb12 | 1126 | cpu_set_log(val | 0x100); |
64201201 FB |
1127 | break; |
1128 | } | |
1129 | } | |
1130 | ||
1131 | /*****************************************************************************/ | |
1132 | /* NVRAM helpers */ | |
c227f099 | 1133 | static inline uint32_t nvram_read (nvram_t *nvram, uint32_t addr) |
64201201 | 1134 | { |
3cbee15b | 1135 | return (*nvram->read_fn)(nvram->opaque, addr);; |
64201201 FB |
1136 | } |
1137 | ||
c227f099 | 1138 | static inline void nvram_write (nvram_t *nvram, uint32_t addr, uint32_t val) |
64201201 | 1139 | { |
3cbee15b | 1140 | (*nvram->write_fn)(nvram->opaque, addr, val); |
64201201 FB |
1141 | } |
1142 | ||
c227f099 | 1143 | void NVRAM_set_byte (nvram_t *nvram, uint32_t addr, uint8_t value) |
64201201 | 1144 | { |
3cbee15b | 1145 | nvram_write(nvram, addr, value); |
64201201 FB |
1146 | } |
1147 | ||
c227f099 | 1148 | uint8_t NVRAM_get_byte (nvram_t *nvram, uint32_t addr) |
3cbee15b JM |
1149 | { |
1150 | return nvram_read(nvram, addr); | |
1151 | } | |
1152 | ||
c227f099 | 1153 | void NVRAM_set_word (nvram_t *nvram, uint32_t addr, uint16_t value) |
3cbee15b JM |
1154 | { |
1155 | nvram_write(nvram, addr, value >> 8); | |
1156 | nvram_write(nvram, addr + 1, value & 0xFF); | |
1157 | } | |
1158 | ||
c227f099 | 1159 | uint16_t NVRAM_get_word (nvram_t *nvram, uint32_t addr) |
64201201 FB |
1160 | { |
1161 | uint16_t tmp; | |
1162 | ||
3cbee15b JM |
1163 | tmp = nvram_read(nvram, addr) << 8; |
1164 | tmp |= nvram_read(nvram, addr + 1); | |
1165 | ||
64201201 FB |
1166 | return tmp; |
1167 | } | |
1168 | ||
c227f099 | 1169 | void NVRAM_set_lword (nvram_t *nvram, uint32_t addr, uint32_t value) |
64201201 | 1170 | { |
3cbee15b JM |
1171 | nvram_write(nvram, addr, value >> 24); |
1172 | nvram_write(nvram, addr + 1, (value >> 16) & 0xFF); | |
1173 | nvram_write(nvram, addr + 2, (value >> 8) & 0xFF); | |
1174 | nvram_write(nvram, addr + 3, value & 0xFF); | |
64201201 FB |
1175 | } |
1176 | ||
c227f099 | 1177 | uint32_t NVRAM_get_lword (nvram_t *nvram, uint32_t addr) |
64201201 FB |
1178 | { |
1179 | uint32_t tmp; | |
1180 | ||
3cbee15b JM |
1181 | tmp = nvram_read(nvram, addr) << 24; |
1182 | tmp |= nvram_read(nvram, addr + 1) << 16; | |
1183 | tmp |= nvram_read(nvram, addr + 2) << 8; | |
1184 | tmp |= nvram_read(nvram, addr + 3); | |
76a66253 | 1185 | |
64201201 FB |
1186 | return tmp; |
1187 | } | |
1188 | ||
c227f099 | 1189 | void NVRAM_set_string (nvram_t *nvram, uint32_t addr, |
b55266b5 | 1190 | const char *str, uint32_t max) |
64201201 FB |
1191 | { |
1192 | int i; | |
1193 | ||
1194 | for (i = 0; i < max && str[i] != '\0'; i++) { | |
3cbee15b | 1195 | nvram_write(nvram, addr + i, str[i]); |
64201201 | 1196 | } |
3cbee15b JM |
1197 | nvram_write(nvram, addr + i, str[i]); |
1198 | nvram_write(nvram, addr + max - 1, '\0'); | |
64201201 FB |
1199 | } |
1200 | ||
c227f099 | 1201 | int NVRAM_get_string (nvram_t *nvram, uint8_t *dst, uint16_t addr, int max) |
64201201 FB |
1202 | { |
1203 | int i; | |
1204 | ||
1205 | memset(dst, 0, max); | |
1206 | for (i = 0; i < max; i++) { | |
1207 | dst[i] = NVRAM_get_byte(nvram, addr + i); | |
1208 | if (dst[i] == '\0') | |
1209 | break; | |
1210 | } | |
1211 | ||
1212 | return i; | |
1213 | } | |
1214 | ||
1215 | static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value) | |
1216 | { | |
1217 | uint16_t tmp; | |
1218 | uint16_t pd, pd1, pd2; | |
1219 | ||
1220 | tmp = prev >> 8; | |
1221 | pd = prev ^ value; | |
1222 | pd1 = pd & 0x000F; | |
1223 | pd2 = ((pd >> 4) & 0x000F) ^ pd1; | |
1224 | tmp ^= (pd1 << 3) | (pd1 << 8); | |
1225 | tmp ^= pd2 | (pd2 << 7) | (pd2 << 12); | |
1226 | ||
1227 | return tmp; | |
1228 | } | |
1229 | ||
c227f099 | 1230 | static uint16_t NVRAM_compute_crc (nvram_t *nvram, uint32_t start, uint32_t count) |
64201201 FB |
1231 | { |
1232 | uint32_t i; | |
1233 | uint16_t crc = 0xFFFF; | |
1234 | int odd; | |
1235 | ||
1236 | odd = count & 1; | |
1237 | count &= ~1; | |
1238 | for (i = 0; i != count; i++) { | |
76a66253 | 1239 | crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i)); |
64201201 FB |
1240 | } |
1241 | if (odd) { | |
76a66253 | 1242 | crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8); |
64201201 FB |
1243 | } |
1244 | ||
1245 | return crc; | |
1246 | } | |
1247 | ||
fd0bbb12 FB |
1248 | #define CMDLINE_ADDR 0x017ff000 |
1249 | ||
c227f099 | 1250 | int PPC_NVRAM_set_params (nvram_t *nvram, uint16_t NVRAM_size, |
b55266b5 | 1251 | const char *arch, |
64201201 FB |
1252 | uint32_t RAM_size, int boot_device, |
1253 | uint32_t kernel_image, uint32_t kernel_size, | |
fd0bbb12 | 1254 | const char *cmdline, |
64201201 | 1255 | uint32_t initrd_image, uint32_t initrd_size, |
fd0bbb12 FB |
1256 | uint32_t NVRAM_image, |
1257 | int width, int height, int depth) | |
64201201 FB |
1258 | { |
1259 | uint16_t crc; | |
1260 | ||
1261 | /* Set parameters for Open Hack'Ware BIOS */ | |
1262 | NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16); | |
1263 | NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */ | |
1264 | NVRAM_set_word(nvram, 0x14, NVRAM_size); | |
1265 | NVRAM_set_string(nvram, 0x20, arch, 16); | |
1266 | NVRAM_set_lword(nvram, 0x30, RAM_size); | |
1267 | NVRAM_set_byte(nvram, 0x34, boot_device); | |
1268 | NVRAM_set_lword(nvram, 0x38, kernel_image); | |
1269 | NVRAM_set_lword(nvram, 0x3C, kernel_size); | |
fd0bbb12 FB |
1270 | if (cmdline) { |
1271 | /* XXX: put the cmdline in NVRAM too ? */ | |
3c178e72 | 1272 | pstrcpy_targphys("cmdline", CMDLINE_ADDR, RAM_size - CMDLINE_ADDR, cmdline); |
fd0bbb12 FB |
1273 | NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR); |
1274 | NVRAM_set_lword(nvram, 0x44, strlen(cmdline)); | |
1275 | } else { | |
1276 | NVRAM_set_lword(nvram, 0x40, 0); | |
1277 | NVRAM_set_lword(nvram, 0x44, 0); | |
1278 | } | |
64201201 FB |
1279 | NVRAM_set_lword(nvram, 0x48, initrd_image); |
1280 | NVRAM_set_lword(nvram, 0x4C, initrd_size); | |
1281 | NVRAM_set_lword(nvram, 0x50, NVRAM_image); | |
fd0bbb12 FB |
1282 | |
1283 | NVRAM_set_word(nvram, 0x54, width); | |
1284 | NVRAM_set_word(nvram, 0x56, height); | |
1285 | NVRAM_set_word(nvram, 0x58, depth); | |
1286 | crc = NVRAM_compute_crc(nvram, 0x00, 0xF8); | |
3cbee15b | 1287 | NVRAM_set_word(nvram, 0xFC, crc); |
64201201 FB |
1288 | |
1289 | return 0; | |
a541f297 | 1290 | } |