Statistics
| Branch: | Revision:

root / hw / ppc440_bamboo.c @ bf3bc4c4

History | View | Annotate | Download (9.4 kB)

1 2c9fade2 aurel32
/*
2 5cbdb3a3 Stefan Weil
 * QEMU PowerPC 440 Bamboo board emulation
3 2c9fade2 aurel32
 *
4 2c9fade2 aurel32
 * Copyright 2007 IBM Corporation.
5 2c9fade2 aurel32
 * Authors:
6 acd1bf90 Alexander Graf
 *        Jerone Young <jyoung5@us.ibm.com>
7 acd1bf90 Alexander Graf
 *        Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
8 acd1bf90 Alexander Graf
 *        Hollis Blanchard <hollisb@us.ibm.com>
9 2c9fade2 aurel32
 *
10 2c9fade2 aurel32
 * This work is licensed under the GNU GPL license version 2 or later.
11 2c9fade2 aurel32
 *
12 2c9fade2 aurel32
 */
13 2c9fade2 aurel32
14 2c9fade2 aurel32
#include "config.h"
15 2c9fade2 aurel32
#include "qemu-common.h"
16 2c9fade2 aurel32
#include "net.h"
17 2c9fade2 aurel32
#include "hw.h"
18 2c9fade2 aurel32
#include "pci.h"
19 2c9fade2 aurel32
#include "boards.h"
20 2c9fade2 aurel32
#include "kvm.h"
21 2c9fade2 aurel32
#include "kvm_ppc.h"
22 2c9fade2 aurel32
#include "device_tree.h"
23 ca20cf32 Blue Swirl
#include "loader.h"
24 ca20cf32 Blue Swirl
#include "elf.h"
25 3e9f0113 Richard Henderson
#include "exec-memory.h"
26 488cb996 Gerd Hoffmann
#include "serial.h"
27 3960b04d Alexander Graf
#include "ppc.h"
28 3960b04d Alexander Graf
#include "ppc405.h"
29 3960b04d Alexander Graf
#include "sysemu.h"
30 34ba1dc8 Alexander Graf
#include "sysbus.h"
31 2c9fade2 aurel32
32 2c9fade2 aurel32
#define BINARY_DEVICE_TREE_FILE "bamboo.dtb"
33 2c9fade2 aurel32
34 ceee6da6 Hollis Blanchard
/* from u-boot */
35 ceee6da6 Hollis Blanchard
#define KERNEL_ADDR  0x1000000
36 ceee6da6 Hollis Blanchard
#define FDT_ADDR     0x1800000
37 ceee6da6 Hollis Blanchard
#define RAMDISK_ADDR 0x1900000
38 ceee6da6 Hollis Blanchard
39 3960b04d Alexander Graf
#define PPC440EP_PCI_CONFIG     0xeec00000
40 3960b04d Alexander Graf
#define PPC440EP_PCI_INTACK     0xeed00000
41 3960b04d Alexander Graf
#define PPC440EP_PCI_SPECIAL    0xeed00000
42 3960b04d Alexander Graf
#define PPC440EP_PCI_REGS       0xef400000
43 3960b04d Alexander Graf
#define PPC440EP_PCI_IO         0xe8000000
44 3960b04d Alexander Graf
#define PPC440EP_PCI_IOLEN      0x00010000
45 3960b04d Alexander Graf
46 3960b04d Alexander Graf
#define PPC440EP_SDRAM_NR_BANKS 4
47 3960b04d Alexander Graf
48 3960b04d Alexander Graf
static const unsigned int ppc440ep_sdram_bank_sizes[] = {
49 3960b04d Alexander Graf
    256<<20, 128<<20, 64<<20, 32<<20, 16<<20, 8<<20, 0
50 3960b04d Alexander Graf
};
51 3960b04d Alexander Graf
52 a8170e5e Avi Kivity
static hwaddr entry;
53 b10a04b5 Alexander Graf
54 a8170e5e Avi Kivity
static int bamboo_load_device_tree(hwaddr addr,
55 2c9fade2 aurel32
                                     uint32_t ramsize,
56 a8170e5e Avi Kivity
                                     hwaddr initrd_base,
57 a8170e5e Avi Kivity
                                     hwaddr initrd_size,
58 2c9fade2 aurel32
                                     const char *kernel_cmdline)
59 2c9fade2 aurel32
{
60 dbf916d8 Aurelien Jarno
    int ret = -1;
61 3f0855b1 Juan Quintela
#ifdef CONFIG_FDT
62 5232fa59 Alexander Graf
    uint32_t mem_reg_property[] = { 0, 0, cpu_to_be32(ramsize) };
63 5cea8590 Paul Brook
    char *filename;
64 7ec632b4 pbrook
    int fdt_size;
65 dbf916d8 Aurelien Jarno
    void *fdt;
66 7dadd40c Alexander Graf
    uint32_t tb_freq = 400000000;
67 7dadd40c Alexander Graf
    uint32_t clock_freq = 400000000;
68 2c9fade2 aurel32
69 5cea8590 Paul Brook
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
70 5cea8590 Paul Brook
    if (!filename) {
71 5cea8590 Paul Brook
        goto out;
72 5cea8590 Paul Brook
    }
73 5cea8590 Paul Brook
    fdt = load_device_tree(filename, &fdt_size);
74 7267c094 Anthony Liguori
    g_free(filename);
75 5cea8590 Paul Brook
    if (fdt == NULL) {
76 2c9fade2 aurel32
        goto out;
77 5cea8590 Paul Brook
    }
78 2c9fade2 aurel32
79 2c9fade2 aurel32
    /* Manipulate device tree in memory. */
80 2c9fade2 aurel32
81 2c9fade2 aurel32
    ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
82 2c9fade2 aurel32
                               sizeof(mem_reg_property));
83 2c9fade2 aurel32
    if (ret < 0)
84 2c9fade2 aurel32
        fprintf(stderr, "couldn't set /memory/reg\n");
85 2c9fade2 aurel32
86 2c9fade2 aurel32
    ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
87 2c9fade2 aurel32
                                    initrd_base);
88 2c9fade2 aurel32
    if (ret < 0)
89 2c9fade2 aurel32
        fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
90 2c9fade2 aurel32
91 2c9fade2 aurel32
    ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
92 2c9fade2 aurel32
                                    (initrd_base + initrd_size));
93 2c9fade2 aurel32
    if (ret < 0)
94 2c9fade2 aurel32
        fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
95 2c9fade2 aurel32
96 2c9fade2 aurel32
    ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
97 2c9fade2 aurel32
                                      kernel_cmdline);
98 2c9fade2 aurel32
    if (ret < 0)
99 2c9fade2 aurel32
        fprintf(stderr, "couldn't set /chosen/bootargs\n");
100 2c9fade2 aurel32
101 7dadd40c Alexander Graf
    /* Copy data from the host device tree into the guest. Since the guest can
102 7dadd40c Alexander Graf
     * directly access the timebase without host involvement, we must expose
103 7dadd40c Alexander Graf
     * the correct frequencies. */
104 a489f7f7 Alexander Graf
    if (kvm_enabled()) {
105 7dadd40c Alexander Graf
        tb_freq = kvmppc_get_tbfreq();
106 7dadd40c Alexander Graf
        clock_freq = kvmppc_get_clockfreq();
107 a489f7f7 Alexander Graf
    }
108 2c9fade2 aurel32
109 7dadd40c Alexander Graf
    qemu_devtree_setprop_cell(fdt, "/cpus/cpu@0", "clock-frequency",
110 7dadd40c Alexander Graf
                              clock_freq);
111 7dadd40c Alexander Graf
    qemu_devtree_setprop_cell(fdt, "/cpus/cpu@0", "timebase-frequency",
112 7dadd40c Alexander Graf
                              tb_freq);
113 2c9fade2 aurel32
114 04088adb Liu Yu
    ret = rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
115 7267c094 Anthony Liguori
    g_free(fdt);
116 7ec632b4 pbrook
117 2c9fade2 aurel32
out:
118 2c9fade2 aurel32
#endif
119 2c9fade2 aurel32
120 04088adb Liu Yu
    return ret;
121 2c9fade2 aurel32
}
122 2c9fade2 aurel32
123 72718e9a Alexander Graf
/* Create reset TLB entries for BookE, spanning the 32bit addr space.  */
124 e2684c0b Andreas Färber
static void mmubooke_create_initial_mapping(CPUPPCState *env,
125 72718e9a Alexander Graf
                                     target_ulong va,
126 a8170e5e Avi Kivity
                                     hwaddr pa)
127 72718e9a Alexander Graf
{
128 72718e9a Alexander Graf
    ppcemb_tlb_t *tlb = &env->tlb.tlbe[0];
129 72718e9a Alexander Graf
130 72718e9a Alexander Graf
    tlb->attr = 0;
131 72718e9a Alexander Graf
    tlb->prot = PAGE_VALID | ((PAGE_READ | PAGE_WRITE | PAGE_EXEC) << 4);
132 72718e9a Alexander Graf
    tlb->size = 1 << 31; /* up to 0x80000000  */
133 72718e9a Alexander Graf
    tlb->EPN = va & TARGET_PAGE_MASK;
134 72718e9a Alexander Graf
    tlb->RPN = pa & TARGET_PAGE_MASK;
135 72718e9a Alexander Graf
    tlb->PID = 0;
136 72718e9a Alexander Graf
137 72718e9a Alexander Graf
    tlb = &env->tlb.tlbe[1];
138 72718e9a Alexander Graf
    tlb->attr = 0;
139 72718e9a Alexander Graf
    tlb->prot = PAGE_VALID | ((PAGE_READ | PAGE_WRITE | PAGE_EXEC) << 4);
140 72718e9a Alexander Graf
    tlb->size = 1 << 31; /* up to 0xffffffff  */
141 72718e9a Alexander Graf
    tlb->EPN = 0x80000000 & TARGET_PAGE_MASK;
142 72718e9a Alexander Graf
    tlb->RPN = 0x80000000 & TARGET_PAGE_MASK;
143 72718e9a Alexander Graf
    tlb->PID = 0;
144 72718e9a Alexander Graf
}
145 72718e9a Alexander Graf
146 b10a04b5 Alexander Graf
static void main_cpu_reset(void *opaque)
147 b10a04b5 Alexander Graf
{
148 182fbbf2 Andreas Färber
    PowerPCCPU *cpu = opaque;
149 182fbbf2 Andreas Färber
    CPUPPCState *env = &cpu->env;
150 b10a04b5 Alexander Graf
151 182fbbf2 Andreas Färber
    cpu_reset(CPU(cpu));
152 b10a04b5 Alexander Graf
    env->gpr[1] = (16<<20) - 8;
153 b10a04b5 Alexander Graf
    env->gpr[3] = FDT_ADDR;
154 b10a04b5 Alexander Graf
    env->nip = entry;
155 72718e9a Alexander Graf
156 72718e9a Alexander Graf
    /* Create a mapping for the kernel.  */
157 72718e9a Alexander Graf
    mmubooke_create_initial_mapping(env, 0, 0);
158 b10a04b5 Alexander Graf
}
159 b10a04b5 Alexander Graf
160 5f072e1f Eduardo Habkost
static void bamboo_init(QEMUMachineInitArgs *args)
161 2c9fade2 aurel32
{
162 5f072e1f Eduardo Habkost
    ram_addr_t ram_size = args->ram_size;
163 5f072e1f Eduardo Habkost
    const char *cpu_model = args->cpu_model;
164 5f072e1f Eduardo Habkost
    const char *kernel_filename = args->kernel_filename;
165 5f072e1f Eduardo Habkost
    const char *kernel_cmdline = args->kernel_cmdline;
166 5f072e1f Eduardo Habkost
    const char *initrd_filename = args->initrd_filename;
167 2c9fade2 aurel32
    unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
168 3e9f0113 Richard Henderson
    MemoryRegion *address_space_mem = get_system_memory();
169 34ba1dc8 Alexander Graf
    MemoryRegion *ram_memories
170 34ba1dc8 Alexander Graf
        = g_malloc(PPC440EP_SDRAM_NR_BANKS * sizeof(*ram_memories));
171 a8170e5e Avi Kivity
    hwaddr ram_bases[PPC440EP_SDRAM_NR_BANKS];
172 a8170e5e Avi Kivity
    hwaddr ram_sizes[PPC440EP_SDRAM_NR_BANKS];
173 34ba1dc8 Alexander Graf
    qemu_irq *pic;
174 34ba1dc8 Alexander Graf
    qemu_irq *irqs;
175 2c9fade2 aurel32
    PCIBus *pcibus;
176 322164e0 Andreas Färber
    PowerPCCPU *cpu;
177 e2684c0b Andreas Färber
    CPUPPCState *env;
178 2c9fade2 aurel32
    uint64_t elf_entry;
179 2c9fade2 aurel32
    uint64_t elf_lowaddr;
180 a8170e5e Avi Kivity
    hwaddr loadaddr = 0;
181 2c9fade2 aurel32
    target_long initrd_size = 0;
182 34ba1dc8 Alexander Graf
    DeviceState *dev;
183 ceee6da6 Hollis Blanchard
    int success;
184 2c9fade2 aurel32
    int i;
185 2c9fade2 aurel32
186 2c9fade2 aurel32
    /* Setup CPU. */
187 34ba1dc8 Alexander Graf
    if (cpu_model == NULL) {
188 34ba1dc8 Alexander Graf
        cpu_model = "440EP";
189 34ba1dc8 Alexander Graf
    }
190 322164e0 Andreas Färber
    cpu = cpu_ppc_init(cpu_model);
191 322164e0 Andreas Färber
    if (cpu == NULL) {
192 34ba1dc8 Alexander Graf
        fprintf(stderr, "Unable to initialize CPU!\n");
193 34ba1dc8 Alexander Graf
        exit(1);
194 34ba1dc8 Alexander Graf
    }
195 322164e0 Andreas Färber
    env = &cpu->env;
196 34ba1dc8 Alexander Graf
197 182fbbf2 Andreas Färber
    qemu_register_reset(main_cpu_reset, cpu);
198 34ba1dc8 Alexander Graf
    ppc_booke_timers_init(env, 400000000, 0);
199 34ba1dc8 Alexander Graf
    ppc_dcr_init(env, NULL, NULL);
200 34ba1dc8 Alexander Graf
201 34ba1dc8 Alexander Graf
    /* interrupt controller */
202 34ba1dc8 Alexander Graf
    irqs = g_malloc0(sizeof(qemu_irq) * PPCUIC_OUTPUT_NB);
203 34ba1dc8 Alexander Graf
    irqs[PPCUIC_OUTPUT_INT] = ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_INT];
204 34ba1dc8 Alexander Graf
    irqs[PPCUIC_OUTPUT_CINT] = ((qemu_irq *)env->irq_inputs)[PPC40x_INPUT_CINT];
205 34ba1dc8 Alexander Graf
    pic = ppcuic_init(env, irqs, 0x0C0, 0, 1);
206 34ba1dc8 Alexander Graf
207 34ba1dc8 Alexander Graf
    /* SDRAM controller */
208 34ba1dc8 Alexander Graf
    memset(ram_bases, 0, sizeof(ram_bases));
209 34ba1dc8 Alexander Graf
    memset(ram_sizes, 0, sizeof(ram_sizes));
210 34ba1dc8 Alexander Graf
    ram_size = ppc4xx_sdram_adjust(ram_size, PPC440EP_SDRAM_NR_BANKS,
211 34ba1dc8 Alexander Graf
                                   ram_memories,
212 34ba1dc8 Alexander Graf
                                   ram_bases, ram_sizes,
213 34ba1dc8 Alexander Graf
                                   ppc440ep_sdram_bank_sizes);
214 34ba1dc8 Alexander Graf
    /* XXX 440EP's ECC interrupts are on UIC1, but we've only created UIC0. */
215 34ba1dc8 Alexander Graf
    ppc4xx_sdram_init(env, pic[14], PPC440EP_SDRAM_NR_BANKS, ram_memories,
216 34ba1dc8 Alexander Graf
                      ram_bases, ram_sizes, 1);
217 34ba1dc8 Alexander Graf
218 34ba1dc8 Alexander Graf
    /* PCI */
219 42c281a2 Andreas Färber
    dev = sysbus_create_varargs(TYPE_PPC4xx_PCI_HOST_BRIDGE,
220 42c281a2 Andreas Färber
                                PPC440EP_PCI_CONFIG,
221 34ba1dc8 Alexander Graf
                                pic[pci_irq_nrs[0]], pic[pci_irq_nrs[1]],
222 34ba1dc8 Alexander Graf
                                pic[pci_irq_nrs[2]], pic[pci_irq_nrs[3]],
223 34ba1dc8 Alexander Graf
                                NULL);
224 34ba1dc8 Alexander Graf
    pcibus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
225 34ba1dc8 Alexander Graf
    if (!pcibus) {
226 34ba1dc8 Alexander Graf
        fprintf(stderr, "couldn't create PCI controller!\n");
227 34ba1dc8 Alexander Graf
        exit(1);
228 34ba1dc8 Alexander Graf
    }
229 34ba1dc8 Alexander Graf
230 34ba1dc8 Alexander Graf
    isa_mmio_init(PPC440EP_PCI_IO, PPC440EP_PCI_IOLEN);
231 34ba1dc8 Alexander Graf
232 34ba1dc8 Alexander Graf
    if (serial_hds[0] != NULL) {
233 34ba1dc8 Alexander Graf
        serial_mm_init(address_space_mem, 0xef600300, 0, pic[0],
234 34ba1dc8 Alexander Graf
                       PPC_SERIAL_MM_BAUDBASE, serial_hds[0],
235 34ba1dc8 Alexander Graf
                       DEVICE_BIG_ENDIAN);
236 34ba1dc8 Alexander Graf
    }
237 34ba1dc8 Alexander Graf
    if (serial_hds[1] != NULL) {
238 34ba1dc8 Alexander Graf
        serial_mm_init(address_space_mem, 0xef600400, 0, pic[1],
239 34ba1dc8 Alexander Graf
                       PPC_SERIAL_MM_BAUDBASE, serial_hds[1],
240 34ba1dc8 Alexander Graf
                       DEVICE_BIG_ENDIAN);
241 34ba1dc8 Alexander Graf
    }
242 2c9fade2 aurel32
243 2c9fade2 aurel32
    if (pcibus) {
244 2c9fade2 aurel32
        /* Register network interfaces. */
245 2c9fade2 aurel32
        for (i = 0; i < nb_nics; i++) {
246 cb457d76 aliguori
            /* There are no PCI NICs on the Bamboo board, but there are
247 cb457d76 aliguori
             * PCI slots, so we can pick whatever default model we want. */
248 07caea31 Markus Armbruster
            pci_nic_init_nofail(&nd_table[i], "e1000", NULL);
249 2c9fade2 aurel32
        }
250 2c9fade2 aurel32
    }
251 2c9fade2 aurel32
252 2c9fade2 aurel32
    /* Load kernel. */
253 2c9fade2 aurel32
    if (kernel_filename) {
254 ceee6da6 Hollis Blanchard
        success = load_uimage(kernel_filename, &entry, &loadaddr, NULL);
255 ceee6da6 Hollis Blanchard
        if (success < 0) {
256 ceee6da6 Hollis Blanchard
            success = load_elf(kernel_filename, NULL, NULL, &elf_entry,
257 ceee6da6 Hollis Blanchard
                               &elf_lowaddr, NULL, 1, ELF_MACHINE, 0);
258 2c9fade2 aurel32
            entry = elf_entry;
259 2c9fade2 aurel32
            loadaddr = elf_lowaddr;
260 2c9fade2 aurel32
        }
261 2c9fade2 aurel32
        /* XXX try again as binary */
262 ceee6da6 Hollis Blanchard
        if (success < 0) {
263 2c9fade2 aurel32
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
264 2c9fade2 aurel32
                    kernel_filename);
265 2c9fade2 aurel32
            exit(1);
266 2c9fade2 aurel32
        }
267 2c9fade2 aurel32
    }
268 2c9fade2 aurel32
269 2c9fade2 aurel32
    /* Load initrd. */
270 2c9fade2 aurel32
    if (initrd_filename) {
271 ceee6da6 Hollis Blanchard
        initrd_size = load_image_targphys(initrd_filename, RAMDISK_ADDR,
272 ceee6da6 Hollis Blanchard
                                          ram_size - RAMDISK_ADDR);
273 2c9fade2 aurel32
274 2c9fade2 aurel32
        if (initrd_size < 0) {
275 ceee6da6 Hollis Blanchard
            fprintf(stderr, "qemu: could not load ram disk '%s' at %x\n",
276 ceee6da6 Hollis Blanchard
                    initrd_filename, RAMDISK_ADDR);
277 2c9fade2 aurel32
            exit(1);
278 2c9fade2 aurel32
        }
279 2c9fade2 aurel32
    }
280 2c9fade2 aurel32
281 2c9fade2 aurel32
    /* If we're loading a kernel directly, we must load the device tree too. */
282 2c9fade2 aurel32
    if (kernel_filename) {
283 ceee6da6 Hollis Blanchard
        if (bamboo_load_device_tree(FDT_ADDR, ram_size, RAMDISK_ADDR,
284 ceee6da6 Hollis Blanchard
                                    initrd_size, kernel_cmdline) < 0) {
285 2c9fade2 aurel32
            fprintf(stderr, "couldn't load device tree\n");
286 2c9fade2 aurel32
            exit(1);
287 2c9fade2 aurel32
        }
288 2c9fade2 aurel32
    }
289 2c9fade2 aurel32
290 2c9fade2 aurel32
    if (kvm_enabled())
291 2c9fade2 aurel32
        kvmppc_init();
292 2c9fade2 aurel32
}
293 2c9fade2 aurel32
294 f80f9ec9 Anthony Liguori
static QEMUMachine bamboo_machine = {
295 d3c4548b Alexander Graf
    .name = "bamboo",
296 977b6b91 Amit Shah
    .desc = "bamboo",
297 977b6b91 Amit Shah
    .init = bamboo_init,
298 977b6b91 Amit Shah
};
299 977b6b91 Amit Shah
300 f80f9ec9 Anthony Liguori
static void bamboo_machine_init(void)
301 f80f9ec9 Anthony Liguori
{
302 f80f9ec9 Anthony Liguori
    qemu_register_machine(&bamboo_machine);
303 f80f9ec9 Anthony Liguori
}
304 f80f9ec9 Anthony Liguori
305 f80f9ec9 Anthony Liguori
machine_init(bamboo_machine_init);