xref: /qemu/hw/mips/mipssim.c (revision ded625e7aa6a7f3173a22657f7dc0e9ab3d8fa3b)
1f0fc6f8fSths /*
2f0fc6f8fSths  * QEMU/mipssim emulation
3f0fc6f8fSths  *
4b5e4946fSStefan Weil  * Emulates a very simple machine model similar to the one used by the
5f0fc6f8fSths  * proprietary MIPS emulator.
6a79ee211Sths  *
7a79ee211Sths  * Copyright (c) 2007 Thiemo Seufer
8a79ee211Sths  *
9a79ee211Sths  * Permission is hereby granted, free of charge, to any person obtaining a copy
10a79ee211Sths  * of this software and associated documentation files (the "Software"), to deal
11a79ee211Sths  * in the Software without restriction, including without limitation the rights
12a79ee211Sths  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13a79ee211Sths  * copies of the Software, and to permit persons to whom the Software is
14a79ee211Sths  * furnished to do so, subject to the following conditions:
15a79ee211Sths  *
16a79ee211Sths  * The above copyright notice and this permission notice shall be included in
17a79ee211Sths  * all copies or substantial portions of the Software.
18a79ee211Sths  *
19a79ee211Sths  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20a79ee211Sths  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21a79ee211Sths  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22a79ee211Sths  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23a79ee211Sths  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24a79ee211Sths  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25a79ee211Sths  * THE SOFTWARE.
26f0fc6f8fSths  */
2771e8a915SMarkus Armbruster 
28c684822aSPeter Maydell #include "qemu/osdep.h"
29da34e65cSMarkus Armbruster #include "qapi/error.h"
302c65db5eSPaolo Bonzini #include "qemu/datadir.h"
318543a806SPhilippe Mathieu-Daudé #include "hw/clock.h"
320d09e41aSPaolo Bonzini #include "hw/mips/mips.h"
330d09e41aSPaolo Bonzini #include "hw/mips/cpudevs.h"
340d09e41aSPaolo Bonzini #include "hw/char/serial.h"
350d09e41aSPaolo Bonzini #include "hw/isa/isa.h"
361422e32dSPaolo Bonzini #include "net/net.h"
379c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
3883c9f4caSPaolo Bonzini #include "hw/boards.h"
390d09e41aSPaolo Bonzini #include "hw/mips/bios.h"
4083c9f4caSPaolo Bonzini #include "hw/loader.h"
41ca20cf32SBlue Swirl #include "elf.h"
4283c9f4caSPaolo Bonzini #include "hw/sysbus.h"
439fac5d88SMarc-André Lureau #include "hw/qdev-properties.h"
442e985fe0SAurelien Jarno #include "qemu/error-report.h"
4522d5523dSAndreas Färber #include "sysemu/qtest.h"
4671e8a915SMarkus Armbruster #include "sysemu/reset.h"
47f0fc6f8fSths 
487df526e3Sths static struct _loaderparams {
497df526e3Sths     int ram_size;
507df526e3Sths     const char *kernel_filename;
517df526e3Sths     const char *kernel_cmdline;
527df526e3Sths     const char *initrd_filename;
537df526e3Sths } loaderparams;
547df526e3Sths 
55e16ad5b0SAurelien Jarno typedef struct ResetData {
562d44fc8eSAndreas Färber     MIPSCPU *cpu;
57e16ad5b0SAurelien Jarno     uint64_t vector;
58e16ad5b0SAurelien Jarno } ResetData;
59e16ad5b0SAurelien Jarno 
60dde98994SJiaxun Yang static uint64_t load_kernel(void)
61f0fc6f8fSths {
62dde98994SJiaxun Yang     uint64_t entry, kernel_high, initrd_size;
63f0fc6f8fSths     long kernel_size;
64c227f099SAnthony Liguori     ram_addr_t initrd_offset;
65f0fc6f8fSths 
664366e1dbSLiam Merwick     kernel_size = load_elf(loaderparams.kernel_filename, NULL,
674366e1dbSLiam Merwick                            cpu_mips_kseg0_to_phys, NULL,
68dde98994SJiaxun Yang                            &entry, NULL,
69*ded625e7SThomas Huth                            &kernel_high, NULL, TARGET_BIG_ENDIAN,
707ef295eaSPeter Crosthwaite                            EM_MIPS, 1, 0);
719d585eaaSPhilippe Mathieu-Daudé     if (kernel_size < 0) {
72bd6e1d81SAlistair Francis         error_report("could not load kernel '%s': %s",
733ee3122cSAurelien Jarno                      loaderparams.kernel_filename,
743ee3122cSAurelien Jarno                      load_elf_strerror(kernel_size));
75f0fc6f8fSths         exit(1);
76f0fc6f8fSths     }
77f0fc6f8fSths 
78f0fc6f8fSths     /* load initrd */
79f0fc6f8fSths     initrd_size = 0;
80f0fc6f8fSths     initrd_offset = 0;
817df526e3Sths     if (loaderparams.initrd_filename) {
827df526e3Sths         initrd_size = get_image_size(loaderparams.initrd_filename);
83f0fc6f8fSths         if (initrd_size > 0) {
84acab36caSPhilippe Mathieu-Daudé             initrd_offset = ROUND_UP(kernel_high, INITRD_PAGE_SIZE);
857df526e3Sths             if (initrd_offset + initrd_size > loaderparams.ram_size) {
86bd6e1d81SAlistair Francis                 error_report("memory too small for initial ram disk '%s'",
877df526e3Sths                              loaderparams.initrd_filename);
88f0fc6f8fSths                 exit(1);
89f0fc6f8fSths             }
90dcac9679Spbrook             initrd_size = load_image_targphys(loaderparams.initrd_filename,
91dcac9679Spbrook                 initrd_offset, loaderparams.ram_size - initrd_offset);
92f0fc6f8fSths         }
93f0fc6f8fSths         if (initrd_size == (target_ulong) -1) {
94bd6e1d81SAlistair Francis             error_report("could not load initial ram disk '%s'",
957df526e3Sths                          loaderparams.initrd_filename);
96f0fc6f8fSths             exit(1);
97f0fc6f8fSths         }
98f0fc6f8fSths     }
99e16ad5b0SAurelien Jarno     return entry;
100f0fc6f8fSths }
101f0fc6f8fSths 
102f0fc6f8fSths static void main_cpu_reset(void *opaque)
103f0fc6f8fSths {
104e16ad5b0SAurelien Jarno     ResetData *s = (ResetData *)opaque;
1052d44fc8eSAndreas Färber     CPUMIPSState *env = &s->cpu->env;
106f0fc6f8fSths 
1072d44fc8eSAndreas Färber     cpu_reset(CPU(s->cpu));
108aecf1376SNathan Froyd     env->active_tc.PC = s->vector & ~(target_ulong)1;
109aecf1376SNathan Froyd     if (s->vector & 1) {
110aecf1376SNathan Froyd         env->hflags |= MIPS_HFLAG_M16;
111aecf1376SNathan Froyd     }
112f0fc6f8fSths }
113f0fc6f8fSths 
114d118d64aSHervé Poussineau static void mipsnet_init(int base, qemu_irq irq, NICInfo *nd)
115d118d64aSHervé Poussineau {
116d118d64aSHervé Poussineau     DeviceState *dev;
117d118d64aSHervé Poussineau     SysBusDevice *s;
118d118d64aSHervé Poussineau 
1193e80f690SMarkus Armbruster     dev = qdev_new("mipsnet");
120d118d64aSHervé Poussineau     qdev_set_nic_properties(dev, nd);
121d118d64aSHervé Poussineau 
1221356b98dSAndreas Färber     s = SYS_BUS_DEVICE(dev);
1233c6ef471SMarkus Armbruster     sysbus_realize_and_unref(s, &error_fatal);
124d118d64aSHervé Poussineau     sysbus_connect_irq(s, 0, irq);
125d118d64aSHervé Poussineau     memory_region_add_subregion(get_system_io(),
126d118d64aSHervé Poussineau                                 base,
127d118d64aSHervé Poussineau                                 sysbus_mmio_get_region(s, 0));
128d118d64aSHervé Poussineau }
129d118d64aSHervé Poussineau 
130f0fc6f8fSths static void
1313ef96221SMarcel Apfelbaum mips_mipssim_init(MachineState *machine)
132f0fc6f8fSths {
1333ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
1343ef96221SMarcel Apfelbaum     const char *kernel_cmdline = machine->kernel_cmdline;
1353ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
1365cea8590SPaul Brook     char *filename;
13723ebf23dSAvi Kivity     MemoryRegion *address_space_mem = get_system_memory();
138bdb75c79SPaolo Bonzini     MemoryRegion *isa = g_new(MemoryRegion, 1);
13923ebf23dSAvi Kivity     MemoryRegion *bios = g_new(MemoryRegion, 1);
1408543a806SPhilippe Mathieu-Daudé     Clock *cpuclk;
1417ee274c1SAndreas Färber     MIPSCPU *cpu;
14261c56c8cSAndreas Färber     CPUMIPSState *env;
143e16ad5b0SAurelien Jarno     ResetData *reset_info;
144b5334159Sths     int bios_size;
145f0fc6f8fSths 
1468543a806SPhilippe Mathieu-Daudé     cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
1478543a806SPhilippe Mathieu-Daudé #ifdef TARGET_MIPS64
1488543a806SPhilippe Mathieu-Daudé     clock_set_hz(cpuclk, 6000000); /* 6 MHz */
1498543a806SPhilippe Mathieu-Daudé #else
1508543a806SPhilippe Mathieu-Daudé     clock_set_hz(cpuclk, 12000000); /* 12 MHz */
1518543a806SPhilippe Mathieu-Daudé #endif
1528543a806SPhilippe Mathieu-Daudé 
153f0fc6f8fSths     /* Init CPUs. */
1548543a806SPhilippe Mathieu-Daudé     cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk);
1557ee274c1SAndreas Färber     env = &cpu->env;
1567ee274c1SAndreas Färber 
157b21e2380SMarkus Armbruster     reset_info = g_new0(ResetData, 1);
1582d44fc8eSAndreas Färber     reset_info->cpu = cpu;
159e16ad5b0SAurelien Jarno     reset_info->vector = env->active_tc.PC;
160e16ad5b0SAurelien Jarno     qemu_register_reset(main_cpu_reset, reset_info);
161f0fc6f8fSths 
162f0fc6f8fSths     /* Allocate RAM. */
1633fab7f23SPhilippe Mathieu-Daudé     memory_region_init_rom(bios, NULL, "mips_mipssim.bios", BIOS_SIZE,
164f8ed85acSMarkus Armbruster                            &error_fatal);
165f0fc6f8fSths 
166ceefaa3bSIgor Mammedov     memory_region_add_subregion(address_space_mem, 0, machine->ram);
167dcac9679Spbrook 
168dcac9679Spbrook     /* Map the BIOS / boot exception handler. */
16923ebf23dSAvi Kivity     memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
170f0fc6f8fSths     /* Load a BIOS / boot exception handler image. */
17159588beaSPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware ?: BIOS_FILENAME);
1725cea8590SPaul Brook     if (filename) {
1735cea8590SPaul Brook         bios_size = load_image_targphys(filename, 0x1fc00000LL, BIOS_SIZE);
1747267c094SAnthony Liguori         g_free(filename);
1755cea8590SPaul Brook     } else {
1765cea8590SPaul Brook         bios_size = -1;
1775cea8590SPaul Brook     }
17822d5523dSAndreas Färber     if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
17959588beaSPaolo Bonzini         machine->firmware && !qtest_enabled()) {
180f0fc6f8fSths         /* Bail out if we have neither a kernel image nor boot vector code. */
18159588beaSPaolo Bonzini         error_report("Could not load MIPS bios '%s'", machine->firmware);
1822e985fe0SAurelien Jarno         exit(1);
183f0fc6f8fSths     } else {
184b5334159Sths         /* We have a boot vector start address. */
185b5dc7732Sths         env->active_tc.PC = (target_long)(int32_t)0xbfc00000;
186f0fc6f8fSths     }
187f0fc6f8fSths 
188f0fc6f8fSths     if (kernel_filename) {
189ceefaa3bSIgor Mammedov         loaderparams.ram_size = machine->ram_size;
1907df526e3Sths         loaderparams.kernel_filename = kernel_filename;
1917df526e3Sths         loaderparams.kernel_cmdline = kernel_cmdline;
1927df526e3Sths         loaderparams.initrd_filename = initrd_filename;
193e16ad5b0SAurelien Jarno         reset_info->vector = load_kernel();
194f0fc6f8fSths     }
195f0fc6f8fSths 
196f0fc6f8fSths     /* Init CPU internal devices. */
1975a975d43SPaolo Bonzini     cpu_mips_irq_init_cpu(cpu);
1985a975d43SPaolo Bonzini     cpu_mips_clock_init(cpu);
199f0fc6f8fSths 
200f0fc6f8fSths     /* Register 64 KB of ISA IO space at 0x1fd00000. */
201bdb75c79SPaolo Bonzini     memory_region_init_alias(isa, NULL, "isa_mmio",
202bdb75c79SPaolo Bonzini                              get_system_io(), 0, 0x00010000);
203bdb75c79SPaolo Bonzini     memory_region_add_subregion(get_system_memory(), 0x1fd00000, isa);
204f0fc6f8fSths 
20533dd6f44SAleksandar Markovic     /*
20633dd6f44SAleksandar Markovic      * A single 16450 sits at offset 0x3f8. It is attached to
20733dd6f44SAleksandar Markovic      * MIPS CPU INT2, which is interrupt 4.
20833dd6f44SAleksandar Markovic      */
2099fac5d88SMarc-André Lureau     if (serial_hd(0)) {
210cf3d932fSPhilippe Mathieu-Daudé         DeviceState *dev = qdev_new(TYPE_SERIAL_MM);
2119fac5d88SMarc-André Lureau 
2129fac5d88SMarc-André Lureau         qdev_prop_set_chr(dev, "chardev", serial_hd(0));
213cf3d932fSPhilippe Mathieu-Daudé         qdev_prop_set_uint8(dev, "regshift", 0);
214cf3d932fSPhilippe Mathieu-Daudé         qdev_prop_set_uint8(dev, "endianness", DEVICE_LITTLE_ENDIAN);
2153c6ef471SMarkus Armbruster         sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
2169fac5d88SMarc-André Lureau         sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, env->irq[4]);
217d9259178SMarc-André Lureau         sysbus_add_io(SYS_BUS_DEVICE(dev), 0x3f8,
218d9259178SMarc-André Lureau                       sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0));
2199fac5d88SMarc-André Lureau     }
220f0fc6f8fSths 
221a005d073SStefan Hajnoczi     if (nd_table[0].used)
222f0fc6f8fSths         /* MIPSnet uses the MIPS CPU INT0, which is interrupt 2. */
223f0fc6f8fSths         mipsnet_init(0x4200, env->irq[2], &nd_table[0]);
224f0fc6f8fSths }
225f0fc6f8fSths 
226e264d29dSEduardo Habkost static void mips_mipssim_machine_init(MachineClass *mc)
227f80f9ec9SAnthony Liguori {
228e264d29dSEduardo Habkost     mc->desc = "MIPS MIPSsim platform";
229e264d29dSEduardo Habkost     mc->init = mips_mipssim_init;
2300fc52fd2SIgor Mammedov #ifdef TARGET_MIPS64
2310fc52fd2SIgor Mammedov     mc->default_cpu_type = MIPS_CPU_TYPE_NAME("5Kf");
2320fc52fd2SIgor Mammedov #else
2330fc52fd2SIgor Mammedov     mc->default_cpu_type = MIPS_CPU_TYPE_NAME("24Kf");
2340fc52fd2SIgor Mammedov #endif
235ceefaa3bSIgor Mammedov     mc->default_ram_id = "mips_mipssim.ram";
236f80f9ec9SAnthony Liguori }
237f80f9ec9SAnthony Liguori 
238e264d29dSEduardo Habkost DEFINE_MACHINE("mipssim", mips_mipssim_machine_init)
239