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