]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppce500_mpc8544ds.c
powerpc: fix compilation with CONFIG_FDT undefined
[mirror_qemu.git] / hw / ppce500_mpc8544ds.c
CommitLineData
1db09b84
AJ
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 <dirent.h>
18
19#include "config.h"
20#include "qemu-common.h"
21#include "net.h"
22#include "hw.h"
23#include "pc.h"
24#include "pci.h"
1db09b84
AJ
25#include "boards.h"
26#include "sysemu.h"
27#include "kvm.h"
28#include "kvm_ppc.h"
29#include "device_tree.h"
30#include "openpic.h"
31#include "ppce500.h"
ca20cf32
BS
32#include "loader.h"
33#include "elf.h"
1db09b84
AJ
34
35#define BINARY_DEVICE_TREE_FILE "mpc8544ds.dtb"
36#define UIMAGE_LOAD_BASE 0
37#define DTB_LOAD_BASE 0x600000
38#define INITRD_LOAD_BASE 0x2000000
39
40#define RAM_SIZES_ALIGN (64UL << 20)
41
42#define MPC8544_CCSRBAR_BASE 0xE0000000
43#define MPC8544_MPIC_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x40000)
44#define MPC8544_SERIAL0_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x4500)
45#define MPC8544_SERIAL1_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x4600)
46#define MPC8544_PCI_REGS_BASE (MPC8544_CCSRBAR_BASE + 0x8000)
47#define MPC8544_PCI_REGS_SIZE 0x1000
48#define MPC8544_PCI_IO 0xE1000000
49#define MPC8544_PCI_IOLEN 0x10000
50
3f0855b1 51#ifdef CONFIG_FDT
1db09b84
AJ
52static int mpc8544_copy_soc_cell(void *fdt, const char *node, const char *prop)
53{
54 uint32_t cell;
55 int ret;
56
57 ret = kvmppc_read_host_property(node, prop, &cell, sizeof(cell));
58 if (ret < 0) {
59 fprintf(stderr, "couldn't read host %s/%s\n", node, prop);
60 goto out;
61 }
62
63 ret = qemu_devtree_setprop_cell(fdt, "/cpus/PowerPC,8544@0",
64 prop, cell);
65 if (ret < 0) {
66 fprintf(stderr, "couldn't set guest /cpus/PowerPC,8544@0/%s\n", prop);
67 goto out;
68 }
69
70out:
71 return ret;
72}
511d2b14 73#endif
1db09b84 74
04088adb 75static int mpc8544_load_device_tree(target_phys_addr_t addr,
1db09b84 76 uint32_t ramsize,
c227f099
AL
77 target_phys_addr_t initrd_base,
78 target_phys_addr_t initrd_size,
1db09b84
AJ
79 const char *kernel_cmdline)
80{
dbf916d8 81 int ret = -1;
3f0855b1 82#ifdef CONFIG_FDT
1db09b84 83 uint32_t mem_reg_property[] = {0, ramsize};
5cea8590 84 char *filename;
7ec632b4 85 int fdt_size;
dbf916d8 86 void *fdt;
1db09b84 87
5cea8590
PB
88 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
89 if (!filename) {
1db09b84 90 goto out;
5cea8590
PB
91 }
92 fdt = load_device_tree(filename, &fdt_size);
93 qemu_free(filename);
94 if (fdt == NULL) {
95 goto out;
96 }
1db09b84
AJ
97
98 /* Manipulate device tree in memory. */
99 ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
100 sizeof(mem_reg_property));
101 if (ret < 0)
102 fprintf(stderr, "couldn't set /memory/reg\n");
103
104 ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
105 initrd_base);
106 if (ret < 0)
107 fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
108
109 ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
110 (initrd_base + initrd_size));
111 if (ret < 0)
112 fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
113
114 ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
115 kernel_cmdline);
116 if (ret < 0)
117 fprintf(stderr, "couldn't set /chosen/bootargs\n");
118
119 if (kvm_enabled()) {
120 struct dirent *dirp;
121 DIR *dp;
122 char buf[128];
123
124 if ((dp = opendir("/proc/device-tree/cpus/")) == NULL) {
125 printf("Can't open directory /proc/device-tree/cpus/\n");
04088adb 126 ret = -1;
1db09b84
AJ
127 goto out;
128 }
129
130 buf[0] = '\0';
131 while ((dirp = readdir(dp)) != NULL) {
132 if (strncmp(dirp->d_name, "PowerPC", 7) == 0) {
133 snprintf(buf, 128, "/cpus/%s", dirp->d_name);
134 break;
135 }
136 }
137 closedir(dp);
138 if (buf[0] == '\0') {
139 printf("Unknow host!\n");
04088adb 140 ret = -1;
1db09b84
AJ
141 goto out;
142 }
143
144 mpc8544_copy_soc_cell(fdt, buf, "clock-frequency");
145 mpc8544_copy_soc_cell(fdt, buf, "timebase-frequency");
146 }
147
04088adb
LY
148 ret = rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
149 qemu_free(fdt);
7ec632b4 150
1db09b84
AJ
151out:
152#endif
153
04088adb 154 return ret;
1db09b84
AJ
155}
156
c227f099 157static void mpc8544ds_init(ram_addr_t ram_size,
1db09b84
AJ
158 const char *boot_device,
159 const char *kernel_filename,
160 const char *kernel_cmdline,
161 const char *initrd_filename,
162 const char *cpu_model)
163{
164 PCIBus *pci_bus;
165 CPUState *env;
166 uint64_t elf_entry;
167 uint64_t elf_lowaddr;
c227f099
AL
168 target_phys_addr_t entry=0;
169 target_phys_addr_t loadaddr=UIMAGE_LOAD_BASE;
1db09b84
AJ
170 target_long kernel_size=0;
171 target_ulong dt_base=DTB_LOAD_BASE;
172 target_ulong initrd_base=INITRD_LOAD_BASE;
173 target_long initrd_size=0;
1db09b84
AJ
174 int i=0;
175 unsigned int pci_irq_nrs[4] = {1, 2, 3, 4};
176 qemu_irq *irqs, *mpic, *pci_irqs;
177 SerialState * serial[2];
178
179 /* Setup CPU */
180 env = cpu_ppc_init("e500v2_v30");
181 if (!env) {
182 fprintf(stderr, "Unable to initialize CPU!\n");
183 exit(1);
184 }
185
186 /* Fixup Memory size on a alignment boundary */
187 ram_size &= ~(RAM_SIZES_ALIGN - 1);
188
189 /* Register Memory */
d7585251 190 cpu_register_physical_memory(0, ram_size, qemu_ram_alloc(ram_size));
1db09b84
AJ
191
192 /* MPIC */
193 irqs = qemu_mallocz(sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
194 irqs[OPENPIC_OUTPUT_INT] = ((qemu_irq *)env->irq_inputs)[PPCE500_INPUT_INT];
195 irqs[OPENPIC_OUTPUT_CINT] = ((qemu_irq *)env->irq_inputs)[PPCE500_INPUT_CINT];
196 mpic = mpic_init(MPC8544_MPIC_REGS_BASE, 1, &irqs, NULL);
197
198 /* Serial */
199 if (serial_hds[0])
200 serial[0] = serial_mm_init(MPC8544_SERIAL0_REGS_BASE,
201 0, mpic[12+26], 399193,
202 serial_hds[0], 1);
203
204 if (serial_hds[1])
205 serial[0] = serial_mm_init(MPC8544_SERIAL1_REGS_BASE,
206 0, mpic[12+26], 399193,
207 serial_hds[0], 1);
208
209 /* PCI */
210 pci_irqs = qemu_malloc(sizeof(qemu_irq) * 4);
211 pci_irqs[0] = mpic[pci_irq_nrs[0]];
212 pci_irqs[1] = mpic[pci_irq_nrs[1]];
213 pci_irqs[2] = mpic[pci_irq_nrs[2]];
214 pci_irqs[3] = mpic[pci_irq_nrs[3]];
215 pci_bus = ppce500_pci_init(pci_irqs, MPC8544_PCI_REGS_BASE);
216 if (!pci_bus)
217 printf("couldn't create PCI controller!\n");
218
219 isa_mmio_init(MPC8544_PCI_IO, MPC8544_PCI_IOLEN);
220
221 if (pci_bus) {
1db09b84
AJ
222 /* Register network interfaces. */
223 for (i = 0; i < nb_nics; i++) {
07caea31 224 pci_nic_init_nofail(&nd_table[i], "virtio", NULL);
1db09b84
AJ
225 }
226 }
227
228 /* Load kernel. */
229 if (kernel_filename) {
230 kernel_size = load_uimage(kernel_filename, &entry, &loadaddr, NULL);
231 if (kernel_size < 0) {
232 kernel_size = load_elf(kernel_filename, 0, &elf_entry, &elf_lowaddr,
ca20cf32 233 NULL, 1, ELF_MACHINE, 0);
1db09b84
AJ
234 entry = elf_entry;
235 loadaddr = elf_lowaddr;
236 }
237 /* XXX try again as binary */
238 if (kernel_size < 0) {
239 fprintf(stderr, "qemu: could not load kernel '%s'\n",
240 kernel_filename);
241 exit(1);
242 }
243 }
244
245 /* Load initrd. */
246 if (initrd_filename) {
d7585251
PB
247 initrd_size = load_image_targphys(initrd_filename, initrd_base,
248 ram_size - initrd_base);
1db09b84
AJ
249
250 if (initrd_size < 0) {
251 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
252 initrd_filename);
253 exit(1);
254 }
255 }
256
257 /* If we're loading a kernel directly, we must load the device tree too. */
258 if (kernel_filename) {
04088adb
LY
259 if (mpc8544_load_device_tree(dt_base, ram_size,
260 initrd_base, initrd_size, kernel_cmdline) < 0) {
1db09b84
AJ
261 fprintf(stderr, "couldn't load device tree\n");
262 exit(1);
263 }
264
c4963817
LY
265 cpu_synchronize_state(env);
266
1db09b84
AJ
267 /* Set initial guest state. */
268 env->gpr[1] = (16<<20) - 8;
269 env->gpr[3] = dt_base;
270 env->nip = entry;
271 /* XXX we currently depend on KVM to create some initial TLB entries. */
272 }
273
274 if (kvm_enabled())
275 kvmppc_init();
276
277 return;
278}
279
f80f9ec9 280static QEMUMachine mpc8544ds_machine = {
1db09b84
AJ
281 .name = "mpc8544ds",
282 .desc = "mpc8544ds",
283 .init = mpc8544ds_init,
1db09b84 284};
f80f9ec9
AL
285
286static void mpc8544ds_machine_init(void)
287{
288 qemu_register_machine(&mpc8544ds_machine);
289}
290
291machine_init(mpc8544ds_machine_init);