Update Sparc parts in documentation
[qemu/qemu_0_9_1_stable.git] / hw / ppc_chrp.c
blobf53c85b8559e9b6de2b212e4a291430c58bf9e10
1 /*
2 * QEMU PPC CHRP/PMAC hardware System Emulator
4 * Copyright (c) 2004-2007 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "vl.h"
26 /* SMP is not enabled, for now */
27 #define MAX_CPUS 1
29 #define BIOS_FILENAME "ppc_rom.bin"
30 #define VGABIOS_FILENAME "video.x"
31 #define NVRAM_SIZE 0x2000
33 #define KERNEL_LOAD_ADDR 0x01000000
34 #define INITRD_LOAD_ADDR 0x01800000
36 /* MacIO devices (mapped inside the MacIO address space): CUDA, DBDMA,
37 NVRAM */
39 static int dbdma_mem_index;
40 static int cuda_mem_index;
41 static int ide0_mem_index = -1;
42 static int ide1_mem_index = -1;
43 static int openpic_mem_index = -1;
44 static int heathrow_pic_mem_index = -1;
45 static int macio_nvram_mem_index = -1;
47 /* DBDMA: currently no op - should suffice right now */
49 static void dbdma_writeb (void *opaque,
50 target_phys_addr_t addr, uint32_t value)
52 printf("%s: 0x" PADDRX " <= 0x%08x\n", __func__, addr, value);
55 static void dbdma_writew (void *opaque,
56 target_phys_addr_t addr, uint32_t value)
60 static void dbdma_writel (void *opaque,
61 target_phys_addr_t addr, uint32_t value)
65 static uint32_t dbdma_readb (void *opaque, target_phys_addr_t addr)
67 printf("%s: 0x" PADDRX " => 0x00000000\n", __func__, addr);
69 return 0;
72 static uint32_t dbdma_readw (void *opaque, target_phys_addr_t addr)
74 return 0;
77 static uint32_t dbdma_readl (void *opaque, target_phys_addr_t addr)
79 return 0;
82 static CPUWriteMemoryFunc *dbdma_write[] = {
83 &dbdma_writeb,
84 &dbdma_writew,
85 &dbdma_writel,
88 static CPUReadMemoryFunc *dbdma_read[] = {
89 &dbdma_readb,
90 &dbdma_readw,
91 &dbdma_readl,
94 /* macio style NVRAM device */
95 typedef struct MacIONVRAMState {
96 uint8_t data[0x2000];
97 } MacIONVRAMState;
99 static void macio_nvram_writeb (void *opaque,
100 target_phys_addr_t addr, uint32_t value)
102 MacIONVRAMState *s = opaque;
103 addr = (addr >> 4) & 0x1fff;
104 s->data[addr] = value;
105 // printf("macio_nvram_writeb %04x = %02x\n", addr, value);
108 static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr)
110 MacIONVRAMState *s = opaque;
111 uint32_t value;
113 addr = (addr >> 4) & 0x1fff;
114 value = s->data[addr];
115 // printf("macio_nvram_readb %04x = %02x\n", addr, value);
117 return value;
120 static CPUWriteMemoryFunc *macio_nvram_write[] = {
121 &macio_nvram_writeb,
122 &macio_nvram_writeb,
123 &macio_nvram_writeb,
126 static CPUReadMemoryFunc *macio_nvram_read[] = {
127 &macio_nvram_readb,
128 &macio_nvram_readb,
129 &macio_nvram_readb,
132 static MacIONVRAMState *macio_nvram_init (void)
134 MacIONVRAMState *s;
135 s = qemu_mallocz(sizeof(MacIONVRAMState));
136 if (!s)
137 return NULL;
138 macio_nvram_mem_index = cpu_register_io_memory(0, macio_nvram_read,
139 macio_nvram_write, s);
141 return s;
144 static void macio_map (PCIDevice *pci_dev, int region_num,
145 uint32_t addr, uint32_t size, int type)
147 if (heathrow_pic_mem_index >= 0) {
148 cpu_register_physical_memory(addr + 0x00000, 0x1000,
149 heathrow_pic_mem_index);
151 cpu_register_physical_memory(addr + 0x08000, 0x1000, dbdma_mem_index);
152 cpu_register_physical_memory(addr + 0x16000, 0x2000, cuda_mem_index);
153 if (ide0_mem_index >= 0)
154 cpu_register_physical_memory(addr + 0x1f000, 0x1000, ide0_mem_index);
155 if (ide1_mem_index >= 0)
156 cpu_register_physical_memory(addr + 0x20000, 0x1000, ide1_mem_index);
157 if (openpic_mem_index >= 0) {
158 cpu_register_physical_memory(addr + 0x40000, 0x40000,
159 openpic_mem_index);
161 if (macio_nvram_mem_index >= 0)
162 cpu_register_physical_memory(addr + 0x60000, 0x20000,
163 macio_nvram_mem_index);
166 static void macio_init (PCIBus *bus, int device_id)
168 PCIDevice *d;
170 d = pci_register_device(bus, "macio", sizeof(PCIDevice),
171 -1, NULL, NULL);
172 /* Note: this code is strongly inspirated from the corresponding code
173 in PearPC */
174 d->config[0x00] = 0x6b; // vendor_id
175 d->config[0x01] = 0x10;
176 d->config[0x02] = device_id;
177 d->config[0x03] = device_id >> 8;
179 d->config[0x0a] = 0x00; // class_sub = pci2pci
180 d->config[0x0b] = 0xff; // class_base = bridge
181 d->config[0x0e] = 0x00; // header_type
183 d->config[0x3d] = 0x01; // interrupt on pin 1
185 dbdma_mem_index = cpu_register_io_memory(0, dbdma_read, dbdma_write, NULL);
187 pci_register_io_region(d, 0, 0x80000,
188 PCI_ADDRESS_SPACE_MEM, macio_map);
191 /* UniN device */
192 static void unin_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
196 static uint32_t unin_readl (void *opaque, target_phys_addr_t addr)
198 return 0;
201 static CPUWriteMemoryFunc *unin_write[] = {
202 &unin_writel,
203 &unin_writel,
204 &unin_writel,
207 static CPUReadMemoryFunc *unin_read[] = {
208 &unin_readl,
209 &unin_readl,
210 &unin_readl,
213 /* temporary frame buffer OSI calls for the video.x driver. The right
214 solution is to modify the driver to use VGA PCI I/Os */
215 /* XXX: to be removed. This is no way related to emulation */
216 static int vga_osi_call (CPUState *env)
218 static int vga_vbl_enabled;
219 int linesize;
221 // printf("osi_call R5=%d\n", env->gpr[5]);
223 /* same handler as PearPC, coming from the original MOL video
224 driver. */
225 switch(env->gpr[5]) {
226 case 4:
227 break;
228 case 28: /* set_vmode */
229 if (env->gpr[6] != 1 || env->gpr[7] != 0)
230 env->gpr[3] = 1;
231 else
232 env->gpr[3] = 0;
233 break;
234 case 29: /* get_vmode_info */
235 if (env->gpr[6] != 0) {
236 if (env->gpr[6] != 1 || env->gpr[7] != 0) {
237 env->gpr[3] = 1;
238 break;
241 env->gpr[3] = 0;
242 env->gpr[4] = (1 << 16) | 1; /* num_vmodes, cur_vmode */
243 env->gpr[5] = (1 << 16) | 0; /* num_depths, cur_depth_mode */
244 env->gpr[6] = (graphic_width << 16) | graphic_height; /* w, h */
245 env->gpr[7] = 85 << 16; /* refresh rate */
246 env->gpr[8] = (graphic_depth + 7) & ~7; /* depth (round to byte) */
247 linesize = ((graphic_depth + 7) >> 3) * graphic_width;
248 linesize = (linesize + 3) & ~3;
249 env->gpr[9] = (linesize << 16) | 0; /* row_bytes, offset */
250 break;
251 case 31: /* set_video power */
252 env->gpr[3] = 0;
253 break;
254 case 39: /* video_ctrl */
255 if (env->gpr[6] == 0 || env->gpr[6] == 1)
256 vga_vbl_enabled = env->gpr[6];
257 env->gpr[3] = 0;
258 break;
259 case 47:
260 break;
261 case 59: /* set_color */
262 /* R6 = index, R7 = RGB */
263 env->gpr[3] = 0;
264 break;
265 case 64: /* get color */
266 /* R6 = index */
267 env->gpr[3] = 0;
268 break;
269 case 116: /* set hwcursor */
270 /* R6 = x, R7 = y, R8 = visible, R9 = data */
271 break;
272 default:
273 fprintf(stderr, "unsupported OSI call R5=" REGX "\n", env->gpr[5]);
274 break;
277 return 1; /* osi_call handled */
280 static uint8_t nvram_chksum (const uint8_t *buf, int n)
282 int sum, i;
283 sum = 0;
284 for(i = 0; i < n; i++)
285 sum += buf[i];
286 return (sum & 0xff) + (sum >> 8);
289 /* set a free Mac OS NVRAM partition */
290 void pmac_format_nvram_partition (uint8_t *buf, int len)
292 char partition_name[12] = "wwwwwwwwwwww";
294 buf[0] = 0x7f; /* free partition magic */
295 buf[1] = 0; /* checksum */
296 buf[2] = len >> 8;
297 buf[3] = len;
298 memcpy(buf + 4, partition_name, 12);
299 buf[1] = nvram_chksum(buf, 16);
302 /* PowerPC CHRP hardware initialisation */
303 static void ppc_chrp_init (int ram_size, int vga_ram_size, int boot_device,
304 DisplayState *ds, const char **fd_filename,
305 int snapshot,
306 const char *kernel_filename,
307 const char *kernel_cmdline,
308 const char *initrd_filename,
309 const char *cpu_model,
310 int is_heathrow)
312 CPUState *env, *envs[MAX_CPUS];
313 char buf[1024];
314 qemu_irq *pic, **openpic_irqs;
315 m48t59_t *nvram;
316 int unin_memory;
317 int linux_boot, i;
318 unsigned long bios_offset, vga_bios_offset;
319 uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
320 ppc_def_t *def;
321 PCIBus *pci_bus;
322 const char *arch_name;
323 int vga_bios_size, bios_size;
324 qemu_irq *dummy_irq;
326 linux_boot = (kernel_filename != NULL);
328 /* init CPUs */
329 env = cpu_init();
330 if (cpu_model == NULL)
331 cpu_model = "default";
332 ppc_find_by_name(cpu_model, &def);
333 if (def == NULL) {
334 cpu_abort(env, "Unable to find PowerPC CPU definition\n");
336 for (i = 0; i < smp_cpus; i++) {
337 cpu_ppc_register(env, def);
338 cpu_ppc_reset(env);
339 /* Set time-base frequency to 100 Mhz */
340 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
341 env->osi_call = vga_osi_call;
342 qemu_register_reset(&cpu_ppc_reset, env);
343 register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
344 envs[i] = env;
347 /* allocate RAM */
348 cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
350 /* allocate and load BIOS */
351 bios_offset = ram_size + vga_ram_size;
352 if (bios_name == NULL)
353 bios_name = BIOS_FILENAME;
354 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
355 bios_size = load_image(buf, phys_ram_base + bios_offset);
356 if (bios_size < 0 || bios_size > BIOS_SIZE) {
357 cpu_abort(env, "qemu: could not load PowerPC bios '%s'\n", buf);
358 exit(1);
360 bios_size = (bios_size + 0xfff) & ~0xfff;
361 cpu_register_physical_memory((uint32_t)(-bios_size),
362 bios_size, bios_offset | IO_MEM_ROM);
364 /* allocate and load VGA BIOS */
365 vga_bios_offset = bios_offset + bios_size;
366 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
367 vga_bios_size = load_image(buf, phys_ram_base + vga_bios_offset + 8);
368 if (vga_bios_size < 0) {
369 /* if no bios is present, we can still work */
370 fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n", buf);
371 vga_bios_size = 0;
372 } else {
373 /* set a specific header (XXX: find real Apple format for NDRV
374 drivers) */
375 phys_ram_base[vga_bios_offset] = 'N';
376 phys_ram_base[vga_bios_offset + 1] = 'D';
377 phys_ram_base[vga_bios_offset + 2] = 'R';
378 phys_ram_base[vga_bios_offset + 3] = 'V';
379 cpu_to_be32w((uint32_t *)(phys_ram_base + vga_bios_offset + 4),
380 vga_bios_size);
381 vga_bios_size += 8;
383 vga_bios_size = (vga_bios_size + 0xfff) & ~0xfff;
385 if (linux_boot) {
386 kernel_base = KERNEL_LOAD_ADDR;
387 /* now we can load the kernel */
388 kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
389 if (kernel_size < 0) {
390 cpu_abort(env, "qemu: could not load kernel '%s'\n",
391 kernel_filename);
392 exit(1);
394 /* load initrd */
395 if (initrd_filename) {
396 initrd_base = INITRD_LOAD_ADDR;
397 initrd_size = load_image(initrd_filename,
398 phys_ram_base + initrd_base);
399 if (initrd_size < 0) {
400 cpu_abort(env, "qemu: could not load initial ram disk '%s'\n",
401 initrd_filename);
402 exit(1);
404 } else {
405 initrd_base = 0;
406 initrd_size = 0;
408 boot_device = 'm';
409 } else {
410 kernel_base = 0;
411 kernel_size = 0;
412 initrd_base = 0;
413 initrd_size = 0;
416 if (is_heathrow) {
417 isa_mem_base = 0x80000000;
419 /* Register 2 MB of ISA IO space */
420 isa_mmio_init(0xfe000000, 0x00200000);
422 /* init basic PC hardware */
423 if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
424 cpu_abort(env, "Only 6xx bus is supported on heathrow machine\n");
425 exit(1);
427 pic = heathrow_pic_init(&heathrow_pic_mem_index);
428 pci_bus = pci_grackle_init(0xfec00000, pic);
429 pci_vga_init(pci_bus, ds, phys_ram_base + ram_size,
430 ram_size, vga_ram_size,
431 vga_bios_offset, vga_bios_size);
433 /* XXX: suppress that */
434 dummy_irq = i8259_init(NULL);
436 /* XXX: use Mac Serial port */
437 serial_init(0x3f8, dummy_irq[4], serial_hds[0]);
439 for(i = 0; i < nb_nics; i++) {
440 if (!nd_table[i].model)
441 nd_table[i].model = "ne2k_pci";
442 pci_nic_init(pci_bus, &nd_table[i], -1);
445 pci_cmd646_ide_init(pci_bus, &bs_table[0], 0);
447 /* cuda also initialize ADB */
448 cuda_mem_index = cuda_init(pic[0x12]);
450 adb_kbd_init(&adb_bus);
451 adb_mouse_init(&adb_bus);
454 MacIONVRAMState *nvr;
455 nvr = macio_nvram_init();
456 pmac_format_nvram_partition(nvr->data, 0x2000);
459 macio_init(pci_bus, 0x0017);
461 nvram = m48t59_init(dummy_irq[8], 0xFFF04000, 0x0074, NVRAM_SIZE, 59);
463 arch_name = "HEATHROW";
464 } else {
465 isa_mem_base = 0x80000000;
467 /* Register 8 MB of ISA IO space */
468 isa_mmio_init(0xf2000000, 0x00800000);
470 /* UniN init */
471 unin_memory = cpu_register_io_memory(0, unin_read, unin_write, NULL);
472 cpu_register_physical_memory(0xf8000000, 0x00001000, unin_memory);
474 openpic_irqs = qemu_mallocz(smp_cpus * sizeof(qemu_irq *));
475 openpic_irqs[0] =
476 qemu_mallocz(smp_cpus * sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
477 for (i = 0; i < smp_cpus; i++) {
478 /* Mac99 IRQ connection between OpenPIC outputs pins
479 * and PowerPC input pins
481 switch (PPC_INPUT(env)) {
482 case PPC_FLAGS_INPUT_6xx:
483 openpic_irqs[i] = openpic_irqs[0] + (i * OPENPIC_OUTPUT_NB);
484 openpic_irqs[i][OPENPIC_OUTPUT_INT] =
485 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
486 openpic_irqs[i][OPENPIC_OUTPUT_CINT] =
487 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
488 openpic_irqs[i][OPENPIC_OUTPUT_MCK] =
489 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_MCP];
490 /* Not connected ? */
491 openpic_irqs[i][OPENPIC_OUTPUT_DEBUG] = NULL;
492 /* Check this */
493 openpic_irqs[i][OPENPIC_OUTPUT_RESET] =
494 ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_HRESET];
495 break;
496 #if defined(TARGET_PPC64)
497 case PPC_FLAGS_INPUT_970:
498 openpic_irqs[i] = openpic_irqs[0] + (i * OPENPIC_OUTPUT_NB);
499 openpic_irqs[i][OPENPIC_OUTPUT_INT] =
500 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_INT];
501 openpic_irqs[i][OPENPIC_OUTPUT_CINT] =
502 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_INT];
503 openpic_irqs[i][OPENPIC_OUTPUT_MCK] =
504 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_MCP];
505 /* Not connected ? */
506 openpic_irqs[i][OPENPIC_OUTPUT_DEBUG] = NULL;
507 /* Check this */
508 openpic_irqs[i][OPENPIC_OUTPUT_RESET] =
509 ((qemu_irq *)env->irq_inputs)[PPC970_INPUT_HRESET];
510 break;
511 #endif /* defined(TARGET_PPC64) */
512 default:
513 cpu_abort(env, "Bus model not supported on mac99 machine\n");
514 exit(1);
517 pic = openpic_init(NULL, &openpic_mem_index, smp_cpus,
518 openpic_irqs, NULL);
519 pci_bus = pci_pmac_init(pic);
520 /* init basic PC hardware */
521 pci_vga_init(pci_bus, ds, phys_ram_base + ram_size,
522 ram_size, vga_ram_size,
523 vga_bios_offset, vga_bios_size);
525 /* XXX: suppress that */
526 dummy_irq = i8259_init(NULL);
528 /* XXX: use Mac Serial port */
529 serial_init(0x3f8, dummy_irq[4], serial_hds[0]);
530 for(i = 0; i < nb_nics; i++) {
531 if (!nd_table[i].model)
532 nd_table[i].model = "ne2k_pci";
533 pci_nic_init(pci_bus, &nd_table[i], -1);
535 #if 1
536 ide0_mem_index = pmac_ide_init(&bs_table[0], pic[0x13]);
537 ide1_mem_index = pmac_ide_init(&bs_table[2], pic[0x14]);
538 #else
539 pci_cmd646_ide_init(pci_bus, &bs_table[0], 0);
540 #endif
541 /* cuda also initialize ADB */
542 cuda_mem_index = cuda_init(pic[0x19]);
544 adb_kbd_init(&adb_bus);
545 adb_mouse_init(&adb_bus);
547 macio_init(pci_bus, 0x0022);
549 nvram = m48t59_init(dummy_irq[8], 0xFFF04000, 0x0074, NVRAM_SIZE, 59);
551 arch_name = "MAC99";
554 if (usb_enabled) {
555 usb_ohci_init_pci(pci_bus, 3, -1);
558 if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
559 graphic_depth = 15;
561 PPC_NVRAM_set_params(nvram, NVRAM_SIZE, arch_name, ram_size, boot_device,
562 kernel_base, kernel_size,
563 kernel_cmdline,
564 initrd_base, initrd_size,
565 /* XXX: need an option to load a NVRAM image */
567 graphic_width, graphic_height, graphic_depth);
568 /* No PCI init: the BIOS will do it */
570 /* Special port to get debug messages from Open-Firmware */
571 register_ioport_write(0x0F00, 4, 1, &PPC_debug_write, NULL);
574 static void ppc_core99_init (int ram_size, int vga_ram_size, int boot_device,
575 DisplayState *ds, const char **fd_filename,
576 int snapshot,
577 const char *kernel_filename,
578 const char *kernel_cmdline,
579 const char *initrd_filename,
580 const char *cpu_model)
582 ppc_chrp_init(ram_size, vga_ram_size, boot_device,
583 ds, fd_filename, snapshot,
584 kernel_filename, kernel_cmdline,
585 initrd_filename, cpu_model, 0);
588 static void ppc_heathrow_init (int ram_size, int vga_ram_size, int boot_device,
589 DisplayState *ds, const char **fd_filename,
590 int snapshot,
591 const char *kernel_filename,
592 const char *kernel_cmdline,
593 const char *initrd_filename,
594 const char *cpu_model)
596 ppc_chrp_init(ram_size, vga_ram_size, boot_device,
597 ds, fd_filename, snapshot,
598 kernel_filename, kernel_cmdline,
599 initrd_filename, cpu_model, 1);
602 QEMUMachine core99_machine = {
603 "mac99",
604 "Mac99 based PowerMAC",
605 ppc_core99_init,
608 QEMUMachine heathrow_machine = {
609 "g3bw",
610 "Heathrow based PowerMAC",
611 ppc_heathrow_init,