]> git.proxmox.com Git - mirror_qemu.git/blob - hw/ppc440_bamboo.c
PPC: bamboo: Move host fdt copy to target
[mirror_qemu.git] / hw / ppc440_bamboo.c
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
27 #define BINARY_DEVICE_TREE_FILE "bamboo.dtb"
28
29 /* from u-boot */
30 #define KERNEL_ADDR 0x1000000
31 #define FDT_ADDR 0x1800000
32 #define RAMDISK_ADDR 0x1900000
33
34 #ifdef CONFIG_FDT
35 static int bamboo_copy_host_cell(void *fdt, const char *node, const char *prop)
36 {
37 uint32_t cell;
38 int ret;
39
40 ret = kvmppc_read_host_property(node, prop, &cell, sizeof(cell));
41 if (ret < 0) {
42 fprintf(stderr, "couldn't read host %s/%s\n", node, prop);
43 goto out;
44 }
45
46 ret = qemu_devtree_setprop_cell(fdt, node, prop, cell);
47 if (ret < 0) {
48 fprintf(stderr, "couldn't set guest %s/%s\n", node, prop);
49 goto out;
50 }
51
52 out:
53 return ret;
54 }
55
56 static void bamboo_fdt_update(void *fdt)
57 {
58 /* Copy data from the host device tree into the guest. Since the guest can
59 * directly access the timebase without host involvement, we must expose
60 * the correct frequencies. */
61 bamboo_copy_host_cell(fdt, "/cpus/cpu@0", "clock-frequency");
62 bamboo_copy_host_cell(fdt, "/cpus/cpu@0", "timebase-frequency");
63 }
64 #endif
65
66 static int bamboo_load_device_tree(target_phys_addr_t addr,
67 uint32_t ramsize,
68 target_phys_addr_t initrd_base,
69 target_phys_addr_t initrd_size,
70 const char *kernel_cmdline)
71 {
72 int ret = -1;
73 #ifdef CONFIG_FDT
74 uint32_t mem_reg_property[] = { 0, 0, ramsize };
75 char *filename;
76 int fdt_size;
77 void *fdt;
78
79 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
80 if (!filename) {
81 goto out;
82 }
83 fdt = load_device_tree(filename, &fdt_size);
84 g_free(filename);
85 if (fdt == NULL) {
86 goto out;
87 }
88
89 /* Manipulate device tree in memory. */
90
91 ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
92 sizeof(mem_reg_property));
93 if (ret < 0)
94 fprintf(stderr, "couldn't set /memory/reg\n");
95
96 ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
97 initrd_base);
98 if (ret < 0)
99 fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
100
101 ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
102 (initrd_base + initrd_size));
103 if (ret < 0)
104 fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
105
106 ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
107 kernel_cmdline);
108 if (ret < 0)
109 fprintf(stderr, "couldn't set /chosen/bootargs\n");
110
111 if (kvm_enabled()) {
112 bamboo_fdt_update(fdt);
113 }
114
115 ret = rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
116 g_free(fdt);
117
118 out:
119 #endif
120
121 return ret;
122 }
123
124 static void bamboo_init(ram_addr_t ram_size,
125 const char *boot_device,
126 const char *kernel_filename,
127 const char *kernel_cmdline,
128 const char *initrd_filename,
129 const char *cpu_model)
130 {
131 unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
132 PCIBus *pcibus;
133 CPUState *env;
134 uint64_t elf_entry;
135 uint64_t elf_lowaddr;
136 target_phys_addr_t entry = 0;
137 target_phys_addr_t loadaddr = 0;
138 target_long initrd_size = 0;
139 int success;
140 int i;
141
142 /* Setup CPU. */
143 env = ppc440ep_init(&ram_size, &pcibus, pci_irq_nrs, 1, cpu_model);
144
145 if (pcibus) {
146 /* Register network interfaces. */
147 for (i = 0; i < nb_nics; i++) {
148 /* There are no PCI NICs on the Bamboo board, but there are
149 * PCI slots, so we can pick whatever default model we want. */
150 pci_nic_init_nofail(&nd_table[i], "e1000", NULL);
151 }
152 }
153
154 /* Load kernel. */
155 if (kernel_filename) {
156 success = load_uimage(kernel_filename, &entry, &loadaddr, NULL);
157 if (success < 0) {
158 success = load_elf(kernel_filename, NULL, NULL, &elf_entry,
159 &elf_lowaddr, NULL, 1, ELF_MACHINE, 0);
160 entry = elf_entry;
161 loadaddr = elf_lowaddr;
162 }
163 /* XXX try again as binary */
164 if (success < 0) {
165 fprintf(stderr, "qemu: could not load kernel '%s'\n",
166 kernel_filename);
167 exit(1);
168 }
169 }
170
171 /* Load initrd. */
172 if (initrd_filename) {
173 initrd_size = load_image_targphys(initrd_filename, RAMDISK_ADDR,
174 ram_size - RAMDISK_ADDR);
175
176 if (initrd_size < 0) {
177 fprintf(stderr, "qemu: could not load ram disk '%s' at %x\n",
178 initrd_filename, RAMDISK_ADDR);
179 exit(1);
180 }
181 }
182
183 /* If we're loading a kernel directly, we must load the device tree too. */
184 if (kernel_filename) {
185 if (bamboo_load_device_tree(FDT_ADDR, ram_size, RAMDISK_ADDR,
186 initrd_size, kernel_cmdline) < 0) {
187 fprintf(stderr, "couldn't load device tree\n");
188 exit(1);
189 }
190
191 /* Set initial guest state. */
192 env->gpr[1] = (16<<20) - 8;
193 env->gpr[3] = FDT_ADDR;
194 env->nip = entry;
195 /* XXX we currently depend on KVM to create some initial TLB entries. */
196 }
197
198 if (kvm_enabled())
199 kvmppc_init();
200 }
201
202 static QEMUMachine bamboo_machine = {
203 .name = "bamboo-0.13",
204 .alias = "bamboo",
205 .desc = "bamboo",
206 .init = bamboo_init,
207 };
208
209 static QEMUMachine bamboo_machine_v0_12 = {
210 .name = "bamboo-0.12",
211 .desc = "bamboo",
212 .init = bamboo_init,
213 .compat_props = (GlobalProperty[]) {
214 {
215 .driver = "virtio-serial-pci",
216 .property = "max_ports",
217 .value = stringify(1),
218 },{
219 .driver = "virtio-serial-pci",
220 .property = "vectors",
221 .value = stringify(0),
222 },
223 { /* end of list */ }
224 },
225 };
226
227 static void bamboo_machine_init(void)
228 {
229 qemu_register_machine(&bamboo_machine);
230 qemu_register_machine(&bamboo_machine_v0_12);
231 }
232
233 machine_init(bamboo_machine_init);