xref: /qemu/hw/mips/mipssim.c (revision 32cad1ffb81dcecf6f4a8af56d6e5892682839b1)
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"
310068b069SPhilippe Mathieu-Daudé #include "exec/address-spaces.h"
328543a806SPhilippe Mathieu-Daudé #include "hw/clock.h"
330d09e41aSPaolo Bonzini #include "hw/mips/mips.h"
347e6b5497SBernhard Beschow #include "hw/char/serial-mm.h"
351422e32dSPaolo Bonzini #include "net/net.h"
36*32cad1ffSPhilippe Mathieu-Daudé #include "system/system.h"
3783c9f4caSPaolo Bonzini #include "hw/boards.h"
3883c9f4caSPaolo Bonzini #include "hw/loader.h"
39ca20cf32SBlue Swirl #include "elf.h"
4083c9f4caSPaolo Bonzini #include "hw/sysbus.h"
419fac5d88SMarc-André Lureau #include "hw/qdev-properties.h"
422e985fe0SAurelien Jarno #include "qemu/error-report.h"
43*32cad1ffSPhilippe Mathieu-Daudé #include "system/qtest.h"
44*32cad1ffSPhilippe Mathieu-Daudé #include "system/reset.h"
451eb64c39SPhilippe Mathieu-Daudé #include "cpu.h"
461eb64c39SPhilippe Mathieu-Daudé 
471eb64c39SPhilippe Mathieu-Daudé #define BIOS_SIZE (4 * MiB)
481eb64c39SPhilippe Mathieu-Daudé 
491eb64c39SPhilippe Mathieu-Daudé #if TARGET_BIG_ENDIAN
501eb64c39SPhilippe Mathieu-Daudé #define BIOS_FILENAME "mips_bios.bin"
511eb64c39SPhilippe Mathieu-Daudé #else
521eb64c39SPhilippe Mathieu-Daudé #define BIOS_FILENAME "mipsel_bios.bin"
531eb64c39SPhilippe Mathieu-Daudé #endif
54f0fc6f8fSths 
557df526e3Sths static struct _loaderparams {
567df526e3Sths     int ram_size;
577df526e3Sths     const char *kernel_filename;
587df526e3Sths     const char *kernel_cmdline;
597df526e3Sths     const char *initrd_filename;
607df526e3Sths } loaderparams;
617df526e3Sths 
62e16ad5b0SAurelien Jarno typedef struct ResetData {
632d44fc8eSAndreas Färber     MIPSCPU *cpu;
64e16ad5b0SAurelien Jarno     uint64_t vector;
65e16ad5b0SAurelien Jarno } ResetData;
66e16ad5b0SAurelien Jarno 
67dde98994SJiaxun Yang static uint64_t load_kernel(void)
68f0fc6f8fSths {
69dde98994SJiaxun Yang     uint64_t entry, kernel_high, initrd_size;
70f0fc6f8fSths     long kernel_size;
71c227f099SAnthony Liguori     ram_addr_t initrd_offset;
72f0fc6f8fSths 
734366e1dbSLiam Merwick     kernel_size = load_elf(loaderparams.kernel_filename, NULL,
744366e1dbSLiam Merwick                            cpu_mips_kseg0_to_phys, NULL,
75dde98994SJiaxun Yang                            &entry, NULL,
76ded625e7SThomas Huth                            &kernel_high, NULL, TARGET_BIG_ENDIAN,
777ef295eaSPeter Crosthwaite                            EM_MIPS, 1, 0);
789d585eaaSPhilippe Mathieu-Daudé     if (kernel_size < 0) {
79bd6e1d81SAlistair Francis         error_report("could not load kernel '%s': %s",
803ee3122cSAurelien Jarno                      loaderparams.kernel_filename,
813ee3122cSAurelien Jarno                      load_elf_strerror(kernel_size));
82f0fc6f8fSths         exit(1);
83f0fc6f8fSths     }
84f0fc6f8fSths 
85f0fc6f8fSths     /* load initrd */
86f0fc6f8fSths     initrd_size = 0;
87f0fc6f8fSths     initrd_offset = 0;
887df526e3Sths     if (loaderparams.initrd_filename) {
897df526e3Sths         initrd_size = get_image_size(loaderparams.initrd_filename);
90f0fc6f8fSths         if (initrd_size > 0) {
91acab36caSPhilippe Mathieu-Daudé             initrd_offset = ROUND_UP(kernel_high, INITRD_PAGE_SIZE);
927df526e3Sths             if (initrd_offset + initrd_size > loaderparams.ram_size) {
93bd6e1d81SAlistair Francis                 error_report("memory too small for initial ram disk '%s'",
947df526e3Sths                              loaderparams.initrd_filename);
95f0fc6f8fSths                 exit(1);
96f0fc6f8fSths             }
97dcac9679Spbrook             initrd_size = load_image_targphys(loaderparams.initrd_filename,
98dcac9679Spbrook                 initrd_offset, loaderparams.ram_size - initrd_offset);
99f0fc6f8fSths         }
100f0fc6f8fSths         if (initrd_size == (target_ulong) -1) {
101bd6e1d81SAlistair Francis             error_report("could not load initial ram disk '%s'",
1027df526e3Sths                          loaderparams.initrd_filename);
103f0fc6f8fSths             exit(1);
104f0fc6f8fSths         }
105f0fc6f8fSths     }
106e16ad5b0SAurelien Jarno     return entry;
107f0fc6f8fSths }
108f0fc6f8fSths 
109f0fc6f8fSths static void main_cpu_reset(void *opaque)
110f0fc6f8fSths {
111e16ad5b0SAurelien Jarno     ResetData *s = (ResetData *)opaque;
1122d44fc8eSAndreas Färber     CPUMIPSState *env = &s->cpu->env;
113f0fc6f8fSths 
1142d44fc8eSAndreas Färber     cpu_reset(CPU(s->cpu));
115aecf1376SNathan Froyd     env->active_tc.PC = s->vector & ~(target_ulong)1;
116aecf1376SNathan Froyd     if (s->vector & 1) {
117aecf1376SNathan Froyd         env->hflags |= MIPS_HFLAG_M16;
118aecf1376SNathan Froyd     }
119f0fc6f8fSths }
120f0fc6f8fSths 
121b07734f9SDavid Woodhouse static void mipsnet_init(int base, qemu_irq irq)
122d118d64aSHervé Poussineau {
123d118d64aSHervé Poussineau     DeviceState *dev;
124d118d64aSHervé Poussineau     SysBusDevice *s;
125d118d64aSHervé Poussineau 
126b07734f9SDavid Woodhouse     dev = qemu_create_nic_device("mipsnet", true, NULL);
127b07734f9SDavid Woodhouse     if (!dev) {
128b07734f9SDavid Woodhouse         return;
129b07734f9SDavid Woodhouse     }
130d118d64aSHervé Poussineau 
1311356b98dSAndreas Färber     s = SYS_BUS_DEVICE(dev);
1323c6ef471SMarkus Armbruster     sysbus_realize_and_unref(s, &error_fatal);
133d118d64aSHervé Poussineau     sysbus_connect_irq(s, 0, irq);
134d118d64aSHervé Poussineau     memory_region_add_subregion(get_system_io(),
135d118d64aSHervé Poussineau                                 base,
136d118d64aSHervé Poussineau                                 sysbus_mmio_get_region(s, 0));
137d118d64aSHervé Poussineau }
138d118d64aSHervé Poussineau 
139f0fc6f8fSths static void
1403ef96221SMarcel Apfelbaum mips_mipssim_init(MachineState *machine)
141f0fc6f8fSths {
1423ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
1433ef96221SMarcel Apfelbaum     const char *kernel_cmdline = machine->kernel_cmdline;
1443ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
1455cea8590SPaul Brook     char *filename;
14623ebf23dSAvi Kivity     MemoryRegion *address_space_mem = get_system_memory();
147bdb75c79SPaolo Bonzini     MemoryRegion *isa = g_new(MemoryRegion, 1);
14823ebf23dSAvi Kivity     MemoryRegion *bios = g_new(MemoryRegion, 1);
1498543a806SPhilippe Mathieu-Daudé     Clock *cpuclk;
1507ee274c1SAndreas Färber     MIPSCPU *cpu;
15161c56c8cSAndreas Färber     CPUMIPSState *env;
152e16ad5b0SAurelien Jarno     ResetData *reset_info;
153b5334159Sths     int bios_size;
154f0fc6f8fSths 
1558543a806SPhilippe Mathieu-Daudé     cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
1568543a806SPhilippe Mathieu-Daudé #ifdef TARGET_MIPS64
1578543a806SPhilippe Mathieu-Daudé     clock_set_hz(cpuclk, 6000000); /* 6 MHz */
1588543a806SPhilippe Mathieu-Daudé #else
1598543a806SPhilippe Mathieu-Daudé     clock_set_hz(cpuclk, 12000000); /* 12 MHz */
1608543a806SPhilippe Mathieu-Daudé #endif
1618543a806SPhilippe Mathieu-Daudé 
162f0fc6f8fSths     /* Init CPUs. */
1633e8f019bSPhilippe Mathieu-Daudé     cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk,
1643e8f019bSPhilippe Mathieu-Daudé                                      TARGET_BIG_ENDIAN);
1657ee274c1SAndreas Färber     env = &cpu->env;
1667ee274c1SAndreas Färber 
167b21e2380SMarkus Armbruster     reset_info = g_new0(ResetData, 1);
1682d44fc8eSAndreas Färber     reset_info->cpu = cpu;
169e16ad5b0SAurelien Jarno     reset_info->vector = env->active_tc.PC;
170e16ad5b0SAurelien Jarno     qemu_register_reset(main_cpu_reset, reset_info);
171f0fc6f8fSths 
172f0fc6f8fSths     /* Allocate RAM. */
1733fab7f23SPhilippe Mathieu-Daudé     memory_region_init_rom(bios, NULL, "mips_mipssim.bios", BIOS_SIZE,
174f8ed85acSMarkus Armbruster                            &error_fatal);
175f0fc6f8fSths 
176ceefaa3bSIgor Mammedov     memory_region_add_subregion(address_space_mem, 0, machine->ram);
177dcac9679Spbrook 
178dcac9679Spbrook     /* Map the BIOS / boot exception handler. */
17923ebf23dSAvi Kivity     memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
180f0fc6f8fSths     /* Load a BIOS / boot exception handler image. */
18159588beaSPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware ?: BIOS_FILENAME);
1825cea8590SPaul Brook     if (filename) {
1835cea8590SPaul Brook         bios_size = load_image_targphys(filename, 0x1fc00000LL, BIOS_SIZE);
1847267c094SAnthony Liguori         g_free(filename);
1855cea8590SPaul Brook     } else {
1865cea8590SPaul Brook         bios_size = -1;
1875cea8590SPaul Brook     }
18822d5523dSAndreas Färber     if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
18959588beaSPaolo Bonzini         machine->firmware && !qtest_enabled()) {
190f0fc6f8fSths         /* Bail out if we have neither a kernel image nor boot vector code. */
19159588beaSPaolo Bonzini         error_report("Could not load MIPS bios '%s'", machine->firmware);
1922e985fe0SAurelien Jarno         exit(1);
193f0fc6f8fSths     } else {
194b5334159Sths         /* We have a boot vector start address. */
195b5dc7732Sths         env->active_tc.PC = (target_long)(int32_t)0xbfc00000;
196f0fc6f8fSths     }
197f0fc6f8fSths 
198f0fc6f8fSths     if (kernel_filename) {
199ceefaa3bSIgor Mammedov         loaderparams.ram_size = machine->ram_size;
2007df526e3Sths         loaderparams.kernel_filename = kernel_filename;
2017df526e3Sths         loaderparams.kernel_cmdline = kernel_cmdline;
2027df526e3Sths         loaderparams.initrd_filename = initrd_filename;
203e16ad5b0SAurelien Jarno         reset_info->vector = load_kernel();
204f0fc6f8fSths     }
205f0fc6f8fSths 
206f0fc6f8fSths     /* Init CPU internal devices. */
2075a975d43SPaolo Bonzini     cpu_mips_irq_init_cpu(cpu);
2085a975d43SPaolo Bonzini     cpu_mips_clock_init(cpu);
209f0fc6f8fSths 
210e6f21933SBernhard Beschow     /*
211e6f21933SBernhard Beschow      * Register 64 KB of ISA IO space at 0x1fd00000.  But without interrupts
212e6f21933SBernhard Beschow      * (except for the hardcoded serial port interrupt) -device cannot work,
213e6f21933SBernhard Beschow      * so do not expose the ISA bus to the user.
214e6f21933SBernhard Beschow      */
215bdb75c79SPaolo Bonzini     memory_region_init_alias(isa, NULL, "isa_mmio",
216bdb75c79SPaolo Bonzini                              get_system_io(), 0, 0x00010000);
217bdb75c79SPaolo Bonzini     memory_region_add_subregion(get_system_memory(), 0x1fd00000, isa);
218f0fc6f8fSths 
21933dd6f44SAleksandar Markovic     /*
22033dd6f44SAleksandar Markovic      * A single 16450 sits at offset 0x3f8. It is attached to
22133dd6f44SAleksandar Markovic      * MIPS CPU INT2, which is interrupt 4.
22233dd6f44SAleksandar Markovic      */
2239fac5d88SMarc-André Lureau     if (serial_hd(0)) {
224cf3d932fSPhilippe Mathieu-Daudé         DeviceState *dev = qdev_new(TYPE_SERIAL_MM);
2259fac5d88SMarc-André Lureau 
2269fac5d88SMarc-André Lureau         qdev_prop_set_chr(dev, "chardev", serial_hd(0));
227cf3d932fSPhilippe Mathieu-Daudé         qdev_prop_set_uint8(dev, "regshift", 0);
228cf3d932fSPhilippe Mathieu-Daudé         qdev_prop_set_uint8(dev, "endianness", DEVICE_LITTLE_ENDIAN);
2293c6ef471SMarkus Armbruster         sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
2309fac5d88SMarc-André Lureau         sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, env->irq[4]);
2310068b069SPhilippe Mathieu-Daudé         memory_region_add_subregion(get_system_io(), 0x3f8,
232d9259178SMarc-André Lureau                       sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0));
2339fac5d88SMarc-André Lureau     }
234f0fc6f8fSths 
235f0fc6f8fSths     /* MIPSnet uses the MIPS CPU INT0, which is interrupt 2. */
236b07734f9SDavid Woodhouse     mipsnet_init(0x4200, env->irq[2]);
237f0fc6f8fSths }
238f0fc6f8fSths 
239e264d29dSEduardo Habkost static void mips_mipssim_machine_init(MachineClass *mc)
240f80f9ec9SAnthony Liguori {
241e264d29dSEduardo Habkost     mc->desc = "MIPS MIPSsim platform";
242e264d29dSEduardo Habkost     mc->init = mips_mipssim_init;
2430fc52fd2SIgor Mammedov #ifdef TARGET_MIPS64
2440fc52fd2SIgor Mammedov     mc->default_cpu_type = MIPS_CPU_TYPE_NAME("5Kf");
2450fc52fd2SIgor Mammedov #else
2460fc52fd2SIgor Mammedov     mc->default_cpu_type = MIPS_CPU_TYPE_NAME("24Kf");
2470fc52fd2SIgor Mammedov #endif
248ceefaa3bSIgor Mammedov     mc->default_ram_id = "mips_mipssim.ram";
249f80f9ec9SAnthony Liguori }
250f80f9ec9SAnthony Liguori 
251e264d29dSEduardo Habkost DEFINE_MACHINE("mipssim", mips_mipssim_machine_init)
252