blob: 431e640dd69315fb0980d0523403c0f43b68a0d8 [file] [log] [blame]
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -07001/* Copyright (C) 2007-2008 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10** GNU General Public License for more details.
11*/
David 'Digit' Turner2ec695a2013-12-17 10:03:39 +010012#include "hw/hw.h"
David 'Digit' Turnerd1298762013-12-17 10:22:24 +010013#include "hw/boards.h"
14#include "hw/devices.h"
David 'Digit' Turner1365eb22014-02-16 22:23:26 +010015#include "hw/loader.h"
David 'Digit' Turnercc330d42013-12-14 23:26:42 +010016#include "net/net.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010017#include "sysemu/sysemu.h"
David 'Digit' Turnerc7169652013-12-17 11:10:33 +010018#include "hw/mips/mips.h"
David 'Digit' Turnerf5bc01c2013-12-17 10:33:07 +010019#include "hw/android/goldfish/device.h"
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +010020#include "hw/android/goldfish/pipe.h"
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070021#include "android/globals.h"
22#include "audio/audio.h"
David 'Digit' Turner34c48ff2013-12-15 00:25:03 +010023#include "sysemu/blockdev.h"
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070024#ifdef CONFIG_MEMCHECK
David 'Digit' Turner96e493a2014-03-14 17:17:26 +010025#include "android/qemu/memcheck/memcheck_api.h"
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070026#endif // CONFIG_MEMCHECK
27
28#include "android/utils/debug.h"
29
30#define D(...) VERBOSE_PRINT(init,__VA_ARGS__)
31
32#define MIPS_CPU_SAVE_VERSION 1
33#define GOLDFISH_IO_SPACE 0x1f000000
34#define GOLDFISH_INTERRUPT 0x1f000000
35#define GOLDFISH_DEVICEBUS 0x1f001000
36#define GOLDFISH_TTY 0x1f002000
37#define GOLDFISH_RTC 0x1f003000
38#define GOLDFISH_AUDIO 0x1f004000
39#define GOLDFISH_MMC 0x1f005000
40#define GOLDFISH_MEMLOG 0x1f006000
41#define GOLDFISH_DEVICES 0x1f010000
42
43char* audio_input_source = NULL;
44
45void goldfish_memlog_init(uint32_t base);
46
47static struct goldfish_device event0_device = {
48 .name = "goldfish_events",
49 .id = 0,
50 .size = 0x1000,
51 .irq_count = 1
52};
53
54static struct goldfish_device nand_device = {
55 .name = "goldfish_nand",
56 .id = 0,
57 .size = 0x1000
58};
59
60/* Board init. */
61
62#define TEST_SWITCH 1
63#if TEST_SWITCH
64uint32_t switch_test_write(void *opaque, uint32_t state)
65{
66 goldfish_switch_set_state(opaque, state);
67 return state;
68}
69#endif
70
71#define VIRT_TO_PHYS_ADDEND (-((int64_t)(int32_t)0x80000000))
72
73#define PHYS_TO_VIRT(x) ((x) | ~(target_ulong)0x7fffffff)
74
David 'Digit' Turnere2678e12014-01-16 15:56:43 +010075static void android_load_kernel(CPUOldState *env, int ram_size, const char *kernel_filename,
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070076 const char *kernel_cmdline, const char *initrd_filename)
77{
78 int initrd_size;
79 ram_addr_t initrd_offset;
80 uint64_t kernel_entry, kernel_low, kernel_high;
81 unsigned int cmdline;
82
83 /* Load the kernel. */
84 if (!kernel_filename) {
85 fprintf(stderr, "Kernel image must be specified\n");
86 exit(1);
87 }
88 if (load_elf(kernel_filename, VIRT_TO_PHYS_ADDEND,
89 (uint64_t *)&kernel_entry, (uint64_t *)&kernel_low,
90 (uint64_t *)&kernel_high) < 0) {
91 fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
92 exit(1);
93 }
94 env->active_tc.PC = (int32_t)kernel_entry;
95
96 /* load initrd */
97 initrd_size = 0;
98 initrd_offset = 0;
99 if (initrd_filename) {
100 initrd_size = get_image_size (initrd_filename);
101 if (initrd_size > 0) {
102 initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
103 if (initrd_offset + initrd_size > ram_size) {
104 fprintf(stderr,
105 "qemu: memory too small for initial ram disk '%s'\n",
106 initrd_filename);
107 exit(1);
108 }
109 initrd_size = load_image_targphys(initrd_filename,
110 initrd_offset,
111 ram_size - initrd_offset);
112
113 }
114 if (initrd_size == (target_ulong) -1) {
115 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
116 initrd_filename);
117 exit(1);
118 }
119 }
120
121 /* Store command line in top page of memory
122 * kernel will copy the command line to a loca buffer
123 */
124 cmdline = ram_size - TARGET_PAGE_SIZE;
125 char kernel_cmd[1024];
126 if (initrd_size > 0)
127 sprintf (kernel_cmd, "%s rd_start=0x" TARGET_FMT_lx " rd_size=%li",
128 kernel_cmdline,
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +0100129 (hwaddr)PHYS_TO_VIRT(initrd_offset),
130 (long int)initrd_size);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700131 else
132 strcpy (kernel_cmd, kernel_cmdline);
133
134 cpu_physical_memory_write(ram_size - TARGET_PAGE_SIZE, (void *)kernel_cmd, strlen(kernel_cmd) + 1);
David 'Digit' Turnerc0052462014-02-25 18:39:29 +0100135
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700136#if 0
137 if (initrd_size > 0)
138 sprintf (phys_ram_base+cmdline, "%s rd_start=0x" TARGET_FMT_lx " rd_size=%li",
139 kernel_cmdline,
140 PHYS_TO_VIRT(initrd_offset), initrd_size);
141 else
142 strcpy (phys_ram_base+cmdline, kernel_cmdline);
143#endif
144
145 env->active_tc.gpr[4] = PHYS_TO_VIRT(cmdline);/* a0 */
146 env->active_tc.gpr[5] = ram_size; /* a1 */
147 env->active_tc.gpr[6] = 0; /* a2 */
148 env->active_tc.gpr[7] = 0; /* a3 */
149
150}
151
152
153static void android_mips_init_(ram_addr_t ram_size,
154 const char *boot_device,
155 const char *kernel_filename,
156 const char *kernel_cmdline,
157 const char *initrd_filename,
158 const char *cpu_model)
159{
David 'Digit' Turnere2678e12014-01-16 15:56:43 +0100160 CPUOldState *env;
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700161 qemu_irq *goldfish_pic;
162 int i;
163 ram_addr_t ram_offset;
164
165 if (!cpu_model)
166 cpu_model = "24Kf";
167
168 env = cpu_init(cpu_model);
169
David 'Digit' Turner5cb5c0b2014-02-17 16:04:03 +0100170 register_savevm(NULL,
171 "cpu",
172 0,
173 MIPS_CPU_SAVE_VERSION,
174 cpu_save,
175 cpu_load,
176 env);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700177
178 if (ram_size > GOLDFISH_IO_SPACE)
179 ram_size = GOLDFISH_IO_SPACE; /* avoid overlap of ram and IO regs */
180 ram_offset = qemu_ram_alloc(NULL, "android_mips", ram_size);
181 cpu_register_physical_memory(0, ram_size, ram_offset | IO_MEM_RAM);
182
183 /* Init internal devices */
184 cpu_mips_irq_init_cpu(env);
185 cpu_mips_clock_init(env);
186
187 goldfish_pic = goldfish_interrupt_init(GOLDFISH_INTERRUPT,
188 env->irq[2], env->irq[3]);
189 goldfish_device_init(goldfish_pic, GOLDFISH_DEVICES, 0x7f0000, 10, 22);
190
191 goldfish_device_bus_init(GOLDFISH_DEVICEBUS, 1);
192
193 goldfish_timer_and_rtc_init(GOLDFISH_RTC, 3);
194
195 goldfish_tty_add(serial_hds[0], 0, GOLDFISH_TTY, 4);
196 for(i = 1; i < MAX_SERIAL_PORTS; i++) {
197 if(serial_hds[i]) {
198 goldfish_tty_add(serial_hds[i], i, 0, 0);
199 }
200 }
201
202 for(i = 0; i < MAX_NICS; i++) {
203 if (nd_table[i].vlan) {
204 if (nd_table[i].model == NULL
205 || strcmp(nd_table[i].model, "smc91c111") == 0) {
206 struct goldfish_device *smc_device;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100207 smc_device = g_malloc0(sizeof(*smc_device));
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700208 smc_device->name = "smc91x";
209 smc_device->id = i;
210 smc_device->size = 0x1000;
211 smc_device->irq_count = 1;
212 goldfish_add_device_no_io(smc_device);
213 smc91c111_init(&nd_table[i], smc_device->base, goldfish_pic[smc_device->irq]);
214 } else {
215 fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd_table[0].model);
216 exit (1);
217 }
218 }
219 }
220
221 goldfish_fb_init(0);
222#ifdef HAS_AUDIO
223 goldfish_audio_init(GOLDFISH_AUDIO, 0, audio_input_source);
224#endif
225 {
226 DriveInfo* info = drive_get( IF_IDE, 0, 0 );
227 if (info != NULL) {
228 goldfish_mmc_init(GOLDFISH_MMC, 0, info->bdrv);
229 }
230 }
231 goldfish_memlog_init(GOLDFISH_MEMLOG);
232
David 'Digit' Turner685b2c22014-01-28 03:42:03 +0100233 goldfish_battery_init(android_hw->hw_battery);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700234
235 goldfish_add_device_no_io(&event0_device);
236 events_dev_init(event0_device.base, goldfish_pic[event0_device.irq]);
237
238#ifdef CONFIG_NAND
239 goldfish_add_device_no_io(&nand_device);
240 nand_dev_init(nand_device.base);
241#endif
David 'Digit' Turner171dd0b2014-02-04 18:04:56 +0100242
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700243#ifdef CONFIG_MEMCHECK
David 'Digit' Turner171dd0b2014-02-04 18:04:56 +0100244 if (memcheck_enabled) {
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700245 trace_dev_init();
246 }
David 'Digit' Turner171dd0b2014-02-04 18:04:56 +0100247#endif // CONFIG_MEMCHECK
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +0100248 bool newDeviceNaming =
249 (androidHwConfig_getKernelDeviceNaming(android_hw) >= 1);
250 pipe_dev_init(newDeviceNaming);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700251
252#if TEST_SWITCH
253 {
254 void *sw;
255 sw = goldfish_switch_add("test", NULL, NULL, 0);
256 goldfish_switch_set_state(sw, 1);
257 goldfish_switch_add("test2", switch_test_write, sw, 1);
258 }
259#endif
260
261 android_load_kernel(env, ram_size, kernel_filename, kernel_cmdline, initrd_filename);
262}
263
264
265QEMUMachine android_mips_machine = {
266 "android_mips",
267 "MIPS Android Emulator",
268 android_mips_init_,
269 0,
270 0,
271 1,
272 NULL
273};
274
275static void android_mips_init(void)
276{
277 qemu_register_machine(&android_mips_machine);
278}
279
280machine_init(android_mips_init);