xref: /openbmc/qemu/hw/ppc/pnv.c (revision 7fb3949d)
1 /*
2  * QEMU PowerPC PowerNV machine model
3  *
4  * Copyright (c) 2016, IBM Corporation.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qemu/units.h"
23 #include "qapi/error.h"
24 #include "sysemu/qtest.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/numa.h"
27 #include "sysemu/reset.h"
28 #include "sysemu/runstate.h"
29 #include "sysemu/cpus.h"
30 #include "sysemu/device_tree.h"
31 #include "sysemu/hw_accel.h"
32 #include "target/ppc/cpu.h"
33 #include "qemu/log.h"
34 #include "hw/ppc/fdt.h"
35 #include "hw/ppc/ppc.h"
36 #include "hw/ppc/pnv.h"
37 #include "hw/ppc/pnv_core.h"
38 #include "hw/loader.h"
39 #include "hw/nmi.h"
40 #include "exec/address-spaces.h"
41 #include "qapi/visitor.h"
42 #include "monitor/monitor.h"
43 #include "hw/intc/intc.h"
44 #include "hw/ipmi/ipmi.h"
45 #include "target/ppc/mmu-hash64.h"
46 #include "hw/pci/msi.h"
47 
48 #include "hw/ppc/xics.h"
49 #include "hw/qdev-properties.h"
50 #include "hw/ppc/pnv_xscom.h"
51 #include "hw/ppc/pnv_pnor.h"
52 
53 #include "hw/isa/isa.h"
54 #include "hw/boards.h"
55 #include "hw/char/serial.h"
56 #include "hw/rtc/mc146818rtc.h"
57 
58 #include <libfdt.h>
59 
60 #define FDT_MAX_SIZE            (1 * MiB)
61 
62 #define FW_FILE_NAME            "skiboot.lid"
63 #define FW_LOAD_ADDR            0x0
64 #define FW_MAX_SIZE             (4 * MiB)
65 
66 #define KERNEL_LOAD_ADDR        0x20000000
67 #define KERNEL_MAX_SIZE         (256 * MiB)
68 #define INITRD_LOAD_ADDR        0x60000000
69 #define INITRD_MAX_SIZE         (256 * MiB)
70 
71 static const char *pnv_chip_core_typename(const PnvChip *o)
72 {
73     const char *chip_type = object_class_get_name(object_get_class(OBJECT(o)));
74     int len = strlen(chip_type) - strlen(PNV_CHIP_TYPE_SUFFIX);
75     char *s = g_strdup_printf(PNV_CORE_TYPE_NAME("%.*s"), len, chip_type);
76     const char *core_type = object_class_get_name(object_class_by_name(s));
77     g_free(s);
78     return core_type;
79 }
80 
81 /*
82  * On Power Systems E880 (POWER8), the max cpus (threads) should be :
83  *     4 * 4 sockets * 12 cores * 8 threads = 1536
84  * Let's make it 2^11
85  */
86 #define MAX_CPUS                2048
87 
88 /*
89  * Memory nodes are created by hostboot, one for each range of memory
90  * that has a different "affinity". In practice, it means one range
91  * per chip.
92  */
93 static void pnv_dt_memory(void *fdt, int chip_id, hwaddr start, hwaddr size)
94 {
95     char *mem_name;
96     uint64_t mem_reg_property[2];
97     int off;
98 
99     mem_reg_property[0] = cpu_to_be64(start);
100     mem_reg_property[1] = cpu_to_be64(size);
101 
102     mem_name = g_strdup_printf("memory@%"HWADDR_PRIx, start);
103     off = fdt_add_subnode(fdt, 0, mem_name);
104     g_free(mem_name);
105 
106     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
107     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
108                        sizeof(mem_reg_property))));
109     _FDT((fdt_setprop_cell(fdt, off, "ibm,chip-id", chip_id)));
110 }
111 
112 static int get_cpus_node(void *fdt)
113 {
114     int cpus_offset = fdt_path_offset(fdt, "/cpus");
115 
116     if (cpus_offset < 0) {
117         cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
118         if (cpus_offset) {
119             _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
120             _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
121         }
122     }
123     _FDT(cpus_offset);
124     return cpus_offset;
125 }
126 
127 /*
128  * The PowerNV cores (and threads) need to use real HW ids and not an
129  * incremental index like it has been done on other platforms. This HW
130  * id is stored in the CPU PIR, it is used to create cpu nodes in the
131  * device tree, used in XSCOM to address cores and in interrupt
132  * servers.
133  */
134 static void pnv_dt_core(PnvChip *chip, PnvCore *pc, void *fdt)
135 {
136     PowerPCCPU *cpu = pc->threads[0];
137     CPUState *cs = CPU(cpu);
138     DeviceClass *dc = DEVICE_GET_CLASS(cs);
139     int smt_threads = CPU_CORE(pc)->nr_threads;
140     CPUPPCState *env = &cpu->env;
141     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
142     uint32_t servers_prop[smt_threads];
143     int i;
144     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
145                        0xffffffff, 0xffffffff};
146     uint32_t tbfreq = PNV_TIMEBASE_FREQ;
147     uint32_t cpufreq = 1000000000;
148     uint32_t page_sizes_prop[64];
149     size_t page_sizes_prop_size;
150     const uint8_t pa_features[] = { 24, 0,
151                                     0xf6, 0x3f, 0xc7, 0xc0, 0x80, 0xf0,
152                                     0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
153                                     0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
154                                     0x80, 0x00, 0x80, 0x00, 0x80, 0x00 };
155     int offset;
156     char *nodename;
157     int cpus_offset = get_cpus_node(fdt);
158 
159     nodename = g_strdup_printf("%s@%x", dc->fw_name, pc->pir);
160     offset = fdt_add_subnode(fdt, cpus_offset, nodename);
161     _FDT(offset);
162     g_free(nodename);
163 
164     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id", chip->chip_id)));
165 
166     _FDT((fdt_setprop_cell(fdt, offset, "reg", pc->pir)));
167     _FDT((fdt_setprop_cell(fdt, offset, "ibm,pir", pc->pir)));
168     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
169 
170     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
171     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
172                             env->dcache_line_size)));
173     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
174                             env->dcache_line_size)));
175     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
176                             env->icache_line_size)));
177     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
178                             env->icache_line_size)));
179 
180     if (pcc->l1_dcache_size) {
181         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
182                                pcc->l1_dcache_size)));
183     } else {
184         warn_report("Unknown L1 dcache size for cpu");
185     }
186     if (pcc->l1_icache_size) {
187         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
188                                pcc->l1_icache_size)));
189     } else {
190         warn_report("Unknown L1 icache size for cpu");
191     }
192 
193     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
194     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
195     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size",
196                            cpu->hash64_opts->slb_size)));
197     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
198     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
199 
200     if (env->spr_cb[SPR_PURR].oea_read) {
201         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
202     }
203 
204     if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG)) {
205         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
206                            segs, sizeof(segs))));
207     }
208 
209     /*
210      * Advertise VMX/VSX (vector extensions) if available
211      *   0 / no property == no vector extensions
212      *   1               == VMX / Altivec available
213      *   2               == VSX available
214      */
215     if (env->insns_flags & PPC_ALTIVEC) {
216         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
217 
218         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
219     }
220 
221     /*
222      * Advertise DFP (Decimal Floating Point) if available
223      *   0 / no property == no DFP
224      *   1               == DFP available
225      */
226     if (env->insns_flags2 & PPC2_DFP) {
227         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
228     }
229 
230     page_sizes_prop_size = ppc_create_page_sizes_prop(cpu, page_sizes_prop,
231                                                       sizeof(page_sizes_prop));
232     if (page_sizes_prop_size) {
233         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
234                            page_sizes_prop, page_sizes_prop_size)));
235     }
236 
237     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features",
238                        pa_features, sizeof(pa_features))));
239 
240     /* Build interrupt servers properties */
241     for (i = 0; i < smt_threads; i++) {
242         servers_prop[i] = cpu_to_be32(pc->pir + i);
243     }
244     _FDT((fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
245                        servers_prop, sizeof(servers_prop))));
246 }
247 
248 static void pnv_dt_icp(PnvChip *chip, void *fdt, uint32_t pir,
249                        uint32_t nr_threads)
250 {
251     uint64_t addr = PNV_ICP_BASE(chip) | (pir << 12);
252     char *name;
253     const char compat[] = "IBM,power8-icp\0IBM,ppc-xicp";
254     uint32_t irange[2], i, rsize;
255     uint64_t *reg;
256     int offset;
257 
258     irange[0] = cpu_to_be32(pir);
259     irange[1] = cpu_to_be32(nr_threads);
260 
261     rsize = sizeof(uint64_t) * 2 * nr_threads;
262     reg = g_malloc(rsize);
263     for (i = 0; i < nr_threads; i++) {
264         reg[i * 2] = cpu_to_be64(addr | ((pir + i) * 0x1000));
265         reg[i * 2 + 1] = cpu_to_be64(0x1000);
266     }
267 
268     name = g_strdup_printf("interrupt-controller@%"PRIX64, addr);
269     offset = fdt_add_subnode(fdt, 0, name);
270     _FDT(offset);
271     g_free(name);
272 
273     _FDT((fdt_setprop(fdt, offset, "compatible", compat, sizeof(compat))));
274     _FDT((fdt_setprop(fdt, offset, "reg", reg, rsize)));
275     _FDT((fdt_setprop_string(fdt, offset, "device_type",
276                               "PowerPC-External-Interrupt-Presentation")));
277     _FDT((fdt_setprop(fdt, offset, "interrupt-controller", NULL, 0)));
278     _FDT((fdt_setprop(fdt, offset, "ibm,interrupt-server-ranges",
279                        irange, sizeof(irange))));
280     _FDT((fdt_setprop_cell(fdt, offset, "#interrupt-cells", 1)));
281     _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0)));
282     g_free(reg);
283 }
284 
285 static void pnv_chip_power8_dt_populate(PnvChip *chip, void *fdt)
286 {
287     static const char compat[] = "ibm,power8-xscom\0ibm,xscom";
288     int i;
289 
290     pnv_dt_xscom(chip, fdt, 0,
291                  cpu_to_be64(PNV_XSCOM_BASE(chip)),
292                  cpu_to_be64(PNV_XSCOM_SIZE),
293                  compat, sizeof(compat));
294 
295     for (i = 0; i < chip->nr_cores; i++) {
296         PnvCore *pnv_core = chip->cores[i];
297 
298         pnv_dt_core(chip, pnv_core, fdt);
299 
300         /* Interrupt Control Presenters (ICP). One per core. */
301         pnv_dt_icp(chip, fdt, pnv_core->pir, CPU_CORE(pnv_core)->nr_threads);
302     }
303 
304     if (chip->ram_size) {
305         pnv_dt_memory(fdt, chip->chip_id, chip->ram_start, chip->ram_size);
306     }
307 }
308 
309 static void pnv_chip_power9_dt_populate(PnvChip *chip, void *fdt)
310 {
311     static const char compat[] = "ibm,power9-xscom\0ibm,xscom";
312     int i;
313 
314     pnv_dt_xscom(chip, fdt, 0,
315                  cpu_to_be64(PNV9_XSCOM_BASE(chip)),
316                  cpu_to_be64(PNV9_XSCOM_SIZE),
317                  compat, sizeof(compat));
318 
319     for (i = 0; i < chip->nr_cores; i++) {
320         PnvCore *pnv_core = chip->cores[i];
321 
322         pnv_dt_core(chip, pnv_core, fdt);
323     }
324 
325     if (chip->ram_size) {
326         pnv_dt_memory(fdt, chip->chip_id, chip->ram_start, chip->ram_size);
327     }
328 
329     pnv_dt_lpc(chip, fdt, 0, PNV9_LPCM_BASE(chip), PNV9_LPCM_SIZE);
330 }
331 
332 static void pnv_chip_power10_dt_populate(PnvChip *chip, void *fdt)
333 {
334     static const char compat[] = "ibm,power10-xscom\0ibm,xscom";
335     int i;
336 
337     pnv_dt_xscom(chip, fdt, 0,
338                  cpu_to_be64(PNV10_XSCOM_BASE(chip)),
339                  cpu_to_be64(PNV10_XSCOM_SIZE),
340                  compat, sizeof(compat));
341 
342     for (i = 0; i < chip->nr_cores; i++) {
343         PnvCore *pnv_core = chip->cores[i];
344 
345         pnv_dt_core(chip, pnv_core, fdt);
346     }
347 
348     if (chip->ram_size) {
349         pnv_dt_memory(fdt, chip->chip_id, chip->ram_start, chip->ram_size);
350     }
351 
352     pnv_dt_lpc(chip, fdt, 0, PNV10_LPCM_BASE(chip), PNV10_LPCM_SIZE);
353 }
354 
355 static void pnv_dt_rtc(ISADevice *d, void *fdt, int lpc_off)
356 {
357     uint32_t io_base = d->ioport_id;
358     uint32_t io_regs[] = {
359         cpu_to_be32(1),
360         cpu_to_be32(io_base),
361         cpu_to_be32(2)
362     };
363     char *name;
364     int node;
365 
366     name = g_strdup_printf("%s@i%x", qdev_fw_name(DEVICE(d)), io_base);
367     node = fdt_add_subnode(fdt, lpc_off, name);
368     _FDT(node);
369     g_free(name);
370 
371     _FDT((fdt_setprop(fdt, node, "reg", io_regs, sizeof(io_regs))));
372     _FDT((fdt_setprop_string(fdt, node, "compatible", "pnpPNP,b00")));
373 }
374 
375 static void pnv_dt_serial(ISADevice *d, void *fdt, int lpc_off)
376 {
377     const char compatible[] = "ns16550\0pnpPNP,501";
378     uint32_t io_base = d->ioport_id;
379     uint32_t io_regs[] = {
380         cpu_to_be32(1),
381         cpu_to_be32(io_base),
382         cpu_to_be32(8)
383     };
384     char *name;
385     int node;
386 
387     name = g_strdup_printf("%s@i%x", qdev_fw_name(DEVICE(d)), io_base);
388     node = fdt_add_subnode(fdt, lpc_off, name);
389     _FDT(node);
390     g_free(name);
391 
392     _FDT((fdt_setprop(fdt, node, "reg", io_regs, sizeof(io_regs))));
393     _FDT((fdt_setprop(fdt, node, "compatible", compatible,
394                       sizeof(compatible))));
395 
396     _FDT((fdt_setprop_cell(fdt, node, "clock-frequency", 1843200)));
397     _FDT((fdt_setprop_cell(fdt, node, "current-speed", 115200)));
398     _FDT((fdt_setprop_cell(fdt, node, "interrupts", d->isairq[0])));
399     _FDT((fdt_setprop_cell(fdt, node, "interrupt-parent",
400                            fdt_get_phandle(fdt, lpc_off))));
401 
402     /* This is needed by Linux */
403     _FDT((fdt_setprop_string(fdt, node, "device_type", "serial")));
404 }
405 
406 static void pnv_dt_ipmi_bt(ISADevice *d, void *fdt, int lpc_off)
407 {
408     const char compatible[] = "bt\0ipmi-bt";
409     uint32_t io_base;
410     uint32_t io_regs[] = {
411         cpu_to_be32(1),
412         0, /* 'io_base' retrieved from the 'ioport' property of 'isa-ipmi-bt' */
413         cpu_to_be32(3)
414     };
415     uint32_t irq;
416     char *name;
417     int node;
418 
419     io_base = object_property_get_int(OBJECT(d), "ioport", &error_fatal);
420     io_regs[1] = cpu_to_be32(io_base);
421 
422     irq = object_property_get_int(OBJECT(d), "irq", &error_fatal);
423 
424     name = g_strdup_printf("%s@i%x", qdev_fw_name(DEVICE(d)), io_base);
425     node = fdt_add_subnode(fdt, lpc_off, name);
426     _FDT(node);
427     g_free(name);
428 
429     _FDT((fdt_setprop(fdt, node, "reg", io_regs, sizeof(io_regs))));
430     _FDT((fdt_setprop(fdt, node, "compatible", compatible,
431                       sizeof(compatible))));
432 
433     /* Mark it as reserved to avoid Linux trying to claim it */
434     _FDT((fdt_setprop_string(fdt, node, "status", "reserved")));
435     _FDT((fdt_setprop_cell(fdt, node, "interrupts", irq)));
436     _FDT((fdt_setprop_cell(fdt, node, "interrupt-parent",
437                            fdt_get_phandle(fdt, lpc_off))));
438 }
439 
440 typedef struct ForeachPopulateArgs {
441     void *fdt;
442     int offset;
443 } ForeachPopulateArgs;
444 
445 static int pnv_dt_isa_device(DeviceState *dev, void *opaque)
446 {
447     ForeachPopulateArgs *args = opaque;
448     ISADevice *d = ISA_DEVICE(dev);
449 
450     if (object_dynamic_cast(OBJECT(dev), TYPE_MC146818_RTC)) {
451         pnv_dt_rtc(d, args->fdt, args->offset);
452     } else if (object_dynamic_cast(OBJECT(dev), TYPE_ISA_SERIAL)) {
453         pnv_dt_serial(d, args->fdt, args->offset);
454     } else if (object_dynamic_cast(OBJECT(dev), "isa-ipmi-bt")) {
455         pnv_dt_ipmi_bt(d, args->fdt, args->offset);
456     } else {
457         error_report("unknown isa device %s@i%x", qdev_fw_name(dev),
458                      d->ioport_id);
459     }
460 
461     return 0;
462 }
463 
464 /*
465  * The default LPC bus of a multichip system is on chip 0. It's
466  * recognized by the firmware (skiboot) using a "primary" property.
467  */
468 static void pnv_dt_isa(PnvMachineState *pnv, void *fdt)
469 {
470     int isa_offset = fdt_path_offset(fdt, pnv->chips[0]->dt_isa_nodename);
471     ForeachPopulateArgs args = {
472         .fdt = fdt,
473         .offset = isa_offset,
474     };
475     uint32_t phandle;
476 
477     _FDT((fdt_setprop(fdt, isa_offset, "primary", NULL, 0)));
478 
479     phandle = qemu_fdt_alloc_phandle(fdt);
480     assert(phandle > 0);
481     _FDT((fdt_setprop_cell(fdt, isa_offset, "phandle", phandle)));
482 
483     /*
484      * ISA devices are not necessarily parented to the ISA bus so we
485      * can not use object_child_foreach()
486      */
487     qbus_walk_children(BUS(pnv->isa_bus), pnv_dt_isa_device, NULL, NULL, NULL,
488                        &args);
489 }
490 
491 static void pnv_dt_power_mgt(PnvMachineState *pnv, void *fdt)
492 {
493     int off;
494 
495     off = fdt_add_subnode(fdt, 0, "ibm,opal");
496     off = fdt_add_subnode(fdt, off, "power-mgt");
497 
498     _FDT(fdt_setprop_cell(fdt, off, "ibm,enabled-stop-levels", 0xc0000000));
499 }
500 
501 static void *pnv_dt_create(MachineState *machine)
502 {
503     PnvMachineClass *pmc = PNV_MACHINE_GET_CLASS(machine);
504     PnvMachineState *pnv = PNV_MACHINE(machine);
505     void *fdt;
506     char *buf;
507     int off;
508     int i;
509 
510     fdt = g_malloc0(FDT_MAX_SIZE);
511     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
512 
513     /* /qemu node */
514     _FDT((fdt_add_subnode(fdt, 0, "qemu")));
515 
516     /* Root node */
517     _FDT((fdt_setprop_cell(fdt, 0, "#address-cells", 0x2)));
518     _FDT((fdt_setprop_cell(fdt, 0, "#size-cells", 0x2)));
519     _FDT((fdt_setprop_string(fdt, 0, "model",
520                              "IBM PowerNV (emulated by qemu)")));
521     _FDT((fdt_setprop(fdt, 0, "compatible", pmc->compat, pmc->compat_size)));
522 
523     buf =  qemu_uuid_unparse_strdup(&qemu_uuid);
524     _FDT((fdt_setprop_string(fdt, 0, "vm,uuid", buf)));
525     if (qemu_uuid_set) {
526         _FDT((fdt_property_string(fdt, "system-id", buf)));
527     }
528     g_free(buf);
529 
530     off = fdt_add_subnode(fdt, 0, "chosen");
531     if (machine->kernel_cmdline) {
532         _FDT((fdt_setprop_string(fdt, off, "bootargs",
533                                  machine->kernel_cmdline)));
534     }
535 
536     if (pnv->initrd_size) {
537         uint32_t start_prop = cpu_to_be32(pnv->initrd_base);
538         uint32_t end_prop = cpu_to_be32(pnv->initrd_base + pnv->initrd_size);
539 
540         _FDT((fdt_setprop(fdt, off, "linux,initrd-start",
541                                &start_prop, sizeof(start_prop))));
542         _FDT((fdt_setprop(fdt, off, "linux,initrd-end",
543                                &end_prop, sizeof(end_prop))));
544     }
545 
546     /* Populate device tree for each chip */
547     for (i = 0; i < pnv->num_chips; i++) {
548         PNV_CHIP_GET_CLASS(pnv->chips[i])->dt_populate(pnv->chips[i], fdt);
549     }
550 
551     /* Populate ISA devices on chip 0 */
552     pnv_dt_isa(pnv, fdt);
553 
554     if (pnv->bmc) {
555         pnv_dt_bmc_sensors(pnv->bmc, fdt);
556     }
557 
558     /* Create an extra node for power management on machines that support it */
559     if (pmc->dt_power_mgt) {
560         pmc->dt_power_mgt(pnv, fdt);
561     }
562 
563     return fdt;
564 }
565 
566 static void pnv_powerdown_notify(Notifier *n, void *opaque)
567 {
568     PnvMachineState *pnv = container_of(n, PnvMachineState, powerdown_notifier);
569 
570     if (pnv->bmc) {
571         pnv_bmc_powerdown(pnv->bmc);
572     }
573 }
574 
575 static void pnv_reset(MachineState *machine)
576 {
577     PnvMachineState *pnv = PNV_MACHINE(machine);
578     IPMIBmc *bmc;
579     void *fdt;
580 
581     qemu_devices_reset();
582 
583     /*
584      * The machine should provide by default an internal BMC simulator.
585      * If not, try to use the BMC device that was provided on the command
586      * line.
587      */
588     bmc = pnv_bmc_find(&error_fatal);
589     if (!pnv->bmc) {
590         if (!bmc) {
591             if (!qtest_enabled()) {
592                 warn_report("machine has no BMC device. Use '-device "
593                             "ipmi-bmc-sim,id=bmc0 -device isa-ipmi-bt,bmc=bmc0,irq=10' "
594                             "to define one");
595             }
596         } else {
597             pnv_bmc_set_pnor(bmc, pnv->pnor);
598             pnv->bmc = bmc;
599         }
600     }
601 
602     fdt = pnv_dt_create(machine);
603 
604     /* Pack resulting tree */
605     _FDT((fdt_pack(fdt)));
606 
607     qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
608     cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
609 
610     g_free(fdt);
611 }
612 
613 static ISABus *pnv_chip_power8_isa_create(PnvChip *chip, Error **errp)
614 {
615     Pnv8Chip *chip8 = PNV8_CHIP(chip);
616     return pnv_lpc_isa_create(&chip8->lpc, true, errp);
617 }
618 
619 static ISABus *pnv_chip_power8nvl_isa_create(PnvChip *chip, Error **errp)
620 {
621     Pnv8Chip *chip8 = PNV8_CHIP(chip);
622     return pnv_lpc_isa_create(&chip8->lpc, false, errp);
623 }
624 
625 static ISABus *pnv_chip_power9_isa_create(PnvChip *chip, Error **errp)
626 {
627     Pnv9Chip *chip9 = PNV9_CHIP(chip);
628     return pnv_lpc_isa_create(&chip9->lpc, false, errp);
629 }
630 
631 static ISABus *pnv_chip_power10_isa_create(PnvChip *chip, Error **errp)
632 {
633     Pnv10Chip *chip10 = PNV10_CHIP(chip);
634     return pnv_lpc_isa_create(&chip10->lpc, false, errp);
635 }
636 
637 static ISABus *pnv_isa_create(PnvChip *chip, Error **errp)
638 {
639     return PNV_CHIP_GET_CLASS(chip)->isa_create(chip, errp);
640 }
641 
642 static void pnv_chip_power8_pic_print_info(PnvChip *chip, Monitor *mon)
643 {
644     Pnv8Chip *chip8 = PNV8_CHIP(chip);
645     int i;
646 
647     ics_pic_print_info(&chip8->psi.ics, mon);
648     for (i = 0; i < chip->num_phbs; i++) {
649         pnv_phb3_msi_pic_print_info(&chip8->phbs[i].msis, mon);
650         ics_pic_print_info(&chip8->phbs[i].lsis, mon);
651     }
652 }
653 
654 static void pnv_chip_power9_pic_print_info(PnvChip *chip, Monitor *mon)
655 {
656     Pnv9Chip *chip9 = PNV9_CHIP(chip);
657     int i, j;
658 
659     pnv_xive_pic_print_info(&chip9->xive, mon);
660     pnv_psi_pic_print_info(&chip9->psi, mon);
661 
662     for (i = 0; i < PNV9_CHIP_MAX_PEC; i++) {
663         PnvPhb4PecState *pec = &chip9->pecs[i];
664         for (j = 0; j < pec->num_stacks; j++) {
665             pnv_phb4_pic_print_info(&pec->stacks[j].phb, mon);
666         }
667     }
668 }
669 
670 static uint64_t pnv_chip_power8_xscom_core_base(PnvChip *chip,
671                                                 uint32_t core_id)
672 {
673     return PNV_XSCOM_EX_BASE(core_id);
674 }
675 
676 static uint64_t pnv_chip_power9_xscom_core_base(PnvChip *chip,
677                                                 uint32_t core_id)
678 {
679     return PNV9_XSCOM_EC_BASE(core_id);
680 }
681 
682 static uint64_t pnv_chip_power10_xscom_core_base(PnvChip *chip,
683                                                  uint32_t core_id)
684 {
685     return PNV10_XSCOM_EC_BASE(core_id);
686 }
687 
688 static bool pnv_match_cpu(const char *default_type, const char *cpu_type)
689 {
690     PowerPCCPUClass *ppc_default =
691         POWERPC_CPU_CLASS(object_class_by_name(default_type));
692     PowerPCCPUClass *ppc =
693         POWERPC_CPU_CLASS(object_class_by_name(cpu_type));
694 
695     return ppc_default->pvr_match(ppc_default, ppc->pvr);
696 }
697 
698 static void pnv_ipmi_bt_init(ISABus *bus, IPMIBmc *bmc, uint32_t irq)
699 {
700     ISADevice *dev = isa_new("isa-ipmi-bt");
701 
702     object_property_set_link(OBJECT(dev), OBJECT(bmc), "bmc", &error_fatal);
703     object_property_set_int(OBJECT(dev), irq, "irq", &error_fatal);
704     isa_realize_and_unref(dev, bus, &error_fatal);
705 }
706 
707 static void pnv_chip_power10_pic_print_info(PnvChip *chip, Monitor *mon)
708 {
709     Pnv10Chip *chip10 = PNV10_CHIP(chip);
710 
711     pnv_psi_pic_print_info(&chip10->psi, mon);
712 }
713 
714 static void pnv_init(MachineState *machine)
715 {
716     PnvMachineState *pnv = PNV_MACHINE(machine);
717     MachineClass *mc = MACHINE_GET_CLASS(machine);
718     char *fw_filename;
719     long fw_size;
720     int i;
721     char *chip_typename;
722     DriveInfo *pnor = drive_get(IF_MTD, 0, 0);
723     DeviceState *dev;
724 
725     /* allocate RAM */
726     if (machine->ram_size < (1 * GiB)) {
727         warn_report("skiboot may not work with < 1GB of RAM");
728     }
729     memory_region_add_subregion(get_system_memory(), 0, machine->ram);
730 
731     /*
732      * Create our simple PNOR device
733      */
734     dev = qdev_new(TYPE_PNV_PNOR);
735     if (pnor) {
736         qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(pnor));
737     }
738     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
739     pnv->pnor = PNV_PNOR(dev);
740 
741     /* load skiboot firmware  */
742     if (bios_name == NULL) {
743         bios_name = FW_FILE_NAME;
744     }
745 
746     fw_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
747     if (!fw_filename) {
748         error_report("Could not find OPAL firmware '%s'", bios_name);
749         exit(1);
750     }
751 
752     fw_size = load_image_targphys(fw_filename, pnv->fw_load_addr, FW_MAX_SIZE);
753     if (fw_size < 0) {
754         error_report("Could not load OPAL firmware '%s'", fw_filename);
755         exit(1);
756     }
757     g_free(fw_filename);
758 
759     /* load kernel */
760     if (machine->kernel_filename) {
761         long kernel_size;
762 
763         kernel_size = load_image_targphys(machine->kernel_filename,
764                                           KERNEL_LOAD_ADDR, KERNEL_MAX_SIZE);
765         if (kernel_size < 0) {
766             error_report("Could not load kernel '%s'",
767                          machine->kernel_filename);
768             exit(1);
769         }
770     }
771 
772     /* load initrd */
773     if (machine->initrd_filename) {
774         pnv->initrd_base = INITRD_LOAD_ADDR;
775         pnv->initrd_size = load_image_targphys(machine->initrd_filename,
776                                   pnv->initrd_base, INITRD_MAX_SIZE);
777         if (pnv->initrd_size < 0) {
778             error_report("Could not load initial ram disk '%s'",
779                          machine->initrd_filename);
780             exit(1);
781         }
782     }
783 
784     /* MSIs are supported on this platform */
785     msi_nonbroken = true;
786 
787     /*
788      * Check compatibility of the specified CPU with the machine
789      * default.
790      */
791     if (!pnv_match_cpu(mc->default_cpu_type, machine->cpu_type)) {
792         error_report("invalid CPU model '%s' for %s machine",
793                      machine->cpu_type, mc->name);
794         exit(1);
795     }
796 
797     /* Create the processor chips */
798     i = strlen(machine->cpu_type) - strlen(POWERPC_CPU_TYPE_SUFFIX);
799     chip_typename = g_strdup_printf(PNV_CHIP_TYPE_NAME("%.*s"),
800                                     i, machine->cpu_type);
801     if (!object_class_by_name(chip_typename)) {
802         error_report("invalid chip model '%.*s' for %s machine",
803                      i, machine->cpu_type, mc->name);
804         exit(1);
805     }
806 
807     pnv->num_chips =
808         machine->smp.max_cpus / (machine->smp.cores * machine->smp.threads);
809     /*
810      * TODO: should we decide on how many chips we can create based
811      * on #cores and Venice vs. Murano vs. Naples chip type etc...,
812      */
813     if (!is_power_of_2(pnv->num_chips) || pnv->num_chips > 4) {
814         error_report("invalid number of chips: '%d'", pnv->num_chips);
815         error_printf("Try '-smp sockets=N'. Valid values are : 1, 2 or 4.\n");
816         exit(1);
817     }
818 
819     pnv->chips = g_new0(PnvChip *, pnv->num_chips);
820     for (i = 0; i < pnv->num_chips; i++) {
821         char chip_name[32];
822         Object *chip = OBJECT(qdev_new(chip_typename));
823 
824         pnv->chips[i] = PNV_CHIP(chip);
825 
826         /*
827          * TODO: put all the memory in one node on chip 0 until we find a
828          * way to specify different ranges for each chip
829          */
830         if (i == 0) {
831             object_property_set_int(chip, machine->ram_size, "ram-size",
832                                     &error_fatal);
833         }
834 
835         snprintf(chip_name, sizeof(chip_name), "chip[%d]", PNV_CHIP_HWID(i));
836         object_property_add_child(OBJECT(pnv), chip_name, chip);
837         object_property_set_int(chip, PNV_CHIP_HWID(i), "chip-id",
838                                 &error_fatal);
839         object_property_set_int(chip, machine->smp.cores,
840                                 "nr-cores", &error_fatal);
841         object_property_set_int(chip, machine->smp.threads,
842                                 "nr-threads", &error_fatal);
843         /*
844          * The POWER8 machine use the XICS interrupt interface.
845          * Propagate the XICS fabric to the chip and its controllers.
846          */
847         if (object_dynamic_cast(OBJECT(pnv), TYPE_XICS_FABRIC)) {
848             object_property_set_link(chip, OBJECT(pnv), "xics", &error_abort);
849         }
850         if (object_dynamic_cast(OBJECT(pnv), TYPE_XIVE_FABRIC)) {
851             object_property_set_link(chip, OBJECT(pnv), "xive-fabric",
852                                      &error_abort);
853         }
854         sysbus_realize_and_unref(SYS_BUS_DEVICE(chip), &error_fatal);
855     }
856     g_free(chip_typename);
857 
858     /* Instantiate ISA bus on chip 0 */
859     pnv->isa_bus = pnv_isa_create(pnv->chips[0], &error_fatal);
860 
861     /* Create serial port */
862     serial_hds_isa_init(pnv->isa_bus, 0, MAX_ISA_SERIAL_PORTS);
863 
864     /* Create an RTC ISA device too */
865     mc146818_rtc_init(pnv->isa_bus, 2000, NULL);
866 
867     /*
868      * Create the machine BMC simulator and the IPMI BT device for
869      * communication with the BMC
870      */
871     if (defaults_enabled()) {
872         pnv->bmc = pnv_bmc_create(pnv->pnor);
873         pnv_ipmi_bt_init(pnv->isa_bus, pnv->bmc, 10);
874     }
875 
876     /*
877      * OpenPOWER systems use a IPMI SEL Event message to notify the
878      * host to powerdown
879      */
880     pnv->powerdown_notifier.notify = pnv_powerdown_notify;
881     qemu_register_powerdown_notifier(&pnv->powerdown_notifier);
882 }
883 
884 /*
885  *    0:21  Reserved - Read as zeros
886  *   22:24  Chip ID
887  *   25:28  Core number
888  *   29:31  Thread ID
889  */
890 static uint32_t pnv_chip_core_pir_p8(PnvChip *chip, uint32_t core_id)
891 {
892     return (chip->chip_id << 7) | (core_id << 3);
893 }
894 
895 static void pnv_chip_power8_intc_create(PnvChip *chip, PowerPCCPU *cpu,
896                                         Error **errp)
897 {
898     Pnv8Chip *chip8 = PNV8_CHIP(chip);
899     Error *local_err = NULL;
900     Object *obj;
901     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
902 
903     obj = icp_create(OBJECT(cpu), TYPE_PNV_ICP, chip8->xics, &local_err);
904     if (local_err) {
905         error_propagate(errp, local_err);
906         return;
907     }
908 
909     pnv_cpu->intc = obj;
910 }
911 
912 
913 static void pnv_chip_power8_intc_reset(PnvChip *chip, PowerPCCPU *cpu)
914 {
915     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
916 
917     icp_reset(ICP(pnv_cpu->intc));
918 }
919 
920 static void pnv_chip_power8_intc_destroy(PnvChip *chip, PowerPCCPU *cpu)
921 {
922     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
923 
924     icp_destroy(ICP(pnv_cpu->intc));
925     pnv_cpu->intc = NULL;
926 }
927 
928 static void pnv_chip_power8_intc_print_info(PnvChip *chip, PowerPCCPU *cpu,
929                                             Monitor *mon)
930 {
931     icp_pic_print_info(ICP(pnv_cpu_state(cpu)->intc), mon);
932 }
933 
934 /*
935  *    0:48  Reserved - Read as zeroes
936  *   49:52  Node ID
937  *   53:55  Chip ID
938  *   56     Reserved - Read as zero
939  *   57:61  Core number
940  *   62:63  Thread ID
941  *
942  * We only care about the lower bits. uint32_t is fine for the moment.
943  */
944 static uint32_t pnv_chip_core_pir_p9(PnvChip *chip, uint32_t core_id)
945 {
946     return (chip->chip_id << 8) | (core_id << 2);
947 }
948 
949 static uint32_t pnv_chip_core_pir_p10(PnvChip *chip, uint32_t core_id)
950 {
951     return (chip->chip_id << 8) | (core_id << 2);
952 }
953 
954 static void pnv_chip_power9_intc_create(PnvChip *chip, PowerPCCPU *cpu,
955                                         Error **errp)
956 {
957     Pnv9Chip *chip9 = PNV9_CHIP(chip);
958     Error *local_err = NULL;
959     Object *obj;
960     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
961 
962     /*
963      * The core creates its interrupt presenter but the XIVE interrupt
964      * controller object is initialized afterwards. Hopefully, it's
965      * only used at runtime.
966      */
967     obj = xive_tctx_create(OBJECT(cpu), XIVE_PRESENTER(&chip9->xive),
968                            &local_err);
969     if (local_err) {
970         error_propagate(errp, local_err);
971         return;
972     }
973 
974     pnv_cpu->intc = obj;
975 }
976 
977 static void pnv_chip_power9_intc_reset(PnvChip *chip, PowerPCCPU *cpu)
978 {
979     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
980 
981     xive_tctx_reset(XIVE_TCTX(pnv_cpu->intc));
982 }
983 
984 static void pnv_chip_power9_intc_destroy(PnvChip *chip, PowerPCCPU *cpu)
985 {
986     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
987 
988     xive_tctx_destroy(XIVE_TCTX(pnv_cpu->intc));
989     pnv_cpu->intc = NULL;
990 }
991 
992 static void pnv_chip_power9_intc_print_info(PnvChip *chip, PowerPCCPU *cpu,
993                                             Monitor *mon)
994 {
995     xive_tctx_pic_print_info(XIVE_TCTX(pnv_cpu_state(cpu)->intc), mon);
996 }
997 
998 static void pnv_chip_power10_intc_create(PnvChip *chip, PowerPCCPU *cpu,
999                                         Error **errp)
1000 {
1001     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
1002 
1003     /* Will be defined when the interrupt controller is */
1004     pnv_cpu->intc = NULL;
1005 }
1006 
1007 static void pnv_chip_power10_intc_reset(PnvChip *chip, PowerPCCPU *cpu)
1008 {
1009     ;
1010 }
1011 
1012 static void pnv_chip_power10_intc_destroy(PnvChip *chip, PowerPCCPU *cpu)
1013 {
1014     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
1015 
1016     pnv_cpu->intc = NULL;
1017 }
1018 
1019 static void pnv_chip_power10_intc_print_info(PnvChip *chip, PowerPCCPU *cpu,
1020                                              Monitor *mon)
1021 {
1022 }
1023 
1024 /*
1025  * Allowed core identifiers on a POWER8 Processor Chip :
1026  *
1027  * <EX0 reserved>
1028  *  EX1  - Venice only
1029  *  EX2  - Venice only
1030  *  EX3  - Venice only
1031  *  EX4
1032  *  EX5
1033  *  EX6
1034  * <EX7,8 reserved> <reserved>
1035  *  EX9  - Venice only
1036  *  EX10 - Venice only
1037  *  EX11 - Venice only
1038  *  EX12
1039  *  EX13
1040  *  EX14
1041  * <EX15 reserved>
1042  */
1043 #define POWER8E_CORE_MASK  (0x7070ull)
1044 #define POWER8_CORE_MASK   (0x7e7eull)
1045 
1046 /*
1047  * POWER9 has 24 cores, ids starting at 0x0
1048  */
1049 #define POWER9_CORE_MASK   (0xffffffffffffffull)
1050 
1051 
1052 #define POWER10_CORE_MASK  (0xffffffffffffffull)
1053 
1054 static void pnv_chip_power8_instance_init(Object *obj)
1055 {
1056     PnvChip *chip = PNV_CHIP(obj);
1057     Pnv8Chip *chip8 = PNV8_CHIP(obj);
1058     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(obj);
1059     int i;
1060 
1061     object_property_add_link(obj, "xics", TYPE_XICS_FABRIC,
1062                              (Object **)&chip8->xics,
1063                              object_property_allow_set_link,
1064                              OBJ_PROP_LINK_STRONG);
1065 
1066     object_initialize_child(obj, "psi", &chip8->psi, TYPE_PNV8_PSI);
1067 
1068     object_initialize_child(obj, "lpc", &chip8->lpc, TYPE_PNV8_LPC);
1069 
1070     object_initialize_child(obj, "occ", &chip8->occ, TYPE_PNV8_OCC);
1071 
1072     object_initialize_child(obj, "homer", &chip8->homer, TYPE_PNV8_HOMER);
1073 
1074     for (i = 0; i < pcc->num_phbs; i++) {
1075         object_initialize_child(obj, "phb[*]", &chip8->phbs[i], TYPE_PNV_PHB3);
1076     }
1077 
1078     /*
1079      * Number of PHBs is the chip default
1080      */
1081     chip->num_phbs = pcc->num_phbs;
1082 }
1083 
1084 static void pnv_chip_icp_realize(Pnv8Chip *chip8, Error **errp)
1085  {
1086     PnvChip *chip = PNV_CHIP(chip8);
1087     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(chip);
1088     int i, j;
1089     char *name;
1090 
1091     name = g_strdup_printf("icp-%x", chip->chip_id);
1092     memory_region_init(&chip8->icp_mmio, OBJECT(chip), name, PNV_ICP_SIZE);
1093     sysbus_init_mmio(SYS_BUS_DEVICE(chip), &chip8->icp_mmio);
1094     g_free(name);
1095 
1096     sysbus_mmio_map(SYS_BUS_DEVICE(chip), 1, PNV_ICP_BASE(chip));
1097 
1098     /* Map the ICP registers for each thread */
1099     for (i = 0; i < chip->nr_cores; i++) {
1100         PnvCore *pnv_core = chip->cores[i];
1101         int core_hwid = CPU_CORE(pnv_core)->core_id;
1102 
1103         for (j = 0; j < CPU_CORE(pnv_core)->nr_threads; j++) {
1104             uint32_t pir = pcc->core_pir(chip, core_hwid) + j;
1105             PnvICPState *icp = PNV_ICP(xics_icp_get(chip8->xics, pir));
1106 
1107             memory_region_add_subregion(&chip8->icp_mmio, pir << 12,
1108                                         &icp->mmio);
1109         }
1110     }
1111 }
1112 
1113 static void pnv_chip_power8_realize(DeviceState *dev, Error **errp)
1114 {
1115     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(dev);
1116     PnvChip *chip = PNV_CHIP(dev);
1117     Pnv8Chip *chip8 = PNV8_CHIP(dev);
1118     Pnv8Psi *psi8 = &chip8->psi;
1119     Error *local_err = NULL;
1120     int i;
1121 
1122     assert(chip8->xics);
1123 
1124     /* XSCOM bridge is first */
1125     pnv_xscom_realize(chip, PNV_XSCOM_SIZE, &local_err);
1126     if (local_err) {
1127         error_propagate(errp, local_err);
1128         return;
1129     }
1130     sysbus_mmio_map(SYS_BUS_DEVICE(chip), 0, PNV_XSCOM_BASE(chip));
1131 
1132     pcc->parent_realize(dev, &local_err);
1133     if (local_err) {
1134         error_propagate(errp, local_err);
1135         return;
1136     }
1137 
1138     /* Processor Service Interface (PSI) Host Bridge */
1139     object_property_set_int(OBJECT(&chip8->psi), PNV_PSIHB_BASE(chip),
1140                             "bar", &error_fatal);
1141     object_property_set_link(OBJECT(&chip8->psi), OBJECT(chip8->xics),
1142                              ICS_PROP_XICS, &error_abort);
1143     qdev_realize(DEVICE(&chip8->psi), NULL, &local_err);
1144     if (local_err) {
1145         error_propagate(errp, local_err);
1146         return;
1147     }
1148     pnv_xscom_add_subregion(chip, PNV_XSCOM_PSIHB_BASE,
1149                             &PNV_PSI(psi8)->xscom_regs);
1150 
1151     /* Create LPC controller */
1152     object_property_set_link(OBJECT(&chip8->lpc), OBJECT(&chip8->psi), "psi",
1153                              &error_abort);
1154     qdev_realize(DEVICE(&chip8->lpc), NULL, &error_fatal);
1155     pnv_xscom_add_subregion(chip, PNV_XSCOM_LPC_BASE, &chip8->lpc.xscom_regs);
1156 
1157     chip->dt_isa_nodename = g_strdup_printf("/xscom@%" PRIx64 "/isa@%x",
1158                                             (uint64_t) PNV_XSCOM_BASE(chip),
1159                                             PNV_XSCOM_LPC_BASE);
1160 
1161     /*
1162      * Interrupt Management Area. This is the memory region holding
1163      * all the Interrupt Control Presenter (ICP) registers
1164      */
1165     pnv_chip_icp_realize(chip8, &local_err);
1166     if (local_err) {
1167         error_propagate(errp, local_err);
1168         return;
1169     }
1170 
1171     /* Create the simplified OCC model */
1172     object_property_set_link(OBJECT(&chip8->occ), OBJECT(&chip8->psi), "psi",
1173                              &error_abort);
1174     qdev_realize(DEVICE(&chip8->occ), NULL, &local_err);
1175     if (local_err) {
1176         error_propagate(errp, local_err);
1177         return;
1178     }
1179     pnv_xscom_add_subregion(chip, PNV_XSCOM_OCC_BASE, &chip8->occ.xscom_regs);
1180 
1181     /* OCC SRAM model */
1182     memory_region_add_subregion(get_system_memory(), PNV_OCC_SENSOR_BASE(chip),
1183                                 &chip8->occ.sram_regs);
1184 
1185     /* HOMER */
1186     object_property_set_link(OBJECT(&chip8->homer), OBJECT(chip), "chip",
1187                              &error_abort);
1188     qdev_realize(DEVICE(&chip8->homer), NULL, &local_err);
1189     if (local_err) {
1190         error_propagate(errp, local_err);
1191         return;
1192     }
1193     /* Homer Xscom region */
1194     pnv_xscom_add_subregion(chip, PNV_XSCOM_PBA_BASE, &chip8->homer.pba_regs);
1195 
1196     /* Homer mmio region */
1197     memory_region_add_subregion(get_system_memory(), PNV_HOMER_BASE(chip),
1198                                 &chip8->homer.regs);
1199 
1200     /* PHB3 controllers */
1201     for (i = 0; i < chip->num_phbs; i++) {
1202         PnvPHB3 *phb = &chip8->phbs[i];
1203         PnvPBCQState *pbcq = &phb->pbcq;
1204 
1205         object_property_set_int(OBJECT(phb), i, "index", &error_fatal);
1206         object_property_set_int(OBJECT(phb), chip->chip_id, "chip-id",
1207                                 &error_fatal);
1208         sysbus_realize(SYS_BUS_DEVICE(phb), &local_err);
1209         if (local_err) {
1210             error_propagate(errp, local_err);
1211             return;
1212         }
1213 
1214         /* Populate the XSCOM address space. */
1215         pnv_xscom_add_subregion(chip,
1216                                 PNV_XSCOM_PBCQ_NEST_BASE + 0x400 * phb->phb_id,
1217                                 &pbcq->xscom_nest_regs);
1218         pnv_xscom_add_subregion(chip,
1219                                 PNV_XSCOM_PBCQ_PCI_BASE + 0x400 * phb->phb_id,
1220                                 &pbcq->xscom_pci_regs);
1221         pnv_xscom_add_subregion(chip,
1222                                 PNV_XSCOM_PBCQ_SPCI_BASE + 0x040 * phb->phb_id,
1223                                 &pbcq->xscom_spci_regs);
1224     }
1225 }
1226 
1227 static uint32_t pnv_chip_power8_xscom_pcba(PnvChip *chip, uint64_t addr)
1228 {
1229     addr &= (PNV_XSCOM_SIZE - 1);
1230     return ((addr >> 4) & ~0xfull) | ((addr >> 3) & 0xf);
1231 }
1232 
1233 static void pnv_chip_power8e_class_init(ObjectClass *klass, void *data)
1234 {
1235     DeviceClass *dc = DEVICE_CLASS(klass);
1236     PnvChipClass *k = PNV_CHIP_CLASS(klass);
1237 
1238     k->chip_cfam_id = 0x221ef04980000000ull;  /* P8 Murano DD2.1 */
1239     k->cores_mask = POWER8E_CORE_MASK;
1240     k->num_phbs = 3;
1241     k->core_pir = pnv_chip_core_pir_p8;
1242     k->intc_create = pnv_chip_power8_intc_create;
1243     k->intc_reset = pnv_chip_power8_intc_reset;
1244     k->intc_destroy = pnv_chip_power8_intc_destroy;
1245     k->intc_print_info = pnv_chip_power8_intc_print_info;
1246     k->isa_create = pnv_chip_power8_isa_create;
1247     k->dt_populate = pnv_chip_power8_dt_populate;
1248     k->pic_print_info = pnv_chip_power8_pic_print_info;
1249     k->xscom_core_base = pnv_chip_power8_xscom_core_base;
1250     k->xscom_pcba = pnv_chip_power8_xscom_pcba;
1251     dc->desc = "PowerNV Chip POWER8E";
1252 
1253     device_class_set_parent_realize(dc, pnv_chip_power8_realize,
1254                                     &k->parent_realize);
1255 }
1256 
1257 static void pnv_chip_power8_class_init(ObjectClass *klass, void *data)
1258 {
1259     DeviceClass *dc = DEVICE_CLASS(klass);
1260     PnvChipClass *k = PNV_CHIP_CLASS(klass);
1261 
1262     k->chip_cfam_id = 0x220ea04980000000ull; /* P8 Venice DD2.0 */
1263     k->cores_mask = POWER8_CORE_MASK;
1264     k->num_phbs = 3;
1265     k->core_pir = pnv_chip_core_pir_p8;
1266     k->intc_create = pnv_chip_power8_intc_create;
1267     k->intc_reset = pnv_chip_power8_intc_reset;
1268     k->intc_destroy = pnv_chip_power8_intc_destroy;
1269     k->intc_print_info = pnv_chip_power8_intc_print_info;
1270     k->isa_create = pnv_chip_power8_isa_create;
1271     k->dt_populate = pnv_chip_power8_dt_populate;
1272     k->pic_print_info = pnv_chip_power8_pic_print_info;
1273     k->xscom_core_base = pnv_chip_power8_xscom_core_base;
1274     k->xscom_pcba = pnv_chip_power8_xscom_pcba;
1275     dc->desc = "PowerNV Chip POWER8";
1276 
1277     device_class_set_parent_realize(dc, pnv_chip_power8_realize,
1278                                     &k->parent_realize);
1279 }
1280 
1281 static void pnv_chip_power8nvl_class_init(ObjectClass *klass, void *data)
1282 {
1283     DeviceClass *dc = DEVICE_CLASS(klass);
1284     PnvChipClass *k = PNV_CHIP_CLASS(klass);
1285 
1286     k->chip_cfam_id = 0x120d304980000000ull;  /* P8 Naples DD1.0 */
1287     k->cores_mask = POWER8_CORE_MASK;
1288     k->num_phbs = 3;
1289     k->core_pir = pnv_chip_core_pir_p8;
1290     k->intc_create = pnv_chip_power8_intc_create;
1291     k->intc_reset = pnv_chip_power8_intc_reset;
1292     k->intc_destroy = pnv_chip_power8_intc_destroy;
1293     k->intc_print_info = pnv_chip_power8_intc_print_info;
1294     k->isa_create = pnv_chip_power8nvl_isa_create;
1295     k->dt_populate = pnv_chip_power8_dt_populate;
1296     k->pic_print_info = pnv_chip_power8_pic_print_info;
1297     k->xscom_core_base = pnv_chip_power8_xscom_core_base;
1298     k->xscom_pcba = pnv_chip_power8_xscom_pcba;
1299     dc->desc = "PowerNV Chip POWER8NVL";
1300 
1301     device_class_set_parent_realize(dc, pnv_chip_power8_realize,
1302                                     &k->parent_realize);
1303 }
1304 
1305 static void pnv_chip_power9_instance_init(Object *obj)
1306 {
1307     PnvChip *chip = PNV_CHIP(obj);
1308     Pnv9Chip *chip9 = PNV9_CHIP(obj);
1309     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(obj);
1310     int i;
1311 
1312     object_initialize_child(obj, "xive", &chip9->xive, TYPE_PNV_XIVE);
1313     object_property_add_alias(obj, "xive-fabric", OBJECT(&chip9->xive),
1314                               "xive-fabric");
1315 
1316     object_initialize_child(obj, "psi", &chip9->psi, TYPE_PNV9_PSI);
1317 
1318     object_initialize_child(obj, "lpc", &chip9->lpc, TYPE_PNV9_LPC);
1319 
1320     object_initialize_child(obj, "occ", &chip9->occ, TYPE_PNV9_OCC);
1321 
1322     object_initialize_child(obj, "homer", &chip9->homer, TYPE_PNV9_HOMER);
1323 
1324     for (i = 0; i < PNV9_CHIP_MAX_PEC; i++) {
1325         object_initialize_child(obj, "pec[*]", &chip9->pecs[i],
1326                                 TYPE_PNV_PHB4_PEC);
1327     }
1328 
1329     /*
1330      * Number of PHBs is the chip default
1331      */
1332     chip->num_phbs = pcc->num_phbs;
1333 }
1334 
1335 static void pnv_chip_quad_realize(Pnv9Chip *chip9, Error **errp)
1336 {
1337     PnvChip *chip = PNV_CHIP(chip9);
1338     int i;
1339 
1340     chip9->nr_quads = DIV_ROUND_UP(chip->nr_cores, 4);
1341     chip9->quads = g_new0(PnvQuad, chip9->nr_quads);
1342 
1343     for (i = 0; i < chip9->nr_quads; i++) {
1344         char eq_name[32];
1345         PnvQuad *eq = &chip9->quads[i];
1346         PnvCore *pnv_core = chip->cores[i * 4];
1347         int core_id = CPU_CORE(pnv_core)->core_id;
1348 
1349         snprintf(eq_name, sizeof(eq_name), "eq[%d]", core_id);
1350         object_initialize_child_with_props(OBJECT(chip), eq_name, eq,
1351                                            sizeof(*eq), TYPE_PNV_QUAD,
1352                                            &error_fatal, NULL);
1353 
1354         object_property_set_int(OBJECT(eq), core_id, "id", &error_fatal);
1355         qdev_realize(DEVICE(eq), NULL, &error_fatal);
1356 
1357         pnv_xscom_add_subregion(chip, PNV9_XSCOM_EQ_BASE(eq->id),
1358                                 &eq->xscom_regs);
1359     }
1360 }
1361 
1362 static void pnv_chip_power9_phb_realize(PnvChip *chip, Error **errp)
1363 {
1364     Pnv9Chip *chip9 = PNV9_CHIP(chip);
1365     Error *local_err = NULL;
1366     int i, j;
1367     int phb_id = 0;
1368 
1369     for (i = 0; i < PNV9_CHIP_MAX_PEC; i++) {
1370         PnvPhb4PecState *pec = &chip9->pecs[i];
1371         PnvPhb4PecClass *pecc = PNV_PHB4_PEC_GET_CLASS(pec);
1372         uint32_t pec_nest_base;
1373         uint32_t pec_pci_base;
1374 
1375         object_property_set_int(OBJECT(pec), i, "index", &error_fatal);
1376         /*
1377          * PEC0 -> 1 stack
1378          * PEC1 -> 2 stacks
1379          * PEC2 -> 3 stacks
1380          */
1381         object_property_set_int(OBJECT(pec), i + 1, "num-stacks",
1382                                 &error_fatal);
1383         object_property_set_int(OBJECT(pec), chip->chip_id, "chip-id",
1384                                  &error_fatal);
1385         object_property_set_link(OBJECT(pec), OBJECT(get_system_memory()),
1386                                  "system-memory", &error_abort);
1387         qdev_realize(DEVICE(pec), NULL, &local_err);
1388         if (local_err) {
1389             error_propagate(errp, local_err);
1390             return;
1391         }
1392 
1393         pec_nest_base = pecc->xscom_nest_base(pec);
1394         pec_pci_base = pecc->xscom_pci_base(pec);
1395 
1396         pnv_xscom_add_subregion(chip, pec_nest_base, &pec->nest_regs_mr);
1397         pnv_xscom_add_subregion(chip, pec_pci_base, &pec->pci_regs_mr);
1398 
1399         for (j = 0; j < pec->num_stacks && phb_id < chip->num_phbs;
1400              j++, phb_id++) {
1401             PnvPhb4PecStack *stack = &pec->stacks[j];
1402             Object *obj = OBJECT(&stack->phb);
1403 
1404             object_property_set_int(obj, phb_id, "index", &error_fatal);
1405             object_property_set_int(obj, chip->chip_id, "chip-id",
1406                                     &error_fatal);
1407             object_property_set_int(obj, PNV_PHB4_VERSION, "version",
1408                                     &error_fatal);
1409             object_property_set_int(obj, PNV_PHB4_DEVICE_ID, "device-id",
1410                                     &error_fatal);
1411             object_property_set_link(obj, OBJECT(stack), "stack", &error_abort);
1412             sysbus_realize(SYS_BUS_DEVICE(obj), &local_err);
1413             if (local_err) {
1414                 error_propagate(errp, local_err);
1415                 return;
1416             }
1417 
1418             /* Populate the XSCOM address space. */
1419             pnv_xscom_add_subregion(chip,
1420                                    pec_nest_base + 0x40 * (stack->stack_no + 1),
1421                                    &stack->nest_regs_mr);
1422             pnv_xscom_add_subregion(chip,
1423                                     pec_pci_base + 0x40 * (stack->stack_no + 1),
1424                                     &stack->pci_regs_mr);
1425             pnv_xscom_add_subregion(chip,
1426                                     pec_pci_base + PNV9_XSCOM_PEC_PCI_STK0 +
1427                                     0x40 * stack->stack_no,
1428                                     &stack->phb_regs_mr);
1429         }
1430     }
1431 }
1432 
1433 static void pnv_chip_power9_realize(DeviceState *dev, Error **errp)
1434 {
1435     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(dev);
1436     Pnv9Chip *chip9 = PNV9_CHIP(dev);
1437     PnvChip *chip = PNV_CHIP(dev);
1438     Pnv9Psi *psi9 = &chip9->psi;
1439     Error *local_err = NULL;
1440 
1441     /* XSCOM bridge is first */
1442     pnv_xscom_realize(chip, PNV9_XSCOM_SIZE, &local_err);
1443     if (local_err) {
1444         error_propagate(errp, local_err);
1445         return;
1446     }
1447     sysbus_mmio_map(SYS_BUS_DEVICE(chip), 0, PNV9_XSCOM_BASE(chip));
1448 
1449     pcc->parent_realize(dev, &local_err);
1450     if (local_err) {
1451         error_propagate(errp, local_err);
1452         return;
1453     }
1454 
1455     pnv_chip_quad_realize(chip9, &local_err);
1456     if (local_err) {
1457         error_propagate(errp, local_err);
1458         return;
1459     }
1460 
1461     /* XIVE interrupt controller (POWER9) */
1462     object_property_set_int(OBJECT(&chip9->xive), PNV9_XIVE_IC_BASE(chip),
1463                             "ic-bar", &error_fatal);
1464     object_property_set_int(OBJECT(&chip9->xive), PNV9_XIVE_VC_BASE(chip),
1465                             "vc-bar", &error_fatal);
1466     object_property_set_int(OBJECT(&chip9->xive), PNV9_XIVE_PC_BASE(chip),
1467                             "pc-bar", &error_fatal);
1468     object_property_set_int(OBJECT(&chip9->xive), PNV9_XIVE_TM_BASE(chip),
1469                             "tm-bar", &error_fatal);
1470     object_property_set_link(OBJECT(&chip9->xive), OBJECT(chip), "chip",
1471                              &error_abort);
1472     sysbus_realize(SYS_BUS_DEVICE(&chip9->xive), &local_err);
1473     if (local_err) {
1474         error_propagate(errp, local_err);
1475         return;
1476     }
1477     pnv_xscom_add_subregion(chip, PNV9_XSCOM_XIVE_BASE,
1478                             &chip9->xive.xscom_regs);
1479 
1480     /* Processor Service Interface (PSI) Host Bridge */
1481     object_property_set_int(OBJECT(&chip9->psi), PNV9_PSIHB_BASE(chip),
1482                             "bar", &error_fatal);
1483     qdev_realize(DEVICE(&chip9->psi), NULL, &local_err);
1484     if (local_err) {
1485         error_propagate(errp, local_err);
1486         return;
1487     }
1488     pnv_xscom_add_subregion(chip, PNV9_XSCOM_PSIHB_BASE,
1489                             &PNV_PSI(psi9)->xscom_regs);
1490 
1491     /* LPC */
1492     object_property_set_link(OBJECT(&chip9->lpc), OBJECT(&chip9->psi), "psi",
1493                              &error_abort);
1494     qdev_realize(DEVICE(&chip9->lpc), NULL, &local_err);
1495     if (local_err) {
1496         error_propagate(errp, local_err);
1497         return;
1498     }
1499     memory_region_add_subregion(get_system_memory(), PNV9_LPCM_BASE(chip),
1500                                 &chip9->lpc.xscom_regs);
1501 
1502     chip->dt_isa_nodename = g_strdup_printf("/lpcm-opb@%" PRIx64 "/lpc@0",
1503                                             (uint64_t) PNV9_LPCM_BASE(chip));
1504 
1505     /* Create the simplified OCC model */
1506     object_property_set_link(OBJECT(&chip9->occ), OBJECT(&chip9->psi), "psi",
1507                              &error_abort);
1508     qdev_realize(DEVICE(&chip9->occ), NULL, &local_err);
1509     if (local_err) {
1510         error_propagate(errp, local_err);
1511         return;
1512     }
1513     pnv_xscom_add_subregion(chip, PNV9_XSCOM_OCC_BASE, &chip9->occ.xscom_regs);
1514 
1515     /* OCC SRAM model */
1516     memory_region_add_subregion(get_system_memory(), PNV9_OCC_SENSOR_BASE(chip),
1517                                 &chip9->occ.sram_regs);
1518 
1519     /* HOMER */
1520     object_property_set_link(OBJECT(&chip9->homer), OBJECT(chip), "chip",
1521                              &error_abort);
1522     qdev_realize(DEVICE(&chip9->homer), NULL, &local_err);
1523     if (local_err) {
1524         error_propagate(errp, local_err);
1525         return;
1526     }
1527     /* Homer Xscom region */
1528     pnv_xscom_add_subregion(chip, PNV9_XSCOM_PBA_BASE, &chip9->homer.pba_regs);
1529 
1530     /* Homer mmio region */
1531     memory_region_add_subregion(get_system_memory(), PNV9_HOMER_BASE(chip),
1532                                 &chip9->homer.regs);
1533 
1534     /* PHBs */
1535     pnv_chip_power9_phb_realize(chip, &local_err);
1536     if (local_err) {
1537         error_propagate(errp, local_err);
1538         return;
1539     }
1540 }
1541 
1542 static uint32_t pnv_chip_power9_xscom_pcba(PnvChip *chip, uint64_t addr)
1543 {
1544     addr &= (PNV9_XSCOM_SIZE - 1);
1545     return addr >> 3;
1546 }
1547 
1548 static void pnv_chip_power9_class_init(ObjectClass *klass, void *data)
1549 {
1550     DeviceClass *dc = DEVICE_CLASS(klass);
1551     PnvChipClass *k = PNV_CHIP_CLASS(klass);
1552 
1553     k->chip_cfam_id = 0x220d104900008000ull; /* P9 Nimbus DD2.0 */
1554     k->cores_mask = POWER9_CORE_MASK;
1555     k->core_pir = pnv_chip_core_pir_p9;
1556     k->intc_create = pnv_chip_power9_intc_create;
1557     k->intc_reset = pnv_chip_power9_intc_reset;
1558     k->intc_destroy = pnv_chip_power9_intc_destroy;
1559     k->intc_print_info = pnv_chip_power9_intc_print_info;
1560     k->isa_create = pnv_chip_power9_isa_create;
1561     k->dt_populate = pnv_chip_power9_dt_populate;
1562     k->pic_print_info = pnv_chip_power9_pic_print_info;
1563     k->xscom_core_base = pnv_chip_power9_xscom_core_base;
1564     k->xscom_pcba = pnv_chip_power9_xscom_pcba;
1565     dc->desc = "PowerNV Chip POWER9";
1566     k->num_phbs = 6;
1567 
1568     device_class_set_parent_realize(dc, pnv_chip_power9_realize,
1569                                     &k->parent_realize);
1570 }
1571 
1572 static void pnv_chip_power10_instance_init(Object *obj)
1573 {
1574     Pnv10Chip *chip10 = PNV10_CHIP(obj);
1575 
1576     object_initialize_child(obj, "psi", &chip10->psi, TYPE_PNV10_PSI);
1577     object_initialize_child(obj, "lpc", &chip10->lpc, TYPE_PNV10_LPC);
1578 }
1579 
1580 static void pnv_chip_power10_realize(DeviceState *dev, Error **errp)
1581 {
1582     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(dev);
1583     PnvChip *chip = PNV_CHIP(dev);
1584     Pnv10Chip *chip10 = PNV10_CHIP(dev);
1585     Error *local_err = NULL;
1586 
1587     /* XSCOM bridge is first */
1588     pnv_xscom_realize(chip, PNV10_XSCOM_SIZE, &local_err);
1589     if (local_err) {
1590         error_propagate(errp, local_err);
1591         return;
1592     }
1593     sysbus_mmio_map(SYS_BUS_DEVICE(chip), 0, PNV10_XSCOM_BASE(chip));
1594 
1595     pcc->parent_realize(dev, &local_err);
1596     if (local_err) {
1597         error_propagate(errp, local_err);
1598         return;
1599     }
1600 
1601     /* Processor Service Interface (PSI) Host Bridge */
1602     object_property_set_int(OBJECT(&chip10->psi), PNV10_PSIHB_BASE(chip),
1603                             "bar", &error_fatal);
1604     qdev_realize(DEVICE(&chip10->psi), NULL, &local_err);
1605     if (local_err) {
1606         error_propagate(errp, local_err);
1607         return;
1608     }
1609     pnv_xscom_add_subregion(chip, PNV10_XSCOM_PSIHB_BASE,
1610                             &PNV_PSI(&chip10->psi)->xscom_regs);
1611 
1612     /* LPC */
1613     object_property_set_link(OBJECT(&chip10->lpc), OBJECT(&chip10->psi), "psi",
1614                              &error_abort);
1615     qdev_realize(DEVICE(&chip10->lpc), NULL, &local_err);
1616     if (local_err) {
1617         error_propagate(errp, local_err);
1618         return;
1619     }
1620     memory_region_add_subregion(get_system_memory(), PNV10_LPCM_BASE(chip),
1621                                 &chip10->lpc.xscom_regs);
1622 
1623     chip->dt_isa_nodename = g_strdup_printf("/lpcm-opb@%" PRIx64 "/lpc@0",
1624                                             (uint64_t) PNV10_LPCM_BASE(chip));
1625 }
1626 
1627 static uint32_t pnv_chip_power10_xscom_pcba(PnvChip *chip, uint64_t addr)
1628 {
1629     addr &= (PNV10_XSCOM_SIZE - 1);
1630     return addr >> 3;
1631 }
1632 
1633 static void pnv_chip_power10_class_init(ObjectClass *klass, void *data)
1634 {
1635     DeviceClass *dc = DEVICE_CLASS(klass);
1636     PnvChipClass *k = PNV_CHIP_CLASS(klass);
1637 
1638     k->chip_cfam_id = 0x120da04900008000ull; /* P10 DD1.0 (with NX) */
1639     k->cores_mask = POWER10_CORE_MASK;
1640     k->core_pir = pnv_chip_core_pir_p10;
1641     k->intc_create = pnv_chip_power10_intc_create;
1642     k->intc_reset = pnv_chip_power10_intc_reset;
1643     k->intc_destroy = pnv_chip_power10_intc_destroy;
1644     k->intc_print_info = pnv_chip_power10_intc_print_info;
1645     k->isa_create = pnv_chip_power10_isa_create;
1646     k->dt_populate = pnv_chip_power10_dt_populate;
1647     k->pic_print_info = pnv_chip_power10_pic_print_info;
1648     k->xscom_core_base = pnv_chip_power10_xscom_core_base;
1649     k->xscom_pcba = pnv_chip_power10_xscom_pcba;
1650     dc->desc = "PowerNV Chip POWER10";
1651 
1652     device_class_set_parent_realize(dc, pnv_chip_power10_realize,
1653                                     &k->parent_realize);
1654 }
1655 
1656 static void pnv_chip_core_sanitize(PnvChip *chip, Error **errp)
1657 {
1658     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(chip);
1659     int cores_max;
1660 
1661     /*
1662      * No custom mask for this chip, let's use the default one from *
1663      * the chip class
1664      */
1665     if (!chip->cores_mask) {
1666         chip->cores_mask = pcc->cores_mask;
1667     }
1668 
1669     /* filter alien core ids ! some are reserved */
1670     if ((chip->cores_mask & pcc->cores_mask) != chip->cores_mask) {
1671         error_setg(errp, "warning: invalid core mask for chip Ox%"PRIx64" !",
1672                    chip->cores_mask);
1673         return;
1674     }
1675     chip->cores_mask &= pcc->cores_mask;
1676 
1677     /* now that we have a sane layout, let check the number of cores */
1678     cores_max = ctpop64(chip->cores_mask);
1679     if (chip->nr_cores > cores_max) {
1680         error_setg(errp, "warning: too many cores for chip ! Limit is %d",
1681                    cores_max);
1682         return;
1683     }
1684 }
1685 
1686 static void pnv_chip_core_realize(PnvChip *chip, Error **errp)
1687 {
1688     Error *error = NULL;
1689     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(chip);
1690     const char *typename = pnv_chip_core_typename(chip);
1691     int i, core_hwid;
1692     PnvMachineState *pnv = PNV_MACHINE(qdev_get_machine());
1693 
1694     if (!object_class_by_name(typename)) {
1695         error_setg(errp, "Unable to find PowerNV CPU Core '%s'", typename);
1696         return;
1697     }
1698 
1699     /* Cores */
1700     pnv_chip_core_sanitize(chip, &error);
1701     if (error) {
1702         error_propagate(errp, error);
1703         return;
1704     }
1705 
1706     chip->cores = g_new0(PnvCore *, chip->nr_cores);
1707 
1708     for (i = 0, core_hwid = 0; (core_hwid < sizeof(chip->cores_mask) * 8)
1709              && (i < chip->nr_cores); core_hwid++) {
1710         char core_name[32];
1711         PnvCore *pnv_core;
1712         uint64_t xscom_core_base;
1713 
1714         if (!(chip->cores_mask & (1ull << core_hwid))) {
1715             continue;
1716         }
1717 
1718         pnv_core = PNV_CORE(object_new(typename));
1719 
1720         snprintf(core_name, sizeof(core_name), "core[%d]", core_hwid);
1721         object_property_add_child(OBJECT(chip), core_name, OBJECT(pnv_core));
1722         chip->cores[i] = pnv_core;
1723         object_property_set_int(OBJECT(pnv_core), chip->nr_threads,
1724                                 "nr-threads", &error_fatal);
1725         object_property_set_int(OBJECT(pnv_core), core_hwid,
1726                                 CPU_CORE_PROP_CORE_ID, &error_fatal);
1727         object_property_set_int(OBJECT(pnv_core),
1728                                 pcc->core_pir(chip, core_hwid),
1729                                 "pir", &error_fatal);
1730         object_property_set_int(OBJECT(pnv_core), pnv->fw_load_addr,
1731                                 "hrmor", &error_fatal);
1732         object_property_set_link(OBJECT(pnv_core), OBJECT(chip), "chip",
1733                                  &error_abort);
1734         qdev_realize(DEVICE(pnv_core), NULL, &error_fatal);
1735 
1736         /* Each core has an XSCOM MMIO region */
1737         xscom_core_base = pcc->xscom_core_base(chip, core_hwid);
1738 
1739         pnv_xscom_add_subregion(chip, xscom_core_base,
1740                                 &pnv_core->xscom_regs);
1741         i++;
1742     }
1743 }
1744 
1745 static void pnv_chip_realize(DeviceState *dev, Error **errp)
1746 {
1747     PnvChip *chip = PNV_CHIP(dev);
1748     Error *error = NULL;
1749 
1750     /* Cores */
1751     pnv_chip_core_realize(chip, &error);
1752     if (error) {
1753         error_propagate(errp, error);
1754         return;
1755     }
1756 }
1757 
1758 static Property pnv_chip_properties[] = {
1759     DEFINE_PROP_UINT32("chip-id", PnvChip, chip_id, 0),
1760     DEFINE_PROP_UINT64("ram-start", PnvChip, ram_start, 0),
1761     DEFINE_PROP_UINT64("ram-size", PnvChip, ram_size, 0),
1762     DEFINE_PROP_UINT32("nr-cores", PnvChip, nr_cores, 1),
1763     DEFINE_PROP_UINT64("cores-mask", PnvChip, cores_mask, 0x0),
1764     DEFINE_PROP_UINT32("nr-threads", PnvChip, nr_threads, 1),
1765     DEFINE_PROP_UINT32("num-phbs", PnvChip, num_phbs, 0),
1766     DEFINE_PROP_END_OF_LIST(),
1767 };
1768 
1769 static void pnv_chip_class_init(ObjectClass *klass, void *data)
1770 {
1771     DeviceClass *dc = DEVICE_CLASS(klass);
1772 
1773     set_bit(DEVICE_CATEGORY_CPU, dc->categories);
1774     dc->realize = pnv_chip_realize;
1775     device_class_set_props(dc, pnv_chip_properties);
1776     dc->desc = "PowerNV Chip";
1777 }
1778 
1779 PowerPCCPU *pnv_chip_find_cpu(PnvChip *chip, uint32_t pir)
1780 {
1781     int i, j;
1782 
1783     for (i = 0; i < chip->nr_cores; i++) {
1784         PnvCore *pc = chip->cores[i];
1785         CPUCore *cc = CPU_CORE(pc);
1786 
1787         for (j = 0; j < cc->nr_threads; j++) {
1788             if (ppc_cpu_pir(pc->threads[j]) == pir) {
1789                 return pc->threads[j];
1790             }
1791         }
1792     }
1793     return NULL;
1794 }
1795 
1796 static ICSState *pnv_ics_get(XICSFabric *xi, int irq)
1797 {
1798     PnvMachineState *pnv = PNV_MACHINE(xi);
1799     int i, j;
1800 
1801     for (i = 0; i < pnv->num_chips; i++) {
1802         PnvChip *chip = pnv->chips[i];
1803         Pnv8Chip *chip8 = PNV8_CHIP(pnv->chips[i]);
1804 
1805         if (ics_valid_irq(&chip8->psi.ics, irq)) {
1806             return &chip8->psi.ics;
1807         }
1808         for (j = 0; j < chip->num_phbs; j++) {
1809             if (ics_valid_irq(&chip8->phbs[j].lsis, irq)) {
1810                 return &chip8->phbs[j].lsis;
1811             }
1812             if (ics_valid_irq(ICS(&chip8->phbs[j].msis), irq)) {
1813                 return ICS(&chip8->phbs[j].msis);
1814             }
1815         }
1816     }
1817     return NULL;
1818 }
1819 
1820 static void pnv_ics_resend(XICSFabric *xi)
1821 {
1822     PnvMachineState *pnv = PNV_MACHINE(xi);
1823     int i, j;
1824 
1825     for (i = 0; i < pnv->num_chips; i++) {
1826         PnvChip *chip = pnv->chips[i];
1827         Pnv8Chip *chip8 = PNV8_CHIP(pnv->chips[i]);
1828 
1829         ics_resend(&chip8->psi.ics);
1830         for (j = 0; j < chip->num_phbs; j++) {
1831             ics_resend(&chip8->phbs[j].lsis);
1832             ics_resend(ICS(&chip8->phbs[j].msis));
1833         }
1834     }
1835 }
1836 
1837 static ICPState *pnv_icp_get(XICSFabric *xi, int pir)
1838 {
1839     PowerPCCPU *cpu = ppc_get_vcpu_by_pir(pir);
1840 
1841     return cpu ? ICP(pnv_cpu_state(cpu)->intc) : NULL;
1842 }
1843 
1844 static void pnv_pic_print_info(InterruptStatsProvider *obj,
1845                                Monitor *mon)
1846 {
1847     PnvMachineState *pnv = PNV_MACHINE(obj);
1848     int i;
1849     CPUState *cs;
1850 
1851     CPU_FOREACH(cs) {
1852         PowerPCCPU *cpu = POWERPC_CPU(cs);
1853 
1854         /* XXX: loop on each chip/core/thread instead of CPU_FOREACH() */
1855         PNV_CHIP_GET_CLASS(pnv->chips[0])->intc_print_info(pnv->chips[0], cpu,
1856                                                            mon);
1857     }
1858 
1859     for (i = 0; i < pnv->num_chips; i++) {
1860         PNV_CHIP_GET_CLASS(pnv->chips[i])->pic_print_info(pnv->chips[i], mon);
1861     }
1862 }
1863 
1864 static int pnv_match_nvt(XiveFabric *xfb, uint8_t format,
1865                          uint8_t nvt_blk, uint32_t nvt_idx,
1866                          bool cam_ignore, uint8_t priority,
1867                          uint32_t logic_serv,
1868                          XiveTCTXMatch *match)
1869 {
1870     PnvMachineState *pnv = PNV_MACHINE(xfb);
1871     int total_count = 0;
1872     int i;
1873 
1874     for (i = 0; i < pnv->num_chips; i++) {
1875         Pnv9Chip *chip9 = PNV9_CHIP(pnv->chips[i]);
1876         XivePresenter *xptr = XIVE_PRESENTER(&chip9->xive);
1877         XivePresenterClass *xpc = XIVE_PRESENTER_GET_CLASS(xptr);
1878         int count;
1879 
1880         count = xpc->match_nvt(xptr, format, nvt_blk, nvt_idx, cam_ignore,
1881                                priority, logic_serv, match);
1882 
1883         if (count < 0) {
1884             return count;
1885         }
1886 
1887         total_count += count;
1888     }
1889 
1890     return total_count;
1891 }
1892 
1893 static void pnv_machine_power8_class_init(ObjectClass *oc, void *data)
1894 {
1895     MachineClass *mc = MACHINE_CLASS(oc);
1896     XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
1897     PnvMachineClass *pmc = PNV_MACHINE_CLASS(oc);
1898     static const char compat[] = "qemu,powernv8\0qemu,powernv\0ibm,powernv";
1899 
1900     mc->desc = "IBM PowerNV (Non-Virtualized) POWER8";
1901     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power8_v2.0");
1902 
1903     xic->icp_get = pnv_icp_get;
1904     xic->ics_get = pnv_ics_get;
1905     xic->ics_resend = pnv_ics_resend;
1906 
1907     pmc->compat = compat;
1908     pmc->compat_size = sizeof(compat);
1909 }
1910 
1911 static void pnv_machine_power9_class_init(ObjectClass *oc, void *data)
1912 {
1913     MachineClass *mc = MACHINE_CLASS(oc);
1914     XiveFabricClass *xfc = XIVE_FABRIC_CLASS(oc);
1915     PnvMachineClass *pmc = PNV_MACHINE_CLASS(oc);
1916     static const char compat[] = "qemu,powernv9\0ibm,powernv";
1917 
1918     mc->desc = "IBM PowerNV (Non-Virtualized) POWER9";
1919     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power9_v2.0");
1920     xfc->match_nvt = pnv_match_nvt;
1921 
1922     mc->alias = "powernv";
1923 
1924     pmc->compat = compat;
1925     pmc->compat_size = sizeof(compat);
1926     pmc->dt_power_mgt = pnv_dt_power_mgt;
1927 }
1928 
1929 static void pnv_machine_power10_class_init(ObjectClass *oc, void *data)
1930 {
1931     MachineClass *mc = MACHINE_CLASS(oc);
1932     PnvMachineClass *pmc = PNV_MACHINE_CLASS(oc);
1933     static const char compat[] = "qemu,powernv10\0ibm,powernv";
1934 
1935     mc->desc = "IBM PowerNV (Non-Virtualized) POWER10";
1936     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power10_v1.0");
1937 
1938     pmc->compat = compat;
1939     pmc->compat_size = sizeof(compat);
1940     pmc->dt_power_mgt = pnv_dt_power_mgt;
1941 }
1942 
1943 static bool pnv_machine_get_hb(Object *obj, Error **errp)
1944 {
1945     PnvMachineState *pnv = PNV_MACHINE(obj);
1946 
1947     return !!pnv->fw_load_addr;
1948 }
1949 
1950 static void pnv_machine_set_hb(Object *obj, bool value, Error **errp)
1951 {
1952     PnvMachineState *pnv = PNV_MACHINE(obj);
1953 
1954     if (value) {
1955         pnv->fw_load_addr = 0x8000000;
1956     }
1957 }
1958 
1959 static void pnv_cpu_do_nmi_on_cpu(CPUState *cs, run_on_cpu_data arg)
1960 {
1961     PowerPCCPU *cpu = POWERPC_CPU(cs);
1962     CPUPPCState *env = &cpu->env;
1963 
1964     cpu_synchronize_state(cs);
1965     ppc_cpu_do_system_reset(cs);
1966     if (env->spr[SPR_SRR1] & SRR1_WAKESTATE) {
1967         /*
1968          * Power-save wakeups, as indicated by non-zero SRR1[46:47] put the
1969          * wakeup reason in SRR1[42:45], system reset is indicated with 0b0100
1970          * (PPC_BIT(43)).
1971          */
1972         if (!(env->spr[SPR_SRR1] & SRR1_WAKERESET)) {
1973             warn_report("ppc_cpu_do_system_reset does not set system reset wakeup reason");
1974             env->spr[SPR_SRR1] |= SRR1_WAKERESET;
1975         }
1976     } else {
1977         /*
1978          * For non-powersave system resets, SRR1[42:45] are defined to be
1979          * implementation-dependent. The POWER9 User Manual specifies that
1980          * an external (SCOM driven, which may come from a BMC nmi command or
1981          * another CPU requesting a NMI IPI) system reset exception should be
1982          * 0b0010 (PPC_BIT(44)).
1983          */
1984         env->spr[SPR_SRR1] |= SRR1_WAKESCOM;
1985     }
1986 }
1987 
1988 static void pnv_nmi(NMIState *n, int cpu_index, Error **errp)
1989 {
1990     CPUState *cs;
1991 
1992     CPU_FOREACH(cs) {
1993         async_run_on_cpu(cs, pnv_cpu_do_nmi_on_cpu, RUN_ON_CPU_NULL);
1994     }
1995 }
1996 
1997 static void pnv_machine_class_init(ObjectClass *oc, void *data)
1998 {
1999     MachineClass *mc = MACHINE_CLASS(oc);
2000     InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
2001     NMIClass *nc = NMI_CLASS(oc);
2002 
2003     mc->desc = "IBM PowerNV (Non-Virtualized)";
2004     mc->init = pnv_init;
2005     mc->reset = pnv_reset;
2006     mc->max_cpus = MAX_CPUS;
2007     /* Pnv provides a AHCI device for storage */
2008     mc->block_default_type = IF_IDE;
2009     mc->no_parallel = 1;
2010     mc->default_boot_order = NULL;
2011     /*
2012      * RAM defaults to less than 2048 for 32-bit hosts, and large
2013      * enough to fit the maximum initrd size at it's load address
2014      */
2015     mc->default_ram_size = INITRD_LOAD_ADDR + INITRD_MAX_SIZE;
2016     mc->default_ram_id = "pnv.ram";
2017     ispc->print_info = pnv_pic_print_info;
2018     nc->nmi_monitor_handler = pnv_nmi;
2019 
2020     object_class_property_add_bool(oc, "hb-mode",
2021                                    pnv_machine_get_hb, pnv_machine_set_hb);
2022     object_class_property_set_description(oc, "hb-mode",
2023                               "Use a hostboot like boot loader");
2024 }
2025 
2026 #define DEFINE_PNV8_CHIP_TYPE(type, class_initfn) \
2027     {                                             \
2028         .name          = type,                    \
2029         .class_init    = class_initfn,            \
2030         .parent        = TYPE_PNV8_CHIP,          \
2031     }
2032 
2033 #define DEFINE_PNV9_CHIP_TYPE(type, class_initfn) \
2034     {                                             \
2035         .name          = type,                    \
2036         .class_init    = class_initfn,            \
2037         .parent        = TYPE_PNV9_CHIP,          \
2038     }
2039 
2040 #define DEFINE_PNV10_CHIP_TYPE(type, class_initfn) \
2041     {                                              \
2042         .name          = type,                     \
2043         .class_init    = class_initfn,             \
2044         .parent        = TYPE_PNV10_CHIP,          \
2045     }
2046 
2047 static const TypeInfo types[] = {
2048     {
2049         .name          = MACHINE_TYPE_NAME("powernv10"),
2050         .parent        = TYPE_PNV_MACHINE,
2051         .class_init    = pnv_machine_power10_class_init,
2052     },
2053     {
2054         .name          = MACHINE_TYPE_NAME("powernv9"),
2055         .parent        = TYPE_PNV_MACHINE,
2056         .class_init    = pnv_machine_power9_class_init,
2057         .interfaces = (InterfaceInfo[]) {
2058             { TYPE_XIVE_FABRIC },
2059             { },
2060         },
2061     },
2062     {
2063         .name          = MACHINE_TYPE_NAME("powernv8"),
2064         .parent        = TYPE_PNV_MACHINE,
2065         .class_init    = pnv_machine_power8_class_init,
2066         .interfaces = (InterfaceInfo[]) {
2067             { TYPE_XICS_FABRIC },
2068             { },
2069         },
2070     },
2071     {
2072         .name          = TYPE_PNV_MACHINE,
2073         .parent        = TYPE_MACHINE,
2074         .abstract       = true,
2075         .instance_size = sizeof(PnvMachineState),
2076         .class_init    = pnv_machine_class_init,
2077         .class_size    = sizeof(PnvMachineClass),
2078         .interfaces = (InterfaceInfo[]) {
2079             { TYPE_INTERRUPT_STATS_PROVIDER },
2080             { TYPE_NMI },
2081             { },
2082         },
2083     },
2084     {
2085         .name          = TYPE_PNV_CHIP,
2086         .parent        = TYPE_SYS_BUS_DEVICE,
2087         .class_init    = pnv_chip_class_init,
2088         .instance_size = sizeof(PnvChip),
2089         .class_size    = sizeof(PnvChipClass),
2090         .abstract      = true,
2091     },
2092 
2093     /*
2094      * P10 chip and variants
2095      */
2096     {
2097         .name          = TYPE_PNV10_CHIP,
2098         .parent        = TYPE_PNV_CHIP,
2099         .instance_init = pnv_chip_power10_instance_init,
2100         .instance_size = sizeof(Pnv10Chip),
2101     },
2102     DEFINE_PNV10_CHIP_TYPE(TYPE_PNV_CHIP_POWER10, pnv_chip_power10_class_init),
2103 
2104     /*
2105      * P9 chip and variants
2106      */
2107     {
2108         .name          = TYPE_PNV9_CHIP,
2109         .parent        = TYPE_PNV_CHIP,
2110         .instance_init = pnv_chip_power9_instance_init,
2111         .instance_size = sizeof(Pnv9Chip),
2112     },
2113     DEFINE_PNV9_CHIP_TYPE(TYPE_PNV_CHIP_POWER9, pnv_chip_power9_class_init),
2114 
2115     /*
2116      * P8 chip and variants
2117      */
2118     {
2119         .name          = TYPE_PNV8_CHIP,
2120         .parent        = TYPE_PNV_CHIP,
2121         .instance_init = pnv_chip_power8_instance_init,
2122         .instance_size = sizeof(Pnv8Chip),
2123     },
2124     DEFINE_PNV8_CHIP_TYPE(TYPE_PNV_CHIP_POWER8, pnv_chip_power8_class_init),
2125     DEFINE_PNV8_CHIP_TYPE(TYPE_PNV_CHIP_POWER8E, pnv_chip_power8e_class_init),
2126     DEFINE_PNV8_CHIP_TYPE(TYPE_PNV_CHIP_POWER8NVL,
2127                           pnv_chip_power8nvl_class_init),
2128 };
2129 
2130 DEFINE_TYPES(types)
2131