Statistics
| Branch: | Revision:

root / hw / mips_r4k.c @ e8b54394

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