]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc_newworld.c
cpu-all.h: fix cpu_get_real_ticks() #ifdef
[mirror_qemu.git] / hw / ppc_newworld.c
CommitLineData
64201201 1/*
3cbee15b 2 * QEMU PowerPC CHRP (currently NewWorld PowerMac) hardware System Emulator
5fafdf24 3 *
47103572 4 * Copyright (c) 2004-2007 Fabrice Bellard
3cbee15b 5 * Copyright (c) 2007 Jocelyn Mayer
5fafdf24 6 *
64201201
FB
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 */
87ecb68b
PB
25#include "hw.h"
26#include "ppc.h"
3cbee15b 27#include "ppc_mac.h"
28ce5ce6 28#include "mac_dbdma.h"
87ecb68b
PB
29#include "nvram.h"
30#include "pc.h"
31#include "pci.h"
18e08a55 32#include "usb-ohci.h"
87ecb68b
PB
33#include "net.h"
34#include "sysemu.h"
35#include "boards.h"
006f3a48 36#include "fw_cfg.h"
7fa9ae1a 37#include "escc.h"
b7169916 38#include "openpic.h"
977e1244 39#include "ide.h"
ca20cf32
BS
40#include "loader.h"
41#include "elf.h"
dc702288 42#include "kvm.h"
267002cd 43
e4bcb14c 44#define MAX_IDE_BUS 2
864c136a 45#define VGA_BIOS_SIZE 65536
006f3a48 46#define CFG_ADDR 0xf0000510
e4bcb14c 47
f3902383
BS
48/* debug UniNorth */
49//#define DEBUG_UNIN
50
51#ifdef DEBUG_UNIN
001faf32
BS
52#define UNIN_DPRINTF(fmt, ...) \
53 do { printf("UNIN: " fmt , ## __VA_ARGS__); } while (0)
f3902383 54#else
001faf32 55#define UNIN_DPRINTF(fmt, ...)
f3902383
BS
56#endif
57
0aa6a4a2 58/* UniN device */
c227f099 59static void unin_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
0aa6a4a2 60{
f3902383 61 UNIN_DPRINTF("writel addr " TARGET_FMT_plx " val %x\n", addr, value);
0aa6a4a2
FB
62}
63
c227f099 64static uint32_t unin_readl (void *opaque, target_phys_addr_t addr)
0aa6a4a2 65{
f3902383
BS
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;
0aa6a4a2
FB
72}
73
d60efc6b 74static CPUWriteMemoryFunc * const unin_write[] = {
0aa6a4a2
FB
75 &unin_writel,
76 &unin_writel,
77 &unin_writel,
78};
79
d60efc6b 80static CPUReadMemoryFunc * const unin_read[] = {
0aa6a4a2
FB
81 &unin_readl,
82 &unin_readl,
83 &unin_readl,
84};
85
513f789f
BS
86static 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
3cbee15b 92/* PowerPC Mac99 hardware initialisation */
c227f099 93static void ppc_core99_init (ram_addr_t ram_size,
3023f332 94 const char *boot_device,
3cbee15b
JM
95 const char *kernel_filename,
96 const char *kernel_cmdline,
97 const char *initrd_filename,
98 const char *cpu_model)
64201201 99{
aaed909a 100 CPUState *env = NULL, *envs[MAX_CPUS];
5cea8590 101 char *filename;
e9df014c 102 qemu_irq *pic, **openpic_irqs;
aef445bd 103 int unin_memory;
d5295253 104 int linux_boot, i;
c227f099 105 ram_addr_t ram_offset, bios_offset, vga_bios_offset;
b6b8bd18 106 uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
46e50e9d 107 PCIBus *pci_bus;
3cbee15b
JM
108 MacIONVRAMState *nvr;
109 int nvram_mem_index;
d5295253 110 int vga_bios_size, bios_size;
7fa9ae1a 111 int pic_mem_index, dbdma_mem_index, cuda_mem_index, escc_mem_index;
28c5af54 112 int ppc_boot_device;
f455e98c 113 DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
006f3a48 114 void *fw_cfg;
28ce5ce6 115 void *dbdma;
44654490 116 uint8_t *vga_bios_ptr;
46e50e9d 117
64201201
FB
118 linux_boot = (kernel_filename != NULL);
119
c68ea704 120 /* init CPUs */
94fc95cd 121 if (cpu_model == NULL)
e6bd862b 122 cpu_model = "G4";
e9df014c 123 for (i = 0; i < smp_cpus; i++) {
aaed909a
FB
124 env = cpu_init(cpu_model);
125 if (!env) {
126 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
127 exit(1);
128 }
e9df014c
JM
129 /* Set time-base frequency to 100 Mhz */
130 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
3cbee15b 131#if 0
e9df014c 132 env->osi_call = vga_osi_call;
3cbee15b 133#endif
d84bda46 134 qemu_register_reset((QEMUResetHandler*)&cpu_reset, env);
e9df014c
JM
135 envs[i] = env;
136 }
c68ea704 137
dc702288
AG
138 /* Make sure all register sets take effect */
139 cpu_synchronize_state(env);
140
64201201 141 /* allocate RAM */
864c136a
BS
142 ram_offset = qemu_ram_alloc(ram_size);
143 cpu_register_physical_memory(0, ram_size, ram_offset);
144
64201201 145 /* allocate and load BIOS */
864c136a 146 bios_offset = qemu_ram_alloc(BIOS_SIZE);
1192dad8 147 if (bios_name == NULL)
006f3a48 148 bios_name = PROM_FILENAME;
5cea8590 149 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
006f3a48
BS
150 cpu_register_physical_memory(PROM_ADDR, BIOS_SIZE, bios_offset | IO_MEM_ROM);
151
152 /* Load OpenBIOS (ELF) */
5cea8590 153 if (filename) {
ca20cf32
BS
154 bios_size = load_elf(filename, 0, NULL, NULL, NULL, 1, ELF_MACHINE, 0);
155
5cea8590
PB
156 qemu_free(filename);
157 } else {
158 bios_size = -1;
159 }
d5295253 160 if (bios_size < 0 || bios_size > BIOS_SIZE) {
5cea8590 161 hw_error("qemu: could not load PowerPC bios '%s'\n", bios_name);
64201201
FB
162 exit(1);
163 }
3b46e624 164
d5295253 165 /* allocate and load VGA BIOS */
864c136a 166 vga_bios_offset = qemu_ram_alloc(VGA_BIOS_SIZE);
44654490 167 vga_bios_ptr = qemu_get_ram_ptr(vga_bios_offset);
5cea8590
PB
168 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, VGABIOS_FILENAME);
169 if (filename) {
170 vga_bios_size = load_image(filename, vga_bios_ptr + 8);
171 qemu_free(filename);
172 } else {
173 vga_bios_size = -1;
174 }
d5295253
FB
175 if (vga_bios_size < 0) {
176 /* if no bios is present, we can still work */
5cea8590
PB
177 fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n",
178 VGABIOS_FILENAME);
d5295253
FB
179 vga_bios_size = 0;
180 } else {
181 /* set a specific header (XXX: find real Apple format for NDRV
182 drivers) */
44654490
PB
183 vga_bios_ptr[0] = 'N';
184 vga_bios_ptr[1] = 'D';
185 vga_bios_ptr[2] = 'R';
186 vga_bios_ptr[3] = 'V';
187 cpu_to_be32w((uint32_t *)(vga_bios_ptr + 4), vga_bios_size);
d5295253 188 vga_bios_size += 8;
a7b022e0
AG
189
190 /* Round to page boundary */
191 vga_bios_size = (vga_bios_size + TARGET_PAGE_SIZE - 1) &
192 TARGET_PAGE_MASK;
d5295253 193 }
3b46e624 194
b6b8bd18 195 if (linux_boot) {
513f789f 196 uint64_t lowaddr = 0;
ca20cf32
BS
197 int bswap_needed;
198
199#ifdef BSWAP_NEEDED
200 bswap_needed = 1;
201#else
202 bswap_needed = 0;
203#endif
b6b8bd18 204 kernel_base = KERNEL_LOAD_ADDR;
513f789f
BS
205
206 /* Now we can load the kernel. The first step tries to load the kernel
207 supposing PhysAddr = 0x00000000. If that was wrong the kernel is
208 loaded again, the new PhysAddr being computed from lowaddr. */
ca20cf32
BS
209 kernel_size = load_elf(kernel_filename, kernel_base, NULL, &lowaddr, NULL,
210 1, ELF_MACHINE, 0);
513f789f
BS
211 if (kernel_size > 0 && lowaddr != KERNEL_LOAD_ADDR) {
212 kernel_size = load_elf(kernel_filename, (2 * kernel_base) - lowaddr,
ca20cf32 213 NULL, NULL, NULL, 1, ELF_MACHINE, 0);
513f789f
BS
214 }
215 if (kernel_size < 0)
216 kernel_size = load_aout(kernel_filename, kernel_base,
ca20cf32
BS
217 ram_size - kernel_base, bswap_needed,
218 TARGET_PAGE_SIZE);
513f789f
BS
219 if (kernel_size < 0)
220 kernel_size = load_image_targphys(kernel_filename,
221 kernel_base,
222 ram_size - kernel_base);
b6b8bd18 223 if (kernel_size < 0) {
2ac71179 224 hw_error("qemu: could not load kernel '%s'\n", kernel_filename);
b6b8bd18
FB
225 exit(1);
226 }
227 /* load initrd */
228 if (initrd_filename) {
229 initrd_base = INITRD_LOAD_ADDR;
44654490
PB
230 initrd_size = load_image_targphys(initrd_filename, initrd_base,
231 ram_size - initrd_base);
b6b8bd18 232 if (initrd_size < 0) {
2ac71179
PB
233 hw_error("qemu: could not load initial ram disk '%s'\n",
234 initrd_filename);
b6b8bd18
FB
235 exit(1);
236 }
237 } else {
238 initrd_base = 0;
239 initrd_size = 0;
240 }
6ac0e82d 241 ppc_boot_device = 'm';
b6b8bd18
FB
242 } else {
243 kernel_base = 0;
244 kernel_size = 0;
245 initrd_base = 0;
246 initrd_size = 0;
28c5af54
JM
247 ppc_boot_device = '\0';
248 /* We consider that NewWorld PowerMac never have any floppy drive
249 * For now, OHW cannot boot from the network.
250 */
0d913fdb
JM
251 for (i = 0; boot_device[i] != '\0'; i++) {
252 if (boot_device[i] >= 'c' && boot_device[i] <= 'f') {
253 ppc_boot_device = boot_device[i];
28c5af54 254 break;
0d913fdb 255 }
28c5af54
JM
256 }
257 if (ppc_boot_device == '\0') {
258 fprintf(stderr, "No valid boot device for Mac99 machine\n");
259 exit(1);
260 }
b6b8bd18 261 }
0aa6a4a2 262
3cbee15b 263 isa_mem_base = 0x80000000;
aef445bd 264
3cbee15b
JM
265 /* Register 8 MB of ISA IO space */
266 isa_mmio_init(0xf2000000, 0x00800000);
3b46e624 267
3cbee15b 268 /* UniN init */
1eed09cb 269 unin_memory = cpu_register_io_memory(unin_read, unin_write, NULL);
3cbee15b 270 cpu_register_physical_memory(0xf8000000, 0x00001000, unin_memory);
47103572 271
3cbee15b
JM
272 openpic_irqs = qemu_mallocz(smp_cpus * sizeof(qemu_irq *));
273 openpic_irqs[0] =
274 qemu_mallocz(smp_cpus * sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
275 for (i = 0; i < smp_cpus; i++) {
276 /* Mac99 IRQ connection between OpenPIC outputs pins
277 * and PowerPC input pins
278 */
279 switch (PPC_INPUT(env)) {
280 case PPC_FLAGS_INPUT_6xx:
281 openpic_irqs[i] = openpic_irqs[0] + (i * OPENPIC_OUTPUT_NB);
282 openpic_irqs[i][OPENPIC_OUTPUT_INT] =
283 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
284 openpic_irqs[i][OPENPIC_OUTPUT_CINT] =
285 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
286 openpic_irqs[i][OPENPIC_OUTPUT_MCK] =
287 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_MCP];
288 /* Not connected ? */
289 openpic_irqs[i][OPENPIC_OUTPUT_DEBUG] = NULL;
290 /* Check this */
291 openpic_irqs[i][OPENPIC_OUTPUT_RESET] =
292 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_HRESET];
293 break;
00af685f 294#if defined(TARGET_PPC64)
3cbee15b
JM
295 case PPC_FLAGS_INPUT_970:
296 openpic_irqs[i] = openpic_irqs[0] + (i * OPENPIC_OUTPUT_NB);
297 openpic_irqs[i][OPENPIC_OUTPUT_INT] =
298 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_INT];
299 openpic_irqs[i][OPENPIC_OUTPUT_CINT] =
300 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_INT];
301 openpic_irqs[i][OPENPIC_OUTPUT_MCK] =
302 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_MCP];
303 /* Not connected ? */
304 openpic_irqs[i][OPENPIC_OUTPUT_DEBUG] = NULL;
305 /* Check this */
306 openpic_irqs[i][OPENPIC_OUTPUT_RESET] =
307 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_HRESET];
308 break;
00af685f 309#endif /* defined(TARGET_PPC64) */
3cbee15b 310 default:
2ac71179 311 hw_error("Bus model not supported on mac99 machine\n");
3cbee15b 312 exit(1);
0aa6a4a2 313 }
3cbee15b
JM
314 }
315 pic = openpic_init(NULL, &pic_mem_index, smp_cpus, openpic_irqs, NULL);
316 pci_bus = pci_pmac_init(pic);
317 /* init basic PC hardware */
fbe1b595 318 pci_vga_init(pci_bus, vga_bios_offset, vga_bios_size);
aae9366a 319
b4b784fe 320 escc_mem_index = escc_init(0x80013000, pic[0x25], pic[0x24],
aeeb69c7 321 serial_hds[0], serial_hds[1], ESCC_CLOCK, 4);
cb457d76
AL
322
323 for(i = 0; i < nb_nics; i++)
07caea31 324 pci_nic_init_nofail(&nd_table[i], "ne2k_pci", NULL);
cb457d76 325
e4bcb14c
TS
326 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
327 fprintf(stderr, "qemu: too many IDE bus\n");
328 exit(1);
329 }
330 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
f455e98c 331 hd[i] = drive_get(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
e4bcb14c 332 }
28ce5ce6 333 dbdma = DBDMA_init(&dbdma_mem_index);
77f0435e
BS
334 pci_cmd646_ide_init(pci_bus, hd, 0);
335
3cbee15b
JM
336 /* cuda also initialize ADB */
337 cuda_init(&cuda_mem_index, pic[0x19]);
aae9366a 338
3cbee15b
JM
339 adb_kbd_init(&adb_bus);
340 adb_mouse_init(&adb_bus);
3b46e624 341
3b46e624 342
4ebcf884 343 macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem_index,
77f0435e 344 dbdma_mem_index, cuda_mem_index, NULL, 0, NULL,
4ebcf884 345 escc_mem_index);
0d92ed30
PB
346
347 if (usb_enabled) {
5b19d9a2 348 usb_ohci_init_pci(pci_bus, -1);
0d92ed30
PB
349 }
350
b6b8bd18
FB
351 if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
352 graphic_depth = 15;
4f3f238b 353
3cbee15b 354 /* The NewWorld NVRAM is not located in the MacIO device */
68af3f24 355 nvr = macio_nvram_init(&nvram_mem_index, 0x2000, 1);
3cbee15b 356 pmac_format_nvram_partition(nvr, 0x2000);
74e91155 357 macio_nvram_map(nvr, 0xFFF04000);
b6b8bd18 358 /* No PCI init: the BIOS will do it */
0aa6a4a2 359
006f3a48
BS
360 fw_cfg = fw_cfg_init(0, 0, CFG_ADDR, CFG_ADDR + 2);
361 fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
362 fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
363 fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, ARCH_MAC99);
513f789f
BS
364 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_base);
365 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
366 if (kernel_cmdline) {
367 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, CMDLINE_ADDR);
3c178e72 368 pstrcpy_targphys("cmdline", CMDLINE_ADDR, TARGET_PAGE_SIZE, kernel_cmdline);
513f789f
BS
369 } else {
370 fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, 0);
371 }
372 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_base);
373 fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
374 fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ppc_boot_device);
10696b4f
BS
375
376 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_WIDTH, graphic_width);
377 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_HEIGHT, graphic_height);
378 fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_DEPTH, graphic_depth);
379
513f789f 380 qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
aae9366a 381}
0aa6a4a2 382
f80f9ec9 383static QEMUMachine core99_machine = {
4b32e168
AL
384 .name = "mac99",
385 .desc = "Mac99 based PowerMAC",
386 .init = ppc_core99_init,
3d878caa 387 .max_cpus = MAX_CPUS,
0aa6a4a2 388};
f80f9ec9
AL
389
390static void core99_machine_init(void)
391{
392 qemu_register_machine(&core99_machine);
393}
394
395machine_init(core99_machine_init);