xref: /openbmc/qemu/hw/ppc/e500.c (revision acb0ef58)
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 EPAPR_MAGIC                (0x45504150)
41 #define BINARY_DEVICE_TREE_FILE    "mpc8544ds.dtb"
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_fdt_add_subnode(fdt, ser);
111     qemu_fdt_setprop_string(fdt, ser, "device_type", "serial");
112     qemu_fdt_setprop_string(fdt, ser, "compatible", "ns16550");
113     qemu_fdt_setprop_cells(fdt, ser, "reg", offset, 0x100);
114     qemu_fdt_setprop_cell(fdt, ser, "cell-index", idx);
115     qemu_fdt_setprop_cell(fdt, ser, "clock-frequency", 0);
116     qemu_fdt_setprop_cells(fdt, ser, "interrupts", 42, 2);
117     qemu_fdt_setprop_phandle(fdt, ser, "interrupt-parent", mpic);
118     qemu_fdt_setprop_string(fdt, "/aliases", alias, ser);
119 
120     if (defcon) {
121         qemu_fdt_setprop_string(fdt, "/chosen", "linux,stdout-path", ser);
122     }
123 }
124 
125 static int ppce500_load_device_tree(MachineState *machine,
126                                     PPCE500Params *params,
127                                     hwaddr addr,
128                                     hwaddr initrd_base,
129                                     hwaddr initrd_size,
130                                     hwaddr kernel_base,
131                                     hwaddr kernel_size,
132                                     bool dry_run)
133 {
134     CPUPPCState *env = first_cpu->env_ptr;
135     int ret = -1;
136     uint64_t mem_reg_property[] = { 0, cpu_to_be64(machine->ram_size) };
137     int fdt_size;
138     void *fdt;
139     uint8_t hypercall[16];
140     uint32_t clock_freq = 400000000;
141     uint32_t tb_freq = 400000000;
142     int i;
143     char compatible_sb[] = "fsl,mpc8544-immr\0simple-bus";
144     char soc[128];
145     char mpic[128];
146     uint32_t mpic_ph;
147     uint32_t msi_ph;
148     char gutil[128];
149     char pci[128];
150     char msi[128];
151     uint32_t *pci_map = NULL;
152     int len;
153     uint32_t pci_ranges[14] =
154         {
155             0x2000000, 0x0, 0xc0000000,
156             0x0, 0xc0000000,
157             0x0, 0x20000000,
158 
159             0x1000000, 0x0, 0x0,
160             0x0, 0xe1000000,
161             0x0, 0x10000,
162         };
163     QemuOpts *machine_opts = qemu_get_machine_opts();
164     const char *dtb_file = qemu_opt_get(machine_opts, "dtb");
165     const char *toplevel_compat = qemu_opt_get(machine_opts, "dt_compatible");
166 
167     if (dtb_file) {
168         char *filename;
169         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_file);
170         if (!filename) {
171             goto out;
172         }
173 
174         fdt = load_device_tree(filename, &fdt_size);
175         if (!fdt) {
176             goto out;
177         }
178         goto done;
179     }
180 
181     fdt = create_device_tree(&fdt_size);
182     if (fdt == NULL) {
183         goto out;
184     }
185 
186     /* Manipulate device tree in memory. */
187     qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 2);
188     qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 2);
189 
190     qemu_fdt_add_subnode(fdt, "/memory");
191     qemu_fdt_setprop_string(fdt, "/memory", "device_type", "memory");
192     qemu_fdt_setprop(fdt, "/memory", "reg", mem_reg_property,
193                      sizeof(mem_reg_property));
194 
195     qemu_fdt_add_subnode(fdt, "/chosen");
196     if (initrd_size) {
197         ret = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
198                                     initrd_base);
199         if (ret < 0) {
200             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
201         }
202 
203         ret = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
204                                     (initrd_base + initrd_size));
205         if (ret < 0) {
206             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
207         }
208 
209     }
210 
211     if (kernel_base != -1ULL) {
212         qemu_fdt_setprop_cells(fdt, "/chosen", "qemu,boot-kernel",
213                                      kernel_base >> 32, kernel_base,
214                                      kernel_size >> 32, kernel_size);
215     }
216 
217     ret = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
218                                       machine->kernel_cmdline);
219     if (ret < 0)
220         fprintf(stderr, "couldn't set /chosen/bootargs\n");
221 
222     if (kvm_enabled()) {
223         /* Read out host's frequencies */
224         clock_freq = kvmppc_get_clockfreq();
225         tb_freq = kvmppc_get_tbfreq();
226 
227         /* indicate KVM hypercall interface */
228         qemu_fdt_add_subnode(fdt, "/hypervisor");
229         qemu_fdt_setprop_string(fdt, "/hypervisor", "compatible",
230                                 "linux,kvm");
231         kvmppc_get_hypercall(env, hypercall, sizeof(hypercall));
232         qemu_fdt_setprop(fdt, "/hypervisor", "hcall-instructions",
233                          hypercall, sizeof(hypercall));
234         /* if KVM supports the idle hcall, set property indicating this */
235         if (kvmppc_get_hasidle(env)) {
236             qemu_fdt_setprop(fdt, "/hypervisor", "has-idle", NULL, 0);
237         }
238     }
239 
240     /* Create CPU nodes */
241     qemu_fdt_add_subnode(fdt, "/cpus");
242     qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 1);
243     qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0);
244 
245     /* We need to generate the cpu nodes in reverse order, so Linux can pick
246        the first node as boot node and be happy */
247     for (i = smp_cpus - 1; i >= 0; i--) {
248         CPUState *cpu;
249         PowerPCCPU *pcpu;
250         char cpu_name[128];
251         uint64_t cpu_release_addr = MPC8544_SPIN_BASE + (i * 0x20);
252 
253         cpu = qemu_get_cpu(i);
254         if (cpu == NULL) {
255             continue;
256         }
257         env = cpu->env_ptr;
258         pcpu = POWERPC_CPU(cpu);
259 
260         snprintf(cpu_name, sizeof(cpu_name), "/cpus/PowerPC,8544@%x",
261                  ppc_get_vcpu_dt_id(pcpu));
262         qemu_fdt_add_subnode(fdt, cpu_name);
263         qemu_fdt_setprop_cell(fdt, cpu_name, "clock-frequency", clock_freq);
264         qemu_fdt_setprop_cell(fdt, cpu_name, "timebase-frequency", tb_freq);
265         qemu_fdt_setprop_string(fdt, cpu_name, "device_type", "cpu");
266         qemu_fdt_setprop_cell(fdt, cpu_name, "reg",
267                               ppc_get_vcpu_dt_id(pcpu));
268         qemu_fdt_setprop_cell(fdt, cpu_name, "d-cache-line-size",
269                               env->dcache_line_size);
270         qemu_fdt_setprop_cell(fdt, cpu_name, "i-cache-line-size",
271                               env->icache_line_size);
272         qemu_fdt_setprop_cell(fdt, cpu_name, "d-cache-size", 0x8000);
273         qemu_fdt_setprop_cell(fdt, cpu_name, "i-cache-size", 0x8000);
274         qemu_fdt_setprop_cell(fdt, cpu_name, "bus-frequency", 0);
275         if (cpu->cpu_index) {
276             qemu_fdt_setprop_string(fdt, cpu_name, "status", "disabled");
277             qemu_fdt_setprop_string(fdt, cpu_name, "enable-method",
278                                     "spin-table");
279             qemu_fdt_setprop_u64(fdt, cpu_name, "cpu-release-addr",
280                                  cpu_release_addr);
281         } else {
282             qemu_fdt_setprop_string(fdt, cpu_name, "status", "okay");
283         }
284     }
285 
286     qemu_fdt_add_subnode(fdt, "/aliases");
287     /* XXX These should go into their respective devices' code */
288     snprintf(soc, sizeof(soc), "/soc@%llx", MPC8544_CCSRBAR_BASE);
289     qemu_fdt_add_subnode(fdt, soc);
290     qemu_fdt_setprop_string(fdt, soc, "device_type", "soc");
291     qemu_fdt_setprop(fdt, soc, "compatible", compatible_sb,
292                      sizeof(compatible_sb));
293     qemu_fdt_setprop_cell(fdt, soc, "#address-cells", 1);
294     qemu_fdt_setprop_cell(fdt, soc, "#size-cells", 1);
295     qemu_fdt_setprop_cells(fdt, soc, "ranges", 0x0,
296                            MPC8544_CCSRBAR_BASE >> 32, MPC8544_CCSRBAR_BASE,
297                            MPC8544_CCSRBAR_SIZE);
298     /* XXX should contain a reasonable value */
299     qemu_fdt_setprop_cell(fdt, soc, "bus-frequency", 0);
300 
301     snprintf(mpic, sizeof(mpic), "%s/pic@%llx", soc, MPC8544_MPIC_REGS_OFFSET);
302     qemu_fdt_add_subnode(fdt, mpic);
303     qemu_fdt_setprop_string(fdt, mpic, "device_type", "open-pic");
304     qemu_fdt_setprop_string(fdt, mpic, "compatible", "fsl,mpic");
305     qemu_fdt_setprop_cells(fdt, mpic, "reg", MPC8544_MPIC_REGS_OFFSET,
306                            0x40000);
307     qemu_fdt_setprop_cell(fdt, mpic, "#address-cells", 0);
308     qemu_fdt_setprop_cell(fdt, mpic, "#interrupt-cells", 2);
309     mpic_ph = qemu_fdt_alloc_phandle(fdt);
310     qemu_fdt_setprop_cell(fdt, mpic, "phandle", mpic_ph);
311     qemu_fdt_setprop_cell(fdt, mpic, "linux,phandle", mpic_ph);
312     qemu_fdt_setprop(fdt, mpic, "interrupt-controller", NULL, 0);
313 
314     /*
315      * We have to generate ser1 first, because Linux takes the first
316      * device it finds in the dt as serial output device. And we generate
317      * devices in reverse order to the dt.
318      */
319     dt_serial_create(fdt, MPC8544_SERIAL1_REGS_OFFSET,
320                      soc, mpic, "serial1", 1, false);
321     dt_serial_create(fdt, MPC8544_SERIAL0_REGS_OFFSET,
322                      soc, mpic, "serial0", 0, true);
323 
324     snprintf(gutil, sizeof(gutil), "%s/global-utilities@%llx", soc,
325              MPC8544_UTIL_OFFSET);
326     qemu_fdt_add_subnode(fdt, gutil);
327     qemu_fdt_setprop_string(fdt, gutil, "compatible", "fsl,mpc8544-guts");
328     qemu_fdt_setprop_cells(fdt, gutil, "reg", MPC8544_UTIL_OFFSET, 0x1000);
329     qemu_fdt_setprop(fdt, gutil, "fsl,has-rstcr", NULL, 0);
330 
331     snprintf(msi, sizeof(msi), "/%s/msi@%llx", soc, MPC8544_MSI_REGS_OFFSET);
332     qemu_fdt_add_subnode(fdt, msi);
333     qemu_fdt_setprop_string(fdt, msi, "compatible", "fsl,mpic-msi");
334     qemu_fdt_setprop_cells(fdt, msi, "reg", MPC8544_MSI_REGS_OFFSET, 0x200);
335     msi_ph = qemu_fdt_alloc_phandle(fdt);
336     qemu_fdt_setprop_cells(fdt, msi, "msi-available-ranges", 0x0, 0x100);
337     qemu_fdt_setprop_phandle(fdt, msi, "interrupt-parent", mpic);
338     qemu_fdt_setprop_cells(fdt, msi, "interrupts",
339         0xe0, 0x0,
340         0xe1, 0x0,
341         0xe2, 0x0,
342         0xe3, 0x0,
343         0xe4, 0x0,
344         0xe5, 0x0,
345         0xe6, 0x0,
346         0xe7, 0x0);
347     qemu_fdt_setprop_cell(fdt, msi, "phandle", msi_ph);
348     qemu_fdt_setprop_cell(fdt, msi, "linux,phandle", msi_ph);
349 
350     snprintf(pci, sizeof(pci), "/pci@%llx", MPC8544_PCI_REGS_BASE);
351     qemu_fdt_add_subnode(fdt, pci);
352     qemu_fdt_setprop_cell(fdt, pci, "cell-index", 0);
353     qemu_fdt_setprop_string(fdt, pci, "compatible", "fsl,mpc8540-pci");
354     qemu_fdt_setprop_string(fdt, pci, "device_type", "pci");
355     qemu_fdt_setprop_cells(fdt, pci, "interrupt-map-mask", 0xf800, 0x0,
356                            0x0, 0x7);
357     pci_map = pci_map_create(fdt, qemu_fdt_get_phandle(fdt, mpic),
358                              params->pci_first_slot, params->pci_nr_slots,
359                              &len);
360     qemu_fdt_setprop(fdt, pci, "interrupt-map", pci_map, len);
361     qemu_fdt_setprop_phandle(fdt, pci, "interrupt-parent", mpic);
362     qemu_fdt_setprop_cells(fdt, pci, "interrupts", 24, 2);
363     qemu_fdt_setprop_cells(fdt, pci, "bus-range", 0, 255);
364     for (i = 0; i < 14; i++) {
365         pci_ranges[i] = cpu_to_be32(pci_ranges[i]);
366     }
367     qemu_fdt_setprop_cell(fdt, pci, "fsl,msi", msi_ph);
368     qemu_fdt_setprop(fdt, pci, "ranges", pci_ranges, sizeof(pci_ranges));
369     qemu_fdt_setprop_cells(fdt, pci, "reg", MPC8544_PCI_REGS_BASE >> 32,
370                            MPC8544_PCI_REGS_BASE, 0, 0x1000);
371     qemu_fdt_setprop_cell(fdt, pci, "clock-frequency", 66666666);
372     qemu_fdt_setprop_cell(fdt, pci, "#interrupt-cells", 1);
373     qemu_fdt_setprop_cell(fdt, pci, "#size-cells", 2);
374     qemu_fdt_setprop_cell(fdt, pci, "#address-cells", 3);
375     qemu_fdt_setprop_string(fdt, "/aliases", "pci0", pci);
376 
377     params->fixup_devtree(params, fdt);
378 
379     if (toplevel_compat) {
380         qemu_fdt_setprop(fdt, "/", "compatible", toplevel_compat,
381                          strlen(toplevel_compat) + 1);
382     }
383 
384 done:
385     if (!dry_run) {
386         qemu_fdt_dumpdtb(fdt, fdt_size);
387         cpu_physical_memory_write(addr, fdt, fdt_size);
388     }
389     ret = fdt_size;
390 
391 out:
392     g_free(pci_map);
393 
394     return ret;
395 }
396 
397 typedef struct DeviceTreeParams {
398     MachineState *machine;
399     PPCE500Params params;
400     hwaddr addr;
401     hwaddr initrd_base;
402     hwaddr initrd_size;
403     hwaddr kernel_base;
404     hwaddr kernel_size;
405 } DeviceTreeParams;
406 
407 static void ppce500_reset_device_tree(void *opaque)
408 {
409     DeviceTreeParams *p = opaque;
410     ppce500_load_device_tree(p->machine, &p->params, p->addr, p->initrd_base,
411                              p->initrd_size, p->kernel_base, p->kernel_size,
412                              false);
413 }
414 
415 static int ppce500_prep_device_tree(MachineState *machine,
416                                     PPCE500Params *params,
417                                     hwaddr addr,
418                                     hwaddr initrd_base,
419                                     hwaddr initrd_size,
420                                     hwaddr kernel_base,
421                                     hwaddr kernel_size)
422 {
423     DeviceTreeParams *p = g_new(DeviceTreeParams, 1);
424     p->machine = machine;
425     p->params = *params;
426     p->addr = addr;
427     p->initrd_base = initrd_base;
428     p->initrd_size = initrd_size;
429     p->kernel_base = kernel_base;
430     p->kernel_size = kernel_size;
431 
432     qemu_register_reset(ppce500_reset_device_tree, p);
433 
434     /* Issue the device tree loader once, so that we get the size of the blob */
435     return ppce500_load_device_tree(machine, params, addr, initrd_base,
436                                     initrd_size, kernel_base, kernel_size,
437                                     true);
438 }
439 
440 /* Create -kernel TLB entries for BookE.  */
441 static inline hwaddr booke206_page_size_to_tlb(uint64_t size)
442 {
443     return 63 - clz64(size >> 10);
444 }
445 
446 static int booke206_initial_map_tsize(CPUPPCState *env)
447 {
448     struct boot_info *bi = env->load_info;
449     hwaddr dt_end;
450     int ps;
451 
452     /* Our initial TLB entry needs to cover everything from 0 to
453        the device tree top */
454     dt_end = bi->dt_base + bi->dt_size;
455     ps = booke206_page_size_to_tlb(dt_end) + 1;
456     if (ps & 1) {
457         /* e500v2 can only do even TLB size bits */
458         ps++;
459     }
460     return ps;
461 }
462 
463 static uint64_t mmubooke_initial_mapsize(CPUPPCState *env)
464 {
465     int tsize;
466 
467     tsize = booke206_initial_map_tsize(env);
468     return (1ULL << 10 << tsize);
469 }
470 
471 static void mmubooke_create_initial_mapping(CPUPPCState *env)
472 {
473     ppcmas_tlb_t *tlb = booke206_get_tlbm(env, 1, 0, 0);
474     hwaddr size;
475     int ps;
476 
477     ps = booke206_initial_map_tsize(env);
478     size = (ps << MAS1_TSIZE_SHIFT);
479     tlb->mas1 = MAS1_VALID | size;
480     tlb->mas2 = 0;
481     tlb->mas7_3 = 0;
482     tlb->mas7_3 |= MAS3_UR | MAS3_UW | MAS3_UX | MAS3_SR | MAS3_SW | MAS3_SX;
483 
484     env->tlb_dirty = true;
485 }
486 
487 static void ppce500_cpu_reset_sec(void *opaque)
488 {
489     PowerPCCPU *cpu = opaque;
490     CPUState *cs = CPU(cpu);
491 
492     cpu_reset(cs);
493 
494     /* Secondary CPU starts in halted state for now. Needs to change when
495        implementing non-kernel boot. */
496     cs->halted = 1;
497     cs->exception_index = EXCP_HLT;
498 }
499 
500 static void ppce500_cpu_reset(void *opaque)
501 {
502     PowerPCCPU *cpu = opaque;
503     CPUState *cs = CPU(cpu);
504     CPUPPCState *env = &cpu->env;
505     struct boot_info *bi = env->load_info;
506 
507     cpu_reset(cs);
508 
509     /* Set initial guest state. */
510     cs->halted = 0;
511     env->gpr[1] = (16<<20) - 8;
512     env->gpr[3] = bi->dt_base;
513     env->gpr[4] = 0;
514     env->gpr[5] = 0;
515     env->gpr[6] = EPAPR_MAGIC;
516     env->gpr[7] = mmubooke_initial_mapsize(env);
517     env->gpr[8] = 0;
518     env->gpr[9] = 0;
519     env->nip = bi->entry;
520     mmubooke_create_initial_mapping(env);
521 }
522 
523 static DeviceState *ppce500_init_mpic_qemu(PPCE500Params *params,
524                                            qemu_irq **irqs)
525 {
526     DeviceState *dev;
527     SysBusDevice *s;
528     int i, j, k;
529 
530     dev = qdev_create(NULL, TYPE_OPENPIC);
531     qdev_prop_set_uint32(dev, "model", params->mpic_version);
532     qdev_prop_set_uint32(dev, "nb_cpus", smp_cpus);
533 
534     qdev_init_nofail(dev);
535     s = SYS_BUS_DEVICE(dev);
536 
537     k = 0;
538     for (i = 0; i < smp_cpus; i++) {
539         for (j = 0; j < OPENPIC_OUTPUT_NB; j++) {
540             sysbus_connect_irq(s, k++, irqs[i][j]);
541         }
542     }
543 
544     return dev;
545 }
546 
547 static DeviceState *ppce500_init_mpic_kvm(PPCE500Params *params,
548                                           qemu_irq **irqs)
549 {
550     DeviceState *dev;
551     CPUState *cs;
552     int r;
553 
554     dev = qdev_create(NULL, TYPE_KVM_OPENPIC);
555     qdev_prop_set_uint32(dev, "model", params->mpic_version);
556 
557     r = qdev_init(dev);
558     if (r) {
559         return NULL;
560     }
561 
562     CPU_FOREACH(cs) {
563         if (kvm_openpic_connect_vcpu(dev, cs)) {
564             fprintf(stderr, "%s: failed to connect vcpu to irqchip\n",
565                     __func__);
566             abort();
567         }
568     }
569 
570     return dev;
571 }
572 
573 static qemu_irq *ppce500_init_mpic(PPCE500Params *params, MemoryRegion *ccsr,
574                                    qemu_irq **irqs)
575 {
576     qemu_irq *mpic;
577     DeviceState *dev = NULL;
578     SysBusDevice *s;
579     int i;
580 
581     mpic = g_new(qemu_irq, 256);
582 
583     if (kvm_enabled()) {
584         QemuOpts *machine_opts = qemu_get_machine_opts();
585         bool irqchip_allowed = qemu_opt_get_bool(machine_opts,
586                                                 "kernel_irqchip", true);
587         bool irqchip_required = qemu_opt_get_bool(machine_opts,
588                                                   "kernel_irqchip", false);
589 
590         if (irqchip_allowed) {
591             dev = ppce500_init_mpic_kvm(params, irqs);
592         }
593 
594         if (irqchip_required && !dev) {
595             fprintf(stderr, "%s: irqchip requested but unavailable\n",
596                     __func__);
597             abort();
598         }
599     }
600 
601     if (!dev) {
602         dev = ppce500_init_mpic_qemu(params, irqs);
603     }
604 
605     for (i = 0; i < 256; i++) {
606         mpic[i] = qdev_get_gpio_in(dev, i);
607     }
608 
609     s = SYS_BUS_DEVICE(dev);
610     memory_region_add_subregion(ccsr, MPC8544_MPIC_REGS_OFFSET,
611                                 s->mmio[0].memory);
612 
613     return mpic;
614 }
615 
616 void ppce500_init(MachineState *machine, PPCE500Params *params)
617 {
618     MemoryRegion *address_space_mem = get_system_memory();
619     MemoryRegion *ram = g_new(MemoryRegion, 1);
620     PCIBus *pci_bus;
621     CPUPPCState *env = NULL;
622     uint64_t loadaddr;
623     hwaddr kernel_base = -1LL;
624     int kernel_size = 0;
625     hwaddr dt_base = 0;
626     hwaddr initrd_base = 0;
627     int initrd_size = 0;
628     hwaddr cur_base = 0;
629     char *filename;
630     hwaddr bios_entry = 0;
631     target_long bios_size;
632     struct boot_info *boot_info;
633     int dt_size;
634     int i;
635     /* irq num for pin INTA, INTB, INTC and INTD is 1, 2, 3 and
636      * 4 respectively */
637     unsigned int pci_irq_nrs[PCI_NUM_PINS] = {1, 2, 3, 4};
638     qemu_irq **irqs, *mpic;
639     DeviceState *dev;
640     CPUPPCState *firstenv = NULL;
641     MemoryRegion *ccsr_addr_space;
642     SysBusDevice *s;
643     PPCE500CCSRState *ccsr;
644 
645     /* Setup CPUs */
646     if (machine->cpu_model == NULL) {
647         machine->cpu_model = "e500v2_v30";
648     }
649 
650     irqs = g_malloc0(smp_cpus * sizeof(qemu_irq *));
651     irqs[0] = g_malloc0(smp_cpus * sizeof(qemu_irq) * OPENPIC_OUTPUT_NB);
652     for (i = 0; i < smp_cpus; i++) {
653         PowerPCCPU *cpu;
654         CPUState *cs;
655         qemu_irq *input;
656 
657         cpu = cpu_ppc_init(machine->cpu_model);
658         if (cpu == NULL) {
659             fprintf(stderr, "Unable to initialize CPU!\n");
660             exit(1);
661         }
662         env = &cpu->env;
663         cs = CPU(cpu);
664 
665         if (!firstenv) {
666             firstenv = env;
667         }
668 
669         irqs[i] = irqs[0] + (i * OPENPIC_OUTPUT_NB);
670         input = (qemu_irq *)env->irq_inputs;
671         irqs[i][OPENPIC_OUTPUT_INT] = input[PPCE500_INPUT_INT];
672         irqs[i][OPENPIC_OUTPUT_CINT] = input[PPCE500_INPUT_CINT];
673         env->spr_cb[SPR_BOOKE_PIR].default_value = cs->cpu_index = i;
674         env->mpic_iack = MPC8544_CCSRBAR_BASE +
675                          MPC8544_MPIC_REGS_OFFSET + 0xa0;
676 
677         ppc_booke_timers_init(cpu, 400000000, PPC_TIMER_E500);
678 
679         /* Register reset handler */
680         if (!i) {
681             /* Primary CPU */
682             struct boot_info *boot_info;
683             boot_info = g_malloc0(sizeof(struct boot_info));
684             qemu_register_reset(ppce500_cpu_reset, cpu);
685             env->load_info = boot_info;
686         } else {
687             /* Secondary CPUs */
688             qemu_register_reset(ppce500_cpu_reset_sec, cpu);
689         }
690     }
691 
692     env = firstenv;
693 
694     /* Fixup Memory size on a alignment boundary */
695     ram_size &= ~(RAM_SIZES_ALIGN - 1);
696     machine->ram_size = ram_size;
697 
698     /* Register Memory */
699     memory_region_init_ram(ram, NULL, "mpc8544ds.ram", ram_size);
700     vmstate_register_ram_global(ram);
701     memory_region_add_subregion(address_space_mem, 0, ram);
702 
703     dev = qdev_create(NULL, "e500-ccsr");
704     object_property_add_child(qdev_get_machine(), "e500-ccsr",
705                               OBJECT(dev), NULL);
706     qdev_init_nofail(dev);
707     ccsr = CCSR(dev);
708     ccsr_addr_space = &ccsr->ccsr_space;
709     memory_region_add_subregion(address_space_mem, MPC8544_CCSRBAR_BASE,
710                                 ccsr_addr_space);
711 
712     mpic = ppce500_init_mpic(params, ccsr_addr_space, irqs);
713 
714     /* Serial */
715     if (serial_hds[0]) {
716         serial_mm_init(ccsr_addr_space, MPC8544_SERIAL0_REGS_OFFSET,
717                        0, mpic[42], 399193,
718                        serial_hds[0], DEVICE_BIG_ENDIAN);
719     }
720 
721     if (serial_hds[1]) {
722         serial_mm_init(ccsr_addr_space, MPC8544_SERIAL1_REGS_OFFSET,
723                        0, mpic[42], 399193,
724                        serial_hds[1], DEVICE_BIG_ENDIAN);
725     }
726 
727     /* General Utility device */
728     dev = qdev_create(NULL, "mpc8544-guts");
729     qdev_init_nofail(dev);
730     s = SYS_BUS_DEVICE(dev);
731     memory_region_add_subregion(ccsr_addr_space, MPC8544_UTIL_OFFSET,
732                                 sysbus_mmio_get_region(s, 0));
733 
734     /* PCI */
735     dev = qdev_create(NULL, "e500-pcihost");
736     qdev_prop_set_uint32(dev, "first_slot", params->pci_first_slot);
737     qdev_prop_set_uint32(dev, "first_pin_irq", pci_irq_nrs[0]);
738     qdev_init_nofail(dev);
739     s = SYS_BUS_DEVICE(dev);
740     for (i = 0; i < PCI_NUM_PINS; i++) {
741         sysbus_connect_irq(s, i, mpic[pci_irq_nrs[i]]);
742     }
743 
744     memory_region_add_subregion(ccsr_addr_space, MPC8544_PCI_REGS_OFFSET,
745                                 sysbus_mmio_get_region(s, 0));
746 
747     pci_bus = (PCIBus *)qdev_get_child_bus(dev, "pci.0");
748     if (!pci_bus)
749         printf("couldn't create PCI controller!\n");
750 
751     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 1, MPC8544_PCI_IO);
752 
753     if (pci_bus) {
754         /* Register network interfaces. */
755         for (i = 0; i < nb_nics; i++) {
756             pci_nic_init_nofail(&nd_table[i], pci_bus, "virtio", NULL);
757         }
758     }
759 
760     /* Register spinning region */
761     sysbus_create_simple("e500-spin", MPC8544_SPIN_BASE, NULL);
762 
763     if (cur_base < (32 * 1024 * 1024)) {
764         /* u-boot occupies memory up to 32MB, so load blobs above */
765         cur_base = (32 * 1024 * 1024);
766     }
767 
768     /* Load kernel. */
769     if (machine->kernel_filename) {
770         kernel_base = cur_base;
771         kernel_size = load_image_targphys(machine->kernel_filename,
772                                           cur_base,
773                                           ram_size - cur_base);
774         if (kernel_size < 0) {
775             fprintf(stderr, "qemu: could not load kernel '%s'\n",
776                     machine->kernel_filename);
777             exit(1);
778         }
779 
780         cur_base += kernel_size;
781     }
782 
783     /* Load initrd. */
784     if (machine->initrd_filename) {
785         initrd_base = (cur_base + INITRD_LOAD_PAD) & ~INITRD_PAD_MASK;
786         initrd_size = load_image_targphys(machine->initrd_filename, initrd_base,
787                                           ram_size - initrd_base);
788 
789         if (initrd_size < 0) {
790             fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
791                     machine->initrd_filename);
792             exit(1);
793         }
794 
795         cur_base = initrd_base + initrd_size;
796     }
797 
798     /*
799      * Smart firmware defaults ahead!
800      *
801      * We follow the following table to select which payload we execute.
802      *
803      *  -kernel | -bios | payload
804      * ---------+-------+---------
805      *     N    |   Y   | u-boot
806      *     N    |   N   | u-boot
807      *     Y    |   Y   | u-boot
808      *     Y    |   N   | kernel
809      *
810      * This ensures backwards compatibility with how we used to expose
811      * -kernel to users but allows them to run through u-boot as well.
812      */
813     if (bios_name == NULL) {
814         if (machine->kernel_filename) {
815             bios_name = machine->kernel_filename;
816         } else {
817             bios_name = "u-boot.e500";
818         }
819     }
820     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
821 
822     bios_size = load_elf(filename, NULL, NULL, &bios_entry, &loadaddr, NULL,
823                          1, ELF_MACHINE, 0);
824     if (bios_size < 0) {
825         /*
826          * Hrm. No ELF image? Try a uImage, maybe someone is giving us an
827          * ePAPR compliant kernel
828          */
829         kernel_size = load_uimage(filename, &bios_entry, &loadaddr, NULL);
830         if (kernel_size < 0) {
831             fprintf(stderr, "qemu: could not load firmware '%s'\n", filename);
832             exit(1);
833         }
834     }
835 
836     /* Reserve space for dtb */
837     dt_base = (loadaddr + bios_size + DTC_LOAD_PAD) & ~DTC_PAD_MASK;
838 
839     dt_size = ppce500_prep_device_tree(machine, params, dt_base,
840                                        initrd_base, initrd_size,
841                                        kernel_base, kernel_size);
842     if (dt_size < 0) {
843         fprintf(stderr, "couldn't load device tree\n");
844         exit(1);
845     }
846     assert(dt_size < DTB_MAX_SIZE);
847 
848     boot_info = env->load_info;
849     boot_info->entry = bios_entry;
850     boot_info->dt_base = dt_base;
851     boot_info->dt_size = dt_size;
852 
853     if (kvm_enabled()) {
854         kvmppc_init();
855     }
856 }
857 
858 static int e500_ccsr_initfn(SysBusDevice *dev)
859 {
860     PPCE500CCSRState *ccsr;
861 
862     ccsr = CCSR(dev);
863     memory_region_init(&ccsr->ccsr_space, OBJECT(ccsr), "e500-ccsr",
864                        MPC8544_CCSRBAR_SIZE);
865     return 0;
866 }
867 
868 static void e500_ccsr_class_init(ObjectClass *klass, void *data)
869 {
870     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
871     k->init = e500_ccsr_initfn;
872 }
873 
874 static const TypeInfo e500_ccsr_info = {
875     .name          = TYPE_CCSR,
876     .parent        = TYPE_SYS_BUS_DEVICE,
877     .instance_size = sizeof(PPCE500CCSRState),
878     .class_init    = e500_ccsr_class_init,
879 };
880 
881 static void e500_register_types(void)
882 {
883     type_register_static(&e500_ccsr_info);
884 }
885 
886 type_init(e500_register_types)
887