]> Git Repo - qemu.git/blame - hw/ppc/spapr.c
ppc/pnv: Drop "num-chips" machine property
[qemu.git] / hw / ppc / spapr.c
CommitLineData
9fdf0c29
DG
1/*
2 * QEMU PowerPC pSeries Logical Partition (aka sPAPR) hardware System Emulator
3 *
4 * Copyright (c) 2004-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
6 * Copyright (c) 2010 David Gibson, IBM Corporation.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
9fdf0c29 25 */
a8d25326 26
0d75590d 27#include "qemu/osdep.h"
a8d25326 28#include "qemu-common.h"
da34e65c 29#include "qapi/error.h"
fa98fbfc 30#include "qapi/visitor.h"
9c17d615 31#include "sysemu/sysemu.h"
b58c5c2d 32#include "sysemu/hostmem.h"
e35704ba 33#include "sysemu/numa.h"
23ff81bd 34#include "sysemu/qtest.h"
71e8a915 35#include "sysemu/reset.h"
54d31236 36#include "sysemu/runstate.h"
03dd024f 37#include "qemu/log.h"
71461b0f 38#include "hw/fw-path-provider.h"
9fdf0c29 39#include "elf.h"
1422e32d 40#include "net/net.h"
ad440b4a 41#include "sysemu/device_tree.h"
9c17d615 42#include "sysemu/cpus.h"
b3946626 43#include "sysemu/hw_accel.h"
e97c3636 44#include "kvm_ppc.h"
c4b63b7c 45#include "migration/misc.h"
ca77ee28 46#include "migration/qemu-file-types.h"
84a899de 47#include "migration/global_state.h"
f2a8f0a6 48#include "migration/register.h"
4be21d56 49#include "mmu-hash64.h"
b4db5413 50#include "mmu-book3s-v3.h"
7abd43ba 51#include "cpu-models.h"
2e5b09fd 52#include "hw/core/cpu.h"
9fdf0c29
DG
53
54#include "hw/boards.h"
0d09e41a 55#include "hw/ppc/ppc.h"
9fdf0c29
DG
56#include "hw/loader.h"
57
7804c353 58#include "hw/ppc/fdt.h"
0d09e41a
PB
59#include "hw/ppc/spapr.h"
60#include "hw/ppc/spapr_vio.h"
a27bd6c7 61#include "hw/qdev-properties.h"
0d09e41a 62#include "hw/pci-host/spapr.h"
a2cb15b0 63#include "hw/pci/msi.h"
9fdf0c29 64
83c9f4ca 65#include "hw/pci/pci.h"
71461b0f
AK
66#include "hw/scsi/scsi.h"
67#include "hw/virtio/virtio-scsi.h"
c4e13492 68#include "hw/virtio/vhost-scsi-common.h"
f61b4bed 69
022c62cb 70#include "exec/address-spaces.h"
2309832a 71#include "exec/ram_addr.h"
35139a59 72#include "hw/usb.h"
1de7afc9 73#include "qemu/config-file.h"
135a129a 74#include "qemu/error-report.h"
2a6593cb 75#include "trace.h"
34316482 76#include "hw/nmi.h"
6449da45 77#include "hw/intc/intc.h"
890c2b77 78
94a94e4c 79#include "hw/ppc/spapr_cpu_core.h"
2cc0e2e8 80#include "hw/mem/memory-device.h"
0fb6bd07 81#include "hw/ppc/spapr_tpm_proxy.h"
68a27b20 82
f041d6af
GK
83#include "monitor/monitor.h"
84
9fdf0c29
DG
85#include <libfdt.h>
86
4d8d5467
BH
87/* SLOF memory layout:
88 *
89 * SLOF raw image loaded at 0, copies its romfs right below the flat
90 * device-tree, then position SLOF itself 31M below that
91 *
92 * So we set FW_OVERHEAD to 40MB which should account for all of that
93 * and more
94 *
95 * We load our kernel at 4M, leaving space for SLOF initial image
96 */
38b02bd8 97#define FDT_MAX_SIZE 0x100000
b7d1f77a 98#define RTAS_MAX_ADDR 0x80000000 /* RTAS must stay below that */
a9f8ad8f
DG
99#define FW_MAX_SIZE 0x400000
100#define FW_FILE_NAME "slof.bin"
4d8d5467
BH
101#define FW_OVERHEAD 0x2800000
102#define KERNEL_LOAD_ADDR FW_MAX_SIZE
a9f8ad8f 103
4d8d5467 104#define MIN_RMA_SLOF 128UL
9fdf0c29 105
5c7adcf4 106#define PHANDLE_INTC 0x00001111
0c103f8e 107
5d0fb150
GK
108/* These two functions implement the VCPU id numbering: one to compute them
109 * all and one to identify thread 0 of a VCORE. Any change to the first one
110 * is likely to have an impact on the second one, so let's keep them close.
111 */
ce2918cb 112static int spapr_vcpu_id(SpaprMachineState *spapr, int cpu_index)
5d0fb150 113{
fe6b6346
LX
114 MachineState *ms = MACHINE(spapr);
115 unsigned int smp_threads = ms->smp.threads;
116
1a5008fc 117 assert(spapr->vsmt);
5d0fb150
GK
118 return
119 (cpu_index / smp_threads) * spapr->vsmt + cpu_index % smp_threads;
120}
ce2918cb 121static bool spapr_is_thread0_in_vcore(SpaprMachineState *spapr,
5d0fb150
GK
122 PowerPCCPU *cpu)
123{
1a5008fc 124 assert(spapr->vsmt);
5d0fb150
GK
125 return spapr_get_vcpu_id(cpu) % spapr->vsmt == 0;
126}
127
46f7afa3
GK
128static bool pre_2_10_vmstate_dummy_icp_needed(void *opaque)
129{
130 /* Dummy entries correspond to unused ICPState objects in older QEMUs,
131 * and newer QEMUs don't even have them. In both cases, we don't want
132 * to send anything on the wire.
133 */
134 return false;
135}
136
137static const VMStateDescription pre_2_10_vmstate_dummy_icp = {
138 .name = "icp/server",
139 .version_id = 1,
140 .minimum_version_id = 1,
141 .needed = pre_2_10_vmstate_dummy_icp_needed,
142 .fields = (VMStateField[]) {
143 VMSTATE_UNUSED(4), /* uint32_t xirr */
144 VMSTATE_UNUSED(1), /* uint8_t pending_priority */
145 VMSTATE_UNUSED(1), /* uint8_t mfrr */
146 VMSTATE_END_OF_LIST()
147 },
148};
149
150static void pre_2_10_vmstate_register_dummy_icp(int i)
151{
152 vmstate_register(NULL, i, &pre_2_10_vmstate_dummy_icp,
153 (void *)(uintptr_t) i);
154}
155
156static void pre_2_10_vmstate_unregister_dummy_icp(int i)
157{
158 vmstate_unregister(NULL, &pre_2_10_vmstate_dummy_icp,
159 (void *)(uintptr_t) i);
160}
161
ce2918cb 162int spapr_max_server_number(SpaprMachineState *spapr)
46f7afa3 163{
fe6b6346
LX
164 MachineState *ms = MACHINE(spapr);
165
1a5008fc 166 assert(spapr->vsmt);
fe6b6346 167 return DIV_ROUND_UP(ms->smp.max_cpus * spapr->vsmt, ms->smp.threads);
46f7afa3
GK
168}
169
833d4668
AK
170static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
171 int smt_threads)
172{
173 int i, ret = 0;
174 uint32_t servers_prop[smt_threads];
175 uint32_t gservers_prop[smt_threads * 2];
14bb4486 176 int index = spapr_get_vcpu_id(cpu);
833d4668 177
d6e166c0
DG
178 if (cpu->compat_pvr) {
179 ret = fdt_setprop_cell(fdt, offset, "cpu-version", cpu->compat_pvr);
6d9412ea
AK
180 if (ret < 0) {
181 return ret;
182 }
183 }
184
833d4668
AK
185 /* Build interrupt servers and gservers properties */
186 for (i = 0; i < smt_threads; i++) {
187 servers_prop[i] = cpu_to_be32(index + i);
188 /* Hack, direct the group queues back to cpu 0 */
189 gservers_prop[i*2] = cpu_to_be32(index + i);
190 gservers_prop[i*2 + 1] = 0;
191 }
192 ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
193 servers_prop, sizeof(servers_prop));
194 if (ret < 0) {
195 return ret;
196 }
197 ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
198 gservers_prop, sizeof(gservers_prop));
199
200 return ret;
201}
202
99861ecb 203static int spapr_fixup_cpu_numa_dt(void *fdt, int offset, PowerPCCPU *cpu)
0da6f3fe 204{
14bb4486 205 int index = spapr_get_vcpu_id(cpu);
0da6f3fe
BR
206 uint32_t associativity[] = {cpu_to_be32(0x5),
207 cpu_to_be32(0x0),
208 cpu_to_be32(0x0),
209 cpu_to_be32(0x0),
15f8b142 210 cpu_to_be32(cpu->node_id),
0da6f3fe
BR
211 cpu_to_be32(index)};
212
213 /* Advertise NUMA via ibm,associativity */
99861ecb 214 return fdt_setprop(fdt, offset, "ibm,associativity", associativity,
0da6f3fe 215 sizeof(associativity));
0da6f3fe
BR
216}
217
86d5771a 218/* Populate the "ibm,pa-features" property */
ce2918cb 219static void spapr_populate_pa_features(SpaprMachineState *spapr,
ee76a09f 220 PowerPCCPU *cpu,
daa36379 221 void *fdt, int offset)
86d5771a
SB
222{
223 uint8_t pa_features_206[] = { 6, 0,
224 0xf6, 0x1f, 0xc7, 0x00, 0x80, 0xc0 };
225 uint8_t pa_features_207[] = { 24, 0,
226 0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0,
227 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
229 0x80, 0x00, 0x80, 0x00, 0x00, 0x00 };
9fb4541f
SB
230 uint8_t pa_features_300[] = { 66, 0,
231 /* 0: MMU|FPU|SLB|RUN|DABR|NX, 1: fri[nzpm]|DABRX|SPRG3|SLB0|PP110 */
232 /* 2: VPM|DS205|PPR|DS202|DS206, 3: LSD|URG, SSO, 5: LE|CFAR|EB|LSQ */
233 0xf6, 0x1f, 0xc7, 0xc0, 0x80, 0xf0, /* 0 - 5 */
234 /* 6: DS207 */
235 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, /* 6 - 11 */
236 /* 16: Vector */
86d5771a 237 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, /* 12 - 17 */
9fb4541f 238 /* 18: Vec. Scalar, 20: Vec. XOR, 22: HTM */
9bf502fe 239 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 18 - 23 */
9fb4541f
SB
240 /* 24: Ext. Dec, 26: 64 bit ftrs, 28: PM ftrs */
241 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 24 - 29 */
242 /* 30: MMR, 32: LE atomic, 34: EBB + ext EBB */
243 0x80, 0x00, 0x80, 0x00, 0xC0, 0x00, /* 30 - 35 */
244 /* 36: SPR SO, 38: Copy/Paste, 40: Radix MMU */
245 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 36 - 41 */
246 /* 42: PM, 44: PC RA, 46: SC vec'd */
247 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 42 - 47 */
248 /* 48: SIMD, 50: QP BFP, 52: String */
249 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 48 - 53 */
250 /* 54: DecFP, 56: DecI, 58: SHA */
251 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, /* 54 - 59 */
252 /* 60: NM atomic, 62: RNG */
253 0x80, 0x00, 0x80, 0x00, 0x00, 0x00, /* 60 - 65 */
254 };
7abd43ba 255 uint8_t *pa_features = NULL;
86d5771a
SB
256 size_t pa_size;
257
7abd43ba 258 if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_2_06, 0, cpu->compat_pvr)) {
86d5771a
SB
259 pa_features = pa_features_206;
260 pa_size = sizeof(pa_features_206);
7abd43ba
SJS
261 }
262 if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_2_07, 0, cpu->compat_pvr)) {
86d5771a
SB
263 pa_features = pa_features_207;
264 pa_size = sizeof(pa_features_207);
7abd43ba
SJS
265 }
266 if (ppc_check_compat(cpu, CPU_POWERPC_LOGICAL_3_00, 0, cpu->compat_pvr)) {
86d5771a
SB
267 pa_features = pa_features_300;
268 pa_size = sizeof(pa_features_300);
7abd43ba
SJS
269 }
270 if (!pa_features) {
86d5771a
SB
271 return;
272 }
273
26cd35b8 274 if (ppc_hash64_has(cpu, PPC_HASH64_CI_LARGEPAGE)) {
86d5771a
SB
275 /*
276 * Note: we keep CI large pages off by default because a 64K capable
277 * guest provisioned with large pages might otherwise try to map a qemu
278 * framebuffer (or other kind of memory mapped PCI BAR) using 64K pages
279 * even if that qemu runs on a 4k host.
280 * We dd this bit back here if we are confident this is not an issue
281 */
282 pa_features[3] |= 0x20;
283 }
4e5fe368 284 if ((spapr_get_cap(spapr, SPAPR_CAP_HTM) != 0) && pa_size > 24) {
86d5771a
SB
285 pa_features[24] |= 0x80; /* Transactional memory support */
286 }
daa36379 287 if (spapr->cas_pre_isa3_guest && pa_size > 40) {
e957f6a9
SB
288 /* Workaround for broken kernels that attempt (guest) radix
289 * mode when they can't handle it, if they see the radix bit set
290 * in pa-features. So hide it from them. */
291 pa_features[40 + 2] &= ~0x80; /* Radix MMU */
292 }
86d5771a
SB
293
294 _FDT((fdt_setprop(fdt, offset, "ibm,pa-features", pa_features, pa_size)));
295}
296
c86c1aff 297static hwaddr spapr_node0_size(MachineState *machine)
b082d65a 298{
aa570207 299 if (machine->numa_state->num_nodes) {
b082d65a 300 int i;
aa570207 301 for (i = 0; i < machine->numa_state->num_nodes; ++i) {
7e721e7b
TX
302 if (machine->numa_state->nodes[i].node_mem) {
303 return MIN(pow2floor(machine->numa_state->nodes[i].node_mem),
fb164994 304 machine->ram_size);
b082d65a
AK
305 }
306 }
307 }
fb164994 308 return machine->ram_size;
b082d65a
AK
309}
310
a1d59c0f
AK
311static void add_str(GString *s, const gchar *s1)
312{
313 g_string_append_len(s, s1, strlen(s1) + 1);
314}
7f763a5d 315
03d196b7 316static int spapr_populate_memory_node(void *fdt, int nodeid, hwaddr start,
26a8c353
AK
317 hwaddr size)
318{
319 uint32_t associativity[] = {
320 cpu_to_be32(0x4), /* length */
321 cpu_to_be32(0x0), cpu_to_be32(0x0),
c3b4f589 322 cpu_to_be32(0x0), cpu_to_be32(nodeid)
26a8c353
AK
323 };
324 char mem_name[32];
325 uint64_t mem_reg_property[2];
326 int off;
327
328 mem_reg_property[0] = cpu_to_be64(start);
329 mem_reg_property[1] = cpu_to_be64(size);
330
3a17e38f 331 sprintf(mem_name, "memory@%" HWADDR_PRIx, start);
26a8c353
AK
332 off = fdt_add_subnode(fdt, 0, mem_name);
333 _FDT(off);
334 _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
335 _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
336 sizeof(mem_reg_property))));
337 _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
338 sizeof(associativity))));
03d196b7 339 return off;
26a8c353
AK
340}
341
ce2918cb 342static int spapr_populate_memory(SpaprMachineState *spapr, void *fdt)
7f763a5d 343{
fb164994 344 MachineState *machine = MACHINE(spapr);
7db8a127 345 hwaddr mem_start, node_size;
aa570207 346 int i, nb_nodes = machine->numa_state->num_nodes;
7e721e7b 347 NodeInfo *nodes = machine->numa_state->nodes;
7f763a5d 348
7db8a127
AK
349 for (i = 0, mem_start = 0; i < nb_nodes; ++i) {
350 if (!nodes[i].node_mem) {
351 continue;
352 }
fb164994 353 if (mem_start >= machine->ram_size) {
5fe269b1
PM
354 node_size = 0;
355 } else {
7db8a127 356 node_size = nodes[i].node_mem;
fb164994
DG
357 if (node_size > machine->ram_size - mem_start) {
358 node_size = machine->ram_size - mem_start;
5fe269b1
PM
359 }
360 }
7db8a127 361 if (!mem_start) {
b472b1a7
DHB
362 /* spapr_machine_init() checks for rma_size <= node0_size
363 * already */
e8f986fc 364 spapr_populate_memory_node(fdt, i, 0, spapr->rma_size);
7db8a127
AK
365 mem_start += spapr->rma_size;
366 node_size -= spapr->rma_size;
367 }
6010818c
AK
368 for ( ; node_size; ) {
369 hwaddr sizetmp = pow2floor(node_size);
370
371 /* mem_start != 0 here */
372 if (ctzl(mem_start) < ctzl(sizetmp)) {
373 sizetmp = 1ULL << ctzl(mem_start);
374 }
375
376 spapr_populate_memory_node(fdt, i, mem_start, sizetmp);
377 node_size -= sizetmp;
378 mem_start += sizetmp;
379 }
7f763a5d
DG
380 }
381
382 return 0;
383}
384
0da6f3fe 385static void spapr_populate_cpu_dt(CPUState *cs, void *fdt, int offset,
ce2918cb 386 SpaprMachineState *spapr)
0da6f3fe 387{
fe6b6346 388 MachineState *ms = MACHINE(spapr);
0da6f3fe
BR
389 PowerPCCPU *cpu = POWERPC_CPU(cs);
390 CPUPPCState *env = &cpu->env;
391 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
14bb4486 392 int index = spapr_get_vcpu_id(cpu);
0da6f3fe
BR
393 uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
394 0xffffffff, 0xffffffff};
afd10a0f
BR
395 uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq()
396 : SPAPR_TIMEBASE_FREQ;
0da6f3fe
BR
397 uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
398 uint32_t page_sizes_prop[64];
399 size_t page_sizes_prop_size;
fe6b6346
LX
400 unsigned int smp_threads = ms->smp.threads;
401 uint32_t vcpus_per_socket = smp_threads * ms->smp.cores;
0da6f3fe 402 uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
abbc1247 403 int compat_smt = MIN(smp_threads, ppc_compat_max_vthreads(cpu));
ce2918cb 404 SpaprDrc *drc;
af81cf32 405 int drc_index;
c64abd1f
SB
406 uint32_t radix_AP_encodings[PPC_PAGE_SIZES_MAX_SZ];
407 int i;
af81cf32 408
fbf55397 409 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU, index);
af81cf32 410 if (drc) {
0b55aa91 411 drc_index = spapr_drc_index(drc);
af81cf32
BR
412 _FDT((fdt_setprop_cell(fdt, offset, "ibm,my-drc-index", drc_index)));
413 }
0da6f3fe
BR
414
415 _FDT((fdt_setprop_cell(fdt, offset, "reg", index)));
416 _FDT((fdt_setprop_string(fdt, offset, "device_type", "cpu")));
417
418 _FDT((fdt_setprop_cell(fdt, offset, "cpu-version", env->spr[SPR_PVR])));
419 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-block-size",
420 env->dcache_line_size)));
421 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-line-size",
422 env->dcache_line_size)));
423 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-block-size",
424 env->icache_line_size)));
425 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-line-size",
426 env->icache_line_size)));
427
428 if (pcc->l1_dcache_size) {
429 _FDT((fdt_setprop_cell(fdt, offset, "d-cache-size",
430 pcc->l1_dcache_size)));
431 } else {
3dc6f869 432 warn_report("Unknown L1 dcache size for cpu");
0da6f3fe
BR
433 }
434 if (pcc->l1_icache_size) {
435 _FDT((fdt_setprop_cell(fdt, offset, "i-cache-size",
436 pcc->l1_icache_size)));
437 } else {
3dc6f869 438 warn_report("Unknown L1 icache size for cpu");
0da6f3fe
BR
439 }
440
441 _FDT((fdt_setprop_cell(fdt, offset, "timebase-frequency", tbfreq)));
442 _FDT((fdt_setprop_cell(fdt, offset, "clock-frequency", cpufreq)));
67d7d66f
DG
443 _FDT((fdt_setprop_cell(fdt, offset, "slb-size", cpu->hash64_opts->slb_size)));
444 _FDT((fdt_setprop_cell(fdt, offset, "ibm,slb-size", cpu->hash64_opts->slb_size)));
0da6f3fe
BR
445 _FDT((fdt_setprop_string(fdt, offset, "status", "okay")));
446 _FDT((fdt_setprop(fdt, offset, "64-bit", NULL, 0)));
447
448 if (env->spr_cb[SPR_PURR].oea_read) {
83f192d3
SJS
449 _FDT((fdt_setprop_cell(fdt, offset, "ibm,purr", 1)));
450 }
451 if (env->spr_cb[SPR_SPURR].oea_read) {
452 _FDT((fdt_setprop_cell(fdt, offset, "ibm,spurr", 1)));
0da6f3fe
BR
453 }
454
58969eee 455 if (ppc_hash64_has(cpu, PPC_HASH64_1TSEG)) {
0da6f3fe
BR
456 _FDT((fdt_setprop(fdt, offset, "ibm,processor-segment-sizes",
457 segs, sizeof(segs))));
458 }
459
29386642 460 /* Advertise VSX (vector extensions) if available
0da6f3fe 461 * 1 == VMX / Altivec available
29386642
DG
462 * 2 == VSX available
463 *
464 * Only CPUs for which we create core types in spapr_cpu_core.c
465 * are possible, and all of those have VMX */
4e5fe368 466 if (spapr_get_cap(spapr, SPAPR_CAP_VSX) != 0) {
29386642
DG
467 _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", 2)));
468 } else {
469 _FDT((fdt_setprop_cell(fdt, offset, "ibm,vmx", 1)));
0da6f3fe
BR
470 }
471
472 /* Advertise DFP (Decimal Floating Point) if available
473 * 0 / no property == no DFP
474 * 1 == DFP available */
4e5fe368 475 if (spapr_get_cap(spapr, SPAPR_CAP_DFP) != 0) {
0da6f3fe
BR
476 _FDT((fdt_setprop_cell(fdt, offset, "ibm,dfp", 1)));
477 }
478
644a2c99
DG
479 page_sizes_prop_size = ppc_create_page_sizes_prop(cpu, page_sizes_prop,
480 sizeof(page_sizes_prop));
0da6f3fe
BR
481 if (page_sizes_prop_size) {
482 _FDT((fdt_setprop(fdt, offset, "ibm,segment-page-sizes",
483 page_sizes_prop, page_sizes_prop_size)));
484 }
485
daa36379 486 spapr_populate_pa_features(spapr, cpu, fdt, offset);
90da0d5a 487
0da6f3fe 488 _FDT((fdt_setprop_cell(fdt, offset, "ibm,chip-id",
22419c2a 489 cs->cpu_index / vcpus_per_socket)));
0da6f3fe
BR
490
491 _FDT((fdt_setprop(fdt, offset, "ibm,pft-size",
492 pft_size_prop, sizeof(pft_size_prop))));
493
aa570207 494 if (ms->numa_state->num_nodes > 1) {
99861ecb
IM
495 _FDT(spapr_fixup_cpu_numa_dt(fdt, offset, cpu));
496 }
0da6f3fe 497
12dbeb16 498 _FDT(spapr_fixup_cpu_smt_dt(fdt, offset, cpu, compat_smt));
c64abd1f
SB
499
500 if (pcc->radix_page_info) {
501 for (i = 0; i < pcc->radix_page_info->count; i++) {
502 radix_AP_encodings[i] =
503 cpu_to_be32(pcc->radix_page_info->entries[i]);
504 }
505 _FDT((fdt_setprop(fdt, offset, "ibm,processor-radix-AP-encodings",
506 radix_AP_encodings,
507 pcc->radix_page_info->count *
508 sizeof(radix_AP_encodings[0]))));
509 }
a8dafa52
SJS
510
511 /*
512 * We set this property to let the guest know that it can use the large
513 * decrementer and its width in bits.
514 */
515 if (spapr_get_cap(spapr, SPAPR_CAP_LARGE_DECREMENTER) != SPAPR_CAP_OFF)
516 _FDT((fdt_setprop_u32(fdt, offset, "ibm,dec-bits",
517 pcc->lrg_decr_bits)));
0da6f3fe
BR
518}
519
ce2918cb 520static void spapr_populate_cpus_dt_node(void *fdt, SpaprMachineState *spapr)
0da6f3fe 521{
04d595b3 522 CPUState **rev;
0da6f3fe 523 CPUState *cs;
04d595b3 524 int n_cpus;
0da6f3fe
BR
525 int cpus_offset;
526 char *nodename;
04d595b3 527 int i;
0da6f3fe
BR
528
529 cpus_offset = fdt_add_subnode(fdt, 0, "cpus");
530 _FDT(cpus_offset);
531 _FDT((fdt_setprop_cell(fdt, cpus_offset, "#address-cells", 0x1)));
532 _FDT((fdt_setprop_cell(fdt, cpus_offset, "#size-cells", 0x0)));
533
534 /*
535 * We walk the CPUs in reverse order to ensure that CPU DT nodes
536 * created by fdt_add_subnode() end up in the right order in FDT
537 * for the guest kernel the enumerate the CPUs correctly.
04d595b3
EC
538 *
539 * The CPU list cannot be traversed in reverse order, so we need
540 * to do extra work.
0da6f3fe 541 */
04d595b3
EC
542 n_cpus = 0;
543 rev = NULL;
544 CPU_FOREACH(cs) {
545 rev = g_renew(CPUState *, rev, n_cpus + 1);
546 rev[n_cpus++] = cs;
547 }
548
549 for (i = n_cpus - 1; i >= 0; i--) {
550 CPUState *cs = rev[i];
0da6f3fe 551 PowerPCCPU *cpu = POWERPC_CPU(cs);
14bb4486 552 int index = spapr_get_vcpu_id(cpu);
0da6f3fe
BR
553 DeviceClass *dc = DEVICE_GET_CLASS(cs);
554 int offset;
555
5d0fb150 556 if (!spapr_is_thread0_in_vcore(spapr, cpu)) {
0da6f3fe
BR
557 continue;
558 }
559
560 nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
561 offset = fdt_add_subnode(fdt, cpus_offset, nodename);
562 g_free(nodename);
563 _FDT(offset);
564 spapr_populate_cpu_dt(cs, fdt, offset, spapr);
565 }
566
eceba347 567 g_free(rev);
0da6f3fe
BR
568}
569
0e947a89
TH
570static int spapr_rng_populate_dt(void *fdt)
571{
572 int node;
573 int ret;
574
575 node = qemu_fdt_add_subnode(fdt, "/ibm,platform-facilities");
576 if (node <= 0) {
577 return -1;
578 }
579 ret = fdt_setprop_string(fdt, node, "device_type",
580 "ibm,platform-facilities");
581 ret |= fdt_setprop_cell(fdt, node, "#address-cells", 0x1);
582 ret |= fdt_setprop_cell(fdt, node, "#size-cells", 0x0);
583
584 node = fdt_add_subnode(fdt, node, "ibm,random-v1");
585 if (node <= 0) {
586 return -1;
587 }
588 ret |= fdt_setprop_string(fdt, node, "compatible", "ibm,random");
589
590 return ret ? -1 : 0;
591}
592
f47bd1c8
IM
593static uint32_t spapr_pc_dimm_node(MemoryDeviceInfoList *list, ram_addr_t addr)
594{
595 MemoryDeviceInfoList *info;
596
597 for (info = list; info; info = info->next) {
598 MemoryDeviceInfo *value = info->value;
599
600 if (value && value->type == MEMORY_DEVICE_INFO_KIND_DIMM) {
601 PCDIMMDeviceInfo *pcdimm_info = value->u.dimm.data;
602
ccc2cef8 603 if (addr >= pcdimm_info->addr &&
f47bd1c8
IM
604 addr < (pcdimm_info->addr + pcdimm_info->size)) {
605 return pcdimm_info->node;
606 }
607 }
608 }
609
610 return -1;
611}
612
a324d6f1
BR
613struct sPAPRDrconfCellV2 {
614 uint32_t seq_lmbs;
615 uint64_t base_addr;
616 uint32_t drc_index;
617 uint32_t aa_index;
618 uint32_t flags;
619} QEMU_PACKED;
620
621typedef struct DrconfCellQueue {
622 struct sPAPRDrconfCellV2 cell;
623 QSIMPLEQ_ENTRY(DrconfCellQueue) entry;
624} DrconfCellQueue;
625
626static DrconfCellQueue *
627spapr_get_drconf_cell(uint32_t seq_lmbs, uint64_t base_addr,
628 uint32_t drc_index, uint32_t aa_index,
629 uint32_t flags)
03d196b7 630{
a324d6f1
BR
631 DrconfCellQueue *elem;
632
633 elem = g_malloc0(sizeof(*elem));
634 elem->cell.seq_lmbs = cpu_to_be32(seq_lmbs);
635 elem->cell.base_addr = cpu_to_be64(base_addr);
636 elem->cell.drc_index = cpu_to_be32(drc_index);
637 elem->cell.aa_index = cpu_to_be32(aa_index);
638 elem->cell.flags = cpu_to_be32(flags);
639
640 return elem;
641}
642
643/* ibm,dynamic-memory-v2 */
ce2918cb 644static int spapr_populate_drmem_v2(SpaprMachineState *spapr, void *fdt,
a324d6f1
BR
645 int offset, MemoryDeviceInfoList *dimms)
646{
b0c14ec4 647 MachineState *machine = MACHINE(spapr);
cc941111 648 uint8_t *int_buf, *cur_index;
a324d6f1
BR
649 int ret;
650 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
651 uint64_t addr, cur_addr, size;
b0c14ec4
DH
652 uint32_t nr_boot_lmbs = (machine->device_memory->base / lmb_size);
653 uint64_t mem_end = machine->device_memory->base +
654 memory_region_size(&machine->device_memory->mr);
cc941111 655 uint32_t node, buf_len, nr_entries = 0;
ce2918cb 656 SpaprDrc *drc;
a324d6f1
BR
657 DrconfCellQueue *elem, *next;
658 MemoryDeviceInfoList *info;
659 QSIMPLEQ_HEAD(, DrconfCellQueue) drconf_queue
660 = QSIMPLEQ_HEAD_INITIALIZER(drconf_queue);
661
662 /* Entry to cover RAM and the gap area */
663 elem = spapr_get_drconf_cell(nr_boot_lmbs, 0, 0, -1,
664 SPAPR_LMB_FLAGS_RESERVED |
665 SPAPR_LMB_FLAGS_DRC_INVALID);
666 QSIMPLEQ_INSERT_TAIL(&drconf_queue, elem, entry);
667 nr_entries++;
668
b0c14ec4 669 cur_addr = machine->device_memory->base;
a324d6f1
BR
670 for (info = dimms; info; info = info->next) {
671 PCDIMMDeviceInfo *di = info->value->u.dimm.data;
672
673 addr = di->addr;
674 size = di->size;
675 node = di->node;
676
677 /* Entry for hot-pluggable area */
678 if (cur_addr < addr) {
679 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, cur_addr / lmb_size);
680 g_assert(drc);
681 elem = spapr_get_drconf_cell((addr - cur_addr) / lmb_size,
682 cur_addr, spapr_drc_index(drc), -1, 0);
683 QSIMPLEQ_INSERT_TAIL(&drconf_queue, elem, entry);
684 nr_entries++;
685 }
686
687 /* Entry for DIMM */
688 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, addr / lmb_size);
689 g_assert(drc);
690 elem = spapr_get_drconf_cell(size / lmb_size, addr,
691 spapr_drc_index(drc), node,
692 SPAPR_LMB_FLAGS_ASSIGNED);
693 QSIMPLEQ_INSERT_TAIL(&drconf_queue, elem, entry);
694 nr_entries++;
695 cur_addr = addr + size;
696 }
697
698 /* Entry for remaining hotpluggable area */
699 if (cur_addr < mem_end) {
700 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, cur_addr / lmb_size);
701 g_assert(drc);
702 elem = spapr_get_drconf_cell((mem_end - cur_addr) / lmb_size,
703 cur_addr, spapr_drc_index(drc), -1, 0);
704 QSIMPLEQ_INSERT_TAIL(&drconf_queue, elem, entry);
705 nr_entries++;
706 }
707
708 buf_len = nr_entries * sizeof(struct sPAPRDrconfCellV2) + sizeof(uint32_t);
709 int_buf = cur_index = g_malloc0(buf_len);
710 *(uint32_t *)int_buf = cpu_to_be32(nr_entries);
711 cur_index += sizeof(nr_entries);
712
713 QSIMPLEQ_FOREACH_SAFE(elem, &drconf_queue, entry, next) {
714 memcpy(cur_index, &elem->cell, sizeof(elem->cell));
715 cur_index += sizeof(elem->cell);
716 QSIMPLEQ_REMOVE(&drconf_queue, elem, DrconfCellQueue, entry);
717 g_free(elem);
718 }
719
720 ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory-v2", int_buf, buf_len);
721 g_free(int_buf);
722 if (ret < 0) {
723 return -1;
724 }
725 return 0;
726}
727
728/* ibm,dynamic-memory */
ce2918cb 729static int spapr_populate_drmem_v1(SpaprMachineState *spapr, void *fdt,
a324d6f1
BR
730 int offset, MemoryDeviceInfoList *dimms)
731{
b0c14ec4 732 MachineState *machine = MACHINE(spapr);
a324d6f1 733 int i, ret;
03d196b7 734 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
0c9269a5 735 uint32_t device_lmb_start = machine->device_memory->base / lmb_size;
b0c14ec4
DH
736 uint32_t nr_lmbs = (machine->device_memory->base +
737 memory_region_size(&machine->device_memory->mr)) /
d0e5a8f2 738 lmb_size;
03d196b7 739 uint32_t *int_buf, *cur_index, buf_len;
16c25aef 740
ef001f06
TH
741 /*
742 * Allocate enough buffer size to fit in ibm,dynamic-memory
ef001f06 743 */
a324d6f1 744 buf_len = (nr_lmbs * SPAPR_DR_LMB_LIST_ENTRY_SIZE + 1) * sizeof(uint32_t);
03d196b7 745 cur_index = int_buf = g_malloc0(buf_len);
03d196b7
BR
746 int_buf[0] = cpu_to_be32(nr_lmbs);
747 cur_index++;
748 for (i = 0; i < nr_lmbs; i++) {
d0e5a8f2 749 uint64_t addr = i * lmb_size;
03d196b7
BR
750 uint32_t *dynamic_memory = cur_index;
751
0c9269a5 752 if (i >= device_lmb_start) {
ce2918cb 753 SpaprDrc *drc;
d0e5a8f2 754
fbf55397 755 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB, i);
d0e5a8f2 756 g_assert(drc);
d0e5a8f2
BR
757
758 dynamic_memory[0] = cpu_to_be32(addr >> 32);
759 dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
0b55aa91 760 dynamic_memory[2] = cpu_to_be32(spapr_drc_index(drc));
d0e5a8f2 761 dynamic_memory[3] = cpu_to_be32(0); /* reserved */
f47bd1c8 762 dynamic_memory[4] = cpu_to_be32(spapr_pc_dimm_node(dimms, addr));
d0e5a8f2
BR
763 if (memory_region_present(get_system_memory(), addr)) {
764 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_ASSIGNED);
765 } else {
766 dynamic_memory[5] = cpu_to_be32(0);
767 }
03d196b7 768 } else {
d0e5a8f2
BR
769 /*
770 * LMB information for RMA, boot time RAM and gap b/n RAM and
0c9269a5 771 * device memory region -- all these are marked as reserved
d0e5a8f2
BR
772 * and as having no valid DRC.
773 */
774 dynamic_memory[0] = cpu_to_be32(addr >> 32);
775 dynamic_memory[1] = cpu_to_be32(addr & 0xffffffff);
776 dynamic_memory[2] = cpu_to_be32(0);
777 dynamic_memory[3] = cpu_to_be32(0); /* reserved */
778 dynamic_memory[4] = cpu_to_be32(-1);
779 dynamic_memory[5] = cpu_to_be32(SPAPR_LMB_FLAGS_RESERVED |
780 SPAPR_LMB_FLAGS_DRC_INVALID);
03d196b7
BR
781 }
782
783 cur_index += SPAPR_DR_LMB_LIST_ENTRY_SIZE;
784 }
785 ret = fdt_setprop(fdt, offset, "ibm,dynamic-memory", int_buf, buf_len);
a324d6f1 786 g_free(int_buf);
03d196b7 787 if (ret < 0) {
a324d6f1
BR
788 return -1;
789 }
790 return 0;
791}
792
793/*
794 * Adds ibm,dynamic-reconfiguration-memory node.
795 * Refer to docs/specs/ppc-spapr-hotplug.txt for the documentation
796 * of this device tree node.
797 */
ce2918cb 798static int spapr_populate_drconf_memory(SpaprMachineState *spapr, void *fdt)
a324d6f1
BR
799{
800 MachineState *machine = MACHINE(spapr);
aa570207 801 int nb_numa_nodes = machine->numa_state->num_nodes;
a324d6f1
BR
802 int ret, i, offset;
803 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
804 uint32_t prop_lmb_size[] = {0, cpu_to_be32(lmb_size)};
805 uint32_t *int_buf, *cur_index, buf_len;
806 int nr_nodes = nb_numa_nodes ? nb_numa_nodes : 1;
807 MemoryDeviceInfoList *dimms = NULL;
808
809 /*
0c9269a5 810 * Don't create the node if there is no device memory
a324d6f1
BR
811 */
812 if (machine->ram_size == machine->maxram_size) {
813 return 0;
814 }
815
816 offset = fdt_add_subnode(fdt, 0, "ibm,dynamic-reconfiguration-memory");
817
818 ret = fdt_setprop(fdt, offset, "ibm,lmb-size", prop_lmb_size,
819 sizeof(prop_lmb_size));
820 if (ret < 0) {
821 return ret;
822 }
823
824 ret = fdt_setprop_cell(fdt, offset, "ibm,memory-flags-mask", 0xff);
825 if (ret < 0) {
826 return ret;
827 }
828
829 ret = fdt_setprop_cell(fdt, offset, "ibm,memory-preservation-time", 0x0);
830 if (ret < 0) {
831 return ret;
832 }
833
834 /* ibm,dynamic-memory or ibm,dynamic-memory-v2 */
2cc0e2e8 835 dimms = qmp_memory_device_list();
a324d6f1
BR
836 if (spapr_ovec_test(spapr->ov5_cas, OV5_DRMEM_V2)) {
837 ret = spapr_populate_drmem_v2(spapr, fdt, offset, dimms);
838 } else {
839 ret = spapr_populate_drmem_v1(spapr, fdt, offset, dimms);
840 }
841 qapi_free_MemoryDeviceInfoList(dimms);
842
843 if (ret < 0) {
844 return ret;
03d196b7
BR
845 }
846
847 /* ibm,associativity-lookup-arrays */
a324d6f1
BR
848 buf_len = (nr_nodes * 4 + 2) * sizeof(uint32_t);
849 cur_index = int_buf = g_malloc0(buf_len);
6663864e 850 int_buf[0] = cpu_to_be32(nr_nodes);
03d196b7
BR
851 int_buf[1] = cpu_to_be32(4); /* Number of entries per associativity list */
852 cur_index += 2;
6663864e 853 for (i = 0; i < nr_nodes; i++) {
03d196b7
BR
854 uint32_t associativity[] = {
855 cpu_to_be32(0x0),
856 cpu_to_be32(0x0),
857 cpu_to_be32(0x0),
858 cpu_to_be32(i)
859 };
860 memcpy(cur_index, associativity, sizeof(associativity));
861 cur_index += 4;
862 }
863 ret = fdt_setprop(fdt, offset, "ibm,associativity-lookup-arrays", int_buf,
864 (cur_index - int_buf) * sizeof(uint32_t));
03d196b7 865 g_free(int_buf);
a324d6f1 866
03d196b7
BR
867 return ret;
868}
869
ce2918cb
DG
870static int spapr_dt_cas_updates(SpaprMachineState *spapr, void *fdt,
871 SpaprOptionVector *ov5_updates)
6787d27b 872{
ce2918cb 873 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
417ece33 874 int ret = 0, offset;
6787d27b
MR
875
876 /* Generate ibm,dynamic-reconfiguration-memory node if required */
877 if (spapr_ovec_test(ov5_updates, OV5_DRCONF_MEMORY)) {
878 g_assert(smc->dr_lmb_enabled);
879 ret = spapr_populate_drconf_memory(spapr, fdt);
417ece33 880 if (ret) {
9b6c1da5 881 return ret;
417ece33 882 }
6787d27b
MR
883 }
884
417ece33
MR
885 offset = fdt_path_offset(fdt, "/chosen");
886 if (offset < 0) {
887 offset = fdt_add_subnode(fdt, 0, "chosen");
888 if (offset < 0) {
889 return offset;
890 }
891 }
9b6c1da5
DHB
892 return spapr_ovec_populate_dt(fdt, offset, spapr->ov5_cas,
893 "ibm,architecture-vec-5");
6787d27b
MR
894}
895
ce2918cb 896static void spapr_dt_rtas(SpaprMachineState *spapr, void *fdt)
3f5dabce 897{
fe6b6346 898 MachineState *ms = MACHINE(spapr);
3f5dabce
DG
899 int rtas;
900 GString *hypertas = g_string_sized_new(256);
901 GString *qemu_hypertas = g_string_sized_new(256);
902 uint32_t refpoints[] = { cpu_to_be32(0x4), cpu_to_be32(0x4) };
0c9269a5 903 uint64_t max_device_addr = MACHINE(spapr)->device_memory->base +
b0c14ec4 904 memory_region_size(&MACHINE(spapr)->device_memory->mr);
3f5dabce 905 uint32_t lrdr_capacity[] = {
0c9269a5
DH
906 cpu_to_be32(max_device_addr >> 32),
907 cpu_to_be32(max_device_addr & 0xffffffff),
3f5dabce 908 0, cpu_to_be32(SPAPR_MEMORY_BLOCK_SIZE),
fe6b6346 909 cpu_to_be32(ms->smp.max_cpus / ms->smp.threads),
3f5dabce 910 };
ec132efa 911 uint32_t maxdomain = cpu_to_be32(spapr->gpu_numa_id > 1 ? 1 : 0);
da9f80fb
SP
912 uint32_t maxdomains[] = {
913 cpu_to_be32(4),
ec132efa
AK
914 maxdomain,
915 maxdomain,
916 maxdomain,
917 cpu_to_be32(spapr->gpu_numa_id),
da9f80fb 918 };
3f5dabce
DG
919
920 _FDT(rtas = fdt_add_subnode(fdt, 0, "rtas"));
921
922 /* hypertas */
923 add_str(hypertas, "hcall-pft");
924 add_str(hypertas, "hcall-term");
925 add_str(hypertas, "hcall-dabr");
926 add_str(hypertas, "hcall-interrupt");
927 add_str(hypertas, "hcall-tce");
928 add_str(hypertas, "hcall-vio");
929 add_str(hypertas, "hcall-splpar");
10741314 930 add_str(hypertas, "hcall-join");
3f5dabce
DG
931 add_str(hypertas, "hcall-bulk");
932 add_str(hypertas, "hcall-set-mode");
933 add_str(hypertas, "hcall-sprg0");
934 add_str(hypertas, "hcall-copy");
935 add_str(hypertas, "hcall-debug");
c24ba3d0 936 add_str(hypertas, "hcall-vphn");
3f5dabce
DG
937 add_str(qemu_hypertas, "hcall-memop1");
938
939 if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
940 add_str(hypertas, "hcall-multi-tce");
941 }
30f4b05b
DG
942
943 if (spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) {
944 add_str(hypertas, "hcall-hpt-resize");
945 }
946
3f5dabce
DG
947 _FDT(fdt_setprop(fdt, rtas, "ibm,hypertas-functions",
948 hypertas->str, hypertas->len));
949 g_string_free(hypertas, TRUE);
950 _FDT(fdt_setprop(fdt, rtas, "qemu,hypertas-functions",
951 qemu_hypertas->str, qemu_hypertas->len));
952 g_string_free(qemu_hypertas, TRUE);
953
954 _FDT(fdt_setprop(fdt, rtas, "ibm,associativity-reference-points",
955 refpoints, sizeof(refpoints)));
956
da9f80fb
SP
957 _FDT(fdt_setprop(fdt, rtas, "ibm,max-associativity-domains",
958 maxdomains, sizeof(maxdomains)));
959
3f5dabce
DG
960 _FDT(fdt_setprop_cell(fdt, rtas, "rtas-error-log-max",
961 RTAS_ERROR_LOG_MAX));
962 _FDT(fdt_setprop_cell(fdt, rtas, "rtas-event-scan-rate",
963 RTAS_EVENT_SCAN_RATE));
964
4f441474
DG
965 g_assert(msi_nonbroken);
966 _FDT(fdt_setprop(fdt, rtas, "ibm,change-msix-capable", NULL, 0));
3f5dabce
DG
967
968 /*
969 * According to PAPR, rtas ibm,os-term does not guarantee a return
970 * back to the guest cpu.
971 *
972 * While an additional ibm,extended-os-term property indicates
973 * that rtas call return will always occur. Set this property.
974 */
975 _FDT(fdt_setprop(fdt, rtas, "ibm,extended-os-term", NULL, 0));
976
977 _FDT(fdt_setprop(fdt, rtas, "ibm,lrdr-capacity",
978 lrdr_capacity, sizeof(lrdr_capacity)));
979
980 spapr_dt_rtas_tokens(fdt, rtas);
981}
982
db592b5b
CLG
983/*
984 * Prepare ibm,arch-vec-5-platform-support, which indicates the MMU
985 * and the XIVE features that the guest may request and thus the valid
986 * values for bytes 23..26 of option vector 5:
987 */
ce2918cb 988static void spapr_dt_ov5_platform_support(SpaprMachineState *spapr, void *fdt,
db592b5b 989 int chosen)
9fb4541f 990{
545d6e2b
SJS
991 PowerPCCPU *first_ppc_cpu = POWERPC_CPU(first_cpu);
992
f2b14e3a 993 char val[2 * 4] = {
ca62823b 994 23, 0x00, /* XICS / XIVE mode */
9fb4541f
SB
995 24, 0x00, /* Hash/Radix, filled in below. */
996 25, 0x00, /* Hash options: Segment Tables == no, GTSE == no. */
997 26, 0x40, /* Radix options: GTSE == yes. */
998 };
999
ca62823b
DG
1000 if (spapr->irq->xics && spapr->irq->xive) {
1001 val[1] = SPAPR_OV5_XIVE_BOTH;
1002 } else if (spapr->irq->xive) {
1003 val[1] = SPAPR_OV5_XIVE_EXPLOIT;
1004 } else {
1005 assert(spapr->irq->xics);
1006 val[1] = SPAPR_OV5_XIVE_LEGACY;
1007 }
1008
7abd43ba
SJS
1009 if (!ppc_check_compat(first_ppc_cpu, CPU_POWERPC_LOGICAL_3_00, 0,
1010 first_ppc_cpu->compat_pvr)) {
db592b5b
CLG
1011 /*
1012 * If we're in a pre POWER9 compat mode then the guest should
1013 * do hash and use the legacy interrupt mode
1014 */
ca62823b 1015 val[1] = SPAPR_OV5_XIVE_LEGACY; /* XICS */
7abd43ba
SJS
1016 val[3] = 0x00; /* Hash */
1017 } else if (kvm_enabled()) {
9fb4541f 1018 if (kvmppc_has_cap_mmu_radix() && kvmppc_has_cap_mmu_hash_v3()) {
f2b14e3a 1019 val[3] = 0x80; /* OV5_MMU_BOTH */
9fb4541f 1020 } else if (kvmppc_has_cap_mmu_radix()) {
f2b14e3a 1021 val[3] = 0x40; /* OV5_MMU_RADIX_300 */
9fb4541f 1022 } else {
f2b14e3a 1023 val[3] = 0x00; /* Hash */
9fb4541f
SB
1024 }
1025 } else {
7abd43ba
SJS
1026 /* V3 MMU supports both hash and radix in tcg (with dynamic switching) */
1027 val[3] = 0xC0;
9fb4541f
SB
1028 }
1029 _FDT(fdt_setprop(fdt, chosen, "ibm,arch-vec-5-platform-support",
1030 val, sizeof(val)));
1031}
1032
ce2918cb 1033static void spapr_dt_chosen(SpaprMachineState *spapr, void *fdt)
7c866c6a
DG
1034{
1035 MachineState *machine = MACHINE(spapr);
6c3829a2 1036 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
7c866c6a
DG
1037 int chosen;
1038 const char *boot_device = machine->boot_order;
1039 char *stdout_path = spapr_vio_stdout_path(spapr->vio_bus);
1040 size_t cb = 0;
907aac2f 1041 char *bootlist = get_boot_devices_list(&cb);
7c866c6a
DG
1042
1043 _FDT(chosen = fdt_add_subnode(fdt, 0, "chosen"));
1044
5ced7895
AK
1045 if (machine->kernel_cmdline && machine->kernel_cmdline[0]) {
1046 _FDT(fdt_setprop_string(fdt, chosen, "bootargs",
1047 machine->kernel_cmdline));
1048 }
1049 if (spapr->initrd_size) {
1050 _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-start",
1051 spapr->initrd_base));
1052 _FDT(fdt_setprop_cell(fdt, chosen, "linux,initrd-end",
1053 spapr->initrd_base + spapr->initrd_size));
1054 }
7c866c6a
DG
1055
1056 if (spapr->kernel_size) {
1057 uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
1058 cpu_to_be64(spapr->kernel_size) };
1059
1060 _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel",
1061 &kprop, sizeof(kprop)));
1062 if (spapr->kernel_le) {
1063 _FDT(fdt_setprop(fdt, chosen, "qemu,boot-kernel-le", NULL, 0));
1064 }
1065 }
1066 if (boot_menu) {
1067 _FDT((fdt_setprop_cell(fdt, chosen, "qemu,boot-menu", boot_menu)));
1068 }
1069 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-width", graphic_width));
1070 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-height", graphic_height));
1071 _FDT(fdt_setprop_cell(fdt, chosen, "qemu,graphic-depth", graphic_depth));
1072
1073 if (cb && bootlist) {
1074 int i;
1075
1076 for (i = 0; i < cb; i++) {
1077 if (bootlist[i] == '\n') {
1078 bootlist[i] = ' ';
1079 }
1080 }
1081 _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-list", bootlist));
1082 }
1083
1084 if (boot_device && strlen(boot_device)) {
1085 _FDT(fdt_setprop_string(fdt, chosen, "qemu,boot-device", boot_device));
1086 }
1087
1088 if (!spapr->has_graphics && stdout_path) {
90ee4e01
ND
1089 /*
1090 * "linux,stdout-path" and "stdout" properties are deprecated by linux
1091 * kernel. New platforms should only use the "stdout-path" property. Set
1092 * the new property and continue using older property to remain
1093 * compatible with the existing firmware.
1094 */
7c866c6a 1095 _FDT(fdt_setprop_string(fdt, chosen, "linux,stdout-path", stdout_path));
90ee4e01 1096 _FDT(fdt_setprop_string(fdt, chosen, "stdout-path", stdout_path));
7c866c6a
DG
1097 }
1098
6c3829a2
AK
1099 /* We can deal with BAR reallocation just fine, advertise it to the guest */
1100 if (smc->linux_pci_probe) {
1101 _FDT(fdt_setprop_cell(fdt, chosen, "linux,pci-probe-only", 0));
1102 }
1103
db592b5b 1104 spapr_dt_ov5_platform_support(spapr, fdt, chosen);
9fb4541f 1105
7c866c6a
DG
1106 g_free(stdout_path);
1107 g_free(bootlist);
1108}
1109
ce2918cb 1110static void spapr_dt_hypervisor(SpaprMachineState *spapr, void *fdt)
fca5f2dc
DG
1111{
1112 /* The /hypervisor node isn't in PAPR - this is a hack to allow PR
1113 * KVM to work under pHyp with some guest co-operation */
1114 int hypervisor;
1115 uint8_t hypercall[16];
1116
1117 _FDT(hypervisor = fdt_add_subnode(fdt, 0, "hypervisor"));
1118 /* indicate KVM hypercall interface */
1119 _FDT(fdt_setprop_string(fdt, hypervisor, "compatible", "linux,kvm"));
1120 if (kvmppc_has_cap_fixup_hcalls()) {
1121 /*
1122 * Older KVM versions with older guest kernels were broken
1123 * with the magic page, don't allow the guest to map it.
1124 */
1125 if (!kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
1126 sizeof(hypercall))) {
1127 _FDT(fdt_setprop(fdt, hypervisor, "hcall-instructions",
1128 hypercall, sizeof(hypercall)));
1129 }
1130 }
1131}
1132
0c21e073 1133void *spapr_build_fdt(SpaprMachineState *spapr, bool reset, size_t space)
a3467baa 1134{
c86c1aff 1135 MachineState *machine = MACHINE(spapr);
3c0c47e3 1136 MachineClass *mc = MACHINE_GET_CLASS(machine);
ce2918cb 1137 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
7c866c6a 1138 int ret;
a3467baa 1139 void *fdt;
ce2918cb 1140 SpaprPhbState *phb;
398a0bd5 1141 char *buf;
a3467baa 1142
97b32a6a
DG
1143 fdt = g_malloc0(space);
1144 _FDT((fdt_create_empty_tree(fdt, space)));
a3467baa 1145
398a0bd5
DG
1146 /* Root node */
1147 _FDT(fdt_setprop_string(fdt, 0, "device_type", "chrp"));
1148 _FDT(fdt_setprop_string(fdt, 0, "model", "IBM pSeries (emulated by qemu)"));
1149 _FDT(fdt_setprop_string(fdt, 0, "compatible", "qemu,pseries"));
1150
0a794529 1151 /* Guest UUID & Name*/
398a0bd5 1152 buf = qemu_uuid_unparse_strdup(&qemu_uuid);
398a0bd5
DG
1153 _FDT(fdt_setprop_string(fdt, 0, "vm,uuid", buf));
1154 if (qemu_uuid_set) {
1155 _FDT(fdt_setprop_string(fdt, 0, "system-id", buf));
1156 }
1157 g_free(buf);
1158
1159 if (qemu_get_vm_name()) {
1160 _FDT(fdt_setprop_string(fdt, 0, "ibm,partition-name",
1161 qemu_get_vm_name()));
1162 }
1163
0a794529
DG
1164 /* Host Model & Serial Number */
1165 if (spapr->host_model) {
1166 _FDT(fdt_setprop_string(fdt, 0, "host-model", spapr->host_model));
1167 } else if (smc->broken_host_serial_model && kvmppc_get_host_model(&buf)) {
1168 _FDT(fdt_setprop_string(fdt, 0, "host-model", buf));
1169 g_free(buf);
1170 }
1171
1172 if (spapr->host_serial) {
1173 _FDT(fdt_setprop_string(fdt, 0, "host-serial", spapr->host_serial));
1174 } else if (smc->broken_host_serial_model && kvmppc_get_host_serial(&buf)) {
1175 _FDT(fdt_setprop_string(fdt, 0, "host-serial", buf));
1176 g_free(buf);
1177 }
1178
398a0bd5
DG
1179 _FDT(fdt_setprop_cell(fdt, 0, "#address-cells", 2));
1180 _FDT(fdt_setprop_cell(fdt, 0, "#size-cells", 2));
4040ab72 1181
fc7e0765 1182 /* /interrupt controller */
05289273 1183 spapr_irq_dt(spapr, spapr_max_server_number(spapr), fdt, PHANDLE_INTC);
fc7e0765 1184
e8f986fc
BR
1185 ret = spapr_populate_memory(spapr, fdt);
1186 if (ret < 0) {
ce9863b7 1187 error_report("couldn't setup memory nodes in fdt");
e8f986fc 1188 exit(1);
7f763a5d
DG
1189 }
1190
bf5a6696
DG
1191 /* /vdevice */
1192 spapr_dt_vdevice(spapr->vio_bus, fdt);
4040ab72 1193
4d9392be
TH
1194 if (object_resolve_path_type("", TYPE_SPAPR_RNG, NULL)) {
1195 ret = spapr_rng_populate_dt(fdt);
1196 if (ret < 0) {
ce9863b7 1197 error_report("could not set up rng device in the fdt");
4d9392be
TH
1198 exit(1);
1199 }
1200 }
1201
3384f95c 1202 QLIST_FOREACH(phb, &spapr->phbs, list) {
8cbe71ec 1203 ret = spapr_dt_phb(spapr, phb, PHANDLE_INTC, fdt, NULL);
da34fed7
TH
1204 if (ret < 0) {
1205 error_report("couldn't setup PCI devices in fdt");
1206 exit(1);
1207 }
3384f95c
DG
1208 }
1209
0da6f3fe
BR
1210 /* cpus */
1211 spapr_populate_cpus_dt_node(fdt, spapr);
6e806cc3 1212
c20d332a 1213 if (smc->dr_lmb_enabled) {
9e7d38e8 1214 _FDT(spapr_dt_drc(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_LMB));
c20d332a
BR
1215 }
1216
c5514d0e 1217 if (mc->has_hotpluggable_cpus) {
af81cf32 1218 int offset = fdt_path_offset(fdt, "/cpus");
9e7d38e8 1219 ret = spapr_dt_drc(fdt, offset, NULL, SPAPR_DR_CONNECTOR_TYPE_CPU);
af81cf32
BR
1220 if (ret < 0) {
1221 error_report("Couldn't set up CPU DR device tree properties");
1222 exit(1);
1223 }
1224 }
1225
ffb1e275 1226 /* /event-sources */
ffbb1705 1227 spapr_dt_events(spapr, fdt);
ffb1e275 1228
3f5dabce
DG
1229 /* /rtas */
1230 spapr_dt_rtas(spapr, fdt);
1231
7c866c6a 1232 /* /chosen */
a49f62b9
AK
1233 if (reset) {
1234 spapr_dt_chosen(spapr, fdt);
1235 }
cf6e5223 1236
fca5f2dc
DG
1237 /* /hypervisor */
1238 if (kvm_enabled()) {
1239 spapr_dt_hypervisor(spapr, fdt);
1240 }
1241
cf6e5223 1242 /* Build memory reserve map */
a49f62b9
AK
1243 if (reset) {
1244 if (spapr->kernel_size) {
1245 _FDT((fdt_add_mem_rsv(fdt, KERNEL_LOAD_ADDR, spapr->kernel_size)));
1246 }
1247 if (spapr->initrd_size) {
1248 _FDT((fdt_add_mem_rsv(fdt, spapr->initrd_base,
1249 spapr->initrd_size)));
1250 }
cf6e5223
DG
1251 }
1252
6787d27b
MR
1253 /* ibm,client-architecture-support updates */
1254 ret = spapr_dt_cas_updates(spapr, fdt, spapr->ov5_cas);
1255 if (ret < 0) {
1256 error_report("couldn't setup CAS properties fdt");
1257 exit(1);
1258 }
1259
3998ccd0 1260 if (smc->dr_phb_enabled) {
9e7d38e8 1261 ret = spapr_dt_drc(fdt, 0, NULL, SPAPR_DR_CONNECTOR_TYPE_PHB);
3998ccd0
NF
1262 if (ret < 0) {
1263 error_report("Couldn't set up PHB DR device tree properties");
1264 exit(1);
1265 }
1266 }
1267
997b6cfc 1268 return fdt;
9fdf0c29
DG
1269}
1270
1271static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
1272{
1273 return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
1274}
1275
1d1be34d
DG
1276static void emulate_spapr_hypercall(PPCVirtualHypervisor *vhyp,
1277 PowerPCCPU *cpu)
9fdf0c29 1278{
1b14670a
AF
1279 CPUPPCState *env = &cpu->env;
1280
8d04fb55
JK
1281 /* The TCG path should also be holding the BQL at this point */
1282 g_assert(qemu_mutex_iothread_locked());
1283
efcb9383
DG
1284 if (msr_pr) {
1285 hcall_dprintf("Hypercall made with MSR[PR]=1\n");
1286 env->gpr[3] = H_PRIVILEGE;
1287 } else {
aa100fa4 1288 env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
efcb9383 1289 }
9fdf0c29
DG
1290}
1291
00fd075e
BH
1292struct LPCRSyncState {
1293 target_ulong value;
1294 target_ulong mask;
1295};
1296
1297static void do_lpcr_sync(CPUState *cs, run_on_cpu_data arg)
1298{
1299 struct LPCRSyncState *s = arg.host_ptr;
1300 PowerPCCPU *cpu = POWERPC_CPU(cs);
1301 CPUPPCState *env = &cpu->env;
1302 target_ulong lpcr;
1303
1304 cpu_synchronize_state(cs);
1305 lpcr = env->spr[SPR_LPCR];
1306 lpcr &= ~s->mask;
1307 lpcr |= s->value;
1308 ppc_store_lpcr(cpu, lpcr);
1309}
1310
1311void spapr_set_all_lpcrs(target_ulong value, target_ulong mask)
1312{
1313 CPUState *cs;
1314 struct LPCRSyncState s = {
1315 .value = value,
1316 .mask = mask
1317 };
1318 CPU_FOREACH(cs) {
1319 run_on_cpu(cs, do_lpcr_sync, RUN_ON_CPU_HOST_PTR(&s));
1320 }
1321}
1322
79825f4d 1323static void spapr_get_pate(PPCVirtualHypervisor *vhyp, ppc_v3_pate_t *entry)
9861bb3e 1324{
ce2918cb 1325 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
9861bb3e 1326
79825f4d
BH
1327 /* Copy PATE1:GR into PATE0:HR */
1328 entry->dw0 = spapr->patb_entry & PATE0_HR;
1329 entry->dw1 = spapr->patb_entry;
9861bb3e
SJS
1330}
1331
e6b8fd24
SMJ
1332#define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2))
1333#define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
1334#define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
1335#define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
1336#define DIRTY_HPTE(_hpte) ((*(uint64_t *)(_hpte)) |= tswap64(HPTE64_V_HPTE_DIRTY))
1337
715c5407
DG
1338/*
1339 * Get the fd to access the kernel htab, re-opening it if necessary
1340 */
ce2918cb 1341static int get_htab_fd(SpaprMachineState *spapr)
715c5407 1342{
14b0d748
GK
1343 Error *local_err = NULL;
1344
715c5407
DG
1345 if (spapr->htab_fd >= 0) {
1346 return spapr->htab_fd;
1347 }
1348
14b0d748 1349 spapr->htab_fd = kvmppc_get_htab_fd(false, 0, &local_err);
715c5407 1350 if (spapr->htab_fd < 0) {
14b0d748 1351 error_report_err(local_err);
715c5407
DG
1352 }
1353
1354 return spapr->htab_fd;
1355}
1356
ce2918cb 1357void close_htab_fd(SpaprMachineState *spapr)
715c5407
DG
1358{
1359 if (spapr->htab_fd >= 0) {
1360 close(spapr->htab_fd);
1361 }
1362 spapr->htab_fd = -1;
1363}
1364
e57ca75c
DG
1365static hwaddr spapr_hpt_mask(PPCVirtualHypervisor *vhyp)
1366{
ce2918cb 1367 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
e57ca75c
DG
1368
1369 return HTAB_SIZE(spapr) / HASH_PTEG_SIZE_64 - 1;
1370}
1371
1ec26c75
GK
1372static target_ulong spapr_encode_hpt_for_kvm_pr(PPCVirtualHypervisor *vhyp)
1373{
ce2918cb 1374 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1ec26c75
GK
1375
1376 assert(kvm_enabled());
1377
1378 if (!spapr->htab) {
1379 return 0;
1380 }
1381
1382 return (target_ulong)(uintptr_t)spapr->htab | (spapr->htab_shift - 18);
1383}
1384
e57ca75c
DG
1385static const ppc_hash_pte64_t *spapr_map_hptes(PPCVirtualHypervisor *vhyp,
1386 hwaddr ptex, int n)
1387{
ce2918cb 1388 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
e57ca75c
DG
1389 hwaddr pte_offset = ptex * HASH_PTE_SIZE_64;
1390
1391 if (!spapr->htab) {
1392 /*
1393 * HTAB is controlled by KVM. Fetch into temporary buffer
1394 */
1395 ppc_hash_pte64_t *hptes = g_malloc(n * HASH_PTE_SIZE_64);
1396 kvmppc_read_hptes(hptes, ptex, n);
1397 return hptes;
1398 }
1399
1400 /*
1401 * HTAB is controlled by QEMU. Just point to the internally
1402 * accessible PTEG.
1403 */
1404 return (const ppc_hash_pte64_t *)(spapr->htab + pte_offset);
1405}
1406
1407static void spapr_unmap_hptes(PPCVirtualHypervisor *vhyp,
1408 const ppc_hash_pte64_t *hptes,
1409 hwaddr ptex, int n)
1410{
ce2918cb 1411 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
e57ca75c
DG
1412
1413 if (!spapr->htab) {
1414 g_free((void *)hptes);
1415 }
1416
1417 /* Nothing to do for qemu managed HPT */
1418}
1419
a2dd4e83
BH
1420void spapr_store_hpte(PowerPCCPU *cpu, hwaddr ptex,
1421 uint64_t pte0, uint64_t pte1)
e57ca75c 1422{
a2dd4e83 1423 SpaprMachineState *spapr = SPAPR_MACHINE(cpu->vhyp);
e57ca75c
DG
1424 hwaddr offset = ptex * HASH_PTE_SIZE_64;
1425
1426 if (!spapr->htab) {
1427 kvmppc_write_hpte(ptex, pte0, pte1);
1428 } else {
3054b0ca
BH
1429 if (pte0 & HPTE64_V_VALID) {
1430 stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1431 /*
1432 * When setting valid, we write PTE1 first. This ensures
1433 * proper synchronization with the reading code in
1434 * ppc_hash64_pteg_search()
1435 */
1436 smp_wmb();
1437 stq_p(spapr->htab + offset, pte0);
1438 } else {
1439 stq_p(spapr->htab + offset, pte0);
1440 /*
1441 * When clearing it we set PTE0 first. This ensures proper
1442 * synchronization with the reading code in
1443 * ppc_hash64_pteg_search()
1444 */
1445 smp_wmb();
1446 stq_p(spapr->htab + offset + HASH_PTE_SIZE_64 / 2, pte1);
1447 }
e57ca75c
DG
1448 }
1449}
1450
a2dd4e83
BH
1451static void spapr_hpte_set_c(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1452 uint64_t pte1)
1453{
1454 hwaddr offset = ptex * HASH_PTE_SIZE_64 + 15;
1455 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1456
1457 if (!spapr->htab) {
1458 /* There should always be a hash table when this is called */
1459 error_report("spapr_hpte_set_c called with no hash table !");
1460 return;
1461 }
1462
1463 /* The HW performs a non-atomic byte update */
1464 stb_p(spapr->htab + offset, (pte1 & 0xff) | 0x80);
1465}
1466
1467static void spapr_hpte_set_r(PPCVirtualHypervisor *vhyp, hwaddr ptex,
1468 uint64_t pte1)
1469{
1470 hwaddr offset = ptex * HASH_PTE_SIZE_64 + 14;
1471 SpaprMachineState *spapr = SPAPR_MACHINE(vhyp);
1472
1473 if (!spapr->htab) {
1474 /* There should always be a hash table when this is called */
1475 error_report("spapr_hpte_set_r called with no hash table !");
1476 return;
1477 }
1478
1479 /* The HW performs a non-atomic byte update */
1480 stb_p(spapr->htab + offset, ((pte1 >> 8) & 0xff) | 0x01);
1481}
1482
0b0b8310 1483int spapr_hpt_shift_for_ramsize(uint64_t ramsize)
8dfe8e7f
DG
1484{
1485 int shift;
1486
1487 /* We aim for a hash table of size 1/128 the size of RAM (rounded
1488 * up). The PAPR recommendation is actually 1/64 of RAM size, but
1489 * that's much more than is needed for Linux guests */
1490 shift = ctz64(pow2ceil(ramsize)) - 7;
1491 shift = MAX(shift, 18); /* Minimum architected size */
1492 shift = MIN(shift, 46); /* Maximum architected size */
1493 return shift;
1494}
1495
ce2918cb 1496void spapr_free_hpt(SpaprMachineState *spapr)
06ec79e8
BR
1497{
1498 g_free(spapr->htab);
1499 spapr->htab = NULL;
1500 spapr->htab_shift = 0;
1501 close_htab_fd(spapr);
1502}
1503
ce2918cb 1504void spapr_reallocate_hpt(SpaprMachineState *spapr, int shift,
2772cf6b 1505 Error **errp)
7f763a5d 1506{
c5f54f3e
DG
1507 long rc;
1508
1509 /* Clean up any HPT info from a previous boot */
06ec79e8 1510 spapr_free_hpt(spapr);
c5f54f3e
DG
1511
1512 rc = kvmppc_reset_htab(shift);
1513 if (rc < 0) {
1514 /* kernel-side HPT needed, but couldn't allocate one */
1515 error_setg_errno(errp, errno,
1516 "Failed to allocate KVM HPT of order %d (try smaller maxmem?)",
1517 shift);
1518 /* This is almost certainly fatal, but if the caller really
1519 * wants to carry on with shift == 0, it's welcome to try */
1520 } else if (rc > 0) {
1521 /* kernel-side HPT allocated */
1522 if (rc != shift) {
1523 error_setg(errp,
1524 "Requested order %d HPT, but kernel allocated order %ld (try smaller maxmem?)",
1525 shift, rc);
7735feda
BR
1526 }
1527
7f763a5d 1528 spapr->htab_shift = shift;
c18ad9a5 1529 spapr->htab = NULL;
b817772a 1530 } else {
c5f54f3e
DG
1531 /* kernel-side HPT not needed, allocate in userspace instead */
1532 size_t size = 1ULL << shift;
1533 int i;
b817772a 1534
c5f54f3e
DG
1535 spapr->htab = qemu_memalign(size, size);
1536 if (!spapr->htab) {
1537 error_setg_errno(errp, errno,
1538 "Could not allocate HPT of order %d", shift);
1539 return;
7735feda
BR
1540 }
1541
c5f54f3e
DG
1542 memset(spapr->htab, 0, size);
1543 spapr->htab_shift = shift;
e6b8fd24 1544
c5f54f3e
DG
1545 for (i = 0; i < size / HASH_PTE_SIZE_64; i++) {
1546 DIRTY_HPTE(HPTE(spapr->htab, i));
e6b8fd24 1547 }
7f763a5d 1548 }
ee4d9ecc 1549 /* We're setting up a hash table, so that means we're not radix */
176dccee 1550 spapr->patb_entry = 0;
00fd075e 1551 spapr_set_all_lpcrs(0, LPCR_HR | LPCR_UPRT);
9fdf0c29
DG
1552}
1553
ce2918cb 1554void spapr_setup_hpt_and_vrma(SpaprMachineState *spapr)
b4db5413 1555{
2772cf6b
DG
1556 int hpt_shift;
1557
1558 if ((spapr->resize_hpt == SPAPR_RESIZE_HPT_DISABLED)
1559 || (spapr->cas_reboot
1560 && !spapr_ovec_test(spapr->ov5_cas, OV5_HPT_RESIZE))) {
1561 hpt_shift = spapr_hpt_shift_for_ramsize(MACHINE(spapr)->maxram_size);
1562 } else {
768a20f3
DG
1563 uint64_t current_ram_size;
1564
1565 current_ram_size = MACHINE(spapr)->ram_size + get_plugged_memory_size();
1566 hpt_shift = spapr_hpt_shift_for_ramsize(current_ram_size);
2772cf6b
DG
1567 }
1568 spapr_reallocate_hpt(spapr, hpt_shift, &error_fatal);
1569
b4db5413 1570 if (spapr->vrma_adjust) {
c86c1aff 1571 spapr->rma_size = kvmppc_rma_size(spapr_node0_size(MACHINE(spapr)),
b4db5413
SJS
1572 spapr->htab_shift);
1573 }
b4db5413
SJS
1574}
1575
82512483
GK
1576static int spapr_reset_drcs(Object *child, void *opaque)
1577{
ce2918cb
DG
1578 SpaprDrc *drc =
1579 (SpaprDrc *) object_dynamic_cast(child,
82512483
GK
1580 TYPE_SPAPR_DR_CONNECTOR);
1581
1582 if (drc) {
1583 spapr_drc_reset(drc);
1584 }
1585
1586 return 0;
1587}
1588
a0628599 1589static void spapr_machine_reset(MachineState *machine)
a3467baa 1590{
ce2918cb 1591 SpaprMachineState *spapr = SPAPR_MACHINE(machine);
182735ef 1592 PowerPCCPU *first_ppc_cpu;
744a928c 1593 hwaddr fdt_addr;
997b6cfc
DG
1594 void *fdt;
1595 int rc;
259186a7 1596
905db916 1597 kvmppc_svm_off(&error_fatal);
9f6edd06 1598 spapr_caps_apply(spapr);
33face6b 1599
1481fe5f
LV
1600 first_ppc_cpu = POWERPC_CPU(first_cpu);
1601 if (kvm_enabled() && kvmppc_has_cap_mmu_radix() &&
ad99d04c
DG
1602 ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00, 0,
1603 spapr->max_compat_pvr)) {
79825f4d
BH
1604 /*
1605 * If using KVM with radix mode available, VCPUs can be started
b4db5413 1606 * without a HPT because KVM will start them in radix mode.
79825f4d
BH
1607 * Set the GR bit in PATE so that we know there is no HPT.
1608 */
1609 spapr->patb_entry = PATE1_GR;
00fd075e 1610 spapr_set_all_lpcrs(LPCR_HR | LPCR_UPRT, LPCR_HR | LPCR_UPRT);
b4db5413 1611 } else {
b4db5413 1612 spapr_setup_hpt_and_vrma(spapr);
c5f54f3e 1613 }
a3467baa 1614
25c9780d
DG
1615 qemu_devices_reset();
1616
79825f4d
BH
1617 /*
1618 * If this reset wasn't generated by CAS, we should reset our
1619 * negotiated options and start from scratch
1620 */
9012a53f
GK
1621 if (!spapr->cas_reboot) {
1622 spapr_ovec_cleanup(spapr->ov5_cas);
1623 spapr->ov5_cas = spapr_ovec_new();
1624
ce03a193 1625 ppc_set_compat_all(spapr->max_compat_pvr, &error_fatal);
9012a53f
GK
1626 }
1627
b2e22477
CLG
1628 /*
1629 * This is fixing some of the default configuration of the XIVE
1630 * devices. To be called after the reset of the machine devices.
1631 */
1632 spapr_irq_reset(spapr, &error_fatal);
1633
23ff81bd
GK
1634 /*
1635 * There is no CAS under qtest. Simulate one to please the code that
1636 * depends on spapr->ov5_cas. This is especially needed to test device
1637 * unplug, so we do that before resetting the DRCs.
1638 */
1639 if (qtest_enabled()) {
1640 spapr_ovec_cleanup(spapr->ov5_cas);
1641 spapr->ov5_cas = spapr_ovec_clone(spapr->ov5);
1642 }
1643
82512483
GK
1644 /* DRC reset may cause a device to be unplugged. This will cause troubles
1645 * if this device is used by another device (eg, a running vhost backend
1646 * will crash QEMU if the DIMM holding the vring goes away). To avoid such
1647 * situations, we reset DRCs after all devices have been reset.
1648 */
1649 object_child_foreach_recursive(object_get_root(), spapr_reset_drcs, NULL);
1650
56258174 1651 spapr_clear_pending_events(spapr);
a3467baa 1652
b7d1f77a
BH
1653 /*
1654 * We place the device tree and RTAS just below either the top of the RMA,
df269271 1655 * or just below 2GB, whichever is lower, so that it can be
b7d1f77a
BH
1656 * processed with 32-bit real mode code if necessary
1657 */
744a928c 1658 fdt_addr = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FDT_MAX_SIZE;
b7d1f77a 1659
97b32a6a 1660 fdt = spapr_build_fdt(spapr, true, FDT_MAX_SIZE);
a3467baa 1661
997b6cfc
DG
1662 rc = fdt_pack(fdt);
1663
1664 /* Should only fail if we've built a corrupted tree */
1665 assert(rc == 0);
1666
997b6cfc
DG
1667 /* Load the fdt */
1668 qemu_fdt_dumpdtb(fdt, fdt_totalsize(fdt));
cae172ab 1669 cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
fea35ca4
AK
1670 g_free(spapr->fdt_blob);
1671 spapr->fdt_size = fdt_totalsize(fdt);
1672 spapr->fdt_initial_size = spapr->fdt_size;
1673 spapr->fdt_blob = fdt;
997b6cfc 1674
a3467baa 1675 /* Set up the entry state */
84369f63 1676 spapr_cpu_set_entry_state(first_ppc_cpu, SPAPR_ENTRY_POINT, fdt_addr);
182735ef 1677 first_ppc_cpu->env.gpr[5] = 0;
a3467baa 1678
6787d27b 1679 spapr->cas_reboot = false;
a3467baa
DG
1680}
1681
ce2918cb 1682static void spapr_create_nvram(SpaprMachineState *spapr)
639e8102 1683{
2ff3de68 1684 DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
3978b863 1685 DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
639e8102 1686
3978b863 1687 if (dinfo) {
6231a6da
MA
1688 qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(dinfo),
1689 &error_fatal);
639e8102
DG
1690 }
1691
1692 qdev_init_nofail(dev);
1693
ce2918cb 1694 spapr->nvram = (struct SpaprNvram *)dev;
639e8102
DG
1695}
1696
ce2918cb 1697static void spapr_rtc_create(SpaprMachineState *spapr)
28df36a1 1698{
f6d4dca8
TH
1699 object_initialize_child(OBJECT(spapr), "rtc",
1700 &spapr->rtc, sizeof(spapr->rtc), TYPE_SPAPR_RTC,
1701 &error_fatal, NULL);
147ff807
CLG
1702 object_property_set_bool(OBJECT(&spapr->rtc), true, "realized",
1703 &error_fatal);
1704 object_property_add_alias(OBJECT(spapr), "rtc-time", OBJECT(&spapr->rtc),
1705 "date", &error_fatal);
28df36a1
DG
1706}
1707
8c57b867 1708/* Returns whether we want to use VGA or not */
14c6a894 1709static bool spapr_vga_init(PCIBus *pci_bus, Error **errp)
f28359d8 1710{
8c57b867 1711 switch (vga_interface_type) {
8c57b867 1712 case VGA_NONE:
7effdaa3
MW
1713 return false;
1714 case VGA_DEVICE:
1715 return true;
1ddcae82 1716 case VGA_STD:
b798c190 1717 case VGA_VIRTIO:
6e66d0c6 1718 case VGA_CIRRUS:
1ddcae82 1719 return pci_vga_init(pci_bus) != NULL;
8c57b867 1720 default:
14c6a894
DG
1721 error_setg(errp,
1722 "Unsupported VGA mode, only -vga std or -vga virtio is supported");
1723 return false;
f28359d8 1724 }
f28359d8
LZ
1725}
1726
4e5fe368
SJS
1727static int spapr_pre_load(void *opaque)
1728{
1729 int rc;
1730
1731 rc = spapr_caps_pre_load(opaque);
1732 if (rc) {
1733 return rc;
1734 }
1735
1736 return 0;
1737}
1738
880ae7de
DG
1739static int spapr_post_load(void *opaque, int version_id)
1740{
ce2918cb 1741 SpaprMachineState *spapr = (SpaprMachineState *)opaque;
880ae7de
DG
1742 int err = 0;
1743
be85537d
DG
1744 err = spapr_caps_post_migration(spapr);
1745 if (err) {
1746 return err;
1747 }
1748
e502202c
CLG
1749 /*
1750 * In earlier versions, there was no separate qdev for the PAPR
880ae7de
DG
1751 * RTC, so the RTC offset was stored directly in sPAPREnvironment.
1752 * So when migrating from those versions, poke the incoming offset
e502202c
CLG
1753 * value into the RTC device
1754 */
880ae7de 1755 if (version_id < 3) {
147ff807 1756 err = spapr_rtc_import_offset(&spapr->rtc, spapr->rtc_offset);
e502202c
CLG
1757 if (err) {
1758 return err;
1759 }
880ae7de
DG
1760 }
1761
0c86b2df 1762 if (kvm_enabled() && spapr->patb_entry) {
d39c90f5 1763 PowerPCCPU *cpu = POWERPC_CPU(first_cpu);
79825f4d 1764 bool radix = !!(spapr->patb_entry & PATE1_GR);
d39c90f5 1765 bool gtse = !!(cpu->env.spr[SPR_LPCR] & LPCR_GTSE);
00fd075e
BH
1766
1767 /*
1768 * Update LPCR:HR and UPRT as they may not be set properly in
1769 * the stream
1770 */
1771 spapr_set_all_lpcrs(radix ? (LPCR_HR | LPCR_UPRT) : 0,
1772 LPCR_HR | LPCR_UPRT);
d39c90f5
BR
1773
1774 err = kvmppc_configure_v3_mmu(cpu, radix, gtse, spapr->patb_entry);
1775 if (err) {
1776 error_report("Process table config unsupported by the host");
1777 return -EINVAL;
1778 }
1779 }
1780
1c53b06c
CLG
1781 err = spapr_irq_post_load(spapr, version_id);
1782 if (err) {
1783 return err;
1784 }
1785
880ae7de
DG
1786 return err;
1787}
1788
4e5fe368
SJS
1789static int spapr_pre_save(void *opaque)
1790{
1791 int rc;
1792
1793 rc = spapr_caps_pre_save(opaque);
1794 if (rc) {
1795 return rc;
1796 }
1797
1798 return 0;
1799}
1800
880ae7de
DG
1801static bool version_before_3(void *opaque, int version_id)
1802{
1803 return version_id < 3;
1804}
1805
fd38804b
DHB
1806static bool spapr_pending_events_needed(void *opaque)
1807{
ce2918cb 1808 SpaprMachineState *spapr = (SpaprMachineState *)opaque;
fd38804b
DHB
1809 return !QTAILQ_EMPTY(&spapr->pending_events);
1810}
1811
1812static const VMStateDescription vmstate_spapr_event_entry = {
1813 .name = "spapr_event_log_entry",
1814 .version_id = 1,
1815 .minimum_version_id = 1,
1816 .fields = (VMStateField[]) {
ce2918cb
DG
1817 VMSTATE_UINT32(summary, SpaprEventLogEntry),
1818 VMSTATE_UINT32(extended_length, SpaprEventLogEntry),
1819 VMSTATE_VBUFFER_ALLOC_UINT32(extended_log, SpaprEventLogEntry, 0,
5341258e 1820 NULL, extended_length),
fd38804b
DHB
1821 VMSTATE_END_OF_LIST()
1822 },
1823};
1824
1825static const VMStateDescription vmstate_spapr_pending_events = {
1826 .name = "spapr_pending_events",
1827 .version_id = 1,
1828 .minimum_version_id = 1,
1829 .needed = spapr_pending_events_needed,
1830 .fields = (VMStateField[]) {
ce2918cb
DG
1831 VMSTATE_QTAILQ_V(pending_events, SpaprMachineState, 1,
1832 vmstate_spapr_event_entry, SpaprEventLogEntry, next),
fd38804b
DHB
1833 VMSTATE_END_OF_LIST()
1834 },
1835};
1836
62ef3760
MR
1837static bool spapr_ov5_cas_needed(void *opaque)
1838{
ce2918cb
DG
1839 SpaprMachineState *spapr = opaque;
1840 SpaprOptionVector *ov5_mask = spapr_ovec_new();
62ef3760
MR
1841 bool cas_needed;
1842
ce2918cb 1843 /* Prior to the introduction of SpaprOptionVector, we had two option
62ef3760
MR
1844 * vectors we dealt with: OV5_FORM1_AFFINITY, and OV5_DRCONF_MEMORY.
1845 * Both of these options encode machine topology into the device-tree
1846 * in such a way that the now-booted OS should still be able to interact
1847 * appropriately with QEMU regardless of what options were actually
1848 * negotiatied on the source side.
1849 *
1850 * As such, we can avoid migrating the CAS-negotiated options if these
1851 * are the only options available on the current machine/platform.
1852 * Since these are the only options available for pseries-2.7 and
1853 * earlier, this allows us to maintain old->new/new->old migration
1854 * compatibility.
1855 *
1856 * For QEMU 2.8+, there are additional CAS-negotiatable options available
1857 * via default pseries-2.8 machines and explicit command-line parameters.
1858 * Some of these options, like OV5_HP_EVT, *do* require QEMU to be aware
1859 * of the actual CAS-negotiated values to continue working properly. For
1860 * example, availability of memory unplug depends on knowing whether
1861 * OV5_HP_EVT was negotiated via CAS.
1862 *
1863 * Thus, for any cases where the set of available CAS-negotiatable
1864 * options extends beyond OV5_FORM1_AFFINITY and OV5_DRCONF_MEMORY, we
aef19c04
GK
1865 * include the CAS-negotiated options in the migration stream, unless
1866 * if they affect boot time behaviour only.
62ef3760
MR
1867 */
1868 spapr_ovec_set(ov5_mask, OV5_FORM1_AFFINITY);
1869 spapr_ovec_set(ov5_mask, OV5_DRCONF_MEMORY);
aef19c04 1870 spapr_ovec_set(ov5_mask, OV5_DRMEM_V2);
62ef3760 1871
d1d32d62
DG
1872 /* We need extra information if we have any bits outside the mask
1873 * defined above */
1874 cas_needed = !spapr_ovec_subset(spapr->ov5, ov5_mask);
62ef3760
MR
1875
1876 spapr_ovec_cleanup(ov5_mask);
62ef3760
MR
1877
1878 return cas_needed;
1879}
1880
1881static const VMStateDescription vmstate_spapr_ov5_cas = {
1882 .name = "spapr_option_vector_ov5_cas",
1883 .version_id = 1,
1884 .minimum_version_id = 1,
1885 .needed = spapr_ov5_cas_needed,
1886 .fields = (VMStateField[]) {
ce2918cb
DG
1887 VMSTATE_STRUCT_POINTER_V(ov5_cas, SpaprMachineState, 1,
1888 vmstate_spapr_ovec, SpaprOptionVector),
62ef3760
MR
1889 VMSTATE_END_OF_LIST()
1890 },
1891};
1892
9861bb3e
SJS
1893static bool spapr_patb_entry_needed(void *opaque)
1894{
ce2918cb 1895 SpaprMachineState *spapr = opaque;
9861bb3e
SJS
1896
1897 return !!spapr->patb_entry;
1898}
1899
1900static const VMStateDescription vmstate_spapr_patb_entry = {
1901 .name = "spapr_patb_entry",
1902 .version_id = 1,
1903 .minimum_version_id = 1,
1904 .needed = spapr_patb_entry_needed,
1905 .fields = (VMStateField[]) {
ce2918cb 1906 VMSTATE_UINT64(patb_entry, SpaprMachineState),
9861bb3e
SJS
1907 VMSTATE_END_OF_LIST()
1908 },
1909};
1910
82cffa2e
CLG
1911static bool spapr_irq_map_needed(void *opaque)
1912{
ce2918cb 1913 SpaprMachineState *spapr = opaque;
82cffa2e
CLG
1914
1915 return spapr->irq_map && !bitmap_empty(spapr->irq_map, spapr->irq_map_nr);
1916}
1917
1918static const VMStateDescription vmstate_spapr_irq_map = {
1919 .name = "spapr_irq_map",
1920 .version_id = 1,
1921 .minimum_version_id = 1,
1922 .needed = spapr_irq_map_needed,
1923 .fields = (VMStateField[]) {
ce2918cb 1924 VMSTATE_BITMAP(irq_map, SpaprMachineState, 0, irq_map_nr),
82cffa2e
CLG
1925 VMSTATE_END_OF_LIST()
1926 },
1927};
1928
fea35ca4
AK
1929static bool spapr_dtb_needed(void *opaque)
1930{
ce2918cb 1931 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(opaque);
fea35ca4
AK
1932
1933 return smc->update_dt_enabled;
1934}
1935
1936static int spapr_dtb_pre_load(void *opaque)
1937{
ce2918cb 1938 SpaprMachineState *spapr = (SpaprMachineState *)opaque;
fea35ca4
AK
1939
1940 g_free(spapr->fdt_blob);
1941 spapr->fdt_blob = NULL;
1942 spapr->fdt_size = 0;
1943
1944 return 0;
1945}
1946
1947static const VMStateDescription vmstate_spapr_dtb = {
1948 .name = "spapr_dtb",
1949 .version_id = 1,
1950 .minimum_version_id = 1,
1951 .needed = spapr_dtb_needed,
1952 .pre_load = spapr_dtb_pre_load,
1953 .fields = (VMStateField[]) {
ce2918cb
DG
1954 VMSTATE_UINT32(fdt_initial_size, SpaprMachineState),
1955 VMSTATE_UINT32(fdt_size, SpaprMachineState),
1956 VMSTATE_VBUFFER_ALLOC_UINT32(fdt_blob, SpaprMachineState, 0, NULL,
fea35ca4
AK
1957 fdt_size),
1958 VMSTATE_END_OF_LIST()
1959 },
1960};
1961
4be21d56
DG
1962static const VMStateDescription vmstate_spapr = {
1963 .name = "spapr",
880ae7de 1964 .version_id = 3,
4be21d56 1965 .minimum_version_id = 1,
4e5fe368 1966 .pre_load = spapr_pre_load,
880ae7de 1967 .post_load = spapr_post_load,
4e5fe368 1968 .pre_save = spapr_pre_save,
3aff6c2f 1969 .fields = (VMStateField[]) {
880ae7de
DG
1970 /* used to be @next_irq */
1971 VMSTATE_UNUSED_BUFFER(version_before_3, 0, 4),
4be21d56
DG
1972
1973 /* RTC offset */
ce2918cb 1974 VMSTATE_UINT64_TEST(rtc_offset, SpaprMachineState, version_before_3),
880ae7de 1975
ce2918cb 1976 VMSTATE_PPC_TIMEBASE_V(tb, SpaprMachineState, 2),
4be21d56
DG
1977 VMSTATE_END_OF_LIST()
1978 },
62ef3760
MR
1979 .subsections = (const VMStateDescription*[]) {
1980 &vmstate_spapr_ov5_cas,
9861bb3e 1981 &vmstate_spapr_patb_entry,
fd38804b 1982 &vmstate_spapr_pending_events,
4e5fe368
SJS
1983 &vmstate_spapr_cap_htm,
1984 &vmstate_spapr_cap_vsx,
1985 &vmstate_spapr_cap_dfp,
8f38eaf8 1986 &vmstate_spapr_cap_cfpc,
09114fd8 1987 &vmstate_spapr_cap_sbbc,
4be8d4e7 1988 &vmstate_spapr_cap_ibs,
64d4a534 1989 &vmstate_spapr_cap_hpt_maxpagesize,
82cffa2e 1990 &vmstate_spapr_irq_map,
b9a477b7 1991 &vmstate_spapr_cap_nested_kvm_hv,
fea35ca4 1992 &vmstate_spapr_dtb,
c982f5cf 1993 &vmstate_spapr_cap_large_decr,
8ff43ee4 1994 &vmstate_spapr_cap_ccf_assist,
62ef3760
MR
1995 NULL
1996 }
4be21d56
DG
1997};
1998
4be21d56
DG
1999static int htab_save_setup(QEMUFile *f, void *opaque)
2000{
ce2918cb 2001 SpaprMachineState *spapr = opaque;
4be21d56 2002
4be21d56 2003 /* "Iteration" header */
3a384297
BR
2004 if (!spapr->htab_shift) {
2005 qemu_put_be32(f, -1);
2006 } else {
2007 qemu_put_be32(f, spapr->htab_shift);
2008 }
4be21d56 2009
e68cb8b4
AK
2010 if (spapr->htab) {
2011 spapr->htab_save_index = 0;
2012 spapr->htab_first_pass = true;
2013 } else {
3a384297
BR
2014 if (spapr->htab_shift) {
2015 assert(kvm_enabled());
2016 }
e68cb8b4
AK
2017 }
2018
2019
4be21d56
DG
2020 return 0;
2021}
2022
ce2918cb 2023static void htab_save_chunk(QEMUFile *f, SpaprMachineState *spapr,
332f7721
GK
2024 int chunkstart, int n_valid, int n_invalid)
2025{
2026 qemu_put_be32(f, chunkstart);
2027 qemu_put_be16(f, n_valid);
2028 qemu_put_be16(f, n_invalid);
2029 qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
2030 HASH_PTE_SIZE_64 * n_valid);
2031}
2032
2033static void htab_save_end_marker(QEMUFile *f)
2034{
2035 qemu_put_be32(f, 0);
2036 qemu_put_be16(f, 0);
2037 qemu_put_be16(f, 0);
2038}
2039
ce2918cb 2040static void htab_save_first_pass(QEMUFile *f, SpaprMachineState *spapr,
4be21d56
DG
2041 int64_t max_ns)
2042{
378bc217 2043 bool has_timeout = max_ns != -1;
4be21d56
DG
2044 int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
2045 int index = spapr->htab_save_index;
bc72ad67 2046 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
4be21d56
DG
2047
2048 assert(spapr->htab_first_pass);
2049
2050 do {
2051 int chunkstart;
2052
2053 /* Consume invalid HPTEs */
2054 while ((index < htabslots)
2055 && !HPTE_VALID(HPTE(spapr->htab, index))) {
4be21d56 2056 CLEAN_HPTE(HPTE(spapr->htab, index));
24ec2863 2057 index++;
4be21d56
DG
2058 }
2059
2060 /* Consume valid HPTEs */
2061 chunkstart = index;
338c25b6 2062 while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
4be21d56 2063 && HPTE_VALID(HPTE(spapr->htab, index))) {
4be21d56 2064 CLEAN_HPTE(HPTE(spapr->htab, index));
24ec2863 2065 index++;
4be21d56
DG
2066 }
2067
2068 if (index > chunkstart) {
2069 int n_valid = index - chunkstart;
2070
332f7721 2071 htab_save_chunk(f, spapr, chunkstart, n_valid, 0);
4be21d56 2072
378bc217
DG
2073 if (has_timeout &&
2074 (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
4be21d56
DG
2075 break;
2076 }
2077 }
2078 } while ((index < htabslots) && !qemu_file_rate_limit(f));
2079
2080 if (index >= htabslots) {
2081 assert(index == htabslots);
2082 index = 0;
2083 spapr->htab_first_pass = false;
2084 }
2085 spapr->htab_save_index = index;
2086}
2087
ce2918cb 2088static int htab_save_later_pass(QEMUFile *f, SpaprMachineState *spapr,
e68cb8b4 2089 int64_t max_ns)
4be21d56
DG
2090{
2091 bool final = max_ns < 0;
2092 int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
2093 int examined = 0, sent = 0;
2094 int index = spapr->htab_save_index;
bc72ad67 2095 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
4be21d56
DG
2096
2097 assert(!spapr->htab_first_pass);
2098
2099 do {
2100 int chunkstart, invalidstart;
2101
2102 /* Consume non-dirty HPTEs */
2103 while ((index < htabslots)
2104 && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
2105 index++;
2106 examined++;
2107 }
2108
2109 chunkstart = index;
2110 /* Consume valid dirty HPTEs */
338c25b6 2111 while ((index < htabslots) && (index - chunkstart < USHRT_MAX)
4be21d56
DG
2112 && HPTE_DIRTY(HPTE(spapr->htab, index))
2113 && HPTE_VALID(HPTE(spapr->htab, index))) {
2114 CLEAN_HPTE(HPTE(spapr->htab, index));
2115 index++;
2116 examined++;
2117 }
2118
2119 invalidstart = index;
2120 /* Consume invalid dirty HPTEs */
338c25b6 2121 while ((index < htabslots) && (index - invalidstart < USHRT_MAX)
4be21d56
DG
2122 && HPTE_DIRTY(HPTE(spapr->htab, index))
2123 && !HPTE_VALID(HPTE(spapr->htab, index))) {
2124 CLEAN_HPTE(HPTE(spapr->htab, index));
2125 index++;
2126 examined++;
2127 }
2128
2129 if (index > chunkstart) {
2130 int n_valid = invalidstart - chunkstart;
2131 int n_invalid = index - invalidstart;
2132
332f7721 2133 htab_save_chunk(f, spapr, chunkstart, n_valid, n_invalid);
4be21d56
DG
2134 sent += index - chunkstart;
2135
bc72ad67 2136 if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
4be21d56
DG
2137 break;
2138 }
2139 }
2140
2141 if (examined >= htabslots) {
2142 break;
2143 }
2144
2145 if (index >= htabslots) {
2146 assert(index == htabslots);
2147 index = 0;
2148 }
2149 } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
2150
2151 if (index >= htabslots) {
2152 assert(index == htabslots);
2153 index = 0;
2154 }
2155
2156 spapr->htab_save_index = index;
2157
e68cb8b4 2158 return (examined >= htabslots) && (sent == 0) ? 1 : 0;
4be21d56
DG
2159}
2160
e68cb8b4
AK
2161#define MAX_ITERATION_NS 5000000 /* 5 ms */
2162#define MAX_KVM_BUF_SIZE 2048
2163
4be21d56
DG
2164static int htab_save_iterate(QEMUFile *f, void *opaque)
2165{
ce2918cb 2166 SpaprMachineState *spapr = opaque;
715c5407 2167 int fd;
e68cb8b4 2168 int rc = 0;
4be21d56
DG
2169
2170 /* Iteration header */
3a384297
BR
2171 if (!spapr->htab_shift) {
2172 qemu_put_be32(f, -1);
e8cd4247 2173 return 1;
3a384297
BR
2174 } else {
2175 qemu_put_be32(f, 0);
2176 }
4be21d56 2177
e68cb8b4
AK
2178 if (!spapr->htab) {
2179 assert(kvm_enabled());
2180
715c5407
DG
2181 fd = get_htab_fd(spapr);
2182 if (fd < 0) {
2183 return fd;
01a57972
SMJ
2184 }
2185
715c5407 2186 rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
e68cb8b4
AK
2187 if (rc < 0) {
2188 return rc;
2189 }
2190 } else if (spapr->htab_first_pass) {
4be21d56
DG
2191 htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
2192 } else {
e68cb8b4 2193 rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
4be21d56
DG
2194 }
2195
332f7721 2196 htab_save_end_marker(f);
4be21d56 2197
e68cb8b4 2198 return rc;
4be21d56
DG
2199}
2200
2201static int htab_save_complete(QEMUFile *f, void *opaque)
2202{
ce2918cb 2203 SpaprMachineState *spapr = opaque;
715c5407 2204 int fd;
4be21d56
DG
2205
2206 /* Iteration header */
3a384297
BR
2207 if (!spapr->htab_shift) {
2208 qemu_put_be32(f, -1);
2209 return 0;
2210 } else {
2211 qemu_put_be32(f, 0);
2212 }
4be21d56 2213
e68cb8b4
AK
2214 if (!spapr->htab) {
2215 int rc;
2216
2217 assert(kvm_enabled());
2218
715c5407
DG
2219 fd = get_htab_fd(spapr);
2220 if (fd < 0) {
2221 return fd;
01a57972
SMJ
2222 }
2223
715c5407 2224 rc = kvmppc_save_htab(f, fd, MAX_KVM_BUF_SIZE, -1);
e68cb8b4
AK
2225 if (rc < 0) {
2226 return rc;
2227 }
e68cb8b4 2228 } else {
378bc217
DG
2229 if (spapr->htab_first_pass) {
2230 htab_save_first_pass(f, spapr, -1);
2231 }
e68cb8b4
AK
2232 htab_save_later_pass(f, spapr, -1);
2233 }
4be21d56
DG
2234
2235 /* End marker */
332f7721 2236 htab_save_end_marker(f);
4be21d56
DG
2237
2238 return 0;
2239}
2240
2241static int htab_load(QEMUFile *f, void *opaque, int version_id)
2242{
ce2918cb 2243 SpaprMachineState *spapr = opaque;
4be21d56 2244 uint32_t section_hdr;
e68cb8b4 2245 int fd = -1;
14b0d748 2246 Error *local_err = NULL;
4be21d56
DG
2247
2248 if (version_id < 1 || version_id > 1) {
98a5d100 2249 error_report("htab_load() bad version");
4be21d56
DG
2250 return -EINVAL;
2251 }
2252
2253 section_hdr = qemu_get_be32(f);
2254
3a384297
BR
2255 if (section_hdr == -1) {
2256 spapr_free_hpt(spapr);
2257 return 0;
2258 }
2259
4be21d56 2260 if (section_hdr) {
c5f54f3e
DG
2261 /* First section gives the htab size */
2262 spapr_reallocate_hpt(spapr, section_hdr, &local_err);
2263 if (local_err) {
2264 error_report_err(local_err);
4be21d56
DG
2265 return -EINVAL;
2266 }
2267 return 0;
2268 }
2269
e68cb8b4
AK
2270 if (!spapr->htab) {
2271 assert(kvm_enabled());
2272
14b0d748 2273 fd = kvmppc_get_htab_fd(true, 0, &local_err);
e68cb8b4 2274 if (fd < 0) {
14b0d748 2275 error_report_err(local_err);
82be8e73 2276 return fd;
e68cb8b4
AK
2277 }
2278 }
2279
4be21d56
DG
2280 while (true) {
2281 uint32_t index;
2282 uint16_t n_valid, n_invalid;
2283
2284 index = qemu_get_be32(f);
2285 n_valid = qemu_get_be16(f);
2286 n_invalid = qemu_get_be16(f);
2287
2288 if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
2289 /* End of Stream */
2290 break;
2291 }
2292
e68cb8b4 2293 if ((index + n_valid + n_invalid) >
4be21d56
DG
2294 (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
2295 /* Bad index in stream */
98a5d100
DG
2296 error_report(
2297 "htab_load() bad index %d (%hd+%hd entries) in htab stream (htab_shift=%d)",
2298 index, n_valid, n_invalid, spapr->htab_shift);
4be21d56
DG
2299 return -EINVAL;
2300 }
2301
e68cb8b4
AK
2302 if (spapr->htab) {
2303 if (n_valid) {
2304 qemu_get_buffer(f, HPTE(spapr->htab, index),
2305 HASH_PTE_SIZE_64 * n_valid);
2306 }
2307 if (n_invalid) {
2308 memset(HPTE(spapr->htab, index + n_valid), 0,
2309 HASH_PTE_SIZE_64 * n_invalid);
2310 }
2311 } else {
2312 int rc;
2313
2314 assert(fd >= 0);
2315
2316 rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
2317 if (rc < 0) {
2318 return rc;
2319 }
4be21d56
DG
2320 }
2321 }
2322
e68cb8b4
AK
2323 if (!spapr->htab) {
2324 assert(fd >= 0);
2325 close(fd);
2326 }
2327
4be21d56
DG
2328 return 0;
2329}
2330
70f794fc 2331static void htab_save_cleanup(void *opaque)
c573fc03 2332{
ce2918cb 2333 SpaprMachineState *spapr = opaque;
c573fc03
TH
2334
2335 close_htab_fd(spapr);
2336}
2337
4be21d56 2338static SaveVMHandlers savevm_htab_handlers = {
9907e842 2339 .save_setup = htab_save_setup,
4be21d56 2340 .save_live_iterate = htab_save_iterate,
a3e06c3d 2341 .save_live_complete_precopy = htab_save_complete,
70f794fc 2342 .save_cleanup = htab_save_cleanup,
4be21d56
DG
2343 .load_state = htab_load,
2344};
2345
5b2128d2
AG
2346static void spapr_boot_set(void *opaque, const char *boot_device,
2347 Error **errp)
2348{
c86c1aff 2349 MachineState *machine = MACHINE(opaque);
5b2128d2
AG
2350 machine->boot_order = g_strdup(boot_device);
2351}
2352
ce2918cb 2353static void spapr_create_lmb_dr_connectors(SpaprMachineState *spapr)
224245bf
DG
2354{
2355 MachineState *machine = MACHINE(spapr);
2356 uint64_t lmb_size = SPAPR_MEMORY_BLOCK_SIZE;
e8f986fc 2357 uint32_t nr_lmbs = (machine->maxram_size - machine->ram_size)/lmb_size;
224245bf
DG
2358 int i;
2359
2360 for (i = 0; i < nr_lmbs; i++) {
224245bf
DG
2361 uint64_t addr;
2362
b0c14ec4 2363 addr = i * lmb_size + machine->device_memory->base;
6caf3ac6
DG
2364 spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_LMB,
2365 addr / lmb_size);
224245bf
DG
2366 }
2367}
2368
2369/*
2370 * If RAM size, maxmem size and individual node mem sizes aren't aligned
2371 * to SPAPR_MEMORY_BLOCK_SIZE(256MB), then refuse to start the guest
2372 * since we can't support such unaligned sizes with DRCONF_MEMORY.
2373 */
7c150d6f 2374static void spapr_validate_node_memory(MachineState *machine, Error **errp)
224245bf
DG
2375{
2376 int i;
2377
7c150d6f
DG
2378 if (machine->ram_size % SPAPR_MEMORY_BLOCK_SIZE) {
2379 error_setg(errp, "Memory size 0x" RAM_ADDR_FMT
ab3dd749 2380 " is not aligned to %" PRIu64 " MiB",
7c150d6f 2381 machine->ram_size,
d23b6caa 2382 SPAPR_MEMORY_BLOCK_SIZE / MiB);
7c150d6f
DG
2383 return;
2384 }
2385
2386 if (machine->maxram_size % SPAPR_MEMORY_BLOCK_SIZE) {
2387 error_setg(errp, "Maximum memory size 0x" RAM_ADDR_FMT
ab3dd749 2388 " is not aligned to %" PRIu64 " MiB",
7c150d6f 2389 machine->ram_size,
d23b6caa 2390 SPAPR_MEMORY_BLOCK_SIZE / MiB);
7c150d6f 2391 return;
224245bf
DG
2392 }
2393
aa570207 2394 for (i = 0; i < machine->numa_state->num_nodes; i++) {
7e721e7b 2395 if (machine->numa_state->nodes[i].node_mem % SPAPR_MEMORY_BLOCK_SIZE) {
7c150d6f
DG
2396 error_setg(errp,
2397 "Node %d memory size 0x%" PRIx64
ab3dd749 2398 " is not aligned to %" PRIu64 " MiB",
7e721e7b 2399 i, machine->numa_state->nodes[i].node_mem,
d23b6caa 2400 SPAPR_MEMORY_BLOCK_SIZE / MiB);
7c150d6f 2401 return;
224245bf
DG
2402 }
2403 }
2404}
2405
535455fd
IM
2406/* find cpu slot in machine->possible_cpus by core_id */
2407static CPUArchId *spapr_find_cpu_slot(MachineState *ms, uint32_t id, int *idx)
2408{
fe6b6346 2409 int index = id / ms->smp.threads;
535455fd
IM
2410
2411 if (index >= ms->possible_cpus->len) {
2412 return NULL;
2413 }
2414 if (idx) {
2415 *idx = index;
2416 }
2417 return &ms->possible_cpus->cpus[index];
2418}
2419
ce2918cb 2420static void spapr_set_vsmt_mode(SpaprMachineState *spapr, Error **errp)
fa98fbfc 2421{
fe6b6346 2422 MachineState *ms = MACHINE(spapr);
29cb4187 2423 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
fa98fbfc
SB
2424 Error *local_err = NULL;
2425 bool vsmt_user = !!spapr->vsmt;
2426 int kvm_smt = kvmppc_smt_threads();
2427 int ret;
fe6b6346 2428 unsigned int smp_threads = ms->smp.threads;
fa98fbfc
SB
2429
2430 if (!kvm_enabled() && (smp_threads > 1)) {
2431 error_setg(&local_err, "TCG cannot support more than 1 thread/core "
2432 "on a pseries machine");
2433 goto out;
2434 }
2435 if (!is_power_of_2(smp_threads)) {
2436 error_setg(&local_err, "Cannot support %d threads/core on a pseries "
2437 "machine because it must be a power of 2", smp_threads);
2438 goto out;
2439 }
2440
2441 /* Detemine the VSMT mode to use: */
2442 if (vsmt_user) {
2443 if (spapr->vsmt < smp_threads) {
2444 error_setg(&local_err, "Cannot support VSMT mode %d"
2445 " because it must be >= threads/core (%d)",
2446 spapr->vsmt, smp_threads);
2447 goto out;
2448 }
2449 /* In this case, spapr->vsmt has been set by the command line */
29cb4187 2450 } else if (!smc->smp_threads_vsmt) {
8904e5a7
DG
2451 /*
2452 * Default VSMT value is tricky, because we need it to be as
2453 * consistent as possible (for migration), but this requires
2454 * changing it for at least some existing cases. We pick 8 as
2455 * the value that we'd get with KVM on POWER8, the
2456 * overwhelmingly common case in production systems.
2457 */
4ad64cbd 2458 spapr->vsmt = MAX(8, smp_threads);
29cb4187
GK
2459 } else {
2460 spapr->vsmt = smp_threads;
fa98fbfc
SB
2461 }
2462
2463 /* KVM: If necessary, set the SMT mode: */
2464 if (kvm_enabled() && (spapr->vsmt != kvm_smt)) {
2465 ret = kvmppc_set_smt_threads(spapr->vsmt);
2466 if (ret) {
1f20f2e0 2467 /* Looks like KVM isn't able to change VSMT mode */
fa98fbfc
SB
2468 error_setg(&local_err,
2469 "Failed to set KVM's VSMT mode to %d (errno %d)",
2470 spapr->vsmt, ret);
1f20f2e0
DG
2471 /* We can live with that if the default one is big enough
2472 * for the number of threads, and a submultiple of the one
2473 * we want. In this case we'll waste some vcpu ids, but
2474 * behaviour will be correct */
2475 if ((kvm_smt >= smp_threads) && ((spapr->vsmt % kvm_smt) == 0)) {
2476 warn_report_err(local_err);
2477 local_err = NULL;
2478 goto out;
2479 } else {
2480 if (!vsmt_user) {
2481 error_append_hint(&local_err,
2482 "On PPC, a VM with %d threads/core"
2483 " on a host with %d threads/core"
2484 " requires the use of VSMT mode %d.\n",
2485 smp_threads, kvm_smt, spapr->vsmt);
2486 }
cdcca22a 2487 kvmppc_error_append_smt_possible_hint(&local_err);
1f20f2e0 2488 goto out;
fa98fbfc 2489 }
fa98fbfc
SB
2490 }
2491 }
2492 /* else TCG: nothing to do currently */
2493out:
2494 error_propagate(errp, local_err);
2495}
2496
ce2918cb 2497static void spapr_init_cpus(SpaprMachineState *spapr)
1a5008fc
GK
2498{
2499 MachineState *machine = MACHINE(spapr);
2500 MachineClass *mc = MACHINE_GET_CLASS(machine);
ce2918cb 2501 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
1a5008fc
GK
2502 const char *type = spapr_get_cpu_core_type(machine->cpu_type);
2503 const CPUArchIdList *possible_cpus;
fe6b6346
LX
2504 unsigned int smp_cpus = machine->smp.cpus;
2505 unsigned int smp_threads = machine->smp.threads;
2506 unsigned int max_cpus = machine->smp.max_cpus;
1a5008fc
GK
2507 int boot_cores_nr = smp_cpus / smp_threads;
2508 int i;
2509
2510 possible_cpus = mc->possible_cpu_arch_ids(machine);
2511 if (mc->has_hotpluggable_cpus) {
2512 if (smp_cpus % smp_threads) {
2513 error_report("smp_cpus (%u) must be multiple of threads (%u)",
2514 smp_cpus, smp_threads);
2515 exit(1);
2516 }
2517 if (max_cpus % smp_threads) {
2518 error_report("max_cpus (%u) must be multiple of threads (%u)",
2519 max_cpus, smp_threads);
2520 exit(1);
2521 }
2522 } else {
2523 if (max_cpus != smp_cpus) {
2524 error_report("This machine version does not support CPU hotplug");
2525 exit(1);
2526 }
2527 boot_cores_nr = possible_cpus->len;
2528 }
2529
1a5008fc
GK
2530 if (smc->pre_2_10_has_unused_icps) {
2531 int i;
2532
1a518e76 2533 for (i = 0; i < spapr_max_server_number(spapr); i++) {
1a5008fc
GK
2534 /* Dummy entries get deregistered when real ICPState objects
2535 * are registered during CPU core hotplug.
2536 */
2537 pre_2_10_vmstate_register_dummy_icp(i);
2538 }
2539 }
2540
2541 for (i = 0; i < possible_cpus->len; i++) {
2542 int core_id = i * smp_threads;
2543
2544 if (mc->has_hotpluggable_cpus) {
2545 spapr_dr_connector_new(OBJECT(spapr), TYPE_SPAPR_DRC_CPU,
2546 spapr_vcpu_id(spapr, core_id));
2547 }
2548
2549 if (i < boot_cores_nr) {
2550 Object *core = object_new(type);
2551 int nr_threads = smp_threads;
2552
2553 /* Handle the partially filled core for older machine types */
2554 if ((i + 1) * smp_threads >= smp_cpus) {
2555 nr_threads = smp_cpus - i * smp_threads;
2556 }
2557
2558 object_property_set_int(core, nr_threads, "nr-threads",
2559 &error_fatal);
2560 object_property_set_int(core, core_id, CPU_CORE_PROP_CORE_ID,
2561 &error_fatal);
2562 object_property_set_bool(core, true, "realized", &error_fatal);
ecda255e
SB
2563
2564 object_unref(core);
1a5008fc
GK
2565 }
2566 }
2567}
2568
999c9caf
GK
2569static PCIHostState *spapr_create_default_phb(void)
2570{
2571 DeviceState *dev;
2572
2573 dev = qdev_create(NULL, TYPE_SPAPR_PCI_HOST_BRIDGE);
2574 qdev_prop_set_uint32(dev, "index", 0);
2575 qdev_init_nofail(dev);
2576
2577 return PCI_HOST_BRIDGE(dev);
2578}
2579
9fdf0c29 2580/* pSeries LPAR / sPAPR hardware init */
bcb5ce08 2581static void spapr_machine_init(MachineState *machine)
9fdf0c29 2582{
ce2918cb
DG
2583 SpaprMachineState *spapr = SPAPR_MACHINE(machine);
2584 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(machine);
3ef96221 2585 const char *kernel_filename = machine->kernel_filename;
3ef96221 2586 const char *initrd_filename = machine->initrd_filename;
8c9f64df 2587 PCIHostState *phb;
9fdf0c29 2588 int i;
890c2b77
AK
2589 MemoryRegion *sysmem = get_system_memory();
2590 MemoryRegion *ram = g_new(MemoryRegion, 1);
c86c1aff 2591 hwaddr node0_size = spapr_node0_size(machine);
b7d1f77a 2592 long load_limit, fw_size;
39ac8455 2593 char *filename;
30f4b05b 2594 Error *resize_hpt_err = NULL;
9fdf0c29 2595
226419d6 2596 msi_nonbroken = true;
0ee2c058 2597
d43b45e2 2598 QLIST_INIT(&spapr->phbs);
0cffce56 2599 QTAILQ_INIT(&spapr->pending_dimm_unplugs);
d43b45e2 2600
9f6edd06
DG
2601 /* Determine capabilities to run with */
2602 spapr_caps_init(spapr);
2603
30f4b05b
DG
2604 kvmppc_check_papr_resize_hpt(&resize_hpt_err);
2605 if (spapr->resize_hpt == SPAPR_RESIZE_HPT_DEFAULT) {
2606 /*
2607 * If the user explicitly requested a mode we should either
2608 * supply it, or fail completely (which we do below). But if
2609 * it's not set explicitly, we reset our mode to something
2610 * that works
2611 */
2612 if (resize_hpt_err) {
2613 spapr->resize_hpt = SPAPR_RESIZE_HPT_DISABLED;
2614 error_free(resize_hpt_err);
2615 resize_hpt_err = NULL;
2616 } else {
2617 spapr->resize_hpt = smc->resize_hpt_default;
2618 }
2619 }
2620
2621 assert(spapr->resize_hpt != SPAPR_RESIZE_HPT_DEFAULT);
2622
2623 if ((spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) && resize_hpt_err) {
2624 /*
2625 * User requested HPT resize, but this host can't supply it. Bail out
2626 */
2627 error_report_err(resize_hpt_err);
2628 exit(1);
2629 }
2630
090052aa 2631 spapr->rma_size = node0_size;
354ac20a 2632
090052aa
DG
2633 /* With KVM, we don't actually know whether KVM supports an
2634 * unbounded RMA (PR KVM) or is limited by the hash table size
2635 * (HV KVM using VRMA), so we always assume the latter
2636 *
2637 * In that case, we also limit the initial allocations for RTAS
2638 * etc... to 256M since we have no way to know what the VRMA size
2639 * is going to be as it depends on the size of the hash table
2640 * which isn't determined yet.
2641 */
2642 if (kvm_enabled()) {
2643 spapr->vrma_adjust = 1;
2644 spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
354ac20a 2645 }
7f763a5d 2646
090052aa
DG
2647 /* Actually we don't support unbounded RMA anymore since we added
2648 * proper emulation of HV mode. The max we can get is 16G which
2649 * also happens to be what we configure for PAPR mode so make sure
2650 * we don't do anything bigger than that
2651 */
2652 spapr->rma_size = MIN(spapr->rma_size, 0x400000000ull);
354ac20a 2653
c4177479 2654 if (spapr->rma_size > node0_size) {
d54e4d76
DG
2655 error_report("Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")",
2656 spapr->rma_size);
c4177479
AK
2657 exit(1);
2658 }
2659
b7d1f77a
BH
2660 /* Setup a load limit for the ramdisk leaving room for SLOF and FDT */
2661 load_limit = MIN(spapr->rma_size, RTAS_MAX_ADDR) - FW_OVERHEAD;
9fdf0c29 2662
482969d6
CLG
2663 /*
2664 * VSMT must be set in order to be able to compute VCPU ids, ie to
1a518e76 2665 * call spapr_max_server_number() or spapr_vcpu_id().
482969d6
CLG
2666 */
2667 spapr_set_vsmt_mode(spapr, &error_fatal);
2668
7b565160 2669 /* Set up Interrupt Controller before we create the VCPUs */
fab397d8 2670 spapr_irq_init(spapr, &error_fatal);
7b565160 2671
dc1b5eee
GK
2672 /* Set up containers for ibm,client-architecture-support negotiated options
2673 */
facdb8b6
MR
2674 spapr->ov5 = spapr_ovec_new();
2675 spapr->ov5_cas = spapr_ovec_new();
2676
224245bf 2677 if (smc->dr_lmb_enabled) {
facdb8b6 2678 spapr_ovec_set(spapr->ov5, OV5_DRCONF_MEMORY);
7c150d6f 2679 spapr_validate_node_memory(machine, &error_fatal);
224245bf
DG
2680 }
2681
417ece33
MR
2682 spapr_ovec_set(spapr->ov5, OV5_FORM1_AFFINITY);
2683
ffbb1705
MR
2684 /* advertise support for dedicated HP event source to guests */
2685 if (spapr->use_hotplug_event_source) {
2686 spapr_ovec_set(spapr->ov5, OV5_HP_EVT);
2687 }
2688
2772cf6b
DG
2689 /* advertise support for HPT resizing */
2690 if (spapr->resize_hpt != SPAPR_RESIZE_HPT_DISABLED) {
2691 spapr_ovec_set(spapr->ov5, OV5_HPT_RESIZE);
2692 }
2693
a324d6f1
BR
2694 /* advertise support for ibm,dyamic-memory-v2 */
2695 spapr_ovec_set(spapr->ov5, OV5_DRMEM_V2);
2696
db592b5b 2697 /* advertise XIVE on POWER9 machines */
ca62823b 2698 if (spapr->irq->xive) {
273fef83 2699 spapr_ovec_set(spapr->ov5, OV5_XIVE_EXPLOIT);
db592b5b
CLG
2700 }
2701
9fdf0c29 2702 /* init CPUs */
0c86d0fd 2703 spapr_init_cpus(spapr);
9fdf0c29 2704
58c46efa
LV
2705 /*
2706 * check we don't have a memory-less/cpu-less NUMA node
2707 * Firmware relies on the existing memory/cpu topology to provide the
2708 * NUMA topology to the kernel.
2709 * And the linux kernel needs to know the NUMA topology at start
2710 * to be able to hotplug CPUs later.
2711 */
2712 if (machine->numa_state->num_nodes) {
2713 for (i = 0; i < machine->numa_state->num_nodes; ++i) {
2714 /* check for memory-less node */
2715 if (machine->numa_state->nodes[i].node_mem == 0) {
2716 CPUState *cs;
2717 int found = 0;
2718 /* check for cpu-less node */
2719 CPU_FOREACH(cs) {
2720 PowerPCCPU *cpu = POWERPC_CPU(cs);
2721 if (cpu->node_id == i) {
2722 found = 1;
2723 break;
2724 }
2725 }
2726 /* memory-less and cpu-less node */
2727 if (!found) {
2728 error_report(
2729 "Memory-less/cpu-less nodes are not supported (node %d)",
2730 i);
2731 exit(1);
2732 }
2733 }
2734 }
2735
2736 }
2737
db5127b2
DG
2738 /*
2739 * NVLink2-connected GPU RAM needs to be placed on a separate NUMA node.
2740 * We assign a new numa ID per GPU in spapr_pci_collect_nvgpu() which is
2741 * called from vPHB reset handler so we initialize the counter here.
2742 * If no NUMA is configured from the QEMU side, we start from 1 as GPU RAM
2743 * must be equally distant from any other node.
2744 * The final value of spapr->gpu_numa_id is going to be written to
2745 * max-associativity-domains in spapr_build_fdt().
2746 */
2747 spapr->gpu_numa_id = MAX(1, machine->numa_state->num_nodes);
2748
0550b120 2749 if ((!kvm_enabled() || kvmppc_has_cap_mmu_radix()) &&
ad99d04c
DG
2750 ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00, 0,
2751 spapr->max_compat_pvr)) {
0550b120
GK
2752 /* KVM and TCG always allow GTSE with radix... */
2753 spapr_ovec_set(spapr->ov5, OV5_MMU_RADIX_GTSE);
2754 }
2755 /* ... but not with hash (currently). */
2756
026bfd89
DG
2757 if (kvm_enabled()) {
2758 /* Enable H_LOGICAL_CI_* so SLOF can talk to in-kernel devices */
2759 kvmppc_enable_logical_ci_hcalls();
ef9971dd 2760 kvmppc_enable_set_mode_hcall();
5145ad4f
NW
2761
2762 /* H_CLEAR_MOD/_REF are mandatory in PAPR, but off by default */
2763 kvmppc_enable_clear_ref_mod_hcalls();
68f9f708
SJS
2764
2765 /* Enable H_PAGE_INIT */
2766 kvmppc_enable_h_page_init();
026bfd89
DG
2767 }
2768
9fdf0c29 2769 /* allocate RAM */
f92f5da1 2770 memory_region_allocate_system_memory(ram, NULL, "ppc_spapr.ram",
fb164994 2771 machine->ram_size);
f92f5da1 2772 memory_region_add_subregion(sysmem, 0, ram);
9fdf0c29 2773
b0c14ec4
DH
2774 /* always allocate the device memory information */
2775 machine->device_memory = g_malloc0(sizeof(*machine->device_memory));
2776
4a1c9cf0
BR
2777 /* initialize hotplug memory address space */
2778 if (machine->ram_size < machine->maxram_size) {
0c9269a5 2779 ram_addr_t device_mem_size = machine->maxram_size - machine->ram_size;
71c9a3dd
BR
2780 /*
2781 * Limit the number of hotpluggable memory slots to half the number
2782 * slots that KVM supports, leaving the other half for PCI and other
2783 * devices. However ensure that number of slots doesn't drop below 32.
2784 */
2785 int max_memslots = kvm_enabled() ? kvm_get_max_memslots() / 2 :
2786 SPAPR_MAX_RAM_SLOTS;
4a1c9cf0 2787
71c9a3dd
BR
2788 if (max_memslots < SPAPR_MAX_RAM_SLOTS) {
2789 max_memslots = SPAPR_MAX_RAM_SLOTS;
2790 }
2791 if (machine->ram_slots > max_memslots) {
d54e4d76
DG
2792 error_report("Specified number of memory slots %"
2793 PRIu64" exceeds max supported %d",
71c9a3dd 2794 machine->ram_slots, max_memslots);
d54e4d76 2795 exit(1);
4a1c9cf0
BR
2796 }
2797
b0c14ec4 2798 machine->device_memory->base = ROUND_UP(machine->ram_size,
0c9269a5 2799 SPAPR_DEVICE_MEM_ALIGN);
b0c14ec4 2800 memory_region_init(&machine->device_memory->mr, OBJECT(spapr),
0c9269a5 2801 "device-memory", device_mem_size);
b0c14ec4
DH
2802 memory_region_add_subregion(sysmem, machine->device_memory->base,
2803 &machine->device_memory->mr);
4a1c9cf0
BR
2804 }
2805
224245bf
DG
2806 if (smc->dr_lmb_enabled) {
2807 spapr_create_lmb_dr_connectors(spapr);
2808 }
2809
ffbb1705 2810 /* Set up RTAS event infrastructure */
74d042e5
DG
2811 spapr_events_init(spapr);
2812
12f42174 2813 /* Set up the RTC RTAS interfaces */
28df36a1 2814 spapr_rtc_create(spapr);
12f42174 2815
b5cec4c5 2816 /* Set up VIO bus */
4040ab72
DG
2817 spapr->vio_bus = spapr_vio_bus_init();
2818
b8846a4d 2819 for (i = 0; i < serial_max_hds(); i++) {
9bca0edb
PM
2820 if (serial_hd(i)) {
2821 spapr_vty_create(spapr->vio_bus, serial_hd(i));
4040ab72
DG
2822 }
2823 }
9fdf0c29 2824
639e8102
DG
2825 /* We always have at least the nvram device on VIO */
2826 spapr_create_nvram(spapr);
2827
962b6c36
MR
2828 /*
2829 * Setup hotplug / dynamic-reconfiguration connectors. top-level
2830 * connectors (described in root DT node's "ibm,drc-types" property)
2831 * are pre-initialized here. additional child connectors (such as
2832 * connectors for a PHBs PCI slots) are added as needed during their
2833 * parent's realization.
2834 */
2835 if (smc->dr_phb_enabled) {
2836 for (i = 0; i < SPAPR_MAX_PHBS; i++) {
2837 spapr_dr_connector_new(OBJECT(machine), TYPE_SPAPR_DRC_PHB, i);
2838 }
2839 }
2840
3384f95c 2841 /* Set up PCI */
fa28f71b
AK
2842 spapr_pci_rtas_init();
2843
999c9caf 2844 phb = spapr_create_default_phb();
3384f95c 2845
277f9acf 2846 for (i = 0; i < nb_nics; i++) {
8d90ad90
DG
2847 NICInfo *nd = &nd_table[i];
2848
2849 if (!nd->model) {
3c3a4e7a 2850 nd->model = g_strdup("spapr-vlan");
8d90ad90
DG
2851 }
2852
3c3a4e7a
TH
2853 if (g_str_equal(nd->model, "spapr-vlan") ||
2854 g_str_equal(nd->model, "ibmveth")) {
d601fac4 2855 spapr_vlan_create(spapr->vio_bus, nd);
8d90ad90 2856 } else {
29b358f9 2857 pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
8d90ad90
DG
2858 }
2859 }
2860
6e270446 2861 for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
d601fac4 2862 spapr_vscsi_create(spapr->vio_bus);
6e270446
BH
2863 }
2864
f28359d8 2865 /* Graphics */
14c6a894 2866 if (spapr_vga_init(phb->bus, &error_fatal)) {
3fc5acde 2867 spapr->has_graphics = true;
c6e76503 2868 machine->usb |= defaults_enabled() && !machine->usb_disabled;
f28359d8
LZ
2869 }
2870
4ee9ced9 2871 if (machine->usb) {
57040d45
TH
2872 if (smc->use_ohci_by_default) {
2873 pci_create_simple(phb->bus, -1, "pci-ohci");
2874 } else {
2875 pci_create_simple(phb->bus, -1, "nec-usb-xhci");
2876 }
c86580b8 2877
35139a59 2878 if (spapr->has_graphics) {
c86580b8
MA
2879 USBBus *usb_bus = usb_bus_find(-1);
2880
2881 usb_create_simple(usb_bus, "usb-kbd");
2882 usb_create_simple(usb_bus, "usb-mouse");
35139a59
DG
2883 }
2884 }
2885
ab3dd749 2886 if (spapr->rma_size < (MIN_RMA_SLOF * MiB)) {
d54e4d76
DG
2887 error_report(
2888 "pSeries SLOF firmware requires >= %ldM guest RMA (Real Mode Area memory)",
2889 MIN_RMA_SLOF);
4d8d5467
BH
2890 exit(1);
2891 }
2892
9fdf0c29
DG
2893 if (kernel_filename) {
2894 uint64_t lowaddr = 0;
2895
4366e1db
LM
2896 spapr->kernel_size = load_elf(kernel_filename, NULL,
2897 translate_kernel_address, NULL,
2898 NULL, &lowaddr, NULL, 1,
a19f7fb0
DG
2899 PPC_ELF_MACHINE, 0, 0);
2900 if (spapr->kernel_size == ELF_LOAD_WRONG_ENDIAN) {
4366e1db 2901 spapr->kernel_size = load_elf(kernel_filename, NULL,
a19f7fb0
DG
2902 translate_kernel_address, NULL, NULL,
2903 &lowaddr, NULL, 0, PPC_ELF_MACHINE,
2904 0, 0);
2905 spapr->kernel_le = spapr->kernel_size > 0;
16457e7f 2906 }
a19f7fb0
DG
2907 if (spapr->kernel_size < 0) {
2908 error_report("error loading %s: %s", kernel_filename,
2909 load_elf_strerror(spapr->kernel_size));
9fdf0c29
DG
2910 exit(1);
2911 }
2912
2913 /* load initrd */
2914 if (initrd_filename) {
4d8d5467
BH
2915 /* Try to locate the initrd in the gap between the kernel
2916 * and the firmware. Add a bit of space just in case
2917 */
a19f7fb0
DG
2918 spapr->initrd_base = (KERNEL_LOAD_ADDR + spapr->kernel_size
2919 + 0x1ffff) & ~0xffff;
2920 spapr->initrd_size = load_image_targphys(initrd_filename,
2921 spapr->initrd_base,
2922 load_limit
2923 - spapr->initrd_base);
2924 if (spapr->initrd_size < 0) {
d54e4d76
DG
2925 error_report("could not load initial ram disk '%s'",
2926 initrd_filename);
9fdf0c29
DG
2927 exit(1);
2928 }
9fdf0c29 2929 }
4d8d5467 2930 }
a3467baa 2931
8e7ea787
AF
2932 if (bios_name == NULL) {
2933 bios_name = FW_FILE_NAME;
2934 }
2935 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
4c56440d 2936 if (!filename) {
68fea5a0 2937 error_report("Could not find LPAR firmware '%s'", bios_name);
4c56440d
SW
2938 exit(1);
2939 }
4d8d5467 2940 fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
68fea5a0
TH
2941 if (fw_size <= 0) {
2942 error_report("Could not load LPAR firmware '%s'", filename);
4d8d5467
BH
2943 exit(1);
2944 }
2945 g_free(filename);
4d8d5467 2946
28e02042
DG
2947 /* FIXME: Should register things through the MachineState's qdev
2948 * interface, this is a legacy from the sPAPREnvironment structure
2949 * which predated MachineState but had a similar function */
4be21d56 2950 vmstate_register(NULL, 0, &vmstate_spapr, spapr);
ce62df53 2951 register_savevm_live("spapr/htab", -1, 1,
4be21d56
DG
2952 &savevm_htab_handlers, spapr);
2953
bb2bdd81
GK
2954 qbus_set_hotplug_handler(sysbus_get_default(), OBJECT(machine),
2955 &error_fatal);
2956
5b2128d2 2957 qemu_register_boot_set(spapr_boot_set, spapr);
42043e4f 2958
93eac7b8
NP
2959 /*
2960 * Nothing needs to be done to resume a suspended guest because
2961 * suspending does not change the machine state, so no need for
2962 * a ->wakeup method.
2963 */
2964 qemu_register_wakeup_support();
2965
42043e4f 2966 if (kvm_enabled()) {
3dc410ae 2967 /* to stop and start vmclock */
42043e4f
LV
2968 qemu_add_vm_change_state_handler(cpu_ppc_clock_vm_state_change,
2969 &spapr->tb);
3dc410ae
AK
2970
2971 kvmppc_spapr_enable_inkernel_multitce();
42043e4f 2972 }
9fdf0c29
DG
2973}
2974
dc0ca80e 2975static int spapr_kvm_type(MachineState *machine, const char *vm_type)
135a129a
AK
2976{
2977 if (!vm_type) {
2978 return 0;
2979 }
2980
2981 if (!strcmp(vm_type, "HV")) {
2982 return 1;
2983 }
2984
2985 if (!strcmp(vm_type, "PR")) {
2986 return 2;
2987 }
2988
2989 error_report("Unknown kvm-type specified '%s'", vm_type);
2990 exit(1);
2991}
2992
71461b0f 2993/*
627b84f4 2994 * Implementation of an interface to adjust firmware path
71461b0f
AK
2995 * for the bootindex property handling.
2996 */
2997static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
2998 DeviceState *dev)
2999{
3000#define CAST(type, obj, name) \
3001 ((type *)object_dynamic_cast(OBJECT(obj), (name)))
3002 SCSIDevice *d = CAST(SCSIDevice, dev, TYPE_SCSI_DEVICE);
ce2918cb 3003 SpaprPhbState *phb = CAST(SpaprPhbState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
c4e13492 3004 VHostSCSICommon *vsc = CAST(VHostSCSICommon, dev, TYPE_VHOST_SCSI_COMMON);
71461b0f
AK
3005
3006 if (d) {
3007 void *spapr = CAST(void, bus->parent, "spapr-vscsi");
3008 VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
3009 USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
3010
3011 if (spapr) {
3012 /*
3013 * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
1ac24c91
TH
3014 * In the top 16 bits of the 64-bit LUN, we use SRP luns of the form
3015 * 0x8000 | (target << 8) | (bus << 5) | lun
3016 * (see the "Logical unit addressing format" table in SAM5)
71461b0f 3017 */
1ac24c91 3018 unsigned id = 0x8000 | (d->id << 8) | (d->channel << 5) | d->lun;
71461b0f
AK
3019 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
3020 (uint64_t)id << 48);
3021 } else if (virtio) {
3022 /*
3023 * We use SRP luns of the form 01000000 | (target << 8) | lun
3024 * in the top 32 bits of the 64-bit LUN
3025 * Note: the quote above is from SLOF and it is wrong,
3026 * the actual binding is:
3027 * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
3028 */
3029 unsigned id = 0x1000000 | (d->id << 16) | d->lun;
bac658d1
TH
3030 if (d->lun >= 256) {
3031 /* Use the LUN "flat space addressing method" */
3032 id |= 0x4000;
3033 }
71461b0f
AK
3034 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
3035 (uint64_t)id << 32);
3036 } else if (usb) {
3037 /*
3038 * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
3039 * in the top 32 bits of the 64-bit LUN
3040 */
3041 unsigned usb_port = atoi(usb->port->path);
3042 unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
3043 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
3044 (uint64_t)id << 32);
3045 }
3046 }
3047
b99260eb
TH
3048 /*
3049 * SLOF probes the USB devices, and if it recognizes that the device is a
3050 * storage device, it changes its name to "storage" instead of "usb-host",
3051 * and additionally adds a child node for the SCSI LUN, so the correct
3052 * boot path in SLOF is something like .../storage@1/disk@xxx" instead.
3053 */
3054 if (strcmp("usb-host", qdev_fw_name(dev)) == 0) {
3055 USBDevice *usbdev = CAST(USBDevice, dev, TYPE_USB_DEVICE);
3056 if (usb_host_dev_is_scsi_storage(usbdev)) {
3057 return g_strdup_printf("storage@%s/disk", usbdev->port->path);
3058 }
3059 }
3060
71461b0f
AK
3061 if (phb) {
3062 /* Replace "pci" with "pci@800000020000000" */
3063 return g_strdup_printf("pci@%"PRIX64, phb->buid);
3064 }
3065
c4e13492
FF
3066 if (vsc) {
3067 /* Same logic as virtio above */
3068 unsigned id = 0x1000000 | (vsc->target << 16) | vsc->lun;
3069 return g_strdup_printf("disk@%"PRIX64, (uint64_t)id << 32);
3070 }
3071
4871dd4c
TH
3072 if (g_str_equal("pci-bridge", qdev_fw_name(dev))) {
3073 /* SLOF uses "pci" instead of "pci-bridge" for PCI bridges */
3074 PCIDevice *pcidev = CAST(PCIDevice, dev, TYPE_PCI_DEVICE);
3075 return g_strdup_printf("pci@%x", PCI_SLOT(pcidev->devfn));
3076 }
3077
71461b0f
AK
3078 return NULL;
3079}
3080
23825581
EH
3081static char *spapr_get_kvm_type(Object *obj, Error **errp)
3082{
ce2918cb 3083 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
23825581 3084
28e02042 3085 return g_strdup(spapr->kvm_type);
23825581
EH
3086}
3087
3088static void spapr_set_kvm_type(Object *obj, const char *value, Error **errp)
3089{
ce2918cb 3090 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
23825581 3091
28e02042
DG
3092 g_free(spapr->kvm_type);
3093 spapr->kvm_type = g_strdup(value);
23825581
EH
3094}
3095
f6229214
MR
3096static bool spapr_get_modern_hotplug_events(Object *obj, Error **errp)
3097{
ce2918cb 3098 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
f6229214
MR
3099
3100 return spapr->use_hotplug_event_source;
3101}
3102
3103static void spapr_set_modern_hotplug_events(Object *obj, bool value,
3104 Error **errp)
3105{
ce2918cb 3106 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
f6229214
MR
3107
3108 spapr->use_hotplug_event_source = value;
3109}
3110
fcad0d21
AK
3111static bool spapr_get_msix_emulation(Object *obj, Error **errp)
3112{
3113 return true;
3114}
3115
30f4b05b
DG
3116static char *spapr_get_resize_hpt(Object *obj, Error **errp)
3117{
ce2918cb 3118 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
30f4b05b
DG
3119
3120 switch (spapr->resize_hpt) {
3121 case SPAPR_RESIZE_HPT_DEFAULT:
3122 return g_strdup("default");
3123 case SPAPR_RESIZE_HPT_DISABLED:
3124 return g_strdup("disabled");
3125 case SPAPR_RESIZE_HPT_ENABLED:
3126 return g_strdup("enabled");
3127 case SPAPR_RESIZE_HPT_REQUIRED:
3128 return g_strdup("required");
3129 }
3130 g_assert_not_reached();
3131}
3132
3133static void spapr_set_resize_hpt(Object *obj, const char *value, Error **errp)
3134{
ce2918cb 3135 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
30f4b05b
DG
3136
3137 if (strcmp(value, "default") == 0) {
3138 spapr->resize_hpt = SPAPR_RESIZE_HPT_DEFAULT;
3139 } else if (strcmp(value, "disabled") == 0) {
3140 spapr->resize_hpt = SPAPR_RESIZE_HPT_DISABLED;
3141 } else if (strcmp(value, "enabled") == 0) {
3142 spapr->resize_hpt = SPAPR_RESIZE_HPT_ENABLED;
3143 } else if (strcmp(value, "required") == 0) {
3144 spapr->resize_hpt = SPAPR_RESIZE_HPT_REQUIRED;
3145 } else {
3146 error_setg(errp, "Bad value for \"resize-hpt\" property");
3147 }
3148}
3149
fa98fbfc
SB
3150static void spapr_get_vsmt(Object *obj, Visitor *v, const char *name,
3151 void *opaque, Error **errp)
3152{
3153 visit_type_uint32(v, name, (uint32_t *)opaque, errp);
3154}
3155
3156static void spapr_set_vsmt(Object *obj, Visitor *v, const char *name,
3157 void *opaque, Error **errp)
3158{
3159 visit_type_uint32(v, name, (uint32_t *)opaque, errp);
3160}
3161
3ba3d0bc
CLG
3162static char *spapr_get_ic_mode(Object *obj, Error **errp)
3163{
ce2918cb 3164 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3ba3d0bc
CLG
3165
3166 if (spapr->irq == &spapr_irq_xics_legacy) {
3167 return g_strdup("legacy");
3168 } else if (spapr->irq == &spapr_irq_xics) {
3169 return g_strdup("xics");
3170 } else if (spapr->irq == &spapr_irq_xive) {
3171 return g_strdup("xive");
13db0cd9
CLG
3172 } else if (spapr->irq == &spapr_irq_dual) {
3173 return g_strdup("dual");
3ba3d0bc
CLG
3174 }
3175 g_assert_not_reached();
3176}
3177
3178static void spapr_set_ic_mode(Object *obj, const char *value, Error **errp)
3179{
ce2918cb 3180 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3ba3d0bc 3181
21df5e4f
GK
3182 if (SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) {
3183 error_setg(errp, "This machine only uses the legacy XICS backend, don't pass ic-mode");
3184 return;
3185 }
3186
3ba3d0bc
CLG
3187 /* The legacy IRQ backend can not be set */
3188 if (strcmp(value, "xics") == 0) {
3189 spapr->irq = &spapr_irq_xics;
3190 } else if (strcmp(value, "xive") == 0) {
3191 spapr->irq = &spapr_irq_xive;
13db0cd9
CLG
3192 } else if (strcmp(value, "dual") == 0) {
3193 spapr->irq = &spapr_irq_dual;
3ba3d0bc
CLG
3194 } else {
3195 error_setg(errp, "Bad value for \"ic-mode\" property");
3196 }
3197}
3198
27461d69
PP
3199static char *spapr_get_host_model(Object *obj, Error **errp)
3200{
ce2918cb 3201 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
27461d69
PP
3202
3203 return g_strdup(spapr->host_model);
3204}
3205
3206static void spapr_set_host_model(Object *obj, const char *value, Error **errp)
3207{
ce2918cb 3208 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
27461d69
PP
3209
3210 g_free(spapr->host_model);
3211 spapr->host_model = g_strdup(value);
3212}
3213
3214static char *spapr_get_host_serial(Object *obj, Error **errp)
3215{
ce2918cb 3216 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
27461d69
PP
3217
3218 return g_strdup(spapr->host_serial);
3219}
3220
3221static void spapr_set_host_serial(Object *obj, const char *value, Error **errp)
3222{
ce2918cb 3223 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
27461d69
PP
3224
3225 g_free(spapr->host_serial);
3226 spapr->host_serial = g_strdup(value);
3227}
3228
bcb5ce08 3229static void spapr_instance_init(Object *obj)
23825581 3230{
ce2918cb
DG
3231 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
3232 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
715c5407
DG
3233
3234 spapr->htab_fd = -1;
f6229214 3235 spapr->use_hotplug_event_source = true;
23825581
EH
3236 object_property_add_str(obj, "kvm-type",
3237 spapr_get_kvm_type, spapr_set_kvm_type, NULL);
49d2e648
MA
3238 object_property_set_description(obj, "kvm-type",
3239 "Specifies the KVM virtualization mode (HV, PR)",
3240 NULL);
f6229214
MR
3241 object_property_add_bool(obj, "modern-hotplug-events",
3242 spapr_get_modern_hotplug_events,
3243 spapr_set_modern_hotplug_events,
3244 NULL);
3245 object_property_set_description(obj, "modern-hotplug-events",
3246 "Use dedicated hotplug event mechanism in"
3247 " place of standard EPOW events when possible"
3248 " (required for memory hot-unplug support)",
3249 NULL);
7843c0d6
DG
3250 ppc_compat_add_property(obj, "max-cpu-compat", &spapr->max_compat_pvr,
3251 "Maximum permitted CPU compatibility mode",
3252 &error_fatal);
30f4b05b
DG
3253
3254 object_property_add_str(obj, "resize-hpt",
3255 spapr_get_resize_hpt, spapr_set_resize_hpt, NULL);
3256 object_property_set_description(obj, "resize-hpt",
3257 "Resizing of the Hash Page Table (enabled, disabled, required)",
3258 NULL);
fa98fbfc
SB
3259 object_property_add(obj, "vsmt", "uint32", spapr_get_vsmt,
3260 spapr_set_vsmt, NULL, &spapr->vsmt, &error_abort);
3261 object_property_set_description(obj, "vsmt",
3262 "Virtual SMT: KVM behaves as if this were"
3263 " the host's SMT mode", &error_abort);
fcad0d21
AK
3264 object_property_add_bool(obj, "vfio-no-msix-emulation",
3265 spapr_get_msix_emulation, NULL, NULL);
3ba3d0bc
CLG
3266
3267 /* The machine class defines the default interrupt controller mode */
3268 spapr->irq = smc->irq;
3269 object_property_add_str(obj, "ic-mode", spapr_get_ic_mode,
3270 spapr_set_ic_mode, NULL);
3271 object_property_set_description(obj, "ic-mode",
13db0cd9 3272 "Specifies the interrupt controller mode (xics, xive, dual)",
3ba3d0bc 3273 NULL);
27461d69
PP
3274
3275 object_property_add_str(obj, "host-model",
3276 spapr_get_host_model, spapr_set_host_model,
3277 &error_abort);
3278 object_property_set_description(obj, "host-model",
0a794529 3279 "Host model to advertise in guest device tree", &error_abort);
27461d69
PP
3280 object_property_add_str(obj, "host-serial",
3281 spapr_get_host_serial, spapr_set_host_serial,
3282 &error_abort);
3283 object_property_set_description(obj, "host-serial",
0a794529 3284 "Host serial number to advertise in guest device tree", &error_abort);
23825581
EH
3285}
3286
87bbdd9c
DG
3287static void spapr_machine_finalizefn(Object *obj)
3288{
ce2918cb 3289 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
87bbdd9c
DG
3290
3291 g_free(spapr->kvm_type);
3292}
3293
1c7ad77e 3294void spapr_do_system_reset_on_cpu(CPUState *cs, run_on_cpu_data arg)
34316482 3295{
34316482
AK
3296 cpu_synchronize_state(cs);
3297 ppc_cpu_do_system_reset(cs);
3298}
3299
3300static void spapr_nmi(NMIState *n, int cpu_index, Error **errp)
3301{
3302 CPUState *cs;
3303
3304 CPU_FOREACH(cs) {
1c7ad77e 3305 async_run_on_cpu(cs, spapr_do_system_reset_on_cpu, RUN_ON_CPU_NULL);
34316482
AK
3306 }
3307}
3308
ce2918cb 3309int spapr_lmb_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
62d38c9b
GK
3310 void *fdt, int *fdt_start_offset, Error **errp)
3311{
3312 uint64_t addr;
3313 uint32_t node;
3314
3315 addr = spapr_drc_index(drc) * SPAPR_MEMORY_BLOCK_SIZE;
3316 node = object_property_get_uint(OBJECT(drc->dev), PC_DIMM_NODE_PROP,
3317 &error_abort);
3318 *fdt_start_offset = spapr_populate_memory_node(fdt, node, addr,
3319 SPAPR_MEMORY_BLOCK_SIZE);
3320 return 0;
3321}
3322
79b78a6b 3323static void spapr_add_lmbs(DeviceState *dev, uint64_t addr_start, uint64_t size,
62d38c9b 3324 bool dedicated_hp_event_source, Error **errp)
c20d332a 3325{
ce2918cb 3326 SpaprDrc *drc;
c20d332a 3327 uint32_t nr_lmbs = size/SPAPR_MEMORY_BLOCK_SIZE;
62d38c9b 3328 int i;
79b78a6b 3329 uint64_t addr = addr_start;
94fd9cba 3330 bool hotplugged = spapr_drc_hotplugged(dev);
160bb678 3331 Error *local_err = NULL;
c20d332a 3332
c20d332a 3333 for (i = 0; i < nr_lmbs; i++) {
fbf55397
DG
3334 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3335 addr / SPAPR_MEMORY_BLOCK_SIZE);
c20d332a
BR
3336 g_assert(drc);
3337
09d876ce 3338 spapr_drc_attach(drc, dev, &local_err);
160bb678
GK
3339 if (local_err) {
3340 while (addr > addr_start) {
3341 addr -= SPAPR_MEMORY_BLOCK_SIZE;
3342 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3343 addr / SPAPR_MEMORY_BLOCK_SIZE);
a8dc47fd 3344 spapr_drc_detach(drc);
160bb678 3345 }
160bb678
GK
3346 error_propagate(errp, local_err);
3347 return;
3348 }
94fd9cba
LV
3349 if (!hotplugged) {
3350 spapr_drc_reset(drc);
3351 }
c20d332a
BR
3352 addr += SPAPR_MEMORY_BLOCK_SIZE;
3353 }
5dd5238c
JD
3354 /* send hotplug notification to the
3355 * guest only in case of hotplugged memory
3356 */
94fd9cba 3357 if (hotplugged) {
79b78a6b 3358 if (dedicated_hp_event_source) {
fbf55397
DG
3359 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3360 addr_start / SPAPR_MEMORY_BLOCK_SIZE);
79b78a6b
MR
3361 spapr_hotplug_req_add_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
3362 nr_lmbs,
0b55aa91 3363 spapr_drc_index(drc));
79b78a6b
MR
3364 } else {
3365 spapr_hotplug_req_add_by_count(SPAPR_DR_CONNECTOR_TYPE_LMB,
3366 nr_lmbs);
3367 }
5dd5238c 3368 }
c20d332a
BR
3369}
3370
3371static void spapr_memory_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
81985f3b 3372 Error **errp)
c20d332a
BR
3373{
3374 Error *local_err = NULL;
ce2918cb 3375 SpaprMachineState *ms = SPAPR_MACHINE(hotplug_dev);
c20d332a 3376 PCDIMMDevice *dimm = PC_DIMM(dev);
b0e62443 3377 uint64_t size, addr;
04790978 3378
946d6154 3379 size = memory_device_get_region_size(MEMORY_DEVICE(dev), &error_abort);
df587133 3380
fd3416f5 3381 pc_dimm_plug(dimm, MACHINE(ms), &local_err);
c20d332a
BR
3382 if (local_err) {
3383 goto out;
3384 }
3385
9ed442b8
MAL
3386 addr = object_property_get_uint(OBJECT(dimm),
3387 PC_DIMM_ADDR_PROP, &local_err);
c20d332a 3388 if (local_err) {
160bb678 3389 goto out_unplug;
c20d332a
BR
3390 }
3391
62d38c9b 3392 spapr_add_lmbs(dev, addr, size, spapr_ovec_test(ms->ov5_cas, OV5_HP_EVT),
160bb678
GK
3393 &local_err);
3394 if (local_err) {
3395 goto out_unplug;
3396 }
3397
3398 return;
c20d332a 3399
160bb678 3400out_unplug:
fd3416f5 3401 pc_dimm_unplug(dimm, MACHINE(ms));
c20d332a
BR
3402out:
3403 error_propagate(errp, local_err);
3404}
3405
c871bc70
LV
3406static void spapr_memory_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3407 Error **errp)
3408{
ce2918cb
DG
3409 const SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(hotplug_dev);
3410 SpaprMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
c871bc70 3411 PCDIMMDevice *dimm = PC_DIMM(dev);
8f1ffe5b 3412 Error *local_err = NULL;
04790978 3413 uint64_t size;
123eec65
DG
3414 Object *memdev;
3415 hwaddr pagesize;
c871bc70 3416
4e8a01bd
DH
3417 if (!smc->dr_lmb_enabled) {
3418 error_setg(errp, "Memory hotplug not supported for this machine");
3419 return;
3420 }
3421
946d6154
DH
3422 size = memory_device_get_region_size(MEMORY_DEVICE(dimm), &local_err);
3423 if (local_err) {
3424 error_propagate(errp, local_err);
04790978
TH
3425 return;
3426 }
04790978 3427
c871bc70
LV
3428 if (size % SPAPR_MEMORY_BLOCK_SIZE) {
3429 error_setg(errp, "Hotplugged memory size must be a multiple of "
ab3dd749 3430 "%" PRIu64 " MB", SPAPR_MEMORY_BLOCK_SIZE / MiB);
c871bc70
LV
3431 return;
3432 }
3433
123eec65
DG
3434 memdev = object_property_get_link(OBJECT(dimm), PC_DIMM_MEMDEV_PROP,
3435 &error_abort);
3436 pagesize = host_memory_backend_pagesize(MEMORY_BACKEND(memdev));
8f1ffe5b
DH
3437 spapr_check_pagesize(spapr, pagesize, &local_err);
3438 if (local_err) {
3439 error_propagate(errp, local_err);
3440 return;
3441 }
3442
fd3416f5 3443 pc_dimm_pre_plug(dimm, MACHINE(hotplug_dev), NULL, errp);
c871bc70
LV
3444}
3445
ce2918cb 3446struct SpaprDimmState {
0cffce56 3447 PCDIMMDevice *dimm;
cf632463 3448 uint32_t nr_lmbs;
ce2918cb 3449 QTAILQ_ENTRY(SpaprDimmState) next;
0cffce56
DG
3450};
3451
ce2918cb 3452static SpaprDimmState *spapr_pending_dimm_unplugs_find(SpaprMachineState *s,
0cffce56
DG
3453 PCDIMMDevice *dimm)
3454{
ce2918cb 3455 SpaprDimmState *dimm_state = NULL;
0cffce56
DG
3456
3457 QTAILQ_FOREACH(dimm_state, &s->pending_dimm_unplugs, next) {
3458 if (dimm_state->dimm == dimm) {
3459 break;
3460 }
3461 }
3462 return dimm_state;
3463}
3464
ce2918cb 3465static SpaprDimmState *spapr_pending_dimm_unplugs_add(SpaprMachineState *spapr,
8d5981c4
BR
3466 uint32_t nr_lmbs,
3467 PCDIMMDevice *dimm)
0cffce56 3468{
ce2918cb 3469 SpaprDimmState *ds = NULL;
8d5981c4
BR
3470
3471 /*
3472 * If this request is for a DIMM whose removal had failed earlier
3473 * (due to guest's refusal to remove the LMBs), we would have this
3474 * dimm already in the pending_dimm_unplugs list. In that
3475 * case don't add again.
3476 */
3477 ds = spapr_pending_dimm_unplugs_find(spapr, dimm);
3478 if (!ds) {
ce2918cb 3479 ds = g_malloc0(sizeof(SpaprDimmState));
8d5981c4
BR
3480 ds->nr_lmbs = nr_lmbs;
3481 ds->dimm = dimm;
3482 QTAILQ_INSERT_HEAD(&spapr->pending_dimm_unplugs, ds, next);
3483 }
3484 return ds;
0cffce56
DG
3485}
3486
ce2918cb
DG
3487static void spapr_pending_dimm_unplugs_remove(SpaprMachineState *spapr,
3488 SpaprDimmState *dimm_state)
0cffce56
DG
3489{
3490 QTAILQ_REMOVE(&spapr->pending_dimm_unplugs, dimm_state, next);
3491 g_free(dimm_state);
3492}
cf632463 3493
ce2918cb 3494static SpaprDimmState *spapr_recover_pending_dimm_state(SpaprMachineState *ms,
16ee9980
DHB
3495 PCDIMMDevice *dimm)
3496{
ce2918cb 3497 SpaprDrc *drc;
946d6154
DH
3498 uint64_t size = memory_device_get_region_size(MEMORY_DEVICE(dimm),
3499 &error_abort);
16ee9980
DHB
3500 uint32_t nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
3501 uint32_t avail_lmbs = 0;
3502 uint64_t addr_start, addr;
3503 int i;
16ee9980
DHB
3504
3505 addr_start = object_property_get_int(OBJECT(dimm), PC_DIMM_ADDR_PROP,
3506 &error_abort);
3507
3508 addr = addr_start;
3509 for (i = 0; i < nr_lmbs; i++) {
fbf55397
DG
3510 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3511 addr / SPAPR_MEMORY_BLOCK_SIZE);
16ee9980 3512 g_assert(drc);
454b580a 3513 if (drc->dev) {
16ee9980
DHB
3514 avail_lmbs++;
3515 }
3516 addr += SPAPR_MEMORY_BLOCK_SIZE;
3517 }
3518
8d5981c4 3519 return spapr_pending_dimm_unplugs_add(ms, avail_lmbs, dimm);
16ee9980
DHB
3520}
3521
31834723
DHB
3522/* Callback to be called during DRC release. */
3523void spapr_lmb_release(DeviceState *dev)
cf632463 3524{
3ec71474 3525 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
ce2918cb
DG
3526 SpaprMachineState *spapr = SPAPR_MACHINE(hotplug_ctrl);
3527 SpaprDimmState *ds = spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(dev));
cf632463 3528
16ee9980
DHB
3529 /* This information will get lost if a migration occurs
3530 * during the unplug process. In this case recover it. */
3531 if (ds == NULL) {
3532 ds = spapr_recover_pending_dimm_state(spapr, PC_DIMM(dev));
8d5981c4 3533 g_assert(ds);
454b580a
DG
3534 /* The DRC being examined by the caller at least must be counted */
3535 g_assert(ds->nr_lmbs);
3536 }
3537
3538 if (--ds->nr_lmbs) {
cf632463
BR
3539 return;
3540 }
3541
cf632463
BR
3542 /*
3543 * Now that all the LMBs have been removed by the guest, call the
3ec71474 3544 * unplug handler chain. This can never fail.
cf632463 3545 */
3ec71474 3546 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
07578b0a 3547 object_unparent(OBJECT(dev));
3ec71474
DH
3548}
3549
3550static void spapr_memory_unplug(HotplugHandler *hotplug_dev, DeviceState *dev)
3551{
ce2918cb
DG
3552 SpaprMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
3553 SpaprDimmState *ds = spapr_pending_dimm_unplugs_find(spapr, PC_DIMM(dev));
3ec71474 3554
fd3416f5 3555 pc_dimm_unplug(PC_DIMM(dev), MACHINE(hotplug_dev));
07578b0a 3556 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
2a129767 3557 spapr_pending_dimm_unplugs_remove(spapr, ds);
cf632463
BR
3558}
3559
3560static void spapr_memory_unplug_request(HotplugHandler *hotplug_dev,
3561 DeviceState *dev, Error **errp)
3562{
ce2918cb 3563 SpaprMachineState *spapr = SPAPR_MACHINE(hotplug_dev);
cf632463
BR
3564 Error *local_err = NULL;
3565 PCDIMMDevice *dimm = PC_DIMM(dev);
04790978
TH
3566 uint32_t nr_lmbs;
3567 uint64_t size, addr_start, addr;
0cffce56 3568 int i;
ce2918cb 3569 SpaprDrc *drc;
04790978 3570
946d6154 3571 size = memory_device_get_region_size(MEMORY_DEVICE(dimm), &error_abort);
04790978
TH
3572 nr_lmbs = size / SPAPR_MEMORY_BLOCK_SIZE;
3573
9ed442b8 3574 addr_start = object_property_get_uint(OBJECT(dimm), PC_DIMM_ADDR_PROP,
0cffce56 3575 &local_err);
cf632463
BR
3576 if (local_err) {
3577 goto out;
3578 }
3579
2a129767
DHB
3580 /*
3581 * An existing pending dimm state for this DIMM means that there is an
3582 * unplug operation in progress, waiting for the spapr_lmb_release
3583 * callback to complete the job (BQL can't cover that far). In this case,
3584 * bail out to avoid detaching DRCs that were already released.
3585 */
3586 if (spapr_pending_dimm_unplugs_find(spapr, dimm)) {
3587 error_setg(&local_err,
3588 "Memory unplug already in progress for device %s",
3589 dev->id);
3590 goto out;
3591 }
3592
8d5981c4 3593 spapr_pending_dimm_unplugs_add(spapr, nr_lmbs, dimm);
0cffce56
DG
3594
3595 addr = addr_start;
3596 for (i = 0; i < nr_lmbs; i++) {
fbf55397
DG
3597 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3598 addr / SPAPR_MEMORY_BLOCK_SIZE);
0cffce56
DG
3599 g_assert(drc);
3600
a8dc47fd 3601 spapr_drc_detach(drc);
0cffce56
DG
3602 addr += SPAPR_MEMORY_BLOCK_SIZE;
3603 }
3604
fbf55397
DG
3605 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_LMB,
3606 addr_start / SPAPR_MEMORY_BLOCK_SIZE);
0cffce56 3607 spapr_hotplug_req_remove_by_count_indexed(SPAPR_DR_CONNECTOR_TYPE_LMB,
0b55aa91 3608 nr_lmbs, spapr_drc_index(drc));
cf632463
BR
3609out:
3610 error_propagate(errp, local_err);
3611}
3612
765d1bdd
DG
3613/* Callback to be called during DRC release. */
3614void spapr_core_release(DeviceState *dev)
ff9006dd 3615{
a4261be1
DH
3616 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
3617
3618 /* Call the unplug handler chain. This can never fail. */
3619 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
07578b0a 3620 object_unparent(OBJECT(dev));
a4261be1
DH
3621}
3622
3623static void spapr_core_unplug(HotplugHandler *hotplug_dev, DeviceState *dev)
3624{
3625 MachineState *ms = MACHINE(hotplug_dev);
ce2918cb 3626 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(ms);
ff9006dd 3627 CPUCore *cc = CPU_CORE(dev);
535455fd 3628 CPUArchId *core_slot = spapr_find_cpu_slot(ms, cc->core_id, NULL);
ff9006dd 3629
46f7afa3 3630 if (smc->pre_2_10_has_unused_icps) {
ce2918cb 3631 SpaprCpuCore *sc = SPAPR_CPU_CORE(OBJECT(dev));
46f7afa3
GK
3632 int i;
3633
3634 for (i = 0; i < cc->nr_threads; i++) {
94ad93bd 3635 CPUState *cs = CPU(sc->threads[i]);
46f7afa3
GK
3636
3637 pre_2_10_vmstate_register_dummy_icp(cs->cpu_index);
3638 }
3639 }
3640
07572c06 3641 assert(core_slot);
535455fd 3642 core_slot->cpu = NULL;
07578b0a 3643 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
ff9006dd
IM
3644}
3645
115debf2
IM
3646static
3647void spapr_core_unplug_request(HotplugHandler *hotplug_dev, DeviceState *dev,
3648 Error **errp)
ff9006dd 3649{
ce2918cb 3650 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
535455fd 3651 int index;
ce2918cb 3652 SpaprDrc *drc;
535455fd 3653 CPUCore *cc = CPU_CORE(dev);
ff9006dd 3654
535455fd
IM
3655 if (!spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index)) {
3656 error_setg(errp, "Unable to find CPU core with core-id: %d",
3657 cc->core_id);
3658 return;
3659 }
ff9006dd
IM
3660 if (index == 0) {
3661 error_setg(errp, "Boot CPU core may not be unplugged");
3662 return;
3663 }
3664
5d0fb150
GK
3665 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU,
3666 spapr_vcpu_id(spapr, cc->core_id));
ff9006dd
IM
3667 g_assert(drc);
3668
47c8c915
GK
3669 if (!spapr_drc_unplug_requested(drc)) {
3670 spapr_drc_detach(drc);
3671 spapr_hotplug_req_remove_by_index(drc);
3672 }
ff9006dd
IM
3673}
3674
ce2918cb 3675int spapr_core_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
345b12b9
GK
3676 void *fdt, int *fdt_start_offset, Error **errp)
3677{
ce2918cb 3678 SpaprCpuCore *core = SPAPR_CPU_CORE(drc->dev);
345b12b9
GK
3679 CPUState *cs = CPU(core->threads[0]);
3680 PowerPCCPU *cpu = POWERPC_CPU(cs);
3681 DeviceClass *dc = DEVICE_GET_CLASS(cs);
3682 int id = spapr_get_vcpu_id(cpu);
3683 char *nodename;
3684 int offset;
3685
3686 nodename = g_strdup_printf("%s@%x", dc->fw_name, id);
3687 offset = fdt_add_subnode(fdt, 0, nodename);
3688 g_free(nodename);
3689
3690 spapr_populate_cpu_dt(cs, fdt, offset, spapr);
3691
3692 *fdt_start_offset = offset;
3693 return 0;
3694}
3695
ff9006dd
IM
3696static void spapr_core_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3697 Error **errp)
3698{
ce2918cb 3699 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
ff9006dd 3700 MachineClass *mc = MACHINE_GET_CLASS(spapr);
ce2918cb
DG
3701 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
3702 SpaprCpuCore *core = SPAPR_CPU_CORE(OBJECT(dev));
ff9006dd 3703 CPUCore *cc = CPU_CORE(dev);
345b12b9 3704 CPUState *cs;
ce2918cb 3705 SpaprDrc *drc;
ff9006dd 3706 Error *local_err = NULL;
535455fd
IM
3707 CPUArchId *core_slot;
3708 int index;
94fd9cba 3709 bool hotplugged = spapr_drc_hotplugged(dev);
b1e81567 3710 int i;
ff9006dd 3711
535455fd
IM
3712 core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3713 if (!core_slot) {
3714 error_setg(errp, "Unable to find CPU core with core-id: %d",
3715 cc->core_id);
3716 return;
3717 }
5d0fb150
GK
3718 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_CPU,
3719 spapr_vcpu_id(spapr, cc->core_id));
ff9006dd 3720
c5514d0e 3721 g_assert(drc || !mc->has_hotpluggable_cpus);
ff9006dd 3722
ff9006dd 3723 if (drc) {
09d876ce 3724 spapr_drc_attach(drc, dev, &local_err);
ff9006dd 3725 if (local_err) {
ff9006dd
IM
3726 error_propagate(errp, local_err);
3727 return;
3728 }
ff9006dd 3729
94fd9cba
LV
3730 if (hotplugged) {
3731 /*
3732 * Send hotplug notification interrupt to the guest only
3733 * in case of hotplugged CPUs.
3734 */
3735 spapr_hotplug_req_add_by_index(drc);
3736 } else {
3737 spapr_drc_reset(drc);
3738 }
ff9006dd 3739 }
94fd9cba 3740
535455fd 3741 core_slot->cpu = OBJECT(dev);
46f7afa3
GK
3742
3743 if (smc->pre_2_10_has_unused_icps) {
46f7afa3 3744 for (i = 0; i < cc->nr_threads; i++) {
bc877283 3745 cs = CPU(core->threads[i]);
46f7afa3
GK
3746 pre_2_10_vmstate_unregister_dummy_icp(cs->cpu_index);
3747 }
3748 }
b1e81567
GK
3749
3750 /*
3751 * Set compatibility mode to match the boot CPU, which was either set
3752 * by the machine reset code or by CAS.
3753 */
3754 if (hotplugged) {
3755 for (i = 0; i < cc->nr_threads; i++) {
3756 ppc_set_compat(core->threads[i], POWERPC_CPU(first_cpu)->compat_pvr,
3757 &local_err);
3758 if (local_err) {
3759 error_propagate(errp, local_err);
3760 return;
3761 }
3762 }
3763 }
ff9006dd
IM
3764}
3765
3766static void spapr_core_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3767 Error **errp)
3768{
3769 MachineState *machine = MACHINE(OBJECT(hotplug_dev));
3770 MachineClass *mc = MACHINE_GET_CLASS(hotplug_dev);
ff9006dd
IM
3771 Error *local_err = NULL;
3772 CPUCore *cc = CPU_CORE(dev);
2e9c10eb 3773 const char *base_core_type = spapr_get_cpu_core_type(machine->cpu_type);
ff9006dd 3774 const char *type = object_get_typename(OBJECT(dev));
535455fd
IM
3775 CPUArchId *core_slot;
3776 int index;
fe6b6346 3777 unsigned int smp_threads = machine->smp.threads;
ff9006dd 3778
c5514d0e 3779 if (dev->hotplugged && !mc->has_hotpluggable_cpus) {
ff9006dd
IM
3780 error_setg(&local_err, "CPU hotplug not supported for this machine");
3781 goto out;
3782 }
3783
3784 if (strcmp(base_core_type, type)) {
3785 error_setg(&local_err, "CPU core type should be %s", base_core_type);
3786 goto out;
3787 }
3788
3789 if (cc->core_id % smp_threads) {
3790 error_setg(&local_err, "invalid core id %d", cc->core_id);
3791 goto out;
3792 }
3793
459264ef
DG
3794 /*
3795 * In general we should have homogeneous threads-per-core, but old
3796 * (pre hotplug support) machine types allow the last core to have
3797 * reduced threads as a compatibility hack for when we allowed
3798 * total vcpus not a multiple of threads-per-core.
3799 */
3800 if (mc->has_hotpluggable_cpus && (cc->nr_threads != smp_threads)) {
df8658de 3801 error_setg(&local_err, "invalid nr-threads %d, must be %d",
8149e299 3802 cc->nr_threads, smp_threads);
df8658de 3803 goto out;
8149e299
DG
3804 }
3805
535455fd
IM
3806 core_slot = spapr_find_cpu_slot(MACHINE(hotplug_dev), cc->core_id, &index);
3807 if (!core_slot) {
ff9006dd
IM
3808 error_setg(&local_err, "core id %d out of range", cc->core_id);
3809 goto out;
3810 }
3811
535455fd 3812 if (core_slot->cpu) {
ff9006dd
IM
3813 error_setg(&local_err, "core %d already populated", cc->core_id);
3814 goto out;
3815 }
3816
a0ceb640 3817 numa_cpu_pre_plug(core_slot, dev, &local_err);
0b8497f0 3818
ff9006dd 3819out:
ff9006dd
IM
3820 error_propagate(errp, local_err);
3821}
3822
ce2918cb 3823int spapr_phb_dt_populate(SpaprDrc *drc, SpaprMachineState *spapr,
bb2bdd81
GK
3824 void *fdt, int *fdt_start_offset, Error **errp)
3825{
ce2918cb 3826 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(drc->dev);
bb2bdd81
GK
3827 int intc_phandle;
3828
3829 intc_phandle = spapr_irq_get_phandle(spapr, spapr->fdt_blob, errp);
3830 if (intc_phandle <= 0) {
3831 return -1;
3832 }
3833
8cbe71ec 3834 if (spapr_dt_phb(spapr, sphb, intc_phandle, fdt, fdt_start_offset)) {
bb2bdd81
GK
3835 error_setg(errp, "unable to create FDT node for PHB %d", sphb->index);
3836 return -1;
3837 }
3838
3839 /* generally SLOF creates these, for hotplug it's up to QEMU */
3840 _FDT(fdt_setprop_string(fdt, *fdt_start_offset, "name", "pci"));
3841
3842 return 0;
3843}
3844
3845static void spapr_phb_pre_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3846 Error **errp)
3847{
ce2918cb
DG
3848 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3849 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(dev);
3850 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
bb2bdd81
GK
3851 const unsigned windows_supported = spapr_phb_windows_supported(sphb);
3852
3853 if (dev->hotplugged && !smc->dr_phb_enabled) {
3854 error_setg(errp, "PHB hotplug not supported for this machine");
3855 return;
3856 }
3857
3858 if (sphb->index == (uint32_t)-1) {
3859 error_setg(errp, "\"index\" for PAPR PHB is mandatory");
3860 return;
3861 }
3862
3863 /*
3864 * This will check that sphb->index doesn't exceed the maximum number of
3865 * PHBs for the current machine type.
3866 */
3867 smc->phb_placement(spapr, sphb->index,
3868 &sphb->buid, &sphb->io_win_addr,
3869 &sphb->mem_win_addr, &sphb->mem64_win_addr,
ec132efa
AK
3870 windows_supported, sphb->dma_liobn,
3871 &sphb->nv2_gpa_win_addr, &sphb->nv2_atsd_win_addr,
3872 errp);
bb2bdd81
GK
3873}
3874
3875static void spapr_phb_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3876 Error **errp)
3877{
ce2918cb
DG
3878 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3879 SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
3880 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(dev);
3881 SpaprDrc *drc;
bb2bdd81
GK
3882 bool hotplugged = spapr_drc_hotplugged(dev);
3883 Error *local_err = NULL;
3884
3885 if (!smc->dr_phb_enabled) {
3886 return;
3887 }
3888
3889 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PHB, sphb->index);
3890 /* hotplug hooks should check it's enabled before getting this far */
3891 assert(drc);
3892
3893 spapr_drc_attach(drc, DEVICE(dev), &local_err);
3894 if (local_err) {
3895 error_propagate(errp, local_err);
3896 return;
3897 }
3898
3899 if (hotplugged) {
3900 spapr_hotplug_req_add_by_index(drc);
3901 } else {
3902 spapr_drc_reset(drc);
3903 }
3904}
3905
3906void spapr_phb_release(DeviceState *dev)
3907{
3908 HotplugHandler *hotplug_ctrl = qdev_get_hotplug_handler(dev);
3909
3910 hotplug_handler_unplug(hotplug_ctrl, dev, &error_abort);
07578b0a 3911 object_unparent(OBJECT(dev));
bb2bdd81
GK
3912}
3913
3914static void spapr_phb_unplug(HotplugHandler *hotplug_dev, DeviceState *dev)
3915{
07578b0a 3916 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
bb2bdd81
GK
3917}
3918
3919static void spapr_phb_unplug_request(HotplugHandler *hotplug_dev,
3920 DeviceState *dev, Error **errp)
3921{
ce2918cb
DG
3922 SpaprPhbState *sphb = SPAPR_PCI_HOST_BRIDGE(dev);
3923 SpaprDrc *drc;
bb2bdd81
GK
3924
3925 drc = spapr_drc_by_id(TYPE_SPAPR_DRC_PHB, sphb->index);
3926 assert(drc);
3927
3928 if (!spapr_drc_unplug_requested(drc)) {
3929 spapr_drc_detach(drc);
3930 spapr_hotplug_req_remove_by_index(drc);
3931 }
3932}
3933
0fb6bd07
MR
3934static void spapr_tpm_proxy_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
3935 Error **errp)
3936{
3937 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3938 SpaprTpmProxy *tpm_proxy = SPAPR_TPM_PROXY(dev);
3939
3940 if (spapr->tpm_proxy != NULL) {
3941 error_setg(errp, "Only one TPM proxy can be specified for this machine");
3942 return;
3943 }
3944
3945 spapr->tpm_proxy = tpm_proxy;
3946}
3947
3948static void spapr_tpm_proxy_unplug(HotplugHandler *hotplug_dev, DeviceState *dev)
3949{
3950 SpaprMachineState *spapr = SPAPR_MACHINE(OBJECT(hotplug_dev));
3951
3952 object_property_set_bool(OBJECT(dev), false, "realized", NULL);
3953 object_unparent(OBJECT(dev));
3954 spapr->tpm_proxy = NULL;
3955}
3956
c20d332a
BR
3957static void spapr_machine_device_plug(HotplugHandler *hotplug_dev,
3958 DeviceState *dev, Error **errp)
3959{
c20d332a 3960 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
81985f3b 3961 spapr_memory_plug(hotplug_dev, dev, errp);
af81cf32
BR
3962 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3963 spapr_core_plug(hotplug_dev, dev, errp);
bb2bdd81
GK
3964 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
3965 spapr_phb_plug(hotplug_dev, dev, errp);
0fb6bd07
MR
3966 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_TPM_PROXY)) {
3967 spapr_tpm_proxy_plug(hotplug_dev, dev, errp);
c20d332a
BR
3968 }
3969}
3970
88432f44
DH
3971static void spapr_machine_device_unplug(HotplugHandler *hotplug_dev,
3972 DeviceState *dev, Error **errp)
3973{
3ec71474
DH
3974 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3975 spapr_memory_unplug(hotplug_dev, dev);
a4261be1
DH
3976 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
3977 spapr_core_unplug(hotplug_dev, dev);
bb2bdd81
GK
3978 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
3979 spapr_phb_unplug(hotplug_dev, dev);
0fb6bd07
MR
3980 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_TPM_PROXY)) {
3981 spapr_tpm_proxy_unplug(hotplug_dev, dev);
3ec71474 3982 }
88432f44
DH
3983}
3984
cf632463
BR
3985static void spapr_machine_device_unplug_request(HotplugHandler *hotplug_dev,
3986 DeviceState *dev, Error **errp)
3987{
ce2918cb 3988 SpaprMachineState *sms = SPAPR_MACHINE(OBJECT(hotplug_dev));
c86c1aff 3989 MachineClass *mc = MACHINE_GET_CLASS(sms);
ce2918cb 3990 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
cf632463
BR
3991
3992 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
3993 if (spapr_ovec_test(sms->ov5_cas, OV5_HP_EVT)) {
3994 spapr_memory_unplug_request(hotplug_dev, dev, errp);
3995 } else {
3996 /* NOTE: this means there is a window after guest reset, prior to
3997 * CAS negotiation, where unplug requests will fail due to the
3998 * capability not being detected yet. This is a bit different than
3999 * the case with PCI unplug, where the events will be queued and
4000 * eventually handled by the guest after boot
4001 */
4002 error_setg(errp, "Memory hot unplug not supported for this guest");
4003 }
6f4b5c3e 4004 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
c5514d0e 4005 if (!mc->has_hotpluggable_cpus) {
6f4b5c3e
BR
4006 error_setg(errp, "CPU hot unplug not supported on this machine");
4007 return;
4008 }
115debf2 4009 spapr_core_unplug_request(hotplug_dev, dev, errp);
bb2bdd81
GK
4010 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
4011 if (!smc->dr_phb_enabled) {
4012 error_setg(errp, "PHB hot unplug not supported on this machine");
4013 return;
4014 }
4015 spapr_phb_unplug_request(hotplug_dev, dev, errp);
0fb6bd07
MR
4016 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_TPM_PROXY)) {
4017 spapr_tpm_proxy_unplug(hotplug_dev, dev);
c20d332a
BR
4018 }
4019}
4020
94a94e4c
BR
4021static void spapr_machine_device_pre_plug(HotplugHandler *hotplug_dev,
4022 DeviceState *dev, Error **errp)
4023{
c871bc70
LV
4024 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM)) {
4025 spapr_memory_pre_plug(hotplug_dev, dev, errp);
4026 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE)) {
94a94e4c 4027 spapr_core_pre_plug(hotplug_dev, dev, errp);
bb2bdd81
GK
4028 } else if (object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE)) {
4029 spapr_phb_pre_plug(hotplug_dev, dev, errp);
94a94e4c
BR
4030 }
4031}
4032
7ebaf795
BR
4033static HotplugHandler *spapr_get_hotplug_handler(MachineState *machine,
4034 DeviceState *dev)
c20d332a 4035{
94a94e4c 4036 if (object_dynamic_cast(OBJECT(dev), TYPE_PC_DIMM) ||
bb2bdd81 4037 object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_CPU_CORE) ||
0fb6bd07
MR
4038 object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_PCI_HOST_BRIDGE) ||
4039 object_dynamic_cast(OBJECT(dev), TYPE_SPAPR_TPM_PROXY)) {
c20d332a
BR
4040 return HOTPLUG_HANDLER(machine);
4041 }
cb600087
DG
4042 if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) {
4043 PCIDevice *pcidev = PCI_DEVICE(dev);
4044 PCIBus *root = pci_device_root_bus(pcidev);
4045 SpaprPhbState *phb =
4046 (SpaprPhbState *)object_dynamic_cast(OBJECT(BUS(root)->parent),
4047 TYPE_SPAPR_PCI_HOST_BRIDGE);
4048
4049 if (phb) {
4050 return HOTPLUG_HANDLER(phb);
4051 }
4052 }
c20d332a
BR
4053 return NULL;
4054}
4055
ea089eeb
IM
4056static CpuInstanceProperties
4057spapr_cpu_index_to_props(MachineState *machine, unsigned cpu_index)
20bb648d 4058{
ea089eeb
IM
4059 CPUArchId *core_slot;
4060 MachineClass *mc = MACHINE_GET_CLASS(machine);
4061
4062 /* make sure possible_cpu are intialized */
4063 mc->possible_cpu_arch_ids(machine);
4064 /* get CPU core slot containing thread that matches cpu_index */
4065 core_slot = spapr_find_cpu_slot(machine, cpu_index, NULL);
4066 assert(core_slot);
4067 return core_slot->props;
20bb648d
DG
4068}
4069
79e07936
IM
4070static int64_t spapr_get_default_cpu_node_id(const MachineState *ms, int idx)
4071{
aa570207 4072 return idx / ms->smp.cores % ms->numa_state->num_nodes;
79e07936
IM
4073}
4074
535455fd
IM
4075static const CPUArchIdList *spapr_possible_cpu_arch_ids(MachineState *machine)
4076{
4077 int i;
fe6b6346
LX
4078 unsigned int smp_threads = machine->smp.threads;
4079 unsigned int smp_cpus = machine->smp.cpus;
d342eb76 4080 const char *core_type;
fe6b6346 4081 int spapr_max_cores = machine->smp.max_cpus / smp_threads;
535455fd
IM
4082 MachineClass *mc = MACHINE_GET_CLASS(machine);
4083
c5514d0e 4084 if (!mc->has_hotpluggable_cpus) {
535455fd
IM
4085 spapr_max_cores = QEMU_ALIGN_UP(smp_cpus, smp_threads) / smp_threads;
4086 }
4087 if (machine->possible_cpus) {
4088 assert(machine->possible_cpus->len == spapr_max_cores);
4089 return machine->possible_cpus;
4090 }
4091
d342eb76
IM
4092 core_type = spapr_get_cpu_core_type(machine->cpu_type);
4093 if (!core_type) {
4094 error_report("Unable to find sPAPR CPU Core definition");
4095 exit(1);
4096 }
4097
535455fd
IM
4098 machine->possible_cpus = g_malloc0(sizeof(CPUArchIdList) +
4099 sizeof(CPUArchId) * spapr_max_cores);
4100 machine->possible_cpus->len = spapr_max_cores;
4101 for (i = 0; i < machine->possible_cpus->len; i++) {
4102 int core_id = i * smp_threads;
4103
d342eb76 4104 machine->possible_cpus->cpus[i].type = core_type;
f2d672c2 4105 machine->possible_cpus->cpus[i].vcpus_count = smp_threads;
535455fd
IM
4106 machine->possible_cpus->cpus[i].arch_id = core_id;
4107 machine->possible_cpus->cpus[i].props.has_core_id = true;
4108 machine->possible_cpus->cpus[i].props.core_id = core_id;
535455fd
IM
4109 }
4110 return machine->possible_cpus;
4111}
4112
ce2918cb 4113static void spapr_phb_placement(SpaprMachineState *spapr, uint32_t index,
daa23699
DG
4114 uint64_t *buid, hwaddr *pio,
4115 hwaddr *mmio32, hwaddr *mmio64,
ec132efa
AK
4116 unsigned n_dma, uint32_t *liobns,
4117 hwaddr *nv2gpa, hwaddr *nv2atsd, Error **errp)
6737d9ad 4118{
357d1e3b
DG
4119 /*
4120 * New-style PHB window placement.
4121 *
4122 * Goals: Gives large (1TiB), naturally aligned 64-bit MMIO window
4123 * for each PHB, in addition to 2GiB 32-bit MMIO and 64kiB PIO
4124 * windows.
4125 *
4126 * Some guest kernels can't work with MMIO windows above 1<<46
4127 * (64TiB), so we place up to 31 PHBs in the area 32TiB..64TiB
4128 *
4129 * 32TiB..(33TiB+1984kiB) contains the 64kiB PIO windows for each
4130 * PHB stacked together. (32TiB+2GiB)..(32TiB+64GiB) contains the
4131 * 2GiB 32-bit MMIO windows for each PHB. Then 33..64TiB has the
4132 * 1TiB 64-bit MMIO windows for each PHB.
4133 */
6737d9ad 4134 const uint64_t base_buid = 0x800000020000000ULL;
6737d9ad
DG
4135 int i;
4136
357d1e3b
DG
4137 /* Sanity check natural alignments */
4138 QEMU_BUILD_BUG_ON((SPAPR_PCI_BASE % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
4139 QEMU_BUILD_BUG_ON((SPAPR_PCI_LIMIT % SPAPR_PCI_MEM64_WIN_SIZE) != 0);
4140 QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM64_WIN_SIZE % SPAPR_PCI_MEM32_WIN_SIZE) != 0);
4141 QEMU_BUILD_BUG_ON((SPAPR_PCI_MEM32_WIN_SIZE % SPAPR_PCI_IO_WIN_SIZE) != 0);
4142 /* Sanity check bounds */
25e6a118
MT
4143 QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_IO_WIN_SIZE) >
4144 SPAPR_PCI_MEM32_WIN_SIZE);
4145 QEMU_BUILD_BUG_ON((SPAPR_MAX_PHBS * SPAPR_PCI_MEM32_WIN_SIZE) >
4146 SPAPR_PCI_MEM64_WIN_SIZE);
4147
4148 if (index >= SPAPR_MAX_PHBS) {
4149 error_setg(errp, "\"index\" for PAPR PHB is too large (max %llu)",
4150 SPAPR_MAX_PHBS - 1);
6737d9ad
DG
4151 return;
4152 }
4153
4154 *buid = base_buid + index;
4155 for (i = 0; i < n_dma; ++i) {
4156 liobns[i] = SPAPR_PCI_LIOBN(index, i);
4157 }
4158
357d1e3b
DG
4159 *pio = SPAPR_PCI_BASE + index * SPAPR_PCI_IO_WIN_SIZE;
4160 *mmio32 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM32_WIN_SIZE;
4161 *mmio64 = SPAPR_PCI_BASE + (index + 1) * SPAPR_PCI_MEM64_WIN_SIZE;
ec132efa
AK
4162
4163 *nv2gpa = SPAPR_PCI_NV2RAM64_WIN_BASE + index * SPAPR_PCI_NV2RAM64_WIN_SIZE;
4164 *nv2atsd = SPAPR_PCI_NV2ATSD_WIN_BASE + index * SPAPR_PCI_NV2ATSD_WIN_SIZE;
6737d9ad
DG
4165}
4166
7844e12b
CLG
4167static ICSState *spapr_ics_get(XICSFabric *dev, int irq)
4168{
ce2918cb 4169 SpaprMachineState *spapr = SPAPR_MACHINE(dev);
7844e12b
CLG
4170
4171 return ics_valid_irq(spapr->ics, irq) ? spapr->ics : NULL;
4172}
4173
4174static void spapr_ics_resend(XICSFabric *dev)
4175{
ce2918cb 4176 SpaprMachineState *spapr = SPAPR_MACHINE(dev);
7844e12b
CLG
4177
4178 ics_resend(spapr->ics);
4179}
4180
81210c20 4181static ICPState *spapr_icp_get(XICSFabric *xi, int vcpu_id)
b2fc59aa 4182{
2e886fb3 4183 PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
b2fc59aa 4184
a28b9a5a 4185 return cpu ? spapr_cpu_state(cpu)->icp : NULL;
b2fc59aa
CLG
4186}
4187
6449da45
CLG
4188static void spapr_pic_print_info(InterruptStatsProvider *obj,
4189 Monitor *mon)
4190{
ce2918cb 4191 SpaprMachineState *spapr = SPAPR_MACHINE(obj);
6449da45 4192
328d8eb2 4193 spapr_irq_print_info(spapr, mon);
f041d6af
GK
4194 monitor_printf(mon, "irqchip: %s\n",
4195 kvm_irqchip_in_kernel() ? "in-kernel" : "emulated");
6449da45
CLG
4196}
4197
932de7ae
CLG
4198static int spapr_match_nvt(XiveFabric *xfb, uint8_t format,
4199 uint8_t nvt_blk, uint32_t nvt_idx,
4200 bool cam_ignore, uint8_t priority,
4201 uint32_t logic_serv, XiveTCTXMatch *match)
4202{
4203 SpaprMachineState *spapr = SPAPR_MACHINE(xfb);
4204 XivePresenter *xptr = XIVE_PRESENTER(spapr->xive);
4205 XivePresenterClass *xpc = XIVE_PRESENTER_GET_CLASS(xptr);
4206 int count;
4207
4208 /* This is a XIVE only operation */
4209 assert(spapr->active_intc == SPAPR_INTC(spapr->xive));
4210
4211 count = xpc->match_nvt(xptr, format, nvt_blk, nvt_idx, cam_ignore,
4212 priority, logic_serv, match);
4213 if (count < 0) {
4214 return count;
4215 }
4216
4217 /*
4218 * When we implement the save and restore of the thread interrupt
4219 * contexts in the enter/exit CPU handlers of the machine and the
4220 * escalations in QEMU, we should be able to handle non dispatched
4221 * vCPUs.
4222 *
4223 * Until this is done, the sPAPR machine should find at least one
4224 * matching context always.
4225 */
4226 if (count == 0) {
4227 qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is not dispatched\n",
4228 nvt_blk, nvt_idx);
4229 }
4230
4231 return count;
4232}
4233
14bb4486 4234int spapr_get_vcpu_id(PowerPCCPU *cpu)
2e886fb3 4235{
b1a568c1 4236 return cpu->vcpu_id;
2e886fb3
SB
4237}
4238
648edb64
GK
4239void spapr_set_vcpu_id(PowerPCCPU *cpu, int cpu_index, Error **errp)
4240{
ce2918cb 4241 SpaprMachineState *spapr = SPAPR_MACHINE(qdev_get_machine());
fe6b6346 4242 MachineState *ms = MACHINE(spapr);
648edb64
GK
4243 int vcpu_id;
4244
5d0fb150 4245 vcpu_id = spapr_vcpu_id(spapr, cpu_index);
648edb64
GK
4246
4247 if (kvm_enabled() && !kvm_vcpu_id_is_valid(vcpu_id)) {
4248 error_setg(errp, "Can't create CPU with id %d in KVM", vcpu_id);
4249 error_append_hint(errp, "Adjust the number of cpus to %d "
4250 "or try to raise the number of threads per core\n",
fe6b6346 4251 vcpu_id * ms->smp.threads / spapr->vsmt);
648edb64
GK
4252 return;
4253 }
4254
4255 cpu->vcpu_id = vcpu_id;
4256}
4257
2e886fb3
SB
4258PowerPCCPU *spapr_find_cpu(int vcpu_id)
4259{
4260 CPUState *cs;
4261
4262 CPU_FOREACH(cs) {
4263 PowerPCCPU *cpu = POWERPC_CPU(cs);
4264
14bb4486 4265 if (spapr_get_vcpu_id(cpu) == vcpu_id) {
2e886fb3
SB
4266 return cpu;
4267 }
4268 }
4269
4270 return NULL;
4271}
4272
03ef074c
NP
4273static void spapr_cpu_exec_enter(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
4274{
4275 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
4276
4277 /* These are only called by TCG, KVM maintains dispatch state */
4278
3a6e6224 4279 spapr_cpu->prod = false;
03ef074c
NP
4280 if (spapr_cpu->vpa_addr) {
4281 CPUState *cs = CPU(cpu);
4282 uint32_t dispatch;
4283
4284 dispatch = ldl_be_phys(cs->as,
4285 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER);
4286 dispatch++;
4287 if ((dispatch & 1) != 0) {
4288 qemu_log_mask(LOG_GUEST_ERROR,
4289 "VPA: incorrect dispatch counter value for "
4290 "dispatched partition %u, correcting.\n", dispatch);
4291 dispatch++;
4292 }
4293 stl_be_phys(cs->as,
4294 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER, dispatch);
4295 }
4296}
4297
4298static void spapr_cpu_exec_exit(PPCVirtualHypervisor *vhyp, PowerPCCPU *cpu)
4299{
4300 SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
4301
4302 if (spapr_cpu->vpa_addr) {
4303 CPUState *cs = CPU(cpu);
4304 uint32_t dispatch;
4305
4306 dispatch = ldl_be_phys(cs->as,
4307 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER);
4308 dispatch++;
4309 if ((dispatch & 1) != 1) {
4310 qemu_log_mask(LOG_GUEST_ERROR,
4311 "VPA: incorrect dispatch counter value for "
4312 "preempted partition %u, correcting.\n", dispatch);
4313 dispatch++;
4314 }
4315 stl_be_phys(cs->as,
4316 spapr_cpu->vpa_addr + VPA_DISPATCH_COUNTER, dispatch);
4317 }
4318}
4319
29ee3247
AK
4320static void spapr_machine_class_init(ObjectClass *oc, void *data)
4321{
4322 MachineClass *mc = MACHINE_CLASS(oc);
ce2918cb 4323 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(oc);
71461b0f 4324 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
34316482 4325 NMIClass *nc = NMI_CLASS(oc);
c20d332a 4326 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc);
1d1be34d 4327 PPCVirtualHypervisorClass *vhc = PPC_VIRTUAL_HYPERVISOR_CLASS(oc);
7844e12b 4328 XICSFabricClass *xic = XICS_FABRIC_CLASS(oc);
6449da45 4329 InterruptStatsProviderClass *ispc = INTERRUPT_STATS_PROVIDER_CLASS(oc);
932de7ae 4330 XiveFabricClass *xfc = XIVE_FABRIC_CLASS(oc);
958db90c 4331
0eb9054c 4332 mc->desc = "pSeries Logical Partition (PAPR compliant)";
907aac2f 4333 mc->ignore_boot_device_suffixes = true;
fc9f38c3
DG
4334
4335 /*
4336 * We set up the default / latest behaviour here. The class_init
4337 * functions for the specific versioned machine types can override
4338 * these details for backwards compatibility
4339 */
bcb5ce08
DG
4340 mc->init = spapr_machine_init;
4341 mc->reset = spapr_machine_reset;
958db90c 4342 mc->block_default_type = IF_SCSI;
6244bb7e 4343 mc->max_cpus = 1024;
958db90c 4344 mc->no_parallel = 1;
5b2128d2 4345 mc->default_boot_order = "";
d23b6caa 4346 mc->default_ram_size = 512 * MiB;
29f9cef3 4347 mc->default_display = "std";
958db90c 4348 mc->kvm_type = spapr_kvm_type;
7da79a16 4349 machine_class_allow_dynamic_sysbus_dev(mc, TYPE_SPAPR_PCI_HOST_BRIDGE);
e4024630 4350 mc->pci_allow_0_address = true;
debbdc00 4351 assert(!mc->get_hotplug_handler);
7ebaf795 4352 mc->get_hotplug_handler = spapr_get_hotplug_handler;
94a94e4c 4353 hc->pre_plug = spapr_machine_device_pre_plug;
c20d332a 4354 hc->plug = spapr_machine_device_plug;
ea089eeb 4355 mc->cpu_index_to_instance_props = spapr_cpu_index_to_props;
79e07936 4356 mc->get_default_cpu_node_id = spapr_get_default_cpu_node_id;
535455fd 4357 mc->possible_cpu_arch_ids = spapr_possible_cpu_arch_ids;
cf632463 4358 hc->unplug_request = spapr_machine_device_unplug_request;
88432f44 4359 hc->unplug = spapr_machine_device_unplug;
00b4fbe2 4360
fc9f38c3 4361 smc->dr_lmb_enabled = true;
fea35ca4 4362 smc->update_dt_enabled = true;
34a6b015 4363 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power9_v2.0");
c5514d0e 4364 mc->has_hotpluggable_cpus = true;
52b81ab5 4365 smc->resize_hpt_default = SPAPR_RESIZE_HPT_ENABLED;
71461b0f 4366 fwc->get_dev_path = spapr_get_fw_dev_path;
34316482 4367 nc->nmi_monitor_handler = spapr_nmi;
6737d9ad 4368 smc->phb_placement = spapr_phb_placement;
1d1be34d 4369 vhc->hypercall = emulate_spapr_hypercall;
e57ca75c
DG
4370 vhc->hpt_mask = spapr_hpt_mask;
4371 vhc->map_hptes = spapr_map_hptes;
4372 vhc->unmap_hptes = spapr_unmap_hptes;
a2dd4e83
BH
4373 vhc->hpte_set_c = spapr_hpte_set_c;
4374 vhc->hpte_set_r = spapr_hpte_set_r;
79825f4d 4375 vhc->get_pate = spapr_get_pate;
1ec26c75 4376 vhc->encode_hpt_for_kvm_pr = spapr_encode_hpt_for_kvm_pr;
03ef074c
NP
4377 vhc->cpu_exec_enter = spapr_cpu_exec_enter;
4378 vhc->cpu_exec_exit = spapr_cpu_exec_exit;
7844e12b
CLG
4379 xic->ics_get = spapr_ics_get;
4380 xic->ics_resend = spapr_ics_resend;
b2fc59aa 4381 xic->icp_get = spapr_icp_get;
6449da45 4382 ispc->print_info = spapr_pic_print_info;
55641213
LV
4383 /* Force NUMA node memory size to be a multiple of
4384 * SPAPR_MEMORY_BLOCK_SIZE (256M) since that's the granularity
4385 * in which LMBs are represented and hot-added
4386 */
4387 mc->numa_mem_align_shift = 28;
cd5ff833 4388 mc->numa_mem_supported = true;
0533ef5f 4389 mc->auto_enable_numa = true;
33face6b 4390
4e5fe368
SJS
4391 smc->default_caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_OFF;
4392 smc->default_caps.caps[SPAPR_CAP_VSX] = SPAPR_CAP_ON;
4393 smc->default_caps.caps[SPAPR_CAP_DFP] = SPAPR_CAP_ON;
2782ad4c
SJS
4394 smc->default_caps.caps[SPAPR_CAP_CFPC] = SPAPR_CAP_WORKAROUND;
4395 smc->default_caps.caps[SPAPR_CAP_SBBC] = SPAPR_CAP_WORKAROUND;
4396 smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_WORKAROUND;
2309832a 4397 smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = 16; /* 64kiB */
b9a477b7 4398 smc->default_caps.caps[SPAPR_CAP_NESTED_KVM_HV] = SPAPR_CAP_OFF;
edaa7995 4399 smc->default_caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = SPAPR_CAP_ON;
8ff43ee4 4400 smc->default_caps.caps[SPAPR_CAP_CCF_ASSIST] = SPAPR_CAP_OFF;
33face6b 4401 spapr_caps_add_properties(smc, &error_abort);
bd94bc06 4402 smc->irq = &spapr_irq_dual;
dae5e39a 4403 smc->dr_phb_enabled = true;
6c3829a2 4404 smc->linux_pci_probe = true;
29cb4187 4405 smc->smp_threads_vsmt = true;
54255c1f 4406 smc->nr_xirqs = SPAPR_NR_XIRQS;
932de7ae 4407 xfc->match_nvt = spapr_match_nvt;
29ee3247
AK
4408}
4409
4410static const TypeInfo spapr_machine_info = {
4411 .name = TYPE_SPAPR_MACHINE,
4412 .parent = TYPE_MACHINE,
4aee7362 4413 .abstract = true,
ce2918cb 4414 .instance_size = sizeof(SpaprMachineState),
bcb5ce08 4415 .instance_init = spapr_instance_init,
87bbdd9c 4416 .instance_finalize = spapr_machine_finalizefn,
ce2918cb 4417 .class_size = sizeof(SpaprMachineClass),
29ee3247 4418 .class_init = spapr_machine_class_init,
71461b0f
AK
4419 .interfaces = (InterfaceInfo[]) {
4420 { TYPE_FW_PATH_PROVIDER },
34316482 4421 { TYPE_NMI },
c20d332a 4422 { TYPE_HOTPLUG_HANDLER },
1d1be34d 4423 { TYPE_PPC_VIRTUAL_HYPERVISOR },
7844e12b 4424 { TYPE_XICS_FABRIC },
6449da45 4425 { TYPE_INTERRUPT_STATS_PROVIDER },
932de7ae 4426 { TYPE_XIVE_FABRIC },
71461b0f
AK
4427 { }
4428 },
29ee3247
AK
4429};
4430
fccbc785 4431#define DEFINE_SPAPR_MACHINE(suffix, verstr, latest) \
5013c547
DG
4432 static void spapr_machine_##suffix##_class_init(ObjectClass *oc, \
4433 void *data) \
4434 { \
4435 MachineClass *mc = MACHINE_CLASS(oc); \
4436 spapr_machine_##suffix##_class_options(mc); \
fccbc785
DG
4437 if (latest) { \
4438 mc->alias = "pseries"; \
4439 mc->is_default = 1; \
4440 } \
5013c547 4441 } \
5013c547
DG
4442 static const TypeInfo spapr_machine_##suffix##_info = { \
4443 .name = MACHINE_TYPE_NAME("pseries-" verstr), \
4444 .parent = TYPE_SPAPR_MACHINE, \
4445 .class_init = spapr_machine_##suffix##_class_init, \
5013c547
DG
4446 }; \
4447 static void spapr_machine_register_##suffix(void) \
4448 { \
4449 type_register(&spapr_machine_##suffix##_info); \
4450 } \
0e6aac87 4451 type_init(spapr_machine_register_##suffix)
5013c547 4452
3eb74d20
CH
4453/*
4454 * pseries-5.0
4455 */
4456static void spapr_machine_5_0_class_options(MachineClass *mc)
4457{
4458 /* Defaults for the latest behaviour inherited from the base class */
4459}
4460
4461DEFINE_SPAPR_MACHINE(5_0, "5.0", true);
4462
9aec2e52
CH
4463/*
4464 * pseries-4.2
4465 */
4466static void spapr_machine_4_2_class_options(MachineClass *mc)
4467{
3eb74d20 4468 spapr_machine_5_0_class_options(mc);
5f258577 4469 compat_props_add(mc->compat_props, hw_compat_4_2, hw_compat_4_2_len);
9aec2e52
CH
4470}
4471
3eb74d20 4472DEFINE_SPAPR_MACHINE(4_2, "4.2", false);
9aec2e52 4473
9bf2650b
CH
4474/*
4475 * pseries-4.1
4476 */
4477static void spapr_machine_4_1_class_options(MachineClass *mc)
4478{
6c3829a2 4479 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
d15d4ad6
DG
4480 static GlobalProperty compat[] = {
4481 /* Only allow 4kiB and 64kiB IOMMU pagesizes */
4482 { TYPE_SPAPR_PCI_HOST_BRIDGE, "pgsz", "0x11000" },
4483 };
4484
9aec2e52 4485 spapr_machine_4_2_class_options(mc);
6c3829a2 4486 smc->linux_pci_probe = false;
29cb4187 4487 smc->smp_threads_vsmt = false;
9aec2e52 4488 compat_props_add(mc->compat_props, hw_compat_4_1, hw_compat_4_1_len);
d15d4ad6 4489 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
9bf2650b
CH
4490}
4491
9aec2e52 4492DEFINE_SPAPR_MACHINE(4_1, "4.1", false);
9bf2650b 4493
84e060bf
AW
4494/*
4495 * pseries-4.0
4496 */
eb3cba82 4497static void phb_placement_4_0(SpaprMachineState *spapr, uint32_t index,
ec132efa
AK
4498 uint64_t *buid, hwaddr *pio,
4499 hwaddr *mmio32, hwaddr *mmio64,
4500 unsigned n_dma, uint32_t *liobns,
4501 hwaddr *nv2gpa, hwaddr *nv2atsd, Error **errp)
4502{
4503 spapr_phb_placement(spapr, index, buid, pio, mmio32, mmio64, n_dma, liobns,
4504 nv2gpa, nv2atsd, errp);
4505 *nv2gpa = 0;
4506 *nv2atsd = 0;
4507}
4508
eb3cba82
DG
4509static void spapr_machine_4_0_class_options(MachineClass *mc)
4510{
4511 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
4512
4513 spapr_machine_4_1_class_options(mc);
4514 compat_props_add(mc->compat_props, hw_compat_4_0, hw_compat_4_0_len);
4515 smc->phb_placement = phb_placement_4_0;
bd94bc06 4516 smc->irq = &spapr_irq_xics;
3725ef1a 4517 smc->pre_4_1_migration = true;
eb3cba82
DG
4518}
4519
4520DEFINE_SPAPR_MACHINE(4_0, "4.0", false);
4521
4522/*
4523 * pseries-3.1
4524 */
d45360d9
CLG
4525static void spapr_machine_3_1_class_options(MachineClass *mc)
4526{
ce2918cb 4527 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
fea35ca4 4528
84e060bf 4529 spapr_machine_4_0_class_options(mc);
abd93cc7 4530 compat_props_add(mc->compat_props, hw_compat_3_1, hw_compat_3_1_len);
27461d69 4531
34a6b015 4532 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power8_v2.0");
fea35ca4 4533 smc->update_dt_enabled = false;
dae5e39a 4534 smc->dr_phb_enabled = false;
0a794529 4535 smc->broken_host_serial_model = true;
2782ad4c
SJS
4536 smc->default_caps.caps[SPAPR_CAP_CFPC] = SPAPR_CAP_BROKEN;
4537 smc->default_caps.caps[SPAPR_CAP_SBBC] = SPAPR_CAP_BROKEN;
4538 smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_BROKEN;
edaa7995 4539 smc->default_caps.caps[SPAPR_CAP_LARGE_DECREMENTER] = SPAPR_CAP_OFF;
d45360d9
CLG
4540}
4541
84e060bf 4542DEFINE_SPAPR_MACHINE(3_1, "3.1", false);
d45360d9 4543
8a4fd427 4544/*
d8c0c7af 4545 * pseries-3.0
8a4fd427 4546 */
d45360d9 4547
d8c0c7af 4548static void spapr_machine_3_0_class_options(MachineClass *mc)
8a4fd427 4549{
ce2918cb 4550 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
82cffa2e 4551
d45360d9 4552 spapr_machine_3_1_class_options(mc);
ddb3235d 4553 compat_props_add(mc->compat_props, hw_compat_3_0, hw_compat_3_0_len);
82cffa2e
CLG
4554
4555 smc->legacy_irq_allocation = true;
54255c1f 4556 smc->nr_xirqs = 0x400;
ae837402 4557 smc->irq = &spapr_irq_xics_legacy;
8a4fd427
DG
4558}
4559
d45360d9 4560DEFINE_SPAPR_MACHINE(3_0, "3.0", false);
8a4fd427 4561
2b615412
DG
4562/*
4563 * pseries-2.12
4564 */
2b615412
DG
4565static void spapr_machine_2_12_class_options(MachineClass *mc)
4566{
ce2918cb 4567 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
88cbe073 4568 static GlobalProperty compat[] = {
6c36bddf
EH
4569 { TYPE_POWERPC_CPU, "pre-3.0-migration", "on" },
4570 { TYPE_SPAPR_CPU_CORE, "pre-3.0-migration", "on" },
88cbe073 4571 };
2309832a 4572
d8c0c7af 4573 spapr_machine_3_0_class_options(mc);
0d47310b 4574 compat_props_add(mc->compat_props, hw_compat_2_12, hw_compat_2_12_len);
88cbe073 4575 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
2309832a 4576
e8937295
GK
4577 /* We depend on kvm_enabled() to choose a default value for the
4578 * hpt-max-page-size capability. Of course we can't do it here
4579 * because this is too early and the HW accelerator isn't initialzed
4580 * yet. Postpone this to machine init (see default_caps_with_cpu()).
4581 */
4582 smc->default_caps.caps[SPAPR_CAP_HPT_MAXPAGESIZE] = 0;
2b615412
DG
4583}
4584
8a4fd427 4585DEFINE_SPAPR_MACHINE(2_12, "2.12", false);
2b615412 4586
813f3cf6
SJS
4587static void spapr_machine_2_12_sxxm_class_options(MachineClass *mc)
4588{
ce2918cb 4589 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
813f3cf6
SJS
4590
4591 spapr_machine_2_12_class_options(mc);
4592 smc->default_caps.caps[SPAPR_CAP_CFPC] = SPAPR_CAP_WORKAROUND;
4593 smc->default_caps.caps[SPAPR_CAP_SBBC] = SPAPR_CAP_WORKAROUND;
4594 smc->default_caps.caps[SPAPR_CAP_IBS] = SPAPR_CAP_FIXED_CCD;
4595}
4596
4597DEFINE_SPAPR_MACHINE(2_12_sxxm, "2.12-sxxm", false);
4598
e2676b16
GK
4599/*
4600 * pseries-2.11
4601 */
2b615412 4602
e2676b16
GK
4603static void spapr_machine_2_11_class_options(MachineClass *mc)
4604{
ce2918cb 4605 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
ee76a09f 4606
2b615412 4607 spapr_machine_2_12_class_options(mc);
4e5fe368 4608 smc->default_caps.caps[SPAPR_CAP_HTM] = SPAPR_CAP_ON;
43df70a9 4609 compat_props_add(mc->compat_props, hw_compat_2_11, hw_compat_2_11_len);
e2676b16
GK
4610}
4611
2b615412 4612DEFINE_SPAPR_MACHINE(2_11, "2.11", false);
e2676b16 4613
3fa14fbe
DG
4614/*
4615 * pseries-2.10
4616 */
e2676b16 4617
3fa14fbe
DG
4618static void spapr_machine_2_10_class_options(MachineClass *mc)
4619{
e2676b16 4620 spapr_machine_2_11_class_options(mc);
503224f4 4621 compat_props_add(mc->compat_props, hw_compat_2_10, hw_compat_2_10_len);
3fa14fbe
DG
4622}
4623
e2676b16 4624DEFINE_SPAPR_MACHINE(2_10, "2.10", false);
3fa14fbe 4625
fa325e6c
DG
4626/*
4627 * pseries-2.9
4628 */
3fa14fbe 4629
fa325e6c
DG
4630static void spapr_machine_2_9_class_options(MachineClass *mc)
4631{
ce2918cb 4632 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
88cbe073 4633 static GlobalProperty compat[] = {
6c36bddf 4634 { TYPE_POWERPC_CPU, "pre-2.10-migration", "on" },
88cbe073 4635 };
46f7afa3 4636
3fa14fbe 4637 spapr_machine_2_10_class_options(mc);
3e803152 4638 compat_props_add(mc->compat_props, hw_compat_2_9, hw_compat_2_9_len);
88cbe073 4639 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
3bfe5716 4640 mc->numa_auto_assign_ram = numa_legacy_auto_assign_ram;
46f7afa3 4641 smc->pre_2_10_has_unused_icps = true;
52b81ab5 4642 smc->resize_hpt_default = SPAPR_RESIZE_HPT_DISABLED;
fa325e6c
DG
4643}
4644
3fa14fbe 4645DEFINE_SPAPR_MACHINE(2_9, "2.9", false);
fa325e6c 4646
db800b21
DG
4647/*
4648 * pseries-2.8
4649 */
fa325e6c 4650
db800b21
DG
4651static void spapr_machine_2_8_class_options(MachineClass *mc)
4652{
88cbe073 4653 static GlobalProperty compat[] = {
6c36bddf 4654 { TYPE_SPAPR_PCI_HOST_BRIDGE, "pcie-extended-configuration-space", "off" },
88cbe073
MAL
4655 };
4656
fa325e6c 4657 spapr_machine_2_9_class_options(mc);
edc24ccd 4658 compat_props_add(mc->compat_props, hw_compat_2_8, hw_compat_2_8_len);
88cbe073 4659 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
55641213 4660 mc->numa_mem_align_shift = 23;
db800b21
DG
4661}
4662
fa325e6c 4663DEFINE_SPAPR_MACHINE(2_8, "2.8", false);
db800b21 4664
1ea1eefc
BR
4665/*
4666 * pseries-2.7
4667 */
357d1e3b 4668
ce2918cb 4669static void phb_placement_2_7(SpaprMachineState *spapr, uint32_t index,
357d1e3b
DG
4670 uint64_t *buid, hwaddr *pio,
4671 hwaddr *mmio32, hwaddr *mmio64,
ec132efa
AK
4672 unsigned n_dma, uint32_t *liobns,
4673 hwaddr *nv2gpa, hwaddr *nv2atsd, Error **errp)
357d1e3b
DG
4674{
4675 /* Legacy PHB placement for pseries-2.7 and earlier machine types */
4676 const uint64_t base_buid = 0x800000020000000ULL;
4677 const hwaddr phb_spacing = 0x1000000000ULL; /* 64 GiB */
4678 const hwaddr mmio_offset = 0xa0000000; /* 2 GiB + 512 MiB */
4679 const hwaddr pio_offset = 0x80000000; /* 2 GiB */
4680 const uint32_t max_index = 255;
4681 const hwaddr phb0_alignment = 0x10000000000ULL; /* 1 TiB */
4682
4683 uint64_t ram_top = MACHINE(spapr)->ram_size;
4684 hwaddr phb0_base, phb_base;
4685 int i;
4686
0c9269a5 4687 /* Do we have device memory? */
357d1e3b
DG
4688 if (MACHINE(spapr)->maxram_size > ram_top) {
4689 /* Can't just use maxram_size, because there may be an
0c9269a5
DH
4690 * alignment gap between normal and device memory regions
4691 */
b0c14ec4
DH
4692 ram_top = MACHINE(spapr)->device_memory->base +
4693 memory_region_size(&MACHINE(spapr)->device_memory->mr);
357d1e3b
DG
4694 }
4695
4696 phb0_base = QEMU_ALIGN_UP(ram_top, phb0_alignment);
4697
4698 if (index > max_index) {
4699 error_setg(errp, "\"index\" for PAPR PHB is too large (max %u)",
4700 max_index);
4701 return;
4702 }
4703
4704 *buid = base_buid + index;
4705 for (i = 0; i < n_dma; ++i) {
4706 liobns[i] = SPAPR_PCI_LIOBN(index, i);
4707 }
4708
4709 phb_base = phb0_base + index * phb_spacing;
4710 *pio = phb_base + pio_offset;
4711 *mmio32 = phb_base + mmio_offset;
4712 /*
4713 * We don't set the 64-bit MMIO window, relying on the PHB's
4714 * fallback behaviour of automatically splitting a large "32-bit"
4715 * window into contiguous 32-bit and 64-bit windows
4716 */
ec132efa
AK
4717
4718 *nv2gpa = 0;
4719 *nv2atsd = 0;
357d1e3b 4720}
db800b21 4721
1ea1eefc
BR
4722static void spapr_machine_2_7_class_options(MachineClass *mc)
4723{
ce2918cb 4724 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
88cbe073 4725 static GlobalProperty compat[] = {
6c36bddf
EH
4726 { TYPE_SPAPR_PCI_HOST_BRIDGE, "mem_win_size", "0xf80000000", },
4727 { TYPE_SPAPR_PCI_HOST_BRIDGE, "mem64_win_size", "0", },
4728 { TYPE_POWERPC_CPU, "pre-2.8-migration", "on", },
4729 { TYPE_SPAPR_PCI_HOST_BRIDGE, "pre-2.8-migration", "on", },
88cbe073 4730 };
3daa4a9f 4731
db800b21 4732 spapr_machine_2_8_class_options(mc);
2e9c10eb 4733 mc->default_cpu_type = POWERPC_CPU_TYPE_NAME("power7_v2.3");
a140c199 4734 mc->default_machine_opts = "modern-hotplug-events=off";
5a995064 4735 compat_props_add(mc->compat_props, hw_compat_2_7, hw_compat_2_7_len);
88cbe073 4736 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
357d1e3b 4737 smc->phb_placement = phb_placement_2_7;
1ea1eefc
BR
4738}
4739
db800b21 4740DEFINE_SPAPR_MACHINE(2_7, "2.7", false);
1ea1eefc 4741
4b23699c
DG
4742/*
4743 * pseries-2.6
4744 */
1ea1eefc 4745
4b23699c
DG
4746static void spapr_machine_2_6_class_options(MachineClass *mc)
4747{
88cbe073 4748 static GlobalProperty compat[] = {
6c36bddf 4749 { TYPE_SPAPR_PCI_HOST_BRIDGE, "ddw", "off" },
88cbe073
MAL
4750 };
4751
1ea1eefc 4752 spapr_machine_2_7_class_options(mc);
c5514d0e 4753 mc->has_hotpluggable_cpus = false;
ff8f261f 4754 compat_props_add(mc->compat_props, hw_compat_2_6, hw_compat_2_6_len);
88cbe073 4755 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
4b23699c
DG
4756}
4757
1ea1eefc 4758DEFINE_SPAPR_MACHINE(2_6, "2.6", false);
4b23699c 4759
1c5f29bb
DG
4760/*
4761 * pseries-2.5
4762 */
4b23699c 4763
5013c547
DG
4764static void spapr_machine_2_5_class_options(MachineClass *mc)
4765{
ce2918cb 4766 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
88cbe073 4767 static GlobalProperty compat[] = {
6c36bddf 4768 { "spapr-vlan", "use-rx-buffer-pools", "off" },
88cbe073 4769 };
57040d45 4770
4b23699c 4771 spapr_machine_2_6_class_options(mc);
57040d45 4772 smc->use_ohci_by_default = true;
fe759610 4773 compat_props_add(mc->compat_props, hw_compat_2_5, hw_compat_2_5_len);
88cbe073 4774 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
1c5f29bb
DG
4775}
4776
4b23699c 4777DEFINE_SPAPR_MACHINE(2_5, "2.5", false);
1c5f29bb
DG
4778
4779/*
4780 * pseries-2.4
4781 */
80fd50f9 4782
5013c547
DG
4783static void spapr_machine_2_4_class_options(MachineClass *mc)
4784{
ce2918cb 4785 SpaprMachineClass *smc = SPAPR_MACHINE_CLASS(mc);
fc9f38c3
DG
4786
4787 spapr_machine_2_5_class_options(mc);
fc9f38c3 4788 smc->dr_lmb_enabled = false;
2f99b9c2 4789 compat_props_add(mc->compat_props, hw_compat_2_4, hw_compat_2_4_len);
1c5f29bb
DG
4790}
4791
fccbc785 4792DEFINE_SPAPR_MACHINE(2_4, "2.4", false);
1c5f29bb
DG
4793
4794/*
4795 * pseries-2.3
4796 */
38ff32c6 4797
5013c547 4798static void spapr_machine_2_3_class_options(MachineClass *mc)
6026db45 4799{
88cbe073 4800 static GlobalProperty compat[] = {
6c36bddf 4801 { "spapr-pci-host-bridge", "dynamic-reconfiguration", "off" },
88cbe073 4802 };
fc9f38c3 4803 spapr_machine_2_4_class_options(mc);
8995dd90 4804 compat_props_add(mc->compat_props, hw_compat_2_3, hw_compat_2_3_len);
88cbe073 4805 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
6026db45 4806}
fccbc785 4807DEFINE_SPAPR_MACHINE(2_3, "2.3", false);
6026db45 4808
1c5f29bb
DG
4809/*
4810 * pseries-2.2
4811 */
1c5f29bb 4812
5013c547 4813static void spapr_machine_2_2_class_options(MachineClass *mc)
4aee7362 4814{
88cbe073 4815 static GlobalProperty compat[] = {
6c36bddf 4816 { TYPE_SPAPR_PCI_HOST_BRIDGE, "mem_win_size", "0x20000000" },
88cbe073
MAL
4817 };
4818
fc9f38c3 4819 spapr_machine_2_3_class_options(mc);
1c30044e 4820 compat_props_add(mc->compat_props, hw_compat_2_2, hw_compat_2_2_len);
88cbe073 4821 compat_props_add(mc->compat_props, compat, G_N_ELEMENTS(compat));
f6d0656b 4822 mc->default_machine_opts = "modern-hotplug-events=off,suppress-vmdesc=on";
4aee7362 4823}
fccbc785 4824DEFINE_SPAPR_MACHINE(2_2, "2.2", false);
4aee7362 4825
1c5f29bb
DG
4826/*
4827 * pseries-2.1
4828 */
3dab0244 4829
5013c547 4830static void spapr_machine_2_1_class_options(MachineClass *mc)
d25228e7 4831{
fc9f38c3 4832 spapr_machine_2_2_class_options(mc);
c4fc5695 4833 compat_props_add(mc->compat_props, hw_compat_2_1, hw_compat_2_1_len);
d25228e7 4834}
fccbc785 4835DEFINE_SPAPR_MACHINE(2_1, "2.1", false);
fb0fc8f6 4836
29ee3247 4837static void spapr_machine_register_types(void)
9fdf0c29 4838{
29ee3247 4839 type_register_static(&spapr_machine_info);
9fdf0c29
DG
4840}
4841
29ee3247 4842type_init(spapr_machine_register_types)
This page took 1.822562 seconds and 4 git commands to generate.