Statistics
| Branch: | Revision:

root / hw / mips_r4k.c @ c6ee607c

History | View | Annotate | Download (7.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 6af0bf9c bellard
#include "vl.h"
11 6af0bf9c bellard
12 2909b29a ths
#ifdef TARGET_WORDS_BIGENDIAN
13 6af0bf9c bellard
#define BIOS_FILENAME "mips_bios.bin"
14 f7bcd4e3 ths
#else
15 f7bcd4e3 ths
#define BIOS_FILENAME "mipsel_bios.bin"
16 f7bcd4e3 ths
#endif
17 44cbbf18 ths
18 c6ee607c pbrook
#define PHYS_TO_VIRT(x) ((x) | ~(target_ulong)0x7fffffff)
19 6af0bf9c bellard
20 5dc4b744 ths
#define VIRT_TO_PHYS_ADDEND (-((int64_t)(int32_t)0x80000000))
21 66a93e0f bellard
22 58126404 pbrook
static const int ide_iobase[2] = { 0x1f0, 0x170 };
23 58126404 pbrook
static const int ide_iobase2[2] = { 0x3f6, 0x376 };
24 58126404 pbrook
static const int ide_irq[2] = { 14, 15 };
25 58126404 pbrook
26 eddbd288 ths
static int serial_io[MAX_SERIAL_PORTS] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
27 eddbd288 ths
static int serial_irq[MAX_SERIAL_PORTS] = { 4, 3, 4, 3 };
28 eddbd288 ths
29 6af0bf9c bellard
extern FILE *logfile;
30 6af0bf9c bellard
31 e16fe40c ths
static PITState *pit; /* PIT i8254 */
32 697584ab bellard
33 e16fe40c ths
/*i8254 PIT is attached to the IRQ0 at PIC i8259 */
34 6af0bf9c bellard
35 7df526e3 ths
static struct _loaderparams {
36 7df526e3 ths
    int ram_size;
37 7df526e3 ths
    const char *kernel_filename;
38 7df526e3 ths
    const char *kernel_cmdline;
39 7df526e3 ths
    const char *initrd_filename;
40 7df526e3 ths
} loaderparams;
41 7df526e3 ths
42 6ae81775 ths
static void mips_qemu_writel (void *opaque, target_phys_addr_t addr,
43 6ae81775 ths
                              uint32_t val)
44 6ae81775 ths
{
45 6ae81775 ths
    if ((addr & 0xffff) == 0 && val == 42)
46 6ae81775 ths
        qemu_system_reset_request ();
47 6ae81775 ths
    else if ((addr & 0xffff) == 4 && val == 42)
48 6ae81775 ths
        qemu_system_shutdown_request ();
49 6ae81775 ths
}
50 6ae81775 ths
51 6ae81775 ths
static uint32_t mips_qemu_readl (void *opaque, target_phys_addr_t addr)
52 6ae81775 ths
{
53 6ae81775 ths
    return 0;
54 6ae81775 ths
}
55 6ae81775 ths
56 6ae81775 ths
static CPUWriteMemoryFunc *mips_qemu_write[] = {
57 6ae81775 ths
    &mips_qemu_writel,
58 6ae81775 ths
    &mips_qemu_writel,
59 6ae81775 ths
    &mips_qemu_writel,
60 6ae81775 ths
};
61 6ae81775 ths
62 6ae81775 ths
static CPUReadMemoryFunc *mips_qemu_read[] = {
63 6ae81775 ths
    &mips_qemu_readl,
64 6ae81775 ths
    &mips_qemu_readl,
65 6ae81775 ths
    &mips_qemu_readl,
66 6ae81775 ths
};
67 6ae81775 ths
68 6ae81775 ths
static int mips_qemu_iomemtype = 0;
69 6ae81775 ths
70 7df526e3 ths
static void load_kernel (CPUState *env)
71 6ae81775 ths
{
72 74287114 ths
    int64_t entry, kernel_low, kernel_high;
73 6ae81775 ths
    long kernel_size, initrd_size;
74 74287114 ths
    ram_addr_t initrd_offset;
75 6ae81775 ths
76 7df526e3 ths
    kernel_size = load_elf(loaderparams.kernel_filename, VIRT_TO_PHYS_ADDEND,
77 74287114 ths
                           &entry, &kernel_low, &kernel_high);
78 c570fd16 ths
    if (kernel_size >= 0) {
79 c570fd16 ths
        if ((entry & ~0x7fffffffULL) == 0x80000000)
80 5dc4b744 ths
            entry = (int32_t)entry;
81 ead9360e ths
        env->PC[env->current_tc] = entry;
82 c570fd16 ths
    } else {
83 9042c0e2 ths
        fprintf(stderr, "qemu: could not load kernel '%s'\n",
84 7df526e3 ths
                loaderparams.kernel_filename);
85 9042c0e2 ths
        exit(1);
86 6ae81775 ths
    }
87 6ae81775 ths
88 6ae81775 ths
    /* load initrd */
89 6ae81775 ths
    initrd_size = 0;
90 74287114 ths
    initrd_offset = 0;
91 7df526e3 ths
    if (loaderparams.initrd_filename) {
92 7df526e3 ths
        initrd_size = get_image_size (loaderparams.initrd_filename);
93 74287114 ths
        if (initrd_size > 0) {
94 74287114 ths
            initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
95 74287114 ths
            if (initrd_offset + initrd_size > ram_size) {
96 74287114 ths
                fprintf(stderr,
97 74287114 ths
                        "qemu: memory too small for initial ram disk '%s'\n",
98 7df526e3 ths
                        loaderparams.initrd_filename);
99 74287114 ths
                exit(1);
100 74287114 ths
            }
101 7df526e3 ths
            initrd_size = load_image(loaderparams.initrd_filename,
102 74287114 ths
                                     phys_ram_base + initrd_offset);
103 74287114 ths
        }
104 6ae81775 ths
        if (initrd_size == (target_ulong) -1) {
105 6ae81775 ths
            fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
106 7df526e3 ths
                    loaderparams.initrd_filename);
107 6ae81775 ths
            exit(1);
108 6ae81775 ths
        }
109 6ae81775 ths
    }
110 6ae81775 ths
111 6ae81775 ths
    /* Store command line.  */
112 6ae81775 ths
    if (initrd_size > 0) {
113 6ae81775 ths
        int ret;
114 6ae81775 ths
        ret = sprintf(phys_ram_base + (16 << 20) - 256,
115 3594c774 ths
                      "rd_start=0x" TARGET_FMT_lx " rd_size=%li ",
116 74287114 ths
                      PHYS_TO_VIRT((uint32_t)initrd_offset),
117 6ae81775 ths
                      initrd_size);
118 7df526e3 ths
        strcpy (phys_ram_base + (16 << 20) - 256 + ret,
119 7df526e3 ths
                loaderparams.kernel_cmdline);
120 6ae81775 ths
    }
121 6ae81775 ths
    else {
122 7df526e3 ths
        strcpy (phys_ram_base + (16 << 20) - 256,
123 7df526e3 ths
                loaderparams.kernel_cmdline);
124 6ae81775 ths
    }
125 6ae81775 ths
126 44cbbf18 ths
    *(int32_t *)(phys_ram_base + (16 << 20) - 260) = tswap32 (0x12345678);
127 44cbbf18 ths
    *(int32_t *)(phys_ram_base + (16 << 20) - 264) = tswap32 (ram_size);
128 6ae81775 ths
}
129 6ae81775 ths
130 6ae81775 ths
static void main_cpu_reset(void *opaque)
131 6ae81775 ths
{
132 6ae81775 ths
    CPUState *env = opaque;
133 6ae81775 ths
    cpu_reset(env);
134 6ae81775 ths
135 7df526e3 ths
    if (loaderparams.kernel_filename)
136 7df526e3 ths
        load_kernel (env);
137 6ae81775 ths
}
138 66a93e0f bellard
139 70705261 ths
static
140 6ac0e82d balrog
void mips_r4k_init (int ram_size, int vga_ram_size, const char *boot_device,
141 6af0bf9c bellard
                    DisplayState *ds, const char **fd_filename, int snapshot,
142 6af0bf9c bellard
                    const char *kernel_filename, const char *kernel_cmdline,
143 94fc95cd j_mayer
                    const char *initrd_filename, const char *cpu_model)
144 6af0bf9c bellard
{
145 6af0bf9c bellard
    char buf[1024];
146 6af0bf9c bellard
    unsigned long bios_offset;
147 f7bcd4e3 ths
    int bios_size;
148 c68ea704 bellard
    CPUState *env;
149 153a08db ths
    RTCState *rtc_state;
150 58126404 pbrook
    int i;
151 d537cf6c pbrook
    qemu_irq *i8259;
152 c68ea704 bellard
153 33d68b5f ths
    /* init CPUs */
154 33d68b5f ths
    if (cpu_model == NULL) {
155 60aa19ab ths
#ifdef TARGET_MIPS64
156 33d68b5f ths
        cpu_model = "R4000";
157 33d68b5f ths
#else
158 1c32f43e ths
        cpu_model = "24Kf";
159 33d68b5f ths
#endif
160 33d68b5f ths
    }
161 aaed909a bellard
    env = cpu_init(cpu_model);
162 aaed909a bellard
    if (!env) {
163 aaed909a bellard
        fprintf(stderr, "Unable to find CPU definition\n");
164 aaed909a bellard
        exit(1);
165 aaed909a bellard
    }
166 c68ea704 bellard
    register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
167 6ae81775 ths
    qemu_register_reset(main_cpu_reset, env);
168 c68ea704 bellard
169 6af0bf9c bellard
    /* allocate RAM */
170 6af0bf9c bellard
    cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
171 66a93e0f bellard
172 6ae81775 ths
    if (!mips_qemu_iomemtype) {
173 6ae81775 ths
        mips_qemu_iomemtype = cpu_register_io_memory(0, mips_qemu_read,
174 33d68b5f ths
                                                     mips_qemu_write, NULL);
175 6ae81775 ths
    }
176 6ae81775 ths
    cpu_register_physical_memory(0x1fbf0000, 0x10000, mips_qemu_iomemtype);
177 6ae81775 ths
178 66a93e0f bellard
    /* Try to load a BIOS image. If this fails, we continue regardless,
179 66a93e0f bellard
       but initialize the hardware ourselves. When a kernel gets
180 66a93e0f bellard
       preloaded we also initialize the hardware, since the BIOS wasn't
181 66a93e0f bellard
       run. */
182 6af0bf9c bellard
    bios_offset = ram_size + vga_ram_size;
183 1192dad8 j_mayer
    if (bios_name == NULL)
184 1192dad8 j_mayer
        bios_name = BIOS_FILENAME;
185 1192dad8 j_mayer
    snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
186 f7bcd4e3 ths
    bios_size = load_image(buf, phys_ram_base + bios_offset);
187 2909b29a ths
    if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
188 44cbbf18 ths
        cpu_register_physical_memory(0x1fc00000,
189 66a93e0f bellard
                                     BIOS_SIZE, bios_offset | IO_MEM_ROM);
190 66a93e0f bellard
    } else {
191 66a93e0f bellard
        /* not fatal */
192 66a93e0f bellard
        fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",
193 66a93e0f bellard
                buf);
194 6af0bf9c bellard
    }
195 66a93e0f bellard
196 66a93e0f bellard
    if (kernel_filename) {
197 7df526e3 ths
        loaderparams.ram_size = ram_size;
198 7df526e3 ths
        loaderparams.kernel_filename = kernel_filename;
199 7df526e3 ths
        loaderparams.kernel_cmdline = kernel_cmdline;
200 7df526e3 ths
        loaderparams.initrd_filename = initrd_filename;
201 7df526e3 ths
        load_kernel (env);
202 6af0bf9c bellard
    }
203 6af0bf9c bellard
204 e16fe40c ths
    /* Init CPU internal devices */
205 d537cf6c pbrook
    cpu_mips_irq_init_cpu(env);
206 c68ea704 bellard
    cpu_mips_clock_init(env);
207 6af0bf9c bellard
    cpu_mips_irqctrl_init();
208 6af0bf9c bellard
209 d537cf6c pbrook
    /* The PIC is attached to the MIPS CPU INT0 pin */
210 d537cf6c pbrook
    i8259 = i8259_init(env->irq[2]);
211 d537cf6c pbrook
212 d537cf6c pbrook
    rtc_state = rtc_init(0x70, i8259[8]);
213 afdfa781 ths
214 0699b548 bellard
    /* Register 64 KB of ISA IO space at 0x14000000 */
215 aef445bd pbrook
    isa_mmio_init(0x14000000, 0x00010000);
216 0699b548 bellard
    isa_mem_base = 0x10000000;
217 0699b548 bellard
218 d537cf6c pbrook
    pit = pit_init(0x40, i8259[0]);
219 afdfa781 ths
220 eddbd288 ths
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
221 eddbd288 ths
        if (serial_hds[i]) {
222 d537cf6c pbrook
            serial_init(serial_io[i], i8259[serial_irq[i]], serial_hds[i]);
223 eddbd288 ths
        }
224 eddbd288 ths
    }
225 eddbd288 ths
226 5fafdf24 ths
    isa_vga_init(ds, phys_ram_base + ram_size, ram_size,
227 89b6b508 bellard
                 vga_ram_size);
228 9827e95c bellard
229 a41b2ff2 pbrook
    if (nd_table[0].vlan) {
230 a41b2ff2 pbrook
        if (nd_table[0].model == NULL
231 a41b2ff2 pbrook
            || strcmp(nd_table[0].model, "ne2k_isa") == 0) {
232 d537cf6c pbrook
            isa_ne2000_init(0x300, i8259[9], &nd_table[0]);
233 c4a7060c blueswir1
        } else if (strcmp(nd_table[0].model, "?") == 0) {
234 c4a7060c blueswir1
            fprintf(stderr, "qemu: Supported NICs: ne2k_isa\n");
235 c4a7060c blueswir1
            exit (1);
236 a41b2ff2 pbrook
        } else {
237 a41b2ff2 pbrook
            fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd_table[0].model);
238 a41b2ff2 pbrook
            exit (1);
239 a41b2ff2 pbrook
        }
240 a41b2ff2 pbrook
    }
241 58126404 pbrook
242 58126404 pbrook
    for(i = 0; i < 2; i++)
243 d537cf6c pbrook
        isa_ide_init(ide_iobase[i], ide_iobase2[i], i8259[ide_irq[i]],
244 58126404 pbrook
                     bs_table[2 * i], bs_table[2 * i + 1]);
245 70705261 ths
246 d537cf6c pbrook
    i8042_init(i8259[1], i8259[12], 0x60);
247 9542611a ths
    ds1225y_init(0x9000, "nvram");
248 6af0bf9c bellard
}
249 6af0bf9c bellard
250 6af0bf9c bellard
QEMUMachine mips_machine = {
251 6af0bf9c bellard
    "mips",
252 6af0bf9c bellard
    "mips r4k platform",
253 6af0bf9c bellard
    mips_r4k_init,
254 6af0bf9c bellard
};