]> git.proxmox.com Git - mirror_qemu.git/blame - hw/ppc/spapr.c
spapr: Create SPAPRMachine struct
[mirror_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.
25 *
26 */
9c17d615 27#include "sysemu/sysemu.h"
83c9f4ca 28#include "hw/hw.h"
71461b0f 29#include "hw/fw-path-provider.h"
9fdf0c29 30#include "elf.h"
1422e32d 31#include "net/net.h"
9c17d615
PB
32#include "sysemu/blockdev.h"
33#include "sysemu/cpus.h"
34#include "sysemu/kvm.h"
e97c3636 35#include "kvm_ppc.h"
4be21d56 36#include "mmu-hash64.h"
3794d548 37#include "qom/cpu.h"
9fdf0c29
DG
38
39#include "hw/boards.h"
0d09e41a 40#include "hw/ppc/ppc.h"
9fdf0c29
DG
41#include "hw/loader.h"
42
0d09e41a
PB
43#include "hw/ppc/spapr.h"
44#include "hw/ppc/spapr_vio.h"
45#include "hw/pci-host/spapr.h"
46#include "hw/ppc/xics.h"
a2cb15b0 47#include "hw/pci/msi.h"
9fdf0c29 48
83c9f4ca 49#include "hw/pci/pci.h"
71461b0f
AK
50#include "hw/scsi/scsi.h"
51#include "hw/virtio/virtio-scsi.h"
f61b4bed 52
022c62cb 53#include "exec/address-spaces.h"
35139a59 54#include "hw/usb.h"
1de7afc9 55#include "qemu/config-file.h"
135a129a 56#include "qemu/error-report.h"
2a6593cb 57#include "trace.h"
890c2b77 58
9fdf0c29
DG
59#include <libfdt.h>
60
4d8d5467
BH
61/* SLOF memory layout:
62 *
63 * SLOF raw image loaded at 0, copies its romfs right below the flat
64 * device-tree, then position SLOF itself 31M below that
65 *
66 * So we set FW_OVERHEAD to 40MB which should account for all of that
67 * and more
68 *
69 * We load our kernel at 4M, leaving space for SLOF initial image
70 */
3bf6eedd 71#define FDT_MAX_SIZE 0x40000
39ac8455 72#define RTAS_MAX_SIZE 0x10000
a9f8ad8f
DG
73#define FW_MAX_SIZE 0x400000
74#define FW_FILE_NAME "slof.bin"
4d8d5467
BH
75#define FW_OVERHEAD 0x2800000
76#define KERNEL_LOAD_ADDR FW_MAX_SIZE
a9f8ad8f 77
4d8d5467 78#define MIN_RMA_SLOF 128UL
9fdf0c29
DG
79
80#define TIMEBASE_FREQ 512000000ULL
81
41019fec 82#define MAX_CPUS 256
4d8d5467 83#define XICS_IRQS 1024
9fdf0c29 84
0c103f8e
DG
85#define PHANDLE_XICP 0x00001111
86
7f763a5d
DG
87#define HTAB_SIZE(spapr) (1ULL << ((spapr)->htab_shift))
88
748abce9
EH
89
90typedef struct SPAPRMachine SPAPRMachine;
29ee3247 91#define TYPE_SPAPR_MACHINE "spapr-machine"
748abce9
EH
92#define SPAPR_MACHINE(obj) \
93 OBJECT_CHECK(SPAPRMachine, (obj), TYPE_SPAPR_MACHINE)
94
95/**
96 * SPAPRMachine:
97 */
98struct SPAPRMachine {
99 /*< private >*/
100 MachineState parent_obj;
101};
102
29ee3247 103
9fdf0c29
DG
104sPAPREnvironment *spapr;
105
ff9d2afa 106int spapr_allocate_irq(int hint, bool lsi)
e6c866d4 107{
a307d594 108 int irq;
e6c866d4
DG
109
110 if (hint) {
111 irq = hint;
f1c2dc7c
AK
112 if (hint >= spapr->next_irq) {
113 spapr->next_irq = hint + 1;
114 }
e6c866d4
DG
115 /* FIXME: we should probably check for collisions somehow */
116 } else {
117 irq = spapr->next_irq++;
118 }
119
a307d594
AK
120 /* Configure irq type */
121 if (!xics_get_qirq(spapr->icp, irq)) {
122 return 0;
e6c866d4
DG
123 }
124
ff9d2afa 125 xics_set_irq_type(spapr->icp, irq, lsi);
e6c866d4 126
a307d594 127 return irq;
e6c866d4
DG
128}
129
f1c2dc7c
AK
130/*
131 * Allocate block of consequtive IRQs, returns a number of the first.
132 * If msi==true, aligns the first IRQ number to num.
133 */
134int spapr_allocate_irq_block(int num, bool lsi, bool msi)
f4b9523b
AK
135{
136 int first = -1;
f1c2dc7c
AK
137 int i, hint = 0;
138
139 /*
140 * MSIMesage::data is used for storing VIRQ so
141 * it has to be aligned to num to support multiple
142 * MSI vectors. MSI-X is not affected by this.
143 * The hint is used for the first IRQ, the rest should
73f395fa 144 * be allocated continuously.
f1c2dc7c
AK
145 */
146 if (msi) {
147 assert((num == 1) || (num == 2) || (num == 4) ||
148 (num == 8) || (num == 16) || (num == 32));
149 hint = (spapr->next_irq + num - 1) & ~(num - 1);
150 }
f4b9523b
AK
151
152 for (i = 0; i < num; ++i) {
153 int irq;
154
f1c2dc7c 155 irq = spapr_allocate_irq(hint, lsi);
f4b9523b
AK
156 if (!irq) {
157 return -1;
158 }
159
160 if (0 == i) {
161 first = irq;
f1c2dc7c 162 hint = 0;
f4b9523b
AK
163 }
164
165 /* If the above doesn't create a consecutive block then that's
166 * an internal bug */
167 assert(irq == (first + i));
168 }
169
170 return first;
171}
172
c04d6cfa
AL
173static XICSState *try_create_xics(const char *type, int nr_servers,
174 int nr_irqs)
175{
176 DeviceState *dev;
177
178 dev = qdev_create(NULL, type);
179 qdev_prop_set_uint32(dev, "nr_servers", nr_servers);
180 qdev_prop_set_uint32(dev, "nr_irqs", nr_irqs);
181 if (qdev_init(dev) < 0) {
182 return NULL;
183 }
184
5a3d7b23 185 return XICS_COMMON(dev);
c04d6cfa
AL
186}
187
188static XICSState *xics_system_init(int nr_servers, int nr_irqs)
189{
190 XICSState *icp = NULL;
191
11ad93f6
DG
192 if (kvm_enabled()) {
193 QemuOpts *machine_opts = qemu_get_machine_opts();
194 bool irqchip_allowed = qemu_opt_get_bool(machine_opts,
195 "kernel_irqchip", true);
196 bool irqchip_required = qemu_opt_get_bool(machine_opts,
197 "kernel_irqchip", false);
198 if (irqchip_allowed) {
199 icp = try_create_xics(TYPE_KVM_XICS, nr_servers, nr_irqs);
200 }
201
202 if (irqchip_required && !icp) {
203 perror("Failed to create in-kernel XICS\n");
204 abort();
205 }
206 }
207
208 if (!icp) {
209 icp = try_create_xics(TYPE_XICS, nr_servers, nr_irqs);
210 }
211
c04d6cfa
AL
212 if (!icp) {
213 perror("Failed to create XICS\n");
214 abort();
215 }
216
217 return icp;
218}
219
833d4668
AK
220static int spapr_fixup_cpu_smt_dt(void *fdt, int offset, PowerPCCPU *cpu,
221 int smt_threads)
222{
223 int i, ret = 0;
224 uint32_t servers_prop[smt_threads];
225 uint32_t gservers_prop[smt_threads * 2];
226 int index = ppc_get_vcpu_dt_id(cpu);
227
6d9412ea
AK
228 if (cpu->cpu_version) {
229 ret = fdt_setprop(fdt, offset, "cpu-version",
230 &cpu->cpu_version, sizeof(cpu->cpu_version));
231 if (ret < 0) {
232 return ret;
233 }
234 }
235
833d4668
AK
236 /* Build interrupt servers and gservers properties */
237 for (i = 0; i < smt_threads; i++) {
238 servers_prop[i] = cpu_to_be32(index + i);
239 /* Hack, direct the group queues back to cpu 0 */
240 gservers_prop[i*2] = cpu_to_be32(index + i);
241 gservers_prop[i*2 + 1] = 0;
242 }
243 ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-server#s",
244 servers_prop, sizeof(servers_prop));
245 if (ret < 0) {
246 return ret;
247 }
248 ret = fdt_setprop(fdt, offset, "ibm,ppc-interrupt-gserver#s",
249 gservers_prop, sizeof(gservers_prop));
250
251 return ret;
252}
253
7f763a5d 254static int spapr_fixup_cpu_dt(void *fdt, sPAPREnvironment *spapr)
6e806cc3 255{
82677ed2
AK
256 int ret = 0, offset, cpus_offset;
257 CPUState *cs;
6e806cc3
BR
258 char cpu_model[32];
259 int smt = kvmppc_smt_threads();
7f763a5d 260 uint32_t pft_size_prop[] = {0, cpu_to_be32(spapr->htab_shift)};
6e806cc3 261
82677ed2
AK
262 CPU_FOREACH(cs) {
263 PowerPCCPU *cpu = POWERPC_CPU(cs);
264 DeviceClass *dc = DEVICE_GET_CLASS(cs);
265 int index = ppc_get_vcpu_dt_id(cpu);
6e806cc3
BR
266 uint32_t associativity[] = {cpu_to_be32(0x5),
267 cpu_to_be32(0x0),
268 cpu_to_be32(0x0),
269 cpu_to_be32(0x0),
82677ed2 270 cpu_to_be32(cs->numa_node),
0f20ba62 271 cpu_to_be32(index)};
6e806cc3 272
0f20ba62 273 if ((index % smt) != 0) {
6e806cc3
BR
274 continue;
275 }
276
82677ed2 277 snprintf(cpu_model, 32, "%s@%x", dc->fw_name, index);
6e806cc3 278
82677ed2
AK
279 cpus_offset = fdt_path_offset(fdt, "/cpus");
280 if (cpus_offset < 0) {
281 cpus_offset = fdt_add_subnode(fdt, fdt_path_offset(fdt, "/"),
282 "cpus");
283 if (cpus_offset < 0) {
284 return cpus_offset;
285 }
286 }
287 offset = fdt_subnode_offset(fdt, cpus_offset, cpu_model);
6e806cc3 288 if (offset < 0) {
82677ed2
AK
289 offset = fdt_add_subnode(fdt, cpus_offset, cpu_model);
290 if (offset < 0) {
291 return offset;
292 }
6e806cc3
BR
293 }
294
7f763a5d
DG
295 if (nb_numa_nodes > 1) {
296 ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
297 sizeof(associativity));
298 if (ret < 0) {
299 return ret;
300 }
301 }
302
303 ret = fdt_setprop(fdt, offset, "ibm,pft-size",
304 pft_size_prop, sizeof(pft_size_prop));
6e806cc3
BR
305 if (ret < 0) {
306 return ret;
307 }
833d4668 308
82677ed2 309 ret = spapr_fixup_cpu_smt_dt(fdt, offset, cpu,
2a48d993 310 ppc_get_compat_smt_threads(cpu));
833d4668
AK
311 if (ret < 0) {
312 return ret;
313 }
6e806cc3
BR
314 }
315 return ret;
316}
317
5af9873d
BH
318
319static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
320 size_t maxsize)
321{
322 size_t maxcells = maxsize / sizeof(uint32_t);
323 int i, j, count;
324 uint32_t *p = prop;
325
326 for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
327 struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
328
329 if (!sps->page_shift) {
330 break;
331 }
332 for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
333 if (sps->enc[count].page_shift == 0) {
334 break;
335 }
336 }
337 if ((p - prop) >= (maxcells - 3 - count * 2)) {
338 break;
339 }
340 *(p++) = cpu_to_be32(sps->page_shift);
341 *(p++) = cpu_to_be32(sps->slb_enc);
342 *(p++) = cpu_to_be32(count);
343 for (j = 0; j < count; j++) {
344 *(p++) = cpu_to_be32(sps->enc[j].page_shift);
345 *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
346 }
347 }
348
349 return (p - prop) * sizeof(uint32_t);
350}
351
7f763a5d
DG
352#define _FDT(exp) \
353 do { \
354 int ret = (exp); \
355 if (ret < 0) { \
356 fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
357 #exp, fdt_strerror(ret)); \
358 exit(1); \
359 } \
360 } while (0)
361
a1d59c0f
AK
362static void add_str(GString *s, const gchar *s1)
363{
364 g_string_append_len(s, s1, strlen(s1) + 1);
365}
7f763a5d 366
3bbf37f2 367static void *spapr_create_fdt_skel(hwaddr initrd_base,
a8170e5e
AK
368 hwaddr initrd_size,
369 hwaddr kernel_size,
16457e7f 370 bool little_endian,
a3467baa 371 const char *boot_device,
74d042e5
DG
372 const char *kernel_cmdline,
373 uint32_t epow_irq)
9fdf0c29
DG
374{
375 void *fdt;
182735ef 376 CPUState *cs;
9fdf0c29
DG
377 uint32_t start_prop = cpu_to_be32(initrd_base);
378 uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
a1d59c0f
AK
379 GString *hypertas = g_string_sized_new(256);
380 GString *qemu_hypertas = g_string_sized_new(256);
7f763a5d 381 uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
b5cec4c5 382 uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};
833d4668 383 int smt = kvmppc_smt_threads();
6e806cc3 384 unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
10582ff8
AK
385 QemuOpts *opts = qemu_opts_find(qemu_find_opts("smp-opts"), NULL);
386 unsigned sockets = opts ? qemu_opt_get_number(opts, "sockets", 0) : 0;
387 uint32_t cpus_per_socket = sockets ? (smp_cpus / sockets) : 1;
9fdf0c29 388
a1d59c0f
AK
389 add_str(hypertas, "hcall-pft");
390 add_str(hypertas, "hcall-term");
391 add_str(hypertas, "hcall-dabr");
392 add_str(hypertas, "hcall-interrupt");
393 add_str(hypertas, "hcall-tce");
394 add_str(hypertas, "hcall-vio");
395 add_str(hypertas, "hcall-splpar");
396 add_str(hypertas, "hcall-bulk");
397 add_str(hypertas, "hcall-set-mode");
398 add_str(qemu_hypertas, "hcall-memop1");
399
7267c094 400 fdt = g_malloc0(FDT_MAX_SIZE);
9fdf0c29
DG
401 _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
402
4d8d5467
BH
403 if (kernel_size) {
404 _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
405 }
406 if (initrd_size) {
407 _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
408 }
9fdf0c29
DG
409 _FDT((fdt_finish_reservemap(fdt)));
410
411 /* Root node */
412 _FDT((fdt_begin_node(fdt, "")));
413 _FDT((fdt_property_string(fdt, "device_type", "chrp")));
5d73dd66 414 _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
d63919c9 415 _FDT((fdt_property_string(fdt, "compatible", "qemu,pseries")));
9fdf0c29
DG
416
417 _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
418 _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
419
420 /* /chosen */
421 _FDT((fdt_begin_node(fdt, "chosen")));
422
6e806cc3
BR
423 /* Set Form1_affinity */
424 _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
425
9fdf0c29
DG
426 _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
427 _FDT((fdt_property(fdt, "linux,initrd-start",
428 &start_prop, sizeof(start_prop))));
429 _FDT((fdt_property(fdt, "linux,initrd-end",
430 &end_prop, sizeof(end_prop))));
4d8d5467
BH
431 if (kernel_size) {
432 uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
433 cpu_to_be64(kernel_size) };
9fdf0c29 434
4d8d5467 435 _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
16457e7f
BH
436 if (little_endian) {
437 _FDT((fdt_property(fdt, "qemu,boot-kernel-le", NULL, 0)));
438 }
4d8d5467 439 }
2c9ee029
AS
440 if (boot_device) {
441 _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device)));
442 }
f28359d8
LZ
443 _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
444 _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
445 _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
3384f95c 446
9fdf0c29
DG
447 _FDT((fdt_end_node(fdt)));
448
9fdf0c29
DG
449 /* cpus */
450 _FDT((fdt_begin_node(fdt, "cpus")));
451
452 _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
453 _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
454
bdc44640 455 CPU_FOREACH(cs) {
182735ef
AF
456 PowerPCCPU *cpu = POWERPC_CPU(cs);
457 CPUPPCState *env = &cpu->env;
3bbf37f2 458 DeviceClass *dc = DEVICE_GET_CLASS(cs);
182735ef 459 PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cs);
0f20ba62 460 int index = ppc_get_vcpu_dt_id(cpu);
9fdf0c29
DG
461 char *nodename;
462 uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
463 0xffffffff, 0xffffffff};
0a8b2938
AG
464 uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
465 uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
5af9873d
BH
466 uint32_t page_sizes_prop[64];
467 size_t page_sizes_prop_size;
9fdf0c29 468
e97c3636
DG
469 if ((index % smt) != 0) {
470 continue;
471 }
472
3bbf37f2 473 nodename = g_strdup_printf("%s@%x", dc->fw_name, index);
9fdf0c29
DG
474
475 _FDT((fdt_begin_node(fdt, nodename)));
476
4ecf8aa5 477 g_free(nodename);
9fdf0c29 478
c7a5c0c9 479 _FDT((fdt_property_cell(fdt, "reg", index)));
9fdf0c29
DG
480 _FDT((fdt_property_string(fdt, "device_type", "cpu")));
481
482 _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));
0cbad81f 483 _FDT((fdt_property_cell(fdt, "d-cache-block-size",
9fdf0c29 484 env->dcache_line_size)));
0cbad81f
DG
485 _FDT((fdt_property_cell(fdt, "d-cache-line-size",
486 env->dcache_line_size)));
487 _FDT((fdt_property_cell(fdt, "i-cache-block-size",
488 env->icache_line_size)));
489 _FDT((fdt_property_cell(fdt, "i-cache-line-size",
9fdf0c29 490 env->icache_line_size)));
0cbad81f
DG
491
492 if (pcc->l1_dcache_size) {
493 _FDT((fdt_property_cell(fdt, "d-cache-size", pcc->l1_dcache_size)));
494 } else {
495 fprintf(stderr, "Warning: Unknown L1 dcache size for cpu\n");
496 }
497 if (pcc->l1_icache_size) {
498 _FDT((fdt_property_cell(fdt, "i-cache-size", pcc->l1_icache_size)));
499 } else {
500 fprintf(stderr, "Warning: Unknown L1 icache size for cpu\n");
501 }
502
0a8b2938
AG
503 _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));
504 _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));
9fdf0c29
DG
505 _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));
506 _FDT((fdt_property_string(fdt, "status", "okay")));
507 _FDT((fdt_property(fdt, "64-bit", NULL, 0)));
e97c3636 508
dcb861cb
AK
509 if (env->spr_cb[SPR_PURR].oea_read) {
510 _FDT((fdt_property(fdt, "ibm,purr", NULL, 0)));
511 }
512
c7a5c0c9 513 if (env->mmu_model & POWERPC_MMU_1TSEG) {
9fdf0c29
DG
514 _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",
515 segs, sizeof(segs))));
516 }
517
6659394f
DG
518 /* Advertise VMX/VSX (vector extensions) if available
519 * 0 / no property == no vector extensions
520 * 1 == VMX / Altivec available
521 * 2 == VSX available */
a7342588
DG
522 if (env->insns_flags & PPC_ALTIVEC) {
523 uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
524
6659394f
DG
525 _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));
526 }
527
528 /* Advertise DFP (Decimal Floating Point) if available
529 * 0 / no property == no DFP
530 * 1 == DFP available */
a7342588
DG
531 if (env->insns_flags2 & PPC2_DFP) {
532 _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
6659394f
DG
533 }
534
5af9873d
BH
535 page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
536 sizeof(page_sizes_prop));
537 if (page_sizes_prop_size) {
538 _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
539 page_sizes_prop, page_sizes_prop_size)));
540 }
541
10582ff8
AK
542 _FDT((fdt_property_cell(fdt, "ibm,chip-id",
543 cs->cpu_index / cpus_per_socket)));
544
9fdf0c29
DG
545 _FDT((fdt_end_node(fdt)));
546 }
547
9fdf0c29
DG
548 _FDT((fdt_end_node(fdt)));
549
f43e3525
DG
550 /* RTAS */
551 _FDT((fdt_begin_node(fdt, "rtas")));
552
da95324e
AK
553 if (!kvm_enabled() || kvmppc_spapr_use_multitce()) {
554 add_str(hypertas, "hcall-multi-tce");
555 }
a1d59c0f
AK
556 _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas->str,
557 hypertas->len)));
558 g_string_free(hypertas, TRUE);
559 _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas->str,
560 qemu_hypertas->len)));
561 g_string_free(qemu_hypertas, TRUE);
f43e3525 562
6e806cc3
BR
563 _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
564 refpoints, sizeof(refpoints))));
565
74d042e5
DG
566 _FDT((fdt_property_cell(fdt, "rtas-error-log-max", RTAS_ERROR_LOG_MAX)));
567
f43e3525
DG
568 _FDT((fdt_end_node(fdt)));
569
b5cec4c5 570 /* interrupt controller */
9dfef5aa 571 _FDT((fdt_begin_node(fdt, "interrupt-controller")));
b5cec4c5
DG
572
573 _FDT((fdt_property_string(fdt, "device_type",
574 "PowerPC-External-Interrupt-Presentation")));
575 _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
b5cec4c5
DG
576 _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
577 _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
578 interrupt_server_ranges_prop,
579 sizeof(interrupt_server_ranges_prop))));
0c103f8e
DG
580 _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
581 _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
582 _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
b5cec4c5
DG
583
584 _FDT((fdt_end_node(fdt)));
585
4040ab72
DG
586 /* vdevice */
587 _FDT((fdt_begin_node(fdt, "vdevice")));
588
589 _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
590 _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
591 _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
592 _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
b5cec4c5
DG
593 _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
594 _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
4040ab72
DG
595
596 _FDT((fdt_end_node(fdt)));
597
74d042e5
DG
598 /* event-sources */
599 spapr_events_fdt_skel(fdt, epow_irq);
600
f7d69146
AG
601 /* /hypervisor node */
602 if (kvm_enabled()) {
603 uint8_t hypercall[16];
604
605 /* indicate KVM hypercall interface */
606 _FDT((fdt_begin_node(fdt, "hypervisor")));
607 _FDT((fdt_property_string(fdt, "compatible", "linux,kvm")));
608 if (kvmppc_has_cap_fixup_hcalls()) {
609 /*
610 * Older KVM versions with older guest kernels were broken with the
611 * magic page, don't allow the guest to map it.
612 */
613 kvmppc_get_hypercall(first_cpu->env_ptr, hypercall,
614 sizeof(hypercall));
615 _FDT((fdt_property(fdt, "hcall-instructions", hypercall,
616 sizeof(hypercall))));
617 }
618 _FDT((fdt_end_node(fdt)));
619 }
620
9fdf0c29
DG
621 _FDT((fdt_end_node(fdt))); /* close root node */
622 _FDT((fdt_finish(fdt)));
623
a3467baa
DG
624 return fdt;
625}
626
2a6593cb
AK
627int spapr_h_cas_compose_response(target_ulong addr, target_ulong size)
628{
629 void *fdt, *fdt_skel;
630 sPAPRDeviceTreeUpdateHeader hdr = { .version_id = 1 };
631
632 size -= sizeof(hdr);
633
634 /* Create sceleton */
635 fdt_skel = g_malloc0(size);
636 _FDT((fdt_create(fdt_skel, size)));
637 _FDT((fdt_begin_node(fdt_skel, "")));
638 _FDT((fdt_end_node(fdt_skel)));
639 _FDT((fdt_finish(fdt_skel)));
640 fdt = g_malloc0(size);
641 _FDT((fdt_open_into(fdt_skel, fdt, size)));
642 g_free(fdt_skel);
643
3794d548
AK
644 /* Fix skeleton up */
645 _FDT((spapr_fixup_cpu_dt(fdt, spapr)));
2a6593cb
AK
646
647 /* Pack resulting tree */
648 _FDT((fdt_pack(fdt)));
649
650 if (fdt_totalsize(fdt) + sizeof(hdr) > size) {
651 trace_spapr_cas_failed(size);
652 return -1;
653 }
654
655 cpu_physical_memory_write(addr, &hdr, sizeof(hdr));
656 cpu_physical_memory_write(addr + sizeof(hdr), fdt, fdt_totalsize(fdt));
657 trace_spapr_cas_continue(fdt_totalsize(fdt) + sizeof(hdr));
658 g_free(fdt);
659
660 return 0;
661}
662
7f763a5d
DG
663static int spapr_populate_memory(sPAPREnvironment *spapr, void *fdt)
664{
665 uint32_t associativity[] = {cpu_to_be32(0x4), cpu_to_be32(0x0),
666 cpu_to_be32(0x0), cpu_to_be32(0x0),
667 cpu_to_be32(0x0)};
668 char mem_name[32];
5fe269b1 669 hwaddr node0_size, mem_start, node_size;
7f763a5d
DG
670 uint64_t mem_reg_property[2];
671 int i, off;
672
673 /* memory node(s) */
5fe269b1
PM
674 if (nb_numa_nodes > 1 && node_mem[0] < ram_size) {
675 node0_size = node_mem[0];
676 } else {
677 node0_size = ram_size;
678 }
7f763a5d
DG
679
680 /* RMA */
681 mem_reg_property[0] = 0;
682 mem_reg_property[1] = cpu_to_be64(spapr->rma_size);
683 off = fdt_add_subnode(fdt, 0, "memory@0");
684 _FDT(off);
685 _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
686 _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
687 sizeof(mem_reg_property))));
688 _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
689 sizeof(associativity))));
690
691 /* RAM: Node 0 */
692 if (node0_size > spapr->rma_size) {
693 mem_reg_property[0] = cpu_to_be64(spapr->rma_size);
694 mem_reg_property[1] = cpu_to_be64(node0_size - spapr->rma_size);
695
696 sprintf(mem_name, "memory@" TARGET_FMT_lx, spapr->rma_size);
697 off = fdt_add_subnode(fdt, 0, mem_name);
698 _FDT(off);
699 _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
700 _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
701 sizeof(mem_reg_property))));
702 _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
703 sizeof(associativity))));
704 }
705
706 /* RAM: Node 1 and beyond */
707 mem_start = node0_size;
708 for (i = 1; i < nb_numa_nodes; i++) {
709 mem_reg_property[0] = cpu_to_be64(mem_start);
5fe269b1
PM
710 if (mem_start >= ram_size) {
711 node_size = 0;
712 } else {
713 node_size = node_mem[i];
714 if (node_size > ram_size - mem_start) {
715 node_size = ram_size - mem_start;
716 }
717 }
718 mem_reg_property[1] = cpu_to_be64(node_size);
7f763a5d
DG
719 associativity[3] = associativity[4] = cpu_to_be32(i);
720 sprintf(mem_name, "memory@" TARGET_FMT_lx, mem_start);
721 off = fdt_add_subnode(fdt, 0, mem_name);
722 _FDT(off);
723 _FDT((fdt_setprop_string(fdt, off, "device_type", "memory")));
724 _FDT((fdt_setprop(fdt, off, "reg", mem_reg_property,
725 sizeof(mem_reg_property))));
726 _FDT((fdt_setprop(fdt, off, "ibm,associativity", associativity,
727 sizeof(associativity))));
5fe269b1 728 mem_start += node_size;
7f763a5d
DG
729 }
730
731 return 0;
732}
733
a3467baa 734static void spapr_finalize_fdt(sPAPREnvironment *spapr,
a8170e5e
AK
735 hwaddr fdt_addr,
736 hwaddr rtas_addr,
737 hwaddr rtas_size)
a3467baa 738{
71461b0f
AK
739 int ret, i;
740 size_t cb = 0;
741 char *bootlist;
a3467baa 742 void *fdt;
3384f95c 743 sPAPRPHBState *phb;
a3467baa 744
7267c094 745 fdt = g_malloc(FDT_MAX_SIZE);
a3467baa
DG
746
747 /* open out the base tree into a temp buffer for the final tweaks */
748 _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
4040ab72 749
7f763a5d
DG
750 ret = spapr_populate_memory(spapr, fdt);
751 if (ret < 0) {
752 fprintf(stderr, "couldn't setup memory nodes in fdt\n");
753 exit(1);
754 }
755
4040ab72
DG
756 ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
757 if (ret < 0) {
758 fprintf(stderr, "couldn't setup vio devices in fdt\n");
759 exit(1);
760 }
761
3384f95c 762 QLIST_FOREACH(phb, &spapr->phbs, list) {
e0fdbd7c 763 ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
3384f95c
DG
764 }
765
766 if (ret < 0) {
767 fprintf(stderr, "couldn't setup PCI devices in fdt\n");
768 exit(1);
769 }
770
39ac8455
DG
771 /* RTAS */
772 ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
773 if (ret < 0) {
774 fprintf(stderr, "Couldn't set up RTAS device tree properties\n");
775 }
776
6e806cc3 777 /* Advertise NUMA via ibm,associativity */
7f763a5d
DG
778 ret = spapr_fixup_cpu_dt(fdt, spapr);
779 if (ret < 0) {
780 fprintf(stderr, "Couldn't finalize CPU device tree properties\n");
6e806cc3
BR
781 }
782
71461b0f
AK
783 bootlist = get_boot_devices_list(&cb, true);
784 if (cb && bootlist) {
785 int offset = fdt_path_offset(fdt, "/chosen");
786 if (offset < 0) {
787 exit(1);
788 }
789 for (i = 0; i < cb; i++) {
790 if (bootlist[i] == '\n') {
791 bootlist[i] = ' ';
792 }
793
794 }
795 ret = fdt_setprop_string(fdt, offset, "qemu,boot-list", bootlist);
796 }
797
3fc5acde 798 if (!spapr->has_graphics) {
f28359d8
LZ
799 spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
800 }
68f3a94c 801
4040ab72
DG
802 _FDT((fdt_pack(fdt)));
803
4d8d5467
BH
804 if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
805 hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n",
806 fdt_totalsize(fdt), FDT_MAX_SIZE);
807 exit(1);
808 }
809
a3467baa 810 cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
9fdf0c29 811
7267c094 812 g_free(fdt);
9fdf0c29
DG
813}
814
815static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
816{
817 return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
818}
819
1b14670a 820static void emulate_spapr_hypercall(PowerPCCPU *cpu)
9fdf0c29 821{
1b14670a
AF
822 CPUPPCState *env = &cpu->env;
823
efcb9383
DG
824 if (msr_pr) {
825 hcall_dprintf("Hypercall made with MSR[PR]=1\n");
826 env->gpr[3] = H_PRIVILEGE;
827 } else {
aa100fa4 828 env->gpr[3] = spapr_hypercall(cpu, env->gpr[3], &env->gpr[4]);
efcb9383 829 }
9fdf0c29
DG
830}
831
7f763a5d
DG
832static void spapr_reset_htab(sPAPREnvironment *spapr)
833{
834 long shift;
835
836 /* allocate hash page table. For now we always make this 16mb,
837 * later we should probably make it scale to the size of guest
838 * RAM */
839
840 shift = kvmppc_reset_htab(spapr->htab_shift);
841
842 if (shift > 0) {
843 /* Kernel handles htab, we don't need to allocate one */
844 spapr->htab_shift = shift;
7c43bca0 845 kvmppc_kern_htab = true;
7f763a5d
DG
846 } else {
847 if (!spapr->htab) {
848 /* Allocate an htab if we don't yet have one */
849 spapr->htab = qemu_memalign(HTAB_SIZE(spapr), HTAB_SIZE(spapr));
850 }
851
852 /* And clear it */
853 memset(spapr->htab, 0, HTAB_SIZE(spapr));
854 }
855
856 /* Update the RMA size if necessary */
857 if (spapr->vrma_adjust) {
c4177479
AK
858 hwaddr node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size;
859 spapr->rma_size = kvmppc_rma_size(node0_size, spapr->htab_shift);
7f763a5d 860 }
9fdf0c29
DG
861}
862
c8787ad4 863static void ppc_spapr_reset(void)
a3467baa 864{
182735ef 865 PowerPCCPU *first_ppc_cpu;
259186a7 866
7f763a5d
DG
867 /* Reset the hash table & recalc the RMA */
868 spapr_reset_htab(spapr);
a3467baa 869
c8787ad4 870 qemu_devices_reset();
a3467baa
DG
871
872 /* Load the fdt */
873 spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
874 spapr->rtas_size);
875
876 /* Set up the entry state */
182735ef
AF
877 first_ppc_cpu = POWERPC_CPU(first_cpu);
878 first_ppc_cpu->env.gpr[3] = spapr->fdt_addr;
879 first_ppc_cpu->env.gpr[5] = 0;
880 first_cpu->halted = 0;
881 first_ppc_cpu->env.nip = spapr->entry_point;
a3467baa
DG
882
883}
884
1bba0dc9
AF
885static void spapr_cpu_reset(void *opaque)
886{
5b2038e0 887 PowerPCCPU *cpu = opaque;
259186a7 888 CPUState *cs = CPU(cpu);
048706d9 889 CPUPPCState *env = &cpu->env;
1bba0dc9 890
259186a7 891 cpu_reset(cs);
048706d9
DG
892
893 /* All CPUs start halted. CPU0 is unhalted from the machine level
894 * reset code and the rest are explicitly started up by the guest
895 * using an RTAS call */
259186a7 896 cs->halted = 1;
048706d9
DG
897
898 env->spr[SPR_HIOR] = 0;
7f763a5d 899
4be21d56 900 env->external_htab = (uint8_t *)spapr->htab;
5736245c
AK
901 if (kvm_enabled() && !env->external_htab) {
902 /*
903 * HV KVM, set external_htab to 1 so our ppc_hash64_load_hpte*
904 * functions do the right thing.
905 */
906 env->external_htab = (void *)1;
907 }
7f763a5d 908 env->htab_base = -1;
f3c75d42
AK
909 /*
910 * htab_mask is the mask used to normalize hash value to PTEG index.
911 * htab_shift is log2 of hash table size.
912 * We have 8 hpte per group, and each hpte is 16 bytes.
913 * ie have 128 bytes per hpte entry.
914 */
915 env->htab_mask = (1ULL << ((spapr)->htab_shift - 7)) - 1;
ec4936e1 916 env->spr[SPR_SDR1] = (target_ulong)(uintptr_t)spapr->htab |
7f763a5d 917 (spapr->htab_shift - 18);
1bba0dc9
AF
918}
919
639e8102
DG
920static void spapr_create_nvram(sPAPREnvironment *spapr)
921{
2ff3de68 922 DeviceState *dev = qdev_create(&spapr->vio_bus->bus, "spapr-nvram");
3978b863 923 DriveInfo *dinfo = drive_get(IF_PFLASH, 0, 0);
639e8102 924
3978b863
PB
925 if (dinfo) {
926 qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv);
639e8102
DG
927 }
928
929 qdev_init_nofail(dev);
930
931 spapr->nvram = (struct sPAPRNVRAM *)dev;
932}
933
8c57b867 934/* Returns whether we want to use VGA or not */
f28359d8
LZ
935static int spapr_vga_init(PCIBus *pci_bus)
936{
8c57b867 937 switch (vga_interface_type) {
8c57b867 938 case VGA_NONE:
7effdaa3
MW
939 return false;
940 case VGA_DEVICE:
941 return true;
1ddcae82
AJ
942 case VGA_STD:
943 return pci_vga_init(pci_bus) != NULL;
8c57b867 944 default:
f28359d8
LZ
945 fprintf(stderr, "This vga model is not supported,"
946 "currently it only supports -vga std\n");
8c57b867 947 exit(0);
f28359d8 948 }
f28359d8
LZ
949}
950
4be21d56
DG
951static const VMStateDescription vmstate_spapr = {
952 .name = "spapr",
98a8b524 953 .version_id = 2,
4be21d56 954 .minimum_version_id = 1,
3aff6c2f 955 .fields = (VMStateField[]) {
4be21d56
DG
956 VMSTATE_UINT32(next_irq, sPAPREnvironment),
957
958 /* RTC offset */
959 VMSTATE_UINT64(rtc_offset, sPAPREnvironment),
98a8b524 960 VMSTATE_PPC_TIMEBASE_V(tb, sPAPREnvironment, 2),
4be21d56
DG
961 VMSTATE_END_OF_LIST()
962 },
963};
964
965#define HPTE(_table, _i) (void *)(((uint64_t *)(_table)) + ((_i) * 2))
966#define HPTE_VALID(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_VALID)
967#define HPTE_DIRTY(_hpte) (tswap64(*((uint64_t *)(_hpte))) & HPTE64_V_HPTE_DIRTY)
968#define CLEAN_HPTE(_hpte) ((*(uint64_t *)(_hpte)) &= tswap64(~HPTE64_V_HPTE_DIRTY))
969
970static int htab_save_setup(QEMUFile *f, void *opaque)
971{
972 sPAPREnvironment *spapr = opaque;
973
4be21d56
DG
974 /* "Iteration" header */
975 qemu_put_be32(f, spapr->htab_shift);
976
e68cb8b4
AK
977 if (spapr->htab) {
978 spapr->htab_save_index = 0;
979 spapr->htab_first_pass = true;
980 } else {
981 assert(kvm_enabled());
982
983 spapr->htab_fd = kvmppc_get_htab_fd(false);
984 if (spapr->htab_fd < 0) {
985 fprintf(stderr, "Unable to open fd for reading hash table from KVM: %s\n",
986 strerror(errno));
987 return -1;
988 }
989 }
990
991
4be21d56
DG
992 return 0;
993}
994
4be21d56
DG
995static void htab_save_first_pass(QEMUFile *f, sPAPREnvironment *spapr,
996 int64_t max_ns)
997{
998 int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
999 int index = spapr->htab_save_index;
bc72ad67 1000 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
4be21d56
DG
1001
1002 assert(spapr->htab_first_pass);
1003
1004 do {
1005 int chunkstart;
1006
1007 /* Consume invalid HPTEs */
1008 while ((index < htabslots)
1009 && !HPTE_VALID(HPTE(spapr->htab, index))) {
1010 index++;
1011 CLEAN_HPTE(HPTE(spapr->htab, index));
1012 }
1013
1014 /* Consume valid HPTEs */
1015 chunkstart = index;
1016 while ((index < htabslots)
1017 && HPTE_VALID(HPTE(spapr->htab, index))) {
1018 index++;
1019 CLEAN_HPTE(HPTE(spapr->htab, index));
1020 }
1021
1022 if (index > chunkstart) {
1023 int n_valid = index - chunkstart;
1024
1025 qemu_put_be32(f, chunkstart);
1026 qemu_put_be16(f, n_valid);
1027 qemu_put_be16(f, 0);
1028 qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
1029 HASH_PTE_SIZE_64 * n_valid);
1030
bc72ad67 1031 if ((qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
4be21d56
DG
1032 break;
1033 }
1034 }
1035 } while ((index < htabslots) && !qemu_file_rate_limit(f));
1036
1037 if (index >= htabslots) {
1038 assert(index == htabslots);
1039 index = 0;
1040 spapr->htab_first_pass = false;
1041 }
1042 spapr->htab_save_index = index;
1043}
1044
e68cb8b4
AK
1045static int htab_save_later_pass(QEMUFile *f, sPAPREnvironment *spapr,
1046 int64_t max_ns)
4be21d56
DG
1047{
1048 bool final = max_ns < 0;
1049 int htabslots = HTAB_SIZE(spapr) / HASH_PTE_SIZE_64;
1050 int examined = 0, sent = 0;
1051 int index = spapr->htab_save_index;
bc72ad67 1052 int64_t starttime = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
4be21d56
DG
1053
1054 assert(!spapr->htab_first_pass);
1055
1056 do {
1057 int chunkstart, invalidstart;
1058
1059 /* Consume non-dirty HPTEs */
1060 while ((index < htabslots)
1061 && !HPTE_DIRTY(HPTE(spapr->htab, index))) {
1062 index++;
1063 examined++;
1064 }
1065
1066 chunkstart = index;
1067 /* Consume valid dirty HPTEs */
1068 while ((index < htabslots)
1069 && HPTE_DIRTY(HPTE(spapr->htab, index))
1070 && HPTE_VALID(HPTE(spapr->htab, index))) {
1071 CLEAN_HPTE(HPTE(spapr->htab, index));
1072 index++;
1073 examined++;
1074 }
1075
1076 invalidstart = index;
1077 /* Consume invalid dirty HPTEs */
1078 while ((index < htabslots)
1079 && HPTE_DIRTY(HPTE(spapr->htab, index))
1080 && !HPTE_VALID(HPTE(spapr->htab, index))) {
1081 CLEAN_HPTE(HPTE(spapr->htab, index));
1082 index++;
1083 examined++;
1084 }
1085
1086 if (index > chunkstart) {
1087 int n_valid = invalidstart - chunkstart;
1088 int n_invalid = index - invalidstart;
1089
1090 qemu_put_be32(f, chunkstart);
1091 qemu_put_be16(f, n_valid);
1092 qemu_put_be16(f, n_invalid);
1093 qemu_put_buffer(f, HPTE(spapr->htab, chunkstart),
1094 HASH_PTE_SIZE_64 * n_valid);
1095 sent += index - chunkstart;
1096
bc72ad67 1097 if (!final && (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - starttime) > max_ns) {
4be21d56
DG
1098 break;
1099 }
1100 }
1101
1102 if (examined >= htabslots) {
1103 break;
1104 }
1105
1106 if (index >= htabslots) {
1107 assert(index == htabslots);
1108 index = 0;
1109 }
1110 } while ((examined < htabslots) && (!qemu_file_rate_limit(f) || final));
1111
1112 if (index >= htabslots) {
1113 assert(index == htabslots);
1114 index = 0;
1115 }
1116
1117 spapr->htab_save_index = index;
1118
e68cb8b4 1119 return (examined >= htabslots) && (sent == 0) ? 1 : 0;
4be21d56
DG
1120}
1121
e68cb8b4
AK
1122#define MAX_ITERATION_NS 5000000 /* 5 ms */
1123#define MAX_KVM_BUF_SIZE 2048
1124
4be21d56
DG
1125static int htab_save_iterate(QEMUFile *f, void *opaque)
1126{
1127 sPAPREnvironment *spapr = opaque;
e68cb8b4 1128 int rc = 0;
4be21d56
DG
1129
1130 /* Iteration header */
1131 qemu_put_be32(f, 0);
1132
e68cb8b4
AK
1133 if (!spapr->htab) {
1134 assert(kvm_enabled());
1135
1136 rc = kvmppc_save_htab(f, spapr->htab_fd,
1137 MAX_KVM_BUF_SIZE, MAX_ITERATION_NS);
1138 if (rc < 0) {
1139 return rc;
1140 }
1141 } else if (spapr->htab_first_pass) {
4be21d56
DG
1142 htab_save_first_pass(f, spapr, MAX_ITERATION_NS);
1143 } else {
e68cb8b4 1144 rc = htab_save_later_pass(f, spapr, MAX_ITERATION_NS);
4be21d56
DG
1145 }
1146
1147 /* End marker */
1148 qemu_put_be32(f, 0);
1149 qemu_put_be16(f, 0);
1150 qemu_put_be16(f, 0);
1151
e68cb8b4 1152 return rc;
4be21d56
DG
1153}
1154
1155static int htab_save_complete(QEMUFile *f, void *opaque)
1156{
1157 sPAPREnvironment *spapr = opaque;
1158
1159 /* Iteration header */
1160 qemu_put_be32(f, 0);
1161
e68cb8b4
AK
1162 if (!spapr->htab) {
1163 int rc;
1164
1165 assert(kvm_enabled());
1166
1167 rc = kvmppc_save_htab(f, spapr->htab_fd, MAX_KVM_BUF_SIZE, -1);
1168 if (rc < 0) {
1169 return rc;
1170 }
1171 close(spapr->htab_fd);
1172 spapr->htab_fd = -1;
1173 } else {
1174 htab_save_later_pass(f, spapr, -1);
1175 }
4be21d56
DG
1176
1177 /* End marker */
1178 qemu_put_be32(f, 0);
1179 qemu_put_be16(f, 0);
1180 qemu_put_be16(f, 0);
1181
1182 return 0;
1183}
1184
1185static int htab_load(QEMUFile *f, void *opaque, int version_id)
1186{
1187 sPAPREnvironment *spapr = opaque;
1188 uint32_t section_hdr;
e68cb8b4 1189 int fd = -1;
4be21d56
DG
1190
1191 if (version_id < 1 || version_id > 1) {
1192 fprintf(stderr, "htab_load() bad version\n");
1193 return -EINVAL;
1194 }
1195
1196 section_hdr = qemu_get_be32(f);
1197
1198 if (section_hdr) {
1199 /* First section, just the hash shift */
1200 if (spapr->htab_shift != section_hdr) {
1201 return -EINVAL;
1202 }
1203 return 0;
1204 }
1205
e68cb8b4
AK
1206 if (!spapr->htab) {
1207 assert(kvm_enabled());
1208
1209 fd = kvmppc_get_htab_fd(true);
1210 if (fd < 0) {
1211 fprintf(stderr, "Unable to open fd to restore KVM hash table: %s\n",
1212 strerror(errno));
1213 }
1214 }
1215
4be21d56
DG
1216 while (true) {
1217 uint32_t index;
1218 uint16_t n_valid, n_invalid;
1219
1220 index = qemu_get_be32(f);
1221 n_valid = qemu_get_be16(f);
1222 n_invalid = qemu_get_be16(f);
1223
1224 if ((index == 0) && (n_valid == 0) && (n_invalid == 0)) {
1225 /* End of Stream */
1226 break;
1227 }
1228
e68cb8b4 1229 if ((index + n_valid + n_invalid) >
4be21d56
DG
1230 (HTAB_SIZE(spapr) / HASH_PTE_SIZE_64)) {
1231 /* Bad index in stream */
1232 fprintf(stderr, "htab_load() bad index %d (%hd+%hd entries) "
e68cb8b4
AK
1233 "in htab stream (htab_shift=%d)\n", index, n_valid, n_invalid,
1234 spapr->htab_shift);
4be21d56
DG
1235 return -EINVAL;
1236 }
1237
e68cb8b4
AK
1238 if (spapr->htab) {
1239 if (n_valid) {
1240 qemu_get_buffer(f, HPTE(spapr->htab, index),
1241 HASH_PTE_SIZE_64 * n_valid);
1242 }
1243 if (n_invalid) {
1244 memset(HPTE(spapr->htab, index + n_valid), 0,
1245 HASH_PTE_SIZE_64 * n_invalid);
1246 }
1247 } else {
1248 int rc;
1249
1250 assert(fd >= 0);
1251
1252 rc = kvmppc_load_htab_chunk(f, fd, index, n_valid, n_invalid);
1253 if (rc < 0) {
1254 return rc;
1255 }
4be21d56
DG
1256 }
1257 }
1258
e68cb8b4
AK
1259 if (!spapr->htab) {
1260 assert(fd >= 0);
1261 close(fd);
1262 }
1263
4be21d56
DG
1264 return 0;
1265}
1266
1267static SaveVMHandlers savevm_htab_handlers = {
1268 .save_live_setup = htab_save_setup,
1269 .save_live_iterate = htab_save_iterate,
1270 .save_live_complete = htab_save_complete,
1271 .load_state = htab_load,
1272};
1273
9fdf0c29 1274/* pSeries LPAR / sPAPR hardware init */
3ef96221 1275static void ppc_spapr_init(MachineState *machine)
9fdf0c29 1276{
3ef96221
MA
1277 ram_addr_t ram_size = machine->ram_size;
1278 const char *cpu_model = machine->cpu_model;
1279 const char *kernel_filename = machine->kernel_filename;
1280 const char *kernel_cmdline = machine->kernel_cmdline;
1281 const char *initrd_filename = machine->initrd_filename;
1282 const char *boot_device = machine->boot_order;
05769733 1283 PowerPCCPU *cpu;
e2684c0b 1284 CPUPPCState *env;
8c9f64df 1285 PCIHostState *phb;
9fdf0c29 1286 int i;
890c2b77
AK
1287 MemoryRegion *sysmem = get_system_memory();
1288 MemoryRegion *ram = g_new(MemoryRegion, 1);
a8170e5e 1289 hwaddr rma_alloc_size;
c4177479 1290 hwaddr node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size;
4d8d5467
BH
1291 uint32_t initrd_base = 0;
1292 long kernel_size = 0, initrd_size = 0;
1293 long load_limit, rtas_limit, fw_size;
16457e7f 1294 bool kernel_le = false;
39ac8455 1295 char *filename;
9fdf0c29 1296
0ee2c058
AK
1297 msi_supported = true;
1298
d43b45e2
DG
1299 spapr = g_malloc0(sizeof(*spapr));
1300 QLIST_INIT(&spapr->phbs);
1301
9fdf0c29
DG
1302 cpu_ppc_hypercall = emulate_spapr_hypercall;
1303
354ac20a
DG
1304 /* Allocate RMA if necessary */
1305 rma_alloc_size = kvmppc_alloc_rma("ppc_spapr.rma", sysmem);
1306
1307 if (rma_alloc_size == -1) {
1308 hw_error("qemu: Unable to create RMA\n");
1309 exit(1);
1310 }
7f763a5d 1311
c4177479 1312 if (rma_alloc_size && (rma_alloc_size < node0_size)) {
7f763a5d 1313 spapr->rma_size = rma_alloc_size;
354ac20a 1314 } else {
c4177479 1315 spapr->rma_size = node0_size;
7f763a5d
DG
1316
1317 /* With KVM, we don't actually know whether KVM supports an
1318 * unbounded RMA (PR KVM) or is limited by the hash table size
1319 * (HV KVM using VRMA), so we always assume the latter
1320 *
1321 * In that case, we also limit the initial allocations for RTAS
1322 * etc... to 256M since we have no way to know what the VRMA size
1323 * is going to be as it depends on the size of the hash table
1324 * isn't determined yet.
1325 */
1326 if (kvm_enabled()) {
1327 spapr->vrma_adjust = 1;
1328 spapr->rma_size = MIN(spapr->rma_size, 0x10000000);
1329 }
354ac20a
DG
1330 }
1331
c4177479
AK
1332 if (spapr->rma_size > node0_size) {
1333 fprintf(stderr, "Error: Numa node 0 has to span the RMA (%#08"HWADDR_PRIx")\n",
1334 spapr->rma_size);
1335 exit(1);
1336 }
1337
4d8d5467 1338 /* We place the device tree and RTAS just below either the top of the RMA,
354ac20a
DG
1339 * or just below 2GB, whichever is lowere, so that it can be
1340 * processed with 32-bit real mode code if necessary */
7f763a5d 1341 rtas_limit = MIN(spapr->rma_size, 0x80000000);
4d8d5467
BH
1342 spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
1343 spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
1344 load_limit = spapr->fdt_addr - FW_OVERHEAD;
9fdf0c29 1345
382be75d
DG
1346 /* We aim for a hash table of size 1/128 the size of RAM. The
1347 * normal rule of thumb is 1/64 the size of RAM, but that's much
1348 * more than needed for the Linux guests we support. */
1349 spapr->htab_shift = 18; /* Minimum architected size */
1350 while (spapr->htab_shift <= 46) {
1351 if ((1ULL << (spapr->htab_shift + 7)) >= ram_size) {
1352 break;
1353 }
1354 spapr->htab_shift++;
1355 }
7f763a5d 1356
7b565160
DG
1357 /* Set up Interrupt Controller before we create the VCPUs */
1358 spapr->icp = xics_system_init(smp_cpus * kvmppc_smt_threads() / smp_threads,
1359 XICS_IRQS);
1360 spapr->next_irq = XICS_IRQ_BASE;
1361
9fdf0c29
DG
1362 /* init CPUs */
1363 if (cpu_model == NULL) {
6b7a2cf6 1364 cpu_model = kvm_enabled() ? "host" : "POWER7";
9fdf0c29
DG
1365 }
1366 for (i = 0; i < smp_cpus; i++) {
05769733
AF
1367 cpu = cpu_ppc_init(cpu_model);
1368 if (cpu == NULL) {
9fdf0c29
DG
1369 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
1370 exit(1);
1371 }
05769733
AF
1372 env = &cpu->env;
1373
9fdf0c29
DG
1374 /* Set time-base frequency to 512 MHz */
1375 cpu_ppc_tb_init(env, TIMEBASE_FREQ);
9fdf0c29 1376
2cf3eb6d
FC
1377 /* PAPR always has exception vectors in RAM not ROM. To ensure this,
1378 * MSR[IP] should never be set.
1379 */
1380 env->msr_mask &= ~(1 << 6);
048706d9
DG
1381
1382 /* Tell KVM that we're in PAPR mode */
1383 if (kvm_enabled()) {
1bc22652 1384 kvmppc_set_papr(cpu);
048706d9
DG
1385 }
1386
6d9412ea
AK
1387 if (cpu->max_compat) {
1388 if (ppc_set_compat(cpu, cpu->max_compat) < 0) {
1389 exit(1);
1390 }
1391 }
1392
24408a7d
AK
1393 xics_cpu_setup(spapr->icp, cpu);
1394
048706d9 1395 qemu_register_reset(spapr_cpu_reset, cpu);
9fdf0c29
DG
1396 }
1397
1398 /* allocate RAM */
f73a2575 1399 spapr->ram_limit = ram_size;
354ac20a
DG
1400 if (spapr->ram_limit > rma_alloc_size) {
1401 ram_addr_t nonrma_base = rma_alloc_size;
1402 ram_addr_t nonrma_size = spapr->ram_limit - rma_alloc_size;
1403
2c9b15ca 1404 memory_region_init_ram(ram, NULL, "ppc_spapr.ram", nonrma_size);
c5705a77 1405 vmstate_register_ram_global(ram);
354ac20a
DG
1406 memory_region_add_subregion(sysmem, nonrma_base, ram);
1407 }
9fdf0c29 1408
39ac8455 1409 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
a3467baa 1410 spapr->rtas_size = load_image_targphys(filename, spapr->rtas_addr,
4d8d5467 1411 rtas_limit - spapr->rtas_addr);
a3467baa 1412 if (spapr->rtas_size < 0) {
39ac8455
DG
1413 hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
1414 exit(1);
1415 }
4d8d5467
BH
1416 if (spapr->rtas_size > RTAS_MAX_SIZE) {
1417 hw_error("RTAS too big ! 0x%lx bytes (max is 0x%x)\n",
1418 spapr->rtas_size, RTAS_MAX_SIZE);
1419 exit(1);
1420 }
7267c094 1421 g_free(filename);
39ac8455 1422
74d042e5
DG
1423 /* Set up EPOW events infrastructure */
1424 spapr_events_init(spapr);
1425
b5cec4c5 1426 /* Set up VIO bus */
4040ab72
DG
1427 spapr->vio_bus = spapr_vio_bus_init();
1428
277f9acf 1429 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4040ab72 1430 if (serial_hds[i]) {
d601fac4 1431 spapr_vty_create(spapr->vio_bus, serial_hds[i]);
4040ab72
DG
1432 }
1433 }
9fdf0c29 1434
639e8102
DG
1435 /* We always have at least the nvram device on VIO */
1436 spapr_create_nvram(spapr);
1437
3384f95c 1438 /* Set up PCI */
f1c2dc7c 1439 spapr_pci_msi_init(spapr, SPAPR_PCI_MSI_WINDOW);
fa28f71b
AK
1440 spapr_pci_rtas_init();
1441
89dfd6e1 1442 phb = spapr_create_phb(spapr, 0);
3384f95c 1443
277f9acf 1444 for (i = 0; i < nb_nics; i++) {
8d90ad90
DG
1445 NICInfo *nd = &nd_table[i];
1446
1447 if (!nd->model) {
7267c094 1448 nd->model = g_strdup("ibmveth");
8d90ad90
DG
1449 }
1450
1451 if (strcmp(nd->model, "ibmveth") == 0) {
d601fac4 1452 spapr_vlan_create(spapr->vio_bus, nd);
8d90ad90 1453 } else {
29b358f9 1454 pci_nic_init_nofail(&nd_table[i], phb->bus, nd->model, NULL);
8d90ad90
DG
1455 }
1456 }
1457
6e270446 1458 for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
d601fac4 1459 spapr_vscsi_create(spapr->vio_bus);
6e270446
BH
1460 }
1461
f28359d8 1462 /* Graphics */
8c9f64df 1463 if (spapr_vga_init(phb->bus)) {
3fc5acde 1464 spapr->has_graphics = true;
f28359d8
LZ
1465 }
1466
094b287f 1467 if (usb_enabled(spapr->has_graphics)) {
8c9f64df 1468 pci_create_simple(phb->bus, -1, "pci-ohci");
35139a59
DG
1469 if (spapr->has_graphics) {
1470 usbdevice_create("keyboard");
1471 usbdevice_create("mouse");
1472 }
1473 }
1474
7f763a5d 1475 if (spapr->rma_size < (MIN_RMA_SLOF << 20)) {
4d8d5467
BH
1476 fprintf(stderr, "qemu: pSeries SLOF firmware requires >= "
1477 "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF);
1478 exit(1);
1479 }
1480
9fdf0c29
DG
1481 if (kernel_filename) {
1482 uint64_t lowaddr = 0;
1483
9fdf0c29
DG
1484 kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
1485 NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0);
3b66da82 1486 if (kernel_size == ELF_LOAD_WRONG_ENDIAN) {
16457e7f
BH
1487 kernel_size = load_elf(kernel_filename,
1488 translate_kernel_address, NULL,
1489 NULL, &lowaddr, NULL, 0, ELF_MACHINE, 0);
1490 kernel_le = kernel_size > 0;
1491 }
9fdf0c29 1492 if (kernel_size < 0) {
3b66da82
AK
1493 fprintf(stderr, "qemu: error loading %s: %s\n",
1494 kernel_filename, load_elf_strerror(kernel_size));
9fdf0c29
DG
1495 exit(1);
1496 }
1497
1498 /* load initrd */
1499 if (initrd_filename) {
4d8d5467
BH
1500 /* Try to locate the initrd in the gap between the kernel
1501 * and the firmware. Add a bit of space just in case
1502 */
1503 initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
9fdf0c29 1504 initrd_size = load_image_targphys(initrd_filename, initrd_base,
4d8d5467 1505 load_limit - initrd_base);
9fdf0c29
DG
1506 if (initrd_size < 0) {
1507 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
1508 initrd_filename);
1509 exit(1);
1510 }
1511 } else {
1512 initrd_base = 0;
1513 initrd_size = 0;
1514 }
4d8d5467 1515 }
a3467baa 1516
8e7ea787
AF
1517 if (bios_name == NULL) {
1518 bios_name = FW_FILE_NAME;
1519 }
1520 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
4d8d5467
BH
1521 fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
1522 if (fw_size < 0) {
1523 hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
1524 exit(1);
1525 }
1526 g_free(filename);
4d8d5467
BH
1527
1528 spapr->entry_point = 0x100;
1529
4be21d56
DG
1530 vmstate_register(NULL, 0, &vmstate_spapr, spapr);
1531 register_savevm_live(NULL, "spapr/htab", -1, 1,
1532 &savevm_htab_handlers, spapr);
1533
9fdf0c29 1534 /* Prepare the device tree */
3bbf37f2 1535 spapr->fdt_skel = spapr_create_fdt_skel(initrd_base, initrd_size,
16457e7f 1536 kernel_size, kernel_le,
74d042e5
DG
1537 boot_device, kernel_cmdline,
1538 spapr->epow_irq);
a3467baa 1539 assert(spapr->fdt_skel != NULL);
9fdf0c29
DG
1540}
1541
135a129a
AK
1542static int spapr_kvm_type(const char *vm_type)
1543{
1544 if (!vm_type) {
1545 return 0;
1546 }
1547
1548 if (!strcmp(vm_type, "HV")) {
1549 return 1;
1550 }
1551
1552 if (!strcmp(vm_type, "PR")) {
1553 return 2;
1554 }
1555
1556 error_report("Unknown kvm-type specified '%s'", vm_type);
1557 exit(1);
1558}
1559
71461b0f
AK
1560/*
1561 * Implementation of an interface to adjust firmware patch
1562 * for the bootindex property handling.
1563 */
1564static char *spapr_get_fw_dev_path(FWPathProvider *p, BusState *bus,
1565 DeviceState *dev)
1566{
1567#define CAST(type, obj, name) \
1568 ((type *)object_dynamic_cast(OBJECT(obj), (name)))
1569 SCSIDevice *d = CAST(SCSIDevice, dev, TYPE_SCSI_DEVICE);
1570 sPAPRPHBState *phb = CAST(sPAPRPHBState, dev, TYPE_SPAPR_PCI_HOST_BRIDGE);
1571
1572 if (d) {
1573 void *spapr = CAST(void, bus->parent, "spapr-vscsi");
1574 VirtIOSCSI *virtio = CAST(VirtIOSCSI, bus->parent, TYPE_VIRTIO_SCSI);
1575 USBDevice *usb = CAST(USBDevice, bus->parent, TYPE_USB_DEVICE);
1576
1577 if (spapr) {
1578 /*
1579 * Replace "channel@0/disk@0,0" with "disk@8000000000000000":
1580 * We use SRP luns of the form 8000 | (bus << 8) | (id << 5) | lun
1581 * in the top 16 bits of the 64-bit LUN
1582 */
1583 unsigned id = 0x8000 | (d->id << 8) | d->lun;
1584 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
1585 (uint64_t)id << 48);
1586 } else if (virtio) {
1587 /*
1588 * We use SRP luns of the form 01000000 | (target << 8) | lun
1589 * in the top 32 bits of the 64-bit LUN
1590 * Note: the quote above is from SLOF and it is wrong,
1591 * the actual binding is:
1592 * swap 0100 or 10 << or 20 << ( target lun-id -- srplun )
1593 */
1594 unsigned id = 0x1000000 | (d->id << 16) | d->lun;
1595 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
1596 (uint64_t)id << 32);
1597 } else if (usb) {
1598 /*
1599 * We use SRP luns of the form 01000000 | (usb-port << 16) | lun
1600 * in the top 32 bits of the 64-bit LUN
1601 */
1602 unsigned usb_port = atoi(usb->port->path);
1603 unsigned id = 0x1000000 | (usb_port << 16) | d->lun;
1604 return g_strdup_printf("%s@%"PRIX64, qdev_fw_name(dev),
1605 (uint64_t)id << 32);
1606 }
1607 }
1608
1609 if (phb) {
1610 /* Replace "pci" with "pci@800000020000000" */
1611 return g_strdup_printf("pci@%"PRIX64, phb->buid);
1612 }
1613
1614 return NULL;
1615}
1616
29ee3247
AK
1617static void spapr_machine_class_init(ObjectClass *oc, void *data)
1618{
1619 MachineClass *mc = MACHINE_CLASS(oc);
71461b0f 1620 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(oc);
958db90c
MA
1621
1622 mc->name = "pseries";
1623 mc->desc = "pSeries Logical Partition (PAPR compliant)";
1624 mc->is_default = 1;
1625 mc->init = ppc_spapr_init;
1626 mc->reset = ppc_spapr_reset;
1627 mc->block_default_type = IF_SCSI;
1628 mc->max_cpus = MAX_CPUS;
1629 mc->no_parallel = 1;
1630 mc->default_boot_order = NULL;
1631 mc->kvm_type = spapr_kvm_type;
00b4fbe2 1632
71461b0f 1633 fwc->get_dev_path = spapr_get_fw_dev_path;
29ee3247
AK
1634}
1635
1636static const TypeInfo spapr_machine_info = {
1637 .name = TYPE_SPAPR_MACHINE,
1638 .parent = TYPE_MACHINE,
748abce9 1639 .instance_size = sizeof(SPAPRMachine),
29ee3247 1640 .class_init = spapr_machine_class_init,
71461b0f
AK
1641 .interfaces = (InterfaceInfo[]) {
1642 { TYPE_FW_PATH_PROVIDER },
1643 { }
1644 },
29ee3247
AK
1645};
1646
1647static void spapr_machine_register_types(void)
9fdf0c29 1648{
29ee3247 1649 type_register_static(&spapr_machine_info);
9fdf0c29
DG
1650}
1651
29ee3247 1652type_init(spapr_machine_register_types)