]> git.proxmox.com Git - qemu.git/blame - hw/ppc_chrp.c
Fix SIGSEGV crash in slirp networking code
[qemu.git] / hw / ppc_chrp.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"
32#include "net.h"
33#include "sysemu.h"
34#include "boards.h"
7fa9ae1a 35#include "escc.h"
267002cd 36
e4bcb14c 37#define MAX_IDE_BUS 2
864c136a 38#define VGA_BIOS_SIZE 65536
e4bcb14c 39
f3902383
BS
40/* debug UniNorth */
41//#define DEBUG_UNIN
42
43#ifdef DEBUG_UNIN
44#define UNIN_DPRINTF(fmt, args...) \
45do { printf("UNIN: " fmt , ##args); } while (0)
46#else
47#define UNIN_DPRINTF(fmt, args...)
48#endif
49
0aa6a4a2
FB
50/* UniN device */
51static void unin_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
52{
f3902383 53 UNIN_DPRINTF("writel addr " TARGET_FMT_plx " val %x\n", addr, value);
0aa6a4a2
FB
54}
55
56static uint32_t unin_readl (void *opaque, target_phys_addr_t addr)
57{
f3902383
BS
58 uint32_t value;
59
60 value = 0;
61 UNIN_DPRINTF("readl addr " TARGET_FMT_plx " val %x\n", addr, value);
62
63 return value;
0aa6a4a2
FB
64}
65
66static CPUWriteMemoryFunc *unin_write[] = {
67 &unin_writel,
68 &unin_writel,
69 &unin_writel,
70};
71
72static CPUReadMemoryFunc *unin_read[] = {
73 &unin_readl,
74 &unin_readl,
75 &unin_readl,
76};
77
3cbee15b 78/* PowerPC Mac99 hardware initialisation */
00f82b8a 79static void ppc_core99_init (ram_addr_t ram_size, int vga_ram_size,
3023f332 80 const char *boot_device,
3cbee15b
JM
81 const char *kernel_filename,
82 const char *kernel_cmdline,
83 const char *initrd_filename,
84 const char *cpu_model)
64201201 85{
aaed909a 86 CPUState *env = NULL, *envs[MAX_CPUS];
64201201 87 char buf[1024];
e9df014c 88 qemu_irq *pic, **openpic_irqs;
aef445bd 89 int unin_memory;
d5295253 90 int linux_boot, i;
864c136a 91 ram_addr_t ram_offset, vga_ram_offset, bios_offset, vga_bios_offset;
b6b8bd18 92 uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
46e50e9d 93 PCIBus *pci_bus;
3cbee15b
JM
94 nvram_t nvram;
95#if 0
96 MacIONVRAMState *nvr;
97 int nvram_mem_index;
98#endif
99 m48t59_t *m48t59;
d5295253 100 int vga_bios_size, bios_size;
d537cf6c 101 qemu_irq *dummy_irq;
7fa9ae1a 102 int pic_mem_index, dbdma_mem_index, cuda_mem_index, escc_mem_index;
3cbee15b 103 int ide_mem_index[2];
28c5af54 104 int ppc_boot_device;
e4bcb14c
TS
105 int index;
106 BlockDriverState *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
28ce5ce6 107 void *dbdma;
46e50e9d 108
64201201
FB
109 linux_boot = (kernel_filename != NULL);
110
c68ea704 111 /* init CPUs */
94fc95cd 112 if (cpu_model == NULL)
d12f4c38 113 cpu_model = "default";
e9df014c 114 for (i = 0; i < smp_cpus; i++) {
aaed909a
FB
115 env = cpu_init(cpu_model);
116 if (!env) {
117 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
118 exit(1);
119 }
e9df014c
JM
120 /* Set time-base frequency to 100 Mhz */
121 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
3cbee15b 122#if 0
e9df014c 123 env->osi_call = vga_osi_call;
3cbee15b 124#endif
fe33cc71 125 qemu_register_reset(&cpu_ppc_reset, env);
e9df014c
JM
126 envs[i] = env;
127 }
4c823cff
JM
128 if (env->nip < 0xFFF80000) {
129 /* Special test for PowerPC 601:
130 * the boot vector is at 0xFFF00100, then we need a 1MB BIOS.
131 * But the NVRAM is located at 0xFFF04000...
132 */
133 cpu_abort(env, "Mac99 hardware can not handle 1 MB BIOS\n");
134 }
c68ea704 135
64201201 136 /* allocate RAM */
864c136a
BS
137 ram_offset = qemu_ram_alloc(ram_size);
138 cpu_register_physical_memory(0, ram_size, ram_offset);
139
140 /* allocate VGA RAM */
141 vga_ram_offset = qemu_ram_alloc(vga_ram_size);
64201201
FB
142
143 /* allocate and load BIOS */
864c136a 144 bios_offset = qemu_ram_alloc(BIOS_SIZE);
1192dad8
JM
145 if (bios_name == NULL)
146 bios_name = BIOS_FILENAME;
147 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
d5295253
FB
148 bios_size = load_image(buf, phys_ram_base + bios_offset);
149 if (bios_size < 0 || bios_size > BIOS_SIZE) {
4a057712 150 cpu_abort(env, "qemu: could not load PowerPC bios '%s'\n", buf);
64201201
FB
151 exit(1);
152 }
d5295253 153 bios_size = (bios_size + 0xfff) & ~0xfff;
4c823cff
JM
154 if (bios_size > 0x00080000) {
155 /* As the NVRAM is located at 0xFFF04000, we cannot use 1 MB BIOSes */
156 cpu_abort(env, "Mac99 hardware can not handle 1 MB BIOS\n");
157 }
4a057712 158 cpu_register_physical_memory((uint32_t)(-bios_size),
d5295253 159 bios_size, bios_offset | IO_MEM_ROM);
3b46e624 160
d5295253 161 /* allocate and load VGA BIOS */
864c136a 162 vga_bios_offset = qemu_ram_alloc(VGA_BIOS_SIZE);
d5295253
FB
163 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
164 vga_bios_size = load_image(buf, phys_ram_base + vga_bios_offset + 8);
165 if (vga_bios_size < 0) {
166 /* if no bios is present, we can still work */
167 fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n", buf);
168 vga_bios_size = 0;
169 } else {
170 /* set a specific header (XXX: find real Apple format for NDRV
171 drivers) */
172 phys_ram_base[vga_bios_offset] = 'N';
173 phys_ram_base[vga_bios_offset + 1] = 'D';
174 phys_ram_base[vga_bios_offset + 2] = 'R';
175 phys_ram_base[vga_bios_offset + 3] = 'V';
5fafdf24 176 cpu_to_be32w((uint32_t *)(phys_ram_base + vga_bios_offset + 4),
d5295253
FB
177 vga_bios_size);
178 vga_bios_size += 8;
179 }
3b46e624 180
b6b8bd18
FB
181 if (linux_boot) {
182 kernel_base = KERNEL_LOAD_ADDR;
183 /* now we can load the kernel */
184 kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
185 if (kernel_size < 0) {
4a057712
JM
186 cpu_abort(env, "qemu: could not load kernel '%s'\n",
187 kernel_filename);
b6b8bd18
FB
188 exit(1);
189 }
190 /* load initrd */
191 if (initrd_filename) {
192 initrd_base = INITRD_LOAD_ADDR;
193 initrd_size = load_image(initrd_filename,
194 phys_ram_base + initrd_base);
195 if (initrd_size < 0) {
4a057712
JM
196 cpu_abort(env, "qemu: could not load initial ram disk '%s'\n",
197 initrd_filename);
b6b8bd18
FB
198 exit(1);
199 }
200 } else {
201 initrd_base = 0;
202 initrd_size = 0;
203 }
6ac0e82d 204 ppc_boot_device = 'm';
b6b8bd18
FB
205 } else {
206 kernel_base = 0;
207 kernel_size = 0;
208 initrd_base = 0;
209 initrd_size = 0;
28c5af54
JM
210 ppc_boot_device = '\0';
211 /* We consider that NewWorld PowerMac never have any floppy drive
212 * For now, OHW cannot boot from the network.
213 */
0d913fdb
JM
214 for (i = 0; boot_device[i] != '\0'; i++) {
215 if (boot_device[i] >= 'c' && boot_device[i] <= 'f') {
216 ppc_boot_device = boot_device[i];
28c5af54 217 break;
0d913fdb 218 }
28c5af54
JM
219 }
220 if (ppc_boot_device == '\0') {
221 fprintf(stderr, "No valid boot device for Mac99 machine\n");
222 exit(1);
223 }
b6b8bd18 224 }
0aa6a4a2 225
3cbee15b 226 isa_mem_base = 0x80000000;
aef445bd 227
3cbee15b
JM
228 /* Register 8 MB of ISA IO space */
229 isa_mmio_init(0xf2000000, 0x00800000);
3b46e624 230
3cbee15b
JM
231 /* UniN init */
232 unin_memory = cpu_register_io_memory(0, unin_read, unin_write, NULL);
233 cpu_register_physical_memory(0xf8000000, 0x00001000, unin_memory);
47103572 234
3cbee15b
JM
235 openpic_irqs = qemu_mallocz(smp_cpus * sizeof(qemu_irq *));
236 openpic_irqs[0] =
237 qemu_mallocz(smp_cpus * sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
238 for (i = 0; i < smp_cpus; i++) {
239 /* Mac99 IRQ connection between OpenPIC outputs pins
240 * and PowerPC input pins
241 */
242 switch (PPC_INPUT(env)) {
243 case PPC_FLAGS_INPUT_6xx:
244 openpic_irqs[i] = openpic_irqs[0] + (i * OPENPIC_OUTPUT_NB);
245 openpic_irqs[i][OPENPIC_OUTPUT_INT] =
246 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
247 openpic_irqs[i][OPENPIC_OUTPUT_CINT] =
248 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
249 openpic_irqs[i][OPENPIC_OUTPUT_MCK] =
250 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_MCP];
251 /* Not connected ? */
252 openpic_irqs[i][OPENPIC_OUTPUT_DEBUG] = NULL;
253 /* Check this */
254 openpic_irqs[i][OPENPIC_OUTPUT_RESET] =
255 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_HRESET];
256 break;
00af685f 257#if defined(TARGET_PPC64)
3cbee15b
JM
258 case PPC_FLAGS_INPUT_970:
259 openpic_irqs[i] = openpic_irqs[0] + (i * OPENPIC_OUTPUT_NB);
260 openpic_irqs[i][OPENPIC_OUTPUT_INT] =
261 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_INT];
262 openpic_irqs[i][OPENPIC_OUTPUT_CINT] =
263 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_INT];
264 openpic_irqs[i][OPENPIC_OUTPUT_MCK] =
265 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_MCP];
266 /* Not connected ? */
267 openpic_irqs[i][OPENPIC_OUTPUT_DEBUG] = NULL;
268 /* Check this */
269 openpic_irqs[i][OPENPIC_OUTPUT_RESET] =
270 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_HRESET];
271 break;
00af685f 272#endif /* defined(TARGET_PPC64) */
3cbee15b
JM
273 default:
274 cpu_abort(env, "Bus model not supported on mac99 machine\n");
275 exit(1);
0aa6a4a2 276 }
3cbee15b
JM
277 }
278 pic = openpic_init(NULL, &pic_mem_index, smp_cpus, openpic_irqs, NULL);
279 pci_bus = pci_pmac_init(pic);
280 /* init basic PC hardware */
3023f332 281 pci_vga_init(pci_bus, phys_ram_base + ram_size,
3cbee15b
JM
282 ram_size, vga_ram_size,
283 vga_bios_offset, vga_bios_size);
aae9366a 284
3cbee15b
JM
285 /* XXX: suppress that */
286 dummy_irq = i8259_init(NULL);
287
aeeb69c7
AJ
288 escc_mem_index = escc_init(0x80013000, dummy_irq[4], dummy_irq[5],
289 serial_hds[0], serial_hds[1], ESCC_CLOCK, 4);
cb457d76
AL
290
291 for(i = 0; i < nb_nics; i++)
292 pci_nic_init(pci_bus, &nd_table[i], -1, "ne2k_pci");
293
e4bcb14c
TS
294 if (drive_get_max_bus(IF_IDE) >= MAX_IDE_BUS) {
295 fprintf(stderr, "qemu: too many IDE bus\n");
296 exit(1);
297 }
298 for(i = 0; i < MAX_IDE_BUS * MAX_IDE_DEVS; i++) {
299 index = drive_get_index(IF_IDE, i / MAX_IDE_DEVS, i % MAX_IDE_DEVS);
300 if (index != -1)
301 hd[i] = drives_table[index].bdrv;
302 else
303 hd[i] = NULL;
304 }
28ce5ce6 305 dbdma = DBDMA_init(&dbdma_mem_index);
0aa6a4a2 306#if 1
e3007e66
AJ
307 ide_mem_index[0] = pmac_ide_init(&hd[0], pic[0x13], dbdma, 0x14, pic[0x01]);
308 ide_mem_index[1] = pmac_ide_init(&hd[2], pic[0x14], dbdma, 0x16, pic[0x02]);
0aa6a4a2 309#else
e4bcb14c 310 pci_cmd646_ide_init(pci_bus, &hd[0], 0);
0aa6a4a2 311#endif
3cbee15b
JM
312 /* cuda also initialize ADB */
313 cuda_init(&cuda_mem_index, pic[0x19]);
aae9366a 314
3cbee15b
JM
315 adb_kbd_init(&adb_bus);
316 adb_mouse_init(&adb_bus);
3b46e624 317
3b46e624 318
4ebcf884
BS
319 macio_init(pci_bus, PCI_DEVICE_ID_APPLE_UNI_N_KEYL, 0, pic_mem_index,
320 dbdma_mem_index, cuda_mem_index, NULL, 2, ide_mem_index,
321 escc_mem_index);
0d92ed30
PB
322
323 if (usb_enabled) {
e24ad6f1 324 usb_ohci_init_pci(pci_bus, 3, -1);
0d92ed30
PB
325 }
326
b6b8bd18
FB
327 if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
328 graphic_depth = 15;
3cbee15b
JM
329#if 0 /* XXX: this is ugly but needed for now, or OHW won't boot */
330 /* The NewWorld NVRAM is not located in the MacIO device */
74e91155 331 nvr = macio_nvram_init(&nvram_mem_index, 0x2000);
3cbee15b 332 pmac_format_nvram_partition(nvr, 0x2000);
74e91155 333 macio_nvram_map(nvr, 0xFFF04000);
3cbee15b
JM
334 nvram.opaque = nvr;
335 nvram.read_fn = &macio_nvram_read;
336 nvram.write_fn = &macio_nvram_write;
337#else
338 m48t59 = m48t59_init(dummy_irq[8], 0xFFF04000, 0x0074, NVRAM_SIZE, 59);
339 nvram.opaque = m48t59;
340 nvram.read_fn = &m48t59_read;
341 nvram.write_fn = &m48t59_write;
342#endif
6ac0e82d
AZ
343 PPC_NVRAM_set_params(&nvram, NVRAM_SIZE, "MAC99", ram_size,
344 ppc_boot_device, kernel_base, kernel_size,
b6b8bd18
FB
345 kernel_cmdline,
346 initrd_base, initrd_size,
64201201 347 /* XXX: need an option to load a NVRAM image */
b6b8bd18
FB
348 0,
349 graphic_width, graphic_height, graphic_depth);
350 /* No PCI init: the BIOS will do it */
0aa6a4a2
FB
351
352 /* Special port to get debug messages from Open-Firmware */
353 register_ioport_write(0x0F00, 4, 1, &PPC_debug_write, NULL);
aae9366a 354}
0aa6a4a2
FB
355
356QEMUMachine core99_machine = {
4b32e168
AL
357 .name = "mac99",
358 .desc = "Mac99 based PowerMAC",
359 .init = ppc_core99_init,
864c136a 360 .ram_require = BIOS_SIZE + VGA_BIOS_SIZE + VGA_RAM_SIZE,
3d878caa 361 .max_cpus = MAX_CPUS,
0aa6a4a2 362};