Statistics
| Branch: | Revision:

root / hw / mips_r4k.c @ 804b2071

History | View | Annotate | Download (9 kB)

1 e16fe40c ths
/*
2 e16fe40c ths
 * QEMU/MIPS pseudo-board
3 e16fe40c ths
 *
4 e16fe40c ths
 * emulates a simple machine with ISA-like bus.
5 e16fe40c ths
 * ISA IO space mapped to the 0x14000000 (PHYS) and
6 e16fe40c ths
 * ISA memory at the 0x10000000 (PHYS, 16Mb in size).
7 e16fe40c ths
 * All peripherial devices are attached to this "bus" with
8 e16fe40c ths
 * the standard PC ISA addresses.
9 e16fe40c ths
*/
10 87ecb68b pbrook
#include "hw.h"
11 87ecb68b pbrook
#include "mips.h"
12 b970ea8f Blue Swirl
#include "mips_cpudevs.h"
13 87ecb68b pbrook
#include "pc.h"
14 87ecb68b pbrook
#include "isa.h"
15 87ecb68b pbrook
#include "net.h"
16 87ecb68b pbrook
#include "sysemu.h"
17 87ecb68b pbrook
#include "boards.h"
18 b305b5ba ths
#include "flash.h"
19 3b3fb322 blueswir1
#include "qemu-log.h"
20 bba831e8 Paul Brook
#include "mips-bios.h"
21 ec82026c Gerd Hoffmann
#include "ide.h"
22 ca20cf32 Blue Swirl
#include "loader.h"
23 ca20cf32 Blue Swirl
#include "elf.h"
24 1d914fa0 Isaku Yamahata
#include "mc146818rtc.h"
25 44cbbf18 ths
26 e4bcb14c ths
#define MAX_IDE_BUS 2
27 e4bcb14c ths
28 58126404 pbrook
static const int ide_iobase[2] = { 0x1f0, 0x170 };
29 58126404 pbrook
static const int ide_iobase2[2] = { 0x3f6, 0x376 };
30 58126404 pbrook
static const int ide_irq[2] = { 14, 15 };
31 58126404 pbrook
32 e16fe40c ths
static PITState *pit; /* PIT i8254 */
33 697584ab bellard
34 1b66074b ths
/* i8254 PIT is attached to the IRQ0 at PIC i8259 */
35 6af0bf9c bellard
36 7df526e3 ths
static struct _loaderparams {
37 7df526e3 ths
    int ram_size;
38 7df526e3 ths
    const char *kernel_filename;
39 7df526e3 ths
    const char *kernel_cmdline;
40 7df526e3 ths
    const char *initrd_filename;
41 7df526e3 ths
} loaderparams;
42 7df526e3 ths
43 c227f099 Anthony Liguori
static void mips_qemu_writel (void *opaque, target_phys_addr_t addr,
44 6ae81775 ths
                              uint32_t val)
45 6ae81775 ths
{
46 6ae81775 ths
    if ((addr & 0xffff) == 0 && val == 42)
47 6ae81775 ths
        qemu_system_reset_request ();
48 6ae81775 ths
    else if ((addr & 0xffff) == 4 && val == 42)
49 6ae81775 ths
        qemu_system_shutdown_request ();
50 6ae81775 ths
}
51 6ae81775 ths
52 c227f099 Anthony Liguori
static uint32_t mips_qemu_readl (void *opaque, target_phys_addr_t addr)
53 6ae81775 ths
{
54 6ae81775 ths
    return 0;
55 6ae81775 ths
}
56 6ae81775 ths
57 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const mips_qemu_write[] = {
58 6ae81775 ths
    &mips_qemu_writel,
59 6ae81775 ths
    &mips_qemu_writel,
60 6ae81775 ths
    &mips_qemu_writel,
61 6ae81775 ths
};
62 6ae81775 ths
63 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const mips_qemu_read[] = {
64 6ae81775 ths
    &mips_qemu_readl,
65 6ae81775 ths
    &mips_qemu_readl,
66 6ae81775 ths
    &mips_qemu_readl,
67 6ae81775 ths
};
68 6ae81775 ths
69 6ae81775 ths
static int mips_qemu_iomemtype = 0;
70 6ae81775 ths
71 e16ad5b0 Aurelien Jarno
typedef struct ResetData {
72 e16ad5b0 Aurelien Jarno
    CPUState *env;
73 e16ad5b0 Aurelien Jarno
    uint64_t vector;
74 e16ad5b0 Aurelien Jarno
} ResetData;
75 e16ad5b0 Aurelien Jarno
76 e16ad5b0 Aurelien Jarno
static int64_t load_kernel(void)
77 6ae81775 ths
{
78 409dbce5 Aurelien Jarno
    int64_t entry, kernel_high;
79 e90e795e Aurelien Jarno
    long kernel_size, initrd_size, params_size;
80 c227f099 Anthony Liguori
    ram_addr_t initrd_offset;
81 e90e795e Aurelien Jarno
    uint32_t *params_buf;
82 ca20cf32 Blue Swirl
    int big_endian;
83 6ae81775 ths
84 ca20cf32 Blue Swirl
#ifdef TARGET_WORDS_BIGENDIAN
85 ca20cf32 Blue Swirl
    big_endian = 1;
86 ca20cf32 Blue Swirl
#else
87 ca20cf32 Blue Swirl
    big_endian = 0;
88 ca20cf32 Blue Swirl
#endif
89 409dbce5 Aurelien Jarno
    kernel_size = load_elf(loaderparams.kernel_filename, cpu_mips_kseg0_to_phys,
90 409dbce5 Aurelien Jarno
                           NULL, (uint64_t *)&entry, NULL,
91 409dbce5 Aurelien Jarno
                           (uint64_t *)&kernel_high, big_endian,
92 409dbce5 Aurelien Jarno
                           ELF_MACHINE, 1);
93 c570fd16 ths
    if (kernel_size >= 0) {
94 c570fd16 ths
        if ((entry & ~0x7fffffffULL) == 0x80000000)
95 5dc4b744 ths
            entry = (int32_t)entry;
96 c570fd16 ths
    } else {
97 9042c0e2 ths
        fprintf(stderr, "qemu: could not load kernel '%s'\n",
98 7df526e3 ths
                loaderparams.kernel_filename);
99 9042c0e2 ths
        exit(1);
100 6ae81775 ths
    }
101 6ae81775 ths
102 6ae81775 ths
    /* load initrd */
103 6ae81775 ths
    initrd_size = 0;
104 74287114 ths
    initrd_offset = 0;
105 7df526e3 ths
    if (loaderparams.initrd_filename) {
106 7df526e3 ths
        initrd_size = get_image_size (loaderparams.initrd_filename);
107 74287114 ths
        if (initrd_size > 0) {
108 74287114 ths
            initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
109 74287114 ths
            if (initrd_offset + initrd_size > ram_size) {
110 74287114 ths
                fprintf(stderr,
111 74287114 ths
                        "qemu: memory too small for initial ram disk '%s'\n",
112 7df526e3 ths
                        loaderparams.initrd_filename);
113 74287114 ths
                exit(1);
114 74287114 ths
            }
115 dcac9679 pbrook
            initrd_size = load_image_targphys(loaderparams.initrd_filename,
116 dcac9679 pbrook
                                              initrd_offset,
117 dcac9679 pbrook
                                              ram_size - initrd_offset);
118 74287114 ths
        }
119 6ae81775 ths
        if (initrd_size == (target_ulong) -1) {
120 6ae81775 ths
            fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
121 7df526e3 ths
                    loaderparams.initrd_filename);
122 6ae81775 ths
            exit(1);
123 6ae81775 ths
        }
124 6ae81775 ths
    }
125 6ae81775 ths
126 6ae81775 ths
    /* Store command line.  */
127 e90e795e Aurelien Jarno
    params_size = 264;
128 e90e795e Aurelien Jarno
    params_buf = qemu_malloc(params_size);
129 e90e795e Aurelien Jarno
130 e90e795e Aurelien Jarno
    params_buf[0] = tswap32(ram_size);
131 e90e795e Aurelien Jarno
    params_buf[1] = tswap32(0x12345678);
132 e90e795e Aurelien Jarno
133 6ae81775 ths
    if (initrd_size > 0) {
134 409dbce5 Aurelien Jarno
        snprintf((char *)params_buf + 8, 256, "rd_start=0x%" PRIx64 " rd_size=%li %s",
135 409dbce5 Aurelien Jarno
                 cpu_mips_phys_to_kseg0(NULL, initrd_offset),
136 e90e795e Aurelien Jarno
                 initrd_size, loaderparams.kernel_cmdline);
137 d7585251 pbrook
    } else {
138 e90e795e Aurelien Jarno
        snprintf((char *)params_buf + 8, 256, "%s", loaderparams.kernel_cmdline);
139 6ae81775 ths
    }
140 6ae81775 ths
141 e90e795e Aurelien Jarno
    rom_add_blob_fixed("params", params_buf, params_size,
142 e90e795e Aurelien Jarno
                       (16 << 20) - 264);
143 e90e795e Aurelien Jarno
144 e16ad5b0 Aurelien Jarno
    return entry;
145 6ae81775 ths
}
146 6ae81775 ths
147 6ae81775 ths
static void main_cpu_reset(void *opaque)
148 6ae81775 ths
{
149 e16ad5b0 Aurelien Jarno
    ResetData *s = (ResetData *)opaque;
150 e16ad5b0 Aurelien Jarno
    CPUState *env = s->env;
151 6ae81775 ths
152 e16ad5b0 Aurelien Jarno
    cpu_reset(env);
153 e16ad5b0 Aurelien Jarno
    env->active_tc.PC = s->vector;
154 6ae81775 ths
}
155 66a93e0f bellard
156 b305b5ba ths
static const int sector_len = 32 * 1024;
157 70705261 ths
static
158 c227f099 Anthony Liguori
void mips_r4k_init (ram_addr_t ram_size,
159 3023f332 aliguori
                    const char *boot_device,
160 6af0bf9c bellard
                    const char *kernel_filename, const char *kernel_cmdline,
161 94fc95cd j_mayer
                    const char *initrd_filename, const char *cpu_model)
162 6af0bf9c bellard
{
163 5cea8590 Paul Brook
    char *filename;
164 c227f099 Anthony Liguori
    ram_addr_t ram_offset;
165 c227f099 Anthony Liguori
    ram_addr_t bios_offset;
166 f7bcd4e3 ths
    int bios_size;
167 c68ea704 bellard
    CPUState *env;
168 e16ad5b0 Aurelien Jarno
    ResetData *reset_info;
169 1d914fa0 Isaku Yamahata
    ISADevice *rtc_state;
170 58126404 pbrook
    int i;
171 d537cf6c pbrook
    qemu_irq *i8259;
172 f455e98c Gerd Hoffmann
    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
173 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
174 3d08ff69 Blue Swirl
    int be;
175 c68ea704 bellard
176 33d68b5f ths
    /* init CPUs */
177 33d68b5f ths
    if (cpu_model == NULL) {
178 60aa19ab ths
#ifdef TARGET_MIPS64
179 33d68b5f ths
        cpu_model = "R4000";
180 33d68b5f ths
#else
181 1c32f43e ths
        cpu_model = "24Kf";
182 33d68b5f ths
#endif
183 33d68b5f ths
    }
184 aaed909a bellard
    env = cpu_init(cpu_model);
185 aaed909a bellard
    if (!env) {
186 aaed909a bellard
        fprintf(stderr, "Unable to find CPU definition\n");
187 aaed909a bellard
        exit(1);
188 aaed909a bellard
    }
189 e16ad5b0 Aurelien Jarno
    reset_info = qemu_mallocz(sizeof(ResetData));
190 e16ad5b0 Aurelien Jarno
    reset_info->env = env;
191 e16ad5b0 Aurelien Jarno
    reset_info->vector = env->active_tc.PC;
192 e16ad5b0 Aurelien Jarno
    qemu_register_reset(main_cpu_reset, reset_info);
193 c68ea704 bellard
194 6af0bf9c bellard
    /* allocate RAM */
195 0ccff151 aurel32
    if (ram_size > (256 << 20)) {
196 0ccff151 aurel32
        fprintf(stderr,
197 0ccff151 aurel32
                "qemu: Too much memory for this machine: %d MB, maximum 256 MB\n",
198 0ccff151 aurel32
                ((unsigned int)ram_size / (1 << 20)));
199 0ccff151 aurel32
        exit(1);
200 0ccff151 aurel32
    }
201 1724f049 Alex Williamson
    ram_offset = qemu_ram_alloc(NULL, "mips_r4k.ram", ram_size);
202 dcac9679 pbrook
203 dcac9679 pbrook
    cpu_register_physical_memory(0, ram_size, ram_offset | IO_MEM_RAM);
204 66a93e0f bellard
205 6ae81775 ths
    if (!mips_qemu_iomemtype) {
206 1eed09cb Avi Kivity
        mips_qemu_iomemtype = cpu_register_io_memory(mips_qemu_read,
207 33d68b5f ths
                                                     mips_qemu_write, NULL);
208 6ae81775 ths
    }
209 6ae81775 ths
    cpu_register_physical_memory(0x1fbf0000, 0x10000, mips_qemu_iomemtype);
210 6ae81775 ths
211 66a93e0f bellard
    /* Try to load a BIOS image. If this fails, we continue regardless,
212 66a93e0f bellard
       but initialize the hardware ourselves. When a kernel gets
213 66a93e0f bellard
       preloaded we also initialize the hardware, since the BIOS wasn't
214 66a93e0f bellard
       run. */
215 1192dad8 j_mayer
    if (bios_name == NULL)
216 1192dad8 j_mayer
        bios_name = BIOS_FILENAME;
217 5cea8590 Paul Brook
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
218 5cea8590 Paul Brook
    if (filename) {
219 5cea8590 Paul Brook
        bios_size = get_image_size(filename);
220 5cea8590 Paul Brook
    } else {
221 5cea8590 Paul Brook
        bios_size = -1;
222 5cea8590 Paul Brook
    }
223 3d08ff69 Blue Swirl
#ifdef TARGET_WORDS_BIGENDIAN
224 3d08ff69 Blue Swirl
    be = 1;
225 3d08ff69 Blue Swirl
#else
226 3d08ff69 Blue Swirl
    be = 0;
227 3d08ff69 Blue Swirl
#endif
228 2909b29a ths
    if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
229 1724f049 Alex Williamson
        bios_offset = qemu_ram_alloc(NULL, "mips_r4k.bios", BIOS_SIZE);
230 dcac9679 pbrook
        cpu_register_physical_memory(0x1fc00000, BIOS_SIZE,
231 dcac9679 pbrook
                                     bios_offset | IO_MEM_ROM);
232 dcac9679 pbrook
233 5cea8590 Paul Brook
        load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);
234 751c6a17 Gerd Hoffmann
    } else if ((dinfo = drive_get(IF_PFLASH, 0, 0)) != NULL) {
235 b305b5ba ths
        uint32_t mips_rom = 0x00400000;
236 1724f049 Alex Williamson
        bios_offset = qemu_ram_alloc(NULL, "mips_r4k.bios", mips_rom);
237 dcac9679 pbrook
        if (!pflash_cfi01_register(0x1fc00000, bios_offset,
238 3d08ff69 Blue Swirl
                                   dinfo->bdrv, sector_len,
239 3d08ff69 Blue Swirl
                                   mips_rom / sector_len,
240 3d08ff69 Blue Swirl
                                   4, 0, 0, 0, 0, be)) {
241 b305b5ba ths
            fprintf(stderr, "qemu: Error registering flash memory.\n");
242 b305b5ba ths
        }
243 b305b5ba ths
    }
244 b305b5ba ths
    else {
245 66a93e0f bellard
        /* not fatal */
246 66a93e0f bellard
        fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",
247 5cea8590 Paul Brook
                bios_name);
248 5cea8590 Paul Brook
    }
249 5cea8590 Paul Brook
    if (filename) {
250 5cea8590 Paul Brook
        qemu_free(filename);
251 6af0bf9c bellard
    }
252 66a93e0f bellard
253 66a93e0f bellard
    if (kernel_filename) {
254 7df526e3 ths
        loaderparams.ram_size = ram_size;
255 7df526e3 ths
        loaderparams.kernel_filename = kernel_filename;
256 7df526e3 ths
        loaderparams.kernel_cmdline = kernel_cmdline;
257 7df526e3 ths
        loaderparams.initrd_filename = initrd_filename;
258 e16ad5b0 Aurelien Jarno
        reset_info->vector = load_kernel();
259 6af0bf9c bellard
    }
260 6af0bf9c bellard
261 e16fe40c ths
    /* Init CPU internal devices */
262 d537cf6c pbrook
    cpu_mips_irq_init_cpu(env);
263 c68ea704 bellard
    cpu_mips_clock_init(env);
264 6af0bf9c bellard
265 d537cf6c pbrook
    /* The PIC is attached to the MIPS CPU INT0 pin */
266 d537cf6c pbrook
    i8259 = i8259_init(env->irq[2]);
267 11d23c35 Gerd Hoffmann
    isa_bus_new(NULL);
268 11d23c35 Gerd Hoffmann
    isa_bus_irqs(i8259);
269 d537cf6c pbrook
270 7d932dfd Jan Kiszka
    rtc_state = rtc_init(2000, NULL);
271 afdfa781 ths
272 0699b548 bellard
    /* Register 64 KB of ISA IO space at 0x14000000 */
273 84108e12 Blue Swirl
#ifdef TARGET_WORDS_BIGENDIAN
274 84108e12 Blue Swirl
    isa_mmio_init(0x14000000, 0x00010000, 1);
275 84108e12 Blue Swirl
#else
276 84108e12 Blue Swirl
    isa_mmio_init(0x14000000, 0x00010000, 0);
277 84108e12 Blue Swirl
#endif
278 0699b548 bellard
    isa_mem_base = 0x10000000;
279 0699b548 bellard
280 d537cf6c pbrook
    pit = pit_init(0x40, i8259[0]);
281 afdfa781 ths
282 eddbd288 ths
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
283 eddbd288 ths
        if (serial_hds[i]) {
284 ac0be998 Gerd Hoffmann
            serial_isa_init(i, serial_hds[i]);
285 eddbd288 ths
        }
286 eddbd288 ths
    }
287 eddbd288 ths
288 fbe1b595 Paul Brook
    isa_vga_init();
289 9827e95c bellard
290 0ae18cee aliguori
    if (nd_table[0].vlan)
291 9453c5bc Gerd Hoffmann
        isa_ne2000_init(0x300, 9, &nd_table[0]);
292 58126404 pbrook
293 e4bcb14c ths
    if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
294 e4bcb14c ths
        fprintf(stderr, "qemu: too many IDE bus\n");
295 e4bcb14c ths
        exit(1);
296 e4bcb14c ths
    }
297 e4bcb14c ths
298 e4bcb14c ths
    for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
299 f455e98c Gerd Hoffmann
        hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
300 e4bcb14c ths
    }
301 e4bcb14c ths
302 e4bcb14c ths
    for(i = 0; i < MAX_IDE_BUS; i++)
303 dea21e97 Gerd Hoffmann
        isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
304 e4bcb14c ths
                     hd[MAX_IDE_DEVS * i],
305 e4bcb14c ths
                     hd[MAX_IDE_DEVS * i + 1]);
306 70705261 ths
307 11d23c35 Gerd Hoffmann
    isa_create_simple("i8042");
308 6af0bf9c bellard
}
309 6af0bf9c bellard
310 f80f9ec9 Anthony Liguori
static QEMUMachine mips_machine = {
311 eec2743e ths
    .name = "mips",
312 eec2743e ths
    .desc = "mips r4k platform",
313 eec2743e ths
    .init = mips_r4k_init,
314 6af0bf9c bellard
};
315 f80f9ec9 Anthony Liguori
316 f80f9ec9 Anthony Liguori
static void mips_machine_init(void)
317 f80f9ec9 Anthony Liguori
{
318 f80f9ec9 Anthony Liguori
    qemu_register_machine(&mips_machine);
319 f80f9ec9 Anthony Liguori
}
320 f80f9ec9 Anthony Liguori
321 f80f9ec9 Anthony Liguori
machine_init(mips_machine_init);