Statistics
| Branch: | Revision:

root / hw / ppce500_mpc8544ds.c @ 66ae7902

History | View | Annotate | Download (10.7 kB)

1
/*
2
 * Qemu PowerPC MPC8544DS board emualtion
3
 *
4
 * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
5
 *
6
 * Author: Yu Liu,     <yu.liu@freescale.com>
7
 *
8
 * This file is derived from hw/ppc440_bamboo.c,
9
 * the copyright for that material belongs to the original owners.
10
 *
11
 * This is free software; you can redistribute it and/or modify
12
 * it under the terms of  the GNU General  Public License as published by
13
 * the Free Software Foundation;  either version 2 of the  License, or
14
 * (at your option) any later version.
15
 */
16

    
17
#include "config.h"
18
#include "qemu-common.h"
19
#include "net.h"
20
#include "hw.h"
21
#include "pc.h"
22
#include "pci.h"
23
#include "boards.h"
24
#include "sysemu.h"
25
#include "kvm.h"
26
#include "kvm_ppc.h"
27
#include "device_tree.h"
28
#include "openpic.h"
29
#include "ppc.h"
30
#include "loader.h"
31
#include "elf.h"
32
#include "sysbus.h"
33

    
34
#define BINARY_DEVICE_TREE_FILE    "mpc8544ds.dtb"
35
#define UIMAGE_LOAD_BASE           0
36
#define DTC_LOAD_PAD               0x500000
37
#define DTC_PAD_MASK               0xFFFFF
38
#define INITRD_LOAD_PAD            0x2000000
39
#define INITRD_PAD_MASK            0xFFFFFF
40

    
41
#define RAM_SIZES_ALIGN            (64UL << 20)
42

    
43
#define MPC8544_CCSRBAR_BASE       0xE0000000
44
#define MPC8544_MPIC_REGS_BASE     (MPC8544_CCSRBAR_BASE + 0x40000)
45
#define MPC8544_SERIAL0_REGS_BASE  (MPC8544_CCSRBAR_BASE + 0x4500)
46
#define MPC8544_SERIAL1_REGS_BASE  (MPC8544_CCSRBAR_BASE + 0x4600)
47
#define MPC8544_PCI_REGS_BASE      (MPC8544_CCSRBAR_BASE + 0x8000)
48
#define MPC8544_PCI_REGS_SIZE      0x1000
49
#define MPC8544_PCI_IO             0xE1000000
50
#define MPC8544_PCI_IOLEN          0x10000
51
#define MPC8544_UTIL_BASE          (MPC8544_CCSRBAR_BASE + 0xe0000)
52

    
53
struct boot_info
54
{
55
    uint32_t dt_base;
56
    uint32_t entry;
57
};
58

    
59
static int mpc8544_load_device_tree(CPUState *env,
60
                                    target_phys_addr_t addr,
61
                                    uint32_t ramsize,
62
                                    target_phys_addr_t initrd_base,
63
                                    target_phys_addr_t initrd_size,
64
                                    const char *kernel_cmdline)
65
{
66
    int ret = -1;
67
#ifdef CONFIG_FDT
68
    uint32_t mem_reg_property[] = {0, cpu_to_be32(ramsize)};
69
    char *filename;
70
    int fdt_size;
71
    void *fdt;
72
    uint8_t hypercall[16];
73
    char cpu_name[128] = "/cpus/PowerPC,8544@0";
74
    uint32_t clock_freq = 400000000;
75
    uint32_t tb_freq = 400000000;
76

    
77
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
78
    if (!filename) {
79
        goto out;
80
    }
81
    fdt = load_device_tree(filename, &fdt_size);
82
    g_free(filename);
83
    if (fdt == NULL) {
84
        goto out;
85
    }
86

    
87
    /* Manipulate device tree in memory. */
88
    ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
89
                               sizeof(mem_reg_property));
90
    if (ret < 0)
91
        fprintf(stderr, "couldn't set /memory/reg\n");
92

    
93
    if (initrd_size) {
94
        ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
95
                                        initrd_base);
96
        if (ret < 0) {
97
            fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
98
        }
99

    
100
        ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
101
                                        (initrd_base + initrd_size));
102
        if (ret < 0) {
103
            fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
104
        }
105
    }
106

    
107
    ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
108
                                      kernel_cmdline);
109
    if (ret < 0)
110
        fprintf(stderr, "couldn't set /chosen/bootargs\n");
111

    
112
    if (kvm_enabled()) {
113
        /* Read out host's frequencies */
114
        clock_freq = kvmppc_get_clockfreq();
115
        tb_freq = kvmppc_get_tbfreq();
116

    
117
        /* indicate KVM hypercall interface */
118
        qemu_devtree_setprop_string(fdt, "/hypervisor", "compatible",
119
                                    "linux,kvm");
120
        kvmppc_get_hypercall(env, hypercall, sizeof(hypercall));
121
        qemu_devtree_setprop(fdt, "/hypervisor", "hcall-instructions",
122
                             hypercall, sizeof(hypercall));
123
    }
124

    
125
    qemu_devtree_setprop_cell(fdt, cpu_name, "clock-frequency", clock_freq);
126
    qemu_devtree_setprop_cell(fdt, cpu_name, "timebase-frequency", tb_freq);
127

    
128
    ret = rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
129
    g_free(fdt);
130

    
131
out:
132
#endif
133

    
134
    return ret;
135
}
136

    
137
/* Create -kernel TLB entries for BookE, linearly spanning 256MB.  */
138
static inline target_phys_addr_t booke206_page_size_to_tlb(uint64_t size)
139
{
140
    return (ffs(size >> 10) - 1) >> 1;
141
}
142

    
143
static void mmubooke_create_initial_mapping(CPUState *env,
144
                                     target_ulong va,
145
                                     target_phys_addr_t pa)
146
{
147
    ppcmas_tlb_t *tlb = booke206_get_tlbm(env, 1, 0, 0);
148
    target_phys_addr_t size;
149

    
150
    size = (booke206_page_size_to_tlb(256 * 1024 * 1024) << MAS1_TSIZE_SHIFT);
151
    tlb->mas1 = MAS1_VALID | size;
152
    tlb->mas2 = va & TARGET_PAGE_MASK;
153
    tlb->mas7_3 = pa & TARGET_PAGE_MASK;
154
    tlb->mas7_3 |= MAS3_UR | MAS3_UW | MAS3_UX | MAS3_SR | MAS3_SW | MAS3_SX;
155
}
156

    
157
static void mpc8544ds_cpu_reset(void *opaque)
158
{
159
    CPUState *env = opaque;
160
    struct boot_info *bi = env->load_info;
161

    
162
    cpu_reset(env);
163

    
164
    /* Set initial guest state. */
165
    env->gpr[1] = (16<<20) - 8;
166
    env->gpr[3] = bi->dt_base;
167
    env->nip = bi->entry;
168
    mmubooke_create_initial_mapping(env, 0, 0);
169
}
170

    
171
static void mpc8544ds_init(ram_addr_t ram_size,
172
                         const char *boot_device,
173
                         const char *kernel_filename,
174
                         const char *kernel_cmdline,
175
                         const char *initrd_filename,
176
                         const char *cpu_model)
177
{
178
    PCIBus *pci_bus;
179
    CPUState *env = NULL;
180
    uint64_t elf_entry;
181
    uint64_t elf_lowaddr;
182
    target_phys_addr_t entry=0;
183
    target_phys_addr_t loadaddr=UIMAGE_LOAD_BASE;
184
    target_long kernel_size=0;
185
    target_ulong dt_base = 0;
186
    target_ulong initrd_base = 0;
187
    target_long initrd_size=0;
188
    int i=0;
189
    unsigned int pci_irq_nrs[4] = {1, 2, 3, 4};
190
    qemu_irq **irqs, *mpic;
191
    DeviceState *dev;
192
    struct boot_info *boot_info;
193
    CPUState *firstenv = NULL;
194

    
195
    /* Setup CPUs */
196
    if (cpu_model == NULL) {
197
        cpu_model = "e500v2_v30";
198
    }
199

    
200
    irqs = g_malloc0(smp_cpus * sizeof(qemu_irq *));
201
    irqs[0] = g_malloc0(smp_cpus * sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
202
    for (i = 0; i < smp_cpus; i++) {
203
        qemu_irq *input;
204
        env = cpu_ppc_init(cpu_model);
205
        if (!env) {
206
            fprintf(stderr, "Unable to initialize CPU!\n");
207
            exit(1);
208
        }
209

    
210
        if (!firstenv) {
211
            firstenv = env;
212
        }
213

    
214
        irqs[i] = irqs[0] + (i * OPENPIC_OUTPUT_NB);
215
        input = (qemu_irq *)env->irq_inputs;
216
        irqs[i][OPENPIC_OUTPUT_INT] = input[PPCE500_INPUT_INT];
217
        irqs[i][OPENPIC_OUTPUT_CINT] = input[PPCE500_INPUT_CINT];
218
        env->spr[SPR_BOOKE_PIR] = env->cpu_index = i;
219

    
220
        /* XXX register timer? */
221
        ppc_emb_timers_init(env, 400000000, PPC_INTERRUPT_DECR);
222
        ppc_dcr_init(env, NULL, NULL);
223
        /* XXX Enable DEC interrupts - probably wrong in the backend */
224
        env->spr[SPR_40x_TCR] = 1 << 26;
225

    
226
        /* Register reset handler */
227
        boot_info = g_malloc0(sizeof(struct boot_info));
228
        qemu_register_reset(mpc8544ds_cpu_reset, env);
229
        env->load_info = boot_info;
230
    }
231

    
232
    env = firstenv;
233

    
234
    /* Fixup Memory size on a alignment boundary */
235
    ram_size &= ~(RAM_SIZES_ALIGN - 1);
236

    
237
    /* Register Memory */
238
    cpu_register_physical_memory(0, ram_size, qemu_ram_alloc(NULL,
239
                                 "mpc8544ds.ram", ram_size));
240

    
241
    /* MPIC */
242
    mpic = mpic_init(MPC8544_MPIC_REGS_BASE, smp_cpus, irqs, NULL);
243

    
244
    if (!mpic) {
245
        cpu_abort(env, "MPIC failed to initialize\n");
246
    }
247

    
248
    /* Serial */
249
    if (serial_hds[0]) {
250
        serial_mm_init(MPC8544_SERIAL0_REGS_BASE,
251
                       0, mpic[12+26], 399193,
252
                       serial_hds[0], 1, 1);
253
    }
254

    
255
    if (serial_hds[1]) {
256
        serial_mm_init(MPC8544_SERIAL1_REGS_BASE,
257
                       0, mpic[12+26], 399193,
258
                       serial_hds[0], 1, 1);
259
    }
260

    
261
    /* General Utility device */
262
    sysbus_create_simple("mpc8544-guts", MPC8544_UTIL_BASE, NULL);
263

    
264
    /* PCI */
265
    dev = sysbus_create_varargs("e500-pcihost", MPC8544_PCI_REGS_BASE,
266
                                mpic[pci_irq_nrs[0]], mpic[pci_irq_nrs[1]],
267
                                mpic[pci_irq_nrs[2]], mpic[pci_irq_nrs[3]],
268
                                NULL);
269
    pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
270
    if (!pci_bus)
271
        printf("couldn't create PCI controller!\n");
272

    
273
    isa_mmio_init(MPC8544_PCI_IO, MPC8544_PCI_IOLEN);
274

    
275
    if (pci_bus) {
276
        /* Register network interfaces. */
277
        for (i = 0; i < nb_nics; i++) {
278
            pci_nic_init_nofail(&nd_table[i], "virtio", NULL);
279
        }
280
    }
281

    
282
    /* Load kernel. */
283
    if (kernel_filename) {
284
        kernel_size = load_uimage(kernel_filename, &entry, &loadaddr, NULL);
285
        if (kernel_size < 0) {
286
            kernel_size = load_elf(kernel_filename, NULL, NULL, &elf_entry,
287
                                   &elf_lowaddr, NULL, 1, ELF_MACHINE, 0);
288
            entry = elf_entry;
289
            loadaddr = elf_lowaddr;
290
        }
291
        /* XXX try again as binary */
292
        if (kernel_size < 0) {
293
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
294
                    kernel_filename);
295
            exit(1);
296
        }
297
    }
298

    
299
    /* Load initrd. */
300
    if (initrd_filename) {
301
        initrd_base = (kernel_size + INITRD_LOAD_PAD) & ~INITRD_PAD_MASK;
302
        initrd_size = load_image_targphys(initrd_filename, initrd_base,
303
                                          ram_size - initrd_base);
304

    
305
        if (initrd_size < 0) {
306
            fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
307
                    initrd_filename);
308
            exit(1);
309
        }
310
    }
311

    
312
    /* If we're loading a kernel directly, we must load the device tree too. */
313
    if (kernel_filename) {
314
#ifndef CONFIG_FDT
315
        cpu_abort(env, "Compiled without FDT support - can't load kernel\n");
316
#endif
317
        dt_base = (kernel_size + DTC_LOAD_PAD) & ~DTC_PAD_MASK;
318
        if (mpc8544_load_device_tree(env, dt_base, ram_size,
319
                    initrd_base, initrd_size, kernel_cmdline) < 0) {
320
            fprintf(stderr, "couldn't load device tree\n");
321
            exit(1);
322
        }
323

    
324
        boot_info = env->load_info;
325
        boot_info->entry = entry;
326
        boot_info->dt_base = dt_base;
327
    }
328

    
329
    if (kvm_enabled()) {
330
        kvmppc_init();
331
    }
332
}
333

    
334
static QEMUMachine mpc8544ds_machine = {
335
    .name = "mpc8544ds",
336
    .desc = "mpc8544ds",
337
    .init = mpc8544ds_init,
338
};
339

    
340
static void mpc8544ds_machine_init(void)
341
{
342
    qemu_register_machine(&mpc8544ds_machine);
343
}
344

    
345
machine_init(mpc8544ds_machine_init);