Statistics
| Branch: | Revision:

root / hw / mips_r4k.c @ 6af0bf9c

History | View | Annotate | Download (8.1 kB)

1 6af0bf9c bellard
#include "vl.h"
2 6af0bf9c bellard
3 6af0bf9c bellard
#define DEBUG_IRQ_COUNT
4 6af0bf9c bellard
5 6af0bf9c bellard
#define BIOS_FILENAME "mips_bios.bin"
6 6af0bf9c bellard
//#define BIOS_FILENAME "system.bin"
7 6af0bf9c bellard
#define KERNEL_LOAD_ADDR 0x80010000
8 6af0bf9c bellard
#define INITRD_LOAD_ADDR 0x80800000
9 6af0bf9c bellard
10 6af0bf9c bellard
/* MIPS R4K IRQ controler */
11 6af0bf9c bellard
#if defined(DEBUG_IRQ_COUNT)
12 6af0bf9c bellard
static uint64_t irq_count[16];
13 6af0bf9c bellard
#endif
14 6af0bf9c bellard
15 6af0bf9c bellard
extern FILE *logfile;
16 6af0bf9c bellard
17 6af0bf9c bellard
void mips_set_irq (int n_IRQ, int level)
18 6af0bf9c bellard
{
19 6af0bf9c bellard
    uint32_t mask;
20 6af0bf9c bellard
21 6af0bf9c bellard
    if (n_IRQ < 0 || n_IRQ >= 8)
22 6af0bf9c bellard
        return;
23 6af0bf9c bellard
    mask = 0x100 << n_IRQ;
24 6af0bf9c bellard
    if (level != 0) {
25 6af0bf9c bellard
#if 1
26 6af0bf9c bellard
        if (logfile) {
27 6af0bf9c bellard
            fprintf(logfile, "%s n %d l %d mask %08x %08x\n",
28 6af0bf9c bellard
                    __func__, n_IRQ, level, mask, cpu_single_env->CP0_Status);
29 6af0bf9c bellard
        }
30 6af0bf9c bellard
#endif
31 6af0bf9c bellard
        cpu_single_env->CP0_Cause |= mask;
32 6af0bf9c bellard
        if ((cpu_single_env->CP0_Status & 0x00000001) &&
33 6af0bf9c bellard
            (cpu_single_env->CP0_Status & mask)) {
34 6af0bf9c bellard
#if defined(DEBUG_IRQ_COUNT)
35 6af0bf9c bellard
            irq_count[n_IRQ]++;
36 6af0bf9c bellard
#endif
37 6af0bf9c bellard
#if 1
38 6af0bf9c bellard
            if (logfile)
39 6af0bf9c bellard
                fprintf(logfile, "%s raise IRQ\n", __func__);
40 6af0bf9c bellard
#endif
41 6af0bf9c bellard
            cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD);
42 6af0bf9c bellard
        }
43 6af0bf9c bellard
    } else {
44 6af0bf9c bellard
        cpu_single_env->CP0_Cause &= ~mask;
45 6af0bf9c bellard
    }
46 6af0bf9c bellard
}
47 6af0bf9c bellard
48 6af0bf9c bellard
void pic_set_irq (int n_IRQ, int level)
49 6af0bf9c bellard
{
50 6af0bf9c bellard
    mips_set_irq(n_IRQ + 2, level);
51 6af0bf9c bellard
}
52 6af0bf9c bellard
53 6af0bf9c bellard
void pic_info (void)
54 6af0bf9c bellard
{
55 6af0bf9c bellard
    term_printf("IRQ asserted: %02x mask: %02x\n",
56 6af0bf9c bellard
                (cpu_single_env->CP0_Cause >> 8) & 0xFF,
57 6af0bf9c bellard
                (cpu_single_env->CP0_Status >> 8) & 0xFF);
58 6af0bf9c bellard
}
59 6af0bf9c bellard
60 6af0bf9c bellard
void irq_info (void)
61 6af0bf9c bellard
{
62 6af0bf9c bellard
#if !defined(DEBUG_IRQ_COUNT)
63 6af0bf9c bellard
    term_printf("irq statistic code not compiled.\n");
64 6af0bf9c bellard
#else
65 6af0bf9c bellard
    int i;
66 6af0bf9c bellard
    int64_t count;
67 6af0bf9c bellard
68 6af0bf9c bellard
    term_printf("IRQ statistics:\n");
69 6af0bf9c bellard
    for (i = 0; i < 8; i++) {
70 6af0bf9c bellard
        count = irq_count[i];
71 6af0bf9c bellard
        if (count > 0)
72 6af0bf9c bellard
            term_printf("%2d: %lld\n", i, count);
73 6af0bf9c bellard
    }
74 6af0bf9c bellard
#endif
75 6af0bf9c bellard
}
76 6af0bf9c bellard
77 6af0bf9c bellard
void cpu_mips_irqctrl_init (void)
78 6af0bf9c bellard
{
79 6af0bf9c bellard
}
80 6af0bf9c bellard
81 6af0bf9c bellard
/* MIPS R4K timer */
82 6af0bf9c bellard
uint32_t cpu_mips_get_random (CPUState *env)
83 6af0bf9c bellard
{
84 6af0bf9c bellard
    uint64_t now = qemu_get_clock(vm_clock);
85 6af0bf9c bellard
86 6af0bf9c bellard
    return (uint32_t)now & 0x0000000F;
87 6af0bf9c bellard
}
88 6af0bf9c bellard
89 6af0bf9c bellard
uint32_t cpu_mips_get_count (CPUState *env)
90 6af0bf9c bellard
{
91 6af0bf9c bellard
    return env->CP0_Count +
92 6af0bf9c bellard
        (uint32_t)muldiv64(qemu_get_clock(vm_clock),
93 6af0bf9c bellard
                           100 * 1000 * 1000, ticks_per_sec);
94 6af0bf9c bellard
}
95 6af0bf9c bellard
96 6af0bf9c bellard
static void cpu_mips_update_count (CPUState *env, uint32_t count,
97 6af0bf9c bellard
                                   uint32_t compare)
98 6af0bf9c bellard
{
99 6af0bf9c bellard
    uint64_t now, next;
100 6af0bf9c bellard
    uint32_t tmp;
101 6af0bf9c bellard
    
102 6af0bf9c bellard
    tmp = count;
103 6af0bf9c bellard
    if (count == compare)
104 6af0bf9c bellard
        tmp++;
105 6af0bf9c bellard
    now = qemu_get_clock(vm_clock);
106 6af0bf9c bellard
    next = now + muldiv64(compare - tmp, ticks_per_sec, 100 * 1000 * 1000);
107 6af0bf9c bellard
    if (next == now)
108 6af0bf9c bellard
        next++;
109 6af0bf9c bellard
#if 1
110 6af0bf9c bellard
    if (logfile) {
111 6af0bf9c bellard
        fprintf(logfile, "%s: 0x%08llx %08x %08x => 0x%08llx\n",
112 6af0bf9c bellard
                __func__, now, count, compare, next - now);
113 6af0bf9c bellard
    }
114 6af0bf9c bellard
#endif
115 6af0bf9c bellard
    /* Store new count and compare registers */
116 6af0bf9c bellard
    env->CP0_Compare = compare;
117 6af0bf9c bellard
    env->CP0_Count =
118 6af0bf9c bellard
        count - (uint32_t)muldiv64(now, 100 * 1000 * 1000, ticks_per_sec);
119 6af0bf9c bellard
    /* Adjust timer */
120 6af0bf9c bellard
    qemu_mod_timer(env->timer, next);
121 6af0bf9c bellard
}
122 6af0bf9c bellard
123 6af0bf9c bellard
void cpu_mips_store_count (CPUState *env, uint32_t value)
124 6af0bf9c bellard
{
125 6af0bf9c bellard
    cpu_mips_update_count(env, value, env->CP0_Compare);
126 6af0bf9c bellard
}
127 6af0bf9c bellard
128 6af0bf9c bellard
void cpu_mips_store_compare (CPUState *env, uint32_t value)
129 6af0bf9c bellard
{
130 6af0bf9c bellard
    cpu_mips_update_count(env, cpu_mips_get_count(env), value);
131 6af0bf9c bellard
    pic_set_irq(5, 0);
132 6af0bf9c bellard
}
133 6af0bf9c bellard
134 6af0bf9c bellard
static void mips_timer_cb (void *opaque)
135 6af0bf9c bellard
{
136 6af0bf9c bellard
    CPUState *env;
137 6af0bf9c bellard
138 6af0bf9c bellard
    env = opaque;
139 6af0bf9c bellard
#if 1
140 6af0bf9c bellard
    if (logfile) {
141 6af0bf9c bellard
        fprintf(logfile, "%s\n", __func__);
142 6af0bf9c bellard
    }
143 6af0bf9c bellard
#endif
144 6af0bf9c bellard
    cpu_mips_update_count(env, cpu_mips_get_count(env), env->CP0_Compare);
145 6af0bf9c bellard
    pic_set_irq(5, 1);
146 6af0bf9c bellard
}
147 6af0bf9c bellard
148 6af0bf9c bellard
void cpu_mips_clock_init (CPUState *env)
149 6af0bf9c bellard
{
150 6af0bf9c bellard
    env->timer = qemu_new_timer(vm_clock, &mips_timer_cb, env);
151 6af0bf9c bellard
    env->CP0_Compare = 0;
152 6af0bf9c bellard
    cpu_mips_update_count(env, 1, 0);
153 6af0bf9c bellard
}
154 6af0bf9c bellard
155 6af0bf9c bellard
static void io_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
156 6af0bf9c bellard
{
157 6af0bf9c bellard
    if (logfile)
158 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
159 6af0bf9c bellard
    cpu_outb(NULL, addr & 0xffff, value);
160 6af0bf9c bellard
}
161 6af0bf9c bellard
162 6af0bf9c bellard
static uint32_t io_readb (void *opaque, target_phys_addr_t addr)
163 6af0bf9c bellard
{
164 6af0bf9c bellard
    uint32_t ret = cpu_inb(NULL, addr & 0xffff);
165 6af0bf9c bellard
    if (logfile)
166 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
167 6af0bf9c bellard
    return ret;
168 6af0bf9c bellard
}
169 6af0bf9c bellard
170 6af0bf9c bellard
static void io_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
171 6af0bf9c bellard
{
172 6af0bf9c bellard
    if (logfile)
173 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
174 6af0bf9c bellard
#ifdef TARGET_WORDS_BIGENDIAN
175 6af0bf9c bellard
    value = bswap16(value);
176 6af0bf9c bellard
#endif
177 6af0bf9c bellard
    cpu_outw(NULL, addr & 0xffff, value);
178 6af0bf9c bellard
}
179 6af0bf9c bellard
180 6af0bf9c bellard
static uint32_t io_readw (void *opaque, target_phys_addr_t addr)
181 6af0bf9c bellard
{
182 6af0bf9c bellard
    uint32_t ret = cpu_inw(NULL, addr & 0xffff);
183 6af0bf9c bellard
#ifdef TARGET_WORDS_BIGENDIAN
184 6af0bf9c bellard
    ret = bswap16(ret);
185 6af0bf9c bellard
#endif
186 6af0bf9c bellard
    if (logfile)
187 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
188 6af0bf9c bellard
    return ret;
189 6af0bf9c bellard
}
190 6af0bf9c bellard
191 6af0bf9c bellard
static void io_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
192 6af0bf9c bellard
{
193 6af0bf9c bellard
    if (logfile)
194 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, value);
195 6af0bf9c bellard
#ifdef TARGET_WORDS_BIGENDIAN
196 6af0bf9c bellard
    value = bswap32(value);
197 6af0bf9c bellard
#endif
198 6af0bf9c bellard
    cpu_outl(NULL, addr & 0xffff, value);
199 6af0bf9c bellard
}
200 6af0bf9c bellard
201 6af0bf9c bellard
static uint32_t io_readl (void *opaque, target_phys_addr_t addr)
202 6af0bf9c bellard
{
203 6af0bf9c bellard
    uint32_t ret = cpu_inl(NULL, addr & 0xffff);
204 6af0bf9c bellard
205 6af0bf9c bellard
#ifdef TARGET_WORDS_BIGENDIAN
206 6af0bf9c bellard
    ret = bswap32(ret);
207 6af0bf9c bellard
#endif
208 6af0bf9c bellard
    if (logfile)
209 6af0bf9c bellard
        fprintf(logfile, "%s: addr %08x val %08x\n", __func__, addr, ret);
210 6af0bf9c bellard
    return ret;
211 6af0bf9c bellard
}
212 6af0bf9c bellard
213 6af0bf9c bellard
CPUWriteMemoryFunc *io_write[] = {
214 6af0bf9c bellard
    &io_writeb,
215 6af0bf9c bellard
    &io_writew,
216 6af0bf9c bellard
    &io_writel,
217 6af0bf9c bellard
};
218 6af0bf9c bellard
219 6af0bf9c bellard
CPUReadMemoryFunc *io_read[] = {
220 6af0bf9c bellard
    &io_readb,
221 6af0bf9c bellard
    &io_readw,
222 6af0bf9c bellard
    &io_readl,
223 6af0bf9c bellard
};
224 6af0bf9c bellard
225 6af0bf9c bellard
void mips_r4k_init (int ram_size, int vga_ram_size, int boot_device,
226 6af0bf9c bellard
                    DisplayState *ds, const char **fd_filename, int snapshot,
227 6af0bf9c bellard
                    const char *kernel_filename, const char *kernel_cmdline,
228 6af0bf9c bellard
                    const char *initrd_filename)
229 6af0bf9c bellard
{
230 6af0bf9c bellard
    char buf[1024];
231 6af0bf9c bellard
    target_ulong kernel_base, kernel_size, initrd_base, initrd_size;
232 6af0bf9c bellard
    unsigned long bios_offset;
233 6af0bf9c bellard
    int io_memory;
234 6af0bf9c bellard
    int linux_boot;
235 6af0bf9c bellard
    int ret;
236 6af0bf9c bellard
237 6af0bf9c bellard
    printf("%s: start\n", __func__);
238 6af0bf9c bellard
    linux_boot = (kernel_filename != NULL);
239 6af0bf9c bellard
    /* allocate RAM */
240 6af0bf9c bellard
    cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
241 6af0bf9c bellard
    bios_offset = ram_size + vga_ram_size;
242 6af0bf9c bellard
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
243 6af0bf9c bellard
    printf("%s: load BIOS '%s' size %d\n", __func__, buf, BIOS_SIZE);
244 6af0bf9c bellard
    ret = load_image(buf, phys_ram_base + bios_offset);
245 6af0bf9c bellard
    if (ret != BIOS_SIZE) {
246 6af0bf9c bellard
        fprintf(stderr, "qemu: could not load MIPS bios '%s'\n", buf);
247 6af0bf9c bellard
        exit(1);
248 6af0bf9c bellard
    }
249 6af0bf9c bellard
    cpu_register_physical_memory((uint32_t)(0x1fc00000),
250 6af0bf9c bellard
                                 BIOS_SIZE, bios_offset | IO_MEM_ROM);
251 6af0bf9c bellard
#if 0
252 6af0bf9c bellard
    memcpy(phys_ram_base + 0x10000, phys_ram_base + bios_offset, BIOS_SIZE);
253 6af0bf9c bellard
    cpu_single_env->PC = 0x80010004;
254 6af0bf9c bellard
#else
255 6af0bf9c bellard
    cpu_single_env->PC = 0xBFC00004;
256 6af0bf9c bellard
#endif
257 6af0bf9c bellard
    if (linux_boot) {
258 6af0bf9c bellard
        kernel_base = KERNEL_LOAD_ADDR;
259 6af0bf9c bellard
        /* now we can load the kernel */
260 6af0bf9c bellard
        kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
261 6af0bf9c bellard
        if (kernel_size < 0) {
262 6af0bf9c bellard
            fprintf(stderr, "qemu: could not load kernel '%s'\n", 
263 6af0bf9c bellard
                    kernel_filename);
264 6af0bf9c bellard
            exit(1);
265 6af0bf9c bellard
        }
266 6af0bf9c bellard
        /* load initrd */
267 6af0bf9c bellard
        if (initrd_filename) {
268 6af0bf9c bellard
            initrd_base = INITRD_LOAD_ADDR;
269 6af0bf9c bellard
            initrd_size = load_image(initrd_filename,
270 6af0bf9c bellard
                                     phys_ram_base + initrd_base);
271 6af0bf9c bellard
            if (initrd_size < 0) {
272 6af0bf9c bellard
                fprintf(stderr, "qemu: could not load initial ram disk '%s'\n", 
273 6af0bf9c bellard
                        initrd_filename);
274 6af0bf9c bellard
                exit(1);
275 6af0bf9c bellard
            }
276 6af0bf9c bellard
        } else {
277 6af0bf9c bellard
            initrd_base = 0;
278 6af0bf9c bellard
            initrd_size = 0;
279 6af0bf9c bellard
        }
280 6af0bf9c bellard
        cpu_single_env->PC = KERNEL_LOAD_ADDR;
281 6af0bf9c bellard
    } else {
282 6af0bf9c bellard
        kernel_base = 0;
283 6af0bf9c bellard
        kernel_size = 0;
284 6af0bf9c bellard
        initrd_base = 0;
285 6af0bf9c bellard
        initrd_size = 0;
286 6af0bf9c bellard
    }
287 6af0bf9c bellard
    /* XXX: should not be ! */
288 6af0bf9c bellard
    printf("%s: init VGA\n", __func__);
289 6af0bf9c bellard
    vga_initialize(NULL, ds, phys_ram_base + ram_size, ram_size, 
290 6af0bf9c bellard
                   vga_ram_size);
291 6af0bf9c bellard
292 6af0bf9c bellard
293 6af0bf9c bellard
    /* Init internal devices */
294 6af0bf9c bellard
    cpu_mips_clock_init(cpu_single_env);
295 6af0bf9c bellard
    cpu_mips_irqctrl_init();
296 6af0bf9c bellard
297 6af0bf9c bellard
    isa_mem_base = 0x78000000;
298 6af0bf9c bellard
    /* Register 64 KB of ISA IO space at random address */
299 6af0bf9c bellard
    io_memory = cpu_register_io_memory(0, io_read, io_write, NULL);
300 6af0bf9c bellard
    cpu_register_physical_memory(0x70000000, 0x00010000, io_memory);
301 6af0bf9c bellard
    serial_init(0x3f8, 4, serial_hds[0]);
302 6af0bf9c bellard
    printf("%s: done\n", __func__);
303 6af0bf9c bellard
}
304 6af0bf9c bellard
305 6af0bf9c bellard
QEMUMachine mips_machine = {
306 6af0bf9c bellard
    "mips",
307 6af0bf9c bellard
    "mips r4k platform",
308 6af0bf9c bellard
    mips_r4k_init,
309 6af0bf9c bellard
};