Statistics
| Branch: | Revision:

root / hw / mips_r4k.c @ 6e473128

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