blob: 2834a46d52d8721a7615190093632ea674d859fe [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*/
pbrook87ecb682007-11-17 17:14:51 +000010#include "hw.h"
11#include "mips.h"
Blue Swirlb970ea82010-03-27 07:26:16 +000012#include "mips_cpudevs.h"
pbrook87ecb682007-11-17 17:14:51 +000013#include "pc.h"
14#include "isa.h"
15#include "net.h"
16#include "sysemu.h"
17#include "boards.h"
thsb305b5b2008-04-20 06:28:28 +000018#include "flash.h"
blueswir13b3fb322008-10-04 07:20:07 +000019#include "qemu-log.h"
Paul Brookbba831e2009-05-19 14:52:42 +010020#include "mips-bios.h"
Gerd Hoffmannec820262009-08-20 15:22:19 +020021#include "ide.h"
Blue Swirlca20cf32009-09-20 14:58:02 +000022#include "loader.h"
23#include "elf.h"
Isaku Yamahata1d914fa2010-05-14 16:29:17 +090024#include "mc146818rtc.h"
Blue Swirl24463332010-08-24 15:22:24 +000025#include "blockdev.h"
ths44cbbf12007-01-24 22:00:13 +000026
thse4bcb142007-12-02 04:51:10 +000027#define MAX_IDE_BUS 2
28
pbrook58126402006-10-29 15:38:28 +000029static const int ide_iobase[2] = { 0x1f0, 0x170 };
30static const int ide_iobase2[2] = { 0x3f6, 0x376 };
31static const int ide_irq[2] = { 14, 15 };
32
Blue Swirl64d7e9a2011-02-13 19:54:40 +000033static ISADevice *pit; /* PIT i8254 */
bellard697584a2005-08-21 09:41:56 +000034
ths1b660742007-12-07 01:13:37 +000035/* i8254 PIT is attached to the IRQ0 at PIC i8259 */
bellard6af0bf92005-07-02 14:58:51 +000036
ths7df526e2007-11-09 17:52:11 +000037static struct _loaderparams {
38 int ram_size;
39 const char *kernel_filename;
40 const char *kernel_cmdline;
41 const char *initrd_filename;
42} loaderparams;
43
Anthony Liguoric227f092009-10-01 16:12:16 -050044static void mips_qemu_writel (void *opaque, target_phys_addr_t addr,
ths6ae81772006-12-06 17:48:52 +000045 uint32_t val)
46{
47 if ((addr & 0xffff) == 0 && val == 42)
48 qemu_system_reset_request ();
49 else if ((addr & 0xffff) == 4 && val == 42)
50 qemu_system_shutdown_request ();
51}
52
Anthony Liguoric227f092009-10-01 16:12:16 -050053static uint32_t mips_qemu_readl (void *opaque, target_phys_addr_t addr)
ths6ae81772006-12-06 17:48:52 +000054{
55 return 0;
56}
57
Blue Swirld60efc62009-08-25 18:29:31 +000058static CPUWriteMemoryFunc * const mips_qemu_write[] = {
ths6ae81772006-12-06 17:48:52 +000059 &mips_qemu_writel,
60 &mips_qemu_writel,
61 &mips_qemu_writel,
62};
63
Blue Swirld60efc62009-08-25 18:29:31 +000064static CPUReadMemoryFunc * const mips_qemu_read[] = {
ths6ae81772006-12-06 17:48:52 +000065 &mips_qemu_readl,
66 &mips_qemu_readl,
67 &mips_qemu_readl,
68};
69
70static int mips_qemu_iomemtype = 0;
71
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +010072typedef struct ResetData {
73 CPUState *env;
74 uint64_t vector;
75} ResetData;
76
77static int64_t load_kernel(void)
ths6ae81772006-12-06 17:48:52 +000078{
Aurelien Jarno409dbce2010-03-14 21:20:59 +010079 int64_t entry, kernel_high;
Aurelien Jarnoe90e7952009-11-15 23:04:20 +010080 long kernel_size, initrd_size, params_size;
Anthony Liguoric227f092009-10-01 16:12:16 -050081 ram_addr_t initrd_offset;
Aurelien Jarnoe90e7952009-11-15 23:04:20 +010082 uint32_t *params_buf;
Blue Swirlca20cf32009-09-20 14:58:02 +000083 int big_endian;
ths6ae81772006-12-06 17:48:52 +000084
Blue Swirlca20cf32009-09-20 14:58:02 +000085#ifdef TARGET_WORDS_BIGENDIAN
86 big_endian = 1;
87#else
88 big_endian = 0;
89#endif
Aurelien Jarno409dbce2010-03-14 21:20:59 +010090 kernel_size = load_elf(loaderparams.kernel_filename, cpu_mips_kseg0_to_phys,
91 NULL, (uint64_t *)&entry, NULL,
92 (uint64_t *)&kernel_high, big_endian,
93 ELF_MACHINE, 1);
thsc570fd12006-12-21 01:19:56 +000094 if (kernel_size >= 0) {
95 if ((entry & ~0x7fffffffULL) == 0x80000000)
ths5dc4b742006-12-21 13:48:28 +000096 entry = (int32_t)entry;
thsc570fd12006-12-21 01:19:56 +000097 } else {
ths9042c0e2006-12-23 14:18:40 +000098 fprintf(stderr, "qemu: could not load kernel '%s'\n",
ths7df526e2007-11-09 17:52:11 +000099 loaderparams.kernel_filename);
ths9042c0e2006-12-23 14:18:40 +0000100 exit(1);
ths6ae81772006-12-06 17:48:52 +0000101 }
102
103 /* load initrd */
104 initrd_size = 0;
ths74287112007-04-01 17:56:37 +0000105 initrd_offset = 0;
ths7df526e2007-11-09 17:52:11 +0000106 if (loaderparams.initrd_filename) {
107 initrd_size = get_image_size (loaderparams.initrd_filename);
ths74287112007-04-01 17:56:37 +0000108 if (initrd_size > 0) {
109 initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
110 if (initrd_offset + initrd_size > ram_size) {
111 fprintf(stderr,
112 "qemu: memory too small for initial ram disk '%s'\n",
ths7df526e2007-11-09 17:52:11 +0000113 loaderparams.initrd_filename);
ths74287112007-04-01 17:56:37 +0000114 exit(1);
115 }
pbrookdcac9672009-04-09 20:05:49 +0000116 initrd_size = load_image_targphys(loaderparams.initrd_filename,
117 initrd_offset,
118 ram_size - initrd_offset);
ths74287112007-04-01 17:56:37 +0000119 }
ths6ae81772006-12-06 17:48:52 +0000120 if (initrd_size == (target_ulong) -1) {
121 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
ths7df526e2007-11-09 17:52:11 +0000122 loaderparams.initrd_filename);
ths6ae81772006-12-06 17:48:52 +0000123 exit(1);
124 }
125 }
126
127 /* Store command line. */
Aurelien Jarnoe90e7952009-11-15 23:04:20 +0100128 params_size = 264;
129 params_buf = qemu_malloc(params_size);
ths6ae81772006-12-06 17:48:52 +0000130
Aurelien Jarnoe90e7952009-11-15 23:04:20 +0100131 params_buf[0] = tswap32(ram_size);
132 params_buf[1] = tswap32(0x12345678);
133
134 if (initrd_size > 0) {
Aurelien Jarno409dbce2010-03-14 21:20:59 +0100135 snprintf((char *)params_buf + 8, 256, "rd_start=0x%" PRIx64 " rd_size=%li %s",
136 cpu_mips_phys_to_kseg0(NULL, initrd_offset),
Aurelien Jarnoe90e7952009-11-15 23:04:20 +0100137 initrd_size, loaderparams.kernel_cmdline);
138 } else {
139 snprintf((char *)params_buf + 8, 256, "%s", loaderparams.kernel_cmdline);
140 }
141
142 rom_add_blob_fixed("params", params_buf, params_size,
143 (16 << 20) - 264);
144
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100145 return entry;
ths6ae81772006-12-06 17:48:52 +0000146}
147
148static void main_cpu_reset(void *opaque)
149{
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100150 ResetData *s = (ResetData *)opaque;
151 CPUState *env = s->env;
ths6ae81772006-12-06 17:48:52 +0000152
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100153 cpu_reset(env);
154 env->active_tc.PC = s->vector;
ths6ae81772006-12-06 17:48:52 +0000155}
bellard66a93e02006-04-26 22:06:55 +0000156
thsb305b5b2008-04-20 06:28:28 +0000157static const int sector_len = 32 * 1024;
ths70705262007-02-18 00:10:59 +0000158static
Anthony Liguoric227f092009-10-01 16:12:16 -0500159void mips_r4k_init (ram_addr_t ram_size,
aliguori3023f332009-01-16 19:04:14 +0000160 const char *boot_device,
bellard6af0bf92005-07-02 14:58:51 +0000161 const char *kernel_filename, const char *kernel_cmdline,
j_mayer94fc95c2007-03-05 19:44:02 +0000162 const char *initrd_filename, const char *cpu_model)
bellard6af0bf92005-07-02 14:58:51 +0000163{
Paul Brook5cea8592009-05-30 00:52:44 +0100164 char *filename;
Anthony Liguoric227f092009-10-01 16:12:16 -0500165 ram_addr_t ram_offset;
166 ram_addr_t bios_offset;
thsf7bcd4e2007-01-06 01:37:51 +0000167 int bios_size;
bellardc68ea702005-11-21 23:33:12 +0000168 CPUState *env;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100169 ResetData *reset_info;
pbrook58126402006-10-29 15:38:28 +0000170 int i;
pbrookd537cf62007-04-07 18:14:41 +0000171 qemu_irq *i8259;
Gerd Hoffmannf455e982009-08-28 15:47:03 +0200172 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200173 DriveInfo *dinfo;
Blue Swirl3d08ff62010-03-29 19:23:56 +0000174 int be;
bellardc68ea702005-11-21 23:33:12 +0000175
ths33d68b52007-03-18 00:30:29 +0000176 /* init CPUs */
177 if (cpu_model == NULL) {
ths60aa19a2007-04-01 12:36:18 +0000178#ifdef TARGET_MIPS64
ths33d68b52007-03-18 00:30:29 +0000179 cpu_model = "R4000";
180#else
ths1c32f432007-04-28 21:07:41 +0000181 cpu_model = "24Kf";
ths33d68b52007-03-18 00:30:29 +0000182#endif
183 }
bellardaaed9092007-11-10 15:15:54 +0000184 env = cpu_init(cpu_model);
185 if (!env) {
186 fprintf(stderr, "Unable to find CPU definition\n");
187 exit(1);
188 }
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100189 reset_info = qemu_mallocz(sizeof(ResetData));
190 reset_info->env = env;
191 reset_info->vector = env->active_tc.PC;
192 qemu_register_reset(main_cpu_reset, reset_info);
bellardc68ea702005-11-21 23:33:12 +0000193
bellard6af0bf92005-07-02 14:58:51 +0000194 /* allocate RAM */
aurel320ccff152009-01-24 15:07:25 +0000195 if (ram_size > (256 << 20)) {
196 fprintf(stderr,
197 "qemu: Too much memory for this machine: %d MB, maximum 256 MB\n",
198 ((unsigned int)ram_size / (1 << 20)));
199 exit(1);
200 }
Alex Williamson1724f042010-06-25 11:09:35 -0600201 ram_offset = qemu_ram_alloc(NULL, "mips_r4k.ram", ram_size);
pbrookdcac9672009-04-09 20:05:49 +0000202
203 cpu_register_physical_memory(0, ram_size, ram_offset | IO_MEM_RAM);
bellard66a93e02006-04-26 22:06:55 +0000204
ths6ae81772006-12-06 17:48:52 +0000205 if (!mips_qemu_iomemtype) {
Avi Kivity1eed09c2009-06-14 11:38:51 +0300206 mips_qemu_iomemtype = cpu_register_io_memory(mips_qemu_read,
Alexander Graf2507c122010-12-08 12:05:37 +0100207 mips_qemu_write, NULL,
208 DEVICE_NATIVE_ENDIAN);
ths6ae81772006-12-06 17:48:52 +0000209 }
210 cpu_register_physical_memory(0x1fbf0000, 0x10000, mips_qemu_iomemtype);
211
bellard66a93e02006-04-26 22:06:55 +0000212 /* Try to load a BIOS image. If this fails, we continue regardless,
213 but initialize the hardware ourselves. When a kernel gets
214 preloaded we also initialize the hardware, since the BIOS wasn't
215 run. */
j_mayer1192dad2007-10-05 13:08:35 +0000216 if (bios_name == NULL)
217 bios_name = BIOS_FILENAME;
Paul Brook5cea8592009-05-30 00:52:44 +0100218 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
219 if (filename) {
220 bios_size = get_image_size(filename);
221 } else {
222 bios_size = -1;
223 }
Blue Swirl3d08ff62010-03-29 19:23:56 +0000224#ifdef TARGET_WORDS_BIGENDIAN
225 be = 1;
226#else
227 be = 0;
228#endif
ths2909b292007-01-06 02:24:15 +0000229 if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
Alex Williamson1724f042010-06-25 11:09:35 -0600230 bios_offset = qemu_ram_alloc(NULL, "mips_r4k.bios", BIOS_SIZE);
pbrookdcac9672009-04-09 20:05:49 +0000231 cpu_register_physical_memory(0x1fc00000, BIOS_SIZE,
232 bios_offset | IO_MEM_ROM);
233
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;
Alex Williamson1724f042010-06-25 11:09:35 -0600237 bios_offset = qemu_ram_alloc(NULL, "mips_r4k.bios", mips_rom);
pbrookdcac9672009-04-09 20:05:49 +0000238 if (!pflash_cfi01_register(0x1fc00000, bios_offset,
Blue Swirl3d08ff62010-03-29 19:23:56 +0000239 dinfo->bdrv, sector_len,
240 mips_rom / sector_len,
241 4, 0, 0, 0, 0, be)) {
thsb305b5b2008-04-20 06:28:28 +0000242 fprintf(stderr, "qemu: Error registering flash memory.\n");
243 }
244 }
245 else {
bellard66a93e02006-04-26 22:06:55 +0000246 /* not fatal */
247 fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",
Paul Brook5cea8592009-05-30 00:52:44 +0100248 bios_name);
249 }
250 if (filename) {
251 qemu_free(filename);
bellard6af0bf92005-07-02 14:58:51 +0000252 }
bellard66a93e02006-04-26 22:06:55 +0000253
bellard66a93e02006-04-26 22:06:55 +0000254 if (kernel_filename) {
ths7df526e2007-11-09 17:52:11 +0000255 loaderparams.ram_size = ram_size;
256 loaderparams.kernel_filename = kernel_filename;
257 loaderparams.kernel_cmdline = kernel_cmdline;
258 loaderparams.initrd_filename = initrd_filename;
Aurelien Jarnoe16ad5b2009-11-14 01:04:29 +0100259 reset_info->vector = load_kernel();
bellard6af0bf92005-07-02 14:58:51 +0000260 }
bellard6af0bf92005-07-02 14:58:51 +0000261
thse16fe402006-12-06 21:38:37 +0000262 /* Init CPU internal devices */
pbrookd537cf62007-04-07 18:14:41 +0000263 cpu_mips_irq_init_cpu(env);
bellardc68ea702005-11-21 23:33:12 +0000264 cpu_mips_clock_init(env);
bellard6af0bf92005-07-02 14:58:51 +0000265
pbrookd537cf62007-04-07 18:14:41 +0000266 /* The PIC is attached to the MIPS CPU INT0 pin */
267 i8259 = i8259_init(env->irq[2]);
Gerd Hoffmann11d23c32009-09-10 11:43:34 +0200268 isa_bus_new(NULL);
269 isa_bus_irqs(i8259);
pbrookd537cf62007-04-07 18:14:41 +0000270
Blue Swirl49a29422010-10-13 18:41:29 +0000271 rtc_init(2000, NULL);
thsafdfa782006-12-07 18:15:35 +0000272
bellard0699b542005-07-02 15:20:29 +0000273 /* Register 64 KB of ISA IO space at 0x14000000 */
Alexander Graf968d6832010-12-08 12:05:49 +0100274 isa_mmio_init(0x14000000, 0x00010000);
bellard0699b542005-07-02 15:20:29 +0000275 isa_mem_base = 0x10000000;
276
Blue Swirl64d7e9a2011-02-13 19:54:40 +0000277 pit = pit_init(0x40, 0);
thsafdfa782006-12-07 18:15:35 +0000278
thseddbd282006-12-23 00:23:19 +0000279 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
280 if (serial_hds[i]) {
Gerd Hoffmannac0be992009-09-22 13:53:21 +0200281 serial_isa_init(i, serial_hds[i]);
thseddbd282006-12-23 00:23:19 +0000282 }
283 }
284
Paul Brookfbe1b592009-05-13 17:56:25 +0100285 isa_vga_init();
bellard9827e952005-07-02 15:26:04 +0000286
aliguori0ae18ce2009-01-13 19:39:36 +0000287 if (nd_table[0].vlan)
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +0200288 isa_ne2000_init(0x300, 9, &nd_table[0]);
pbrook58126402006-10-29 15:38:28 +0000289
Isaku Yamahata75717902011-04-03 20:32:46 +0900290 ide_drive_get(hd, MAX_IDE_BUS);
thse4bcb142007-12-02 04:51:10 +0000291 for(i = 0; i < MAX_IDE_BUS; i++)
Gerd Hoffmanndea21e92009-09-15 20:05:00 +0000292 isa_ide_init(ide_iobase[i], ide_iobase2[i], ide_irq[i],
thse4bcb142007-12-02 04:51:10 +0000293 hd[MAX_IDE_DEVS * i],
294 hd[MAX_IDE_DEVS * i + 1]);
ths70705262007-02-18 00:10:59 +0000295
Gerd Hoffmann11d23c32009-09-10 11:43:34 +0200296 isa_create_simple("i8042");
bellard6af0bf92005-07-02 14:58:51 +0000297}
298
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500299static QEMUMachine mips_machine = {
thseec27432008-08-13 13:01:28 +0000300 .name = "mips",
301 .desc = "mips r4k platform",
302 .init = mips_r4k_init,
bellard6af0bf92005-07-02 14:58:51 +0000303};
Anthony Liguorif80f9ec2009-05-20 18:38:09 -0500304
305static void mips_machine_init(void)
306{
307 qemu_register_machine(&mips_machine);
308}
309
310machine_init(mips_machine_init);