powerpc/booke: move fdt loading to rom infrastructure
[qemu/ar7.git] / hw / ppc440_bamboo.c
blobdce37219c8a41ad3306123bcb7ad35a73405a61a
1 /*
2 * Qemu PowerPC 440 Bamboo board emulation
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>
10 * This work is licensed under the GNU GPL license version 2 or later.
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 "sysemu.h"
21 #include "ppc440.h"
22 #include "kvm.h"
23 #include "kvm_ppc.h"
24 #include "device_tree.h"
25 #include "loader.h"
26 #include "elf.h"
28 #define BINARY_DEVICE_TREE_FILE "bamboo.dtb"
30 static int bamboo_load_device_tree(target_phys_addr_t addr,
31 uint32_t ramsize,
32 target_phys_addr_t initrd_base,
33 target_phys_addr_t initrd_size,
34 const char *kernel_cmdline)
36 void *fdt = NULL;
37 #ifdef CONFIG_FDT
38 uint32_t mem_reg_property[] = { 0, 0, ramsize };
39 char *filename;
40 int fdt_size;
41 int ret;
43 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
44 if (!filename) {
45 ret = -1;
46 goto out;
48 fdt = load_device_tree(filename, &fdt_size);
49 qemu_free(filename);
50 if (fdt == NULL) {
51 ret = -1;
52 goto out;
55 /* Manipulate device tree in memory. */
57 ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
58 sizeof(mem_reg_property));
59 if (ret < 0)
60 fprintf(stderr, "couldn't set /memory/reg\n");
62 ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
63 initrd_base);
64 if (ret < 0)
65 fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
67 ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
68 (initrd_base + initrd_size));
69 if (ret < 0)
70 fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
72 ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
73 kernel_cmdline);
74 if (ret < 0)
75 fprintf(stderr, "couldn't set /chosen/bootargs\n");
77 if (kvm_enabled())
78 kvmppc_fdt_update(fdt);
80 ret = rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
81 qemu_free(fdt);
83 out:
84 #endif
86 return ret;
89 static void bamboo_init(ram_addr_t ram_size,
90 const char *boot_device,
91 const char *kernel_filename,
92 const char *kernel_cmdline,
93 const char *initrd_filename,
94 const char *cpu_model)
96 unsigned int pci_irq_nrs[4] = { 28, 27, 26, 25 };
97 PCIBus *pcibus;
98 CPUState *env;
99 uint64_t elf_entry;
100 uint64_t elf_lowaddr;
101 target_phys_addr_t entry = 0;
102 target_phys_addr_t loadaddr = 0;
103 target_long kernel_size = 0;
104 target_ulong initrd_base = 0;
105 target_long initrd_size = 0;
106 target_ulong dt_base = 0;
107 int i;
109 /* Setup CPU. */
110 env = ppc440ep_init(&ram_size, &pcibus, pci_irq_nrs, 1, cpu_model);
112 if (pcibus) {
113 /* Register network interfaces. */
114 for (i = 0; i < nb_nics; i++) {
115 /* There are no PCI NICs on the Bamboo board, but there are
116 * PCI slots, so we can pick whatever default model we want. */
117 pci_nic_init_nofail(&nd_table[i], "e1000", NULL);
121 /* Load kernel. */
122 if (kernel_filename) {
123 kernel_size = load_uimage(kernel_filename, &entry, &loadaddr, NULL);
124 if (kernel_size < 0) {
125 kernel_size = load_elf(kernel_filename, 0, &elf_entry, &elf_lowaddr,
126 NULL, 1, ELF_MACHINE, 0);
127 entry = elf_entry;
128 loadaddr = elf_lowaddr;
130 /* XXX try again as binary */
131 if (kernel_size < 0) {
132 fprintf(stderr, "qemu: could not load kernel '%s'\n",
133 kernel_filename);
134 exit(1);
138 /* Load initrd. */
139 if (initrd_filename) {
140 initrd_base = kernel_size + loadaddr;
141 initrd_size = load_image_targphys(initrd_filename, initrd_base,
142 ram_size - initrd_base);
144 if (initrd_size < 0) {
145 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
146 initrd_filename);
147 exit(1);
151 /* If we're loading a kernel directly, we must load the device tree too. */
152 if (kernel_filename) {
153 if (initrd_base)
154 dt_base = initrd_base + initrd_size;
155 else
156 dt_base = kernel_size + loadaddr;
158 if (bamboo_load_device_tree(dt_base, ram_size,
159 initrd_base, initrd_size, kernel_cmdline) < 0) {
160 fprintf(stderr, "couldn't load device tree\n");
161 exit(1);
164 cpu_synchronize_state(env);
166 /* Set initial guest state. */
167 env->gpr[1] = (16<<20) - 8;
168 env->gpr[3] = dt_base;
169 env->nip = entry;
170 /* XXX we currently depend on KVM to create some initial TLB entries. */
173 if (kvm_enabled())
174 kvmppc_init();
177 static QEMUMachine bamboo_machine = {
178 .name = "bamboo",
179 .desc = "bamboo",
180 .init = bamboo_init,
183 static void bamboo_machine_init(void)
185 qemu_register_machine(&bamboo_machine);
188 machine_init(bamboo_machine_init);