blob: 4b256606fa2625c416283a55b5536439c40d4153 [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"
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +010024#ifdef CONFIG_ANDROID_MEMCHECK
David 'Digit' Turner96e493a2014-03-14 17:17:26 +010025#include "android/qemu/memcheck/memcheck_api.h"
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +010026#endif // CONFIG_ANDROID_MEMCHECK
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070027
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
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070045static struct goldfish_device event0_device = {
46 .name = "goldfish_events",
47 .id = 0,
48 .size = 0x1000,
49 .irq_count = 1
50};
51
52static struct goldfish_device nand_device = {
53 .name = "goldfish_nand",
54 .id = 0,
55 .size = 0x1000
56};
57
58/* Board init. */
59
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070060#define VIRT_TO_PHYS_ADDEND (-((int64_t)(int32_t)0x80000000))
61
62#define PHYS_TO_VIRT(x) ((x) | ~(target_ulong)0x7fffffff)
63
David 'Digit' Turnere2678e12014-01-16 15:56:43 +010064static void android_load_kernel(CPUOldState *env, int ram_size, const char *kernel_filename,
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -070065 const char *kernel_cmdline, const char *initrd_filename)
66{
67 int initrd_size;
68 ram_addr_t initrd_offset;
69 uint64_t kernel_entry, kernel_low, kernel_high;
70 unsigned int cmdline;
71
72 /* Load the kernel. */
73 if (!kernel_filename) {
74 fprintf(stderr, "Kernel image must be specified\n");
75 exit(1);
76 }
77 if (load_elf(kernel_filename, VIRT_TO_PHYS_ADDEND,
78 (uint64_t *)&kernel_entry, (uint64_t *)&kernel_low,
79 (uint64_t *)&kernel_high) < 0) {
80 fprintf(stderr, "qemu: could not load kernel '%s'\n", kernel_filename);
81 exit(1);
82 }
83 env->active_tc.PC = (int32_t)kernel_entry;
84
85 /* load initrd */
86 initrd_size = 0;
87 initrd_offset = 0;
88 if (initrd_filename) {
89 initrd_size = get_image_size (initrd_filename);
90 if (initrd_size > 0) {
91 initrd_offset = (kernel_high + ~TARGET_PAGE_MASK) & TARGET_PAGE_MASK;
92 if (initrd_offset + initrd_size > ram_size) {
93 fprintf(stderr,
94 "qemu: memory too small for initial ram disk '%s'\n",
95 initrd_filename);
96 exit(1);
97 }
98 initrd_size = load_image_targphys(initrd_filename,
99 initrd_offset,
100 ram_size - initrd_offset);
101
102 }
103 if (initrd_size == (target_ulong) -1) {
104 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
105 initrd_filename);
106 exit(1);
107 }
108 }
109
110 /* Store command line in top page of memory
111 * kernel will copy the command line to a loca buffer
112 */
113 cmdline = ram_size - TARGET_PAGE_SIZE;
114 char kernel_cmd[1024];
115 if (initrd_size > 0)
116 sprintf (kernel_cmd, "%s rd_start=0x" TARGET_FMT_lx " rd_size=%li",
117 kernel_cmdline,
David 'Digit' Turnera2c14f92014-02-04 01:02:30 +0100118 (hwaddr)PHYS_TO_VIRT(initrd_offset),
119 (long int)initrd_size);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700120 else
121 strcpy (kernel_cmd, kernel_cmdline);
122
123 cpu_physical_memory_write(ram_size - TARGET_PAGE_SIZE, (void *)kernel_cmd, strlen(kernel_cmd) + 1);
David 'Digit' Turnerc0052462014-02-25 18:39:29 +0100124
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700125#if 0
126 if (initrd_size > 0)
127 sprintf (phys_ram_base+cmdline, "%s rd_start=0x" TARGET_FMT_lx " rd_size=%li",
128 kernel_cmdline,
129 PHYS_TO_VIRT(initrd_offset), initrd_size);
130 else
131 strcpy (phys_ram_base+cmdline, kernel_cmdline);
132#endif
133
134 env->active_tc.gpr[4] = PHYS_TO_VIRT(cmdline);/* a0 */
135 env->active_tc.gpr[5] = ram_size; /* a1 */
136 env->active_tc.gpr[6] = 0; /* a2 */
137 env->active_tc.gpr[7] = 0; /* a3 */
138
139}
140
141
142static void android_mips_init_(ram_addr_t ram_size,
143 const char *boot_device,
144 const char *kernel_filename,
145 const char *kernel_cmdline,
146 const char *initrd_filename,
147 const char *cpu_model)
148{
David 'Digit' Turnere2678e12014-01-16 15:56:43 +0100149 CPUOldState *env;
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700150 qemu_irq *goldfish_pic;
151 int i;
152 ram_addr_t ram_offset;
153
154 if (!cpu_model)
155 cpu_model = "24Kf";
156
157 env = cpu_init(cpu_model);
158
David 'Digit' Turner5cb5c0b2014-02-17 16:04:03 +0100159 register_savevm(NULL,
160 "cpu",
161 0,
162 MIPS_CPU_SAVE_VERSION,
163 cpu_save,
164 cpu_load,
165 env);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700166
167 if (ram_size > GOLDFISH_IO_SPACE)
168 ram_size = GOLDFISH_IO_SPACE; /* avoid overlap of ram and IO regs */
169 ram_offset = qemu_ram_alloc(NULL, "android_mips", ram_size);
170 cpu_register_physical_memory(0, ram_size, ram_offset | IO_MEM_RAM);
171
172 /* Init internal devices */
173 cpu_mips_irq_init_cpu(env);
174 cpu_mips_clock_init(env);
175
176 goldfish_pic = goldfish_interrupt_init(GOLDFISH_INTERRUPT,
177 env->irq[2], env->irq[3]);
178 goldfish_device_init(goldfish_pic, GOLDFISH_DEVICES, 0x7f0000, 10, 22);
179
180 goldfish_device_bus_init(GOLDFISH_DEVICEBUS, 1);
181
182 goldfish_timer_and_rtc_init(GOLDFISH_RTC, 3);
183
184 goldfish_tty_add(serial_hds[0], 0, GOLDFISH_TTY, 4);
185 for(i = 1; i < MAX_SERIAL_PORTS; i++) {
186 if(serial_hds[i]) {
187 goldfish_tty_add(serial_hds[i], i, 0, 0);
188 }
189 }
190
191 for(i = 0; i < MAX_NICS; i++) {
192 if (nd_table[i].vlan) {
193 if (nd_table[i].model == NULL
194 || strcmp(nd_table[i].model, "smc91c111") == 0) {
195 struct goldfish_device *smc_device;
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100196 smc_device = g_malloc0(sizeof(*smc_device));
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700197 smc_device->name = "smc91x";
198 smc_device->id = i;
199 smc_device->size = 0x1000;
200 smc_device->irq_count = 1;
201 goldfish_add_device_no_io(smc_device);
202 smc91c111_init(&nd_table[i], smc_device->base, goldfish_pic[smc_device->irq]);
203 } else {
204 fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd_table[0].model);
205 exit (1);
206 }
207 }
208 }
209
210 goldfish_fb_init(0);
211#ifdef HAS_AUDIO
212 goldfish_audio_init(GOLDFISH_AUDIO, 0, audio_input_source);
213#endif
214 {
215 DriveInfo* info = drive_get( IF_IDE, 0, 0 );
216 if (info != NULL) {
217 goldfish_mmc_init(GOLDFISH_MMC, 0, info->bdrv);
218 }
219 }
David 'Digit' Turner685b2c22014-01-28 03:42:03 +0100220 goldfish_battery_init(android_hw->hw_battery);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700221
222 goldfish_add_device_no_io(&event0_device);
223 events_dev_init(event0_device.base, goldfish_pic[event0_device.irq]);
224
225#ifdef CONFIG_NAND
226 goldfish_add_device_no_io(&nand_device);
227 nand_dev_init(nand_device.base);
228#endif
David 'Digit' Turner171dd0b2014-02-04 18:04:56 +0100229
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +0100230#ifdef CONFIG_ANDROID_MEMCHECK
David 'Digit' Turner171dd0b2014-02-04 18:04:56 +0100231 if (memcheck_enabled) {
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700232 trace_dev_init();
233 }
David 'Digit' Turner5bb450e2014-03-14 17:19:45 +0100234#endif // CONFIG_ANDROID_MEMCHECK
David 'Digit' Turnerc6e0cae2014-03-07 23:08:30 +0100235 bool newDeviceNaming =
236 (androidHwConfig_getKernelDeviceNaming(android_hw) >= 1);
237 pipe_dev_init(newDeviceNaming);
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700238
Bhanu Chetlapalli741dc132012-05-08 17:16:03 -0700239 android_load_kernel(env, ram_size, kernel_filename, kernel_cmdline, initrd_filename);
240}
241
242
243QEMUMachine android_mips_machine = {
244 "android_mips",
245 "MIPS Android Emulator",
246 android_mips_init_,
247 0,
248 0,
249 1,
250 NULL
251};
252
253static void android_mips_init(void)
254{
255 qemu_register_machine(&android_mips_machine);
256}
257
258machine_init(android_mips_init);