~ahs3/+junk/cq-qemu

« back to all changes in this revision

Viewing changes to hw/ppc440_bamboo.c

  • Committer: Al Stone
  • Date: 2012-02-09 01:17:20 UTC
  • Revision ID: albert.stone@canonical.com-20120209011720-tztl7ik3qayz80p4
first commit to bzr for qemu

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Qemu PowerPC 440 Bamboo board emulation
 
3
 *
 
4
 * Copyright 2007 IBM Corporation.
 
5
 * Authors:
 
6
 *      Jerone Young <jyoung5@us.ibm.com>
 
7
 *      Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com>
 
8
 *      Hollis Blanchard <hollisb@us.ibm.com>
 
9
 *
 
10
 * This work is licensed under the GNU GPL license version 2 or later.
 
11
 *
 
12
 */
 
13
 
 
14
#include "config.h"
 
15
#include "qemu-common.h"
 
16
#include "net.h"
 
17
#include "hw.h"
 
18
#include "pci.h"
 
19
#include "boards.h"
 
20
#include "ppc440.h"
 
21
#include "kvm.h"
 
22
#include "kvm_ppc.h"
 
23
#include "device_tree.h"
 
24
#include "loader.h"
 
25
#include "elf.h"
 
26
#include "exec-memory.h"
 
27
 
 
28
#define BINARY_DEVICE_TREE_FILE "bamboo.dtb"
 
29
 
 
30
/* from u-boot */
 
31
#define KERNEL_ADDR  0x1000000
 
32
#define FDT_ADDR     0x1800000
 
33
#define RAMDISK_ADDR 0x1900000
 
34
 
 
35
static int bamboo_load_device_tree(target_phys_addr_t addr,
 
36
                                     uint32_t ramsize,
 
37
                                     target_phys_addr_t initrd_base,
 
38
                                     target_phys_addr_t initrd_size,
 
39
                                     const char *kernel_cmdline)
 
40
{
 
41
    int ret = -1;
 
42
#ifdef CONFIG_FDT
 
43
    uint32_t mem_reg_property[] = { 0, 0, ramsize };
 
44
    char *filename;
 
45
    int fdt_size;
 
46
    void *fdt;
 
47
    uint32_t tb_freq = 400000000;
 
48
    uint32_t clock_freq = 400000000;
 
49
 
 
50
    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
 
51
    if (!filename) {
 
52
        goto out;
 
53
    }
 
54
    fdt = load_device_tree(filename, &fdt_size);
 
55
    g_free(filename);
 
56
    if (fdt == NULL) {
 
57
        goto out;
 
58
    }
 
59
 
 
60
    /* Manipulate device tree in memory. */
 
61
 
 
62
    ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
 
63
                               sizeof(mem_reg_property));
 
64
    if (ret < 0)
 
65
        fprintf(stderr, "couldn't set /memory/reg\n");
 
66
 
 
67
    ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
 
68
                                    initrd_base);
 
69
    if (ret < 0)
 
70
        fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
 
71
 
 
72
    ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
 
73
                                    (initrd_base + initrd_size));
 
74
    if (ret < 0)
 
75
        fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
 
76
 
 
77
    ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
 
78
                                      kernel_cmdline);
 
79
    if (ret < 0)
 
80
        fprintf(stderr, "couldn't set /chosen/bootargs\n");
 
81
 
 
82
    /* Copy data from the host device tree into the guest. Since the guest can
 
83
     * directly access the timebase without host involvement, we must expose
 
84
     * the correct frequencies. */
 
85
    if (kvm_enabled()) {
 
86
        tb_freq = kvmppc_get_tbfreq();
 
87
        clock_freq = kvmppc_get_clockfreq();
 
88
    }
 
89
 
 
90
    qemu_devtree_setprop_cell(fdt, "/cpus/cpu@0", "clock-frequency",
 
91
                              clock_freq);
 
92
    qemu_devtree_setprop_cell(fdt, "/cpus/cpu@0", "timebase-frequency",
 
93
                              tb_freq);
 
94
 
 
95
    ret = rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
 
96
    g_free(fdt);
 
97
 
 
98
out:
 
99
#endif
 
100
 
 
101
    return ret;
 
102
}
 
103
 
 
104
static void bamboo_init(ram_addr_t ram_size,
 
105
                        const char *boot_device,
 
106
                        const char *kernel_filename,
 
107
                        const char *kernel_cmdline,
 
108
                        const char *initrd_filename,
 
109
                        const char *cpu_model)
 
110
{
 
111
    unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
 
112
    MemoryRegion *address_space_mem = get_system_memory();
 
113
    PCIBus *pcibus;
 
114
    CPUState *env;
 
115
    uint64_t elf_entry;
 
116
    uint64_t elf_lowaddr;
 
117
    target_phys_addr_t entry = 0;
 
118
    target_phys_addr_t loadaddr = 0;
 
119
    target_long initrd_size = 0;
 
120
    int success;
 
121
    int i;
 
122
 
 
123
    /* Setup CPU. */
 
124
    env = ppc440ep_init(address_space_mem, &ram_size, &pcibus,
 
125
                        pci_irq_nrs, 1, cpu_model);
 
126
 
 
127
    if (pcibus) {
 
128
        /* Register network interfaces. */
 
129
        for (i = 0; i < nb_nics; i++) {
 
130
            /* There are no PCI NICs on the Bamboo board, but there are
 
131
             * PCI slots, so we can pick whatever default model we want. */
 
132
            pci_nic_init_nofail(&nd_table[i], "e1000", NULL);
 
133
        }
 
134
    }
 
135
 
 
136
    /* Load kernel. */
 
137
    if (kernel_filename) {
 
138
        success = load_uimage(kernel_filename, &entry, &loadaddr, NULL);
 
139
        if (success < 0) {
 
140
            success = load_elf(kernel_filename, NULL, NULL, &elf_entry,
 
141
                               &elf_lowaddr, NULL, 1, ELF_MACHINE, 0);
 
142
            entry = elf_entry;
 
143
            loadaddr = elf_lowaddr;
 
144
        }
 
145
        /* XXX try again as binary */
 
146
        if (success < 0) {
 
147
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
 
148
                    kernel_filename);
 
149
            exit(1);
 
150
        }
 
151
    }
 
152
 
 
153
    /* Load initrd. */
 
154
    if (initrd_filename) {
 
155
        initrd_size = load_image_targphys(initrd_filename, RAMDISK_ADDR,
 
156
                                          ram_size - RAMDISK_ADDR);
 
157
 
 
158
        if (initrd_size < 0) {
 
159
            fprintf(stderr, "qemu: could not load ram disk '%s' at %x\n",
 
160
                    initrd_filename, RAMDISK_ADDR);
 
161
            exit(1);
 
162
        }
 
163
    }
 
164
 
 
165
    /* If we're loading a kernel directly, we must load the device tree too. */
 
166
    if (kernel_filename) {
 
167
        if (bamboo_load_device_tree(FDT_ADDR, ram_size, RAMDISK_ADDR,
 
168
                                    initrd_size, kernel_cmdline) < 0) {
 
169
            fprintf(stderr, "couldn't load device tree\n");
 
170
            exit(1);
 
171
        }
 
172
 
 
173
        /* Set initial guest state. */
 
174
        env->gpr[1] = (16<<20) - 8;
 
175
        env->gpr[3] = FDT_ADDR;
 
176
        env->nip = entry;
 
177
        /* XXX we currently depend on KVM to create some initial TLB entries. */
 
178
    }
 
179
 
 
180
    if (kvm_enabled())
 
181
        kvmppc_init();
 
182
}
 
183
 
 
184
static QEMUMachine bamboo_machine = {
 
185
    .name = "bamboo-0.13",
 
186
    .alias = "bamboo",
 
187
    .desc = "bamboo",
 
188
    .init = bamboo_init,
 
189
};
 
190
 
 
191
static QEMUMachine bamboo_machine_v0_12 = {
 
192
    .name = "bamboo-0.12",
 
193
    .desc = "bamboo",
 
194
    .init = bamboo_init,
 
195
    .compat_props = (GlobalProperty[]) {
 
196
        {
 
197
            .driver   = "virtio-serial-pci",
 
198
            .property = "max_ports",
 
199
            .value    = stringify(1),
 
200
        },{
 
201
            .driver   = "virtio-serial-pci",
 
202
            .property = "vectors",
 
203
            .value    = stringify(0),
 
204
        },
 
205
        { /* end of list */ }
 
206
    },
 
207
};
 
208
 
 
209
static void bamboo_machine_init(void)
 
210
{
 
211
    qemu_register_machine(&bamboo_machine);
 
212
    qemu_register_machine(&bamboo_machine_v0_12);
 
213
}
 
214
 
 
215
machine_init(bamboo_machine_init);