]>
Commit | Line | Data |
---|---|---|
a541f297 | 1 | /* |
e9df014c | 2 | * QEMU generic PowerPC hardware System Emulator |
a541f297 | 3 | * |
76a66253 | 4 | * Copyright (c) 2003-2007 Jocelyn Mayer |
a541f297 FB |
5 | * |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
a541f297 | 24 | #include "vl.h" |
fd0bbb12 | 25 | #include "m48t59.h" |
a541f297 | 26 | |
e9df014c JM |
27 | //#define PPC_DEBUG_IRQ |
28 | ||
47103572 JM |
29 | extern FILE *logfile; |
30 | extern int loglevel; | |
31 | ||
e9df014c | 32 | void ppc_set_irq (CPUState *env, int n_IRQ, int level) |
47103572 | 33 | { |
47103572 JM |
34 | if (level) { |
35 | env->pending_interrupts |= 1 << n_IRQ; | |
36 | cpu_interrupt(env, CPU_INTERRUPT_HARD); | |
37 | } else { | |
38 | env->pending_interrupts &= ~(1 << n_IRQ); | |
39 | if (env->pending_interrupts == 0) | |
40 | cpu_reset_interrupt(env, CPU_INTERRUPT_HARD); | |
41 | } | |
e9df014c | 42 | #if defined(PPC_DEBUG_IRQ) |
a496775f JM |
43 | if (loglevel & CPU_LOG_INT) { |
44 | fprintf(logfile, "%s: %p n_IRQ %d level %d => pending %08x req %08x\n", | |
45 | __func__, env, n_IRQ, level, | |
46 | env->pending_interrupts, env->interrupt_request); | |
47 | } | |
47103572 JM |
48 | #endif |
49 | } | |
50 | ||
e9df014c JM |
51 | /* PowerPC 6xx / 7xx internal IRQ controller */ |
52 | static void ppc6xx_set_irq (void *opaque, int pin, int level) | |
d537cf6c | 53 | { |
e9df014c JM |
54 | CPUState *env = opaque; |
55 | int cur_level; | |
d537cf6c | 56 | |
e9df014c | 57 | #if defined(PPC_DEBUG_IRQ) |
a496775f JM |
58 | if (loglevel & CPU_LOG_INT) { |
59 | fprintf(logfile, "%s: env %p pin %d level %d\n", __func__, | |
60 | env, pin, level); | |
61 | } | |
e9df014c JM |
62 | #endif |
63 | cur_level = (env->irq_input_state >> pin) & 1; | |
64 | /* Don't generate spurious events */ | |
24be5ae3 | 65 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { |
e9df014c | 66 | switch (pin) { |
24be5ae3 JM |
67 | case PPC6xx_INPUT_INT: |
68 | /* Level sensitive - active high */ | |
e9df014c | 69 | #if defined(PPC_DEBUG_IRQ) |
a496775f JM |
70 | if (loglevel & CPU_LOG_INT) { |
71 | fprintf(logfile, "%s: set the external IRQ state to %d\n", | |
72 | __func__, level); | |
73 | } | |
e9df014c JM |
74 | #endif |
75 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); | |
76 | break; | |
24be5ae3 | 77 | case PPC6xx_INPUT_SMI: |
e9df014c JM |
78 | /* Level sensitive - active high */ |
79 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
80 | if (loglevel & CPU_LOG_INT) { |
81 | fprintf(logfile, "%s: set the SMI IRQ state to %d\n", | |
82 | __func__, level); | |
83 | } | |
e9df014c JM |
84 | #endif |
85 | ppc_set_irq(env, PPC_INTERRUPT_SMI, level); | |
86 | break; | |
24be5ae3 | 87 | case PPC6xx_INPUT_MCP: |
e9df014c JM |
88 | /* Negative edge sensitive */ |
89 | /* XXX: TODO: actual reaction may depends on HID0 status | |
90 | * 603/604/740/750: check HID0[EMCP] | |
91 | */ | |
92 | if (cur_level == 1 && level == 0) { | |
93 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
94 | if (loglevel & CPU_LOG_INT) { |
95 | fprintf(logfile, "%s: raise machine check state\n", | |
96 | __func__); | |
97 | } | |
e9df014c JM |
98 | #endif |
99 | ppc_set_irq(env, PPC_INTERRUPT_MCK, 1); | |
100 | } | |
101 | break; | |
24be5ae3 | 102 | case PPC6xx_INPUT_CKSTP_IN: |
e9df014c JM |
103 | /* Level sensitive - active low */ |
104 | /* XXX: TODO: relay the signal to CKSTP_OUT pin */ | |
105 | if (level) { | |
106 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
107 | if (loglevel & CPU_LOG_INT) { |
108 | fprintf(logfile, "%s: stop the CPU\n", __func__); | |
109 | } | |
e9df014c JM |
110 | #endif |
111 | env->halted = 1; | |
112 | } else { | |
113 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
114 | if (loglevel & CPU_LOG_INT) { |
115 | fprintf(logfile, "%s: restart the CPU\n", __func__); | |
116 | } | |
e9df014c JM |
117 | #endif |
118 | env->halted = 0; | |
119 | } | |
120 | break; | |
24be5ae3 | 121 | case PPC6xx_INPUT_HRESET: |
e9df014c JM |
122 | /* Level sensitive - active low */ |
123 | if (level) { | |
124 | #if 0 // XXX: TOFIX | |
125 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
126 | if (loglevel & CPU_LOG_INT) { |
127 | fprintf(logfile, "%s: reset the CPU\n", __func__); | |
128 | } | |
e9df014c JM |
129 | #endif |
130 | cpu_reset(env); | |
131 | #endif | |
132 | } | |
133 | break; | |
24be5ae3 | 134 | case PPC6xx_INPUT_SRESET: |
e9df014c | 135 | #if defined(PPC_DEBUG_IRQ) |
a496775f JM |
136 | if (loglevel & CPU_LOG_INT) { |
137 | fprintf(logfile, "%s: set the RESET IRQ state to %d\n", | |
138 | __func__, level); | |
139 | } | |
e9df014c JM |
140 | #endif |
141 | ppc_set_irq(env, PPC_INTERRUPT_RESET, level); | |
142 | break; | |
143 | default: | |
144 | /* Unknown pin - do nothing */ | |
145 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
146 | if (loglevel & CPU_LOG_INT) { |
147 | fprintf(logfile, "%s: unknown IRQ pin %d\n", __func__, pin); | |
148 | } | |
e9df014c JM |
149 | #endif |
150 | return; | |
151 | } | |
152 | if (level) | |
153 | env->irq_input_state |= 1 << pin; | |
154 | else | |
155 | env->irq_input_state &= ~(1 << pin); | |
d537cf6c PB |
156 | } |
157 | } | |
158 | ||
e9df014c | 159 | void ppc6xx_irq_init (CPUState *env) |
47103572 | 160 | { |
e9df014c | 161 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppc6xx_set_irq, env, 6); |
47103572 JM |
162 | } |
163 | ||
24be5ae3 JM |
164 | /* PowerPC 405 internal IRQ controller */ |
165 | static void ppc405_set_irq (void *opaque, int pin, int level) | |
166 | { | |
167 | CPUState *env = opaque; | |
168 | int cur_level; | |
169 | ||
170 | #if defined(PPC_DEBUG_IRQ) | |
171 | printf("%s: env %p pin %d level %d\n", __func__, env, pin, level); | |
172 | #endif | |
173 | cur_level = (env->irq_input_state >> pin) & 1; | |
174 | /* Don't generate spurious events */ | |
175 | if ((cur_level == 1 && level == 0) || (cur_level == 0 && level != 0)) { | |
176 | switch (pin) { | |
177 | case PPC405_INPUT_RESET_SYS: | |
178 | /* XXX: TODO: reset all peripherals */ | |
179 | /* No break here */ | |
180 | case PPC405_INPUT_RESET_CHIP: | |
181 | /* XXX: TODO: reset on-chip peripherals */ | |
182 | /* No break here */ | |
183 | case PPC405_INPUT_RESET_CORE: | |
184 | /* XXX: TODO: update DBSR[MRR] */ | |
185 | if (level) { | |
186 | #if 0 // XXX: TOFIX | |
187 | #if defined(PPC_DEBUG_IRQ) | |
188 | printf("%s: reset the CPU\n", __func__); | |
189 | #endif | |
190 | cpu_reset(env); | |
191 | #endif | |
192 | } | |
193 | break; | |
194 | case PPC405_INPUT_CINT: | |
195 | /* Level sensitive - active high */ | |
196 | #if defined(PPC_DEBUG_IRQ) | |
197 | printf("%s: set the critical IRQ state to %d\n", __func__, level); | |
198 | #endif | |
199 | /* XXX: TOFIX */ | |
200 | ppc_set_irq(env, PPC_INTERRUPT_RESET, level); | |
201 | break; | |
202 | case PPC405_INPUT_INT: | |
203 | /* Level sensitive - active high */ | |
204 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
205 | if (loglevel & CPU_LOG_INT) { |
206 | fprintf(logfile, "%s: set the external IRQ state to %d\n", | |
207 | __func__, level); | |
208 | } | |
24be5ae3 JM |
209 | #endif |
210 | ppc_set_irq(env, PPC_INTERRUPT_EXT, level); | |
211 | break; | |
212 | case PPC405_INPUT_HALT: | |
213 | /* Level sensitive - active low */ | |
214 | if (level) { | |
215 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
216 | if (loglevel & CPU_LOG_INT) { |
217 | fprintf(logfile, "%s: stop the CPU\n", __func__); | |
218 | } | |
24be5ae3 JM |
219 | #endif |
220 | env->halted = 1; | |
221 | } else { | |
222 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
223 | if (loglevel & CPU_LOG_INT) { |
224 | fprintf(logfile, "%s: restart the CPU\n", __func__); | |
225 | } | |
24be5ae3 JM |
226 | #endif |
227 | env->halted = 0; | |
228 | } | |
229 | break; | |
230 | case PPC405_INPUT_DEBUG: | |
231 | /* Level sensitive - active high */ | |
232 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
233 | if (loglevel & CPU_LOG_INT) { |
234 | fprintf(logfile, "%s: set the external IRQ state to %d\n", | |
235 | __func__, level); | |
236 | } | |
24be5ae3 JM |
237 | #endif |
238 | ppc_set_irq(env, EXCP_40x_DEBUG, level); | |
239 | break; | |
240 | default: | |
241 | /* Unknown pin - do nothing */ | |
242 | #if defined(PPC_DEBUG_IRQ) | |
a496775f JM |
243 | if (loglevel & CPU_LOG_INT) { |
244 | fprintf(logfile, "%s: unknown IRQ pin %d\n", __func__, pin); | |
245 | } | |
24be5ae3 JM |
246 | #endif |
247 | return; | |
248 | } | |
249 | if (level) | |
250 | env->irq_input_state |= 1 << pin; | |
251 | else | |
252 | env->irq_input_state &= ~(1 << pin); | |
253 | } | |
254 | } | |
255 | ||
256 | void ppc405_irq_init (CPUState *env) | |
257 | { | |
24be5ae3 JM |
258 | env->irq_inputs = (void **)qemu_allocate_irqs(&ppc405_set_irq, env, 7); |
259 | } | |
260 | ||
9fddaa0c | 261 | /*****************************************************************************/ |
e9df014c | 262 | /* PowerPC time base and decrementer emulation */ |
9fddaa0c FB |
263 | //#define DEBUG_TB |
264 | ||
265 | struct ppc_tb_t { | |
266 | /* Time base management */ | |
267 | int64_t tb_offset; /* Compensation */ | |
268 | uint32_t tb_freq; /* TB frequency */ | |
269 | /* Decrementer management */ | |
270 | uint64_t decr_next; /* Tick for next decr interrupt */ | |
271 | struct QEMUTimer *decr_timer; | |
47103572 | 272 | void *opaque; |
9fddaa0c FB |
273 | }; |
274 | ||
275 | static inline uint64_t cpu_ppc_get_tb (ppc_tb_t *tb_env) | |
276 | { | |
277 | /* TB time in tb periods */ | |
278 | return muldiv64(qemu_get_clock(vm_clock) + tb_env->tb_offset, | |
76a66253 | 279 | tb_env->tb_freq, ticks_per_sec); |
9fddaa0c FB |
280 | } |
281 | ||
282 | uint32_t cpu_ppc_load_tbl (CPUState *env) | |
283 | { | |
284 | ppc_tb_t *tb_env = env->tb_env; | |
285 | uint64_t tb; | |
286 | ||
287 | tb = cpu_ppc_get_tb(tb_env); | |
288 | #ifdef DEBUG_TB | |
289 | { | |
76a66253 JM |
290 | static int last_time; |
291 | int now; | |
292 | now = time(NULL); | |
293 | if (last_time != now) { | |
294 | last_time = now; | |
a496775f JM |
295 | if (loglevel) { |
296 | fprintf(logfile, "%s: tb=0x%016lx %d %08lx\n", | |
297 | __func__, tb, now, tb_env->tb_offset); | |
298 | } | |
76a66253 | 299 | } |
9fddaa0c FB |
300 | } |
301 | #endif | |
302 | ||
303 | return tb & 0xFFFFFFFF; | |
304 | } | |
305 | ||
306 | uint32_t cpu_ppc_load_tbu (CPUState *env) | |
307 | { | |
308 | ppc_tb_t *tb_env = env->tb_env; | |
309 | uint64_t tb; | |
310 | ||
311 | tb = cpu_ppc_get_tb(tb_env); | |
312 | #ifdef DEBUG_TB | |
a496775f JM |
313 | if (loglevel) { |
314 | fprintf(logfile, "%s: tb=0x%016lx\n", __func__, tb); | |
315 | } | |
9fddaa0c | 316 | #endif |
76a66253 | 317 | |
9fddaa0c FB |
318 | return tb >> 32; |
319 | } | |
320 | ||
321 | static void cpu_ppc_store_tb (ppc_tb_t *tb_env, uint64_t value) | |
322 | { | |
323 | tb_env->tb_offset = muldiv64(value, ticks_per_sec, tb_env->tb_freq) | |
324 | - qemu_get_clock(vm_clock); | |
325 | #ifdef DEBUG_TB | |
a496775f JM |
326 | if (loglevel) { |
327 | fprintf(logfile, "%s: tb=0x%016lx offset=%08x\n", __func__, value); | |
328 | } | |
9fddaa0c FB |
329 | #endif |
330 | } | |
331 | ||
332 | void cpu_ppc_store_tbu (CPUState *env, uint32_t value) | |
333 | { | |
334 | ppc_tb_t *tb_env = env->tb_env; | |
335 | ||
336 | cpu_ppc_store_tb(tb_env, | |
337 | ((uint64_t)value << 32) | cpu_ppc_load_tbl(env)); | |
338 | } | |
339 | ||
340 | void cpu_ppc_store_tbl (CPUState *env, uint32_t value) | |
341 | { | |
342 | ppc_tb_t *tb_env = env->tb_env; | |
343 | ||
344 | cpu_ppc_store_tb(tb_env, | |
345 | ((uint64_t)cpu_ppc_load_tbu(env) << 32) | value); | |
346 | } | |
347 | ||
348 | uint32_t cpu_ppc_load_decr (CPUState *env) | |
349 | { | |
350 | ppc_tb_t *tb_env = env->tb_env; | |
351 | uint32_t decr; | |
4e588a4d | 352 | int64_t diff; |
9fddaa0c | 353 | |
4e588a4d FB |
354 | diff = tb_env->decr_next - qemu_get_clock(vm_clock); |
355 | if (diff >= 0) | |
356 | decr = muldiv64(diff, tb_env->tb_freq, ticks_per_sec); | |
357 | else | |
358 | decr = -muldiv64(-diff, tb_env->tb_freq, ticks_per_sec); | |
fd0bbb12 | 359 | #if defined(DEBUG_TB) |
a496775f JM |
360 | if (loglevel) { |
361 | fprintf(logfile, "%s: 0x%08x\n", __func__, decr); | |
362 | } | |
9fddaa0c | 363 | #endif |
76a66253 | 364 | |
9fddaa0c FB |
365 | return decr; |
366 | } | |
367 | ||
368 | /* When decrementer expires, | |
369 | * all we need to do is generate or queue a CPU exception | |
370 | */ | |
371 | static inline void cpu_ppc_decr_excp (CPUState *env) | |
372 | { | |
373 | /* Raise it */ | |
374 | #ifdef DEBUG_TB | |
a496775f JM |
375 | if (loglevel) { |
376 | fprintf(logfile, "raise decrementer exception\n"); | |
377 | } | |
9fddaa0c | 378 | #endif |
47103572 | 379 | ppc_set_irq(env, PPC_INTERRUPT_DECR, 1); |
9fddaa0c FB |
380 | } |
381 | ||
382 | static void _cpu_ppc_store_decr (CPUState *env, uint32_t decr, | |
383 | uint32_t value, int is_excp) | |
384 | { | |
385 | ppc_tb_t *tb_env = env->tb_env; | |
386 | uint64_t now, next; | |
387 | ||
388 | #ifdef DEBUG_TB | |
a496775f JM |
389 | if (loglevel) { |
390 | fprintf(logfile, "%s: 0x%08x => 0x%08x\n", __func__, decr, value); | |
391 | } | |
9fddaa0c FB |
392 | #endif |
393 | now = qemu_get_clock(vm_clock); | |
394 | next = now + muldiv64(value, ticks_per_sec, tb_env->tb_freq); | |
395 | if (is_excp) | |
396 | next += tb_env->decr_next - now; | |
397 | if (next == now) | |
76a66253 | 398 | next++; |
9fddaa0c FB |
399 | tb_env->decr_next = next; |
400 | /* Adjust timer */ | |
401 | qemu_mod_timer(tb_env->decr_timer, next); | |
402 | /* If we set a negative value and the decrementer was positive, | |
403 | * raise an exception. | |
404 | */ | |
405 | if ((value & 0x80000000) && !(decr & 0x80000000)) | |
76a66253 | 406 | cpu_ppc_decr_excp(env); |
9fddaa0c FB |
407 | } |
408 | ||
409 | void cpu_ppc_store_decr (CPUState *env, uint32_t value) | |
410 | { | |
411 | _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0); | |
412 | } | |
413 | ||
414 | static void cpu_ppc_decr_cb (void *opaque) | |
415 | { | |
416 | _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1); | |
417 | } | |
418 | ||
419 | /* Set up (once) timebase frequency (in Hz) */ | |
420 | ppc_tb_t *cpu_ppc_tb_init (CPUState *env, uint32_t freq) | |
421 | { | |
422 | ppc_tb_t *tb_env; | |
423 | ||
424 | tb_env = qemu_mallocz(sizeof(ppc_tb_t)); | |
425 | if (tb_env == NULL) | |
426 | return NULL; | |
427 | env->tb_env = tb_env; | |
428 | if (tb_env->tb_freq == 0 || 1) { | |
76a66253 JM |
429 | tb_env->tb_freq = freq; |
430 | /* Create new timer */ | |
431 | tb_env->decr_timer = | |
9fddaa0c | 432 | qemu_new_timer(vm_clock, &cpu_ppc_decr_cb, env); |
76a66253 JM |
433 | /* There is a bug in Linux 2.4 kernels: |
434 | * if a decrementer exception is pending when it enables msr_ee, | |
435 | * it's not ready to handle it... | |
436 | */ | |
437 | _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0); | |
9fddaa0c FB |
438 | } |
439 | ||
440 | return tb_env; | |
441 | } | |
442 | ||
76a66253 JM |
443 | /* Specific helpers for POWER & PowerPC 601 RTC */ |
444 | ppc_tb_t *cpu_ppc601_rtc_init (CPUState *env) | |
445 | { | |
446 | return cpu_ppc_tb_init(env, 7812500); | |
447 | } | |
448 | ||
449 | void cpu_ppc601_store_rtcu (CPUState *env, uint32_t value) | |
450 | __attribute__ (( alias ("cpu_ppc_store_tbu") )); | |
451 | ||
452 | uint32_t cpu_ppc601_load_rtcu (CPUState *env) | |
453 | __attribute__ (( alias ("cpu_ppc_load_tbu") )); | |
454 | ||
455 | void cpu_ppc601_store_rtcl (CPUState *env, uint32_t value) | |
456 | { | |
457 | cpu_ppc_store_tbl(env, value & 0x3FFFFF80); | |
458 | } | |
459 | ||
460 | uint32_t cpu_ppc601_load_rtcl (CPUState *env) | |
461 | { | |
462 | return cpu_ppc_load_tbl(env) & 0x3FFFFF80; | |
463 | } | |
464 | ||
636aaad7 | 465 | /*****************************************************************************/ |
76a66253 | 466 | /* Embedded PowerPC timers */ |
636aaad7 JM |
467 | |
468 | /* PIT, FIT & WDT */ | |
469 | typedef struct ppcemb_timer_t ppcemb_timer_t; | |
470 | struct ppcemb_timer_t { | |
471 | uint64_t pit_reload; /* PIT auto-reload value */ | |
472 | uint64_t fit_next; /* Tick for next FIT interrupt */ | |
473 | struct QEMUTimer *fit_timer; | |
474 | uint64_t wdt_next; /* Tick for next WDT interrupt */ | |
475 | struct QEMUTimer *wdt_timer; | |
476 | }; | |
477 | ||
478 | /* Fixed interval timer */ | |
479 | static void cpu_4xx_fit_cb (void *opaque) | |
480 | { | |
481 | CPUState *env; | |
482 | ppc_tb_t *tb_env; | |
483 | ppcemb_timer_t *ppcemb_timer; | |
484 | uint64_t now, next; | |
485 | ||
486 | env = opaque; | |
487 | tb_env = env->tb_env; | |
488 | ppcemb_timer = tb_env->opaque; | |
489 | now = qemu_get_clock(vm_clock); | |
490 | switch ((env->spr[SPR_40x_TCR] >> 24) & 0x3) { | |
491 | case 0: | |
492 | next = 1 << 9; | |
493 | break; | |
494 | case 1: | |
495 | next = 1 << 13; | |
496 | break; | |
497 | case 2: | |
498 | next = 1 << 17; | |
499 | break; | |
500 | case 3: | |
501 | next = 1 << 21; | |
502 | break; | |
503 | default: | |
504 | /* Cannot occur, but makes gcc happy */ | |
505 | return; | |
506 | } | |
507 | next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq); | |
508 | if (next == now) | |
509 | next++; | |
510 | qemu_mod_timer(ppcemb_timer->fit_timer, next); | |
511 | tb_env->decr_next = next; | |
512 | env->spr[SPR_40x_TSR] |= 1 << 26; | |
513 | if ((env->spr[SPR_40x_TCR] >> 23) & 0x1) | |
514 | ppc_set_irq(env, PPC_INTERRUPT_FIT, 1); | |
515 | if (loglevel) { | |
e96efcfc JM |
516 | fprintf(logfile, "%s: ir %d TCR " ADDRX " TSR " ADDRX "\n", __func__, |
517 | (int)((env->spr[SPR_40x_TCR] >> 23) & 0x1), | |
636aaad7 JM |
518 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); |
519 | } | |
520 | } | |
521 | ||
522 | /* Programmable interval timer */ | |
523 | static void cpu_4xx_pit_cb (void *opaque) | |
76a66253 | 524 | { |
636aaad7 JM |
525 | CPUState *env; |
526 | ppc_tb_t *tb_env; | |
527 | ppcemb_timer_t *ppcemb_timer; | |
528 | uint64_t now, next; | |
529 | ||
530 | env = opaque; | |
531 | tb_env = env->tb_env; | |
532 | ppcemb_timer = tb_env->opaque; | |
533 | now = qemu_get_clock(vm_clock); | |
534 | if ((env->spr[SPR_40x_TCR] >> 22) & 0x1) { | |
535 | /* Auto reload */ | |
536 | next = now + muldiv64(ppcemb_timer->pit_reload, | |
537 | ticks_per_sec, tb_env->tb_freq); | |
538 | if (next == now) | |
539 | next++; | |
540 | qemu_mod_timer(tb_env->decr_timer, next); | |
541 | tb_env->decr_next = next; | |
542 | } | |
543 | env->spr[SPR_40x_TSR] |= 1 << 27; | |
544 | if ((env->spr[SPR_40x_TCR] >> 26) & 0x1) | |
545 | ppc_set_irq(env, PPC_INTERRUPT_PIT, 1); | |
546 | if (loglevel) { | |
e96efcfc JM |
547 | fprintf(logfile, "%s: ar %d ir %d TCR " ADDRX " TSR " ADDRX " " |
548 | "%016" PRIx64 "\n", __func__, | |
549 | (int)((env->spr[SPR_40x_TCR] >> 22) & 0x1), | |
550 | (int)((env->spr[SPR_40x_TCR] >> 26) & 0x1), | |
636aaad7 JM |
551 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR], |
552 | ppcemb_timer->pit_reload); | |
553 | } | |
554 | } | |
555 | ||
556 | /* Watchdog timer */ | |
557 | static void cpu_4xx_wdt_cb (void *opaque) | |
558 | { | |
559 | CPUState *env; | |
560 | ppc_tb_t *tb_env; | |
561 | ppcemb_timer_t *ppcemb_timer; | |
562 | uint64_t now, next; | |
563 | ||
564 | env = opaque; | |
565 | tb_env = env->tb_env; | |
566 | ppcemb_timer = tb_env->opaque; | |
567 | now = qemu_get_clock(vm_clock); | |
568 | switch ((env->spr[SPR_40x_TCR] >> 30) & 0x3) { | |
569 | case 0: | |
570 | next = 1 << 17; | |
571 | break; | |
572 | case 1: | |
573 | next = 1 << 21; | |
574 | break; | |
575 | case 2: | |
576 | next = 1 << 25; | |
577 | break; | |
578 | case 3: | |
579 | next = 1 << 29; | |
580 | break; | |
581 | default: | |
582 | /* Cannot occur, but makes gcc happy */ | |
583 | return; | |
584 | } | |
585 | next = now + muldiv64(next, ticks_per_sec, tb_env->tb_freq); | |
586 | if (next == now) | |
587 | next++; | |
588 | if (loglevel) { | |
e96efcfc | 589 | fprintf(logfile, "%s: TCR " ADDRX " TSR " ADDRX "\n", __func__, |
636aaad7 JM |
590 | env->spr[SPR_40x_TCR], env->spr[SPR_40x_TSR]); |
591 | } | |
592 | switch ((env->spr[SPR_40x_TSR] >> 30) & 0x3) { | |
593 | case 0x0: | |
594 | case 0x1: | |
595 | qemu_mod_timer(ppcemb_timer->wdt_timer, next); | |
596 | ppcemb_timer->wdt_next = next; | |
597 | env->spr[SPR_40x_TSR] |= 1 << 31; | |
598 | break; | |
599 | case 0x2: | |
600 | qemu_mod_timer(ppcemb_timer->wdt_timer, next); | |
601 | ppcemb_timer->wdt_next = next; | |
602 | env->spr[SPR_40x_TSR] |= 1 << 30; | |
603 | if ((env->spr[SPR_40x_TCR] >> 27) & 0x1) | |
604 | ppc_set_irq(env, PPC_INTERRUPT_WDT, 1); | |
605 | break; | |
606 | case 0x3: | |
607 | env->spr[SPR_40x_TSR] &= ~0x30000000; | |
608 | env->spr[SPR_40x_TSR] |= env->spr[SPR_40x_TCR] & 0x30000000; | |
609 | switch ((env->spr[SPR_40x_TCR] >> 28) & 0x3) { | |
610 | case 0x0: | |
611 | /* No reset */ | |
612 | break; | |
613 | case 0x1: /* Core reset */ | |
614 | case 0x2: /* Chip reset */ | |
615 | case 0x3: /* System reset */ | |
616 | qemu_system_reset_request(); | |
617 | return; | |
618 | } | |
619 | } | |
76a66253 JM |
620 | } |
621 | ||
622 | void store_40x_pit (CPUState *env, target_ulong val) | |
623 | { | |
636aaad7 JM |
624 | ppc_tb_t *tb_env; |
625 | ppcemb_timer_t *ppcemb_timer; | |
626 | uint64_t now, next; | |
627 | ||
628 | tb_env = env->tb_env; | |
629 | ppcemb_timer = tb_env->opaque; | |
a496775f | 630 | if (loglevel) { |
636aaad7 | 631 | fprintf(logfile, "%s %p %p\n", __func__, tb_env, ppcemb_timer); |
a496775f | 632 | } |
636aaad7 JM |
633 | ppcemb_timer->pit_reload = val; |
634 | if (val == 0) { | |
635 | /* Stop PIT */ | |
a496775f | 636 | if (loglevel) { |
636aaad7 | 637 | fprintf(logfile, "%s: stop PIT\n", __func__); |
a496775f | 638 | } |
636aaad7 JM |
639 | qemu_del_timer(tb_env->decr_timer); |
640 | } else { | |
a496775f | 641 | if (loglevel) { |
e96efcfc | 642 | fprintf(logfile, "%s: start PIT 0x" ADDRX "\n", __func__, val); |
a496775f | 643 | } |
636aaad7 JM |
644 | now = qemu_get_clock(vm_clock); |
645 | next = now + muldiv64(val, ticks_per_sec, tb_env->tb_freq); | |
646 | if (next == now) | |
647 | next++; | |
648 | qemu_mod_timer(tb_env->decr_timer, next); | |
649 | tb_env->decr_next = next; | |
650 | } | |
76a66253 JM |
651 | } |
652 | ||
636aaad7 | 653 | target_ulong load_40x_pit (CPUState *env) |
76a66253 | 654 | { |
636aaad7 | 655 | return cpu_ppc_load_decr(env); |
76a66253 JM |
656 | } |
657 | ||
658 | void store_booke_tsr (CPUState *env, target_ulong val) | |
659 | { | |
636aaad7 JM |
660 | env->spr[SPR_40x_TSR] = val & 0xFC000000; |
661 | } | |
662 | ||
663 | void store_booke_tcr (CPUState *env, target_ulong val) | |
664 | { | |
665 | /* We don't update timers now. Maybe we should... */ | |
666 | env->spr[SPR_40x_TCR] = val & 0xFF800000; | |
667 | } | |
668 | ||
669 | void ppc_emb_timers_init (CPUState *env) | |
670 | { | |
671 | ppc_tb_t *tb_env; | |
672 | ppcemb_timer_t *ppcemb_timer; | |
673 | ||
674 | tb_env = env->tb_env; | |
675 | ppcemb_timer = qemu_mallocz(sizeof(ppcemb_timer_t)); | |
676 | tb_env->opaque = ppcemb_timer; | |
677 | if (loglevel) | |
678 | fprintf(logfile, "%s %p %p\n", __func__, tb_env, ppcemb_timer); | |
679 | if (ppcemb_timer != NULL) { | |
680 | /* We use decr timer for PIT */ | |
681 | tb_env->decr_timer = qemu_new_timer(vm_clock, &cpu_4xx_pit_cb, env); | |
682 | ppcemb_timer->fit_timer = | |
683 | qemu_new_timer(vm_clock, &cpu_4xx_fit_cb, env); | |
684 | ppcemb_timer->wdt_timer = | |
685 | qemu_new_timer(vm_clock, &cpu_4xx_wdt_cb, env); | |
686 | } | |
76a66253 JM |
687 | } |
688 | ||
2e719ba3 JM |
689 | /*****************************************************************************/ |
690 | /* Embedded PowerPC Device Control Registers */ | |
691 | typedef struct ppc_dcrn_t ppc_dcrn_t; | |
692 | struct ppc_dcrn_t { | |
693 | dcr_read_cb dcr_read; | |
694 | dcr_write_cb dcr_write; | |
695 | void *opaque; | |
696 | }; | |
697 | ||
698 | #define DCRN_NB 1024 | |
699 | struct ppc_dcr_t { | |
700 | ppc_dcrn_t dcrn[DCRN_NB]; | |
701 | int (*read_error)(int dcrn); | |
702 | int (*write_error)(int dcrn); | |
703 | }; | |
704 | ||
705 | int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp) | |
706 | { | |
707 | ppc_dcrn_t *dcr; | |
708 | ||
709 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
710 | goto error; | |
711 | dcr = &dcr_env->dcrn[dcrn]; | |
712 | if (dcr->dcr_read == NULL) | |
713 | goto error; | |
714 | *valp = (*dcr->dcr_read)(dcr->opaque, dcrn); | |
715 | ||
716 | return 0; | |
717 | ||
718 | error: | |
719 | if (dcr_env->read_error != NULL) | |
720 | return (*dcr_env->read_error)(dcrn); | |
721 | ||
722 | return -1; | |
723 | } | |
724 | ||
725 | int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val) | |
726 | { | |
727 | ppc_dcrn_t *dcr; | |
728 | ||
729 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
730 | goto error; | |
731 | dcr = &dcr_env->dcrn[dcrn]; | |
732 | if (dcr->dcr_write == NULL) | |
733 | goto error; | |
734 | (*dcr->dcr_write)(dcr->opaque, dcrn, val); | |
735 | ||
736 | return 0; | |
737 | ||
738 | error: | |
739 | if (dcr_env->write_error != NULL) | |
740 | return (*dcr_env->write_error)(dcrn); | |
741 | ||
742 | return -1; | |
743 | } | |
744 | ||
745 | int ppc_dcr_register (CPUState *env, int dcrn, void *opaque, | |
746 | dcr_read_cb dcr_read, dcr_write_cb dcr_write) | |
747 | { | |
748 | ppc_dcr_t *dcr_env; | |
749 | ppc_dcrn_t *dcr; | |
750 | ||
751 | dcr_env = env->dcr_env; | |
752 | if (dcr_env == NULL) | |
753 | return -1; | |
754 | if (dcrn < 0 || dcrn >= DCRN_NB) | |
755 | return -1; | |
756 | dcr = &dcr_env->dcrn[dcrn]; | |
757 | if (dcr->opaque != NULL || | |
758 | dcr->dcr_read != NULL || | |
759 | dcr->dcr_write != NULL) | |
760 | return -1; | |
761 | dcr->opaque = opaque; | |
762 | dcr->dcr_read = dcr_read; | |
763 | dcr->dcr_write = dcr_write; | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
768 | int ppc_dcr_init (CPUState *env, int (*read_error)(int dcrn), | |
769 | int (*write_error)(int dcrn)) | |
770 | { | |
771 | ppc_dcr_t *dcr_env; | |
772 | ||
773 | dcr_env = qemu_mallocz(sizeof(ppc_dcr_t)); | |
774 | if (dcr_env == NULL) | |
775 | return -1; | |
776 | dcr_env->read_error = read_error; | |
777 | dcr_env->write_error = write_error; | |
778 | env->dcr_env = dcr_env; | |
779 | ||
780 | return 0; | |
781 | } | |
782 | ||
783 | ||
9fddaa0c FB |
784 | #if 0 |
785 | /*****************************************************************************/ | |
786 | /* Handle system reset (for now, just stop emulation) */ | |
787 | void cpu_ppc_reset (CPUState *env) | |
788 | { | |
789 | printf("Reset asked... Stop emulation\n"); | |
790 | abort(); | |
791 | } | |
792 | #endif | |
793 | ||
64201201 FB |
794 | /*****************************************************************************/ |
795 | /* Debug port */ | |
fd0bbb12 | 796 | void PPC_debug_write (void *opaque, uint32_t addr, uint32_t val) |
64201201 FB |
797 | { |
798 | addr &= 0xF; | |
799 | switch (addr) { | |
800 | case 0: | |
801 | printf("%c", val); | |
802 | break; | |
803 | case 1: | |
804 | printf("\n"); | |
805 | fflush(stdout); | |
806 | break; | |
807 | case 2: | |
808 | printf("Set loglevel to %04x\n", val); | |
fd0bbb12 | 809 | cpu_set_log(val | 0x100); |
64201201 FB |
810 | break; |
811 | } | |
812 | } | |
813 | ||
814 | /*****************************************************************************/ | |
815 | /* NVRAM helpers */ | |
816 | void NVRAM_set_byte (m48t59_t *nvram, uint32_t addr, uint8_t value) | |
817 | { | |
819385c5 | 818 | m48t59_write(nvram, addr, value); |
64201201 FB |
819 | } |
820 | ||
821 | uint8_t NVRAM_get_byte (m48t59_t *nvram, uint32_t addr) | |
822 | { | |
819385c5 | 823 | return m48t59_read(nvram, addr); |
64201201 FB |
824 | } |
825 | ||
826 | void NVRAM_set_word (m48t59_t *nvram, uint32_t addr, uint16_t value) | |
827 | { | |
819385c5 FB |
828 | m48t59_write(nvram, addr, value >> 8); |
829 | m48t59_write(nvram, addr + 1, value & 0xFF); | |
64201201 FB |
830 | } |
831 | ||
832 | uint16_t NVRAM_get_word (m48t59_t *nvram, uint32_t addr) | |
833 | { | |
834 | uint16_t tmp; | |
835 | ||
819385c5 FB |
836 | tmp = m48t59_read(nvram, addr) << 8; |
837 | tmp |= m48t59_read(nvram, addr + 1); | |
64201201 FB |
838 | return tmp; |
839 | } | |
840 | ||
841 | void NVRAM_set_lword (m48t59_t *nvram, uint32_t addr, uint32_t value) | |
842 | { | |
819385c5 FB |
843 | m48t59_write(nvram, addr, value >> 24); |
844 | m48t59_write(nvram, addr + 1, (value >> 16) & 0xFF); | |
845 | m48t59_write(nvram, addr + 2, (value >> 8) & 0xFF); | |
846 | m48t59_write(nvram, addr + 3, value & 0xFF); | |
64201201 FB |
847 | } |
848 | ||
849 | uint32_t NVRAM_get_lword (m48t59_t *nvram, uint32_t addr) | |
850 | { | |
851 | uint32_t tmp; | |
852 | ||
819385c5 FB |
853 | tmp = m48t59_read(nvram, addr) << 24; |
854 | tmp |= m48t59_read(nvram, addr + 1) << 16; | |
855 | tmp |= m48t59_read(nvram, addr + 2) << 8; | |
856 | tmp |= m48t59_read(nvram, addr + 3); | |
76a66253 | 857 | |
64201201 FB |
858 | return tmp; |
859 | } | |
860 | ||
861 | void NVRAM_set_string (m48t59_t *nvram, uint32_t addr, | |
862 | const unsigned char *str, uint32_t max) | |
863 | { | |
864 | int i; | |
865 | ||
866 | for (i = 0; i < max && str[i] != '\0'; i++) { | |
819385c5 | 867 | m48t59_write(nvram, addr + i, str[i]); |
64201201 | 868 | } |
819385c5 | 869 | m48t59_write(nvram, addr + max - 1, '\0'); |
64201201 FB |
870 | } |
871 | ||
872 | int NVRAM_get_string (m48t59_t *nvram, uint8_t *dst, uint16_t addr, int max) | |
873 | { | |
874 | int i; | |
875 | ||
876 | memset(dst, 0, max); | |
877 | for (i = 0; i < max; i++) { | |
878 | dst[i] = NVRAM_get_byte(nvram, addr + i); | |
879 | if (dst[i] == '\0') | |
880 | break; | |
881 | } | |
882 | ||
883 | return i; | |
884 | } | |
885 | ||
886 | static uint16_t NVRAM_crc_update (uint16_t prev, uint16_t value) | |
887 | { | |
888 | uint16_t tmp; | |
889 | uint16_t pd, pd1, pd2; | |
890 | ||
891 | tmp = prev >> 8; | |
892 | pd = prev ^ value; | |
893 | pd1 = pd & 0x000F; | |
894 | pd2 = ((pd >> 4) & 0x000F) ^ pd1; | |
895 | tmp ^= (pd1 << 3) | (pd1 << 8); | |
896 | tmp ^= pd2 | (pd2 << 7) | (pd2 << 12); | |
897 | ||
898 | return tmp; | |
899 | } | |
900 | ||
901 | uint16_t NVRAM_compute_crc (m48t59_t *nvram, uint32_t start, uint32_t count) | |
902 | { | |
903 | uint32_t i; | |
904 | uint16_t crc = 0xFFFF; | |
905 | int odd; | |
906 | ||
907 | odd = count & 1; | |
908 | count &= ~1; | |
909 | for (i = 0; i != count; i++) { | |
76a66253 | 910 | crc = NVRAM_crc_update(crc, NVRAM_get_word(nvram, start + i)); |
64201201 FB |
911 | } |
912 | if (odd) { | |
76a66253 | 913 | crc = NVRAM_crc_update(crc, NVRAM_get_byte(nvram, start + i) << 8); |
64201201 FB |
914 | } |
915 | ||
916 | return crc; | |
917 | } | |
918 | ||
fd0bbb12 FB |
919 | #define CMDLINE_ADDR 0x017ff000 |
920 | ||
64201201 FB |
921 | int PPC_NVRAM_set_params (m48t59_t *nvram, uint16_t NVRAM_size, |
922 | const unsigned char *arch, | |
923 | uint32_t RAM_size, int boot_device, | |
924 | uint32_t kernel_image, uint32_t kernel_size, | |
fd0bbb12 | 925 | const char *cmdline, |
64201201 | 926 | uint32_t initrd_image, uint32_t initrd_size, |
fd0bbb12 FB |
927 | uint32_t NVRAM_image, |
928 | int width, int height, int depth) | |
64201201 FB |
929 | { |
930 | uint16_t crc; | |
931 | ||
932 | /* Set parameters for Open Hack'Ware BIOS */ | |
933 | NVRAM_set_string(nvram, 0x00, "QEMU_BIOS", 16); | |
934 | NVRAM_set_lword(nvram, 0x10, 0x00000002); /* structure v2 */ | |
935 | NVRAM_set_word(nvram, 0x14, NVRAM_size); | |
936 | NVRAM_set_string(nvram, 0x20, arch, 16); | |
937 | NVRAM_set_lword(nvram, 0x30, RAM_size); | |
938 | NVRAM_set_byte(nvram, 0x34, boot_device); | |
939 | NVRAM_set_lword(nvram, 0x38, kernel_image); | |
940 | NVRAM_set_lword(nvram, 0x3C, kernel_size); | |
fd0bbb12 FB |
941 | if (cmdline) { |
942 | /* XXX: put the cmdline in NVRAM too ? */ | |
943 | strcpy(phys_ram_base + CMDLINE_ADDR, cmdline); | |
944 | NVRAM_set_lword(nvram, 0x40, CMDLINE_ADDR); | |
945 | NVRAM_set_lword(nvram, 0x44, strlen(cmdline)); | |
946 | } else { | |
947 | NVRAM_set_lword(nvram, 0x40, 0); | |
948 | NVRAM_set_lword(nvram, 0x44, 0); | |
949 | } | |
64201201 FB |
950 | NVRAM_set_lword(nvram, 0x48, initrd_image); |
951 | NVRAM_set_lword(nvram, 0x4C, initrd_size); | |
952 | NVRAM_set_lword(nvram, 0x50, NVRAM_image); | |
fd0bbb12 FB |
953 | |
954 | NVRAM_set_word(nvram, 0x54, width); | |
955 | NVRAM_set_word(nvram, 0x56, height); | |
956 | NVRAM_set_word(nvram, 0x58, depth); | |
957 | crc = NVRAM_compute_crc(nvram, 0x00, 0xF8); | |
958 | NVRAM_set_word(nvram, 0xFC, crc); | |
64201201 FB |
959 | |
960 | return 0; | |
a541f297 | 961 | } |