mac_oldworld.c 12.9 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
Peter Maydell 已提交
26
#include "qemu/osdep.h"
27
#include "hw/hw.h"
P
Paolo Bonzini 已提交
28
#include "hw/ppc/ppc.h"
29
#include "mac.h"
P
Paolo Bonzini 已提交
30 31
#include "hw/input/adb.h"
#include "hw/timer/m48t59.h"
32
#include "sysemu/sysemu.h"
P
Paolo Bonzini 已提交
33
#include "net/net.h"
P
Paolo Bonzini 已提交
34
#include "hw/isa/isa.h"
35 36
#include "hw/pci/pci.h"
#include "hw/boards.h"
P
Paolo Bonzini 已提交
37 38
#include "hw/nvram/fw_cfg.h"
#include "hw/char/escc.h"
39 40
#include "hw/ide.h"
#include "hw/loader.h"
B
Blue Swirl 已提交
41
#include "elf.h"
42
#include "qemu/error-report.h"
43
#include "sysemu/kvm.h"
44
#include "kvm_ppc.h"
45
#include "sysemu/block-backend.h"
46
#include "exec/address-spaces.h"
J
j_mayer 已提交
47

T
ths 已提交
48
#define MAX_IDE_BUS 2
B
blueswir1 已提交
49
#define CFG_ADDR 0xf0000510
50
#define TBFREQ 16600000UL
51 52
#define CLOCKFREQ 266000000UL
#define BUSFREQ 66000000UL
B
blueswir1 已提交
53

54 55
static void fw_cfg_boot_set(void *opaque, const char *boot_device,
                            Error **errp)
56
{
57
    fw_cfg_modify_i16(opaque, FW_CFG_BOOT_DEVICE, boot_device[0]);
58 59
}

60 61 62 63 64
static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
{
    return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
}

A
Avi Kivity 已提交
65
static hwaddr round_page(hwaddr addr)
66 67 68 69
{
    return (addr + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
}

70 71
static void ppc_heathrow_reset(void *opaque)
{
72
    PowerPCCPU *cpu = opaque;
73

74
    cpu_reset(CPU(cpu));
75 76
}

77
static void ppc_heathrow_init(MachineState *machine)
J
j_mayer 已提交
78
{
79 80 81 82 83
    ram_addr_t ram_size = machine->ram_size;
    const char *kernel_filename = machine->kernel_filename;
    const char *kernel_cmdline = machine->kernel_cmdline;
    const char *initrd_filename = machine->initrd_filename;
    const char *boot_device = machine->boot_order;
A
Avi Kivity 已提交
84
    MemoryRegion *sysmem = get_system_memory();
85
    PowerPCCPU *cpu = NULL;
A
Andreas Färber 已提交
86
    CPUPPCState *env = NULL;
P
Paul Brook 已提交
87
    char *filename;
J
j_mayer 已提交
88 89
    qemu_irq *pic, **heathrow_irqs;
    int linux_boot, i;
A
Avi Kivity 已提交
90 91
    MemoryRegion *ram = g_new(MemoryRegion, 1);
    MemoryRegion *bios = g_new(MemoryRegion, 1);
92
    MemoryRegion *isa = g_new(MemoryRegion, 1);
93
    uint32_t kernel_base, initrd_base, cmdline_base = 0;
94
    int32_t kernel_size, initrd_size;
J
j_mayer 已提交
95
    PCIBus *pci_bus;
A
Andreas Färber 已提交
96
    PCIDevice *macio;
A
Andreas Färber 已提交
97 98
    MACIOIDEState *macio_ide;
    DeviceState *dev;
99
    BusState *adb_bus;
B
Blue Swirl 已提交
100
    int bios_size;
A
Andreas Färber 已提交
101
    MemoryRegion *pic_mem;
A
Andreas Färber 已提交
102
    MemoryRegion *escc_mem, *escc_bar = g_new(MemoryRegion, 1);
103
    uint16_t ppc_boot_device;
104
    DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
B
blueswir1 已提交
105
    void *fw_cfg;
106
    uint64_t tbfreq;
J
j_mayer 已提交
107 108 109 110

    linux_boot = (kernel_filename != NULL);

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

121
        /* Set time-base frequency to 16.6 Mhz */
122
        cpu_ppc_tb_init(env,  TBFREQ);
123
        qemu_register_reset(ppc_heathrow_reset, cpu);
J
j_mayer 已提交
124 125 126
    }

    /* allocate RAM */
127 128 129 130 131 132 133
    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);
    }

134 135
    memory_region_allocate_system_memory(ram, NULL, "ppc_heathrow.ram",
                                         ram_size);
A
Avi Kivity 已提交
136
    memory_region_add_subregion(sysmem, 0, ram);
137

J
j_mayer 已提交
138
    /* allocate and load BIOS */
139
    memory_region_init_ram(bios, NULL, "ppc_heathrow.bios", BIOS_SIZE,
140
                           &error_fatal);
H
Hu Tao 已提交
141 142
    vmstate_register_ram_global(bios);

J
j_mayer 已提交
143
    if (bios_name == NULL)
B
blueswir1 已提交
144
        bios_name = PROM_FILENAME;
P
Paul Brook 已提交
145
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
A
Avi Kivity 已提交
146 147
    memory_region_set_readonly(bios, true);
    memory_region_add_subregion(sysmem, PROM_ADDR, bios);
B
blueswir1 已提交
148 149

    /* Load OpenBIOS (ELF) */
P
Paul Brook 已提交
150
    if (filename) {
151
        bios_size = load_elf(filename, 0, NULL, NULL, NULL, NULL,
152
                             1, PPC_ELF_MACHINE, 0, 0);
153
        g_free(filename);
P
Paul Brook 已提交
154 155 156
    } else {
        bios_size = -1;
    }
J
j_mayer 已提交
157
    if (bios_size < 0 || bios_size > BIOS_SIZE) {
158
        error_report("could not load PowerPC bios '%s'", bios_name);
J
j_mayer 已提交
159 160 161 162
        exit(1);
    }

    if (linux_boot) {
163
        uint64_t lowaddr = 0;
B
Blue Swirl 已提交
164 165 166 167 168 169 170
        int bswap_needed;

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

    /* Register 2 MB of ISA IO space */
234 235 236
    memory_region_init_alias(isa, NULL, "isa_mmio",
                             get_system_io(), 0, 0x00200000);
    memory_region_add_subregion(sysmem, 0xfe000000, isa);
J
j_mayer 已提交
237 238

    /* XXX: we register only 1 output pin for heathrow PIC */
239
    heathrow_irqs = g_malloc0(smp_cpus * sizeof(qemu_irq *));
J
j_mayer 已提交
240
    heathrow_irqs[0] =
241
        g_malloc0(smp_cpus * sizeof(qemu_irq) * 1);
J
j_mayer 已提交
242 243 244 245 246 247 248 249 250
    /* 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:
251 252
            error_report("Bus model not supported on OldWorld Mac machine");
            exit(1);
J
j_mayer 已提交
253 254 255
        }
    }

256 257 258 259 260 261 262
    /* Timebase Frequency */
    if (kvm_enabled()) {
        tbfreq = kvmppc_get_tbfreq();
    } else {
        tbfreq = TBFREQ;
    }

J
j_mayer 已提交
263 264
    /* init basic PC hardware */
    if (PPC_INPUT(env) != PPC_FLAGS_INPUT_6xx) {
265 266
        error_report("Only 6xx bus is supported on heathrow machine");
        exit(1);
J
j_mayer 已提交
267
    }
A
Avi Kivity 已提交
268
    pic = heathrow_pic_init(&pic_mem, 1, heathrow_irqs);
269 270 271
    pci_bus = pci_grackle_init(0xfec00000, pic,
                               get_system_memory(),
                               get_system_io());
272
    pci_vga_init(pci_bus);
273

A
Alexander Graf 已提交
274
    escc_mem = escc_init(0, pic[0x0f], pic[0x10], serial_hds[0],
B
blueswir1 已提交
275
                               serial_hds[1], ESCC_CLOCK, 4);
276
    memory_region_init_alias(escc_bar, NULL, "escc-bar",
277
                             escc_mem, 0, memory_region_size(escc_mem));
278

279
    for(i = 0; i < nb_nics; i++)
280
        pci_nic_init_nofail(&nd_table[i], pci_bus, "ne2k_pci", NULL);
J
j_mayer 已提交
281

T
ths 已提交
282

283
    ide_drive_get(hd, ARRAY_SIZE(hd));
284

A
Andreas Färber 已提交
285
    macio = pci_create(pci_bus, -1, TYPE_OLDWORLD_MACIO);
A
Andreas Färber 已提交
286
    dev = DEVICE(macio);
A
Andreas Färber 已提交
287
    qdev_connect_gpio_out(dev, 0, pic[0x12]); /* CUDA */
288 289 290 291
    qdev_connect_gpio_out(dev, 1, pic[0x0D]); /* IDE-0 */
    qdev_connect_gpio_out(dev, 2, pic[0x02]); /* IDE-0 DMA */
    qdev_connect_gpio_out(dev, 3, pic[0x0E]); /* IDE-1 */
    qdev_connect_gpio_out(dev, 4, pic[0x03]); /* IDE-1 DMA */
292
    qdev_prop_set_uint64(dev, "frequency", tbfreq);
A
Andreas Färber 已提交
293
    macio_init(macio, pic_mem, escc_bar);
A
Andreas Färber 已提交
294 295

    macio_ide = MACIO_IDE(object_resolve_path_component(OBJECT(macio),
296
                                                        "ide[0]"));
A
Andreas Färber 已提交
297 298
    macio_ide_init_drives(macio_ide, hd);

299 300 301
    macio_ide = MACIO_IDE(object_resolve_path_component(OBJECT(macio),
                                                        "ide[1]"));
    macio_ide_init_drives(macio_ide, &hd[MAX_IDE_DEVS]);
J
j_mayer 已提交
302

303 304 305
    dev = DEVICE(object_resolve_path_component(OBJECT(macio), "cuda"));
    adb_bus = qdev_get_child_bus(dev, "adb.0");
    dev = qdev_create(adb_bus, TYPE_ADB_KEYBOARD);
A
Andreas Färber 已提交
306
    qdev_init_nofail(dev);
307
    dev = qdev_create(adb_bus, TYPE_ADB_MOUSE);
A
Andreas Färber 已提交
308
    qdev_init_nofail(dev);
A
Andreas Färber 已提交
309

310
    if (usb_enabled()) {
311
        pci_create_simple(pci_bus, -1, "pci-ohci");
J
j_mayer 已提交
312 313 314 315 316 317 318
    }

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

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

319
    fw_cfg = fw_cfg_init_mem(CFG_ADDR, CFG_ADDR + 2);
320
    fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, (uint16_t)max_cpus);
B
blueswir1 已提交
321 322
    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);
323 324 325
    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) {
326 327
        fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_CMDLINE, cmdline_base);
        pstrcpy_targphys("cmdline", cmdline_base, TARGET_PAGE_SIZE, kernel_cmdline);
328 329 330 331 332 333
    } 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);
334 335 336 337 338

    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);

339
    fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_IS_KVM, kvm_enabled());
340 341
    if (kvm_enabled()) {
#ifdef CONFIG_KVM
342 343
        uint8_t *hypercall;

344
        hypercall = g_malloc(16);
345 346 347
        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());
348 349
#endif
    }
350
    fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_TBFREQ, tbfreq);
351
    /* Mac OS X requires a "known good" clock-frequency value; pass it one. */
352 353
    fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_CLOCKFREQ, CLOCKFREQ);
    fw_cfg_add_i32(fw_cfg, FW_CFG_PPC_BUSFREQ, BUSFREQ);
354

355
    qemu_register_boot_set(fw_cfg_boot_set, fw_cfg);
J
j_mayer 已提交
356 357
}

358 359 360 361 362 363
static int heathrow_kvm_type(const char *arg)
{
    /* Always force PR KVM */
    return 2;
}

364 365 366 367 368
static void heathrow_machine_init(MachineClass *mc)
{
    mc->desc = "Heathrow based PowerMAC";
    mc->init = ppc_heathrow_init;
    mc->max_cpus = MAX_CPUS;
369
#ifndef TARGET_PPC64
370
    mc->is_default = 1;
371
#endif
E
Eduardo Habkost 已提交
372
    /* TOFIX "cad" when Mac floppy is implemented */
373 374
    mc->default_boot_order = "cd";
    mc->kvm_type = heathrow_kvm_type;
375 376
}

377
DEFINE_MACHINE("g3beige", heathrow_machine_init)