xref: /qemu/hw/mips/mipssim.c (revision e6f2193367a6b060b65c8898c02d0502f8aaa7e5)
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/char/serial.h"
341422e32dSPaolo Bonzini #include "net/net.h"
359c17d615SPaolo Bonzini #include "sysemu/sysemu.h"
3683c9f4caSPaolo Bonzini #include "hw/boards.h"
3783c9f4caSPaolo Bonzini #include "hw/loader.h"
38ca20cf32SBlue Swirl #include "elf.h"
3983c9f4caSPaolo Bonzini #include "hw/sysbus.h"
409fac5d88SMarc-André Lureau #include "hw/qdev-properties.h"
412e985fe0SAurelien Jarno #include "qemu/error-report.h"
4222d5523dSAndreas Färber #include "sysemu/qtest.h"
4371e8a915SMarkus Armbruster #include "sysemu/reset.h"
441eb64c39SPhilippe Mathieu-Daudé #include "cpu.h"
451eb64c39SPhilippe Mathieu-Daudé 
461eb64c39SPhilippe Mathieu-Daudé #define BIOS_SIZE (4 * MiB)
471eb64c39SPhilippe Mathieu-Daudé 
481eb64c39SPhilippe Mathieu-Daudé #if TARGET_BIG_ENDIAN
491eb64c39SPhilippe Mathieu-Daudé #define BIOS_FILENAME "mips_bios.bin"
501eb64c39SPhilippe Mathieu-Daudé #else
511eb64c39SPhilippe Mathieu-Daudé #define BIOS_FILENAME "mipsel_bios.bin"
521eb64c39SPhilippe Mathieu-Daudé #endif
53f0fc6f8fSths 
547df526e3Sths static struct _loaderparams {
557df526e3Sths     int ram_size;
567df526e3Sths     const char *kernel_filename;
577df526e3Sths     const char *kernel_cmdline;
587df526e3Sths     const char *initrd_filename;
597df526e3Sths } loaderparams;
607df526e3Sths 
61e16ad5b0SAurelien Jarno typedef struct ResetData {
622d44fc8eSAndreas Färber     MIPSCPU *cpu;
63e16ad5b0SAurelien Jarno     uint64_t vector;
64e16ad5b0SAurelien Jarno } ResetData;
65e16ad5b0SAurelien Jarno 
66dde98994SJiaxun Yang static uint64_t load_kernel(void)
67f0fc6f8fSths {
68dde98994SJiaxun Yang     uint64_t entry, kernel_high, initrd_size;
69f0fc6f8fSths     long kernel_size;
70c227f099SAnthony Liguori     ram_addr_t initrd_offset;
71f0fc6f8fSths 
724366e1dbSLiam Merwick     kernel_size = load_elf(loaderparams.kernel_filename, NULL,
734366e1dbSLiam Merwick                            cpu_mips_kseg0_to_phys, NULL,
74dde98994SJiaxun Yang                            &entry, NULL,
75ded625e7SThomas Huth                            &kernel_high, NULL, TARGET_BIG_ENDIAN,
767ef295eaSPeter Crosthwaite                            EM_MIPS, 1, 0);
779d585eaaSPhilippe Mathieu-Daudé     if (kernel_size < 0) {
78bd6e1d81SAlistair Francis         error_report("could not load kernel '%s': %s",
793ee3122cSAurelien Jarno                      loaderparams.kernel_filename,
803ee3122cSAurelien Jarno                      load_elf_strerror(kernel_size));
81f0fc6f8fSths         exit(1);
82f0fc6f8fSths     }
83f0fc6f8fSths 
84f0fc6f8fSths     /* load initrd */
85f0fc6f8fSths     initrd_size = 0;
86f0fc6f8fSths     initrd_offset = 0;
877df526e3Sths     if (loaderparams.initrd_filename) {
887df526e3Sths         initrd_size = get_image_size(loaderparams.initrd_filename);
89f0fc6f8fSths         if (initrd_size > 0) {
90acab36caSPhilippe Mathieu-Daudé             initrd_offset = ROUND_UP(kernel_high, INITRD_PAGE_SIZE);
917df526e3Sths             if (initrd_offset + initrd_size > loaderparams.ram_size) {
92bd6e1d81SAlistair Francis                 error_report("memory too small for initial ram disk '%s'",
937df526e3Sths                              loaderparams.initrd_filename);
94f0fc6f8fSths                 exit(1);
95f0fc6f8fSths             }
96dcac9679Spbrook             initrd_size = load_image_targphys(loaderparams.initrd_filename,
97dcac9679Spbrook                 initrd_offset, loaderparams.ram_size - initrd_offset);
98f0fc6f8fSths         }
99f0fc6f8fSths         if (initrd_size == (target_ulong) -1) {
100bd6e1d81SAlistair Francis             error_report("could not load initial ram disk '%s'",
1017df526e3Sths                          loaderparams.initrd_filename);
102f0fc6f8fSths             exit(1);
103f0fc6f8fSths         }
104f0fc6f8fSths     }
105e16ad5b0SAurelien Jarno     return entry;
106f0fc6f8fSths }
107f0fc6f8fSths 
108f0fc6f8fSths static void main_cpu_reset(void *opaque)
109f0fc6f8fSths {
110e16ad5b0SAurelien Jarno     ResetData *s = (ResetData *)opaque;
1112d44fc8eSAndreas Färber     CPUMIPSState *env = &s->cpu->env;
112f0fc6f8fSths 
1132d44fc8eSAndreas Färber     cpu_reset(CPU(s->cpu));
114aecf1376SNathan Froyd     env->active_tc.PC = s->vector & ~(target_ulong)1;
115aecf1376SNathan Froyd     if (s->vector & 1) {
116aecf1376SNathan Froyd         env->hflags |= MIPS_HFLAG_M16;
117aecf1376SNathan Froyd     }
118f0fc6f8fSths }
119f0fc6f8fSths 
120b07734f9SDavid Woodhouse static void mipsnet_init(int base, qemu_irq irq)
121d118d64aSHervé Poussineau {
122d118d64aSHervé Poussineau     DeviceState *dev;
123d118d64aSHervé Poussineau     SysBusDevice *s;
124d118d64aSHervé Poussineau 
125b07734f9SDavid Woodhouse     dev = qemu_create_nic_device("mipsnet", true, NULL);
126b07734f9SDavid Woodhouse     if (!dev) {
127b07734f9SDavid Woodhouse         return;
128b07734f9SDavid Woodhouse     }
129d118d64aSHervé Poussineau 
1301356b98dSAndreas Färber     s = SYS_BUS_DEVICE(dev);
1313c6ef471SMarkus Armbruster     sysbus_realize_and_unref(s, &error_fatal);
132d118d64aSHervé Poussineau     sysbus_connect_irq(s, 0, irq);
133d118d64aSHervé Poussineau     memory_region_add_subregion(get_system_io(),
134d118d64aSHervé Poussineau                                 base,
135d118d64aSHervé Poussineau                                 sysbus_mmio_get_region(s, 0));
136d118d64aSHervé Poussineau }
137d118d64aSHervé Poussineau 
138f0fc6f8fSths static void
1393ef96221SMarcel Apfelbaum mips_mipssim_init(MachineState *machine)
140f0fc6f8fSths {
1413ef96221SMarcel Apfelbaum     const char *kernel_filename = machine->kernel_filename;
1423ef96221SMarcel Apfelbaum     const char *kernel_cmdline = machine->kernel_cmdline;
1433ef96221SMarcel Apfelbaum     const char *initrd_filename = machine->initrd_filename;
1445cea8590SPaul Brook     char *filename;
14523ebf23dSAvi Kivity     MemoryRegion *address_space_mem = get_system_memory();
146bdb75c79SPaolo Bonzini     MemoryRegion *isa = g_new(MemoryRegion, 1);
14723ebf23dSAvi Kivity     MemoryRegion *bios = g_new(MemoryRegion, 1);
1488543a806SPhilippe Mathieu-Daudé     Clock *cpuclk;
1497ee274c1SAndreas Färber     MIPSCPU *cpu;
15061c56c8cSAndreas Färber     CPUMIPSState *env;
151e16ad5b0SAurelien Jarno     ResetData *reset_info;
152b5334159Sths     int bios_size;
153f0fc6f8fSths 
1548543a806SPhilippe Mathieu-Daudé     cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
1558543a806SPhilippe Mathieu-Daudé #ifdef TARGET_MIPS64
1568543a806SPhilippe Mathieu-Daudé     clock_set_hz(cpuclk, 6000000); /* 6 MHz */
1578543a806SPhilippe Mathieu-Daudé #else
1588543a806SPhilippe Mathieu-Daudé     clock_set_hz(cpuclk, 12000000); /* 12 MHz */
1598543a806SPhilippe Mathieu-Daudé #endif
1608543a806SPhilippe Mathieu-Daudé 
161f0fc6f8fSths     /* Init CPUs. */
1628543a806SPhilippe Mathieu-Daudé     cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk);
1637ee274c1SAndreas Färber     env = &cpu->env;
1647ee274c1SAndreas Färber 
165b21e2380SMarkus Armbruster     reset_info = g_new0(ResetData, 1);
1662d44fc8eSAndreas Färber     reset_info->cpu = cpu;
167e16ad5b0SAurelien Jarno     reset_info->vector = env->active_tc.PC;
168e16ad5b0SAurelien Jarno     qemu_register_reset(main_cpu_reset, reset_info);
169f0fc6f8fSths 
170f0fc6f8fSths     /* Allocate RAM. */
1713fab7f23SPhilippe Mathieu-Daudé     memory_region_init_rom(bios, NULL, "mips_mipssim.bios", BIOS_SIZE,
172f8ed85acSMarkus Armbruster                            &error_fatal);
173f0fc6f8fSths 
174ceefaa3bSIgor Mammedov     memory_region_add_subregion(address_space_mem, 0, machine->ram);
175dcac9679Spbrook 
176dcac9679Spbrook     /* Map the BIOS / boot exception handler. */
17723ebf23dSAvi Kivity     memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
178f0fc6f8fSths     /* Load a BIOS / boot exception handler image. */
17959588beaSPaolo Bonzini     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, machine->firmware ?: BIOS_FILENAME);
1805cea8590SPaul Brook     if (filename) {
1815cea8590SPaul Brook         bios_size = load_image_targphys(filename, 0x1fc00000LL, BIOS_SIZE);
1827267c094SAnthony Liguori         g_free(filename);
1835cea8590SPaul Brook     } else {
1845cea8590SPaul Brook         bios_size = -1;
1855cea8590SPaul Brook     }
18622d5523dSAndreas Färber     if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
18759588beaSPaolo Bonzini         machine->firmware && !qtest_enabled()) {
188f0fc6f8fSths         /* Bail out if we have neither a kernel image nor boot vector code. */
18959588beaSPaolo Bonzini         error_report("Could not load MIPS bios '%s'", machine->firmware);
1902e985fe0SAurelien Jarno         exit(1);
191f0fc6f8fSths     } else {
192b5334159Sths         /* We have a boot vector start address. */
193b5dc7732Sths         env->active_tc.PC = (target_long)(int32_t)0xbfc00000;
194f0fc6f8fSths     }
195f0fc6f8fSths 
196f0fc6f8fSths     if (kernel_filename) {
197ceefaa3bSIgor Mammedov         loaderparams.ram_size = machine->ram_size;
1987df526e3Sths         loaderparams.kernel_filename = kernel_filename;
1997df526e3Sths         loaderparams.kernel_cmdline = kernel_cmdline;
2007df526e3Sths         loaderparams.initrd_filename = initrd_filename;
201e16ad5b0SAurelien Jarno         reset_info->vector = load_kernel();
202f0fc6f8fSths     }
203f0fc6f8fSths 
204f0fc6f8fSths     /* Init CPU internal devices. */
2055a975d43SPaolo Bonzini     cpu_mips_irq_init_cpu(cpu);
2065a975d43SPaolo Bonzini     cpu_mips_clock_init(cpu);
207f0fc6f8fSths 
208*e6f21933SBernhard Beschow     /*
209*e6f21933SBernhard Beschow      * Register 64 KB of ISA IO space at 0x1fd00000.  But without interrupts
210*e6f21933SBernhard Beschow      * (except for the hardcoded serial port interrupt) -device cannot work,
211*e6f21933SBernhard Beschow      * so do not expose the ISA bus to the user.
212*e6f21933SBernhard Beschow      */
213bdb75c79SPaolo Bonzini     memory_region_init_alias(isa, NULL, "isa_mmio",
214bdb75c79SPaolo Bonzini                              get_system_io(), 0, 0x00010000);
215bdb75c79SPaolo Bonzini     memory_region_add_subregion(get_system_memory(), 0x1fd00000, isa);
216f0fc6f8fSths 
21733dd6f44SAleksandar Markovic     /*
21833dd6f44SAleksandar Markovic      * A single 16450 sits at offset 0x3f8. It is attached to
21933dd6f44SAleksandar Markovic      * MIPS CPU INT2, which is interrupt 4.
22033dd6f44SAleksandar Markovic      */
2219fac5d88SMarc-André Lureau     if (serial_hd(0)) {
222cf3d932fSPhilippe Mathieu-Daudé         DeviceState *dev = qdev_new(TYPE_SERIAL_MM);
2239fac5d88SMarc-André Lureau 
2249fac5d88SMarc-André Lureau         qdev_prop_set_chr(dev, "chardev", serial_hd(0));
225cf3d932fSPhilippe Mathieu-Daudé         qdev_prop_set_uint8(dev, "regshift", 0);
226cf3d932fSPhilippe Mathieu-Daudé         qdev_prop_set_uint8(dev, "endianness", DEVICE_LITTLE_ENDIAN);
2273c6ef471SMarkus Armbruster         sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
2289fac5d88SMarc-André Lureau         sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, env->irq[4]);
229d9259178SMarc-André Lureau         sysbus_add_io(SYS_BUS_DEVICE(dev), 0x3f8,
230d9259178SMarc-André Lureau                       sysbus_mmio_get_region(SYS_BUS_DEVICE(dev), 0));
2319fac5d88SMarc-André Lureau     }
232f0fc6f8fSths 
233f0fc6f8fSths     /* MIPSnet uses the MIPS CPU INT0, which is interrupt 2. */
234b07734f9SDavid Woodhouse     mipsnet_init(0x4200, env->irq[2]);
235f0fc6f8fSths }
236f0fc6f8fSths 
237e264d29dSEduardo Habkost static void mips_mipssim_machine_init(MachineClass *mc)
238f80f9ec9SAnthony Liguori {
239e264d29dSEduardo Habkost     mc->desc = "MIPS MIPSsim platform";
240e264d29dSEduardo Habkost     mc->init = mips_mipssim_init;
2410fc52fd2SIgor Mammedov #ifdef TARGET_MIPS64
2420fc52fd2SIgor Mammedov     mc->default_cpu_type = MIPS_CPU_TYPE_NAME("5Kf");
2430fc52fd2SIgor Mammedov #else
2440fc52fd2SIgor Mammedov     mc->default_cpu_type = MIPS_CPU_TYPE_NAME("24Kf");
2450fc52fd2SIgor Mammedov #endif
246ceefaa3bSIgor Mammedov     mc->default_ram_id = "mips_mipssim.ram";
247f80f9ec9SAnthony Liguori }
248f80f9ec9SAnthony Liguori 
249e264d29dSEduardo Habkost DEFINE_MACHINE("mipssim", mips_mipssim_machine_init)
250