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"
31dfc56946SRichard Henderson #include "system/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"
3632cad1ffSPhilippe 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"
4332cad1ffSPhilippe Mathieu-Daudé #include "system/qtest.h"
4432cad1ffSPhilippe 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é
497df526e3Sths static struct _loaderparams {
507df526e3Sths int ram_size;
517df526e3Sths const char *kernel_filename;
527df526e3Sths const char *kernel_cmdline;
537df526e3Sths const char *initrd_filename;
547df526e3Sths } loaderparams;
557df526e3Sths
56e16ad5b0SAurelien Jarno typedef struct ResetData {
572d44fc8eSAndreas Färber MIPSCPU *cpu;
58e16ad5b0SAurelien Jarno uint64_t vector;
59e16ad5b0SAurelien Jarno } ResetData;
60e16ad5b0SAurelien Jarno
load_kernel(void)61dde98994SJiaxun Yang static uint64_t load_kernel(void)
62f0fc6f8fSths {
63dde98994SJiaxun Yang uint64_t entry, kernel_high, initrd_size;
64f0fc6f8fSths long kernel_size;
65c227f099SAnthony Liguori ram_addr_t initrd_offset;
66f0fc6f8fSths
674366e1dbSLiam Merwick kernel_size = load_elf(loaderparams.kernel_filename, NULL,
684366e1dbSLiam Merwick cpu_mips_kseg0_to_phys, NULL,
69dde98994SJiaxun Yang &entry, NULL,
70adc1a4a2SPhilippe Mathieu-Daudé &kernel_high, NULL,
71adc1a4a2SPhilippe Mathieu-Daudé TARGET_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB,
727ef295eaSPeter Crosthwaite EM_MIPS, 1, 0);
739d585eaaSPhilippe Mathieu-Daudé if (kernel_size < 0) {
74bd6e1d81SAlistair Francis error_report("could not load kernel '%s': %s",
753ee3122cSAurelien Jarno loaderparams.kernel_filename,
763ee3122cSAurelien Jarno load_elf_strerror(kernel_size));
77f0fc6f8fSths exit(1);
78f0fc6f8fSths }
79f0fc6f8fSths
80f0fc6f8fSths /* load initrd */
81f0fc6f8fSths initrd_size = 0;
82f0fc6f8fSths initrd_offset = 0;
837df526e3Sths if (loaderparams.initrd_filename) {
847df526e3Sths initrd_size = get_image_size(loaderparams.initrd_filename);
85f0fc6f8fSths if (initrd_size > 0) {
86acab36caSPhilippe Mathieu-Daudé initrd_offset = ROUND_UP(kernel_high, INITRD_PAGE_SIZE);
877df526e3Sths if (initrd_offset + initrd_size > loaderparams.ram_size) {
88bd6e1d81SAlistair Francis error_report("memory too small for initial ram disk '%s'",
897df526e3Sths loaderparams.initrd_filename);
90f0fc6f8fSths exit(1);
91f0fc6f8fSths }
92dcac9679Spbrook initrd_size = load_image_targphys(loaderparams.initrd_filename,
93dcac9679Spbrook initrd_offset, loaderparams.ram_size - initrd_offset);
94f0fc6f8fSths }
95f0fc6f8fSths if (initrd_size == (target_ulong) -1) {
96bd6e1d81SAlistair Francis error_report("could not load initial ram disk '%s'",
977df526e3Sths loaderparams.initrd_filename);
98f0fc6f8fSths exit(1);
99f0fc6f8fSths }
100f0fc6f8fSths }
101e16ad5b0SAurelien Jarno return entry;
102f0fc6f8fSths }
103f0fc6f8fSths
main_cpu_reset(void * opaque)104f0fc6f8fSths static void main_cpu_reset(void *opaque)
105f0fc6f8fSths {
106e16ad5b0SAurelien Jarno ResetData *s = (ResetData *)opaque;
1072d44fc8eSAndreas Färber CPUMIPSState *env = &s->cpu->env;
108f0fc6f8fSths
1092d44fc8eSAndreas Färber cpu_reset(CPU(s->cpu));
110aecf1376SNathan Froyd env->active_tc.PC = s->vector & ~(target_ulong)1;
111aecf1376SNathan Froyd if (s->vector & 1) {
112aecf1376SNathan Froyd env->hflags |= MIPS_HFLAG_M16;
113aecf1376SNathan Froyd }
114f0fc6f8fSths }
115f0fc6f8fSths
mipsnet_init(int base,qemu_irq irq)116b07734f9SDavid Woodhouse static void mipsnet_init(int base, qemu_irq irq)
117d118d64aSHervé Poussineau {
118d118d64aSHervé Poussineau DeviceState *dev;
119d118d64aSHervé Poussineau SysBusDevice *s;
120d118d64aSHervé Poussineau
121b07734f9SDavid Woodhouse dev = qemu_create_nic_device("mipsnet", true, NULL);
122b07734f9SDavid Woodhouse if (!dev) {
123b07734f9SDavid Woodhouse return;
124b07734f9SDavid Woodhouse }
125d118d64aSHervé Poussineau
1261356b98dSAndreas Färber s = SYS_BUS_DEVICE(dev);
1273c6ef471SMarkus Armbruster sysbus_realize_and_unref(s, &error_fatal);
128d118d64aSHervé Poussineau sysbus_connect_irq(s, 0, irq);
129d118d64aSHervé Poussineau memory_region_add_subregion(get_system_io(),
130d118d64aSHervé Poussineau base,
131d118d64aSHervé Poussineau sysbus_mmio_get_region(s, 0));
132d118d64aSHervé Poussineau }
133d118d64aSHervé Poussineau
134f0fc6f8fSths static void
mips_mipssim_init(MachineState * machine)1353ef96221SMarcel Apfelbaum mips_mipssim_init(MachineState *machine)
136f0fc6f8fSths {
1373ef96221SMarcel Apfelbaum const char *kernel_filename = machine->kernel_filename;
1383ef96221SMarcel Apfelbaum const char *kernel_cmdline = machine->kernel_cmdline;
1393ef96221SMarcel Apfelbaum const char *initrd_filename = machine->initrd_filename;
140*1b079a6eSPhilippe Mathieu-Daudé const char *bios_name = TARGET_BIG_ENDIAN ? "mips_bios.bin"
141*1b079a6eSPhilippe Mathieu-Daudé : "mipsel_bios.bin";
1425cea8590SPaul Brook char *filename;
14323ebf23dSAvi Kivity MemoryRegion *address_space_mem = get_system_memory();
144bdb75c79SPaolo Bonzini MemoryRegion *isa = g_new(MemoryRegion, 1);
14523ebf23dSAvi Kivity MemoryRegion *bios = g_new(MemoryRegion, 1);
1468543a806SPhilippe Mathieu-Daudé Clock *cpuclk;
1477ee274c1SAndreas Färber MIPSCPU *cpu;
14861c56c8cSAndreas Färber CPUMIPSState *env;
149e16ad5b0SAurelien Jarno ResetData *reset_info;
150b5334159Sths int bios_size;
151f0fc6f8fSths
1528543a806SPhilippe Mathieu-Daudé cpuclk = clock_new(OBJECT(machine), "cpu-refclk");
1538543a806SPhilippe Mathieu-Daudé #ifdef TARGET_MIPS64
1548543a806SPhilippe Mathieu-Daudé clock_set_hz(cpuclk, 6000000); /* 6 MHz */
1558543a806SPhilippe Mathieu-Daudé #else
1568543a806SPhilippe Mathieu-Daudé clock_set_hz(cpuclk, 12000000); /* 12 MHz */
1578543a806SPhilippe Mathieu-Daudé #endif
1588543a806SPhilippe Mathieu-Daudé
159f0fc6f8fSths /* Init CPUs. */
1603e8f019bSPhilippe Mathieu-Daudé cpu = mips_cpu_create_with_clock(machine->cpu_type, cpuclk,
1613e8f019bSPhilippe Mathieu-Daudé TARGET_BIG_ENDIAN);
1627ee274c1SAndreas Färber env = &cpu->env;
1637ee274c1SAndreas Färber
164b21e2380SMarkus Armbruster reset_info = g_new0(ResetData, 1);
1652d44fc8eSAndreas Färber reset_info->cpu = cpu;
166e16ad5b0SAurelien Jarno reset_info->vector = env->active_tc.PC;
167e16ad5b0SAurelien Jarno qemu_register_reset(main_cpu_reset, reset_info);
168f0fc6f8fSths
169f0fc6f8fSths /* Allocate RAM. */
1703fab7f23SPhilippe Mathieu-Daudé memory_region_init_rom(bios, NULL, "mips_mipssim.bios", BIOS_SIZE,
171f8ed85acSMarkus Armbruster &error_fatal);
172f0fc6f8fSths
173ceefaa3bSIgor Mammedov memory_region_add_subregion(address_space_mem, 0, machine->ram);
174dcac9679Spbrook
175dcac9679Spbrook /* Map the BIOS / boot exception handler. */
17623ebf23dSAvi Kivity memory_region_add_subregion(address_space_mem, 0x1fc00000LL, bios);
177f0fc6f8fSths /* Load a BIOS / boot exception handler image. */
178*1b079a6eSPhilippe Mathieu-Daudé filename = qemu_find_file(QEMU_FILE_TYPE_BIOS,
179*1b079a6eSPhilippe Mathieu-Daudé machine->firmware ?: bios_name);
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
208e6f21933SBernhard Beschow /*
209e6f21933SBernhard Beschow * Register 64 KB of ISA IO space at 0x1fd00000. But without interrupts
210e6f21933SBernhard Beschow * (except for the hardcoded serial port interrupt) -device cannot work,
211e6f21933SBernhard Beschow * so do not expose the ISA bus to the user.
212e6f21933SBernhard 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]);
2290068b069SPhilippe Mathieu-Daudé memory_region_add_subregion(get_system_io(), 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
mips_mipssim_machine_init(MachineClass * mc)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