Statistics
| Branch: | Revision:

root / hw / mips_r4k.c @ be62a2eb

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