xref: /openbmc/qemu/hw/ppc/e500.c (revision 7a6e869c)
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 "qemu/osdep.h"
18 #include "qemu/datadir.h"
19 #include "qemu/units.h"
20 #include "qemu/guest-random.h"
21 #include "qapi/error.h"
22 #include "e500.h"
23 #include "e500-ccsr.h"
24 #include "net/net.h"
25 #include "qemu/config-file.h"
26 #include "hw/block/flash.h"
27 #include "hw/char/serial.h"
28 #include "hw/pci/pci.h"
29 #include "sysemu/block-backend-io.h"
30 #include "sysemu/sysemu.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/reset.h"
33 #include "sysemu/runstate.h"
34 #include "kvm_ppc.h"
35 #include "sysemu/device_tree.h"
36 #include "hw/ppc/openpic.h"
37 #include "hw/ppc/openpic_kvm.h"
38 #include "hw/ppc/ppc.h"
39 #include "hw/qdev-properties.h"
40 #include "hw/loader.h"
41 #include "elf.h"
42 #include "hw/sysbus.h"
43 #include "qemu/host-utils.h"
44 #include "qemu/option.h"
45 #include "hw/pci-host/ppce500.h"
46 #include "qemu/error-report.h"
47 #include "hw/platform-bus.h"
48 #include "hw/net/fsl_etsec/etsec.h"
49 #include "hw/i2c/i2c.h"
50 #include "hw/irq.h"
51 #include "hw/sd/sdhci.h"
52 #include "hw/misc/unimp.h"
53 
54 #define EPAPR_MAGIC                (0x45504150)
55 #define DTC_LOAD_PAD               0x1800000
56 #define DTC_PAD_MASK               0xFFFFF
57 #define DTB_MAX_SIZE               (8 * MiB)
58 #define INITRD_LOAD_PAD            0x2000000
59 #define INITRD_PAD_MASK            0xFFFFFF
60 
61 #define RAM_SIZES_ALIGN            (64 * MiB)
62 
63 /* TODO: parameterize */
64 #define MPC8544_CCSRBAR_SIZE       0x00100000ULL
65 #define MPC8544_MPIC_REGS_OFFSET   0x40000ULL
66 #define MPC8544_MSI_REGS_OFFSET   0x41600ULL
67 #define MPC8544_SERIAL0_REGS_OFFSET 0x4500ULL
68 #define MPC8544_SERIAL1_REGS_OFFSET 0x4600ULL
69 #define MPC8544_PCI_REGS_OFFSET    0x8000ULL
70 #define MPC8544_PCI_REGS_SIZE      0x1000ULL
71 #define MPC85XX_ESDHC_REGS_OFFSET  0x2e000ULL
72 #define MPC85XX_ESDHC_REGS_SIZE    0x1000ULL
73 #define MPC8544_UTIL_OFFSET        0xe0000ULL
74 #define MPC8XXX_GPIO_OFFSET        0x000FF000ULL
75 #define MPC8544_I2C_REGS_OFFSET    0x3000ULL
76 #define MPC8XXX_GPIO_IRQ           47
77 #define MPC8544_I2C_IRQ            43
78 #define MPC85XX_ESDHC_IRQ          72
79 #define RTC_REGS_OFFSET            0x68
80 
81 #define PLATFORM_CLK_FREQ_HZ       (400 * 1000 * 1000)
82 
83 struct boot_info
84 {
85     uint32_t dt_base;
86     uint32_t dt_size;
87     uint32_t entry;
88 };
89 
90 static uint32_t *pci_map_create(void *fdt, uint32_t mpic, int first_slot,
91                                 int nr_slots, int *len)
92 {
93     int i = 0;
94     int slot;
95     int pci_irq;
96     int host_irq;
97     int last_slot = first_slot + nr_slots;
98     uint32_t *pci_map;
99 
100     *len = nr_slots * 4 * 7 * sizeof(uint32_t);
101     pci_map = g_malloc(*len);
102 
103     for (slot = first_slot; slot < last_slot; slot++) {
104         for (pci_irq = 0; pci_irq < 4; pci_irq++) {
105             pci_map[i++] = cpu_to_be32(slot << 11);
106             pci_map[i++] = cpu_to_be32(0x0);
107             pci_map[i++] = cpu_to_be32(0x0);
108             pci_map[i++] = cpu_to_be32(pci_irq + 1);
109             pci_map[i++] = cpu_to_be32(mpic);
110             host_irq = ppce500_pci_map_irq_slot(slot, pci_irq);
111             pci_map[i++] = cpu_to_be32(host_irq + 1);
112             pci_map[i++] = cpu_to_be32(0x1);
113         }
114     }
115 
116     assert((i * sizeof(uint32_t)) == *len);
117 
118     return pci_map;
119 }
120 
121 static void dt_serial_create(void *fdt, unsigned long long offset,
122                              const char *soc, const char *mpic,
123                              const char *alias, int idx, bool defcon)
124 {
125     char *ser;
126 
127     ser = g_strdup_printf("%s/serial@%llx", soc, offset);
128     qemu_fdt_add_subnode(fdt, ser);
129     qemu_fdt_setprop_string(fdt, ser, "device_type", "serial");
130     qemu_fdt_setprop_string(fdt, ser, "compatible", "ns16550");
131     qemu_fdt_setprop_cells(fdt, ser, "reg", offset, 0x100);
132     qemu_fdt_setprop_cell(fdt, ser, "cell-index", idx);
133     qemu_fdt_setprop_cell(fdt, ser, "clock-frequency", PLATFORM_CLK_FREQ_HZ);
134     qemu_fdt_setprop_cells(fdt, ser, "interrupts", 42, 2);
135     qemu_fdt_setprop_phandle(fdt, ser, "interrupt-parent", mpic);
136     qemu_fdt_setprop_string(fdt, "/aliases", alias, ser);
137 
138     if (defcon) {
139         /*
140          * "linux,stdout-path" and "stdout" properties are deprecated by linux
141          * kernel. New platforms should only use the "stdout-path" property. Set
142          * the new property and continue using older property to remain
143          * compatible with the existing firmware.
144          */
145         qemu_fdt_setprop_string(fdt, "/chosen", "linux,stdout-path", ser);
146         qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", ser);
147     }
148     g_free(ser);
149 }
150 
151 static void create_dt_mpc8xxx_gpio(void *fdt, const char *soc, const char *mpic)
152 {
153     hwaddr mmio0 = MPC8XXX_GPIO_OFFSET;
154     int irq0 = MPC8XXX_GPIO_IRQ;
155     gchar *node = g_strdup_printf("%s/gpio@%"PRIx64, soc, mmio0);
156     gchar *poweroff = g_strdup_printf("%s/power-off", soc);
157     int gpio_ph;
158 
159     qemu_fdt_add_subnode(fdt, node);
160     qemu_fdt_setprop_string(fdt, node, "compatible", "fsl,qoriq-gpio");
161     qemu_fdt_setprop_cells(fdt, node, "reg", mmio0, 0x1000);
162     qemu_fdt_setprop_cells(fdt, node, "interrupts", irq0, 0x2);
163     qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", mpic);
164     qemu_fdt_setprop_cells(fdt, node, "#gpio-cells", 2);
165     qemu_fdt_setprop(fdt, node, "gpio-controller", NULL, 0);
166     gpio_ph = qemu_fdt_alloc_phandle(fdt);
167     qemu_fdt_setprop_cell(fdt, node, "phandle", gpio_ph);
168     qemu_fdt_setprop_cell(fdt, node, "linux,phandle", gpio_ph);
169 
170     /* Power Off Pin */
171     qemu_fdt_add_subnode(fdt, poweroff);
172     qemu_fdt_setprop_string(fdt, poweroff, "compatible", "gpio-poweroff");
173     qemu_fdt_setprop_cells(fdt, poweroff, "gpios", gpio_ph, 0, 0);
174 
175     g_free(node);
176     g_free(poweroff);
177 }
178 
179 static void dt_rtc_create(void *fdt, const char *i2c, const char *alias)
180 {
181     int offset = RTC_REGS_OFFSET;
182 
183     gchar *rtc = g_strdup_printf("%s/rtc@%"PRIx32, i2c, offset);
184     qemu_fdt_add_subnode(fdt, rtc);
185     qemu_fdt_setprop_string(fdt, rtc, "compatible", "pericom,pt7c4338");
186     qemu_fdt_setprop_cells(fdt, rtc, "reg", offset);
187     qemu_fdt_setprop_string(fdt, "/aliases", alias, rtc);
188 
189     g_free(rtc);
190 }
191 
192 static void dt_i2c_create(void *fdt, const char *soc, const char *mpic,
193                              const char *alias)
194 {
195     hwaddr mmio0 = MPC8544_I2C_REGS_OFFSET;
196     int irq0 = MPC8544_I2C_IRQ;
197 
198     gchar *i2c = g_strdup_printf("%s/i2c@%"PRIx64, soc, mmio0);
199     qemu_fdt_add_subnode(fdt, i2c);
200     qemu_fdt_setprop_string(fdt, i2c, "device_type", "i2c");
201     qemu_fdt_setprop_string(fdt, i2c, "compatible", "fsl-i2c");
202     qemu_fdt_setprop_cells(fdt, i2c, "reg", mmio0, 0x14);
203     qemu_fdt_setprop_cells(fdt, i2c, "cell-index", 0);
204     qemu_fdt_setprop_cells(fdt, i2c, "interrupts", irq0, 0x2);
205     qemu_fdt_setprop_phandle(fdt, i2c, "interrupt-parent", mpic);
206     qemu_fdt_setprop_string(fdt, "/aliases", alias, i2c);
207 
208     g_free(i2c);
209 }
210 
211 static void dt_sdhc_create(void *fdt, const char *parent, const char *mpic)
212 {
213     hwaddr mmio = MPC85XX_ESDHC_REGS_OFFSET;
214     hwaddr size = MPC85XX_ESDHC_REGS_SIZE;
215     int irq = MPC85XX_ESDHC_IRQ;
216     g_autofree char *name = NULL;
217 
218     name = g_strdup_printf("%s/sdhc@%" PRIx64, parent, mmio);
219     qemu_fdt_add_subnode(fdt, name);
220     qemu_fdt_setprop(fdt, name, "sdhci,auto-cmd12", NULL, 0);
221     qemu_fdt_setprop_phandle(fdt, name, "interrupt-parent", mpic);
222     qemu_fdt_setprop_cells(fdt, name, "bus-width", 4);
223     qemu_fdt_setprop_cells(fdt, name, "interrupts", irq, 0x2);
224     qemu_fdt_setprop_cells(fdt, name, "reg", mmio, size);
225     qemu_fdt_setprop_string(fdt, name, "compatible", "fsl,esdhc");
226 }
227 
228 typedef struct PlatformDevtreeData {
229     void *fdt;
230     const char *mpic;
231     int irq_start;
232     const char *node;
233     PlatformBusDevice *pbus;
234 } PlatformDevtreeData;
235 
236 static int create_devtree_etsec(SysBusDevice *sbdev, PlatformDevtreeData *data)
237 {
238     eTSEC *etsec = ETSEC_COMMON(sbdev);
239     PlatformBusDevice *pbus = data->pbus;
240     hwaddr mmio0 = platform_bus_get_mmio_addr(pbus, sbdev, 0);
241     int irq0 = platform_bus_get_irqn(pbus, sbdev, 0);
242     int irq1 = platform_bus_get_irqn(pbus, sbdev, 1);
243     int irq2 = platform_bus_get_irqn(pbus, sbdev, 2);
244     gchar *node = g_strdup_printf("/platform/ethernet@%"PRIx64, mmio0);
245     gchar *group = g_strdup_printf("%s/queue-group", node);
246     void *fdt = data->fdt;
247 
248     assert((int64_t)mmio0 >= 0);
249     assert(irq0 >= 0);
250     assert(irq1 >= 0);
251     assert(irq2 >= 0);
252 
253     qemu_fdt_add_subnode(fdt, node);
254     qemu_fdt_setprop(fdt, node, "ranges", NULL, 0);
255     qemu_fdt_setprop_string(fdt, node, "device_type", "network");
256     qemu_fdt_setprop_string(fdt, node, "compatible", "fsl,etsec2");
257     qemu_fdt_setprop_string(fdt, node, "model", "eTSEC");
258     qemu_fdt_setprop(fdt, node, "local-mac-address", etsec->conf.macaddr.a, 6);
259     qemu_fdt_setprop_cells(fdt, node, "fixed-link", 0, 1, 1000, 0, 0);
260     qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1);
261     qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1);
262 
263     qemu_fdt_add_subnode(fdt, group);
264     qemu_fdt_setprop_cells(fdt, group, "reg", mmio0, 0x1000);
265     qemu_fdt_setprop_cells(fdt, group, "interrupts",
266         data->irq_start + irq0, 0x2,
267         data->irq_start + irq1, 0x2,
268         data->irq_start + irq2, 0x2);
269 
270     g_free(node);
271     g_free(group);
272 
273     return 0;
274 }
275 
276 static void sysbus_device_create_devtree(SysBusDevice *sbdev, void *opaque)
277 {
278     PlatformDevtreeData *data = opaque;
279     bool matched = false;
280 
281     if (object_dynamic_cast(OBJECT(sbdev), TYPE_ETSEC_COMMON)) {
282         create_devtree_etsec(sbdev, data);
283         matched = true;
284     }
285 
286     if (!matched) {
287         error_report("Device %s is not supported by this machine yet.",
288                      qdev_fw_name(DEVICE(sbdev)));
289         exit(1);
290     }
291 }
292 
293 static void create_devtree_flash(SysBusDevice *sbdev,
294                                  PlatformDevtreeData *data)
295 {
296     g_autofree char *name = NULL;
297     uint64_t num_blocks = object_property_get_uint(OBJECT(sbdev),
298                                                    "num-blocks",
299                                                    &error_fatal);
300     uint64_t sector_length = object_property_get_uint(OBJECT(sbdev),
301                                                       "sector-length",
302                                                       &error_fatal);
303     uint64_t bank_width = object_property_get_uint(OBJECT(sbdev),
304                                                    "width",
305                                                    &error_fatal);
306     hwaddr flashbase = 0;
307     hwaddr flashsize = num_blocks * sector_length;
308     void *fdt = data->fdt;
309 
310     name = g_strdup_printf("%s/nor@%" PRIx64, data->node, flashbase);
311     qemu_fdt_add_subnode(fdt, name);
312     qemu_fdt_setprop_string(fdt, name, "compatible", "cfi-flash");
313     qemu_fdt_setprop_sized_cells(fdt, name, "reg",
314                                  1, flashbase, 1, flashsize);
315     qemu_fdt_setprop_cell(fdt, name, "bank-width", bank_width);
316 }
317 
318 static void platform_bus_create_devtree(PPCE500MachineState *pms,
319                                         void *fdt, const char *mpic)
320 {
321     const PPCE500MachineClass *pmc = PPCE500_MACHINE_GET_CLASS(pms);
322     gchar *node = g_strdup_printf("/platform@%"PRIx64, pmc->platform_bus_base);
323     const char platcomp[] = "qemu,platform\0simple-bus";
324     uint64_t addr = pmc->platform_bus_base;
325     uint64_t size = pmc->platform_bus_size;
326     int irq_start = pmc->platform_bus_first_irq;
327     SysBusDevice *sbdev;
328     bool ambiguous;
329 
330     /* Create a /platform node that we can put all devices into */
331 
332     qemu_fdt_add_subnode(fdt, node);
333     qemu_fdt_setprop(fdt, node, "compatible", platcomp, sizeof(platcomp));
334 
335     /* Our platform bus region is less than 32bit big, so 1 cell is enough for
336        address and size */
337     qemu_fdt_setprop_cells(fdt, node, "#size-cells", 1);
338     qemu_fdt_setprop_cells(fdt, node, "#address-cells", 1);
339     qemu_fdt_setprop_cells(fdt, node, "ranges", 0, addr >> 32, addr, size);
340 
341     qemu_fdt_setprop_phandle(fdt, node, "interrupt-parent", mpic);
342 
343     /* Create dt nodes for dynamic devices */
344     PlatformDevtreeData data = {
345         .fdt = fdt,
346         .mpic = mpic,
347         .irq_start = irq_start,
348         .node = node,
349         .pbus = pms->pbus_dev,
350     };
351 
352     /* Loop through all dynamic sysbus devices and create nodes for them */
353     foreach_dynamic_sysbus_device(sysbus_device_create_devtree, &data);
354 
355     sbdev = SYS_BUS_DEVICE(object_resolve_path_type("", TYPE_PFLASH_CFI01,
356                                                     &ambiguous));
357     if (sbdev) {
358         assert(!ambiguous);
359         create_devtree_flash(sbdev, &data);
360     }
361 
362     g_free(node);
363 }
364 
365 static int ppce500_load_device_tree(PPCE500MachineState *pms,
366                                     hwaddr addr,
367                                     hwaddr initrd_base,
368                                     hwaddr initrd_size,
369                                     hwaddr kernel_base,
370                                     hwaddr kernel_size,
371                                     bool dry_run)
372 {
373     MachineState *machine = MACHINE(pms);
374     unsigned int smp_cpus = machine->smp.cpus;
375     const PPCE500MachineClass *pmc = PPCE500_MACHINE_GET_CLASS(pms);
376     CPUPPCState *env = first_cpu->env_ptr;
377     int ret = -1;
378     uint64_t mem_reg_property[] = { 0, cpu_to_be64(machine->ram_size) };
379     int fdt_size;
380     void *fdt;
381     uint8_t hypercall[16];
382     uint32_t clock_freq = PLATFORM_CLK_FREQ_HZ;
383     uint32_t tb_freq = PLATFORM_CLK_FREQ_HZ;
384     int i;
385     char compatible_sb[] = "fsl,mpc8544-immr\0simple-bus";
386     char *soc;
387     char *mpic;
388     uint32_t mpic_ph;
389     uint32_t msi_ph;
390     char *gutil;
391     char *pci;
392     char *msi;
393     uint32_t *pci_map = NULL;
394     int len;
395     uint32_t pci_ranges[14] =
396         {
397             0x2000000, 0x0, pmc->pci_mmio_bus_base,
398             pmc->pci_mmio_base >> 32, pmc->pci_mmio_base,
399             0x0, 0x20000000,
400 
401             0x1000000, 0x0, 0x0,
402             pmc->pci_pio_base >> 32, pmc->pci_pio_base,
403             0x0, 0x10000,
404         };
405     const char *dtb_file = machine->dtb;
406     const char *toplevel_compat = machine->dt_compatible;
407     uint8_t rng_seed[32];
408 
409     if (dtb_file) {
410         char *filename;
411         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_file);
412         if (!filename) {
413             goto out;
414         }
415 
416         fdt = load_device_tree(filename, &fdt_size);
417         g_free(filename);
418         if (!fdt) {
419             goto out;
420         }
421         goto done;
422     }
423 
424     fdt = create_device_tree(&fdt_size);
425     if (fdt == NULL) {
426         goto out;
427     }
428 
429     /* Manipulate device tree in memory. */
430     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 2);
431     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 2);
432 
433     qemu_fdt_add_subnode(fdt, "/memory");
434     qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
435     qemu_fdt_setprop(fdt, "/memory", "reg", mem_reg_property,
436                      sizeof(mem_reg_property));
437 
438     qemu_fdt_add_subnode(fdt, "/chosen");
439     if (initrd_size) {
440         ret = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
441                                     initrd_base);
442         if (ret < 0) {
443             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
444         }
445 
446         ret = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
447                                     (initrd_base + initrd_size));
448         if (ret < 0) {
449             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
450         }
451 
452     }
453 
454     if (kernel_base != -1ULL) {
455         qemu_fdt_setprop_cells(fdt, "/chosen", "qemu,boot-kernel",
456                                      kernel_base >> 32, kernel_base,
457                                      kernel_size >> 32, kernel_size);
458     }
459 
460     ret = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
461                                       machine->kernel_cmdline);
462     if (ret < 0)
463         fprintf(stderr, "couldn't set /chosen/bootargs\n");
464 
465     qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed));
466     qemu_fdt_setprop(fdt, "/chosen", "rng-seed", rng_seed, sizeof(rng_seed));
467 
468     if (kvm_enabled()) {
469         /* Read out host's frequencies */
470         clock_freq = kvmppc_get_clockfreq();
471         tb_freq = kvmppc_get_tbfreq();
472 
473         /* indicate KVM hypercall interface */
474         qemu_fdt_add_subnode(fdt, "/hypervisor");
475         qemu_fdt_setprop_string(fdt, "/hypervisor", "compatible",
476                                 "linux,kvm");
477         kvmppc_get_hypercall(env, hypercall, sizeof(hypercall));
478         qemu_fdt_setprop(fdt, "/hypervisor", "hcall-instructions",
479                          hypercall, sizeof(hypercall));
480         /* if KVM supports the idle hcall, set property indicating this */
481         if (kvmppc_get_hasidle(env)) {
482             qemu_fdt_setprop(fdt, "/hypervisor", "has-idle", NULL, 0);
483         }
484     }
485 
486     /* Create CPU nodes */
487     qemu_fdt_add_subnode(fdt, "/cpus");
488     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 1);
489     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0);
490 
491     /* We need to generate the cpu nodes in reverse order, so Linux can pick
492        the first node as boot node and be happy */
493     for (i = smp_cpus - 1; i >= 0; i--) {
494         CPUState *cpu;
495         char *cpu_name;
496         uint64_t cpu_release_addr = pmc->spin_base + (i * 0x20);
497 
498         cpu = qemu_get_cpu(i);
499         if (cpu == NULL) {
500             continue;
501         }
502         env = cpu->env_ptr;
503 
504         cpu_name = g_strdup_printf("/cpus/PowerPC,8544@%x", i);
505         qemu_fdt_add_subnode(fdt, cpu_name);
506         qemu_fdt_setprop_cell(fdt, cpu_name, "clock-frequency", clock_freq);
507         qemu_fdt_setprop_cell(fdt, cpu_name, "timebase-frequency", tb_freq);
508         qemu_fdt_setprop_string(fdt, cpu_name, "device_type", "cpu");
509         qemu_fdt_setprop_cell(fdt, cpu_name, "reg", i);
510         qemu_fdt_setprop_cell(fdt, cpu_name, "d-cache-line-size",
511                               env->dcache_line_size);
512         qemu_fdt_setprop_cell(fdt, cpu_name, "i-cache-line-size",
513                               env->icache_line_size);
514         qemu_fdt_setprop_cell(fdt, cpu_name, "d-cache-size", 0x8000);
515         qemu_fdt_setprop_cell(fdt, cpu_name, "i-cache-size", 0x8000);
516         qemu_fdt_setprop_cell(fdt, cpu_name, "bus-frequency", 0);
517         if (cpu->cpu_index) {
518             qemu_fdt_setprop_string(fdt, cpu_name, "status", "disabled");
519             qemu_fdt_setprop_string(fdt, cpu_name, "enable-method",
520                                     "spin-table");
521             qemu_fdt_setprop_u64(fdt, cpu_name, "cpu-release-addr",
522                                  cpu_release_addr);
523         } else {
524             qemu_fdt_setprop_string(fdt, cpu_name, "status", "okay");
525         }
526         g_free(cpu_name);
527     }
528 
529     qemu_fdt_add_subnode(fdt, "/aliases");
530     /* XXX These should go into their respective devices' code */
531     soc = g_strdup_printf("/soc@%"PRIx64, pmc->ccsrbar_base);
532     qemu_fdt_add_subnode(fdt, soc);
533     qemu_fdt_setprop_string(fdt, soc, "device_type", "soc");
534     qemu_fdt_setprop(fdt, soc, "compatible", compatible_sb,
535                      sizeof(compatible_sb));
536     qemu_fdt_setprop_cell(fdt, soc, "#address-cells", 1);
537     qemu_fdt_setprop_cell(fdt, soc, "#size-cells", 1);
538     qemu_fdt_setprop_cells(fdt, soc, "ranges", 0x0,
539                            pmc->ccsrbar_base >> 32, pmc->ccsrbar_base,
540                            MPC8544_CCSRBAR_SIZE);
541     /* XXX should contain a reasonable value */
542     qemu_fdt_setprop_cell(fdt, soc, "bus-frequency", 0);
543 
544     mpic = g_strdup_printf("%s/pic@%llx", soc, MPC8544_MPIC_REGS_OFFSET);
545     qemu_fdt_add_subnode(fdt, mpic);
546     qemu_fdt_setprop_string(fdt, mpic, "device_type", "open-pic");
547     qemu_fdt_setprop_string(fdt, mpic, "compatible", "fsl,mpic");
548     qemu_fdt_setprop_cells(fdt, mpic, "reg", MPC8544_MPIC_REGS_OFFSET,
549                            0x40000);
550     qemu_fdt_setprop_cell(fdt, mpic, "#address-cells", 0);
551     qemu_fdt_setprop_cell(fdt, mpic, "#interrupt-cells", 2);
552     mpic_ph = qemu_fdt_alloc_phandle(fdt);
553     qemu_fdt_setprop_cell(fdt, mpic, "phandle", mpic_ph);
554     qemu_fdt_setprop_cell(fdt, mpic, "linux,phandle", mpic_ph);
555     qemu_fdt_setprop(fdt, mpic, "interrupt-controller", NULL, 0);
556 
557     /*
558      * We have to generate ser1 first, because Linux takes the first
559      * device it finds in the dt as serial output device. And we generate
560      * devices in reverse order to the dt.
561      */
562     if (serial_hd(1)) {
563         dt_serial_create(fdt, MPC8544_SERIAL1_REGS_OFFSET,
564                          soc, mpic, "serial1", 1, false);
565     }
566 
567     if (serial_hd(0)) {
568         dt_serial_create(fdt, MPC8544_SERIAL0_REGS_OFFSET,
569                          soc, mpic, "serial0", 0, true);
570     }
571 
572     /* i2c */
573     dt_i2c_create(fdt, soc, mpic, "i2c");
574 
575     dt_rtc_create(fdt, "i2c", "rtc");
576 
577     /* sdhc */
578     if (pmc->has_esdhc) {
579         dt_sdhc_create(fdt, soc, mpic);
580     }
581 
582     gutil = g_strdup_printf("%s/global-utilities@%llx", soc,
583                             MPC8544_UTIL_OFFSET);
584     qemu_fdt_add_subnode(fdt, gutil);
585     qemu_fdt_setprop_string(fdt, gutil, "compatible", "fsl,mpc8544-guts");
586     qemu_fdt_setprop_cells(fdt, gutil, "reg", MPC8544_UTIL_OFFSET, 0x1000);
587     qemu_fdt_setprop(fdt, gutil, "fsl,has-rstcr", NULL, 0);
588     g_free(gutil);
589 
590     msi = g_strdup_printf("/%s/msi@%llx", soc, MPC8544_MSI_REGS_OFFSET);
591     qemu_fdt_add_subnode(fdt, msi);
592     qemu_fdt_setprop_string(fdt, msi, "compatible", "fsl,mpic-msi");
593     qemu_fdt_setprop_cells(fdt, msi, "reg", MPC8544_MSI_REGS_OFFSET, 0x200);
594     msi_ph = qemu_fdt_alloc_phandle(fdt);
595     qemu_fdt_setprop_cells(fdt, msi, "msi-available-ranges", 0x0, 0x100);
596     qemu_fdt_setprop_phandle(fdt, msi, "interrupt-parent", mpic);
597     qemu_fdt_setprop_cells(fdt, msi, "interrupts",
598         0xe0, 0x0,
599         0xe1, 0x0,
600         0xe2, 0x0,
601         0xe3, 0x0,
602         0xe4, 0x0,
603         0xe5, 0x0,
604         0xe6, 0x0,
605         0xe7, 0x0);
606     qemu_fdt_setprop_cell(fdt, msi, "phandle", msi_ph);
607     qemu_fdt_setprop_cell(fdt, msi, "linux,phandle", msi_ph);
608     g_free(msi);
609 
610     pci = g_strdup_printf("/pci@%llx",
611                           pmc->ccsrbar_base + MPC8544_PCI_REGS_OFFSET);
612     qemu_fdt_add_subnode(fdt, pci);
613     qemu_fdt_setprop_cell(fdt, pci, "cell-index", 0);
614     qemu_fdt_setprop_string(fdt, pci, "compatible", "fsl,mpc8540-pci");
615     qemu_fdt_setprop_string(fdt, pci, "device_type", "pci");
616     qemu_fdt_setprop_cells(fdt, pci, "interrupt-map-mask", 0xf800, 0x0,
617                            0x0, 0x7);
618     pci_map = pci_map_create(fdt, qemu_fdt_get_phandle(fdt, mpic),
619                              pmc->pci_first_slot, pmc->pci_nr_slots,
620                              &len);
621     qemu_fdt_setprop(fdt, pci, "interrupt-map", pci_map, len);
622     qemu_fdt_setprop_phandle(fdt, pci, "interrupt-parent", mpic);
623     qemu_fdt_setprop_cells(fdt, pci, "interrupts", 24, 2);
624     qemu_fdt_setprop_cells(fdt, pci, "bus-range", 0, 255);
625     for (i = 0; i < 14; i++) {
626         pci_ranges[i] = cpu_to_be32(pci_ranges[i]);
627     }
628     qemu_fdt_setprop_cell(fdt, pci, "fsl,msi", msi_ph);
629     qemu_fdt_setprop(fdt, pci, "ranges", pci_ranges, sizeof(pci_ranges));
630     qemu_fdt_setprop_cells(fdt, pci, "reg",
631                            (pmc->ccsrbar_base + MPC8544_PCI_REGS_OFFSET) >> 32,
632                            (pmc->ccsrbar_base + MPC8544_PCI_REGS_OFFSET),
633                            0, 0x1000);
634     qemu_fdt_setprop_cell(fdt, pci, "clock-frequency", 66666666);
635     qemu_fdt_setprop_cell(fdt, pci, "#interrupt-cells", 1);
636     qemu_fdt_setprop_cell(fdt, pci, "#size-cells", 2);
637     qemu_fdt_setprop_cell(fdt, pci, "#address-cells", 3);
638     qemu_fdt_setprop_string(fdt, "/aliases", "pci0", pci);
639     g_free(pci);
640 
641     if (pmc->has_mpc8xxx_gpio) {
642         create_dt_mpc8xxx_gpio(fdt, soc, mpic);
643     }
644     g_free(soc);
645 
646     if (pms->pbus_dev) {
647         platform_bus_create_devtree(pms, fdt, mpic);
648     }
649     g_free(mpic);
650 
651     pmc->fixup_devtree(fdt);
652 
653     if (toplevel_compat) {
654         qemu_fdt_setprop(fdt, "/", "compatible", toplevel_compat,
655                          strlen(toplevel_compat) + 1);
656     }
657 
658 done:
659     if (!dry_run) {
660         qemu_fdt_dumpdtb(fdt, fdt_size);
661         cpu_physical_memory_write(addr, fdt, fdt_size);
662     }
663     ret = fdt_size;
664     g_free(fdt);
665 
666 out:
667     g_free(pci_map);
668 
669     return ret;
670 }
671 
672 typedef struct DeviceTreeParams {
673     PPCE500MachineState *machine;
674     hwaddr addr;
675     hwaddr initrd_base;
676     hwaddr initrd_size;
677     hwaddr kernel_base;
678     hwaddr kernel_size;
679     Notifier notifier;
680 } DeviceTreeParams;
681 
682 static void ppce500_reset_device_tree(void *opaque)
683 {
684     DeviceTreeParams *p = opaque;
685     ppce500_load_device_tree(p->machine, p->addr, p->initrd_base,
686                              p->initrd_size, p->kernel_base, p->kernel_size,
687                              false);
688 }
689 
690 static void ppce500_init_notify(Notifier *notifier, void *data)
691 {
692     DeviceTreeParams *p = container_of(notifier, DeviceTreeParams, notifier);
693     ppce500_reset_device_tree(p);
694 }
695 
696 static int ppce500_prep_device_tree(PPCE500MachineState *machine,
697                                     hwaddr addr,
698                                     hwaddr initrd_base,
699                                     hwaddr initrd_size,
700                                     hwaddr kernel_base,
701                                     hwaddr kernel_size)
702 {
703     DeviceTreeParams *p = g_new(DeviceTreeParams, 1);
704     p->machine = machine;
705     p->addr = addr;
706     p->initrd_base = initrd_base;
707     p->initrd_size = initrd_size;
708     p->kernel_base = kernel_base;
709     p->kernel_size = kernel_size;
710 
711     qemu_register_reset(ppce500_reset_device_tree, p);
712     p->notifier.notify = ppce500_init_notify;
713     qemu_add_machine_init_done_notifier(&p->notifier);
714 
715     /* Issue the device tree loader once, so that we get the size of the blob */
716     return ppce500_load_device_tree(machine, addr, initrd_base, initrd_size,
717                                     kernel_base, kernel_size, true);
718 }
719 
720 hwaddr booke206_page_size_to_tlb(uint64_t size)
721 {
722     return 63 - clz64(size / KiB);
723 }
724 
725 static int booke206_initial_map_tsize(CPUPPCState *env)
726 {
727     struct boot_info *bi = env->load_info;
728     hwaddr dt_end;
729     int ps;
730 
731     /* Our initial TLB entry needs to cover everything from 0 to
732        the device tree top */
733     dt_end = bi->dt_base + bi->dt_size;
734     ps = booke206_page_size_to_tlb(dt_end) + 1;
735     if (ps & 1) {
736         /* e500v2 can only do even TLB size bits */
737         ps++;
738     }
739     return ps;
740 }
741 
742 static uint64_t mmubooke_initial_mapsize(CPUPPCState *env)
743 {
744     int tsize;
745 
746     tsize = booke206_initial_map_tsize(env);
747     return (1ULL << 10 << tsize);
748 }
749 
750 /* Create -kernel TLB entries for BookE. */
751 static void mmubooke_create_initial_mapping(CPUPPCState *env)
752 {
753     ppcmas_tlb_t *tlb = booke206_get_tlbm(env, 1, 0, 0);
754     hwaddr size;
755     int ps;
756 
757     ps = booke206_initial_map_tsize(env);
758     size = (ps << MAS1_TSIZE_SHIFT);
759     tlb->mas1 = MAS1_VALID | size;
760     tlb->mas2 = 0;
761     tlb->mas7_3 = 0;
762     tlb->mas7_3 |= MAS3_UR | MAS3_UW | MAS3_UX | MAS3_SR | MAS3_SW | MAS3_SX;
763 
764     env->tlb_dirty = true;
765 }
766 
767 static void ppce500_cpu_reset_sec(void *opaque)
768 {
769     PowerPCCPU *cpu = opaque;
770     CPUState *cs = CPU(cpu);
771 
772     cpu_reset(cs);
773 
774     cs->exception_index = EXCP_HLT;
775 }
776 
777 static void ppce500_cpu_reset(void *opaque)
778 {
779     PowerPCCPU *cpu = opaque;
780     CPUState *cs = CPU(cpu);
781     CPUPPCState *env = &cpu->env;
782     struct boot_info *bi = env->load_info;
783 
784     cpu_reset(cs);
785 
786     /* Set initial guest state. */
787     cs->halted = 0;
788     env->gpr[1] = (16 * MiB) - 8;
789     env->gpr[3] = bi->dt_base;
790     env->gpr[4] = 0;
791     env->gpr[5] = 0;
792     env->gpr[6] = EPAPR_MAGIC;
793     env->gpr[7] = mmubooke_initial_mapsize(env);
794     env->gpr[8] = 0;
795     env->gpr[9] = 0;
796     env->nip = bi->entry;
797     mmubooke_create_initial_mapping(env);
798 }
799 
800 static DeviceState *ppce500_init_mpic_qemu(PPCE500MachineState *pms,
801                                            IrqLines  *irqs)
802 {
803     DeviceState *dev;
804     SysBusDevice *s;
805     int i, j, k;
806     MachineState *machine = MACHINE(pms);
807     unsigned int smp_cpus = machine->smp.cpus;
808     const PPCE500MachineClass *pmc = PPCE500_MACHINE_GET_CLASS(pms);
809 
810     dev = qdev_new(TYPE_OPENPIC);
811     object_property_add_child(OBJECT(machine), "pic", OBJECT(dev));
812     qdev_prop_set_uint32(dev, "model", pmc->mpic_version);
813     qdev_prop_set_uint32(dev, "nb_cpus", smp_cpus);
814 
815     s = SYS_BUS_DEVICE(dev);
816     sysbus_realize_and_unref(s, &error_fatal);
817 
818     k = 0;
819     for (i = 0; i < smp_cpus; i++) {
820         for (j = 0; j < OPENPIC_OUTPUT_NB; j++) {
821             sysbus_connect_irq(s, k++, irqs[i].irq[j]);
822         }
823     }
824 
825     return dev;
826 }
827 
828 static DeviceState *ppce500_init_mpic_kvm(const PPCE500MachineClass *pmc,
829                                           IrqLines *irqs, Error **errp)
830 {
831     DeviceState *dev;
832     CPUState *cs;
833 
834     dev = qdev_new(TYPE_KVM_OPENPIC);
835     qdev_prop_set_uint32(dev, "model", pmc->mpic_version);
836 
837     if (!sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), errp)) {
838         object_unparent(OBJECT(dev));
839         return NULL;
840     }
841 
842     CPU_FOREACH(cs) {
843         if (kvm_openpic_connect_vcpu(dev, cs)) {
844             fprintf(stderr, "%s: failed to connect vcpu to irqchip\n",
845                     __func__);
846             abort();
847         }
848     }
849 
850     return dev;
851 }
852 
853 static DeviceState *ppce500_init_mpic(PPCE500MachineState *pms,
854                                       MemoryRegion *ccsr,
855                                       IrqLines *irqs)
856 {
857     const PPCE500MachineClass *pmc = PPCE500_MACHINE_GET_CLASS(pms);
858     DeviceState *dev = NULL;
859     SysBusDevice *s;
860 
861     if (kvm_enabled()) {
862         Error *err = NULL;
863 
864         if (kvm_kernel_irqchip_allowed()) {
865             dev = ppce500_init_mpic_kvm(pmc, irqs, &err);
866         }
867         if (kvm_kernel_irqchip_required() && !dev) {
868             error_reportf_err(err,
869                               "kernel_irqchip requested but unavailable: ");
870             exit(1);
871         }
872     }
873 
874     if (!dev) {
875         dev = ppce500_init_mpic_qemu(pms, irqs);
876     }
877 
878     s = SYS_BUS_DEVICE(dev);
879     memory_region_add_subregion(ccsr, MPC8544_MPIC_REGS_OFFSET,
880                                 s->mmio[0].memory);
881 
882     return dev;
883 }
884 
885 static void ppce500_power_off(void *opaque, int line, int on)
886 {
887     if (on) {
888         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
889     }
890 }
891 
892 void ppce500_init(MachineState *machine)
893 {
894     MemoryRegion *address_space_mem = get_system_memory();
895     PPCE500MachineState *pms = PPCE500_MACHINE(machine);
896     const PPCE500MachineClass *pmc = PPCE500_MACHINE_GET_CLASS(machine);
897     PCIBus *pci_bus;
898     CPUPPCState *env = NULL;
899     uint64_t loadaddr;
900     hwaddr kernel_base = -1LL;
901     int kernel_size = 0;
902     hwaddr dt_base = 0;
903     hwaddr initrd_base = 0;
904     int initrd_size = 0;
905     hwaddr cur_base = 0;
906     char *filename;
907     const char *payload_name;
908     bool kernel_as_payload;
909     hwaddr bios_entry = 0;
910     target_long payload_size;
911     struct boot_info *boot_info = NULL;
912     int dt_size;
913     int i;
914     unsigned int smp_cpus = machine->smp.cpus;
915     /* irq num for pin INTA, INTB, INTC and INTD is 1, 2, 3 and
916      * 4 respectively */
917     unsigned int pci_irq_nrs[PCI_NUM_PINS] = {1, 2, 3, 4};
918     IrqLines *irqs;
919     DeviceState *dev, *mpicdev;
920     DriveInfo *dinfo;
921     CPUPPCState *firstenv = NULL;
922     MemoryRegion *ccsr_addr_space;
923     SysBusDevice *s;
924     PPCE500CCSRState *ccsr;
925     I2CBus *i2c;
926 
927     irqs = g_new0(IrqLines, smp_cpus);
928     for (i = 0; i < smp_cpus; i++) {
929         PowerPCCPU *cpu;
930         CPUState *cs;
931 
932         cpu = POWERPC_CPU(object_new(machine->cpu_type));
933         env = &cpu->env;
934         cs = CPU(cpu);
935 
936         if (env->mmu_model != POWERPC_MMU_BOOKE206) {
937             error_report("MMU model %i not supported by this machine",
938                          env->mmu_model);
939             exit(1);
940         }
941 
942         /*
943          * Secondary CPU starts in halted state for now. Needs to change
944          * when implementing non-kernel boot.
945          */
946         object_property_set_bool(OBJECT(cs), "start-powered-off", i != 0,
947                                  &error_fatal);
948         qdev_realize_and_unref(DEVICE(cs), NULL, &error_fatal);
949 
950         if (!firstenv) {
951             firstenv = env;
952         }
953 
954         irqs[i].irq[OPENPIC_OUTPUT_INT] =
955             qdev_get_gpio_in(DEVICE(cpu), PPCE500_INPUT_INT);
956         irqs[i].irq[OPENPIC_OUTPUT_CINT] =
957             qdev_get_gpio_in(DEVICE(cpu), PPCE500_INPUT_CINT);
958         env->spr_cb[SPR_BOOKE_PIR].default_value = cs->cpu_index = i;
959         env->mpic_iack = pmc->ccsrbar_base + MPC8544_MPIC_REGS_OFFSET + 0xa0;
960 
961         ppc_booke_timers_init(cpu, PLATFORM_CLK_FREQ_HZ, PPC_TIMER_E500);
962 
963         /* Register reset handler */
964         if (!i) {
965             /* Primary CPU */
966             boot_info = g_new0(struct boot_info, 1);
967             qemu_register_reset(ppce500_cpu_reset, cpu);
968             env->load_info = boot_info;
969         } else {
970             /* Secondary CPUs */
971             qemu_register_reset(ppce500_cpu_reset_sec, cpu);
972         }
973     }
974 
975     env = firstenv;
976 
977     if (!QEMU_IS_ALIGNED(machine->ram_size, RAM_SIZES_ALIGN)) {
978         error_report("RAM size must be multiple of %" PRIu64, RAM_SIZES_ALIGN);
979         exit(EXIT_FAILURE);
980     }
981 
982     /* Register Memory */
983     memory_region_add_subregion(address_space_mem, 0, machine->ram);
984 
985     dev = qdev_new("e500-ccsr");
986     object_property_add_child(OBJECT(machine), "e500-ccsr", OBJECT(dev));
987     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
988     ccsr = CCSR(dev);
989     ccsr_addr_space = &ccsr->ccsr_space;
990     memory_region_add_subregion(address_space_mem, pmc->ccsrbar_base,
991                                 ccsr_addr_space);
992 
993     mpicdev = ppce500_init_mpic(pms, ccsr_addr_space, irqs);
994     g_free(irqs);
995 
996     /* Serial */
997     if (serial_hd(0)) {
998         serial_mm_init(ccsr_addr_space, MPC8544_SERIAL0_REGS_OFFSET,
999                        0, qdev_get_gpio_in(mpicdev, 42), 399193,
1000                        serial_hd(0), DEVICE_BIG_ENDIAN);
1001     }
1002 
1003     if (serial_hd(1)) {
1004         serial_mm_init(ccsr_addr_space, MPC8544_SERIAL1_REGS_OFFSET,
1005                        0, qdev_get_gpio_in(mpicdev, 42), 399193,
1006                        serial_hd(1), DEVICE_BIG_ENDIAN);
1007     }
1008 
1009     /* I2C */
1010     dev = qdev_new("mpc-i2c");
1011     s = SYS_BUS_DEVICE(dev);
1012     sysbus_realize_and_unref(s, &error_fatal);
1013     sysbus_connect_irq(s, 0, qdev_get_gpio_in(mpicdev, MPC8544_I2C_IRQ));
1014     memory_region_add_subregion(ccsr_addr_space, MPC8544_I2C_REGS_OFFSET,
1015                                 sysbus_mmio_get_region(s, 0));
1016     i2c = (I2CBus *)qdev_get_child_bus(dev, "i2c");
1017     i2c_slave_create_simple(i2c, "ds1338", RTC_REGS_OFFSET);
1018 
1019     /* eSDHC */
1020     if (pmc->has_esdhc) {
1021         create_unimplemented_device("esdhc",
1022                                     pmc->ccsrbar_base + MPC85XX_ESDHC_REGS_OFFSET,
1023                                     MPC85XX_ESDHC_REGS_SIZE);
1024 
1025         /*
1026          * Compatible with:
1027          * - SD Host Controller Specification Version 2.0 Part A2
1028          * (See MPC8569E Reference Manual)
1029          */
1030         dev = qdev_new(TYPE_SYSBUS_SDHCI);
1031         qdev_prop_set_uint8(dev, "sd-spec-version", 2);
1032         qdev_prop_set_uint8(dev, "endianness", DEVICE_BIG_ENDIAN);
1033         s = SYS_BUS_DEVICE(dev);
1034         sysbus_realize_and_unref(s, &error_fatal);
1035         sysbus_connect_irq(s, 0, qdev_get_gpio_in(mpicdev, MPC85XX_ESDHC_IRQ));
1036         memory_region_add_subregion(ccsr_addr_space, MPC85XX_ESDHC_REGS_OFFSET,
1037                                     sysbus_mmio_get_region(s, 0));
1038     }
1039 
1040     /* General Utility device */
1041     dev = qdev_new("mpc8544-guts");
1042     s = SYS_BUS_DEVICE(dev);
1043     sysbus_realize_and_unref(s, &error_fatal);
1044     memory_region_add_subregion(ccsr_addr_space, MPC8544_UTIL_OFFSET,
1045                                 sysbus_mmio_get_region(s, 0));
1046 
1047     /* PCI */
1048     dev = qdev_new("e500-pcihost");
1049     object_property_add_child(OBJECT(machine), "pci-host", OBJECT(dev));
1050     qdev_prop_set_uint32(dev, "first_slot", pmc->pci_first_slot);
1051     qdev_prop_set_uint32(dev, "first_pin_irq", pci_irq_nrs[0]);
1052     s = SYS_BUS_DEVICE(dev);
1053     sysbus_realize_and_unref(s, &error_fatal);
1054     for (i = 0; i < PCI_NUM_PINS; i++) {
1055         sysbus_connect_irq(s, i, qdev_get_gpio_in(mpicdev, pci_irq_nrs[i]));
1056     }
1057 
1058     memory_region_add_subregion(ccsr_addr_space, MPC8544_PCI_REGS_OFFSET,
1059                                 sysbus_mmio_get_region(s, 0));
1060 
1061     pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
1062     if (!pci_bus)
1063         printf("couldn't create PCI controller!\n");
1064 
1065     if (pci_bus) {
1066         /* Register network interfaces. */
1067         for (i = 0; i < nb_nics; i++) {
1068             pci_nic_init_nofail(&nd_table[i], pci_bus, "virtio-net-pci", NULL);
1069         }
1070     }
1071 
1072     /* Register spinning region */
1073     sysbus_create_simple("e500-spin", pmc->spin_base, NULL);
1074 
1075     if (pmc->has_mpc8xxx_gpio) {
1076         qemu_irq poweroff_irq;
1077 
1078         dev = qdev_new("mpc8xxx_gpio");
1079         s = SYS_BUS_DEVICE(dev);
1080         sysbus_realize_and_unref(s, &error_fatal);
1081         sysbus_connect_irq(s, 0, qdev_get_gpio_in(mpicdev, MPC8XXX_GPIO_IRQ));
1082         memory_region_add_subregion(ccsr_addr_space, MPC8XXX_GPIO_OFFSET,
1083                                     sysbus_mmio_get_region(s, 0));
1084 
1085         /* Power Off GPIO at Pin 0 */
1086         poweroff_irq = qemu_allocate_irq(ppce500_power_off, NULL, 0);
1087         qdev_connect_gpio_out(dev, 0, poweroff_irq);
1088     }
1089 
1090     /* Platform Bus Device */
1091     dev = qdev_new(TYPE_PLATFORM_BUS_DEVICE);
1092     dev->id = g_strdup(TYPE_PLATFORM_BUS_DEVICE);
1093     qdev_prop_set_uint32(dev, "num_irqs", pmc->platform_bus_num_irqs);
1094     qdev_prop_set_uint32(dev, "mmio_size", pmc->platform_bus_size);
1095     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1096     pms->pbus_dev = PLATFORM_BUS_DEVICE(dev);
1097 
1098     s = SYS_BUS_DEVICE(pms->pbus_dev);
1099     for (i = 0; i < pmc->platform_bus_num_irqs; i++) {
1100         int irqn = pmc->platform_bus_first_irq + i;
1101         sysbus_connect_irq(s, i, qdev_get_gpio_in(mpicdev, irqn));
1102     }
1103 
1104     memory_region_add_subregion(address_space_mem,
1105                                 pmc->platform_bus_base,
1106                                 &pms->pbus_dev->mmio);
1107 
1108     dinfo = drive_get(IF_PFLASH, 0, 0);
1109     if (dinfo) {
1110         BlockBackend *blk = blk_by_legacy_dinfo(dinfo);
1111         BlockDriverState *bs = blk_bs(blk);
1112         uint64_t mmio_size = memory_region_size(&pms->pbus_dev->mmio);
1113         uint64_t size = bdrv_getlength(bs);
1114         uint32_t sector_len = 64 * KiB;
1115 
1116         if (!is_power_of_2(size)) {
1117             error_report("Size of pflash file must be a power of two.");
1118             exit(1);
1119         }
1120 
1121         if (size > mmio_size) {
1122             error_report("Size of pflash file must not be bigger than %" PRIu64
1123                          " bytes.", mmio_size);
1124             exit(1);
1125         }
1126 
1127         if (!QEMU_IS_ALIGNED(size, sector_len)) {
1128             error_report("Size of pflash file must be a multiple of %" PRIu32
1129                          ".", sector_len);
1130             exit(1);
1131         }
1132 
1133         dev = qdev_new(TYPE_PFLASH_CFI01);
1134         qdev_prop_set_drive(dev, "drive", blk);
1135         qdev_prop_set_uint32(dev, "num-blocks", size / sector_len);
1136         qdev_prop_set_uint64(dev, "sector-length", sector_len);
1137         qdev_prop_set_uint8(dev, "width", 2);
1138         qdev_prop_set_bit(dev, "big-endian", true);
1139         qdev_prop_set_uint16(dev, "id0", 0x89);
1140         qdev_prop_set_uint16(dev, "id1", 0x18);
1141         qdev_prop_set_uint16(dev, "id2", 0x0000);
1142         qdev_prop_set_uint16(dev, "id3", 0x0);
1143         qdev_prop_set_string(dev, "name", "e500.flash");
1144         sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
1145 
1146         memory_region_add_subregion(&pms->pbus_dev->mmio, 0,
1147                                     pflash_cfi01_get_memory(PFLASH_CFI01(dev)));
1148     }
1149 
1150     /*
1151      * Smart firmware defaults ahead!
1152      *
1153      * We follow the following table to select which payload we execute.
1154      *
1155      *  -kernel | -bios | payload
1156      * ---------+-------+---------
1157      *     N    |   Y   | u-boot
1158      *     N    |   N   | u-boot
1159      *     Y    |   Y   | u-boot
1160      *     Y    |   N   | kernel
1161      *
1162      * This ensures backwards compatibility with how we used to expose
1163      * -kernel to users but allows them to run through u-boot as well.
1164      */
1165     kernel_as_payload = false;
1166     if (machine->firmware == NULL) {
1167         if (machine->kernel_filename) {
1168             payload_name = machine->kernel_filename;
1169             kernel_as_payload = true;
1170         } else {
1171             payload_name = "u-boot.e500";
1172         }
1173     } else {
1174         payload_name = machine->firmware;
1175     }
1176 
1177     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, payload_name);
1178     if (!filename) {
1179         error_report("could not find firmware/kernel file '%s'", payload_name);
1180         exit(1);
1181     }
1182 
1183     payload_size = load_elf(filename, NULL, NULL, NULL,
1184                             &bios_entry, &loadaddr, NULL, NULL,
1185                             1, PPC_ELF_MACHINE, 0, 0);
1186     if (payload_size < 0) {
1187         /*
1188          * Hrm. No ELF image? Try a uImage, maybe someone is giving us an
1189          * ePAPR compliant kernel
1190          */
1191         loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
1192         payload_size = load_uimage(filename, &bios_entry, &loadaddr, NULL,
1193                                    NULL, NULL);
1194         if (payload_size < 0) {
1195             error_report("could not load firmware '%s'", filename);
1196             exit(1);
1197         }
1198     }
1199 
1200     g_free(filename);
1201 
1202     if (kernel_as_payload) {
1203         kernel_base = loadaddr;
1204         kernel_size = payload_size;
1205     }
1206 
1207     cur_base = loadaddr + payload_size;
1208     if (cur_base < 32 * MiB) {
1209         /* u-boot occupies memory up to 32MB, so load blobs above */
1210         cur_base = 32 * MiB;
1211     }
1212 
1213     /* Load bare kernel only if no bios/u-boot has been provided */
1214     if (machine->kernel_filename && !kernel_as_payload) {
1215         kernel_base = cur_base;
1216         kernel_size = load_image_targphys(machine->kernel_filename,
1217                                           cur_base,
1218                                           machine->ram_size - cur_base);
1219         if (kernel_size < 0) {
1220             error_report("could not load kernel '%s'",
1221                          machine->kernel_filename);
1222             exit(1);
1223         }
1224 
1225         cur_base += kernel_size;
1226     }
1227 
1228     /* Load initrd. */
1229     if (machine->initrd_filename) {
1230         initrd_base = (cur_base + INITRD_LOAD_PAD) & ~INITRD_PAD_MASK;
1231         initrd_size = load_image_targphys(machine->initrd_filename, initrd_base,
1232                                           machine->ram_size - initrd_base);
1233 
1234         if (initrd_size < 0) {
1235             error_report("could not load initial ram disk '%s'",
1236                          machine->initrd_filename);
1237             exit(1);
1238         }
1239 
1240         cur_base = initrd_base + initrd_size;
1241     }
1242 
1243     /*
1244      * Reserve space for dtb behind the kernel image because Linux has a bug
1245      * where it can only handle the dtb if it's within the first 64MB of where
1246      * <kernel> starts. dtb cannot not reach initrd_base because INITRD_LOAD_PAD
1247      * ensures enough space between kernel and initrd.
1248      */
1249     dt_base = (loadaddr + payload_size + DTC_LOAD_PAD) & ~DTC_PAD_MASK;
1250     if (dt_base + DTB_MAX_SIZE > machine->ram_size) {
1251             error_report("not enough memory for device tree");
1252             exit(1);
1253     }
1254 
1255     dt_size = ppce500_prep_device_tree(pms, dt_base,
1256                                        initrd_base, initrd_size,
1257                                        kernel_base, kernel_size);
1258     if (dt_size < 0) {
1259         error_report("couldn't load device tree");
1260         exit(1);
1261     }
1262     assert(dt_size < DTB_MAX_SIZE);
1263 
1264     boot_info->entry = bios_entry;
1265     boot_info->dt_base = dt_base;
1266     boot_info->dt_size = dt_size;
1267 }
1268 
1269 static void e500_ccsr_initfn(Object *obj)
1270 {
1271     PPCE500CCSRState *ccsr = CCSR(obj);
1272     memory_region_init(&ccsr->ccsr_space, obj, "e500-ccsr",
1273                        MPC8544_CCSRBAR_SIZE);
1274 }
1275 
1276 static const TypeInfo e500_ccsr_info = {
1277     .name          = TYPE_CCSR,
1278     .parent        = TYPE_SYS_BUS_DEVICE,
1279     .instance_size = sizeof(PPCE500CCSRState),
1280     .instance_init = e500_ccsr_initfn,
1281 };
1282 
1283 static const TypeInfo ppce500_info = {
1284     .name          = TYPE_PPCE500_MACHINE,
1285     .parent        = TYPE_MACHINE,
1286     .abstract      = true,
1287     .instance_size = sizeof(PPCE500MachineState),
1288     .class_size    = sizeof(PPCE500MachineClass),
1289 };
1290 
1291 static void e500_register_types(void)
1292 {
1293     type_register_static(&e500_ccsr_info);
1294     type_register_static(&ppce500_info);
1295 }
1296 
1297 type_init(e500_register_types)
1298