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