xref: /openbmc/qemu/hw/ppc/e500.c (revision 2993683b)
1 /*
2  * QEMU PowerPC e500-based platforms
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 "config.h"
18 #include "qemu-common.h"
19 #include "e500.h"
20 #include "e500-ccsr.h"
21 #include "net/net.h"
22 #include "qemu/config-file.h"
23 #include "hw/hw.h"
24 #include "hw/char/serial.h"
25 #include "hw/pci/pci.h"
26 #include "hw/boards.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/kvm.h"
29 #include "kvm_ppc.h"
30 #include "sysemu/device_tree.h"
31 #include "hw/ppc/openpic.h"
32 #include "hw/ppc/ppc.h"
33 #include "hw/loader.h"
34 #include "elf.h"
35 #include "hw/sysbus.h"
36 #include "exec/address-spaces.h"
37 #include "qemu/host-utils.h"
38 #include "hw/pci-host/ppce500.h"
39 
40 #define BINARY_DEVICE_TREE_FILE    "mpc8544ds.dtb"
41 #define UIMAGE_LOAD_BASE           0
42 #define DTC_LOAD_PAD               0x1800000
43 #define DTC_PAD_MASK               0xFFFFF
44 #define DTB_MAX_SIZE               (8 * 1024 * 1024)
45 #define INITRD_LOAD_PAD            0x2000000
46 #define INITRD_PAD_MASK            0xFFFFFF
47 
48 #define RAM_SIZES_ALIGN            (64UL << 20)
49 
50 /* TODO: parameterize */
51 #define MPC8544_CCSRBAR_BASE       0xE0000000ULL
52 #define MPC8544_CCSRBAR_SIZE       0x00100000ULL
53 #define MPC8544_MPIC_REGS_OFFSET   0x40000ULL
54 #define MPC8544_MSI_REGS_OFFSET   0x41600ULL
55 #define MPC8544_SERIAL0_REGS_OFFSET 0x4500ULL
56 #define MPC8544_SERIAL1_REGS_OFFSET 0x4600ULL
57 #define MPC8544_PCI_REGS_OFFSET    0x8000ULL
58 #define MPC8544_PCI_REGS_BASE      (MPC8544_CCSRBAR_BASE + \
59                                     MPC8544_PCI_REGS_OFFSET)
60 #define MPC8544_PCI_REGS_SIZE      0x1000ULL
61 #define MPC8544_PCI_IO             0xE1000000ULL
62 #define MPC8544_UTIL_OFFSET        0xe0000ULL
63 #define MPC8544_SPIN_BASE          0xEF000000ULL
64 
65 struct boot_info
66 {
67     uint32_t dt_base;
68     uint32_t dt_size;
69     uint32_t entry;
70 };
71 
72 static uint32_t *pci_map_create(void *fdt, uint32_t mpic, int first_slot,
73                                 int nr_slots, int *len)
74 {
75     int i = 0;
76     int slot;
77     int pci_irq;
78     int host_irq;
79     int last_slot = first_slot + nr_slots;
80     uint32_t *pci_map;
81 
82     *len = nr_slots * 4 * 7 * sizeof(uint32_t);
83     pci_map = g_malloc(*len);
84 
85     for (slot = first_slot; slot < last_slot; slot++) {
86         for (pci_irq = 0; pci_irq < 4; pci_irq++) {
87             pci_map[i++] = cpu_to_be32(slot << 11);
88             pci_map[i++] = cpu_to_be32(0x0);
89             pci_map[i++] = cpu_to_be32(0x0);
90             pci_map[i++] = cpu_to_be32(pci_irq + 1);
91             pci_map[i++] = cpu_to_be32(mpic);
92             host_irq = ppce500_pci_map_irq_slot(slot, pci_irq);
93             pci_map[i++] = cpu_to_be32(host_irq + 1);
94             pci_map[i++] = cpu_to_be32(0x1);
95         }
96     }
97 
98     assert((i * sizeof(uint32_t)) == *len);
99 
100     return pci_map;
101 }
102 
103 static void dt_serial_create(void *fdt, unsigned long long offset,
104                              const char *soc, const char *mpic,
105                              const char *alias, int idx, bool defcon)
106 {
107     char ser[128];
108 
109     snprintf(ser, sizeof(ser), "%s/serial@%llx", soc, offset);
110     qemu_devtree_add_subnode(fdt, ser);
111     qemu_devtree_setprop_string(fdt, ser, "device_type", "serial");
112     qemu_devtree_setprop_string(fdt, ser, "compatible", "ns16550");
113     qemu_devtree_setprop_cells(fdt, ser, "reg", offset, 0x100);
114     qemu_devtree_setprop_cell(fdt, ser, "cell-index", idx);
115     qemu_devtree_setprop_cell(fdt, ser, "clock-frequency", 0);
116     qemu_devtree_setprop_cells(fdt, ser, "interrupts", 42, 2);
117     qemu_devtree_setprop_phandle(fdt, ser, "interrupt-parent", mpic);
118     qemu_devtree_setprop_string(fdt, "/aliases", alias, ser);
119 
120     if (defcon) {
121         qemu_devtree_setprop_string(fdt, "/chosen", "linux,stdout-path", ser);
122     }
123 }
124 
125 static int ppce500_load_device_tree(CPUPPCState *env,
126                                     PPCE500Params *params,
127                                     hwaddr addr,
128                                     hwaddr initrd_base,
129                                     hwaddr initrd_size)
130 {
131     int ret = -1;
132     uint64_t mem_reg_property[] = { 0, cpu_to_be64(params->ram_size) };
133     int fdt_size;
134     void *fdt;
135     uint8_t hypercall[16];
136     uint32_t clock_freq = 400000000;
137     uint32_t tb_freq = 400000000;
138     int i;
139     const char *toplevel_compat = NULL; /* user override */
140     char compatible_sb[] = "fsl,mpc8544-immr\0simple-bus";
141     char soc[128];
142     char mpic[128];
143     uint32_t mpic_ph;
144     uint32_t msi_ph;
145     char gutil[128];
146     char pci[128];
147     char msi[128];
148     uint32_t *pci_map = NULL;
149     int len;
150     uint32_t pci_ranges[14] =
151         {
152             0x2000000, 0x0, 0xc0000000,
153             0x0, 0xc0000000,
154             0x0, 0x20000000,
155 
156             0x1000000, 0x0, 0x0,
157             0x0, 0xe1000000,
158             0x0, 0x10000,
159         };
160     QemuOpts *machine_opts;
161     const char *dtb_file = NULL;
162 
163     machine_opts = qemu_opts_find(qemu_find_opts("machine"), 0);
164     if (machine_opts) {
165         dtb_file = qemu_opt_get(machine_opts, "dtb");
166         toplevel_compat = qemu_opt_get(machine_opts, "dt_compatible");
167     }
168 
169     if (dtb_file) {
170         char *filename;
171         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_file);
172         if (!filename) {
173             goto out;
174         }
175 
176         fdt = load_device_tree(filename, &fdt_size);
177         if (!fdt) {
178             goto out;
179         }
180         goto done;
181     }
182 
183     fdt = create_device_tree(&fdt_size);
184     if (fdt == NULL) {
185         goto out;
186     }
187 
188     /* Manipulate device tree in memory. */
189     qemu_devtree_setprop_cell(fdt, "/", "#address-cells", 2);
190     qemu_devtree_setprop_cell(fdt, "/", "#size-cells", 2);
191 
192     qemu_devtree_add_subnode(fdt, "/memory");
193     qemu_devtree_setprop_string(fdt, "/memory", "device_type", "memory");
194     qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
195                          sizeof(mem_reg_property));
196 
197     qemu_devtree_add_subnode(fdt, "/chosen");
198     if (initrd_size) {
199         ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-start",
200                                         initrd_base);
201         if (ret < 0) {
202             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
203         }
204 
205         ret = qemu_devtree_setprop_cell(fdt, "/chosen", "linux,initrd-end",
206                                         (initrd_base + initrd_size));
207         if (ret < 0) {
208             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
209         }
210     }
211 
212     ret = qemu_devtree_setprop_string(fdt, "/chosen", "bootargs",
213                                       params->kernel_cmdline);
214     if (ret < 0)
215         fprintf(stderr, "couldn't set /chosen/bootargs\n");
216 
217     if (kvm_enabled()) {
218         /* Read out host's frequencies */
219         clock_freq = kvmppc_get_clockfreq();
220         tb_freq = kvmppc_get_tbfreq();
221 
222         /* indicate KVM hypercall interface */
223         qemu_devtree_add_subnode(fdt, "/hypervisor");
224         qemu_devtree_setprop_string(fdt, "/hypervisor", "compatible",
225                                     "linux,kvm");
226         kvmppc_get_hypercall(env, hypercall, sizeof(hypercall));
227         qemu_devtree_setprop(fdt, "/hypervisor", "hcall-instructions",
228                              hypercall, sizeof(hypercall));
229         /* if KVM supports the idle hcall, set property indicating this */
230         if (kvmppc_get_hasidle(env)) {
231             qemu_devtree_setprop(fdt, "/hypervisor", "has-idle", NULL, 0);
232         }
233     }
234 
235     /* Create CPU nodes */
236     qemu_devtree_add_subnode(fdt, "/cpus");
237     qemu_devtree_setprop_cell(fdt, "/cpus", "#address-cells", 1);
238     qemu_devtree_setprop_cell(fdt, "/cpus", "#size-cells", 0);
239 
240     /* We need to generate the cpu nodes in reverse order, so Linux can pick
241        the first node as boot node and be happy */
242     for (i = smp_cpus - 1; i >= 0; i--) {
243         CPUState *cpu;
244         char cpu_name[128];
245         uint64_t cpu_release_addr = MPC8544_SPIN_BASE + (i * 0x20);
246 
247         cpu = qemu_get_cpu(i);
248         if (cpu == NULL) {
249             continue;
250         }
251         env = cpu->env_ptr;
252 
253         snprintf(cpu_name, sizeof(cpu_name), "/cpus/PowerPC,8544@%x",
254                  cpu->cpu_index);
255         qemu_devtree_add_subnode(fdt, cpu_name);
256         qemu_devtree_setprop_cell(fdt, cpu_name, "clock-frequency", clock_freq);
257         qemu_devtree_setprop_cell(fdt, cpu_name, "timebase-frequency", tb_freq);
258         qemu_devtree_setprop_string(fdt, cpu_name, "device_type", "cpu");
259         qemu_devtree_setprop_cell(fdt, cpu_name, "reg", cpu->cpu_index);
260         qemu_devtree_setprop_cell(fdt, cpu_name, "d-cache-line-size",
261                                   env->dcache_line_size);
262         qemu_devtree_setprop_cell(fdt, cpu_name, "i-cache-line-size",
263                                   env->icache_line_size);
264         qemu_devtree_setprop_cell(fdt, cpu_name, "d-cache-size", 0x8000);
265         qemu_devtree_setprop_cell(fdt, cpu_name, "i-cache-size", 0x8000);
266         qemu_devtree_setprop_cell(fdt, cpu_name, "bus-frequency", 0);
267         if (cpu->cpu_index) {
268             qemu_devtree_setprop_string(fdt, cpu_name, "status", "disabled");
269             qemu_devtree_setprop_string(fdt, cpu_name, "enable-method", "spin-table");
270             qemu_devtree_setprop_u64(fdt, cpu_name, "cpu-release-addr",
271                                      cpu_release_addr);
272         } else {
273             qemu_devtree_setprop_string(fdt, cpu_name, "status", "okay");
274         }
275     }
276 
277     qemu_devtree_add_subnode(fdt, "/aliases");
278     /* XXX These should go into their respective devices' code */
279     snprintf(soc, sizeof(soc), "/soc@%llx", MPC8544_CCSRBAR_BASE);
280     qemu_devtree_add_subnode(fdt, soc);
281     qemu_devtree_setprop_string(fdt, soc, "device_type", "soc");
282     qemu_devtree_setprop(fdt, soc, "compatible", compatible_sb,
283                          sizeof(compatible_sb));
284     qemu_devtree_setprop_cell(fdt, soc, "#address-cells", 1);
285     qemu_devtree_setprop_cell(fdt, soc, "#size-cells", 1);
286     qemu_devtree_setprop_cells(fdt, soc, "ranges", 0x0,
287                                MPC8544_CCSRBAR_BASE >> 32, MPC8544_CCSRBAR_BASE,
288                                MPC8544_CCSRBAR_SIZE);
289     /* XXX should contain a reasonable value */
290     qemu_devtree_setprop_cell(fdt, soc, "bus-frequency", 0);
291 
292     snprintf(mpic, sizeof(mpic), "%s/pic@%llx", soc, MPC8544_MPIC_REGS_OFFSET);
293     qemu_devtree_add_subnode(fdt, mpic);
294     qemu_devtree_setprop_string(fdt, mpic, "device_type", "open-pic");
295     qemu_devtree_setprop_string(fdt, mpic, "compatible", "fsl,mpic");
296     qemu_devtree_setprop_cells(fdt, mpic, "reg", MPC8544_MPIC_REGS_OFFSET,
297                                0x40000);
298     qemu_devtree_setprop_cell(fdt, mpic, "#address-cells", 0);
299     qemu_devtree_setprop_cell(fdt, mpic, "#interrupt-cells", 2);
300     mpic_ph = qemu_devtree_alloc_phandle(fdt);
301     qemu_devtree_setprop_cell(fdt, mpic, "phandle", mpic_ph);
302     qemu_devtree_setprop_cell(fdt, mpic, "linux,phandle", mpic_ph);
303     qemu_devtree_setprop(fdt, mpic, "interrupt-controller", NULL, 0);
304 
305     /*
306      * We have to generate ser1 first, because Linux takes the first
307      * device it finds in the dt as serial output device. And we generate
308      * devices in reverse order to the dt.
309      */
310     dt_serial_create(fdt, MPC8544_SERIAL1_REGS_OFFSET,
311                      soc, mpic, "serial1", 1, false);
312     dt_serial_create(fdt, MPC8544_SERIAL0_REGS_OFFSET,
313                      soc, mpic, "serial0", 0, true);
314 
315     snprintf(gutil, sizeof(gutil), "%s/global-utilities@%llx", soc,
316              MPC8544_UTIL_OFFSET);
317     qemu_devtree_add_subnode(fdt, gutil);
318     qemu_devtree_setprop_string(fdt, gutil, "compatible", "fsl,mpc8544-guts");
319     qemu_devtree_setprop_cells(fdt, gutil, "reg", MPC8544_UTIL_OFFSET, 0x1000);
320     qemu_devtree_setprop(fdt, gutil, "fsl,has-rstcr", NULL, 0);
321 
322     snprintf(msi, sizeof(msi), "/%s/msi@%llx", soc, MPC8544_MSI_REGS_OFFSET);
323     qemu_devtree_add_subnode(fdt, msi);
324     qemu_devtree_setprop_string(fdt, msi, "compatible", "fsl,mpic-msi");
325     qemu_devtree_setprop_cells(fdt, msi, "reg", MPC8544_MSI_REGS_OFFSET, 0x200);
326     msi_ph = qemu_devtree_alloc_phandle(fdt);
327     qemu_devtree_setprop_cells(fdt, msi, "msi-available-ranges", 0x0, 0x100);
328     qemu_devtree_setprop_phandle(fdt, msi, "interrupt-parent", mpic);
329     qemu_devtree_setprop_cells(fdt, msi, "interrupts",
330         0xe0, 0x0,
331         0xe1, 0x0,
332         0xe2, 0x0,
333         0xe3, 0x0,
334         0xe4, 0x0,
335         0xe5, 0x0,
336         0xe6, 0x0,
337         0xe7, 0x0);
338     qemu_devtree_setprop_cell(fdt, msi, "phandle", msi_ph);
339     qemu_devtree_setprop_cell(fdt, msi, "linux,phandle", msi_ph);
340 
341     snprintf(pci, sizeof(pci), "/pci@%llx", MPC8544_PCI_REGS_BASE);
342     qemu_devtree_add_subnode(fdt, pci);
343     qemu_devtree_setprop_cell(fdt, pci, "cell-index", 0);
344     qemu_devtree_setprop_string(fdt, pci, "compatible", "fsl,mpc8540-pci");
345     qemu_devtree_setprop_string(fdt, pci, "device_type", "pci");
346     qemu_devtree_setprop_cells(fdt, pci, "interrupt-map-mask", 0xf800, 0x0,
347                                0x0, 0x7);
348     pci_map = pci_map_create(fdt, qemu_devtree_get_phandle(fdt, mpic),
349                              params->pci_first_slot, params->pci_nr_slots,
350                              &len);
351     qemu_devtree_setprop(fdt, pci, "interrupt-map", pci_map, len);
352     qemu_devtree_setprop_phandle(fdt, pci, "interrupt-parent", mpic);
353     qemu_devtree_setprop_cells(fdt, pci, "interrupts", 24, 2);
354     qemu_devtree_setprop_cells(fdt, pci, "bus-range", 0, 255);
355     for (i = 0; i < 14; i++) {
356         pci_ranges[i] = cpu_to_be32(pci_ranges[i]);
357     }
358     qemu_devtree_setprop_cell(fdt, pci, "fsl,msi", msi_ph);
359     qemu_devtree_setprop(fdt, pci, "ranges", pci_ranges, sizeof(pci_ranges));
360     qemu_devtree_setprop_cells(fdt, pci, "reg", MPC8544_PCI_REGS_BASE >> 32,
361                                MPC8544_PCI_REGS_BASE, 0, 0x1000);
362     qemu_devtree_setprop_cell(fdt, pci, "clock-frequency", 66666666);
363     qemu_devtree_setprop_cell(fdt, pci, "#interrupt-cells", 1);
364     qemu_devtree_setprop_cell(fdt, pci, "#size-cells", 2);
365     qemu_devtree_setprop_cell(fdt, pci, "#address-cells", 3);
366     qemu_devtree_setprop_string(fdt, "/aliases", "pci0", pci);
367 
368     params->fixup_devtree(params, fdt);
369 
370     if (toplevel_compat) {
371         qemu_devtree_setprop(fdt, "/", "compatible", toplevel_compat,
372                              strlen(toplevel_compat) + 1);
373     }
374 
375 done:
376     qemu_devtree_dumpdtb(fdt, fdt_size);
377     ret = rom_add_blob_fixed(BINARY_DEVICE_TREE_FILE, fdt, fdt_size, addr);
378     if (ret < 0) {
379         goto out;
380     }
381     g_free(fdt);
382     ret = fdt_size;
383 
384 out:
385     g_free(pci_map);
386 
387     return ret;
388 }
389 
390 /* Create -kernel TLB entries for BookE.  */
391 static inline hwaddr booke206_page_size_to_tlb(uint64_t size)
392 {
393     return 63 - clz64(size >> 10);
394 }
395 
396 static void mmubooke_create_initial_mapping(CPUPPCState *env)
397 {
398     struct boot_info *bi = env->load_info;
399     ppcmas_tlb_t *tlb = booke206_get_tlbm(env, 1, 0, 0);
400     hwaddr size, dt_end;
401     int ps;
402 
403     /* Our initial TLB entry needs to cover everything from 0 to
404        the device tree top */
405     dt_end = bi->dt_base + bi->dt_size;
406     ps = booke206_page_size_to_tlb(dt_end) + 1;
407     if (ps & 1) {
408         /* e500v2 can only do even TLB size bits */
409         ps++;
410     }
411     size = (ps << MAS1_TSIZE_SHIFT);
412     tlb->mas1 = MAS1_VALID | size;
413     tlb->mas2 = 0;
414     tlb->mas7_3 = 0;
415     tlb->mas7_3 |= MAS3_UR | MAS3_UW | MAS3_UX | MAS3_SR | MAS3_SW | MAS3_SX;
416 
417     env->tlb_dirty = true;
418 }
419 
420 static void ppce500_cpu_reset_sec(void *opaque)
421 {
422     PowerPCCPU *cpu = opaque;
423     CPUState *cs = CPU(cpu);
424     CPUPPCState *env = &cpu->env;
425 
426     cpu_reset(cs);
427 
428     /* Secondary CPU starts in halted state for now. Needs to change when
429        implementing non-kernel boot. */
430     cs->halted = 1;
431     env->exception_index = EXCP_HLT;
432 }
433 
434 static void ppce500_cpu_reset(void *opaque)
435 {
436     PowerPCCPU *cpu = opaque;
437     CPUState *cs = CPU(cpu);
438     CPUPPCState *env = &cpu->env;
439     struct boot_info *bi = env->load_info;
440 
441     cpu_reset(cs);
442 
443     /* Set initial guest state. */
444     cs->halted = 0;
445     env->gpr[1] = (16<<20) - 8;
446     env->gpr[3] = bi->dt_base;
447     env->nip = bi->entry;
448     mmubooke_create_initial_mapping(env);
449 }
450 
451 void ppce500_init(PPCE500Params *params)
452 {
453     MemoryRegion *address_space_mem = get_system_memory();
454     MemoryRegion *ram = g_new(MemoryRegion, 1);
455     PCIBus *pci_bus;
456     CPUPPCState *env = NULL;
457     uint64_t elf_entry;
458     uint64_t elf_lowaddr;
459     hwaddr entry=0;
460     hwaddr loadaddr=UIMAGE_LOAD_BASE;
461     target_long kernel_size=0;
462     target_ulong dt_base = 0;
463     target_ulong initrd_base = 0;
464     target_long initrd_size = 0;
465     target_ulong cur_base = 0;
466     int i = 0, j, k;
467     unsigned int pci_irq_nrs[4] = {1, 2, 3, 4};
468     qemu_irq **irqs, *mpic;
469     DeviceState *dev;
470     CPUPPCState *firstenv = NULL;
471     MemoryRegion *ccsr_addr_space;
472     SysBusDevice *s;
473     PPCE500CCSRState *ccsr;
474 
475     /* Setup CPUs */
476     if (params->cpu_model == NULL) {
477         params->cpu_model = "e500v2_v30";
478     }
479 
480     irqs = g_malloc0(smp_cpus * sizeof(qemu_irq *));
481     irqs[0] = g_malloc0(smp_cpus * sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
482     for (i = 0; i < smp_cpus; i++) {
483         PowerPCCPU *cpu;
484         CPUState *cs;
485         qemu_irq *input;
486 
487         cpu = cpu_ppc_init(params->cpu_model);
488         if (cpu == NULL) {
489             fprintf(stderr, "Unable to initialize CPU!\n");
490             exit(1);
491         }
492         env = &cpu->env;
493         cs = CPU(cpu);
494 
495         if (!firstenv) {
496             firstenv = env;
497         }
498 
499         irqs[i] = irqs[0] + (i * OPENPIC_OUTPUT_NB);
500         input = (qemu_irq *)env->irq_inputs;
501         irqs[i][OPENPIC_OUTPUT_INT] = input[PPCE500_INPUT_INT];
502         irqs[i][OPENPIC_OUTPUT_CINT] = input[PPCE500_INPUT_CINT];
503         env->spr[SPR_BOOKE_PIR] = cs->cpu_index = i;
504         env->mpic_iack = MPC8544_CCSRBAR_BASE +
505                          MPC8544_MPIC_REGS_OFFSET + 0xa0;
506 
507         ppc_booke_timers_init(cpu, 400000000, PPC_TIMER_E500);
508 
509         /* Register reset handler */
510         if (!i) {
511             /* Primary CPU */
512             struct boot_info *boot_info;
513             boot_info = g_malloc0(sizeof(struct boot_info));
514             qemu_register_reset(ppce500_cpu_reset, cpu);
515             env->load_info = boot_info;
516         } else {
517             /* Secondary CPUs */
518             qemu_register_reset(ppce500_cpu_reset_sec, cpu);
519         }
520     }
521 
522     env = firstenv;
523 
524     /* Fixup Memory size on a alignment boundary */
525     ram_size &= ~(RAM_SIZES_ALIGN - 1);
526 
527     /* Register Memory */
528     memory_region_init_ram(ram, "mpc8544ds.ram", ram_size);
529     vmstate_register_ram_global(ram);
530     memory_region_add_subregion(address_space_mem, 0, ram);
531 
532     dev = qdev_create(NULL, "e500-ccsr");
533     object_property_add_child(qdev_get_machine(), "e500-ccsr",
534                               OBJECT(dev), NULL);
535     qdev_init_nofail(dev);
536     ccsr = CCSR(dev);
537     ccsr_addr_space = &ccsr->ccsr_space;
538     memory_region_add_subregion(address_space_mem, MPC8544_CCSRBAR_BASE,
539                                 ccsr_addr_space);
540 
541     /* MPIC */
542     mpic = g_new(qemu_irq, 256);
543     dev = qdev_create(NULL, "openpic");
544     qdev_prop_set_uint32(dev, "nb_cpus", smp_cpus);
545     qdev_prop_set_uint32(dev, "model", params->mpic_version);
546     qdev_init_nofail(dev);
547     s = SYS_BUS_DEVICE(dev);
548 
549     k = 0;
550     for (i = 0; i < smp_cpus; i++) {
551         for (j = 0; j < OPENPIC_OUTPUT_NB; j++) {
552             sysbus_connect_irq(s, k++, irqs[i][j]);
553         }
554     }
555 
556     for (i = 0; i < 256; i++) {
557         mpic[i] = qdev_get_gpio_in(dev, i);
558     }
559 
560     memory_region_add_subregion(ccsr_addr_space, MPC8544_MPIC_REGS_OFFSET,
561                                 s->mmio[0].memory);
562 
563     /* Serial */
564     if (serial_hds[0]) {
565         serial_mm_init(ccsr_addr_space, MPC8544_SERIAL0_REGS_OFFSET,
566                        0, mpic[42], 399193,
567                        serial_hds[0], DEVICE_BIG_ENDIAN);
568     }
569 
570     if (serial_hds[1]) {
571         serial_mm_init(ccsr_addr_space, MPC8544_SERIAL1_REGS_OFFSET,
572                        0, mpic[42], 399193,
573                        serial_hds[1], DEVICE_BIG_ENDIAN);
574     }
575 
576     /* General Utility device */
577     dev = qdev_create(NULL, "mpc8544-guts");
578     qdev_init_nofail(dev);
579     s = SYS_BUS_DEVICE(dev);
580     memory_region_add_subregion(ccsr_addr_space, MPC8544_UTIL_OFFSET,
581                                 sysbus_mmio_get_region(s, 0));
582 
583     /* PCI */
584     dev = qdev_create(NULL, "e500-pcihost");
585     qdev_prop_set_uint32(dev, "first_slot", params->pci_first_slot);
586     qdev_init_nofail(dev);
587     s = SYS_BUS_DEVICE(dev);
588     sysbus_connect_irq(s, 0, mpic[pci_irq_nrs[0]]);
589     sysbus_connect_irq(s, 1, mpic[pci_irq_nrs[1]]);
590     sysbus_connect_irq(s, 2, mpic[pci_irq_nrs[2]]);
591     sysbus_connect_irq(s, 3, mpic[pci_irq_nrs[3]]);
592     memory_region_add_subregion(ccsr_addr_space, MPC8544_PCI_REGS_OFFSET,
593                                 sysbus_mmio_get_region(s, 0));
594 
595     pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
596     if (!pci_bus)
597         printf("couldn't create PCI controller!\n");
598 
599     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, MPC8544_PCI_IO);
600 
601     if (pci_bus) {
602         /* Register network interfaces. */
603         for (i = 0; i < nb_nics; i++) {
604             pci_nic_init_nofail(&nd_table[i], "virtio", NULL);
605         }
606     }
607 
608     /* Register spinning region */
609     sysbus_create_simple("e500-spin", MPC8544_SPIN_BASE, NULL);
610 
611     /* Load kernel. */
612     if (params->kernel_filename) {
613         kernel_size = load_uimage(params->kernel_filename, &entry,
614                                   &loadaddr, NULL);
615         if (kernel_size < 0) {
616             kernel_size = load_elf(params->kernel_filename, NULL, NULL,
617                                    &elf_entry, &elf_lowaddr, NULL, 1,
618                                    ELF_MACHINE, 0);
619             entry = elf_entry;
620             loadaddr = elf_lowaddr;
621         }
622         /* XXX try again as binary */
623         if (kernel_size < 0) {
624             fprintf(stderr, "qemu: could not load kernel '%s'\n",
625                     params->kernel_filename);
626             exit(1);
627         }
628 
629         cur_base = loadaddr + kernel_size;
630 
631         /* Reserve space for dtb */
632         dt_base = (cur_base + DTC_LOAD_PAD) & ~DTC_PAD_MASK;
633         cur_base += DTB_MAX_SIZE;
634     }
635 
636     /* Load initrd. */
637     if (params->initrd_filename) {
638         initrd_base = (cur_base + INITRD_LOAD_PAD) & ~INITRD_PAD_MASK;
639         initrd_size = load_image_targphys(params->initrd_filename, initrd_base,
640                                           ram_size - initrd_base);
641 
642         if (initrd_size < 0) {
643             fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
644                     params->initrd_filename);
645             exit(1);
646         }
647 
648         cur_base = initrd_base + initrd_size;
649     }
650 
651     /* If we're loading a kernel directly, we must load the device tree too. */
652     if (params->kernel_filename) {
653         struct boot_info *boot_info;
654         int dt_size;
655 
656         dt_size = ppce500_load_device_tree(env, params, dt_base, initrd_base,
657                                            initrd_size);
658         if (dt_size < 0) {
659             fprintf(stderr, "couldn't load device tree\n");
660             exit(1);
661         }
662         assert(dt_size < DTB_MAX_SIZE);
663 
664         boot_info = env->load_info;
665         boot_info->entry = entry;
666         boot_info->dt_base = dt_base;
667         boot_info->dt_size = dt_size;
668     }
669 
670     if (kvm_enabled()) {
671         kvmppc_init();
672     }
673 }
674 
675 static int e500_ccsr_initfn(SysBusDevice *dev)
676 {
677     PPCE500CCSRState *ccsr;
678 
679     ccsr = CCSR(dev);
680     memory_region_init(&ccsr->ccsr_space, "e500-ccsr",
681                        MPC8544_CCSRBAR_SIZE);
682     return 0;
683 }
684 
685 static void e500_ccsr_class_init(ObjectClass *klass, void *data)
686 {
687     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
688     k->init = e500_ccsr_initfn;
689 }
690 
691 static const TypeInfo e500_ccsr_info = {
692     .name          = TYPE_CCSR,
693     .parent        = TYPE_SYS_BUS_DEVICE,
694     .instance_size = sizeof(PPCE500CCSRState),
695     .class_init    = e500_ccsr_class_init,
696 };
697 
698 static void e500_register_types(void)
699 {
700     type_register_static(&e500_ccsr_info);
701 }
702 
703 type_init(e500_register_types)
704