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