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