Statistics
| Branch: | Revision:

root / hw / ppce500_mpc8544ds.c @ d78f3995

History | View | Annotate | Download (8.6 kB)

1 1db09b84 aurel32
/*
2 1db09b84 aurel32
 * Qemu PowerPC MPC8544DS board emualtion
3 1db09b84 aurel32
 *
4 1db09b84 aurel32
 * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
5 1db09b84 aurel32
 *
6 1db09b84 aurel32
 * Author: Yu Liu,     <yu.liu@freescale.com>
7 1db09b84 aurel32
 *
8 1db09b84 aurel32
 * This file is derived from hw/ppc440_bamboo.c,
9 1db09b84 aurel32
 * the copyright for that material belongs to the original owners.
10 1db09b84 aurel32
 *
11 1db09b84 aurel32
 * This is free software; you can redistribute it and/or modify
12 1db09b84 aurel32
 * it under the terms of  the GNU General  Public License as published by
13 1db09b84 aurel32
 * the Free Software Foundation;  either version 2 of the  License, or
14 1db09b84 aurel32
 * (at your option) any later version.
15 1db09b84 aurel32
 */
16 1db09b84 aurel32
17 1db09b84 aurel32
#include <dirent.h>
18 1db09b84 aurel32
19 1db09b84 aurel32
#include "config.h"
20 1db09b84 aurel32
#include "qemu-common.h"
21 1db09b84 aurel32
#include "net.h"
22 1db09b84 aurel32
#include "hw.h"
23 1db09b84 aurel32
#include "pc.h"
24 1db09b84 aurel32
#include "pci.h"
25 1db09b84 aurel32
#include "virtio-blk.h"
26 1db09b84 aurel32
#include "boards.h"
27 1db09b84 aurel32
#include "sysemu.h"
28 1db09b84 aurel32
#include "kvm.h"
29 1db09b84 aurel32
#include "kvm_ppc.h"
30 1db09b84 aurel32
#include "device_tree.h"
31 1db09b84 aurel32
#include "openpic.h"
32 1db09b84 aurel32
#include "ppce500.h"
33 1db09b84 aurel32
34 1db09b84 aurel32
#define BINARY_DEVICE_TREE_FILE    "mpc8544ds.dtb"
35 1db09b84 aurel32
#define UIMAGE_LOAD_BASE           0
36 1db09b84 aurel32
#define DTB_LOAD_BASE              0x600000
37 1db09b84 aurel32
#define INITRD_LOAD_BASE           0x2000000
38 1db09b84 aurel32
39 1db09b84 aurel32
#define RAM_SIZES_ALIGN            (64UL << 20)
40 1db09b84 aurel32
41 1db09b84 aurel32
#define MPC8544_CCSRBAR_BASE       0xE0000000
42 1db09b84 aurel32
#define MPC8544_MPIC_REGS_BASE     (MPC8544_CCSRBAR_BASE + 0x40000)
43 1db09b84 aurel32
#define MPC8544_SERIAL0_REGS_BASE  (MPC8544_CCSRBAR_BASE + 0x4500)
44 1db09b84 aurel32
#define MPC8544_SERIAL1_REGS_BASE  (MPC8544_CCSRBAR_BASE + 0x4600)
45 1db09b84 aurel32
#define MPC8544_PCI_REGS_BASE      (MPC8544_CCSRBAR_BASE + 0x8000)
46 1db09b84 aurel32
#define MPC8544_PCI_REGS_SIZE      0x1000
47 1db09b84 aurel32
#define MPC8544_PCI_IO             0xE1000000
48 1db09b84 aurel32
#define MPC8544_PCI_IOLEN          0x10000
49 1db09b84 aurel32
50 511d2b14 blueswir1
#ifdef HAVE_FDT
51 1db09b84 aurel32
static int mpc8544_copy_soc_cell(void *fdt, const char *node, const char *prop)
52 1db09b84 aurel32
{
53 1db09b84 aurel32
    uint32_t cell;
54 1db09b84 aurel32
    int ret;
55 1db09b84 aurel32
56 1db09b84 aurel32
    ret = kvmppc_read_host_property(node, prop, &cell, sizeof(cell));
57 1db09b84 aurel32
    if (ret < 0) {
58 1db09b84 aurel32
        fprintf(stderr, "couldn't read host %s/%s\n", node, prop);
59 1db09b84 aurel32
        goto out;
60 1db09b84 aurel32
    }
61 1db09b84 aurel32
62 1db09b84 aurel32
    ret = qemu_devtree_setprop_cell(fdt, "/cpus/PowerPC,8544@0",
63 1db09b84 aurel32
                                prop, cell);
64 1db09b84 aurel32
    if (ret < 0) {
65 1db09b84 aurel32
        fprintf(stderr, "couldn't set guest /cpus/PowerPC,8544@0/%s\n", prop);
66 1db09b84 aurel32
        goto out;
67 1db09b84 aurel32
    }
68 1db09b84 aurel32
69 1db09b84 aurel32
out:
70 1db09b84 aurel32
    return ret;
71 1db09b84 aurel32
}
72 511d2b14 blueswir1
#endif
73 1db09b84 aurel32
74 1db09b84 aurel32
static void *mpc8544_load_device_tree(void *addr,
75 1db09b84 aurel32
                                     uint32_t ramsize,
76 1db09b84 aurel32
                                     target_phys_addr_t initrd_base,
77 1db09b84 aurel32
                                     target_phys_addr_t initrd_size,
78 1db09b84 aurel32
                                     const char *kernel_cmdline)
79 1db09b84 aurel32
{
80 1db09b84 aurel32
    void *fdt = NULL;
81 1db09b84 aurel32
#ifdef HAVE_FDT
82 1db09b84 aurel32
    uint32_t mem_reg_property[] = {0, ramsize};
83 1db09b84 aurel32
    char *path;
84 1db09b84 aurel32
    int pathlen;
85 1db09b84 aurel32
    int ret;
86 1db09b84 aurel32
87 1db09b84 aurel32
    pathlen = snprintf(NULL, 0, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE) + 1;
88 1db09b84 aurel32
    path = qemu_malloc(pathlen);
89 1db09b84 aurel32
90 1db09b84 aurel32
    snprintf(path, pathlen, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE);
91 1db09b84 aurel32
92 1db09b84 aurel32
    fdt = load_device_tree(path, addr);
93 1db09b84 aurel32
    qemu_free(path);
94 1db09b84 aurel32
    if (fdt == NULL)
95 1db09b84 aurel32
        goto out;
96 1db09b84 aurel32
97 1db09b84 aurel32
    /* Manipulate device tree in memory. */
98 1db09b84 aurel32
    ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
99 1db09b84 aurel32
                               sizeof(mem_reg_property));
100 1db09b84 aurel32
    if (ret < 0)
101 1db09b84 aurel32
        fprintf(stderr, "couldn't set /memory/reg\n");
102 1db09b84 aurel32
103 1db09b84 aurel32
    ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
104 1db09b84 aurel32
                                    initrd_base);
105 1db09b84 aurel32
    if (ret < 0)
106 1db09b84 aurel32
        fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
107 1db09b84 aurel32
108 1db09b84 aurel32
    ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
109 1db09b84 aurel32
                                    (initrd_base + initrd_size));
110 1db09b84 aurel32
    if (ret < 0)
111 1db09b84 aurel32
        fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
112 1db09b84 aurel32
113 1db09b84 aurel32
    ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
114 1db09b84 aurel32
                                      kernel_cmdline);
115 1db09b84 aurel32
    if (ret < 0)
116 1db09b84 aurel32
        fprintf(stderr, "couldn't set /chosen/bootargs\n");
117 1db09b84 aurel32
118 1db09b84 aurel32
    if (kvm_enabled()) {
119 1db09b84 aurel32
        struct dirent *dirp;
120 1db09b84 aurel32
        DIR *dp;
121 1db09b84 aurel32
        char buf[128];
122 1db09b84 aurel32
123 1db09b84 aurel32
        if ((dp = opendir("/proc/device-tree/cpus/")) == NULL) {
124 1db09b84 aurel32
            printf("Can't open directory /proc/device-tree/cpus/\n");
125 1db09b84 aurel32
            goto out;
126 1db09b84 aurel32
        }
127 1db09b84 aurel32
128 1db09b84 aurel32
        buf[0] = '\0';
129 1db09b84 aurel32
        while ((dirp = readdir(dp)) != NULL) {
130 1db09b84 aurel32
            if (strncmp(dirp->d_name, "PowerPC", 7) == 0) {
131 1db09b84 aurel32
                snprintf(buf, 128, "/cpus/%s", dirp->d_name);
132 1db09b84 aurel32
                break;
133 1db09b84 aurel32
            }
134 1db09b84 aurel32
        }
135 1db09b84 aurel32
        closedir(dp);
136 1db09b84 aurel32
        if (buf[0] == '\0') {
137 1db09b84 aurel32
            printf("Unknow host!\n");
138 1db09b84 aurel32
            goto out;
139 1db09b84 aurel32
        }
140 1db09b84 aurel32
141 1db09b84 aurel32
        mpc8544_copy_soc_cell(fdt, buf, "clock-frequency");
142 1db09b84 aurel32
        mpc8544_copy_soc_cell(fdt, buf, "timebase-frequency");
143 1db09b84 aurel32
    }
144 1db09b84 aurel32
145 1db09b84 aurel32
out:
146 1db09b84 aurel32
#endif
147 1db09b84 aurel32
148 1db09b84 aurel32
    return fdt;
149 1db09b84 aurel32
}
150 1db09b84 aurel32
151 1db09b84 aurel32
static void mpc8544ds_init(ram_addr_t ram_size, int vga_ram_size,
152 1db09b84 aurel32
                         const char *boot_device,
153 1db09b84 aurel32
                         const char *kernel_filename,
154 1db09b84 aurel32
                         const char *kernel_cmdline,
155 1db09b84 aurel32
                         const char *initrd_filename,
156 1db09b84 aurel32
                         const char *cpu_model)
157 1db09b84 aurel32
{
158 1db09b84 aurel32
    PCIBus *pci_bus;
159 1db09b84 aurel32
    CPUState *env;
160 1db09b84 aurel32
    uint64_t elf_entry;
161 1db09b84 aurel32
    uint64_t elf_lowaddr;
162 1db09b84 aurel32
    target_ulong entry=0;
163 1db09b84 aurel32
    target_ulong loadaddr=UIMAGE_LOAD_BASE;
164 1db09b84 aurel32
    target_long kernel_size=0;
165 1db09b84 aurel32
    target_ulong dt_base=DTB_LOAD_BASE;
166 1db09b84 aurel32
    target_ulong initrd_base=INITRD_LOAD_BASE;
167 1db09b84 aurel32
    target_long initrd_size=0;
168 1db09b84 aurel32
    void *fdt;
169 1db09b84 aurel32
    int i=0;
170 1db09b84 aurel32
    unsigned int pci_irq_nrs[4] = {1, 2, 3, 4};
171 1db09b84 aurel32
    qemu_irq *irqs, *mpic, *pci_irqs;
172 1db09b84 aurel32
    SerialState * serial[2];
173 1db09b84 aurel32
174 1db09b84 aurel32
    /* Setup CPU */
175 1db09b84 aurel32
    env = cpu_ppc_init("e500v2_v30");
176 1db09b84 aurel32
    if (!env) {
177 1db09b84 aurel32
        fprintf(stderr, "Unable to initialize CPU!\n");
178 1db09b84 aurel32
        exit(1);
179 1db09b84 aurel32
    }
180 1db09b84 aurel32
181 1db09b84 aurel32
    /* Fixup Memory size on a alignment boundary */
182 1db09b84 aurel32
    ram_size &= ~(RAM_SIZES_ALIGN - 1);
183 1db09b84 aurel32
184 1db09b84 aurel32
    /* Register Memory */
185 1db09b84 aurel32
    cpu_register_physical_memory(0, ram_size, 0);
186 1db09b84 aurel32
187 1db09b84 aurel32
    /* MPIC */
188 1db09b84 aurel32
    irqs = qemu_mallocz(sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
189 1db09b84 aurel32
    irqs[OPENPIC_OUTPUT_INT] = ((qemu_irq *)env->irq_inputs)[PPCE500_INPUT_INT];
190 1db09b84 aurel32
    irqs[OPENPIC_OUTPUT_CINT] = ((qemu_irq *)env->irq_inputs)[PPCE500_INPUT_CINT];
191 1db09b84 aurel32
    mpic = mpic_init(MPC8544_MPIC_REGS_BASE, 1, &irqs, NULL);
192 1db09b84 aurel32
193 1db09b84 aurel32
    /* Serial */
194 1db09b84 aurel32
    if (serial_hds[0])
195 1db09b84 aurel32
        serial[0] = serial_mm_init(MPC8544_SERIAL0_REGS_BASE,
196 1db09b84 aurel32
                               0, mpic[12+26], 399193,
197 1db09b84 aurel32
                        serial_hds[0], 1);
198 1db09b84 aurel32
199 1db09b84 aurel32
    if (serial_hds[1])
200 1db09b84 aurel32
        serial[0] = serial_mm_init(MPC8544_SERIAL1_REGS_BASE,
201 1db09b84 aurel32
                        0, mpic[12+26], 399193,
202 1db09b84 aurel32
                        serial_hds[0], 1);
203 1db09b84 aurel32
204 1db09b84 aurel32
    /* PCI */
205 1db09b84 aurel32
    pci_irqs = qemu_malloc(sizeof(qemu_irq) * 4);
206 1db09b84 aurel32
    pci_irqs[0] = mpic[pci_irq_nrs[0]];
207 1db09b84 aurel32
    pci_irqs[1] = mpic[pci_irq_nrs[1]];
208 1db09b84 aurel32
    pci_irqs[2] = mpic[pci_irq_nrs[2]];
209 1db09b84 aurel32
    pci_irqs[3] = mpic[pci_irq_nrs[3]];
210 1db09b84 aurel32
    pci_bus = ppce500_pci_init(pci_irqs, MPC8544_PCI_REGS_BASE);
211 1db09b84 aurel32
    if (!pci_bus)
212 1db09b84 aurel32
        printf("couldn't create PCI controller!\n");
213 1db09b84 aurel32
214 1db09b84 aurel32
    isa_mmio_init(MPC8544_PCI_IO, MPC8544_PCI_IOLEN);
215 1db09b84 aurel32
216 1db09b84 aurel32
    if (pci_bus) {
217 1db09b84 aurel32
        int unit_id = 0;
218 1db09b84 aurel32
219 1db09b84 aurel32
        /* Add virtio block devices. */
220 1db09b84 aurel32
        while ((i = drive_get_index(IF_VIRTIO, 0, unit_id)) != -1) {
221 1db09b84 aurel32
            virtio_blk_init(pci_bus, drives_table[i].bdrv);
222 1db09b84 aurel32
            unit_id++;
223 1db09b84 aurel32
        }
224 1db09b84 aurel32
225 1db09b84 aurel32
        /* Register network interfaces. */
226 1db09b84 aurel32
        for (i = 0; i < nb_nics; i++) {
227 1db09b84 aurel32
            pci_nic_init(pci_bus, &nd_table[i], -1, "virtio");
228 1db09b84 aurel32
        }
229 1db09b84 aurel32
    }
230 1db09b84 aurel32
231 1db09b84 aurel32
    /* Load kernel. */
232 1db09b84 aurel32
    if (kernel_filename) {
233 1db09b84 aurel32
        kernel_size = load_uimage(kernel_filename, &entry, &loadaddr, NULL);
234 1db09b84 aurel32
        if (kernel_size < 0) {
235 1db09b84 aurel32
            kernel_size = load_elf(kernel_filename, 0, &elf_entry, &elf_lowaddr,
236 1db09b84 aurel32
                                   NULL);
237 1db09b84 aurel32
            entry = elf_entry;
238 1db09b84 aurel32
            loadaddr = elf_lowaddr;
239 1db09b84 aurel32
        }
240 1db09b84 aurel32
        /* XXX try again as binary */
241 1db09b84 aurel32
        if (kernel_size < 0) {
242 1db09b84 aurel32
            fprintf(stderr, "qemu: could not load kernel '%s'\n",
243 1db09b84 aurel32
                    kernel_filename);
244 1db09b84 aurel32
            exit(1);
245 1db09b84 aurel32
        }
246 1db09b84 aurel32
    }
247 1db09b84 aurel32
248 1db09b84 aurel32
    /* Load initrd. */
249 1db09b84 aurel32
    if (initrd_filename) {
250 1db09b84 aurel32
        initrd_size = load_image(initrd_filename, phys_ram_base + initrd_base);
251 1db09b84 aurel32
252 1db09b84 aurel32
        if (initrd_size < 0) {
253 1db09b84 aurel32
            fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
254 1db09b84 aurel32
                    initrd_filename);
255 1db09b84 aurel32
            exit(1);
256 1db09b84 aurel32
        }
257 1db09b84 aurel32
    }
258 1db09b84 aurel32
259 1db09b84 aurel32
    /* If we're loading a kernel directly, we must load the device tree too. */
260 1db09b84 aurel32
    if (kernel_filename) {
261 1db09b84 aurel32
        fdt = mpc8544_load_device_tree(phys_ram_base + dt_base, ram_size,
262 1db09b84 aurel32
                                      initrd_base, initrd_size, kernel_cmdline);
263 1db09b84 aurel32
        if (fdt == NULL) {
264 1db09b84 aurel32
            fprintf(stderr, "couldn't load device tree\n");
265 1db09b84 aurel32
            exit(1);
266 1db09b84 aurel32
        }
267 1db09b84 aurel32
268 1db09b84 aurel32
        /* Set initial guest state. */
269 1db09b84 aurel32
        env->gpr[1] = (16<<20) - 8;
270 1db09b84 aurel32
        env->gpr[3] = dt_base;
271 1db09b84 aurel32
        env->nip = entry;
272 1db09b84 aurel32
        /* XXX we currently depend on KVM to create some initial TLB entries. */
273 1db09b84 aurel32
    }
274 1db09b84 aurel32
275 1db09b84 aurel32
    if (kvm_enabled())
276 1db09b84 aurel32
        kvmppc_init();
277 1db09b84 aurel32
278 1db09b84 aurel32
    return;
279 1db09b84 aurel32
}
280 1db09b84 aurel32
281 1db09b84 aurel32
QEMUMachine mpc8544ds_machine = {
282 1db09b84 aurel32
    .name = "mpc8544ds",
283 1db09b84 aurel32
    .desc = "mpc8544ds",
284 1db09b84 aurel32
    .init = mpc8544ds_init,
285 1db09b84 aurel32
    .ram_require = RAM_SIZES_ALIGN | RAMSIZE_FIXED,
286 1db09b84 aurel32
};