Statistics
| Branch: | Revision:

root / hw / mips_r4k.c @ 6f3a7798

History | View | Annotate | Download (8.6 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 87ecb68b pbrook
#include "pc.h"
13 87ecb68b pbrook
#include "isa.h"
14 87ecb68b pbrook
#include "net.h"
15 87ecb68b pbrook
#include "sysemu.h"
16 87ecb68b pbrook
#include "boards.h"
17 b305b5ba ths
#include "flash.h"
18 3b3fb322 blueswir1
#include "qemu-log.h"
19 bba831e8 Paul Brook
#include "mips-bios.h"
20 ec82026c Gerd Hoffmann
#include "ide.h"
21 ca20cf32 Blue Swirl
#include "loader.h"
22 ca20cf32 Blue Swirl
#include "elf.h"
23 44cbbf18 ths
24 c6ee607c pbrook
#define PHYS_TO_VIRT(x) ((x) | ~(target_ulong)0x7fffffff)
25 6af0bf9c bellard
26 5dc4b744 ths
#define VIRT_TO_PHYS_ADDEND (-((int64_t)(int32_t)0x80000000))
27 66a93e0f bellard
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 eddbd288 ths
static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
35 eddbd288 ths
static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
36 eddbd288 ths
37 e16fe40c ths
static PITState *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 c227f099 Anthony Liguori
static void mips_qemu_writel (void *opaque, target_phys_addr_t addr,
49 6ae81775 ths
                              uint32_t val)
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 c227f099 Anthony Liguori
static uint32_t mips_qemu_readl (void *opaque, target_phys_addr_t addr)
58 6ae81775 ths
{
59 6ae81775 ths
    return 0;
60 6ae81775 ths
}
61 6ae81775 ths
62 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const mips_qemu_write[] = {
63 6ae81775 ths
    &mips_qemu_writel,
64 6ae81775 ths
    &mips_qemu_writel,
65 6ae81775 ths
    &mips_qemu_writel,
66 6ae81775 ths
};
67 6ae81775 ths
68 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const mips_qemu_read[] = {
69 6ae81775 ths
    &mips_qemu_readl,
70 6ae81775 ths
    &mips_qemu_readl,
71 6ae81775 ths
    &mips_qemu_readl,
72 6ae81775 ths
};
73 6ae81775 ths
74 6ae81775 ths
static int mips_qemu_iomemtype = 0;
75 6ae81775 ths
76 7df526e3 ths
static void load_kernel (CPUState *env)
77 6ae81775 ths
{
78 74287114 ths
    int64_t entry, kernel_low, kernel_high;
79 6ae81775 ths
    long kernel_size, initrd_size;
80 c227f099 Anthony Liguori
    ram_addr_t initrd_offset;
81 d7585251 pbrook
    int ret;
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 7df526e3 ths
    kernel_size = load_elf(loaderparams.kernel_filename, VIRT_TO_PHYS_ADDEND,
90 b55266b5 blueswir1
                           (uint64_t *)&entry, (uint64_t *)&kernel_low,
91 ca20cf32 Blue Swirl
                           (uint64_t *)&kernel_high, big_endian, ELF_MACHINE, 1);
92 c570fd16 ths
    if (kernel_size >= 0) {
93 c570fd16 ths
        if ((entry & ~0x7fffffffULL) == 0x80000000)
94 5dc4b744 ths
            entry = (int32_t)entry;
95 b5dc7732 ths
        env->active_tc.PC = 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 6ae81775 ths
    if (initrd_size > 0) {
128 d7585251 pbrook
        char buf[64];
129 d7585251 pbrook
        ret = snprintf(buf, 64, "rd_start=0x" TARGET_FMT_lx " rd_size=%li ",
130 d7585251 pbrook
                       PHYS_TO_VIRT((uint32_t)initrd_offset),
131 d7585251 pbrook
                       initrd_size);
132 d7585251 pbrook
        cpu_physical_memory_write((16 << 20) - 256, (void *)buf, 64);
133 d7585251 pbrook
    } else {
134 d7585251 pbrook
        ret = 0;
135 6ae81775 ths
    }
136 d7585251 pbrook
    pstrcpy_targphys((16 << 20) - 256 + ret, 256,
137 d7585251 pbrook
                     loaderparams.kernel_cmdline);
138 6ae81775 ths
139 d7585251 pbrook
    stl_phys((16 << 20) - 260, 0x12345678);
140 d7585251 pbrook
    stl_phys((16 << 20) - 264, ram_size);
141 6ae81775 ths
}
142 6ae81775 ths
143 6ae81775 ths
static void main_cpu_reset(void *opaque)
144 6ae81775 ths
{
145 6ae81775 ths
    CPUState *env = opaque;
146 6ae81775 ths
    cpu_reset(env);
147 6ae81775 ths
148 7df526e3 ths
    if (loaderparams.kernel_filename)
149 7df526e3 ths
        load_kernel (env);
150 6ae81775 ths
}
151 66a93e0f bellard
152 b305b5ba ths
static const int sector_len = 32 * 1024;
153 70705261 ths
static
154 c227f099 Anthony Liguori
void mips_r4k_init (ram_addr_t ram_size,
155 3023f332 aliguori
                    const char *boot_device,
156 6af0bf9c bellard
                    const char *kernel_filename, const char *kernel_cmdline,
157 94fc95cd j_mayer
                    const char *initrd_filename, const char *cpu_model)
158 6af0bf9c bellard
{
159 5cea8590 Paul Brook
    char *filename;
160 c227f099 Anthony Liguori
    ram_addr_t ram_offset;
161 c227f099 Anthony Liguori
    ram_addr_t bios_offset;
162 f7bcd4e3 ths
    int bios_size;
163 c68ea704 bellard
    CPUState *env;
164 153a08db ths
    RTCState *rtc_state;
165 58126404 pbrook
    int i;
166 d537cf6c pbrook
    qemu_irq *i8259;
167 f455e98c Gerd Hoffmann
    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
168 751c6a17 Gerd Hoffmann
    DriveInfo *dinfo;
169 c68ea704 bellard
170 33d68b5f ths
    /* init CPUs */
171 33d68b5f ths
    if (cpu_model == NULL) {
172 60aa19ab ths
#ifdef TARGET_MIPS64
173 33d68b5f ths
        cpu_model = "R4000";
174 33d68b5f ths
#else
175 1c32f43e ths
        cpu_model = "24Kf";
176 33d68b5f ths
#endif
177 33d68b5f ths
    }
178 aaed909a bellard
    env = cpu_init(cpu_model);
179 aaed909a bellard
    if (!env) {
180 aaed909a bellard
        fprintf(stderr, "Unable to find CPU definition\n");
181 aaed909a bellard
        exit(1);
182 aaed909a bellard
    }
183 a08d4367 Jan Kiszka
    qemu_register_reset(main_cpu_reset, env);
184 c68ea704 bellard
185 6af0bf9c bellard
    /* allocate RAM */
186 0ccff151 aurel32
    if (ram_size > (256 << 20)) {
187 0ccff151 aurel32
        fprintf(stderr,
188 0ccff151 aurel32
                "qemu: Too much memory for this machine: %d MB, maximum 256 MB\n",
189 0ccff151 aurel32
                ((unsigned int)ram_size / (1 << 20)));
190 0ccff151 aurel32
        exit(1);
191 0ccff151 aurel32
    }
192 dcac9679 pbrook
    ram_offset = qemu_ram_alloc(ram_size);
193 dcac9679 pbrook
194 dcac9679 pbrook
    cpu_register_physical_memory(0, ram_size, ram_offset | IO_MEM_RAM);
195 66a93e0f bellard
196 6ae81775 ths
    if (!mips_qemu_iomemtype) {
197 1eed09cb Avi Kivity
        mips_qemu_iomemtype = cpu_register_io_memory(mips_qemu_read,
198 33d68b5f ths
                                                     mips_qemu_write, NULL);
199 6ae81775 ths
    }
200 6ae81775 ths
    cpu_register_physical_memory(0x1fbf0000, 0x10000, mips_qemu_iomemtype);
201 6ae81775 ths
202 66a93e0f bellard
    /* Try to load a BIOS image. If this fails, we continue regardless,
203 66a93e0f bellard
       but initialize the hardware ourselves. When a kernel gets
204 66a93e0f bellard
       preloaded we also initialize the hardware, since the BIOS wasn't
205 66a93e0f bellard
       run. */
206 1192dad8 j_mayer
    if (bios_name == NULL)
207 1192dad8 j_mayer
        bios_name = BIOS_FILENAME;
208 5cea8590 Paul Brook
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
209 5cea8590 Paul Brook
    if (filename) {
210 5cea8590 Paul Brook
        bios_size = get_image_size(filename);
211 5cea8590 Paul Brook
    } else {
212 5cea8590 Paul Brook
        bios_size = -1;
213 5cea8590 Paul Brook
    }
214 2909b29a ths
    if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
215 dcac9679 pbrook
        bios_offset = qemu_ram_alloc(BIOS_SIZE);
216 dcac9679 pbrook
        cpu_register_physical_memory(0x1fc00000, BIOS_SIZE,
217 dcac9679 pbrook
                                     bios_offset | IO_MEM_ROM);
218 dcac9679 pbrook
219 5cea8590 Paul Brook
        load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);
220 751c6a17 Gerd Hoffmann
    } else if ((dinfo = drive_get(IF_PFLASH, 0, 0)) != NULL) {
221 b305b5ba ths
        uint32_t mips_rom = 0x00400000;
222 dcac9679 pbrook
        bios_offset = qemu_ram_alloc(mips_rom);
223 dcac9679 pbrook
        if (!pflash_cfi01_register(0x1fc00000, bios_offset,
224 751c6a17 Gerd Hoffmann
            dinfo->bdrv, sector_len, mips_rom / sector_len,
225 b305b5ba ths
            4, 0, 0, 0, 0)) {
226 b305b5ba ths
            fprintf(stderr, "qemu: Error registering flash memory.\n");
227 b305b5ba ths
        }
228 b305b5ba ths
    }
229 b305b5ba ths
    else {
230 66a93e0f bellard
        /* not fatal */
231 66a93e0f bellard
        fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",
232 5cea8590 Paul Brook
                bios_name);
233 5cea8590 Paul Brook
    }
234 5cea8590 Paul Brook
    if (filename) {
235 5cea8590 Paul Brook
        qemu_free(filename);
236 6af0bf9c bellard
    }
237 66a93e0f bellard
238 66a93e0f bellard
    if (kernel_filename) {
239 7df526e3 ths
        loaderparams.ram_size = ram_size;
240 7df526e3 ths
        loaderparams.kernel_filename = kernel_filename;
241 7df526e3 ths
        loaderparams.kernel_cmdline = kernel_cmdline;
242 7df526e3 ths
        loaderparams.initrd_filename = initrd_filename;
243 7df526e3 ths
        load_kernel (env);
244 6af0bf9c bellard
    }
245 6af0bf9c bellard
246 e16fe40c ths
    /* Init CPU internal devices */
247 d537cf6c pbrook
    cpu_mips_irq_init_cpu(env);
248 c68ea704 bellard
    cpu_mips_clock_init(env);
249 6af0bf9c bellard
250 d537cf6c pbrook
    /* The PIC is attached to the MIPS CPU INT0 pin */
251 d537cf6c pbrook
    i8259 = i8259_init(env->irq[2]);
252 11d23c35 Gerd Hoffmann
    isa_bus_new(NULL);
253 11d23c35 Gerd Hoffmann
    isa_bus_irqs(i8259);
254 d537cf6c pbrook
255 32e0c826 Gerd Hoffmann
    rtc_state = rtc_init(2000);
256 afdfa781 ths
257 0699b548 bellard
    /* Register 64 KB of ISA IO space at 0x14000000 */
258 aef445bd pbrook
    isa_mmio_init(0x14000000, 0x00010000);
259 0699b548 bellard
    isa_mem_base = 0x10000000;
260 0699b548 bellard
261 d537cf6c pbrook
    pit = pit_init(0x40, i8259[0]);
262 afdfa781 ths
263 eddbd288 ths
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
264 eddbd288 ths
        if (serial_hds[i]) {
265 b6cd0ea1 aurel32
            serial_init(serial_io[i], i8259[serial_irq[i]], 115200,
266 b6cd0ea1 aurel32
                        serial_hds[i]);
267 eddbd288 ths
        }
268 eddbd288 ths
    }
269 eddbd288 ths
270 fbe1b595 Paul Brook
    isa_vga_init();
271 9827e95c bellard
272 0ae18cee aliguori
    if (nd_table[0].vlan)
273 9453c5bc Gerd Hoffmann
        isa_ne2000_init(0x300, 9, &nd_table[0]);
274 58126404 pbrook
275 e4bcb14c ths
    if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
276 e4bcb14c ths
        fprintf(stderr, "qemu: too many IDE bus\n");
277 e4bcb14c ths
        exit(1);
278 e4bcb14c ths
    }
279 e4bcb14c ths
280 e4bcb14c ths
    for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
281 f455e98c Gerd Hoffmann
        hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
282 e4bcb14c ths
    }
283 e4bcb14c ths
284 e4bcb14c ths
    for(i = 0; i < MAX_IDE_BUS; i++)
285 dea21e97 Gerd Hoffmann
        isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
286 e4bcb14c ths
                     hd[MAX_IDE_DEVS * i],
287 e4bcb14c ths
                     hd[MAX_IDE_DEVS * i + 1]);
288 70705261 ths
289 11d23c35 Gerd Hoffmann
    isa_create_simple("i8042");
290 6af0bf9c bellard
}
291 6af0bf9c bellard
292 f80f9ec9 Anthony Liguori
static QEMUMachine mips_machine = {
293 eec2743e ths
    .name = "mips",
294 eec2743e ths
    .desc = "mips r4k platform",
295 eec2743e ths
    .init = mips_r4k_init,
296 6af0bf9c bellard
};
297 f80f9ec9 Anthony Liguori
298 f80f9ec9 Anthony Liguori
static void mips_machine_init(void)
299 f80f9ec9 Anthony Liguori
{
300 f80f9ec9 Anthony Liguori
    qemu_register_machine(&mips_machine);
301 f80f9ec9 Anthony Liguori
}
302 f80f9ec9 Anthony Liguori
303 f80f9ec9 Anthony Liguori
machine_init(mips_machine_init);