blob: e04b49d3c5d8aae5468a1e03398dabe4d113bfa5 [file] [log] [blame]
thse16fe402006-12-06 21:38:37 +00001/*
2 * QEMU/MIPS pseudo-board
3 *
4 * emulates a simple machine with ISA-like bus.
5 * ISA IO space mapped to the 0x14000000 (PHYS) and
6 * ISA memory at the 0x10000000 (PHYS, 16Mb in size).
7 * All peripherial devices are attached to this "bus" with
8 * the standard PC ISA addresses.
9*/
Peter Maydellc6848222016-01-18 17:35:00 +000010#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010011#include "qapi/error.h"
Paolo Bonzini4771d752016-01-19 21:51:44 +010012#include "qemu-common.h"
13#include "cpu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010014#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010015#include "hw/mips/mips.h"
16#include "hw/mips/cpudevs.h"
17#include "hw/i386/pc.h"
18#include "hw/char/serial.h"
19#include "hw/isa/isa.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020020#include "net/net.h"
Philippe Mathieu-Daudé489983d2017-10-17 13:44:22 -030021#include "hw/net/ne2000-isa.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/sysemu.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010023#include "hw/boards.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010024#include "hw/block/flash.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/log.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010026#include "hw/mips/bios.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010027#include "hw/ide.h"
28#include "hw/loader.h"
Blue Swirlca20cf32009-09-20 14:58:02 +000029#include "elf.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010030#include "hw/timer/mc146818rtc.h"
Philippe Mathieu-Daudé47973a22018-03-08 23:39:24 +010031#include "hw/input/i8042.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "hw/timer/i8254.h"
Markus Armbrusterfa1d36d2014-10-07 13:59:13 +020033#include "sysemu/block-backend.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010034#include "exec/address-spaces.h"
Andreas Färberc9dd6a92013-07-29 16:05:33 +020035#include "sysemu/qtest.h"
Aurelien Jarno3ee31222017-07-27 01:56:13 +020036#include "qemu/error-report.h"
ths44cbbf12007-01-24 22:00:13 +000037
thse4bcb142007-12-02 04:51:10 +000038#define MAX_IDE_BUS 2
39
pbrook58126402006-10-29 15:38:28 +000040static const int ide_iobase[2] = { 0x1f0, 0x170 };
41static const int ide_iobase2[2] = { 0x3f6, 0x376 };
42static const int ide_irq[2] = { 14, 15 };
43
Blue Swirl64d7e9a2011-02-13 19:54:40 +000044static ISADevice *pit; /* PIT i8254 */
bellard697584a2005-08-21 09:41:56 +000045
ths1b660742007-12-07 01:13:37 +000046/* i8254 PIT is attached to the IRQ0 at PIC i8259 */
bellard6af0bf92005-07-02 14:58:51 +000047
ths7df526e2007-11-09 17:52:11 +000048static struct _loaderparams {
49 int ram_size;
50 const char *kernel_filename;
51 const char *kernel_cmdline;
52 const char *initrd_filename;
53} loaderparams;
54
Avi Kivitya8170e52012-10-23 12:30:10 +020055static void mips_qemu_write (void *opaque, hwaddr addr,
Avi Kivity0ae16452011-08-08 22:22:38 +030056 uint64_t val, unsigned size)
ths6ae81772006-12-06 17:48:52 +000057{
58 if ((addr & 0xffff) == 0 && val == 42)
Eric Blakecf83f142017-05-15 16:41:13 -050059 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
ths6ae81772006-12-06 17:48:52 +000060 else if ((addr & 0xffff) == 4 && val == 42)
Eric Blakecf83f142017-05-15 16:41:13 -050061 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
ths6ae81772006-12-06 17:48:52 +000062}
63
Avi Kivitya8170e52012-10-23 12:30:10 +020064static uint64_t mips_qemu_read (void *opaque, hwaddr addr,
Avi Kivity0ae16452011-08-08 22:22:38 +030065 unsigned size)
ths6ae81772006-12-06 17:48:52 +000066{
67 return 0;
68}
69
Avi Kivity0ae16452011-08-08 22:22:38 +030070static const MemoryRegionOps mips_qemu_ops = {
71 .read = mips_qemu_read,
72 .write = mips_qemu_write,
73 .endianness = DEVICE_NATIVE_ENDIAN,
ths6ae81772006-12-06 17:48:52 +000074};
75
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +010076typedef struct ResetData {
Andreas Färberfa156e52012-05-05 14:23:25 +020077 MIPSCPU *cpu;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +010078 uint64_t vector;
79} ResetData;
80
81static int64_t load_kernel(void)
ths6ae81772006-12-06 17:48:52 +000082{
Aurelien Jarno409dbce2010-03-14 21:20:59 +010083 int64_t entry, kernel_high;
Aurelien Jarnoe90e7952009-11-15 23:04:20 +010084 long kernel_size, initrd_size, params_size;
Anthony Liguoric227f092009-10-01 16:12:16 -050085 ram_addr_t initrd_offset;
Aurelien Jarnoe90e7952009-11-15 23:04:20 +010086 uint32_t *params_buf;
Blue Swirlca20cf32009-09-20 14:58:02 +000087 int big_endian;
ths6ae81772006-12-06 17:48:52 +000088
Blue Swirlca20cf32009-09-20 14:58:02 +000089#ifdef TARGET_WORDS_BIGENDIAN
90 big_endian = 1;
91#else
92 big_endian = 0;
93#endif
Aurelien Jarno409dbce2010-03-14 21:20:59 +010094 kernel_size = load_elf(loaderparams.kernel_filename, cpu_mips_kseg0_to_phys,
95 NULL, (uint64_t *)&entry, NULL,
96 (uint64_t *)&kernel_high, big_endian,
Peter Crosthwaite7ef295e2016-03-04 11:30:21 +000097 EM_MIPS, 1, 0);
thsc570fd12006-12-21 01:19:56 +000098 if (kernel_size >= 0) {
99 if ((entry & ~0x7fffffffULL) == 0x80000000)
ths5dc4b742006-12-21 13:48:28 +0000100 entry = (int32_t)entry;
thsc570fd12006-12-21 01:19:56 +0000101 } else {
Alistair Francisbd6e1d82018-02-03 09:43:06 +0100102 error_report("could not load kernel '%s': %s",
Aurelien Jarno3ee31222017-07-27 01:56:13 +0200103 loaderparams.kernel_filename,
104 load_elf_strerror(kernel_size));
ths9042c0e2006-12-23 14:18:40 +0000105 exit(1);
ths6ae81772006-12-06 17:48:52 +0000106 }
107
108 /* load initrd */
109 initrd_size = 0;
ths74287112007-04-01 17:56:37 +0000110 initrd_offset = 0;
ths7df526e2007-11-09 17:52:11 +0000111 if (loaderparams.initrd_filename) {
112 initrd_size = get_image_size (loaderparams.initrd_filename);
ths74287112007-04-01 17:56:37 +0000113 if (initrd_size > 0) {
James Hogan05b32742013-06-27 08:35:27 +0100114 initrd_offset = (kernel_high + ~INITRD_PAGE_MASK) & INITRD_PAGE_MASK;
ths74287112007-04-01 17:56:37 +0000115 if (initrd_offset + initrd_size > ram_size) {
Alistair Francisbd6e1d82018-02-03 09:43:06 +0100116 error_report("memory too small for initial ram disk '%s'",
117 loaderparams.initrd_filename);
ths74287112007-04-01 17:56:37 +0000118 exit(1);
119 }
pbrookdcac9672009-04-09 20:05:49 +0000120 initrd_size = load_image_targphys(loaderparams.initrd_filename,
121 initrd_offset,
122 ram_size - initrd_offset);
ths74287112007-04-01 17:56:37 +0000123 }
ths6ae81772006-12-06 17:48:52 +0000124 if (initrd_size == (target_ulong) -1) {
Alistair Francisbd6e1d82018-02-03 09:43:06 +0100125 error_report("could not load initial ram disk '%s'",
126 loaderparams.initrd_filename);
ths6ae81772006-12-06 17:48:52 +0000127 exit(1);
128 }
129 }
130
131 /* Store command line. */
Aurelien Jarnoe90e7952009-11-15 23:04:20 +0100132 params_size = 264;
Anthony Liguori7267c092011-08-20 22:09:37 -0500133 params_buf = g_malloc(params_size);
ths6ae81772006-12-06 17:48:52 +0000134
Aurelien Jarnoe90e7952009-11-15 23:04:20 +0100135 params_buf[0] = tswap32(ram_size);
136 params_buf[1] = tswap32(0x12345678);
137
138 if (initrd_size > 0) {
Aurelien Jarno409dbce2010-03-14 21:20:59 +0100139 snprintf((char *)params_buf + 8, 256, "rd_start=0x%" PRIx64 " rd_size=%li %s",
140 cpu_mips_phys_to_kseg0(NULL, initrd_offset),
Aurelien Jarnoe90e7952009-11-15 23:04:20 +0100141 initrd_size, loaderparams.kernel_cmdline);
142 } else {
143 snprintf((char *)params_buf + 8, 256, "%s", loaderparams.kernel_cmdline);
144 }
145
146 rom_add_blob_fixed("params", params_buf, params_size,
147 (16 << 20) - 264);
148
Gonglei3ad9fd52015-04-28 17:11:02 +0800149 g_free(params_buf);
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100150 return entry;
ths6ae81772006-12-06 17:48:52 +0000151}
152
153static void main_cpu_reset(void *opaque)
154{
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100155 ResetData *s = (ResetData *)opaque;
Andreas Färberfa156e52012-05-05 14:23:25 +0200156 CPUMIPSState *env = &s->cpu->env;
ths6ae81772006-12-06 17:48:52 +0000157
Andreas Färberfa156e52012-05-05 14:23:25 +0200158 cpu_reset(CPU(s->cpu));
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100159 env->active_tc.PC = s->vector;
ths6ae81772006-12-06 17:48:52 +0000160}
bellard66a93e02006-04-26 22:06:55 +0000161
thsb305b5b2008-04-20 06:28:28 +0000162static const int sector_len = 32 * 1024;
ths70705262007-02-18 00:10:59 +0000163static
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300164void mips_r4k_init(MachineState *machine)
bellard6af0bf92005-07-02 14:58:51 +0000165{
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300166 ram_addr_t ram_size = machine->ram_size;
Marcel Apfelbaum3ef96222014-05-07 17:42:57 +0300167 const char *kernel_filename = machine->kernel_filename;
168 const char *kernel_cmdline = machine->kernel_cmdline;
169 const char *initrd_filename = machine->initrd_filename;
Paul Brook5cea8592009-05-30 00:52:44 +0100170 char *filename;
Avi Kivity0ae16452011-08-08 22:22:38 +0300171 MemoryRegion *address_space_mem = get_system_memory();
172 MemoryRegion *ram = g_new(MemoryRegion, 1);
Avi Kivitycfe5f012011-08-04 15:55:30 +0300173 MemoryRegion *bios;
Avi Kivity0ae16452011-08-08 22:22:38 +0300174 MemoryRegion *iomem = g_new(MemoryRegion, 1);
Hervé Poussineau0c109622015-02-01 09:12:53 +0100175 MemoryRegion *isa_io = g_new(MemoryRegion, 1);
176 MemoryRegion *isa_mem = g_new(MemoryRegion, 1);
thsf7bcd4e2007-01-06 01:37:51 +0000177 int bios_size;
Andreas Färber9ac67e22012-05-05 14:21:43 +0200178 MIPSCPU *cpu;
Andreas Färber61c56c82012-03-14 01:38:23 +0100179 CPUMIPSState *env;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100180 ResetData *reset_info;
pbrook58126402006-10-29 15:38:28 +0000181 int i;
pbrookd537cf62007-04-07 18:14:41 +0000182 qemu_irq *i8259;
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100183 ISABus *isa_bus;
Gerd Hoffmannf455e982009-08-28 15:47:03 +0200184 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200185 DriveInfo *dinfo;
Blue Swirl3d08ff62010-03-29 19:23:56 +0000186 int be;
bellardc68ea702005-11-21 23:33:12 +0000187
ths33d68b52007-03-18 00:30:29 +0000188 /* init CPUs */
Igor Mammedov5daab282017-10-05 15:51:14 +0200189 cpu = MIPS_CPU(cpu_create(machine->cpu_type));
Andreas Färber9ac67e22012-05-05 14:21:43 +0200190 env = &cpu->env;
191
Anthony Liguori7267c092011-08-20 22:09:37 -0500192 reset_info = g_malloc0(sizeof(ResetData));
Andreas Färberfa156e52012-05-05 14:23:25 +0200193 reset_info->cpu = cpu;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100194 reset_info->vector = env->active_tc.PC;
195 qemu_register_reset(main_cpu_reset, reset_info);
bellardc68ea702005-11-21 23:33:12 +0000196
bellard6af0bf92005-07-02 14:58:51 +0000197 /* allocate RAM */
aurel320ccff152009-01-24 15:07:25 +0000198 if (ram_size > (256 << 20)) {
Alistair Francisbd6e1d82018-02-03 09:43:06 +0100199 error_report("Too much memory for this machine: %dMB, maximum 256MB",
200 ((unsigned int)ram_size / (1 << 20)));
aurel320ccff152009-01-24 15:07:25 +0000201 exit(1);
202 }
Dirk Müller6a926fb2015-03-24 22:28:15 +0100203 memory_region_allocate_system_memory(ram, NULL, "mips_r4k.ram", ram_size);
pbrookdcac9672009-04-09 20:05:49 +0000204
Avi Kivity0ae16452011-08-08 22:22:38 +0300205 memory_region_add_subregion(address_space_mem, 0, ram);
bellard66a93e02006-04-26 22:06:55 +0000206
Paolo Bonzini2c9b15c2013-06-06 05:41:28 -0400207 memory_region_init_io(iomem, NULL, &mips_qemu_ops, NULL, "mips-qemu", 0x10000);
Avi Kivity0ae16452011-08-08 22:22:38 +0300208 memory_region_add_subregion(address_space_mem, 0x1fbf0000, iomem);
ths6ae81772006-12-06 17:48:52 +0000209
bellard66a93e02006-04-26 22:06:55 +0000210 /* Try to load a BIOS image. If this fails, we continue regardless,
211 but initialize the hardware ourselves. When a kernel gets
212 preloaded we also initialize the hardware, since the BIOS wasn't
213 run. */
j_mayer1192dad2007-10-05 13:08:35 +0000214 if (bios_name == NULL)
215 bios_name = BIOS_FILENAME;
Paul Brook5cea8592009-05-30 00:52:44 +0100216 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
217 if (filename) {
218 bios_size = get_image_size(filename);
219 } else {
220 bios_size = -1;
221 }
Blue Swirl3d08ff62010-03-29 19:23:56 +0000222#ifdef TARGET_WORDS_BIGENDIAN
223 be = 1;
224#else
225 be = 0;
226#endif
ths2909b292007-01-06 02:24:15 +0000227 if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
Avi Kivitycfe5f012011-08-04 15:55:30 +0300228 bios = g_new(MemoryRegion, 1);
Peter Maydell98a99ce2017-07-07 15:42:53 +0100229 memory_region_init_ram(bios, NULL, "mips_r4k.bios", BIOS_SIZE,
Markus Armbrusterf8ed85a2015-09-11 16:51:43 +0200230 &error_fatal);
Avi Kivitycfe5f012011-08-04 15:55:30 +0300231 memory_region_set_readonly(bios, true);
232 memory_region_add_subregion(get_system_memory(), 0x1fc00000, bios);
Anthony Liguori01e04512011-08-25 14:39:18 -0500233
Paul Brook5cea8592009-05-30 00:52:44 +0100234 load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200235 } else if ((dinfo = drive_get(IF_PFLASH, 0, 0)) != NULL) {
thsb305b5b2008-04-20 06:28:28 +0000236 uint32_t mips_rom = 0x00400000;
Avi Kivitycfe5f012011-08-04 15:55:30 +0300237 if (!pflash_cfi01_register(0x1fc00000, NULL, "mips_r4k.bios", mips_rom,
Markus Armbruster4be74632014-10-07 13:59:18 +0200238 blk_by_legacy_dinfo(dinfo),
Markus Armbrusterfa1d36d2014-10-07 13:59:13 +0200239 sector_len, mips_rom / sector_len,
Anthony Liguori01e04512011-08-25 14:39:18 -0500240 4, 0, 0, 0, 0, be)) {
thsb305b5b2008-04-20 06:28:28 +0000241 fprintf(stderr, "qemu: Error registering flash memory.\n");
242 }
Andreas Färberc9dd6a92013-07-29 16:05:33 +0200243 } else if (!qtest_enabled()) {
Alistair Francis8297be82017-09-11 12:52:53 -0700244 /* not fatal */
Alistair Francisb62e39b2017-09-11 12:52:56 -0700245 warn_report("could not load MIPS bios '%s'", bios_name);
Paul Brook5cea8592009-05-30 00:52:44 +0100246 }
Daniel P. Berrangeef1e1e02015-08-26 12:17:18 +0100247 g_free(filename);
bellard66a93e02006-04-26 22:06:55 +0000248
bellard66a93e02006-04-26 22:06:55 +0000249 if (kernel_filename) {
ths7df526e2007-11-09 17:52:11 +0000250 loaderparams.ram_size = ram_size;
251 loaderparams.kernel_filename = kernel_filename;
252 loaderparams.kernel_cmdline = kernel_cmdline;
253 loaderparams.initrd_filename = initrd_filename;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100254 reset_info->vector = load_kernel();
bellard6af0bf92005-07-02 14:58:51 +0000255 }
bellard6af0bf92005-07-02 14:58:51 +0000256
thse16fe402006-12-06 21:38:37 +0000257 /* Init CPU internal devices */
Paolo Bonzini5a975d42016-03-15 14:32:19 +0100258 cpu_mips_irq_init_cpu(cpu);
259 cpu_mips_clock_init(cpu);
bellard6af0bf92005-07-02 14:58:51 +0000260
Hervé Poussineau0c109622015-02-01 09:12:53 +0100261 /* ISA bus: IO space at 0x14000000, mem space at 0x10000000 */
262 memory_region_init_alias(isa_io, NULL, "isa-io",
263 get_system_io(), 0, 0x00010000);
264 memory_region_init(isa_mem, NULL, "isa-mem", 0x01000000);
265 memory_region_add_subregion(get_system_memory(), 0x14000000, isa_io);
266 memory_region_add_subregion(get_system_memory(), 0x10000000, isa_mem);
Markus Armbrusterd10e5432015-12-17 17:35:18 +0100267 isa_bus = isa_bus_new(NULL, isa_mem, get_system_io(), &error_abort);
Hervé Poussineau0c109622015-02-01 09:12:53 +0100268
pbrookd537cf62007-04-07 18:14:41 +0000269 /* The PIC is attached to the MIPS CPU INT0 pin */
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100270 i8259 = i8259_init(isa_bus, env->irq[2]);
271 isa_bus_irqs(isa_bus, i8259);
pbrookd537cf62007-04-07 18:14:41 +0000272
Philippe Mathieu-Daudé6c646a12017-10-17 13:44:16 -0300273 mc146818_rtc_init(isa_bus, 2000, NULL);
thsafdfa782006-12-07 18:15:35 +0000274
Philippe Mathieu-Daudéacf695e2017-10-17 13:44:15 -0300275 pit = i8254_pit_init(isa_bus, 0x40, 0, NULL);
thsafdfa782006-12-07 18:15:35 +0000276
Peter Maydelldef337f2018-04-20 15:52:46 +0100277 serial_hds_isa_init(isa_bus, 0, MAX_ISA_SERIAL_PORTS);
thseddbd282006-12-23 00:23:19 +0000278
Aurelien Jarnof642dfc2012-09-08 17:02:29 +0200279 isa_vga_init(isa_bus);
bellard9827e952005-07-02 15:26:04 +0000280
Stefan Hajnoczia005d072012-07-24 16:35:11 +0100281 if (nd_table[0].used)
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100282 isa_ne2000_init(isa_bus, 0x300, 9, &nd_table[0]);
pbrook58126402006-10-29 15:38:28 +0000283
John Snowd8f94e12014-10-01 14:19:27 -0400284 ide_drive_get(hd, ARRAY_SIZE(hd));
thse4bcb142007-12-02 04:51:10 +0000285 for(i = 0; i < MAX_IDE_BUS; i++)
Hervé Poussineau48a18b32011-12-15 22:09:51 +0100286 isa_ide_init(isa_bus, ide_iobase[i], ide_iobase2[i], ide_irq[i],
thse4bcb142007-12-02 04:51:10 +0000287 hd[MAX_IDE_DEVS * i],
288 hd[MAX_IDE_DEVS * i + 1]);
ths70705262007-02-18 00:10:59 +0000289
Philippe Mathieu-Daudé47973a22018-03-08 23:39:24 +0100290 isa_create_simple(isa_bus, TYPE_I8042);
bellard6af0bf92005-07-02 14:58:51 +0000291}
292
Eduardo Habkoste264d292015-09-04 15:37:08 -0300293static void mips_machine_init(MachineClass *mc)
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500294{
Eduardo Habkoste264d292015-09-04 15:37:08 -0300295 mc->desc = "mips r4k platform";
296 mc->init = mips_r4k_init;
Markus Armbruster20598392017-02-15 11:05:40 +0100297 mc->block_default_type = IF_IDE;
Igor Mammedov5daab282017-10-05 15:51:14 +0200298#ifdef TARGET_MIPS64
299 mc->default_cpu_type = MIPS_CPU_TYPE_NAME("R4000");
300#else
301 mc->default_cpu_type = MIPS_CPU_TYPE_NAME("24Kf");
302#endif
303
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500304}
305
Eduardo Habkoste264d292015-09-04 15:37:08 -0300306DEFINE_MACHINE("mips", mips_machine_init)