2 * QEMU OldWorld PowerMac (currently ~G3 Beige) hardware System Emulator
4 * Copyright (c) 2004-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
28 #include "mac_dbdma.h"
44 #define VGA_BIOS_SIZE 65536
45 #define CFG_ADDR 0xf0000510
47 /* temporary frame buffer OSI calls for the video.x driver. The right
48 solution is to modify the driver to use VGA PCI I/Os */
49 /* XXX: to be removed. This is no way related to emulation */
50 static int vga_osi_call (CPUState
*env
)
52 static int vga_vbl_enabled
;
56 printf("osi_call R5=%016" PRIx64
"\n", ppc_dump_gpr(env
, 5));
59 /* same handler as PearPC, coming from the original MOL video
64 case 28: /* set_vmode */
65 if (env
->gpr
[6] != 1 || env
->gpr
[7] != 0)
70 case 29: /* get_vmode_info */
71 if (env
->gpr
[6] != 0) {
72 if (env
->gpr
[6] != 1 || env
->gpr
[7] != 0) {
78 env
->gpr
[4] = (1 << 16) | 1; /* num_vmodes, cur_vmode */
79 env
->gpr
[5] = (1 << 16) | 0; /* num_depths, cur_depth_mode */
80 env
->gpr
[6] = (graphic_width
<< 16) | graphic_height
; /* w, h */
81 env
->gpr
[7] = 85 << 16; /* refresh rate */
82 env
->gpr
[8] = (graphic_depth
+ 7) & ~7; /* depth (round to byte) */
83 linesize
= ((graphic_depth
+ 7) >> 3) * graphic_width
;
84 linesize
= (linesize
+ 3) & ~3;
85 env
->gpr
[9] = (linesize
<< 16) | 0; /* row_bytes, offset */
87 case 31: /* set_video power */
90 case 39: /* video_ctrl */
91 if (env
->gpr
[6] == 0 || env
->gpr
[6] == 1)
92 vga_vbl_enabled
= env
->gpr
[6];
97 case 59: /* set_color */
98 /* R6 = index, R7 = RGB */
101 case 64: /* get color */
105 case 116: /* set hwcursor */
106 /* R6 = x, R7 = y, R8 = visible, R9 = data */
109 fprintf(stderr
, "unsupported OSI call R5=%016" PRIx64
"\n",
110 ppc_dump_gpr(env
, 5));
114 return 1; /* osi_call handled */
117 static int fw_cfg_boot_set(void *opaque
, const char *boot_device
)
119 fw_cfg_add_i16(opaque
, FW_CFG_BOOT_DEVICE
, boot_device
[0]);
123 static void ppc_heathrow_init (ram_addr_t ram_size
,
124 const char *boot_device
,
125 const char *kernel_filename
,
126 const char *kernel_cmdline
,
127 const char *initrd_filename
,
128 const char *cpu_model
)
130 CPUState
*env
= NULL
, *envs
[MAX_CPUS
];
132 qemu_irq
*pic
, **heathrow_irqs
;
134 ram_addr_t ram_offset
, bios_offset
, vga_bios_offset
;
135 uint32_t kernel_base
, initrd_base
;
136 int32_t kernel_size
, initrd_size
;
138 MacIONVRAMState
*nvr
;
139 int vga_bios_size
, bios_size
;
140 int pic_mem_index
, nvram_mem_index
, dbdma_mem_index
, cuda_mem_index
;
141 int escc_mem_index
, ide_mem_index
[2];
142 uint16_t ppc_boot_device
;
143 DriveInfo
*hd
[MAX_IDE_BUS
* MAX_IDE_DEVS
];
146 uint8_t *vga_bios_ptr
;
148 linux_boot
= (kernel_filename
!= NULL
);
151 if (cpu_model
== NULL
)
153 for (i
= 0; i
< smp_cpus
; i
++) {
154 env
= cpu_init(cpu_model
);
156 fprintf(stderr
, "Unable to find PowerPC CPU definition\n");
159 /* Set time-base frequency to 16.6 Mhz */
160 cpu_ppc_tb_init(env
, 16600000UL);
161 env
->osi_call
= vga_osi_call
;
162 qemu_register_reset((QEMUResetHandler
*)&cpu_reset
, env
);
167 if (ram_size
> (2047 << 20)) {
169 "qemu: Too much memory for this machine: %d MB, maximum 2047 MB\n",
170 ((unsigned int)ram_size
/ (1 << 20)));
174 ram_offset
= qemu_ram_alloc(ram_size
);
175 cpu_register_physical_memory(0, ram_size
, ram_offset
);
177 /* allocate and load BIOS */
178 bios_offset
= qemu_ram_alloc(BIOS_SIZE
);
179 if (bios_name
== NULL
)
180 bios_name
= PROM_FILENAME
;
181 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, bios_name
);
182 cpu_register_physical_memory(PROM_ADDR
, BIOS_SIZE
, bios_offset
| IO_MEM_ROM
);
184 /* Load OpenBIOS (ELF) */
186 bios_size
= load_elf(filename
, 0, NULL
, NULL
, NULL
,
192 if (bios_size
< 0 || bios_size
> BIOS_SIZE
) {
193 hw_error("qemu: could not load PowerPC bios '%s'\n", bios_name
);
197 /* allocate and load VGA BIOS */
198 vga_bios_offset
= qemu_ram_alloc(VGA_BIOS_SIZE
);
199 vga_bios_ptr
= qemu_get_ram_ptr(vga_bios_offset
);
200 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, VGABIOS_FILENAME
);
202 vga_bios_size
= load_image(filename
, vga_bios_ptr
+ 8);
207 if (vga_bios_size
< 0) {
208 /* if no bios is present, we can still work */
209 fprintf(stderr
, "qemu: warning: could not load VGA bios '%s'\n",
213 /* set a specific header (XXX: find real Apple format for NDRV
215 vga_bios_ptr
[0] = 'N';
216 vga_bios_ptr
[1] = 'D';
217 vga_bios_ptr
[2] = 'R';
218 vga_bios_ptr
[3] = 'V';
219 cpu_to_be32w((uint32_t *)(vga_bios_ptr
+ 4), vga_bios_size
);
222 /* Round to page boundary */
223 vga_bios_size
= (vga_bios_size
+ TARGET_PAGE_SIZE
- 1) &
228 uint64_t lowaddr
= 0;
236 kernel_base
= KERNEL_LOAD_ADDR
;
237 /* Now we can load the kernel. The first step tries to load the kernel
238 supposing PhysAddr = 0x00000000. If that was wrong the kernel is
239 loaded again, the new PhysAddr being computed from lowaddr. */
240 kernel_size
= load_elf(kernel_filename
, kernel_base
, NULL
, &lowaddr
, NULL
,
242 if (kernel_size
> 0 && lowaddr
!= KERNEL_LOAD_ADDR
) {
243 kernel_size
= load_elf(kernel_filename
, (2 * kernel_base
) - lowaddr
,
244 NULL
, NULL
, NULL
, 1, ELF_MACHINE
, 0);
247 kernel_size
= load_aout(kernel_filename
, kernel_base
,
248 ram_size
- kernel_base
, bswap_needed
,
251 kernel_size
= load_image_targphys(kernel_filename
,
253 ram_size
- kernel_base
);
254 if (kernel_size
< 0) {
255 hw_error("qemu: could not load kernel '%s'\n",
260 if (initrd_filename
) {
261 initrd_base
= INITRD_LOAD_ADDR
;
262 initrd_size
= load_image_targphys(initrd_filename
, initrd_base
,
263 ram_size
- initrd_base
);
264 if (initrd_size
< 0) {
265 hw_error("qemu: could not load initial ram disk '%s'\n",
273 ppc_boot_device
= 'm';
279 ppc_boot_device
= '\0';
280 for (i
= 0; boot_device
[i
] != '\0'; i
++) {
281 /* TOFIX: for now, the second IDE channel is not properly
282 * used by OHW. The Mac floppy disk are not emulated.
283 * For now, OHW cannot boot from the network.
286 if (boot_device
[i
] >= 'a' && boot_device
[i
] <= 'f') {
287 ppc_boot_device
= boot_device
[i
];
291 if (boot_device
[i
] >= 'c' && boot_device
[i
] <= 'd') {
292 ppc_boot_device
= boot_device
[i
];
297 if (ppc_boot_device
== '\0') {
298 fprintf(stderr
, "No valid boot device for G3 Beige machine\n");
303 isa_mem_base
= 0x80000000;
305 /* Register 2 MB of ISA IO space */
306 isa_mmio_init(0xfe000000, 0x00200000);
308 /* XXX: we register only 1 output pin for heathrow PIC */
309 heathrow_irqs
= qemu_mallocz(smp_cpus
* sizeof(qemu_irq
*));
311 qemu_mallocz(smp_cpus
* sizeof(qemu_irq
) * 1);
312 /* Connect the heathrow PIC outputs to the 6xx bus */
313 for (i
= 0; i
< smp_cpus
; i
++) {
314 switch (PPC_INPUT(env
)) {
315 case PPC_FLAGS_INPUT_6xx
:
316 heathrow_irqs
[i
] = heathrow_irqs
[0] + (i
* 1);
317 heathrow_irqs
[i
][0] =
318 ((qemu_irq
*)env
->irq_inputs
)[PPC6xx_INPUT_INT
];
321 hw_error("Bus model not supported on OldWorld Mac machine\n");
325 /* init basic PC hardware */
326 if (PPC_INPUT(env
) != PPC_FLAGS_INPUT_6xx
) {
327 hw_error("Only 6xx bus is supported on heathrow machine\n");
329 pic
= heathrow_pic_init(&pic_mem_index
, 1, heathrow_irqs
);
330 pci_bus
= pci_grackle_init(0xfec00000, pic
);
331 pci_vga_init(pci_bus
, vga_bios_offset
, vga_bios_size
);
333 escc_mem_index
= escc_init(0x80013000, pic
[0x0f], pic
[0x10], serial_hds
[0],
334 serial_hds
[1], ESCC_CLOCK
, 4);
336 for(i
= 0; i
< nb_nics
; i
++)
337 pci_nic_init_nofail(&nd_table
[i
], "ne2k_pci", NULL
);
340 if (drive_get_max_bus(IF_IDE
) >= MAX_IDE_BUS
) {
341 fprintf(stderr
, "qemu: too many IDE bus\n");
345 /* First IDE channel is a MAC IDE on the MacIO bus */
346 hd
[0] = drive_get(IF_IDE
, 0, 0);
347 hd
[1] = drive_get(IF_IDE
, 0, 1);
348 dbdma
= DBDMA_init(&dbdma_mem_index
);
349 ide_mem_index
[0] = -1;
350 ide_mem_index
[1] = pmac_ide_init(hd
, pic
[0x0D], dbdma
, 0x16, pic
[0x02]);
352 /* Second IDE channel is a CMD646 on the PCI bus */
353 hd
[0] = drive_get(IF_IDE
, 1, 0);
354 hd
[1] = drive_get(IF_IDE
, 1, 1);
355 hd
[3] = hd
[2] = NULL
;
356 pci_cmd646_ide_init(pci_bus
, hd
, 0);
358 /* cuda also initialize ADB */
359 cuda_init(&cuda_mem_index
, pic
[0x12]);
361 adb_kbd_init(&adb_bus
);
362 adb_mouse_init(&adb_bus
);
364 nvr
= macio_nvram_init(&nvram_mem_index
, 0x2000, 4);
365 pmac_format_nvram_partition(nvr
, 0x2000);
367 macio_init(pci_bus
, PCI_DEVICE_ID_APPLE_343S1201
, 1, pic_mem_index
,
368 dbdma_mem_index
, cuda_mem_index
, nvr
, 2, ide_mem_index
,
372 usb_ohci_init_pci(pci_bus
, -1);
375 if (graphic_depth
!= 15 && graphic_depth
!= 32 && graphic_depth
!= 8)
378 /* No PCI init: the BIOS will do it */
380 fw_cfg
= fw_cfg_init(0, 0, CFG_ADDR
, CFG_ADDR
+ 2);
381 fw_cfg_add_i32(fw_cfg
, FW_CFG_ID
, 1);
382 fw_cfg_add_i64(fw_cfg
, FW_CFG_RAM_SIZE
, (uint64_t)ram_size
);
383 fw_cfg_add_i16(fw_cfg
, FW_CFG_MACHINE_ID
, ARCH_HEATHROW
);
384 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_ADDR
, kernel_base
);
385 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_SIZE
, kernel_size
);
386 if (kernel_cmdline
) {
387 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_CMDLINE
, CMDLINE_ADDR
);
388 pstrcpy_targphys("cmdline", CMDLINE_ADDR
, TARGET_PAGE_SIZE
, kernel_cmdline
);
390 fw_cfg_add_i32(fw_cfg
, FW_CFG_KERNEL_CMDLINE
, 0);
392 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_ADDR
, initrd_base
);
393 fw_cfg_add_i32(fw_cfg
, FW_CFG_INITRD_SIZE
, initrd_size
);
394 fw_cfg_add_i16(fw_cfg
, FW_CFG_BOOT_DEVICE
, ppc_boot_device
);
396 fw_cfg_add_i16(fw_cfg
, FW_CFG_PPC_WIDTH
, graphic_width
);
397 fw_cfg_add_i16(fw_cfg
, FW_CFG_PPC_HEIGHT
, graphic_height
);
398 fw_cfg_add_i16(fw_cfg
, FW_CFG_PPC_DEPTH
, graphic_depth
);
400 qemu_register_boot_set(fw_cfg_boot_set
, fw_cfg
);
403 static QEMUMachine heathrow_machine
= {
405 .desc
= "Heathrow based PowerMAC",
406 .init
= ppc_heathrow_init
,
407 .max_cpus
= MAX_CPUS
,
411 static void heathrow_machine_init(void)
413 qemu_register_machine(&heathrow_machine
);
416 machine_init(heathrow_machine_init
);