Statistics
| Branch: | Revision:

root / hw / mips / mips_r4k.c @ bf0da4df

History | View | Annotate | Download (9.4 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 83c9f4ca Paolo Bonzini
#include "hw/hw.h"
11 0d09e41a Paolo Bonzini
#include "hw/mips/mips.h"
12 0d09e41a Paolo Bonzini
#include "hw/mips/cpudevs.h"
13 0d09e41a Paolo Bonzini
#include "hw/i386/pc.h"
14 0d09e41a Paolo Bonzini
#include "hw/char/serial.h"
15 0d09e41a Paolo Bonzini
#include "hw/isa/isa.h"
16 1422e32d Paolo Bonzini
#include "net/net.h"
17 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
18 83c9f4ca Paolo Bonzini
#include "hw/boards.h"
19 0d09e41a Paolo Bonzini
#include "hw/block/flash.h"
20 1de7afc9 Paolo Bonzini
#include "qemu/log.h"
21 0d09e41a Paolo Bonzini
#include "hw/mips/bios.h"
22 83c9f4ca Paolo Bonzini
#include "hw/ide.h"
23 83c9f4ca Paolo Bonzini
#include "hw/loader.h"
24 ca20cf32 Blue Swirl
#include "elf.h"
25 0d09e41a Paolo Bonzini
#include "hw/timer/mc146818rtc.h"
26 0d09e41a Paolo Bonzini
#include "hw/timer/i8254.h"
27 9c17d615 Paolo Bonzini
#include "sysemu/blockdev.h"
28 022c62cb Paolo Bonzini
#include "exec/address-spaces.h"
29 c9dd6a9f Andreas Färber
#include "sysemu/qtest.h"
30 44cbbf18 ths
31 e4bcb14c ths
#define MAX_IDE_BUS 2
32 e4bcb14c ths
33 58126404 pbrook
static const int ide_iobase[2] = { 0x1f0, 0x170 };
34 58126404 pbrook
static const int ide_iobase2[2] = { 0x3f6, 0x376 };
35 58126404 pbrook
static const int ide_irq[2] = { 14, 15 };
36 58126404 pbrook
37 64d7e9a4 Blue Swirl
static ISADevice *pit; /* PIT i8254 */
38 697584ab bellard
39 1b66074b ths
/* i8254 PIT is attached to the IRQ0 at PIC i8259 */
40 6af0bf9c bellard
41 7df526e3 ths
static struct _loaderparams {
42 7df526e3 ths
    int ram_size;
43 7df526e3 ths
    const char *kernel_filename;
44 7df526e3 ths
    const char *kernel_cmdline;
45 7df526e3 ths
    const char *initrd_filename;
46 7df526e3 ths
} loaderparams;
47 7df526e3 ths
48 a8170e5e Avi Kivity
static void mips_qemu_write (void *opaque, hwaddr addr,
49 0ae16450 Avi Kivity
                             uint64_t val, unsigned size)
50 6ae81775 ths
{
51 6ae81775 ths
    if ((addr & 0xffff) == 0 && val == 42)
52 6ae81775 ths
        qemu_system_reset_request ();
53 6ae81775 ths
    else if ((addr & 0xffff) == 4 && val == 42)
54 6ae81775 ths
        qemu_system_shutdown_request ();
55 6ae81775 ths
}
56 6ae81775 ths
57 a8170e5e Avi Kivity
static uint64_t mips_qemu_read (void *opaque, hwaddr addr,
58 0ae16450 Avi Kivity
                                unsigned size)
59 6ae81775 ths
{
60 6ae81775 ths
    return 0;
61 6ae81775 ths
}
62 6ae81775 ths
63 0ae16450 Avi Kivity
static const MemoryRegionOps mips_qemu_ops = {
64 0ae16450 Avi Kivity
    .read = mips_qemu_read,
65 0ae16450 Avi Kivity
    .write = mips_qemu_write,
66 0ae16450 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
67 6ae81775 ths
};
68 6ae81775 ths
69 e16ad5b0 Aurelien Jarno
typedef struct ResetData {
70 fa156e51 Andreas Färber
    MIPSCPU *cpu;
71 e16ad5b0 Aurelien Jarno
    uint64_t vector;
72 e16ad5b0 Aurelien Jarno
} ResetData;
73 e16ad5b0 Aurelien Jarno
74 e16ad5b0 Aurelien Jarno
static int64_t load_kernel(void)
75 6ae81775 ths
{
76 409dbce5 Aurelien Jarno
    int64_t entry, kernel_high;
77 e90e795e Aurelien Jarno
    long kernel_size, initrd_size, params_size;
78 c227f099 Anthony Liguori
    ram_addr_t initrd_offset;
79 e90e795e Aurelien Jarno
    uint32_t *params_buf;
80 ca20cf32 Blue Swirl
    int big_endian;
81 6ae81775 ths
82 ca20cf32 Blue Swirl
#ifdef TARGET_WORDS_BIGENDIAN
83 ca20cf32 Blue Swirl
    big_endian = 1;
84 ca20cf32 Blue Swirl
#else
85 ca20cf32 Blue Swirl
    big_endian = 0;
86 ca20cf32 Blue Swirl
#endif
87 409dbce5 Aurelien Jarno
    kernel_size = load_elf(loaderparams.kernel_filename, cpu_mips_kseg0_to_phys,
88 409dbce5 Aurelien Jarno
                           NULL, (uint64_t *)&entry, NULL,
89 409dbce5 Aurelien Jarno
                           (uint64_t *)&kernel_high, big_endian,
90 409dbce5 Aurelien Jarno
                           ELF_MACHINE, 1);
91 c570fd16 ths
    if (kernel_size >= 0) {
92 c570fd16 ths
        if ((entry & ~0x7fffffffULL) == 0x80000000)
93 5dc4b744 ths
            entry = (int32_t)entry;
94 c570fd16 ths
    } else {
95 9042c0e2 ths
        fprintf(stderr, "qemu: could not load kernel '%s'\n",
96 7df526e3 ths
                loaderparams.kernel_filename);
97 9042c0e2 ths
        exit(1);
98 6ae81775 ths
    }
99 6ae81775 ths
100 6ae81775 ths
    /* load initrd */
101 6ae81775 ths
    initrd_size = 0;
102 74287114 ths
    initrd_offset = 0;
103 7df526e3 ths
    if (loaderparams.initrd_filename) {
104 7df526e3 ths
        initrd_size = get_image_size (loaderparams.initrd_filename);
105 74287114 ths
        if (initrd_size > 0) {
106 05b3274b James Hogan
            initrd_offset = (kernel_high + ~INITRD_PAGE_MASK) & INITRD_PAGE_MASK;
107 74287114 ths
            if (initrd_offset + initrd_size > ram_size) {
108 74287114 ths
                fprintf(stderr,
109 74287114 ths
                        "qemu: memory too small for initial ram disk '%s'\n",
110 7df526e3 ths
                        loaderparams.initrd_filename);
111 74287114 ths
                exit(1);
112 74287114 ths
            }
113 dcac9679 pbrook
            initrd_size = load_image_targphys(loaderparams.initrd_filename,
114 dcac9679 pbrook
                                              initrd_offset,
115 dcac9679 pbrook
                                              ram_size - initrd_offset);
116 74287114 ths
        }
117 6ae81775 ths
        if (initrd_size == (target_ulong) -1) {
118 6ae81775 ths
            fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
119 7df526e3 ths
                    loaderparams.initrd_filename);
120 6ae81775 ths
            exit(1);
121 6ae81775 ths
        }
122 6ae81775 ths
    }
123 6ae81775 ths
124 6ae81775 ths
    /* Store command line.  */
125 e90e795e Aurelien Jarno
    params_size = 264;
126 7267c094 Anthony Liguori
    params_buf = g_malloc(params_size);
127 e90e795e Aurelien Jarno
128 e90e795e Aurelien Jarno
    params_buf[0] = tswap32(ram_size);
129 e90e795e Aurelien Jarno
    params_buf[1] = tswap32(0x12345678);
130 e90e795e Aurelien Jarno
131 6ae81775 ths
    if (initrd_size > 0) {
132 409dbce5 Aurelien Jarno
        snprintf((char *)params_buf + 8, 256, "rd_start=0x%" PRIx64 " rd_size=%li %s",
133 409dbce5 Aurelien Jarno
                 cpu_mips_phys_to_kseg0(NULL, initrd_offset),
134 e90e795e Aurelien Jarno
                 initrd_size, loaderparams.kernel_cmdline);
135 d7585251 pbrook
    } else {
136 e90e795e Aurelien Jarno
        snprintf((char *)params_buf + 8, 256, "%s", loaderparams.kernel_cmdline);
137 6ae81775 ths
    }
138 6ae81775 ths
139 e90e795e Aurelien Jarno
    rom_add_blob_fixed("params", params_buf, params_size,
140 e90e795e Aurelien Jarno
                       (16 << 20) - 264);
141 e90e795e Aurelien Jarno
142 e16ad5b0 Aurelien Jarno
    return entry;
143 6ae81775 ths
}
144 6ae81775 ths
145 6ae81775 ths
static void main_cpu_reset(void *opaque)
146 6ae81775 ths
{
147 e16ad5b0 Aurelien Jarno
    ResetData *s = (ResetData *)opaque;
148 fa156e51 Andreas Färber
    CPUMIPSState *env = &s->cpu->env;
149 6ae81775 ths
150 fa156e51 Andreas Färber
    cpu_reset(CPU(s->cpu));
151 e16ad5b0 Aurelien Jarno
    env->active_tc.PC = s->vector;
152 6ae81775 ths
}
153 66a93e0f bellard
154 b305b5ba ths
static const int sector_len = 32 * 1024;
155 70705261 ths
static
156 5f072e1f Eduardo Habkost
void mips_r4k_init(QEMUMachineInitArgs *args)
157 6af0bf9c bellard
{
158 5f072e1f Eduardo Habkost
    ram_addr_t ram_size = args->ram_size;
159 5f072e1f Eduardo Habkost
    const char *cpu_model = args->cpu_model;
160 5f072e1f Eduardo Habkost
    const char *kernel_filename = args->kernel_filename;
161 5f072e1f Eduardo Habkost
    const char *kernel_cmdline = args->kernel_cmdline;
162 5f072e1f Eduardo Habkost
    const char *initrd_filename = args->initrd_filename;
163 5cea8590 Paul Brook
    char *filename;
164 0ae16450 Avi Kivity
    MemoryRegion *address_space_mem = get_system_memory();
165 0ae16450 Avi Kivity
    MemoryRegion *ram = g_new(MemoryRegion, 1);
166 cfe5f011 Avi Kivity
    MemoryRegion *bios;
167 0ae16450 Avi Kivity
    MemoryRegion *iomem = g_new(MemoryRegion, 1);
168 39594968 Paolo Bonzini
    MemoryRegion *isa = g_new(MemoryRegion, 1);
169 f7bcd4e3 ths
    int bios_size;
170 9ac67e21 Andreas Färber
    MIPSCPU *cpu;
171 61c56c8c Andreas Färber
    CPUMIPSState *env;
172 e16ad5b0 Aurelien Jarno
    ResetData *reset_info;
173 58126404 pbrook
    int i;
174 d537cf6c pbrook
    qemu_irq *i8259;
175 48a18b3c Hervé Poussineau
    ISABus *isa_bus;
176 f455e98c Gerd Hoffmann
    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
177 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
178 3d08ff69 Blue Swirl
    int be;
179 c68ea704 bellard
180 33d68b5f ths
    /* init CPUs */
181 33d68b5f ths
    if (cpu_model == NULL) {
182 60aa19ab ths
#ifdef TARGET_MIPS64
183 33d68b5f ths
        cpu_model = "R4000";
184 33d68b5f ths
#else
185 1c32f43e ths
        cpu_model = "24Kf";
186 33d68b5f ths
#endif
187 33d68b5f ths
    }
188 9ac67e21 Andreas Färber
    cpu = cpu_mips_init(cpu_model);
189 9ac67e21 Andreas Färber
    if (cpu == NULL) {
190 aaed909a bellard
        fprintf(stderr, "Unable to find CPU definition\n");
191 aaed909a bellard
        exit(1);
192 aaed909a bellard
    }
193 9ac67e21 Andreas Färber
    env = &cpu->env;
194 9ac67e21 Andreas Färber
195 7267c094 Anthony Liguori
    reset_info = g_malloc0(sizeof(ResetData));
196 fa156e51 Andreas Färber
    reset_info->cpu = cpu;
197 e16ad5b0 Aurelien Jarno
    reset_info->vector = env->active_tc.PC;
198 e16ad5b0 Aurelien Jarno
    qemu_register_reset(main_cpu_reset, reset_info);
199 c68ea704 bellard
200 6af0bf9c bellard
    /* allocate RAM */
201 0ccff151 aurel32
    if (ram_size > (256 << 20)) {
202 0ccff151 aurel32
        fprintf(stderr,
203 0ccff151 aurel32
                "qemu: Too much memory for this machine: %d MB, maximum 256 MB\n",
204 0ccff151 aurel32
                ((unsigned int)ram_size / (1 << 20)));
205 0ccff151 aurel32
        exit(1);
206 0ccff151 aurel32
    }
207 2c9b15ca Paolo Bonzini
    memory_region_init_ram(ram, NULL, "mips_r4k.ram", ram_size);
208 c5705a77 Avi Kivity
    vmstate_register_ram_global(ram);
209 dcac9679 pbrook
210 0ae16450 Avi Kivity
    memory_region_add_subregion(address_space_mem, 0, ram);
211 66a93e0f bellard
212 2c9b15ca Paolo Bonzini
    memory_region_init_io(iomem, NULL, &mips_qemu_ops, NULL, "mips-qemu", 0x10000);
213 0ae16450 Avi Kivity
    memory_region_add_subregion(address_space_mem, 0x1fbf0000, iomem);
214 6ae81775 ths
215 66a93e0f bellard
    /* Try to load a BIOS image. If this fails, we continue regardless,
216 66a93e0f bellard
       but initialize the hardware ourselves. When a kernel gets
217 66a93e0f bellard
       preloaded we also initialize the hardware, since the BIOS wasn't
218 66a93e0f bellard
       run. */
219 1192dad8 j_mayer
    if (bios_name == NULL)
220 1192dad8 j_mayer
        bios_name = BIOS_FILENAME;
221 5cea8590 Paul Brook
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
222 5cea8590 Paul Brook
    if (filename) {
223 5cea8590 Paul Brook
        bios_size = get_image_size(filename);
224 5cea8590 Paul Brook
    } else {
225 5cea8590 Paul Brook
        bios_size = -1;
226 5cea8590 Paul Brook
    }
227 3d08ff69 Blue Swirl
#ifdef TARGET_WORDS_BIGENDIAN
228 3d08ff69 Blue Swirl
    be = 1;
229 3d08ff69 Blue Swirl
#else
230 3d08ff69 Blue Swirl
    be = 0;
231 3d08ff69 Blue Swirl
#endif
232 2909b29a ths
    if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
233 cfe5f011 Avi Kivity
        bios = g_new(MemoryRegion, 1);
234 2c9b15ca Paolo Bonzini
        memory_region_init_ram(bios, NULL, "mips_r4k.bios", BIOS_SIZE);
235 c5705a77 Avi Kivity
        vmstate_register_ram_global(bios);
236 cfe5f011 Avi Kivity
        memory_region_set_readonly(bios, true);
237 cfe5f011 Avi Kivity
        memory_region_add_subregion(get_system_memory(), 0x1fc00000, bios);
238 01e0451a Anthony Liguori
239 5cea8590 Paul Brook
        load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);
240 751c6a17 Gerd Hoffmann
    } else if ((dinfo = drive_get(IF_PFLASH, 0, 0)) != NULL) {
241 b305b5ba ths
        uint32_t mips_rom = 0x00400000;
242 cfe5f011 Avi Kivity
        if (!pflash_cfi01_register(0x1fc00000, NULL, "mips_r4k.bios", mips_rom,
243 3d08ff69 Blue Swirl
                                   dinfo->bdrv, sector_len,
244 3d08ff69 Blue Swirl
                                   mips_rom / sector_len,
245 01e0451a Anthony Liguori
                                   4, 0, 0, 0, 0, be)) {
246 b305b5ba ths
            fprintf(stderr, "qemu: Error registering flash memory.\n");
247 b305b5ba ths
        }
248 c9dd6a9f Andreas Färber
    } else if (!qtest_enabled()) {
249 66a93e0f bellard
        /* not fatal */
250 66a93e0f bellard
        fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",
251 5cea8590 Paul Brook
                bios_name);
252 5cea8590 Paul Brook
    }
253 5cea8590 Paul Brook
    if (filename) {
254 7267c094 Anthony Liguori
        g_free(filename);
255 6af0bf9c bellard
    }
256 66a93e0f bellard
257 66a93e0f bellard
    if (kernel_filename) {
258 7df526e3 ths
        loaderparams.ram_size = ram_size;
259 7df526e3 ths
        loaderparams.kernel_filename = kernel_filename;
260 7df526e3 ths
        loaderparams.kernel_cmdline = kernel_cmdline;
261 7df526e3 ths
        loaderparams.initrd_filename = initrd_filename;
262 e16ad5b0 Aurelien Jarno
        reset_info->vector = load_kernel();
263 6af0bf9c bellard
    }
264 6af0bf9c bellard
265 e16fe40c ths
    /* Init CPU internal devices */
266 d537cf6c pbrook
    cpu_mips_irq_init_cpu(env);
267 c68ea704 bellard
    cpu_mips_clock_init(env);
268 6af0bf9c bellard
269 d537cf6c pbrook
    /* The PIC is attached to the MIPS CPU INT0 pin */
270 48a18b3c Hervé Poussineau
    isa_bus = isa_bus_new(NULL, get_system_io());
271 48a18b3c Hervé Poussineau
    i8259 = i8259_init(isa_bus, env->irq[2]);
272 48a18b3c Hervé Poussineau
    isa_bus_irqs(isa_bus, i8259);
273 d537cf6c pbrook
274 48a18b3c Hervé Poussineau
    rtc_init(isa_bus, 2000, NULL);
275 afdfa781 ths
276 0699b548 bellard
    /* Register 64 KB of ISA IO space at 0x14000000 */
277 39594968 Paolo Bonzini
    memory_region_init_alias(isa, NULL, "isa_mmio",
278 39594968 Paolo Bonzini
                             get_system_io(), 0, 0x00010000);
279 39594968 Paolo Bonzini
    memory_region_add_subregion(get_system_memory(), 0x14000000, isa);
280 39594968 Paolo Bonzini
281 0699b548 bellard
    isa_mem_base = 0x10000000;
282 0699b548 bellard
283 319ba9f5 Jan Kiszka
    pit = pit_init(isa_bus, 0x40, 0, NULL);
284 afdfa781 ths
285 eddbd288 ths
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
286 eddbd288 ths
        if (serial_hds[i]) {
287 48a18b3c Hervé Poussineau
            serial_isa_init(isa_bus, i, serial_hds[i]);
288 eddbd288 ths
        }
289 eddbd288 ths
    }
290 eddbd288 ths
291 f642dfce Aurelien Jarno
    isa_vga_init(isa_bus);
292 9827e95c bellard
293 a005d073 Stefan Hajnoczi
    if (nd_table[0].used)
294 48a18b3c Hervé Poussineau
        isa_ne2000_init(isa_bus, 0x300, 9, &nd_table[0]);
295 58126404 pbrook
296 75717903 Isaku Yamahata
    ide_drive_get(hd, MAX_IDE_BUS);
297 e4bcb14c ths
    for(i = 0; i < MAX_IDE_BUS; i++)
298 48a18b3c Hervé Poussineau
        isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
299 e4bcb14c ths
                     hd[MAX_IDE_DEVS * i],
300 e4bcb14c ths
                     hd[MAX_IDE_DEVS * i + 1]);
301 70705261 ths
302 48a18b3c Hervé Poussineau
    isa_create_simple(isa_bus, "i8042");
303 6af0bf9c bellard
}
304 6af0bf9c bellard
305 f80f9ec9 Anthony Liguori
static QEMUMachine mips_machine = {
306 eec2743e ths
    .name = "mips",
307 eec2743e ths
    .desc = "mips r4k platform",
308 eec2743e ths
    .init = mips_r4k_init,
309 e4ada29e Avik Sil
    DEFAULT_MACHINE_OPTIONS,
310 6af0bf9c bellard
};
311 f80f9ec9 Anthony Liguori
312 f80f9ec9 Anthony Liguori
static void mips_machine_init(void)
313 f80f9ec9 Anthony Liguori
{
314 f80f9ec9 Anthony Liguori
    qemu_register_machine(&mips_machine);
315 f80f9ec9 Anthony Liguori
}
316 f80f9ec9 Anthony Liguori
317 f80f9ec9 Anthony Liguori
machine_init(mips_machine_init);