ppc_oldworld.c 11.6 KB
Newer Older
B
Blue Swirl 已提交
1

J
j_mayer 已提交
2
/*
3
 * QEMU OldWorld PowerMac (currently ~G3 Beige) hardware System Emulator
J
j_mayer 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Copyright (c) 2004-2007 Fabrice Bellard
 * Copyright (c) 2007 Jocelyn Mayer
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
P
pbrook 已提交
26 27
#include "hw.h"
#include "ppc.h"
J
j_mayer 已提交
28
#include "ppc_mac.h"
29
#include "adb.h"
A
aurel32 已提交
30
#include "mac_dbdma.h"
P
pbrook 已提交
31 32 33 34 35 36 37
#include "nvram.h"
#include "pc.h"
#include "sysemu.h"
#include "net.h"
#include "isa.h"
#include "pci.h"
#include "boards.h"
B
blueswir1 已提交
38
#include "fw_cfg.h"
B
blueswir1 已提交
39
#include "escc.h"
G
Gerd Hoffmann 已提交
40
#include "ide.h"
B
Blue Swirl 已提交
41 42
#include "loader.h"
#include "elf.h"
A
Alexander Graf 已提交
43
#include "kvm.h"
44
#include "kvm_ppc.h"
B
Blue Swirl 已提交
45
#include "blockdev.h"
46
#include "exec-memory.h"
J
j_mayer 已提交
47

T
ths 已提交
48
#define MAX_IDE_BUS 2
B
blueswir1 已提交
49 50
#define CFG_ADDR 0xf0000510

51 52 53 54 55 56
static int fw_cfg_boot_set(void *opaque, const char *boot_device)
{
    fw_cfg_add_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
    return 0;
}

57 58 59 60 61 62

static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
{
    return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
}

63 64 65 66 67
static target_phys_addr_t round_page(target_phys_addr_t addr)
{
    return (addr + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
}

68 69
static void ppc_heathrow_reset(void *opaque)
{
A
Andreas Färber 已提交
70
    CPUPPCState *env = opaque;
71 72 73 74

    cpu_state_reset(env);
}

A
Anthony Liguori 已提交
75
static void ppc_heathrow_init (ram_addr_t ram_size,
76
                               const char *boot_device,
J
j_mayer 已提交
77 78 79 80 81
                               const char *kernel_filename,
                               const char *kernel_cmdline,
                               const char *initrd_filename,
                               const char *cpu_model)
{
A
Avi Kivity 已提交
82
    MemoryRegion *sysmem = get_system_memory();
83
    PowerPCCPU *cpu = NULL;
A
Andreas Färber 已提交
84
    CPUPPCState *env = NULL;
P
Paul Brook 已提交
85
    char *filename;
J
j_mayer 已提交
86 87
    qemu_irq *pic, **heathrow_irqs;
    int linux_boot, i;
A
Avi Kivity 已提交
88 89
    MemoryRegion *ram = g_new(MemoryRegion, 1);
    MemoryRegion *bios = g_new(MemoryRegion, 1);
90
    uint32_t kernel_base, initrd_base, cmdline_base = 0;
91
    int32_t kernel_size, initrd_size;
J
j_mayer 已提交
92 93
    PCIBus *pci_bus;
    MacIONVRAMState *nvr;
B
Blue Swirl 已提交
94
    int bios_size;
A
Avi Kivity 已提交
95
    MemoryRegion *pic_mem, *dbdma_mem, *cuda_mem;
96
    MemoryRegion *escc_mem, *escc_bar = g_new(MemoryRegion, 1), *ide_mem[2];
97
    uint16_t ppc_boot_device;
98
    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
B
blueswir1 已提交
99
    void *fw_cfg;
A
aurel32 已提交
100
    void *dbdma;
J
j_mayer 已提交
101 102 103 104 105

    linux_boot = (kernel_filename != NULL);

    /* init CPUs */
    if (cpu_model == NULL)
106
        cpu_model = "G3";
J
j_mayer 已提交
107
    for (i = 0; i < smp_cpus; i++) {
108 109
        cpu = cpu_ppc_init(cpu_model);
        if (cpu == NULL) {
B
bellard 已提交
110 111 112
            fprintf(stderr, "Unable to find PowerPC CPU definition\n");
            exit(1);
        }
113 114
        env = &cpu->env;

115 116
        /* Set time-base frequency to 16.6 Mhz */
        cpu_ppc_tb_init(env,  16600000UL);
117
        qemu_register_reset(ppc_heathrow_reset, env);
J
j_mayer 已提交
118 119 120
    }

    /* allocate RAM */
121 122 123 124 125 126 127
    if (ram_size > (2047 << 20)) {
        fprintf(stderr,
                "qemu: Too much memory for this machine: %d MB, maximum 2047 MB\n",
                ((unsigned int)ram_size / (1 << 20)));
        exit(1);
    }

128 129
    memory_region_init_ram(ram, "ppc_heathrow.ram", ram_size);
    vmstate_register_ram_global(ram);
A
Avi Kivity 已提交
130
    memory_region_add_subregion(sysmem, 0, ram);
131

J
j_mayer 已提交
132
    /* allocate and load BIOS */
133 134
    memory_region_init_ram(bios, "ppc_heathrow.bios", BIOS_SIZE);
    vmstate_register_ram_global(bios);
J
j_mayer 已提交
135
    if (bios_name == NULL)
B
blueswir1 已提交
136
        bios_name = PROM_FILENAME;
P
Paul Brook 已提交
137
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
A
Avi Kivity 已提交
138 139
    memory_region_set_readonly(bios, true);
    memory_region_add_subregion(sysmem, PROM_ADDR, bios);
B
blueswir1 已提交
140 141

    /* Load OpenBIOS (ELF) */
P
Paul Brook 已提交
142
    if (filename) {
143 144
        bios_size = load_elf(filename, 0, NULL, NULL, NULL, NULL,
                             1, ELF_MACHINE, 0);
145
        g_free(filename);
P
Paul Brook 已提交
146 147 148
    } else {
        bios_size = -1;
    }
J
j_mayer 已提交
149
    if (bios_size < 0 || bios_size > BIOS_SIZE) {
P
Paul Brook 已提交
150
        hw_error("qemu: could not load PowerPC bios '%s'\n", bios_name);
J
j_mayer 已提交
151 152 153 154
        exit(1);
    }

    if (linux_boot) {
155
        uint64_t lowaddr = 0;
B
Blue Swirl 已提交
156 157 158 159 160 161 162
        int bswap_needed;

#ifdef BSWAP_NEEDED
        bswap_needed = 1;
#else
        bswap_needed = 0;
#endif
J
j_mayer 已提交
163
        kernel_base = KERNEL_LOAD_ADDR;
164 165
        kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
                               NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0);
B
blueswir1 已提交
166 167
        if (kernel_size < 0)
            kernel_size = load_aout(kernel_filename, kernel_base,
B
Blue Swirl 已提交
168 169
                                    ram_size - kernel_base, bswap_needed,
                                    TARGET_PAGE_SIZE);
B
blueswir1 已提交
170 171 172 173
        if (kernel_size < 0)
            kernel_size = load_image_targphys(kernel_filename,
                                              kernel_base,
                                              ram_size - kernel_base);
J
j_mayer 已提交
174
        if (kernel_size < 0) {
P
Paul Brook 已提交
175
            hw_error("qemu: could not load kernel '%s'\n",
J
j_mayer 已提交
176 177 178 179 180
                      kernel_filename);
            exit(1);
        }
        /* load initrd */
        if (initrd_filename) {
181
            initrd_base = round_page(kernel_base + kernel_size + KERNEL_GAP);
182 183
            initrd_size = load_image_targphys(initrd_filename, initrd_base,
                                              ram_size - initrd_base);
J
j_mayer 已提交
184
            if (initrd_size < 0) {
P
Paul Brook 已提交
185 186
                hw_error("qemu: could not load initial ram disk '%s'\n",
                         initrd_filename);
J
j_mayer 已提交
187 188
                exit(1);
            }
189
            cmdline_base = round_page(initrd_base + initrd_size);
J
j_mayer 已提交
190 191 192
        } else {
            initrd_base = 0;
            initrd_size = 0;
193
            cmdline_base = round_page(kernel_base + kernel_size + KERNEL_GAP);
J
j_mayer 已提交
194
        }
195
        ppc_boot_device = 'm';
J
j_mayer 已提交
196 197 198 199 200
    } else {
        kernel_base = 0;
        kernel_size = 0;
        initrd_base = 0;
        initrd_size = 0;
201
        ppc_boot_device = '\0';
J
j_mayer 已提交
202
        for (i = 0; boot_device[i] != '\0'; i++) {
203
            /* TOFIX: for now, the second IDE channel is not properly
J
j_mayer 已提交
204
             *        used by OHW. The Mac floppy disk are not emulated.
205 206 207
             *        For now, OHW cannot boot from the network.
             */
#if 0
J
j_mayer 已提交
208 209
            if (boot_device[i] >= 'a' && boot_device[i] <= 'f') {
                ppc_boot_device = boot_device[i];
210
                break;
J
j_mayer 已提交
211
            }
212
#else
J
j_mayer 已提交
213 214
            if (boot_device[i] >= 'c' && boot_device[i] <= 'd') {
                ppc_boot_device = boot_device[i];
215
                break;
J
j_mayer 已提交
216
            }
217 218 219
#endif
        }
        if (ppc_boot_device == '\0') {
220
            fprintf(stderr, "No valid boot device for G3 Beige machine\n");
221 222
            exit(1);
        }
J
j_mayer 已提交
223 224 225
    }

    /* Register 2 MB of ISA IO space */
226
    isa_mmio_init(0xfe000000, 0x00200000);
J
j_mayer 已提交
227 228

    /* XXX: we register only 1 output pin for heathrow PIC */
229
    heathrow_irqs = g_malloc0(smp_cpus * sizeof(qemu_irq *));
J
j_mayer 已提交
230
    heathrow_irqs[0] =
231
        g_malloc0(smp_cpus * sizeof(qemu_irq) * 1);
J
j_mayer 已提交
232 233 234 235 236 237 238 239 240
    /* Connect the heathrow PIC outputs to the 6xx bus */
    for (i = 0; i < smp_cpus; i++) {
        switch (PPC_INPUT(env)) {
        case PPC_FLAGS_INPUT_6xx:
            heathrow_irqs[i] = heathrow_irqs[0] + (i * 1);
            heathrow_irqs[i][0] =
                ((qemu_irq *)env->irq_inputs)[PPC6xx_INPUT_INT];
            break;
        default:
P
Paul Brook 已提交
241
            hw_error("Bus model not supported on OldWorld Mac machine\n");
J
j_mayer 已提交
242 243 244 245 246
        }
    }

    /* init basic PC hardware */
    if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
P
Paul Brook 已提交
247
        hw_error("Only 6xx bus is supported on heathrow machine\n");
J
j_mayer 已提交
248
    }
A
Avi Kivity 已提交
249
    pic = heathrow_pic_init(&pic_mem, 1, heathrow_irqs);
250 251 252
    pci_bus = pci_grackle_init(0xfec00000, pic,
                               get_system_memory(),
                               get_system_io());
G
Gerd Hoffmann 已提交
253
    pci_vga_init(pci_bus);
254

A
Alexander Graf 已提交
255
    escc_mem = escc_init(0, pic[0x0f], pic[0x10], serial_hds[0],
B
blueswir1 已提交
256
                               serial_hds[1], ESCC_CLOCK, 4);
257 258
    memory_region_init_alias(escc_bar, "escc-bar",
                             escc_mem, 0, memory_region_size(escc_mem));
259

260
    for(i = 0; i < nb_nics; i++)
261
        pci_nic_init_nofail(&nd_table[i], "ne2k_pci", NULL);
J
j_mayer 已提交
262

T
ths 已提交
263

264
    ide_drive_get(hd, MAX_IDE_BUS);
265 266

    /* First IDE channel is a MAC IDE on the MacIO bus */
A
Avi Kivity 已提交
267 268 269
    dbdma = DBDMA_init(&dbdma_mem);
    ide_mem[0] = NULL;
    ide_mem[1] = pmac_ide_init(hd, pic[0x0D], dbdma, 0x16, pic[0x02]);
T
ths 已提交
270

271
    /* Second IDE channel is a CMD646 on the PCI bus */
272 273
    hd[0] = hd[MAX_IDE_DEVS];
    hd[1] = hd[MAX_IDE_DEVS + 1];
274 275
    hd[3] = hd[2] = NULL;
    pci_cmd646_ide_init(pci_bus, hd, 0);
J
j_mayer 已提交
276 277

    /* cuda also initialize ADB */
A
Avi Kivity 已提交
278
    cuda_init(&cuda_mem, pic[0x12]);
J
j_mayer 已提交
279 280 281

    adb_kbd_init(&adb_bus);
    adb_mouse_init(&adb_bus);
282

A
Avi Kivity 已提交
283
    nvr = macio_nvram_init(0x2000, 4);
J
j_mayer 已提交
284 285
    pmac_format_nvram_partition(nvr, 0x2000);

A
Avi Kivity 已提交
286
    macio_init(pci_bus, PCI_DEVICE_ID_APPLE_343S1201, 1, pic_mem,
287
               dbdma_mem, cuda_mem, nvr, 2, ide_mem, escc_bar);
J
j_mayer 已提交
288 289

    if (usb_enabled) {
290
        pci_create_simple(pci_bus, -1, "pci-ohci");
J
j_mayer 已提交
291 292 293 294 295 296 297
    }

    if (graphic_depth != 15 && graphic_depth != 32 && graphic_depth != 8)
        graphic_depth = 15;

    /* No PCI init: the BIOS will do it */

B
blueswir1 已提交
298 299 300 301
    fw_cfg = fw_cfg_init(0, 0, CFG_ADDR, CFG_ADDR + 2);
    fw_cfg_add_i32(fw_cfg, FW_CFG_ID, 1);
    fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)ram_size);
    fw_cfg_add_i16(fw_cfg, FW_CFG_MACHINE_ID, ARCH_HEATHROW);
302 303 304
    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, kernel_base);
    fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
    if (kernel_cmdline) {
305 306
        fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, cmdline_base);
        pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE, kernel_cmdline);
307 308 309 310 311 312
    } else {
        fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, 0);
    }
    fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_base);
    fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
    fw_cfg_add_i16(fw_cfg, FW_CFG_BOOT_DEVICE, ppc_boot_device);
313 314 315 316 317

    fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_WIDTH, graphic_width);
    fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_HEIGHT, graphic_height);
    fw_cfg_add_i16(fw_cfg, FW_CFG_PPC_DEPTH, graphic_depth);

318
    fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_IS_KVM, kvm_enabled());
319 320
    if (kvm_enabled()) {
#ifdef CONFIG_KVM
321 322
        uint8_t *hypercall;

323
        fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_TBFREQ, kvmppc_get_tbfreq());
324
        hypercall = g_malloc(16);
325 326 327
        kvmppc_get_hypercall(env, hypercall, 16);
        fw_cfg_add_bytes(fw_cfg, FW_CFG_PPC_KVM_HC, hypercall, 16);
        fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_KVM_PID, getpid());
328 329 330 331 332
#endif
    } else {
        fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_TBFREQ, get_ticks_per_sec());
    }

333
    qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
J
j_mayer 已提交
334 335
}

336
static QEMUMachine heathrow_machine = {
337
    .name = "g3beige",
338 339
    .desc = "Heathrow based PowerMAC",
    .init = ppc_heathrow_init,
B
balrog 已提交
340
    .max_cpus = MAX_CPUS,
341
#ifndef TARGET_PPC64
342
    .is_default = 1,
343
#endif
J
j_mayer 已提交
344
};
345 346 347 348 349 350 351

static void heathrow_machine_init(void)
{
    qemu_register_machine(&heathrow_machine);
}

machine_init(heathrow_machine_init);