]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc_chrp.c
Note FPU enable/disable issue.
[mirror_qemu.git] / hw / ppc_chrp.c
CommitLineData
64201201
FB
1/*
2 * QEMU PPC CHRP/PMAC hardware System Emulator
3 *
4 * Copyright (c) 2004 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24#include "vl.h"
25
26#define BIOS_FILENAME "ppc_rom.bin"
d5295253 27#define VGABIOS_FILENAME "video.x"
64201201
FB
28#define NVRAM_SIZE 0x2000
29
b6b8bd18
FB
30#define KERNEL_LOAD_ADDR 0x01000000
31#define INITRD_LOAD_ADDR 0x01800000
32
267002cd 33/* MacIO devices (mapped inside the MacIO address space): CUDA, DBDMA,
e5733356 34 NVRAM */
267002cd
FB
35
36static int dbdma_mem_index;
37static int cuda_mem_index;
0aa6a4a2
FB
38static int ide0_mem_index = -1;
39static int ide1_mem_index = -1;
40static int openpic_mem_index = -1;
41static int heathrow_pic_mem_index = -1;
e5733356 42static int macio_nvram_mem_index = -1;
267002cd
FB
43
44/* DBDMA: currently no op - should suffice right now */
45
46static void dbdma_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
47{
b6b8bd18 48 printf("%s: 0x%08x <= 0x%08x\n", __func__, addr, value);
267002cd
FB
49}
50
51static void dbdma_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
52{
53}
54
55static void dbdma_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
56{
57}
58
59static uint32_t dbdma_readb (void *opaque, target_phys_addr_t addr)
60{
b6b8bd18 61 printf("%s: 0x%08x => 0x00000000\n", __func__, addr);
267002cd
FB
62 return 0;
63}
64
65static uint32_t dbdma_readw (void *opaque, target_phys_addr_t addr)
66{
67 return 0;
68}
69
70static uint32_t dbdma_readl (void *opaque, target_phys_addr_t addr)
71{
72 return 0;
73}
74
75static CPUWriteMemoryFunc *dbdma_write[] = {
76 &dbdma_writeb,
77 &dbdma_writew,
78 &dbdma_writel,
79};
80
81static CPUReadMemoryFunc *dbdma_read[] = {
82 &dbdma_readb,
83 &dbdma_readw,
84 &dbdma_readl,
85};
86
e5733356
FB
87/* macio style NVRAM device */
88typedef struct MacIONVRAMState {
89 uint8_t data[0x2000];
90} MacIONVRAMState;
91
92static void macio_nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
93{
94 MacIONVRAMState *s = opaque;
95 addr = (addr >> 4) & 0x1fff;
96 s->data[addr] = value;
97 // printf("macio_nvram_writeb %04x = %02x\n", addr, value);
98}
99
100static uint32_t macio_nvram_readb (void *opaque, target_phys_addr_t addr)
101{
102 MacIONVRAMState *s = opaque;
103 uint32_t value;
104
105 addr = (addr >> 4) & 0x1fff;
106 value = s->data[addr];
107 // printf("macio_nvram_readb %04x = %02x\n", addr, value);
108 return value;
109}
110
111static CPUWriteMemoryFunc *macio_nvram_write[] = {
112 &macio_nvram_writeb,
113 &macio_nvram_writeb,
114 &macio_nvram_writeb,
115};
116
117static CPUReadMemoryFunc *macio_nvram_read[] = {
118 &macio_nvram_readb,
119 &macio_nvram_readb,
120 &macio_nvram_readb,
121};
122
123static MacIONVRAMState *macio_nvram_init(void)
124{
125 MacIONVRAMState *s;
126 s = qemu_mallocz(sizeof(MacIONVRAMState));
127 if (!s)
128 return NULL;
129 macio_nvram_mem_index = cpu_register_io_memory(0, macio_nvram_read,
130 macio_nvram_write, s);
131 return s;
132}
133
267002cd
FB
134static void macio_map(PCIDevice *pci_dev, int region_num,
135 uint32_t addr, uint32_t size, int type)
136{
0aa6a4a2
FB
137 if (heathrow_pic_mem_index >= 0) {
138 cpu_register_physical_memory(addr + 0x00000, 0x1000,
139 heathrow_pic_mem_index);
140 }
267002cd
FB
141 cpu_register_physical_memory(addr + 0x08000, 0x1000, dbdma_mem_index);
142 cpu_register_physical_memory(addr + 0x16000, 0x2000, cuda_mem_index);
0aa6a4a2
FB
143 if (ide0_mem_index >= 0)
144 cpu_register_physical_memory(addr + 0x1f000, 0x1000, ide0_mem_index);
145 if (ide1_mem_index >= 0)
146 cpu_register_physical_memory(addr + 0x20000, 0x1000, ide1_mem_index);
147 if (openpic_mem_index >= 0) {
148 cpu_register_physical_memory(addr + 0x40000, 0x40000,
149 openpic_mem_index);
150 }
e5733356
FB
151 if (macio_nvram_mem_index >= 0)
152 cpu_register_physical_memory(addr + 0x60000, 0x20000, macio_nvram_mem_index);
267002cd
FB
153}
154
e5733356 155static void macio_init(PCIBus *bus, int device_id)
267002cd
FB
156{
157 PCIDevice *d;
158
46e50e9d
FB
159 d = pci_register_device(bus, "macio", sizeof(PCIDevice),
160 -1, NULL, NULL);
267002cd
FB
161 /* Note: this code is strongly inspirated from the corresponding code
162 in PearPC */
163 d->config[0x00] = 0x6b; // vendor_id
164 d->config[0x01] = 0x10;
e5733356
FB
165 d->config[0x02] = device_id;
166 d->config[0x03] = device_id >> 8;
267002cd
FB
167
168 d->config[0x0a] = 0x00; // class_sub = pci2pci
169 d->config[0x0b] = 0xff; // class_base = bridge
170 d->config[0x0e] = 0x00; // header_type
171
172 d->config[0x3d] = 0x01; // interrupt on pin 1
173
174 dbdma_mem_index = cpu_register_io_memory(0, dbdma_read, dbdma_write, NULL);
175
176 pci_register_io_region(d, 0, 0x80000,
177 PCI_ADDRESS_SPACE_MEM, macio_map);
178}
179
0aa6a4a2
FB
180/* UniN device */
181static void unin_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
182{
183}
184
185static uint32_t unin_readl (void *opaque, target_phys_addr_t addr)
186{
187 return 0;
188}
189
190static CPUWriteMemoryFunc *unin_write[] = {
191 &unin_writel,
192 &unin_writel,
193 &unin_writel,
194};
195
196static CPUReadMemoryFunc *unin_read[] = {
197 &unin_readl,
198 &unin_readl,
199 &unin_readl,
200};
201
202/* temporary frame buffer OSI calls for the video.x driver. The right
203 solution is to modify the driver to use VGA PCI I/Os */
204static int vga_osi_call(CPUState *env)
205{
206 static int vga_vbl_enabled;
207 int linesize;
208
209 // printf("osi_call R5=%d\n", env->gpr[5]);
210
211 /* same handler as PearPC, coming from the original MOL video
212 driver. */
213 switch(env->gpr[5]) {
214 case 4:
215 break;
216 case 28: /* set_vmode */
217 if (env->gpr[6] != 1 || env->gpr[7] != 0)
218 env->gpr[3] = 1;
219 else
220 env->gpr[3] = 0;
221 break;
222 case 29: /* get_vmode_info */
223 if (env->gpr[6] != 0) {
224 if (env->gpr[6] != 1 || env->gpr[7] != 0) {
225 env->gpr[3] = 1;
226 break;
227 }
228 }
229 env->gpr[3] = 0;
230 env->gpr[4] = (1 << 16) | 1; /* num_vmodes, cur_vmode */
231 env->gpr[5] = (1 << 16) | 0; /* num_depths, cur_depth_mode */
232 env->gpr[6] = (graphic_width << 16) | graphic_height; /* w, h */
233 env->gpr[7] = 85 << 16; /* refresh rate */
234 env->gpr[8] = (graphic_depth + 7) & ~7; /* depth (round to byte) */
235 linesize = ((graphic_depth + 7) >> 3) * graphic_width;
236 linesize = (linesize + 3) & ~3;
237 env->gpr[9] = (linesize << 16) | 0; /* row_bytes, offset */
238 break;
239 case 31: /* set_video power */
240 env->gpr[3] = 0;
241 break;
242 case 39: /* video_ctrl */
243 if (env->gpr[6] == 0 || env->gpr[6] == 1)
244 vga_vbl_enabled = env->gpr[6];
245 env->gpr[3] = 0;
246 break;
247 case 47:
248 break;
249 case 59: /* set_color */
250 /* R6 = index, R7 = RGB */
251 env->gpr[3] = 0;
252 break;
253 case 64: /* get color */
254 /* R6 = index */
255 env->gpr[3] = 0;
256 break;
257 case 116: /* set hwcursor */
258 /* R6 = x, R7 = y, R8 = visible, R9 = data */
259 break;
260 default:
261 fprintf(stderr, "unsupported OSI call R5=%08x\n", env->gpr[5]);
262 break;
263 }
264 return 1; /* osi_call handled */
265}
266
3de388f6
FB
267/* XXX: suppress that */
268static void pic_irq_request(void *opaque, int level)
269{
270}
271
e5733356
FB
272static uint8_t nvram_chksum(const uint8_t *buf, int n)
273{
274 int sum, i;
275 sum = 0;
276 for(i = 0; i < n; i++)
277 sum += buf[i];
278 return (sum & 0xff) + (sum >> 8);
279}
280
281/* set a free Mac OS NVRAM partition */
282void pmac_format_nvram_partition(uint8_t *buf, int len)
283{
284 char partition_name[12] = "wwwwwwwwwwww";
285
286 buf[0] = 0x7f; /* free partition magic */
287 buf[1] = 0; /* checksum */
288 buf[2] = len >> 8;
289 buf[3] = len;
290 memcpy(buf + 4, partition_name, 12);
291 buf[1] = nvram_chksum(buf, 16);
292}
293
0aa6a4a2 294/* PowerPC CHRP hardware initialisation */
94fc95cd
JM
295static void ppc_chrp_init (int ram_size, int vga_ram_size, int boot_device,
296 DisplayState *ds, const char **fd_filename,
297 int snapshot,
298 const char *kernel_filename,
299 const char *kernel_cmdline,
300 const char *initrd_filename,
301 const char *cpu_model,
302 int is_heathrow)
64201201 303{
c68ea704 304 CPUState *env;
64201201 305 char buf[1024];
0aa6a4a2
FB
306 SetIRQFunc *set_irq;
307 void *pic;
64201201 308 m48t59_t *nvram;
aef445bd 309 int unin_memory;
d5295253
FB
310 int linux_boot, i;
311 unsigned long bios_offset, vga_bios_offset;
b6b8bd18 312 uint32_t kernel_base, kernel_size, initrd_base, initrd_size;
3fc6c082 313 ppc_def_t *def;
46e50e9d 314 PCIBus *pci_bus;
0aa6a4a2 315 const char *arch_name;
d5295253 316 int vga_bios_size, bios_size;
46e50e9d 317
64201201
FB
318 linux_boot = (kernel_filename != NULL);
319
c68ea704
FB
320 /* init CPUs */
321 env = cpu_init();
322 register_savevm("cpu", 0, 3, cpu_save, cpu_load, env);
323
94fc95cd
JM
324 /* Default CPU is a generic 74x/75x */
325 if (cpu_model == NULL)
326 cpu_model = "750";
c68ea704
FB
327 /* XXX: CPU model (or PVR) should be provided on command line */
328 // ppc_find_by_name("750gx", &def); // Linux boot OK
329 // ppc_find_by_name("750fx", &def); // Linux boot OK
330 /* Linux does not boot on 750cxe (and probably other 750cx based)
331 * because it assumes it has 8 IBAT & DBAT pairs as it only have 4.
332 */
94fc95cd 333 ppc_find_by_name(cpu_model, &def);
c68ea704
FB
334 if (def == NULL) {
335 cpu_abort(env, "Unable to find PowerPC CPU definition\n");
336 }
337 cpu_ppc_register(env, def);
338
339 /* Set time-base frequency to 100 Mhz */
340 cpu_ppc_tb_init(env, 100UL * 1000UL * 1000UL);
341
342 env->osi_call = vga_osi_call;
343
64201201
FB
344 /* allocate RAM */
345 cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);
346
347 /* allocate and load BIOS */
348 bios_offset = ram_size + vga_ram_size;
349 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, BIOS_FILENAME);
d5295253
FB
350 bios_size = load_image(buf, phys_ram_base + bios_offset);
351 if (bios_size < 0 || bios_size > BIOS_SIZE) {
352 fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n", buf);
64201201
FB
353 exit(1);
354 }
d5295253
FB
355 bios_size = (bios_size + 0xfff) & ~0xfff;
356 cpu_register_physical_memory((uint32_t)(-bios_size),
357 bios_size, bios_offset | IO_MEM_ROM);
358
359 /* allocate and load VGA BIOS */
360 vga_bios_offset = bios_offset + bios_size;
361 snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
362 vga_bios_size = load_image(buf, phys_ram_base + vga_bios_offset + 8);
363 if (vga_bios_size < 0) {
364 /* if no bios is present, we can still work */
365 fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n", buf);
366 vga_bios_size = 0;
367 } else {
368 /* set a specific header (XXX: find real Apple format for NDRV
369 drivers) */
370 phys_ram_base[vga_bios_offset] = 'N';
371 phys_ram_base[vga_bios_offset + 1] = 'D';
372 phys_ram_base[vga_bios_offset + 2] = 'R';
373 phys_ram_base[vga_bios_offset + 3] = 'V';
374 cpu_to_be32w((uint32_t *)(phys_ram_base + vga_bios_offset + 4),
375 vga_bios_size);
376 vga_bios_size += 8;
377 }
378 vga_bios_size = (vga_bios_size + 0xfff) & ~0xfff;
379
b6b8bd18
FB
380 if (linux_boot) {
381 kernel_base = KERNEL_LOAD_ADDR;
382 /* now we can load the kernel */
383 kernel_size = load_image(kernel_filename, phys_ram_base + kernel_base);
384 if (kernel_size < 0) {
385 fprintf(stderr, "qemu: could not load kernel '%s'\n",
386 kernel_filename);
387 exit(1);
388 }
389 /* load initrd */
390 if (initrd_filename) {
391 initrd_base = INITRD_LOAD_ADDR;
392 initrd_size = load_image(initrd_filename,
393 phys_ram_base + initrd_base);
394 if (initrd_size < 0) {
395 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
396 initrd_filename);
397 exit(1);
398 }
399 } else {
400 initrd_base = 0;
401 initrd_size = 0;
402 }
403 boot_device = 'm';
404 } else {
405 kernel_base = 0;
406 kernel_size = 0;
407 initrd_base = 0;
408 initrd_size = 0;
409 }
0aa6a4a2
FB
410
411 if (is_heathrow) {
412 isa_mem_base = 0x80000000;
0aa6a4a2
FB
413
414 /* Register 2 MB of ISA IO space */
aef445bd
PB
415 isa_mmio_init(0xfe000000, 0x00200000);
416
0aa6a4a2 417 /* init basic PC hardware */
502a5395
PB
418 pic = heathrow_pic_init(&heathrow_pic_mem_index);
419 set_irq = heathrow_pic_set_irq;
420 pci_bus = pci_grackle_init(0xfec00000, pic);
89b6b508
FB
421 pci_vga_init(pci_bus, ds, phys_ram_base + ram_size,
422 ram_size, vga_ram_size,
423 vga_bios_offset, vga_bios_size);
0aa6a4a2
FB
424
425 /* XXX: suppress that */
fa296b0f 426 isa_pic = pic_init(pic_irq_request, NULL);
0aa6a4a2
FB
427
428 /* XXX: use Mac Serial port */
e5d13e2f 429 serial_init(&pic_set_irq_new, isa_pic, 0x3f8, 4, serial_hds[0]);
0aa6a4a2
FB
430
431 for(i = 0; i < nb_nics; i++) {
a41b2ff2
PB
432 if (!nd_table[i].model)
433 nd_table[i].model = "ne2k_pci";
abcebc7e 434 pci_nic_init(pci_bus, &nd_table[i], -1);
0aa6a4a2
FB
435 }
436
437 pci_cmd646_ide_init(pci_bus, &bs_table[0], 0);
438
439 /* cuda also initialize ADB */
440 cuda_mem_index = cuda_init(set_irq, pic, 0x12);
441
442 adb_kbd_init(&adb_bus);
443 adb_mouse_init(&adb_bus);
444
e5733356
FB
445 {
446 MacIONVRAMState *nvr;
447 nvr = macio_nvram_init();
448 pmac_format_nvram_partition(nvr->data, 0x2000);
449 }
450
451 macio_init(pci_bus, 0x0017);
0aa6a4a2 452
819385c5 453 nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE, 59);
0aa6a4a2
FB
454
455 arch_name = "HEATHROW";
456 } else {
457 isa_mem_base = 0x80000000;
0aa6a4a2
FB
458
459 /* Register 8 MB of ISA IO space */
aef445bd 460 isa_mmio_init(0xf2000000, 0x00800000);
0aa6a4a2
FB
461
462 /* UniN init */
463 unin_memory = cpu_register_io_memory(0, unin_read, unin_write, NULL);
464 cpu_register_physical_memory(0xf8000000, 0x00001000, unin_memory);
465
502a5395
PB
466 pic = openpic_init(NULL, &openpic_mem_index, 1, &env);
467 set_irq = openpic_set_irq;
468 pci_bus = pci_pmac_init(pic);
0aa6a4a2 469 /* init basic PC hardware */
89b6b508
FB
470 pci_vga_init(pci_bus, ds, phys_ram_base + ram_size,
471 ram_size, vga_ram_size,
472 vga_bios_offset, vga_bios_size);
0aa6a4a2
FB
473
474 /* XXX: suppress that */
fa296b0f 475 isa_pic = pic_init(pic_irq_request, NULL);
0aa6a4a2
FB
476
477 /* XXX: use Mac Serial port */
e5d13e2f 478 serial_init(&pic_set_irq_new, isa_pic, 0x3f8, 4, serial_hds[0]);
0aa6a4a2
FB
479
480 for(i = 0; i < nb_nics; i++) {
abcebc7e 481 pci_ne2000_init(pci_bus, &nd_table[i], -1);
0aa6a4a2
FB
482 }
483
484#if 1
485 ide0_mem_index = pmac_ide_init(&bs_table[0], set_irq, pic, 0x13);
486 ide1_mem_index = pmac_ide_init(&bs_table[2], set_irq, pic, 0x14);
487#else
488 pci_cmd646_ide_init(pci_bus, &bs_table[0], 0);
489#endif
490 /* cuda also initialize ADB */
491 cuda_mem_index = cuda_init(set_irq, pic, 0x19);
492
493 adb_kbd_init(&adb_bus);
494 adb_mouse_init(&adb_bus);
495
e5733356 496 macio_init(pci_bus, 0x0022);
0aa6a4a2 497
819385c5 498 nvram = m48t59_init(8, 0xFFF04000, 0x0074, NVRAM_SIZE, 59);
0aa6a4a2
FB
499
500 arch_name = "MAC99";
64201201 501 }
0d92ed30
PB
502
503 if (usb_enabled) {
504 usb_ohci_init(pci_bus, 3, -1);
505 }
506
b6b8bd18
FB
507 if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
508 graphic_depth = 15;
64201201 509
0aa6a4a2 510 PPC_NVRAM_set_params(nvram, NVRAM_SIZE, arch_name, ram_size, boot_device,
b6b8bd18
FB
511 kernel_base, kernel_size,
512 kernel_cmdline,
513 initrd_base, initrd_size,
64201201 514 /* XXX: need an option to load a NVRAM image */
b6b8bd18
FB
515 0,
516 graphic_width, graphic_height, graphic_depth);
517 /* No PCI init: the BIOS will do it */
0aa6a4a2
FB
518
519 /* Special port to get debug messages from Open-Firmware */
520 register_ioport_write(0x0F00, 4, 1, &PPC_debug_write, NULL);
521}
522
94fc95cd
JM
523static void ppc_core99_init (int ram_size, int vga_ram_size, int boot_device,
524 DisplayState *ds, const char **fd_filename,
525 int snapshot,
526 const char *kernel_filename,
527 const char *kernel_cmdline,
528 const char *initrd_filename,
529 const char *cpu_model)
0aa6a4a2
FB
530{
531 ppc_chrp_init(ram_size, vga_ram_size, boot_device,
532 ds, fd_filename, snapshot,
533 kernel_filename, kernel_cmdline,
94fc95cd 534 initrd_filename, cpu_model, 0);
64201201 535}
0aa6a4a2 536
94fc95cd
JM
537static void ppc_heathrow_init (int ram_size, int vga_ram_size, int boot_device,
538 DisplayState *ds, const char **fd_filename,
539 int snapshot,
540 const char *kernel_filename,
541 const char *kernel_cmdline,
542 const char *initrd_filename,
543 const char *cpu_model)
0aa6a4a2
FB
544{
545 ppc_chrp_init(ram_size, vga_ram_size, boot_device,
546 ds, fd_filename, snapshot,
547 kernel_filename, kernel_cmdline,
94fc95cd 548 initrd_filename, cpu_model, 1);
0aa6a4a2
FB
549}
550
551QEMUMachine core99_machine = {
0289b2c1
FB
552 "mac99",
553 "Mac99 based PowerMAC",
0aa6a4a2
FB
554 ppc_core99_init,
555};
556
557QEMUMachine heathrow_machine = {
0289b2c1 558 "g3bw",
0aa6a4a2
FB
559 "Heathrow based PowerMAC",
560 ppc_heathrow_init,
561};