xref: /openbmc/qemu/hw/ppc/pnv.c (revision 37677d7d)
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/sysemu.h"
25 #include "sysemu/numa.h"
26 #include "sysemu/cpus.h"
27 #include "hw/hw.h"
28 #include "target/ppc/cpu.h"
29 #include "qemu/log.h"
30 #include "hw/ppc/fdt.h"
31 #include "hw/ppc/ppc.h"
32 #include "hw/ppc/pnv.h"
33 #include "hw/ppc/pnv_core.h"
34 #include "hw/loader.h"
35 #include "exec/address-spaces.h"
36 #include "qapi/visitor.h"
37 #include "monitor/monitor.h"
38 #include "hw/intc/intc.h"
39 #include "hw/ipmi/ipmi.h"
40 #include "target/ppc/mmu-hash64.h"
41 
42 #include "hw/ppc/xics.h"
43 #include "hw/ppc/pnv_xscom.h"
44 
45 #include "hw/isa/isa.h"
46 #include "hw/char/serial.h"
47 #include "hw/timer/mc146818rtc.h"
48 
49 #include <libfdt.h>
50 
51 #define FDT_MAX_SIZE            (1 * MiB)
52 
53 #define FW_FILE_NAME            "skiboot.lid"
54 #define FW_LOAD_ADDR            0x0
55 #define FW_MAX_SIZE             (4 * MiB)
56 
57 #define KERNEL_LOAD_ADDR        0x20000000
58 #define KERNEL_MAX_SIZE         (256 * MiB)
59 #define INITRD_LOAD_ADDR        0x60000000
60 #define INITRD_MAX_SIZE         (256 * MiB)
61 
62 static const char *pnv_chip_core_typename(const PnvChip *o)
63 {
64     const char *chip_type = object_class_get_name(object_get_class(OBJECT(o)));
65     int len = strlen(chip_type) - strlen(PNV_CHIP_TYPE_SUFFIX);
66     char *s = g_strdup_printf(PNV_CORE_TYPE_NAME("%.*s"), len, chip_type);
67     const char *core_type = object_class_get_name(object_class_by_name(s));
68     g_free(s);
69     return core_type;
70 }
71 
72 /*
73  * On Power Systems E880 (POWER8), the max cpus (threads) should be :
74  *     4 * 4 sockets * 12 cores * 8 threads = 1536
75  * Let's make it 2^11
76  */
77 #define MAX_CPUS                2048
78 
79 /*
80  * Memory nodes are created by hostboot, one for each range of memory
81  * that has a different "affinity". In practice, it means one range
82  * per chip.
83  */
84 static void pnv_dt_memory(void *fdt, int chip_id, hwaddr start, hwaddr size)
85 {
86     char *mem_name;
87     uint64_t mem_reg_property[2];
88     int off;
89 
90     mem_reg_property[0] = cpu_to_be64(start);
91     mem_reg_property[1] = cpu_to_be64(size);
92 
93     mem_name = g_strdup_printf("memory@%"HWADDR_PRIx, start);
94     off = fdt_add_subnode(fdt, 0, mem_name);
95     g_free(mem_name);
96 
97     _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
98     _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
99                        sizeof(mem_reg_property))));
100     _FDT((fdt_setprop_cell(fdt, off, "ibm,chip-id", chip_id)));
101 }
102 
103 static int get_cpus_node(void *fdt)
104 {
105     int cpus_offset = fdt_path_offset(fdt, "/cpus");
106 
107     if (cpus_offset < 0) {
108         cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
109         if (cpus_offset) {
110             _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
111             _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
112         }
113     }
114     _FDT(cpus_offset);
115     return cpus_offset;
116 }
117 
118 /*
119  * The PowerNV cores (and threads) need to use real HW ids and not an
120  * incremental index like it has been done on other platforms. This HW
121  * id is stored in the CPU PIR, it is used to create cpu nodes in the
122  * device tree, used in XSCOM to address cores and in interrupt
123  * servers.
124  */
125 static void pnv_dt_core(PnvChip *chip, PnvCore *pc, void *fdt)
126 {
127     PowerPCCPU *cpu = pc->threads[0];
128     CPUState *cs = CPU(cpu);
129     DeviceClass *dc = DEVICE_GET_CLASS(cs);
130     int smt_threads = CPU_CORE(pc)->nr_threads;
131     CPUPPCState *env = &cpu->env;
132     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
133     uint32_t servers_prop[smt_threads];
134     int i;
135     uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
136                        0xffffffff, 0xffffffff};
137     uint32_t tbfreq = PNV_TIMEBASE_FREQ;
138     uint32_t cpufreq = 1000000000;
139     uint32_t page_sizes_prop[64];
140     size_t page_sizes_prop_size;
141     const uint8_t pa_features[] = { 24, 0,
142                                     0xf6, 0x3f, 0xc7, 0xc0, 0x80, 0xf0,
143                                     0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
144                                     0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
145                                     0x80, 0x00, 0x80, 0x00, 0x80, 0x00 };
146     int offset;
147     char *nodename;
148     int cpus_offset = get_cpus_node(fdt);
149 
150     nodename = g_strdup_printf("%s@%x", dc->fw_name, pc->pir);
151     offset = fdt_add_subnode(fdt, cpus_offset, nodename);
152     _FDT(offset);
153     g_free(nodename);
154 
155     _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id", chip->chip_id)));
156 
157     _FDT((fdt_setprop_cell(fdt, offset, "reg", pc->pir)));
158     _FDT((fdt_setprop_cell(fdt, offset, "ibm,pir", pc->pir)));
159     _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
160 
161     _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
162     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
163                             env->dcache_line_size)));
164     _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
165                             env->dcache_line_size)));
166     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
167                             env->icache_line_size)));
168     _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
169                             env->icache_line_size)));
170 
171     if (pcc->l1_dcache_size) {
172         _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
173                                pcc->l1_dcache_size)));
174     } else {
175         warn_report("Unknown L1 dcache size for cpu");
176     }
177     if (pcc->l1_icache_size) {
178         _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
179                                pcc->l1_icache_size)));
180     } else {
181         warn_report("Unknown L1 icache size for cpu");
182     }
183 
184     _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
185     _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
186     _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", cpu->hash64_opts->slb_size)));
187     _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
188     _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
189 
190     if (env->spr_cb[SPR_PURR].oea_read) {
191         _FDT((fdt_setprop(fdt, offset, "ibm,purr", NULL, 0)));
192     }
193 
194     if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG)) {
195         _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
196                            segs, sizeof(segs))));
197     }
198 
199     /* Advertise VMX/VSX (vector extensions) if available
200      *   0 / no property == no vector extensions
201      *   1               == VMX / Altivec available
202      *   2               == VSX available */
203     if (env->insns_flags & PPC_ALTIVEC) {
204         uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
205 
206         _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", vmx)));
207     }
208 
209     /* Advertise DFP (Decimal Floating Point) if available
210      *   0 / no property == no DFP
211      *   1               == DFP available */
212     if (env->insns_flags2 & PPC2_DFP) {
213         _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
214     }
215 
216     page_sizes_prop_size = ppc_create_page_sizes_prop(cpu, page_sizes_prop,
217                                                       sizeof(page_sizes_prop));
218     if (page_sizes_prop_size) {
219         _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
220                            page_sizes_prop, page_sizes_prop_size)));
221     }
222 
223     _FDT((fdt_setprop(fdt, offset, "ibm,pa-features",
224                        pa_features, sizeof(pa_features))));
225 
226     /* Build interrupt servers properties */
227     for (i = 0; i < smt_threads; i++) {
228         servers_prop[i] = cpu_to_be32(pc->pir + i);
229     }
230     _FDT((fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
231                        servers_prop, sizeof(servers_prop))));
232 }
233 
234 static void pnv_dt_icp(PnvChip *chip, void *fdt, uint32_t pir,
235                        uint32_t nr_threads)
236 {
237     uint64_t addr = PNV_ICP_BASE(chip) | (pir << 12);
238     char *name;
239     const char compat[] = "IBM,power8-icp\0IBM,ppc-xicp";
240     uint32_t irange[2], i, rsize;
241     uint64_t *reg;
242     int offset;
243 
244     irange[0] = cpu_to_be32(pir);
245     irange[1] = cpu_to_be32(nr_threads);
246 
247     rsize = sizeof(uint64_t) * 2 * nr_threads;
248     reg = g_malloc(rsize);
249     for (i = 0; i < nr_threads; i++) {
250         reg[i * 2] = cpu_to_be64(addr | ((pir + i) * 0x1000));
251         reg[i * 2 + 1] = cpu_to_be64(0x1000);
252     }
253 
254     name = g_strdup_printf("interrupt-controller@%"PRIX64, addr);
255     offset = fdt_add_subnode(fdt, 0, name);
256     _FDT(offset);
257     g_free(name);
258 
259     _FDT((fdt_setprop(fdt, offset, "compatible", compat, sizeof(compat))));
260     _FDT((fdt_setprop(fdt, offset, "reg", reg, rsize)));
261     _FDT((fdt_setprop_string(fdt, offset, "device_type",
262                               "PowerPC-External-Interrupt-Presentation")));
263     _FDT((fdt_setprop(fdt, offset, "interrupt-controller", NULL, 0)));
264     _FDT((fdt_setprop(fdt, offset, "ibm,interrupt-server-ranges",
265                        irange, sizeof(irange))));
266     _FDT((fdt_setprop_cell(fdt, offset, "#interrupt-cells", 1)));
267     _FDT((fdt_setprop_cell(fdt, offset, "#address-cells", 0)));
268     g_free(reg);
269 }
270 
271 static void pnv_chip_power8_dt_populate(PnvChip *chip, void *fdt)
272 {
273     const char *typename = pnv_chip_core_typename(chip);
274     size_t typesize = object_type_get_instance_size(typename);
275     int i;
276 
277     pnv_dt_xscom(chip, fdt, 0);
278 
279     for (i = 0; i < chip->nr_cores; i++) {
280         PnvCore *pnv_core = PNV_CORE(chip->cores + i * typesize);
281 
282         pnv_dt_core(chip, pnv_core, fdt);
283 
284         /* Interrupt Control Presenters (ICP). One per core. */
285         pnv_dt_icp(chip, fdt, pnv_core->pir, CPU_CORE(pnv_core)->nr_threads);
286     }
287 
288     if (chip->ram_size) {
289         pnv_dt_memory(fdt, chip->chip_id, chip->ram_start, chip->ram_size);
290     }
291 }
292 
293 static void pnv_chip_power9_dt_populate(PnvChip *chip, void *fdt)
294 {
295     const char *typename = pnv_chip_core_typename(chip);
296     size_t typesize = object_type_get_instance_size(typename);
297     int i;
298 
299     pnv_dt_xscom(chip, fdt, 0);
300 
301     for (i = 0; i < chip->nr_cores; i++) {
302         PnvCore *pnv_core = PNV_CORE(chip->cores + i * typesize);
303 
304         pnv_dt_core(chip, pnv_core, fdt);
305     }
306 
307     if (chip->ram_size) {
308         pnv_dt_memory(fdt, chip->chip_id, chip->ram_start, chip->ram_size);
309     }
310 
311     pnv_dt_lpc(chip, fdt, 0);
312 }
313 
314 static void pnv_dt_rtc(ISADevice *d, void *fdt, int lpc_off)
315 {
316     uint32_t io_base = d->ioport_id;
317     uint32_t io_regs[] = {
318         cpu_to_be32(1),
319         cpu_to_be32(io_base),
320         cpu_to_be32(2)
321     };
322     char *name;
323     int node;
324 
325     name = g_strdup_printf("%s@i%x", qdev_fw_name(DEVICE(d)), io_base);
326     node = fdt_add_subnode(fdt, lpc_off, name);
327     _FDT(node);
328     g_free(name);
329 
330     _FDT((fdt_setprop(fdt, node, "reg", io_regs, sizeof(io_regs))));
331     _FDT((fdt_setprop_string(fdt, node, "compatible", "pnpPNP,b00")));
332 }
333 
334 static void pnv_dt_serial(ISADevice *d, void *fdt, int lpc_off)
335 {
336     const char compatible[] = "ns16550\0pnpPNP,501";
337     uint32_t io_base = d->ioport_id;
338     uint32_t io_regs[] = {
339         cpu_to_be32(1),
340         cpu_to_be32(io_base),
341         cpu_to_be32(8)
342     };
343     char *name;
344     int node;
345 
346     name = g_strdup_printf("%s@i%x", qdev_fw_name(DEVICE(d)), io_base);
347     node = fdt_add_subnode(fdt, lpc_off, name);
348     _FDT(node);
349     g_free(name);
350 
351     _FDT((fdt_setprop(fdt, node, "reg", io_regs, sizeof(io_regs))));
352     _FDT((fdt_setprop(fdt, node, "compatible", compatible,
353                       sizeof(compatible))));
354 
355     _FDT((fdt_setprop_cell(fdt, node, "clock-frequency", 1843200)));
356     _FDT((fdt_setprop_cell(fdt, node, "current-speed", 115200)));
357     _FDT((fdt_setprop_cell(fdt, node, "interrupts", d->isairq[0])));
358     _FDT((fdt_setprop_cell(fdt, node, "interrupt-parent",
359                            fdt_get_phandle(fdt, lpc_off))));
360 
361     /* This is needed by Linux */
362     _FDT((fdt_setprop_string(fdt, node, "device_type", "serial")));
363 }
364 
365 static void pnv_dt_ipmi_bt(ISADevice *d, void *fdt, int lpc_off)
366 {
367     const char compatible[] = "bt\0ipmi-bt";
368     uint32_t io_base;
369     uint32_t io_regs[] = {
370         cpu_to_be32(1),
371         0, /* 'io_base' retrieved from the 'ioport' property of 'isa-ipmi-bt' */
372         cpu_to_be32(3)
373     };
374     uint32_t irq;
375     char *name;
376     int node;
377 
378     io_base = object_property_get_int(OBJECT(d), "ioport", &error_fatal);
379     io_regs[1] = cpu_to_be32(io_base);
380 
381     irq = object_property_get_int(OBJECT(d), "irq", &error_fatal);
382 
383     name = g_strdup_printf("%s@i%x", qdev_fw_name(DEVICE(d)), io_base);
384     node = fdt_add_subnode(fdt, lpc_off, name);
385     _FDT(node);
386     g_free(name);
387 
388     _FDT((fdt_setprop(fdt, node, "reg", io_regs, sizeof(io_regs))));
389     _FDT((fdt_setprop(fdt, node, "compatible", compatible,
390                       sizeof(compatible))));
391 
392     /* Mark it as reserved to avoid Linux trying to claim it */
393     _FDT((fdt_setprop_string(fdt, node, "status", "reserved")));
394     _FDT((fdt_setprop_cell(fdt, node, "interrupts", irq)));
395     _FDT((fdt_setprop_cell(fdt, node, "interrupt-parent",
396                            fdt_get_phandle(fdt, lpc_off))));
397 }
398 
399 typedef struct ForeachPopulateArgs {
400     void *fdt;
401     int offset;
402 } ForeachPopulateArgs;
403 
404 static int pnv_dt_isa_device(DeviceState *dev, void *opaque)
405 {
406     ForeachPopulateArgs *args = opaque;
407     ISADevice *d = ISA_DEVICE(dev);
408 
409     if (object_dynamic_cast(OBJECT(dev), TYPE_MC146818_RTC)) {
410         pnv_dt_rtc(d, args->fdt, args->offset);
411     } else if (object_dynamic_cast(OBJECT(dev), TYPE_ISA_SERIAL)) {
412         pnv_dt_serial(d, args->fdt, args->offset);
413     } else if (object_dynamic_cast(OBJECT(dev), "isa-ipmi-bt")) {
414         pnv_dt_ipmi_bt(d, args->fdt, args->offset);
415     } else {
416         error_report("unknown isa device %s@i%x", qdev_fw_name(dev),
417                      d->ioport_id);
418     }
419 
420     return 0;
421 }
422 
423 /* The default LPC bus of a multichip system is on chip 0. It's
424  * recognized by the firmware (skiboot) using a "primary" property.
425  */
426 static void pnv_dt_isa(PnvMachineState *pnv, void *fdt)
427 {
428     int isa_offset = fdt_path_offset(fdt, pnv->chips[0]->dt_isa_nodename);
429     ForeachPopulateArgs args = {
430         .fdt = fdt,
431         .offset = isa_offset,
432     };
433 
434     _FDT((fdt_setprop(fdt, isa_offset, "primary", NULL, 0)));
435 
436     /* ISA devices are not necessarily parented to the ISA bus so we
437      * can not use object_child_foreach() */
438     qbus_walk_children(BUS(pnv->isa_bus), pnv_dt_isa_device, NULL, NULL, NULL,
439                        &args);
440 }
441 
442 static void pnv_dt_power_mgt(void *fdt)
443 {
444     int off;
445 
446     off = fdt_add_subnode(fdt, 0, "ibm,opal");
447     off = fdt_add_subnode(fdt, off, "power-mgt");
448 
449     _FDT(fdt_setprop_cell(fdt, off, "ibm,enabled-stop-levels", 0xc0000000));
450 }
451 
452 static void *pnv_dt_create(MachineState *machine)
453 {
454     const char plat_compat8[] = "qemu,powernv8\0qemu,powernv\0ibm,powernv";
455     const char plat_compat9[] = "qemu,powernv9\0ibm,powernv";
456     PnvMachineState *pnv = PNV_MACHINE(machine);
457     void *fdt;
458     char *buf;
459     int off;
460     int i;
461 
462     fdt = g_malloc0(FDT_MAX_SIZE);
463     _FDT((fdt_create_empty_tree(fdt, FDT_MAX_SIZE)));
464 
465     /* Root node */
466     _FDT((fdt_setprop_cell(fdt, 0, "#address-cells", 0x2)));
467     _FDT((fdt_setprop_cell(fdt, 0, "#size-cells", 0x2)));
468     _FDT((fdt_setprop_string(fdt, 0, "model",
469                              "IBM PowerNV (emulated by qemu)")));
470     if (pnv_is_power9(pnv)) {
471         _FDT((fdt_setprop(fdt, 0, "compatible", plat_compat9,
472                           sizeof(plat_compat9))));
473     } else {
474         _FDT((fdt_setprop(fdt, 0, "compatible", plat_compat8,
475                           sizeof(plat_compat8))));
476     }
477 
478 
479     buf =  qemu_uuid_unparse_strdup(&qemu_uuid);
480     _FDT((fdt_setprop_string(fdt, 0, "vm,uuid", buf)));
481     if (qemu_uuid_set) {
482         _FDT((fdt_property_string(fdt, "system-id", buf)));
483     }
484     g_free(buf);
485 
486     off = fdt_add_subnode(fdt, 0, "chosen");
487     if (machine->kernel_cmdline) {
488         _FDT((fdt_setprop_string(fdt, off, "bootargs",
489                                  machine->kernel_cmdline)));
490     }
491 
492     if (pnv->initrd_size) {
493         uint32_t start_prop = cpu_to_be32(pnv->initrd_base);
494         uint32_t end_prop = cpu_to_be32(pnv->initrd_base + pnv->initrd_size);
495 
496         _FDT((fdt_setprop(fdt, off, "linux,initrd-start",
497                                &start_prop, sizeof(start_prop))));
498         _FDT((fdt_setprop(fdt, off, "linux,initrd-end",
499                                &end_prop, sizeof(end_prop))));
500     }
501 
502     /* Populate device tree for each chip */
503     for (i = 0; i < pnv->num_chips; i++) {
504         PNV_CHIP_GET_CLASS(pnv->chips[i])->dt_populate(pnv->chips[i], fdt);
505     }
506 
507     /* Populate ISA devices on chip 0 */
508     pnv_dt_isa(pnv, fdt);
509 
510     if (pnv->bmc) {
511         pnv_dt_bmc_sensors(pnv->bmc, fdt);
512     }
513 
514     /* Create an extra node for power management on Power9 */
515     if (pnv_is_power9(pnv)) {
516         pnv_dt_power_mgt(fdt);
517     }
518 
519     return fdt;
520 }
521 
522 static void pnv_powerdown_notify(Notifier *n, void *opaque)
523 {
524     PnvMachineState *pnv = PNV_MACHINE(qdev_get_machine());
525 
526     if (pnv->bmc) {
527         pnv_bmc_powerdown(pnv->bmc);
528     }
529 }
530 
531 static void pnv_reset(void)
532 {
533     MachineState *machine = MACHINE(qdev_get_machine());
534     PnvMachineState *pnv = PNV_MACHINE(machine);
535     void *fdt;
536     Object *obj;
537 
538     qemu_devices_reset();
539 
540     /* OpenPOWER systems have a BMC, which can be defined on the
541      * command line with:
542      *
543      *   -device ipmi-bmc-sim,id=bmc0
544      *
545      * This is the internal simulator but it could also be an external
546      * BMC.
547      */
548     obj = object_resolve_path_type("", "ipmi-bmc-sim", NULL);
549     if (obj) {
550         pnv->bmc = IPMI_BMC(obj);
551     }
552 
553     fdt = pnv_dt_create(machine);
554 
555     /* Pack resulting tree */
556     _FDT((fdt_pack(fdt)));
557 
558     cpu_physical_memory_write(PNV_FDT_ADDR, fdt, fdt_totalsize(fdt));
559 }
560 
561 static ISABus *pnv_chip_power8_isa_create(PnvChip *chip, Error **errp)
562 {
563     Pnv8Chip *chip8 = PNV8_CHIP(chip);
564     return pnv_lpc_isa_create(&chip8->lpc, true, errp);
565 }
566 
567 static ISABus *pnv_chip_power8nvl_isa_create(PnvChip *chip, Error **errp)
568 {
569     Pnv8Chip *chip8 = PNV8_CHIP(chip);
570     return pnv_lpc_isa_create(&chip8->lpc, false, errp);
571 }
572 
573 static ISABus *pnv_chip_power9_isa_create(PnvChip *chip, Error **errp)
574 {
575     Pnv9Chip *chip9 = PNV9_CHIP(chip);
576     return pnv_lpc_isa_create(&chip9->lpc, false, errp);
577 }
578 
579 static ISABus *pnv_isa_create(PnvChip *chip, Error **errp)
580 {
581     return PNV_CHIP_GET_CLASS(chip)->isa_create(chip, errp);
582 }
583 
584 static void pnv_chip_power8_pic_print_info(PnvChip *chip, Monitor *mon)
585 {
586     Pnv8Chip *chip8 = PNV8_CHIP(chip);
587 
588     ics_pic_print_info(&chip8->psi.ics, mon);
589 }
590 
591 static void pnv_chip_power9_pic_print_info(PnvChip *chip, Monitor *mon)
592 {
593     Pnv9Chip *chip9 = PNV9_CHIP(chip);
594 
595     pnv_xive_pic_print_info(&chip9->xive, mon);
596     pnv_psi_pic_print_info(&chip9->psi, mon);
597 }
598 
599 static void pnv_init(MachineState *machine)
600 {
601     PnvMachineState *pnv = PNV_MACHINE(machine);
602     MemoryRegion *ram;
603     char *fw_filename;
604     long fw_size;
605     int i;
606     char *chip_typename;
607 
608     /* allocate RAM */
609     if (machine->ram_size < (1 * GiB)) {
610         warn_report("skiboot may not work with < 1GB of RAM");
611     }
612 
613     ram = g_new(MemoryRegion, 1);
614     memory_region_allocate_system_memory(ram, NULL, "pnv.ram",
615                                          machine->ram_size);
616     memory_region_add_subregion(get_system_memory(), 0, ram);
617 
618     /* load skiboot firmware  */
619     if (bios_name == NULL) {
620         bios_name = FW_FILE_NAME;
621     }
622 
623     fw_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
624     if (!fw_filename) {
625         error_report("Could not find OPAL firmware '%s'", bios_name);
626         exit(1);
627     }
628 
629     fw_size = load_image_targphys(fw_filename, FW_LOAD_ADDR, FW_MAX_SIZE);
630     if (fw_size < 0) {
631         error_report("Could not load OPAL firmware '%s'", fw_filename);
632         exit(1);
633     }
634     g_free(fw_filename);
635 
636     /* load kernel */
637     if (machine->kernel_filename) {
638         long kernel_size;
639 
640         kernel_size = load_image_targphys(machine->kernel_filename,
641                                           KERNEL_LOAD_ADDR, KERNEL_MAX_SIZE);
642         if (kernel_size < 0) {
643             error_report("Could not load kernel '%s'",
644                          machine->kernel_filename);
645             exit(1);
646         }
647     }
648 
649     /* load initrd */
650     if (machine->initrd_filename) {
651         pnv->initrd_base = INITRD_LOAD_ADDR;
652         pnv->initrd_size = load_image_targphys(machine->initrd_filename,
653                                   pnv->initrd_base, INITRD_MAX_SIZE);
654         if (pnv->initrd_size < 0) {
655             error_report("Could not load initial ram disk '%s'",
656                          machine->initrd_filename);
657             exit(1);
658         }
659     }
660 
661     /* Create the processor chips */
662     i = strlen(machine->cpu_type) - strlen(POWERPC_CPU_TYPE_SUFFIX);
663     chip_typename = g_strdup_printf(PNV_CHIP_TYPE_NAME("%.*s"),
664                                     i, machine->cpu_type);
665     if (!object_class_by_name(chip_typename)) {
666         error_report("invalid CPU model '%.*s' for %s machine",
667                      i, machine->cpu_type, MACHINE_GET_CLASS(machine)->name);
668         exit(1);
669     }
670 
671     pnv->chips = g_new0(PnvChip *, pnv->num_chips);
672     for (i = 0; i < pnv->num_chips; i++) {
673         char chip_name[32];
674         Object *chip = object_new(chip_typename);
675 
676         pnv->chips[i] = PNV_CHIP(chip);
677 
678         /* TODO: put all the memory in one node on chip 0 until we find a
679          * way to specify different ranges for each chip
680          */
681         if (i == 0) {
682             object_property_set_int(chip, machine->ram_size, "ram-size",
683                                     &error_fatal);
684         }
685 
686         snprintf(chip_name, sizeof(chip_name), "chip[%d]", PNV_CHIP_HWID(i));
687         object_property_add_child(OBJECT(pnv), chip_name, chip, &error_fatal);
688         object_property_set_int(chip, PNV_CHIP_HWID(i), "chip-id",
689                                 &error_fatal);
690         object_property_set_int(chip, smp_cores, "nr-cores", &error_fatal);
691         object_property_set_bool(chip, true, "realized", &error_fatal);
692     }
693     g_free(chip_typename);
694 
695     /* Instantiate ISA bus on chip 0 */
696     pnv->isa_bus = pnv_isa_create(pnv->chips[0], &error_fatal);
697 
698     /* Create serial port */
699     serial_hds_isa_init(pnv->isa_bus, 0, MAX_ISA_SERIAL_PORTS);
700 
701     /* Create an RTC ISA device too */
702     mc146818_rtc_init(pnv->isa_bus, 2000, NULL);
703 
704     /* OpenPOWER systems use a IPMI SEL Event message to notify the
705      * host to powerdown */
706     pnv->powerdown_notifier.notify = pnv_powerdown_notify;
707     qemu_register_powerdown_notifier(&pnv->powerdown_notifier);
708 }
709 
710 /*
711  *    0:21  Reserved - Read as zeros
712  *   22:24  Chip ID
713  *   25:28  Core number
714  *   29:31  Thread ID
715  */
716 static uint32_t pnv_chip_core_pir_p8(PnvChip *chip, uint32_t core_id)
717 {
718     return (chip->chip_id << 7) | (core_id << 3);
719 }
720 
721 static void pnv_chip_power8_intc_create(PnvChip *chip, PowerPCCPU *cpu,
722                                         Error **errp)
723 {
724     Error *local_err = NULL;
725     Object *obj;
726     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
727 
728     obj = icp_create(OBJECT(cpu), TYPE_PNV_ICP, XICS_FABRIC(qdev_get_machine()),
729                      &local_err);
730     if (local_err) {
731         error_propagate(errp, local_err);
732         return;
733     }
734 
735     pnv_cpu->intc = obj;
736 }
737 
738 /*
739  *    0:48  Reserved - Read as zeroes
740  *   49:52  Node ID
741  *   53:55  Chip ID
742  *   56     Reserved - Read as zero
743  *   57:61  Core number
744  *   62:63  Thread ID
745  *
746  * We only care about the lower bits. uint32_t is fine for the moment.
747  */
748 static uint32_t pnv_chip_core_pir_p9(PnvChip *chip, uint32_t core_id)
749 {
750     return (chip->chip_id << 8) | (core_id << 2);
751 }
752 
753 static void pnv_chip_power9_intc_create(PnvChip *chip, PowerPCCPU *cpu,
754                                         Error **errp)
755 {
756     Pnv9Chip *chip9 = PNV9_CHIP(chip);
757     Error *local_err = NULL;
758     Object *obj;
759     PnvCPUState *pnv_cpu = pnv_cpu_state(cpu);
760 
761     /*
762      * The core creates its interrupt presenter but the XIVE interrupt
763      * controller object is initialized afterwards. Hopefully, it's
764      * only used at runtime.
765      */
766     obj = xive_tctx_create(OBJECT(cpu), XIVE_ROUTER(&chip9->xive), &local_err);
767     if (local_err) {
768         error_propagate(errp, local_err);
769         return;
770     }
771 
772     pnv_cpu->intc = obj;
773 }
774 
775 /* Allowed core identifiers on a POWER8 Processor Chip :
776  *
777  * <EX0 reserved>
778  *  EX1  - Venice only
779  *  EX2  - Venice only
780  *  EX3  - Venice only
781  *  EX4
782  *  EX5
783  *  EX6
784  * <EX7,8 reserved> <reserved>
785  *  EX9  - Venice only
786  *  EX10 - Venice only
787  *  EX11 - Venice only
788  *  EX12
789  *  EX13
790  *  EX14
791  * <EX15 reserved>
792  */
793 #define POWER8E_CORE_MASK  (0x7070ull)
794 #define POWER8_CORE_MASK   (0x7e7eull)
795 
796 /*
797  * POWER9 has 24 cores, ids starting at 0x0
798  */
799 #define POWER9_CORE_MASK   (0xffffffffffffffull)
800 
801 static void pnv_chip_power8_instance_init(Object *obj)
802 {
803     Pnv8Chip *chip8 = PNV8_CHIP(obj);
804 
805     object_initialize_child(obj, "psi",  &chip8->psi, sizeof(chip8->psi),
806                             TYPE_PNV8_PSI, &error_abort, NULL);
807     object_property_add_const_link(OBJECT(&chip8->psi), "xics",
808                                    OBJECT(qdev_get_machine()), &error_abort);
809 
810     object_initialize_child(obj, "lpc",  &chip8->lpc, sizeof(chip8->lpc),
811                             TYPE_PNV8_LPC, &error_abort, NULL);
812     object_property_add_const_link(OBJECT(&chip8->lpc), "psi",
813                                    OBJECT(&chip8->psi), &error_abort);
814 
815     object_initialize_child(obj, "occ",  &chip8->occ, sizeof(chip8->occ),
816                             TYPE_PNV8_OCC, &error_abort, NULL);
817     object_property_add_const_link(OBJECT(&chip8->occ), "psi",
818                                    OBJECT(&chip8->psi), &error_abort);
819 }
820 
821 static void pnv_chip_icp_realize(Pnv8Chip *chip8, Error **errp)
822  {
823     PnvChip *chip = PNV_CHIP(chip8);
824     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(chip);
825     const char *typename = pnv_chip_core_typename(chip);
826     size_t typesize = object_type_get_instance_size(typename);
827     int i, j;
828     char *name;
829     XICSFabric *xi = XICS_FABRIC(qdev_get_machine());
830 
831     name = g_strdup_printf("icp-%x", chip->chip_id);
832     memory_region_init(&chip8->icp_mmio, OBJECT(chip), name, PNV_ICP_SIZE);
833     sysbus_init_mmio(SYS_BUS_DEVICE(chip), &chip8->icp_mmio);
834     g_free(name);
835 
836     sysbus_mmio_map(SYS_BUS_DEVICE(chip), 1, PNV_ICP_BASE(chip));
837 
838     /* Map the ICP registers for each thread */
839     for (i = 0; i < chip->nr_cores; i++) {
840         PnvCore *pnv_core = PNV_CORE(chip->cores + i * typesize);
841         int core_hwid = CPU_CORE(pnv_core)->core_id;
842 
843         for (j = 0; j < CPU_CORE(pnv_core)->nr_threads; j++) {
844             uint32_t pir = pcc->core_pir(chip, core_hwid) + j;
845             PnvICPState *icp = PNV_ICP(xics_icp_get(xi, pir));
846 
847             memory_region_add_subregion(&chip8->icp_mmio, pir << 12,
848                                         &icp->mmio);
849         }
850     }
851 }
852 
853 static void pnv_chip_power8_realize(DeviceState *dev, Error **errp)
854 {
855     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(dev);
856     PnvChip *chip = PNV_CHIP(dev);
857     Pnv8Chip *chip8 = PNV8_CHIP(dev);
858     Pnv8Psi *psi8 = &chip8->psi;
859     Error *local_err = NULL;
860 
861     pcc->parent_realize(dev, &local_err);
862     if (local_err) {
863         error_propagate(errp, local_err);
864         return;
865     }
866 
867     /* Processor Service Interface (PSI) Host Bridge */
868     object_property_set_int(OBJECT(&chip8->psi), PNV_PSIHB_BASE(chip),
869                             "bar", &error_fatal);
870     object_property_set_bool(OBJECT(&chip8->psi), true, "realized", &local_err);
871     if (local_err) {
872         error_propagate(errp, local_err);
873         return;
874     }
875     pnv_xscom_add_subregion(chip, PNV_XSCOM_PSIHB_BASE,
876                             &PNV_PSI(psi8)->xscom_regs);
877 
878     /* Create LPC controller */
879     object_property_set_bool(OBJECT(&chip8->lpc), true, "realized",
880                              &error_fatal);
881     pnv_xscom_add_subregion(chip, PNV_XSCOM_LPC_BASE, &chip8->lpc.xscom_regs);
882 
883     chip->dt_isa_nodename = g_strdup_printf("/xscom@%" PRIx64 "/isa@%x",
884                                             (uint64_t) PNV_XSCOM_BASE(chip),
885                                             PNV_XSCOM_LPC_BASE);
886 
887     /* Interrupt Management Area. This is the memory region holding
888      * all the Interrupt Control Presenter (ICP) registers */
889     pnv_chip_icp_realize(chip8, &local_err);
890     if (local_err) {
891         error_propagate(errp, local_err);
892         return;
893     }
894 
895     /* Create the simplified OCC model */
896     object_property_set_bool(OBJECT(&chip8->occ), true, "realized", &local_err);
897     if (local_err) {
898         error_propagate(errp, local_err);
899         return;
900     }
901     pnv_xscom_add_subregion(chip, PNV_XSCOM_OCC_BASE, &chip8->occ.xscom_regs);
902 }
903 
904 static void pnv_chip_power8e_class_init(ObjectClass *klass, void *data)
905 {
906     DeviceClass *dc = DEVICE_CLASS(klass);
907     PnvChipClass *k = PNV_CHIP_CLASS(klass);
908 
909     k->chip_type = PNV_CHIP_POWER8E;
910     k->chip_cfam_id = 0x221ef04980000000ull;  /* P8 Murano DD2.1 */
911     k->cores_mask = POWER8E_CORE_MASK;
912     k->core_pir = pnv_chip_core_pir_p8;
913     k->intc_create = pnv_chip_power8_intc_create;
914     k->isa_create = pnv_chip_power8_isa_create;
915     k->dt_populate = pnv_chip_power8_dt_populate;
916     k->pic_print_info = pnv_chip_power8_pic_print_info;
917     k->xscom_base = 0x003fc0000000000ull;
918     dc->desc = "PowerNV Chip POWER8E";
919 
920     device_class_set_parent_realize(dc, pnv_chip_power8_realize,
921                                     &k->parent_realize);
922 }
923 
924 static void pnv_chip_power8_class_init(ObjectClass *klass, void *data)
925 {
926     DeviceClass *dc = DEVICE_CLASS(klass);
927     PnvChipClass *k = PNV_CHIP_CLASS(klass);
928 
929     k->chip_type = PNV_CHIP_POWER8;
930     k->chip_cfam_id = 0x220ea04980000000ull; /* P8 Venice DD2.0 */
931     k->cores_mask = POWER8_CORE_MASK;
932     k->core_pir = pnv_chip_core_pir_p8;
933     k->intc_create = pnv_chip_power8_intc_create;
934     k->isa_create = pnv_chip_power8_isa_create;
935     k->dt_populate = pnv_chip_power8_dt_populate;
936     k->pic_print_info = pnv_chip_power8_pic_print_info;
937     k->xscom_base = 0x003fc0000000000ull;
938     dc->desc = "PowerNV Chip POWER8";
939 
940     device_class_set_parent_realize(dc, pnv_chip_power8_realize,
941                                     &k->parent_realize);
942 }
943 
944 static void pnv_chip_power8nvl_class_init(ObjectClass *klass, void *data)
945 {
946     DeviceClass *dc = DEVICE_CLASS(klass);
947     PnvChipClass *k = PNV_CHIP_CLASS(klass);
948 
949     k->chip_type = PNV_CHIP_POWER8NVL;
950     k->chip_cfam_id = 0x120d304980000000ull;  /* P8 Naples DD1.0 */
951     k->cores_mask = POWER8_CORE_MASK;
952     k->core_pir = pnv_chip_core_pir_p8;
953     k->intc_create = pnv_chip_power8_intc_create;
954     k->isa_create = pnv_chip_power8nvl_isa_create;
955     k->dt_populate = pnv_chip_power8_dt_populate;
956     k->pic_print_info = pnv_chip_power8_pic_print_info;
957     k->xscom_base = 0x003fc0000000000ull;
958     dc->desc = "PowerNV Chip POWER8NVL";
959 
960     device_class_set_parent_realize(dc, pnv_chip_power8_realize,
961                                     &k->parent_realize);
962 }
963 
964 static void pnv_chip_power9_instance_init(Object *obj)
965 {
966     Pnv9Chip *chip9 = PNV9_CHIP(obj);
967 
968     object_initialize_child(obj, "xive", &chip9->xive, sizeof(chip9->xive),
969                             TYPE_PNV_XIVE, &error_abort, NULL);
970     object_property_add_const_link(OBJECT(&chip9->xive), "chip", obj,
971                                    &error_abort);
972 
973     object_initialize_child(obj, "psi",  &chip9->psi, sizeof(chip9->psi),
974                             TYPE_PNV9_PSI, &error_abort, NULL);
975     object_property_add_const_link(OBJECT(&chip9->psi), "chip", obj,
976                                    &error_abort);
977 
978     object_initialize_child(obj, "lpc",  &chip9->lpc, sizeof(chip9->lpc),
979                             TYPE_PNV9_LPC, &error_abort, NULL);
980     object_property_add_const_link(OBJECT(&chip9->lpc), "psi",
981                                    OBJECT(&chip9->psi), &error_abort);
982 
983     object_initialize_child(obj, "occ",  &chip9->occ, sizeof(chip9->occ),
984                             TYPE_PNV9_OCC, &error_abort, NULL);
985     object_property_add_const_link(OBJECT(&chip9->occ), "psi",
986                                    OBJECT(&chip9->psi), &error_abort);
987 }
988 
989 static void pnv_chip_quad_realize(Pnv9Chip *chip9, Error **errp)
990 {
991     PnvChip *chip = PNV_CHIP(chip9);
992     const char *typename = pnv_chip_core_typename(chip);
993     size_t typesize = object_type_get_instance_size(typename);
994     int i;
995 
996     chip9->nr_quads = DIV_ROUND_UP(chip->nr_cores, 4);
997     chip9->quads = g_new0(PnvQuad, chip9->nr_quads);
998 
999     for (i = 0; i < chip9->nr_quads; i++) {
1000         char eq_name[32];
1001         PnvQuad *eq = &chip9->quads[i];
1002         PnvCore *pnv_core = PNV_CORE(chip->cores + (i * 4) * typesize);
1003         int core_id = CPU_CORE(pnv_core)->core_id;
1004 
1005         snprintf(eq_name, sizeof(eq_name), "eq[%d]", core_id);
1006         object_initialize_child(OBJECT(chip), eq_name, eq, sizeof(*eq),
1007                                 TYPE_PNV_QUAD, &error_fatal, NULL);
1008 
1009         object_property_set_int(OBJECT(eq), core_id, "id", &error_fatal);
1010         object_property_set_bool(OBJECT(eq), true, "realized", &error_fatal);
1011 
1012         pnv_xscom_add_subregion(chip, PNV9_XSCOM_EQ_BASE(eq->id),
1013                                 &eq->xscom_regs);
1014     }
1015 }
1016 
1017 static void pnv_chip_power9_realize(DeviceState *dev, Error **errp)
1018 {
1019     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(dev);
1020     Pnv9Chip *chip9 = PNV9_CHIP(dev);
1021     PnvChip *chip = PNV_CHIP(dev);
1022     Pnv9Psi *psi9 = &chip9->psi;
1023     Error *local_err = NULL;
1024 
1025     pcc->parent_realize(dev, &local_err);
1026     if (local_err) {
1027         error_propagate(errp, local_err);
1028         return;
1029     }
1030 
1031     pnv_chip_quad_realize(chip9, &local_err);
1032     if (local_err) {
1033         error_propagate(errp, local_err);
1034         return;
1035     }
1036 
1037     /* XIVE interrupt controller (POWER9) */
1038     object_property_set_int(OBJECT(&chip9->xive), PNV9_XIVE_IC_BASE(chip),
1039                             "ic-bar", &error_fatal);
1040     object_property_set_int(OBJECT(&chip9->xive), PNV9_XIVE_VC_BASE(chip),
1041                             "vc-bar", &error_fatal);
1042     object_property_set_int(OBJECT(&chip9->xive), PNV9_XIVE_PC_BASE(chip),
1043                             "pc-bar", &error_fatal);
1044     object_property_set_int(OBJECT(&chip9->xive), PNV9_XIVE_TM_BASE(chip),
1045                             "tm-bar", &error_fatal);
1046     object_property_set_bool(OBJECT(&chip9->xive), true, "realized",
1047                              &local_err);
1048     if (local_err) {
1049         error_propagate(errp, local_err);
1050         return;
1051     }
1052     pnv_xscom_add_subregion(chip, PNV9_XSCOM_XIVE_BASE,
1053                             &chip9->xive.xscom_regs);
1054 
1055     /* Processor Service Interface (PSI) Host Bridge */
1056     object_property_set_int(OBJECT(&chip9->psi), PNV9_PSIHB_BASE(chip),
1057                             "bar", &error_fatal);
1058     object_property_set_bool(OBJECT(&chip9->psi), true, "realized", &local_err);
1059     if (local_err) {
1060         error_propagate(errp, local_err);
1061         return;
1062     }
1063     pnv_xscom_add_subregion(chip, PNV9_XSCOM_PSIHB_BASE,
1064                             &PNV_PSI(psi9)->xscom_regs);
1065 
1066     /* LPC */
1067     object_property_set_bool(OBJECT(&chip9->lpc), true, "realized", &local_err);
1068     if (local_err) {
1069         error_propagate(errp, local_err);
1070         return;
1071     }
1072     memory_region_add_subregion(get_system_memory(), PNV9_LPCM_BASE(chip),
1073                                 &chip9->lpc.xscom_regs);
1074 
1075     chip->dt_isa_nodename = g_strdup_printf("/lpcm-opb@%" PRIx64 "/lpc@0",
1076                                             (uint64_t) PNV9_LPCM_BASE(chip));
1077 
1078     /* Create the simplified OCC model */
1079     object_property_set_bool(OBJECT(&chip9->occ), true, "realized", &local_err);
1080     if (local_err) {
1081         error_propagate(errp, local_err);
1082         return;
1083     }
1084     pnv_xscom_add_subregion(chip, PNV9_XSCOM_OCC_BASE, &chip9->occ.xscom_regs);
1085 }
1086 
1087 static void pnv_chip_power9_class_init(ObjectClass *klass, void *data)
1088 {
1089     DeviceClass *dc = DEVICE_CLASS(klass);
1090     PnvChipClass *k = PNV_CHIP_CLASS(klass);
1091 
1092     k->chip_type = PNV_CHIP_POWER9;
1093     k->chip_cfam_id = 0x220d104900008000ull; /* P9 Nimbus DD2.0 */
1094     k->cores_mask = POWER9_CORE_MASK;
1095     k->core_pir = pnv_chip_core_pir_p9;
1096     k->intc_create = pnv_chip_power9_intc_create;
1097     k->isa_create = pnv_chip_power9_isa_create;
1098     k->dt_populate = pnv_chip_power9_dt_populate;
1099     k->pic_print_info = pnv_chip_power9_pic_print_info;
1100     k->xscom_base = 0x00603fc00000000ull;
1101     dc->desc = "PowerNV Chip POWER9";
1102 
1103     device_class_set_parent_realize(dc, pnv_chip_power9_realize,
1104                                     &k->parent_realize);
1105 }
1106 
1107 static void pnv_chip_core_sanitize(PnvChip *chip, Error **errp)
1108 {
1109     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(chip);
1110     int cores_max;
1111 
1112     /*
1113      * No custom mask for this chip, let's use the default one from *
1114      * the chip class
1115      */
1116     if (!chip->cores_mask) {
1117         chip->cores_mask = pcc->cores_mask;
1118     }
1119 
1120     /* filter alien core ids ! some are reserved */
1121     if ((chip->cores_mask & pcc->cores_mask) != chip->cores_mask) {
1122         error_setg(errp, "warning: invalid core mask for chip Ox%"PRIx64" !",
1123                    chip->cores_mask);
1124         return;
1125     }
1126     chip->cores_mask &= pcc->cores_mask;
1127 
1128     /* now that we have a sane layout, let check the number of cores */
1129     cores_max = ctpop64(chip->cores_mask);
1130     if (chip->nr_cores > cores_max) {
1131         error_setg(errp, "warning: too many cores for chip ! Limit is %d",
1132                    cores_max);
1133         return;
1134     }
1135 }
1136 
1137 static void pnv_chip_instance_init(Object *obj)
1138 {
1139     PNV_CHIP(obj)->xscom_base = PNV_CHIP_GET_CLASS(obj)->xscom_base;
1140 }
1141 
1142 static void pnv_chip_core_realize(PnvChip *chip, Error **errp)
1143 {
1144     Error *error = NULL;
1145     PnvChipClass *pcc = PNV_CHIP_GET_CLASS(chip);
1146     const char *typename = pnv_chip_core_typename(chip);
1147     size_t typesize = object_type_get_instance_size(typename);
1148     int i, core_hwid;
1149 
1150     if (!object_class_by_name(typename)) {
1151         error_setg(errp, "Unable to find PowerNV CPU Core '%s'", typename);
1152         return;
1153     }
1154 
1155     /* Cores */
1156     pnv_chip_core_sanitize(chip, &error);
1157     if (error) {
1158         error_propagate(errp, error);
1159         return;
1160     }
1161 
1162     chip->cores = g_malloc0(typesize * chip->nr_cores);
1163 
1164     for (i = 0, core_hwid = 0; (core_hwid < sizeof(chip->cores_mask) * 8)
1165              && (i < chip->nr_cores); core_hwid++) {
1166         char core_name[32];
1167         void *pnv_core = chip->cores + i * typesize;
1168         uint64_t xscom_core_base;
1169 
1170         if (!(chip->cores_mask & (1ull << core_hwid))) {
1171             continue;
1172         }
1173 
1174         snprintf(core_name, sizeof(core_name), "core[%d]", core_hwid);
1175         object_initialize_child(OBJECT(chip), core_name, pnv_core, typesize,
1176                                 typename, &error_fatal, NULL);
1177         object_property_set_int(OBJECT(pnv_core), smp_threads, "nr-threads",
1178                                 &error_fatal);
1179         object_property_set_int(OBJECT(pnv_core), core_hwid,
1180                                 CPU_CORE_PROP_CORE_ID, &error_fatal);
1181         object_property_set_int(OBJECT(pnv_core),
1182                                 pcc->core_pir(chip, core_hwid),
1183                                 "pir", &error_fatal);
1184         object_property_add_const_link(OBJECT(pnv_core), "chip",
1185                                        OBJECT(chip), &error_fatal);
1186         object_property_set_bool(OBJECT(pnv_core), true, "realized",
1187                                  &error_fatal);
1188 
1189         /* Each core has an XSCOM MMIO region */
1190         if (!pnv_chip_is_power9(chip)) {
1191             xscom_core_base = PNV_XSCOM_EX_BASE(core_hwid);
1192         } else {
1193             xscom_core_base = PNV9_XSCOM_EC_BASE(core_hwid);
1194         }
1195 
1196         pnv_xscom_add_subregion(chip, xscom_core_base,
1197                                 &PNV_CORE(pnv_core)->xscom_regs);
1198         i++;
1199     }
1200 }
1201 
1202 static void pnv_chip_realize(DeviceState *dev, Error **errp)
1203 {
1204     PnvChip *chip = PNV_CHIP(dev);
1205     Error *error = NULL;
1206 
1207     /* XSCOM bridge */
1208     pnv_xscom_realize(chip, &error);
1209     if (error) {
1210         error_propagate(errp, error);
1211         return;
1212     }
1213     sysbus_mmio_map(SYS_BUS_DEVICE(chip), 0, PNV_XSCOM_BASE(chip));
1214 
1215     /* Cores */
1216     pnv_chip_core_realize(chip, &error);
1217     if (error) {
1218         error_propagate(errp, error);
1219         return;
1220     }
1221 }
1222 
1223 static Property pnv_chip_properties[] = {
1224     DEFINE_PROP_UINT32("chip-id", PnvChip, chip_id, 0),
1225     DEFINE_PROP_UINT64("ram-start", PnvChip, ram_start, 0),
1226     DEFINE_PROP_UINT64("ram-size", PnvChip, ram_size, 0),
1227     DEFINE_PROP_UINT32("nr-cores", PnvChip, nr_cores, 1),
1228     DEFINE_PROP_UINT64("cores-mask", PnvChip, cores_mask, 0x0),
1229     DEFINE_PROP_END_OF_LIST(),
1230 };
1231 
1232 static void pnv_chip_class_init(ObjectClass *klass, void *data)
1233 {
1234     DeviceClass *dc = DEVICE_CLASS(klass);
1235 
1236     set_bit(DEVICE_CATEGORY_CPU, dc->categories);
1237     dc->realize = pnv_chip_realize;
1238     dc->props = pnv_chip_properties;
1239     dc->desc = "PowerNV Chip";
1240 }
1241 
1242 static ICSState *pnv_ics_get(XICSFabric *xi, int irq)
1243 {
1244     PnvMachineState *pnv = PNV_MACHINE(xi);
1245     int i;
1246 
1247     for (i = 0; i < pnv->num_chips; i++) {
1248         Pnv8Chip *chip8 = PNV8_CHIP(pnv->chips[i]);
1249 
1250         if (ics_valid_irq(&chip8->psi.ics, irq)) {
1251             return &chip8->psi.ics;
1252         }
1253     }
1254     return NULL;
1255 }
1256 
1257 static void pnv_ics_resend(XICSFabric *xi)
1258 {
1259     PnvMachineState *pnv = PNV_MACHINE(xi);
1260     int i;
1261 
1262     for (i = 0; i < pnv->num_chips; i++) {
1263         Pnv8Chip *chip8 = PNV8_CHIP(pnv->chips[i]);
1264         ics_resend(&chip8->psi.ics);
1265     }
1266 }
1267 
1268 static ICPState *pnv_icp_get(XICSFabric *xi, int pir)
1269 {
1270     PowerPCCPU *cpu = ppc_get_vcpu_by_pir(pir);
1271 
1272     return cpu ? ICP(pnv_cpu_state(cpu)->intc) : NULL;
1273 }
1274 
1275 static void pnv_pic_print_info(InterruptStatsProvider *obj,
1276                                Monitor *mon)
1277 {
1278     PnvMachineState *pnv = PNV_MACHINE(obj);
1279     int i;
1280     CPUState *cs;
1281 
1282     CPU_FOREACH(cs) {
1283         PowerPCCPU *cpu = POWERPC_CPU(cs);
1284 
1285         if (pnv_chip_is_power9(pnv->chips[0])) {
1286             xive_tctx_pic_print_info(XIVE_TCTX(pnv_cpu_state(cpu)->intc), mon);
1287         } else {
1288             icp_pic_print_info(ICP(pnv_cpu_state(cpu)->intc), mon);
1289         }
1290     }
1291 
1292     for (i = 0; i < pnv->num_chips; i++) {
1293         PNV_CHIP_GET_CLASS(pnv->chips[i])->pic_print_info(pnv->chips[i], mon);
1294     }
1295 }
1296 
1297 static void pnv_get_num_chips(Object *obj, Visitor *v, const char *name,
1298                               void *opaque, Error **errp)
1299 {
1300     visit_type_uint32(v, name, &PNV_MACHINE(obj)->num_chips, errp);
1301 }
1302 
1303 static void pnv_set_num_chips(Object *obj, Visitor *v, const char *name,
1304                               void *opaque, Error **errp)
1305 {
1306     PnvMachineState *pnv = PNV_MACHINE(obj);
1307     uint32_t num_chips;
1308     Error *local_err = NULL;
1309 
1310     visit_type_uint32(v, name, &num_chips, &local_err);
1311     if (local_err) {
1312         error_propagate(errp, local_err);
1313         return;
1314     }
1315 
1316     /*
1317      * TODO: should we decide on how many chips we can create based
1318      * on #cores and Venice vs. Murano vs. Naples chip type etc...,
1319      */
1320     if (!is_power_of_2(num_chips) || num_chips > 4) {
1321         error_setg(errp, "invalid number of chips: '%d'", num_chips);
1322         return;
1323     }
1324 
1325     pnv->num_chips = num_chips;
1326 }
1327 
1328 static void pnv_machine_instance_init(Object *obj)
1329 {
1330     PnvMachineState *pnv = PNV_MACHINE(obj);
1331     pnv->num_chips = 1;
1332 }
1333 
1334 static void pnv_machine_class_props_init(ObjectClass *oc)
1335 {
1336     object_class_property_add(oc, "num-chips", "uint32",
1337                               pnv_get_num_chips, pnv_set_num_chips,
1338                               NULL, NULL, NULL);
1339     object_class_property_set_description(oc, "num-chips",
1340                               "Specifies the number of processor chips",
1341                               NULL);
1342 }
1343 
1344 static void pnv_machine_class_init(ObjectClass *oc, void *data)
1345 {
1346     MachineClass *mc = MACHINE_CLASS(oc);
1347     XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
1348     InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
1349 
1350     mc->desc = "IBM PowerNV (Non-Virtualized)";
1351     mc->init = pnv_init;
1352     mc->reset = pnv_reset;
1353     mc->max_cpus = MAX_CPUS;
1354     mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power8_v2.0");
1355     mc->block_default_type = IF_IDE; /* Pnv provides a AHCI device for
1356                                       * storage */
1357     mc->no_parallel = 1;
1358     mc->default_boot_order = NULL;
1359     mc->default_ram_size = 1 * GiB;
1360     xic->icp_get = pnv_icp_get;
1361     xic->ics_get = pnv_ics_get;
1362     xic->ics_resend = pnv_ics_resend;
1363     ispc->print_info = pnv_pic_print_info;
1364 
1365     pnv_machine_class_props_init(oc);
1366 }
1367 
1368 #define DEFINE_PNV8_CHIP_TYPE(type, class_initfn) \
1369     {                                             \
1370         .name          = type,                    \
1371         .class_init    = class_initfn,            \
1372         .parent        = TYPE_PNV8_CHIP,          \
1373     }
1374 
1375 #define DEFINE_PNV9_CHIP_TYPE(type, class_initfn) \
1376     {                                             \
1377         .name          = type,                    \
1378         .class_init    = class_initfn,            \
1379         .parent        = TYPE_PNV9_CHIP,          \
1380     }
1381 
1382 static const TypeInfo types[] = {
1383     {
1384         .name          = TYPE_PNV_MACHINE,
1385         .parent        = TYPE_MACHINE,
1386         .instance_size = sizeof(PnvMachineState),
1387         .instance_init = pnv_machine_instance_init,
1388         .class_init    = pnv_machine_class_init,
1389         .interfaces = (InterfaceInfo[]) {
1390             { TYPE_XICS_FABRIC },
1391             { TYPE_INTERRUPT_STATS_PROVIDER },
1392             { },
1393         },
1394     },
1395     {
1396         .name          = TYPE_PNV_CHIP,
1397         .parent        = TYPE_SYS_BUS_DEVICE,
1398         .class_init    = pnv_chip_class_init,
1399         .instance_init = pnv_chip_instance_init,
1400         .instance_size = sizeof(PnvChip),
1401         .class_size    = sizeof(PnvChipClass),
1402         .abstract      = true,
1403     },
1404 
1405     /*
1406      * P9 chip and variants
1407      */
1408     {
1409         .name          = TYPE_PNV9_CHIP,
1410         .parent        = TYPE_PNV_CHIP,
1411         .instance_init = pnv_chip_power9_instance_init,
1412         .instance_size = sizeof(Pnv9Chip),
1413     },
1414     DEFINE_PNV9_CHIP_TYPE(TYPE_PNV_CHIP_POWER9, pnv_chip_power9_class_init),
1415 
1416     /*
1417      * P8 chip and variants
1418      */
1419     {
1420         .name          = TYPE_PNV8_CHIP,
1421         .parent        = TYPE_PNV_CHIP,
1422         .instance_init = pnv_chip_power8_instance_init,
1423         .instance_size = sizeof(Pnv8Chip),
1424     },
1425     DEFINE_PNV8_CHIP_TYPE(TYPE_PNV_CHIP_POWER8, pnv_chip_power8_class_init),
1426     DEFINE_PNV8_CHIP_TYPE(TYPE_PNV_CHIP_POWER8E, pnv_chip_power8e_class_init),
1427     DEFINE_PNV8_CHIP_TYPE(TYPE_PNV_CHIP_POWER8NVL,
1428                           pnv_chip_power8nvl_class_init),
1429 };
1430 
1431 DEFINE_TYPES(types)
1432