2 * QEMU generic PPC hardware System Emulator
4 * Copyright (c) 2003-2004 Jocelyn Mayer
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 void ppc_prep_init (int ram_size, int vga_ram_size, int boot_device,
27 DisplayState *ds, const char **fd_filename, int snapshot,
28 const char *kernel_filename, const char *kernel_cmdline,
29 const char *initrd_filename);
31 /*****************************************************************************/
32 /* PPC time base and decrementer emulation */
36 /* Time base management */
37 int64_t tb_offset; /* Compensation */
38 uint32_t tb_freq; /* TB frequency */
39 /* Decrementer management */
40 uint64_t decr_next; /* Tick for next decr interrupt */
41 struct QEMUTimer *decr_timer;
44 static inline uint64_t cpu_ppc_get_tb (ppc_tb_t *tb_env)
46 /* TB time in tb periods */
47 return muldiv64(qemu_get_clock(vm_clock) + tb_env->tb_offset,
48 tb_env->tb_freq, ticks_per_sec);
51 uint32_t cpu_ppc_load_tbl (CPUState *env)
53 ppc_tb_t *tb_env = env->tb_env;
56 tb = cpu_ppc_get_tb(tb_env);
62 if (last_time != now) {
64 printf("%s: tb=0x%016lx %d %08lx\n",
65 __func__, tb, now, tb_env->tb_offset);
70 return tb & 0xFFFFFFFF;
73 uint32_t cpu_ppc_load_tbu (CPUState *env)
75 ppc_tb_t *tb_env = env->tb_env;
78 tb = cpu_ppc_get_tb(tb_env);
80 printf("%s: tb=0x%016lx\n", __func__, tb);
85 static void cpu_ppc_store_tb (ppc_tb_t *tb_env, uint64_t value)
87 tb_env->tb_offset = muldiv64(value, ticks_per_sec, tb_env->tb_freq)
88 - qemu_get_clock(vm_clock);
90 printf("%s: tb=0x%016lx offset=%08x\n", __func__, value);
94 void cpu_ppc_store_tbu (CPUState *env, uint32_t value)
96 ppc_tb_t *tb_env = env->tb_env;
98 cpu_ppc_store_tb(tb_env,
99 ((uint64_t)value << 32) | cpu_ppc_load_tbl(env));
102 void cpu_ppc_store_tbl (CPUState *env, uint32_t value)
104 ppc_tb_t *tb_env = env->tb_env;
106 cpu_ppc_store_tb(tb_env,
107 ((uint64_t)cpu_ppc_load_tbu(env) << 32) | value);
110 uint32_t cpu_ppc_load_decr (CPUState *env)
112 ppc_tb_t *tb_env = env->tb_env;
115 decr = muldiv64(tb_env->decr_next - qemu_get_clock(vm_clock),
116 tb_env->tb_freq, ticks_per_sec);
118 printf("%s: 0x%08x\n", __func__, decr);
124 /* When decrementer expires,
125 * all we need to do is generate or queue a CPU exception
127 static inline void cpu_ppc_decr_excp (CPUState *env)
131 printf("raise decrementer exception\n");
133 cpu_interrupt(env, CPU_INTERRUPT_TIMER);
136 static void _cpu_ppc_store_decr (CPUState *env, uint32_t decr,
137 uint32_t value, int is_excp)
139 ppc_tb_t *tb_env = env->tb_env;
143 printf("%s: 0x%08x => 0x%08x\n", __func__, decr, value);
145 now = qemu_get_clock(vm_clock);
146 next = now + muldiv64(value, ticks_per_sec, tb_env->tb_freq);
148 next += tb_env->decr_next - now;
151 tb_env->decr_next = next;
153 qemu_mod_timer(tb_env->decr_timer, next);
154 /* If we set a negative value and the decrementer was positive,
155 * raise an exception.
157 if ((value & 0x80000000) && !(decr & 0x80000000))
158 cpu_ppc_decr_excp(env);
161 void cpu_ppc_store_decr (CPUState *env, uint32_t value)
163 _cpu_ppc_store_decr(env, cpu_ppc_load_decr(env), value, 0);
166 static void cpu_ppc_decr_cb (void *opaque)
168 _cpu_ppc_store_decr(opaque, 0x00000000, 0xFFFFFFFF, 1);
171 /* Set up (once) timebase frequency (in Hz) */
172 ppc_tb_t *cpu_ppc_tb_init (CPUState *env, uint32_t freq)
176 tb_env = qemu_mallocz(sizeof(ppc_tb_t));
179 env->tb_env = tb_env;
180 if (tb_env->tb_freq == 0 || 1) {
181 tb_env->tb_freq = freq;
182 /* Create new timer */
184 qemu_new_timer(vm_clock, &cpu_ppc_decr_cb, env);
185 /* There is a bug in 2.4 kernels:
186 * if a decrementer exception is pending when it enables msr_ee,
187 * it's not ready to handle it...
189 _cpu_ppc_store_decr(env, 0xFFFFFFFF, 0xFFFFFFFF, 0);
196 /*****************************************************************************/
197 /* Handle system reset (for now, just stop emulation) */
198 void cpu_ppc_reset (CPUState *env)
200 printf("Reset asked... Stop emulation\n");
205 /*****************************************************************************/
206 void ppc_init (int ram_size, int vga_ram_size, int boot_device,
207 DisplayState *ds, const char **fd_filename, int snapshot,
208 const char *kernel_filename, const char *kernel_cmdline,
209 const char *initrd_filename)
211 /* For now, only PREP is supported */
212 return ppc_prep_init(ram_size, vga_ram_size, boot_device, ds, fd_filename,
213 snapshot, kernel_filename, kernel_cmdline,