]> git.proxmox.com Git - mirror_qemu.git/blob - hw/ppc_newworld.c
targe-ppc: Sync CPU state for KVM
[mirror_qemu.git] / hw / ppc_newworld.c
1 /*
2 * QEMU PowerPC CHRP (currently NewWorld PowerMac) hardware System Emulator
3 *
4 * Copyright (c) 2004-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25 #include "hw.h"
26 #include "ppc.h"
27 #include "ppc_mac.h"
28 #include "mac_dbdma.h"
29 #include "nvram.h"
30 #include "pc.h"
31 #include "pci.h"
32 #include "usb-ohci.h"
33 #include "net.h"
34 #include "sysemu.h"
35 #include "boards.h"
36 #include "fw_cfg.h"
37 #include "escc.h"
38 #include "openpic.h"
39 #include "ide.h"
40 #include "loader.h"
41 #include "elf.h"
42 #include "kvm.h"
43
44 #define MAX_IDE_BUS 2
45 #define VGA_BIOS_SIZE 65536
46 #define CFG_ADDR 0xf0000510
47
48 /* debug UniNorth */
49 //#define DEBUG_UNIN
50
51 #ifdef DEBUG_UNIN
52 #define UNIN_DPRINTF(fmt, ...) \
53 do { printf("UNIN: " fmt , ## __VA_ARGS__); } while (0)
54 #else
55 #define UNIN_DPRINTF(fmt, ...)
56 #endif
57
58 /* UniN device */
59 static void unin_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
60 {
61 UNIN_DPRINTF("writel addr " TARGET_FMT_plx " val %x\n", addr, value);
62 }
63
64 static uint32_t unin_readl (void *opaque, target_phys_addr_t addr)
65 {
66 uint32_t value;
67
68 value = 0;
69 UNIN_DPRINTF("readl addr " TARGET_FMT_plx " val %x\n", addr, value);
70
71 return value;
72 }
73
74 static CPUWriteMemoryFunc * const unin_write[] = {
75 &unin_writel,
76 &unin_writel,
77 &unin_writel,
78 };
79
80 static CPUReadMemoryFunc * const unin_read[] = {
81 &unin_readl,
82 &unin_readl,
83 &unin_readl,
84 };
85
86 static int fw_cfg_boot_set(void *opaque, const char *boot_device)
87 {
88 fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
89 return 0;
90 }
91
92 /* PowerPC Mac99 hardware initialisation */
93 static void ppc_core99_init (ram_addr_t ram_size,
94 const char *boot_device,
95 const char *kernel_filename,
96 const char *kernel_cmdline,
97 const char *initrd_filename,
98 const char *cpu_model)
99 {
100 CPUState *env = NULL, *envs[MAX_CPUS];
101 char *filename;
102 qemu_irq *pic, **openpic_irqs;
103 int unin_memory;
104 int linux_boot, i;
105 ram_addr_t ram_offset, bios_offset, vga_bios_offset;
106 uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
107 PCIBus *pci_bus;
108 MacIONVRAMState *nvr;
109 int nvram_mem_index;
110 int vga_bios_size, bios_size;
111 qemu_irq *dummy_irq;
112 int pic_mem_index, dbdma_mem_index, cuda_mem_index, escc_mem_index;
113 int ppc_boot_device;
114 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
115 void *fw_cfg;
116 void *dbdma;
117 uint8_t *vga_bios_ptr;
118
119 linux_boot = (kernel_filename != NULL);
120
121 /* init CPUs */
122 if (cpu_model == NULL)
123 cpu_model = "G4";
124 for (i = 0; i < smp_cpus; i++) {
125 env = cpu_init(cpu_model);
126 if (!env) {
127 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
128 exit(1);
129 }
130 /* Set time-base frequency to 100 Mhz */
131 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
132 #if 0
133 env->osi_call = vga_osi_call;
134 #endif
135 qemu_register_reset((QEMUResetHandler*)&cpu_reset, env);
136 envs[i] = env;
137 }
138
139 /* Make sure all register sets take effect */
140 cpu_synchronize_state(env);
141
142 /* allocate RAM */
143 ram_offset = qemu_ram_alloc(ram_size);
144 cpu_register_physical_memory(0, ram_size, ram_offset);
145
146 /* allocate and load BIOS */
147 bios_offset = qemu_ram_alloc(BIOS_SIZE);
148 if (bios_name == NULL)
149 bios_name = PROM_FILENAME;
150 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
151 cpu_register_physical_memory(PROM_ADDR, BIOS_SIZE, bios_offset | IO_MEM_ROM);
152
153 /* Load OpenBIOS (ELF) */
154 if (filename) {
155 bios_size = load_elf(filename, 0, NULL, NULL, NULL, 1, ELF_MACHINE, 0);
156
157 qemu_free(filename);
158 } else {
159 bios_size = -1;
160 }
161 if (bios_size < 0 || bios_size > BIOS_SIZE) {
162 hw_error("qemu: could not load PowerPC bios '%s'\n", bios_name);
163 exit(1);
164 }
165
166 /* allocate and load VGA BIOS */
167 vga_bios_offset = qemu_ram_alloc(VGA_BIOS_SIZE);
168 vga_bios_ptr = qemu_get_ram_ptr(vga_bios_offset);
169 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, VGABIOS_FILENAME);
170 if (filename) {
171 vga_bios_size = load_image(filename, vga_bios_ptr + 8);
172 qemu_free(filename);
173 } else {
174 vga_bios_size = -1;
175 }
176 if (vga_bios_size < 0) {
177 /* if no bios is present, we can still work */
178 fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n",
179 VGABIOS_FILENAME);
180 vga_bios_size = 0;
181 } else {
182 /* set a specific header (XXX: find real Apple format for NDRV
183 drivers) */
184 vga_bios_ptr[0] = 'N';
185 vga_bios_ptr[1] = 'D';
186 vga_bios_ptr[2] = 'R';
187 vga_bios_ptr[3] = 'V';
188 cpu_to_be32w((uint32_t *)(vga_bios_ptr + 4), vga_bios_size);
189 vga_bios_size += 8;
190
191 /* Round to page boundary */
192 vga_bios_size = (vga_bios_size + TARGET_PAGE_SIZE - 1) &
193 TARGET_PAGE_MASK;
194 }
195
196 if (linux_boot) {
197 uint64_t lowaddr = 0;
198 int bswap_needed;
199
200 #ifdef BSWAP_NEEDED
201 bswap_needed = 1;
202 #else
203 bswap_needed = 0;
204 #endif
205 kernel_base = KERNEL_LOAD_ADDR;
206
207 /* Now we can load the kernel. The first step tries to load the kernel
208 supposing PhysAddr = 0x00000000. If that was wrong the kernel is
209 loaded again, the new PhysAddr being computed from lowaddr. */
210 kernel_size = load_elf(kernel_filename, kernel_base, NULL, &lowaddr, NULL,
211 1, ELF_MACHINE, 0);
212 if (kernel_size > 0 && lowaddr != KERNEL_LOAD_ADDR) {
213 kernel_size = load_elf(kernel_filename, (2 * kernel_base) - lowaddr,
214 NULL, NULL, NULL, 1, ELF_MACHINE, 0);
215 }
216 if (kernel_size < 0)
217 kernel_size = load_aout(kernel_filename, kernel_base,
218 ram_size - kernel_base, bswap_needed,
219 TARGET_PAGE_SIZE);
220 if (kernel_size < 0)
221 kernel_size = load_image_targphys(kernel_filename,
222 kernel_base,
223 ram_size - kernel_base);
224 if (kernel_size < 0) {
225 hw_error("qemu: could not load kernel '%s'\n", kernel_filename);
226 exit(1);
227 }
228 /* load initrd */
229 if (initrd_filename) {
230 initrd_base = INITRD_LOAD_ADDR;
231 initrd_size = load_image_targphys(initrd_filename, initrd_base,
232 ram_size - initrd_base);
233 if (initrd_size < 0) {
234 hw_error("qemu: could not load initial ram disk '%s'\n",
235 initrd_filename);
236 exit(1);
237 }
238 } else {
239 initrd_base = 0;
240 initrd_size = 0;
241 }
242 ppc_boot_device = 'm';
243 } else {
244 kernel_base = 0;
245 kernel_size = 0;
246 initrd_base = 0;
247 initrd_size = 0;
248 ppc_boot_device = '\0';
249 /* We consider that NewWorld PowerMac never have any floppy drive
250 * For now, OHW cannot boot from the network.
251 */
252 for (i = 0; boot_device[i] != '\0'; i++) {
253 if (boot_device[i] >= 'c' && boot_device[i] <= 'f') {
254 ppc_boot_device = boot_device[i];
255 break;
256 }
257 }
258 if (ppc_boot_device == '\0') {
259 fprintf(stderr, "No valid boot device for Mac99 machine\n");
260 exit(1);
261 }
262 }
263
264 isa_mem_base = 0x80000000;
265
266 /* Register 8 MB of ISA IO space */
267 isa_mmio_init(0xf2000000, 0x00800000);
268
269 /* UniN init */
270 unin_memory = cpu_register_io_memory(unin_read, unin_write, NULL);
271 cpu_register_physical_memory(0xf8000000, 0x00001000, unin_memory);
272
273 openpic_irqs = qemu_mallocz(smp_cpus * sizeof(qemu_irq *));
274 openpic_irqs[0] =
275 qemu_mallocz(smp_cpus * sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
276 for (i = 0; i < smp_cpus; i++) {
277 /* Mac99 IRQ connection between OpenPIC outputs pins
278 * and PowerPC input pins
279 */
280 switch (PPC_INPUT(env)) {
281 case PPC_FLAGS_INPUT_6xx:
282 openpic_irqs[i] = openpic_irqs[0] + (i * OPENPIC_OUTPUT_NB);
283 openpic_irqs[i][OPENPIC_OUTPUT_INT] =
284 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
285 openpic_irqs[i][OPENPIC_OUTPUT_CINT] =
286 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
287 openpic_irqs[i][OPENPIC_OUTPUT_MCK] =
288 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_MCP];
289 /* Not connected ? */
290 openpic_irqs[i][OPENPIC_OUTPUT_DEBUG] = NULL;
291 /* Check this */
292 openpic_irqs[i][OPENPIC_OUTPUT_RESET] =
293 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_HRESET];
294 break;
295 #if defined(TARGET_PPC64)
296 case PPC_FLAGS_INPUT_970:
297 openpic_irqs[i] = openpic_irqs[0] + (i * OPENPIC_OUTPUT_NB);
298 openpic_irqs[i][OPENPIC_OUTPUT_INT] =
299 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_INT];
300 openpic_irqs[i][OPENPIC_OUTPUT_CINT] =
301 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_INT];
302 openpic_irqs[i][OPENPIC_OUTPUT_MCK] =
303 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_MCP];
304 /* Not connected ? */
305 openpic_irqs[i][OPENPIC_OUTPUT_DEBUG] = NULL;
306 /* Check this */
307 openpic_irqs[i][OPENPIC_OUTPUT_RESET] =
308 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_HRESET];
309 break;
310 #endif /* defined(TARGET_PPC64) */
311 default:
312 hw_error("Bus model not supported on mac99 machine\n");
313 exit(1);
314 }
315 }
316 pic = openpic_init(NULL, &pic_mem_index, smp_cpus, openpic_irqs, NULL);
317 pci_bus = pci_pmac_init(pic);
318 /* init basic PC hardware */
319 pci_vga_init(pci_bus, vga_bios_offset, vga_bios_size);
320
321 /* XXX: suppress that */
322 dummy_irq = i8259_init(NULL);
323
324 escc_mem_index = escc_init(0x80013000, dummy_irq[4], dummy_irq[5],
325 serial_hds[0], serial_hds[1], ESCC_CLOCK, 4);
326
327 for(i = 0; i < nb_nics; i++)
328 pci_nic_init_nofail(&nd_table[i], "ne2k_pci", NULL);
329
330 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
331 fprintf(stderr, "qemu: too many IDE bus\n");
332 exit(1);
333 }
334 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
335 hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
336 }
337 dbdma = DBDMA_init(&dbdma_mem_index);
338 pci_cmd646_ide_init(pci_bus, hd, 0);
339
340 /* cuda also initialize ADB */
341 cuda_init(&cuda_mem_index, pic[0x19]);
342
343 adb_kbd_init(&adb_bus);
344 adb_mouse_init(&adb_bus);
345
346
347 macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem_index,
348 dbdma_mem_index, cuda_mem_index, NULL, 0, NULL,
349 escc_mem_index);
350
351 if (usb_enabled) {
352 usb_ohci_init_pci(pci_bus, -1);
353 }
354
355 if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
356 graphic_depth = 15;
357
358 /* The NewWorld NVRAM is not located in the MacIO device */
359 nvr = macio_nvram_init(&nvram_mem_index, 0x2000, 1);
360 pmac_format_nvram_partition(nvr, 0x2000);
361 macio_nvram_map(nvr, 0xFFF04000);
362 /* No PCI init: the BIOS will do it */
363
364 fw_cfg = fw_cfg_init(0, 0, CFG_ADDR, CFG_ADDR + 2);
365 fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
366 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
367 fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, ARCH_MAC99);
368 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_base);
369 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
370 if (kernel_cmdline) {
371 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, CMDLINE_ADDR);
372 pstrcpy_targphys("cmdline", CMDLINE_ADDR, TARGET_PAGE_SIZE, kernel_cmdline);
373 } else {
374 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, 0);
375 }
376 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_base);
377 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
378 fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ppc_boot_device);
379
380 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_WIDTH, graphic_width);
381 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_HEIGHT, graphic_height);
382 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_DEPTH, graphic_depth);
383
384 qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
385 }
386
387 static QEMUMachine core99_machine = {
388 .name = "mac99",
389 .desc = "Mac99 based PowerMAC",
390 .init = ppc_core99_init,
391 .max_cpus = MAX_CPUS,
392 };
393
394 static void core99_machine_init(void)
395 {
396 qemu_register_machine(&core99_machine);
397 }
398
399 machine_init(core99_machine_init);