]> git.proxmox.com Git - mirror_qemu.git/blame - hw/spapr.c
pseries: Remove extraneous prints
[mirror_qemu.git] / hw / 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 */
27#include "sysemu.h"
9fdf0c29
DG
28#include "hw.h"
29#include "elf.h"
8d90ad90 30#include "net.h"
6e270446 31#include "blockdev.h"
e97c3636
DG
32#include "cpus.h"
33#include "kvm.h"
34#include "kvm_ppc.h"
9fdf0c29
DG
35
36#include "hw/boards.h"
37#include "hw/ppc.h"
38#include "hw/loader.h"
39
40#include "hw/spapr.h"
4040ab72 41#include "hw/spapr_vio.h"
3384f95c 42#include "hw/spapr_pci.h"
b5cec4c5 43#include "hw/xics.h"
9fdf0c29 44
f61b4bed
AG
45#include "kvm.h"
46#include "kvm_ppc.h"
3384f95c 47#include "pci.h"
f28359d8 48#include "vga-pci.h"
f61b4bed 49
890c2b77
AK
50#include "exec-memory.h"
51
9fdf0c29
DG
52#include <libfdt.h>
53
4d8d5467
BH
54/* SLOF memory layout:
55 *
56 * SLOF raw image loaded at 0, copies its romfs right below the flat
57 * device-tree, then position SLOF itself 31M below that
58 *
59 * So we set FW_OVERHEAD to 40MB which should account for all of that
60 * and more
61 *
62 * We load our kernel at 4M, leaving space for SLOF initial image
63 */
9fdf0c29 64#define FDT_MAX_SIZE 0x10000
39ac8455 65#define RTAS_MAX_SIZE 0x10000
a9f8ad8f
DG
66#define FW_MAX_SIZE 0x400000
67#define FW_FILE_NAME "slof.bin"
4d8d5467
BH
68#define FW_OVERHEAD 0x2800000
69#define KERNEL_LOAD_ADDR FW_MAX_SIZE
a9f8ad8f 70
4d8d5467 71#define MIN_RMA_SLOF 128UL
9fdf0c29
DG
72
73#define TIMEBASE_FREQ 512000000ULL
74
41019fec 75#define MAX_CPUS 256
4d8d5467 76#define XICS_IRQS 1024
9fdf0c29 77
3384f95c
DG
78#define SPAPR_PCI_BUID 0x800000020000001ULL
79#define SPAPR_PCI_MEM_WIN_ADDR (0x10000000000ULL + 0xA0000000)
80#define SPAPR_PCI_MEM_WIN_SIZE 0x20000000
81#define SPAPR_PCI_IO_WIN_ADDR (0x10000000000ULL + 0x80000000)
82
0c103f8e
DG
83#define PHANDLE_XICP 0x00001111
84
9fdf0c29
DG
85sPAPREnvironment *spapr;
86
d07fee7e
DG
87qemu_irq spapr_allocate_irq(uint32_t hint, uint32_t *irq_num,
88 enum xics_irq_type type)
e6c866d4
DG
89{
90 uint32_t irq;
91 qemu_irq qirq;
92
93 if (hint) {
94 irq = hint;
95 /* FIXME: we should probably check for collisions somehow */
96 } else {
97 irq = spapr->next_irq++;
98 }
99
d07fee7e 100 qirq = xics_assign_irq(spapr->icp, irq, type);
e6c866d4
DG
101 if (!qirq) {
102 return NULL;
103 }
104
105 if (irq_num) {
106 *irq_num = irq;
107 }
108
109 return qirq;
110}
111
6e806cc3
BR
112static int spapr_set_associativity(void *fdt, sPAPREnvironment *spapr)
113{
114 int ret = 0, offset;
e2684c0b 115 CPUPPCState *env;
6e806cc3
BR
116 char cpu_model[32];
117 int smt = kvmppc_smt_threads();
118
119 assert(spapr->cpu_model);
120
121 for (env = first_cpu; env != NULL; env = env->next_cpu) {
122 uint32_t associativity[] = {cpu_to_be32(0x5),
123 cpu_to_be32(0x0),
124 cpu_to_be32(0x0),
125 cpu_to_be32(0x0),
126 cpu_to_be32(env->numa_node),
127 cpu_to_be32(env->cpu_index)};
128
129 if ((env->cpu_index % smt) != 0) {
130 continue;
131 }
132
133 snprintf(cpu_model, 32, "/cpus/%s@%x", spapr->cpu_model,
134 env->cpu_index);
135
136 offset = fdt_path_offset(fdt, cpu_model);
137 if (offset < 0) {
138 return offset;
139 }
140
141 ret = fdt_setprop(fdt, offset, "ibm,associativity", associativity,
142 sizeof(associativity));
143 if (ret < 0) {
144 return ret;
145 }
146 }
147 return ret;
148}
149
5af9873d
BH
150
151static size_t create_page_sizes_prop(CPUPPCState *env, uint32_t *prop,
152 size_t maxsize)
153{
154 size_t maxcells = maxsize / sizeof(uint32_t);
155 int i, j, count;
156 uint32_t *p = prop;
157
158 for (i = 0; i < PPC_PAGE_SIZES_MAX_SZ; i++) {
159 struct ppc_one_seg_page_size *sps = &env->sps.sps[i];
160
161 if (!sps->page_shift) {
162 break;
163 }
164 for (count = 0; count < PPC_PAGE_SIZES_MAX_SZ; count++) {
165 if (sps->enc[count].page_shift == 0) {
166 break;
167 }
168 }
169 if ((p - prop) >= (maxcells - 3 - count * 2)) {
170 break;
171 }
172 *(p++) = cpu_to_be32(sps->page_shift);
173 *(p++) = cpu_to_be32(sps->slb_enc);
174 *(p++) = cpu_to_be32(count);
175 for (j = 0; j < count; j++) {
176 *(p++) = cpu_to_be32(sps->enc[j].page_shift);
177 *(p++) = cpu_to_be32(sps->enc[j].pte_enc);
178 }
179 }
180
181 return (p - prop) * sizeof(uint32_t);
182}
183
a3467baa 184static void *spapr_create_fdt_skel(const char *cpu_model,
354ac20a 185 target_phys_addr_t rma_size,
a3467baa
DG
186 target_phys_addr_t initrd_base,
187 target_phys_addr_t initrd_size,
4d8d5467 188 target_phys_addr_t kernel_size,
a3467baa
DG
189 const char *boot_device,
190 const char *kernel_cmdline,
191 long hash_shift)
9fdf0c29
DG
192{
193 void *fdt;
e2684c0b 194 CPUPPCState *env;
6e806cc3 195 uint64_t mem_reg_property[2];
9fdf0c29
DG
196 uint32_t start_prop = cpu_to_be32(initrd_base);
197 uint32_t end_prop = cpu_to_be32(initrd_base + initrd_size);
f43e3525 198 uint32_t pft_size_prop[] = {0, cpu_to_be32(hash_shift)};
ee86dfee 199 char hypertas_prop[] = "hcall-pft\0hcall-term\0hcall-dabr\0hcall-interrupt"
a3d0abae 200 "\0hcall-tce\0hcall-vio\0hcall-splpar\0hcall-bulk";
c73e3771 201 char qemu_hypertas_prop[] = "hcall-memop1";
b5cec4c5 202 uint32_t interrupt_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};
9fdf0c29
DG
203 int i;
204 char *modelname;
e97c3636 205 int smt = kvmppc_smt_threads();
6e806cc3
BR
206 unsigned char vec5[] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x80};
207 uint32_t refpoints[] = {cpu_to_be32(0x4), cpu_to_be32(0x4)};
208 uint32_t associativity[] = {cpu_to_be32(0x4), cpu_to_be32(0x0),
209 cpu_to_be32(0x0), cpu_to_be32(0x0),
210 cpu_to_be32(0x0)};
211 char mem_name[32];
212 target_phys_addr_t node0_size, mem_start;
9fdf0c29
DG
213
214#define _FDT(exp) \
215 do { \
216 int ret = (exp); \
217 if (ret < 0) { \
218 fprintf(stderr, "qemu: error creating device tree: %s: %s\n", \
219 #exp, fdt_strerror(ret)); \
220 exit(1); \
221 } \
222 } while (0)
223
7267c094 224 fdt = g_malloc0(FDT_MAX_SIZE);
9fdf0c29
DG
225 _FDT((fdt_create(fdt, FDT_MAX_SIZE)));
226
4d8d5467
BH
227 if (kernel_size) {
228 _FDT((fdt_add_reservemap_entry(fdt, KERNEL_LOAD_ADDR, kernel_size)));
229 }
230 if (initrd_size) {
231 _FDT((fdt_add_reservemap_entry(fdt, initrd_base, initrd_size)));
232 }
9fdf0c29
DG
233 _FDT((fdt_finish_reservemap(fdt)));
234
235 /* Root node */
236 _FDT((fdt_begin_node(fdt, "")));
237 _FDT((fdt_property_string(fdt, "device_type", "chrp")));
5d73dd66 238 _FDT((fdt_property_string(fdt, "model", "IBM pSeries (emulated by qemu)")));
9fdf0c29
DG
239
240 _FDT((fdt_property_cell(fdt, "#address-cells", 0x2)));
241 _FDT((fdt_property_cell(fdt, "#size-cells", 0x2)));
242
243 /* /chosen */
244 _FDT((fdt_begin_node(fdt, "chosen")));
245
6e806cc3
BR
246 /* Set Form1_affinity */
247 _FDT((fdt_property(fdt, "ibm,architecture-vec-5", vec5, sizeof(vec5))));
248
9fdf0c29
DG
249 _FDT((fdt_property_string(fdt, "bootargs", kernel_cmdline)));
250 _FDT((fdt_property(fdt, "linux,initrd-start",
251 &start_prop, sizeof(start_prop))));
252 _FDT((fdt_property(fdt, "linux,initrd-end",
253 &end_prop, sizeof(end_prop))));
4d8d5467
BH
254 if (kernel_size) {
255 uint64_t kprop[2] = { cpu_to_be64(KERNEL_LOAD_ADDR),
256 cpu_to_be64(kernel_size) };
9fdf0c29 257
4d8d5467
BH
258 _FDT((fdt_property(fdt, "qemu,boot-kernel", &kprop, sizeof(kprop))));
259 }
260 _FDT((fdt_property_string(fdt, "qemu,boot-device", boot_device)));
f28359d8
LZ
261 _FDT((fdt_property_cell(fdt, "qemu,graphic-width", graphic_width)));
262 _FDT((fdt_property_cell(fdt, "qemu,graphic-height", graphic_height)));
263 _FDT((fdt_property_cell(fdt, "qemu,graphic-depth", graphic_depth)));
3384f95c 264
9fdf0c29
DG
265 _FDT((fdt_end_node(fdt)));
266
354ac20a 267 /* memory node(s) */
6e806cc3
BR
268 node0_size = (nb_numa_nodes > 1) ? node_mem[0] : ram_size;
269 if (rma_size > node0_size) {
270 rma_size = node0_size;
271 }
9fdf0c29 272
6e806cc3
BR
273 /* RMA */
274 mem_reg_property[0] = 0;
275 mem_reg_property[1] = cpu_to_be64(rma_size);
276 _FDT((fdt_begin_node(fdt, "memory@0")));
9fdf0c29 277 _FDT((fdt_property_string(fdt, "device_type", "memory")));
6e806cc3
BR
278 _FDT((fdt_property(fdt, "reg", mem_reg_property,
279 sizeof(mem_reg_property))));
280 _FDT((fdt_property(fdt, "ibm,associativity", associativity,
281 sizeof(associativity))));
9fdf0c29
DG
282 _FDT((fdt_end_node(fdt)));
283
6e806cc3
BR
284 /* RAM: Node 0 */
285 if (node0_size > rma_size) {
286 mem_reg_property[0] = cpu_to_be64(rma_size);
287 mem_reg_property[1] = cpu_to_be64(node0_size - rma_size);
354ac20a 288
6e806cc3 289 sprintf(mem_name, "memory@" TARGET_FMT_lx, rma_size);
354ac20a
DG
290 _FDT((fdt_begin_node(fdt, mem_name)));
291 _FDT((fdt_property_string(fdt, "device_type", "memory")));
6e806cc3
BR
292 _FDT((fdt_property(fdt, "reg", mem_reg_property,
293 sizeof(mem_reg_property))));
294 _FDT((fdt_property(fdt, "ibm,associativity", associativity,
295 sizeof(associativity))));
354ac20a
DG
296 _FDT((fdt_end_node(fdt)));
297 }
298
6e806cc3
BR
299 /* RAM: Node 1 and beyond */
300 mem_start = node0_size;
301 for (i = 1; i < nb_numa_nodes; i++) {
302 mem_reg_property[0] = cpu_to_be64(mem_start);
303 mem_reg_property[1] = cpu_to_be64(node_mem[i]);
304 associativity[3] = associativity[4] = cpu_to_be32(i);
305 sprintf(mem_name, "memory@" TARGET_FMT_lx, mem_start);
306 _FDT((fdt_begin_node(fdt, mem_name)));
307 _FDT((fdt_property_string(fdt, "device_type", "memory")));
308 _FDT((fdt_property(fdt, "reg", mem_reg_property,
309 sizeof(mem_reg_property))));
310 _FDT((fdt_property(fdt, "ibm,associativity", associativity,
311 sizeof(associativity))));
312 _FDT((fdt_end_node(fdt)));
313 mem_start += node_mem[i];
314 }
315
9fdf0c29
DG
316 /* cpus */
317 _FDT((fdt_begin_node(fdt, "cpus")));
318
319 _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
320 _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
321
7267c094 322 modelname = g_strdup(cpu_model);
9fdf0c29
DG
323
324 for (i = 0; i < strlen(modelname); i++) {
325 modelname[i] = toupper(modelname[i]);
326 }
327
6e806cc3
BR
328 /* This is needed during FDT finalization */
329 spapr->cpu_model = g_strdup(modelname);
330
c7a5c0c9
DG
331 for (env = first_cpu; env != NULL; env = env->next_cpu) {
332 int index = env->cpu_index;
e97c3636
DG
333 uint32_t servers_prop[smp_threads];
334 uint32_t gservers_prop[smp_threads * 2];
9fdf0c29
DG
335 char *nodename;
336 uint32_t segs[] = {cpu_to_be32(28), cpu_to_be32(40),
337 0xffffffff, 0xffffffff};
0a8b2938
AG
338 uint32_t tbfreq = kvm_enabled() ? kvmppc_get_tbfreq() : TIMEBASE_FREQ;
339 uint32_t cpufreq = kvm_enabled() ? kvmppc_get_clockfreq() : 1000000000;
5af9873d
BH
340 uint32_t page_sizes_prop[64];
341 size_t page_sizes_prop_size;
9fdf0c29 342
e97c3636
DG
343 if ((index % smt) != 0) {
344 continue;
345 }
346
c7a5c0c9 347 if (asprintf(&nodename, "%s@%x", modelname, index) < 0) {
9fdf0c29
DG
348 fprintf(stderr, "Allocation failure\n");
349 exit(1);
350 }
351
352 _FDT((fdt_begin_node(fdt, nodename)));
353
354 free(nodename);
355
c7a5c0c9 356 _FDT((fdt_property_cell(fdt, "reg", index)));
9fdf0c29
DG
357 _FDT((fdt_property_string(fdt, "device_type", "cpu")));
358
359 _FDT((fdt_property_cell(fdt, "cpu-version", env->spr[SPR_PVR])));
360 _FDT((fdt_property_cell(fdt, "dcache-block-size",
361 env->dcache_line_size)));
362 _FDT((fdt_property_cell(fdt, "icache-block-size",
363 env->icache_line_size)));
0a8b2938
AG
364 _FDT((fdt_property_cell(fdt, "timebase-frequency", tbfreq)));
365 _FDT((fdt_property_cell(fdt, "clock-frequency", cpufreq)));
9fdf0c29 366 _FDT((fdt_property_cell(fdt, "ibm,slb-size", env->slb_nr)));
f43e3525
DG
367 _FDT((fdt_property(fdt, "ibm,pft-size",
368 pft_size_prop, sizeof(pft_size_prop))));
9fdf0c29
DG
369 _FDT((fdt_property_string(fdt, "status", "okay")));
370 _FDT((fdt_property(fdt, "64-bit", NULL, 0)));
e97c3636
DG
371
372 /* Build interrupt servers and gservers properties */
373 for (i = 0; i < smp_threads; i++) {
374 servers_prop[i] = cpu_to_be32(index + i);
375 /* Hack, direct the group queues back to cpu 0 */
376 gservers_prop[i*2] = cpu_to_be32(index + i);
377 gservers_prop[i*2 + 1] = 0;
378 }
379 _FDT((fdt_property(fdt, "ibm,ppc-interrupt-server#s",
380 servers_prop, sizeof(servers_prop))));
b5cec4c5 381 _FDT((fdt_property(fdt, "ibm,ppc-interrupt-gserver#s",
e97c3636 382 gservers_prop, sizeof(gservers_prop))));
9fdf0c29 383
c7a5c0c9 384 if (env->mmu_model & POWERPC_MMU_1TSEG) {
9fdf0c29
DG
385 _FDT((fdt_property(fdt, "ibm,processor-segment-sizes",
386 segs, sizeof(segs))));
387 }
388
6659394f
DG
389 /* Advertise VMX/VSX (vector extensions) if available
390 * 0 / no property == no vector extensions
391 * 1 == VMX / Altivec available
392 * 2 == VSX available */
a7342588
DG
393 if (env->insns_flags & PPC_ALTIVEC) {
394 uint32_t vmx = (env->insns_flags2 & PPC2_VSX) ? 2 : 1;
395
6659394f
DG
396 _FDT((fdt_property_cell(fdt, "ibm,vmx", vmx)));
397 }
398
399 /* Advertise DFP (Decimal Floating Point) if available
400 * 0 / no property == no DFP
401 * 1 == DFP available */
a7342588
DG
402 if (env->insns_flags2 & PPC2_DFP) {
403 _FDT((fdt_property_cell(fdt, "ibm,dfp", 1)));
6659394f
DG
404 }
405
5af9873d
BH
406 page_sizes_prop_size = create_page_sizes_prop(env, page_sizes_prop,
407 sizeof(page_sizes_prop));
408 if (page_sizes_prop_size) {
409 _FDT((fdt_property(fdt, "ibm,segment-page-sizes",
410 page_sizes_prop, page_sizes_prop_size)));
411 }
412
9fdf0c29
DG
413 _FDT((fdt_end_node(fdt)));
414 }
415
7267c094 416 g_free(modelname);
9fdf0c29
DG
417
418 _FDT((fdt_end_node(fdt)));
419
f43e3525
DG
420 /* RTAS */
421 _FDT((fdt_begin_node(fdt, "rtas")));
422
423 _FDT((fdt_property(fdt, "ibm,hypertas-functions", hypertas_prop,
424 sizeof(hypertas_prop))));
c73e3771
BH
425 _FDT((fdt_property(fdt, "qemu,hypertas-functions", qemu_hypertas_prop,
426 sizeof(qemu_hypertas_prop))));
f43e3525 427
6e806cc3
BR
428 _FDT((fdt_property(fdt, "ibm,associativity-reference-points",
429 refpoints, sizeof(refpoints))));
430
f43e3525
DG
431 _FDT((fdt_end_node(fdt)));
432
b5cec4c5 433 /* interrupt controller */
9dfef5aa 434 _FDT((fdt_begin_node(fdt, "interrupt-controller")));
b5cec4c5
DG
435
436 _FDT((fdt_property_string(fdt, "device_type",
437 "PowerPC-External-Interrupt-Presentation")));
438 _FDT((fdt_property_string(fdt, "compatible", "IBM,ppc-xicp")));
b5cec4c5
DG
439 _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
440 _FDT((fdt_property(fdt, "ibm,interrupt-server-ranges",
441 interrupt_server_ranges_prop,
442 sizeof(interrupt_server_ranges_prop))));
0c103f8e
DG
443 _FDT((fdt_property_cell(fdt, "#interrupt-cells", 2)));
444 _FDT((fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP)));
445 _FDT((fdt_property_cell(fdt, "phandle", PHANDLE_XICP)));
b5cec4c5
DG
446
447 _FDT((fdt_end_node(fdt)));
448
4040ab72
DG
449 /* vdevice */
450 _FDT((fdt_begin_node(fdt, "vdevice")));
451
452 _FDT((fdt_property_string(fdt, "device_type", "vdevice")));
453 _FDT((fdt_property_string(fdt, "compatible", "IBM,vdevice")));
454 _FDT((fdt_property_cell(fdt, "#address-cells", 0x1)));
455 _FDT((fdt_property_cell(fdt, "#size-cells", 0x0)));
b5cec4c5
DG
456 _FDT((fdt_property_cell(fdt, "#interrupt-cells", 0x2)));
457 _FDT((fdt_property(fdt, "interrupt-controller", NULL, 0)));
4040ab72
DG
458
459 _FDT((fdt_end_node(fdt)));
460
9fdf0c29
DG
461 _FDT((fdt_end_node(fdt))); /* close root node */
462 _FDT((fdt_finish(fdt)));
463
a3467baa
DG
464 return fdt;
465}
466
467static void spapr_finalize_fdt(sPAPREnvironment *spapr,
468 target_phys_addr_t fdt_addr,
469 target_phys_addr_t rtas_addr,
470 target_phys_addr_t rtas_size)
471{
472 int ret;
473 void *fdt;
3384f95c 474 sPAPRPHBState *phb;
a3467baa 475
7267c094 476 fdt = g_malloc(FDT_MAX_SIZE);
a3467baa
DG
477
478 /* open out the base tree into a temp buffer for the final tweaks */
479 _FDT((fdt_open_into(spapr->fdt_skel, fdt, FDT_MAX_SIZE)));
4040ab72
DG
480
481 ret = spapr_populate_vdevice(spapr->vio_bus, fdt);
482 if (ret < 0) {
483 fprintf(stderr, "couldn't setup vio devices in fdt\n");
484 exit(1);
485 }
486
3384f95c 487 QLIST_FOREACH(phb, &spapr->phbs, list) {
e0fdbd7c 488 ret = spapr_populate_pci_dt(phb, PHANDLE_XICP, fdt);
3384f95c
DG
489 }
490
491 if (ret < 0) {
492 fprintf(stderr, "couldn't setup PCI devices in fdt\n");
493 exit(1);
494 }
495
39ac8455
DG
496 /* RTAS */
497 ret = spapr_rtas_device_tree_setup(fdt, rtas_addr, rtas_size);
498 if (ret < 0) {
499 fprintf(stderr, "Couldn't set up RTAS device tree properties\n");
500 }
501
6e806cc3
BR
502 /* Advertise NUMA via ibm,associativity */
503 if (nb_numa_nodes > 1) {
504 ret = spapr_set_associativity(fdt, spapr);
505 if (ret < 0) {
506 fprintf(stderr, "Couldn't set up NUMA device tree properties\n");
507 }
508 }
509
3fc5acde 510 if (!spapr->has_graphics) {
f28359d8
LZ
511 spapr_populate_chosen_stdout(fdt, spapr->vio_bus);
512 }
68f3a94c 513
4040ab72
DG
514 _FDT((fdt_pack(fdt)));
515
4d8d5467
BH
516 if (fdt_totalsize(fdt) > FDT_MAX_SIZE) {
517 hw_error("FDT too big ! 0x%x bytes (max is 0x%x)\n",
518 fdt_totalsize(fdt), FDT_MAX_SIZE);
519 exit(1);
520 }
521
a3467baa 522 cpu_physical_memory_write(fdt_addr, fdt, fdt_totalsize(fdt));
9fdf0c29 523
7267c094 524 g_free(fdt);
9fdf0c29
DG
525}
526
527static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
528{
529 return (addr & 0x0fffffff) + KERNEL_LOAD_ADDR;
530}
531
e2684c0b 532static void emulate_spapr_hypercall(CPUPPCState *env)
9fdf0c29
DG
533{
534 env->gpr[3] = spapr_hypercall(env, env->gpr[3], &env->gpr[4]);
535}
536
a3467baa
DG
537static void spapr_reset(void *opaque)
538{
539 sPAPREnvironment *spapr = (sPAPREnvironment *)opaque;
540
a3467baa
DG
541 /* flush out the hash table */
542 memset(spapr->htab, 0, spapr->htab_size);
543
544 /* Load the fdt */
545 spapr_finalize_fdt(spapr, spapr->fdt_addr, spapr->rtas_addr,
546 spapr->rtas_size);
547
548 /* Set up the entry state */
549 first_cpu->gpr[3] = spapr->fdt_addr;
550 first_cpu->gpr[5] = 0;
551 first_cpu->halted = 0;
552 first_cpu->nip = spapr->entry_point;
553
554}
555
1bba0dc9
AF
556static void spapr_cpu_reset(void *opaque)
557{
5b2038e0 558 PowerPCCPU *cpu = opaque;
1bba0dc9 559
5b2038e0 560 cpu_reset(CPU(cpu));
1bba0dc9
AF
561}
562
8c57b867 563/* Returns whether we want to use VGA or not */
f28359d8
LZ
564static int spapr_vga_init(PCIBus *pci_bus)
565{
8c57b867
AG
566 switch (vga_interface_type) {
567 case VGA_STD:
f28359d8 568 pci_vga_init(pci_bus);
8c57b867
AG
569 return 1;
570 case VGA_NONE:
571 return 0;
572 default:
f28359d8
LZ
573 fprintf(stderr, "This vga model is not supported,"
574 "currently it only supports -vga std\n");
8c57b867
AG
575 exit(0);
576 break;
f28359d8 577 }
f28359d8
LZ
578}
579
9fdf0c29
DG
580/* pSeries LPAR / sPAPR hardware init */
581static void ppc_spapr_init(ram_addr_t ram_size,
582 const char *boot_device,
583 const char *kernel_filename,
584 const char *kernel_cmdline,
585 const char *initrd_filename,
586 const char *cpu_model)
587{
05769733 588 PowerPCCPU *cpu;
e2684c0b 589 CPUPPCState *env;
9fdf0c29 590 int i;
890c2b77
AK
591 MemoryRegion *sysmem = get_system_memory();
592 MemoryRegion *ram = g_new(MemoryRegion, 1);
354ac20a 593 target_phys_addr_t rma_alloc_size, rma_size;
4d8d5467
BH
594 uint32_t initrd_base = 0;
595 long kernel_size = 0, initrd_size = 0;
596 long load_limit, rtas_limit, fw_size;
f43e3525 597 long pteg_shift = 17;
39ac8455 598 char *filename;
9fdf0c29 599
d43b45e2
DG
600 spapr = g_malloc0(sizeof(*spapr));
601 QLIST_INIT(&spapr->phbs);
602
9fdf0c29
DG
603 cpu_ppc_hypercall = emulate_spapr_hypercall;
604
354ac20a
DG
605 /* Allocate RMA if necessary */
606 rma_alloc_size = kvmppc_alloc_rma("ppc_spapr.rma", sysmem);
607
608 if (rma_alloc_size == -1) {
609 hw_error("qemu: Unable to create RMA\n");
610 exit(1);
611 }
612 if (rma_alloc_size && (rma_alloc_size < ram_size)) {
613 rma_size = rma_alloc_size;
614 } else {
615 rma_size = ram_size;
616 }
617
4d8d5467 618 /* We place the device tree and RTAS just below either the top of the RMA,
354ac20a
DG
619 * or just below 2GB, whichever is lowere, so that it can be
620 * processed with 32-bit real mode code if necessary */
4d8d5467
BH
621 rtas_limit = MIN(rma_size, 0x80000000);
622 spapr->rtas_addr = rtas_limit - RTAS_MAX_SIZE;
623 spapr->fdt_addr = spapr->rtas_addr - FDT_MAX_SIZE;
624 load_limit = spapr->fdt_addr - FW_OVERHEAD;
9fdf0c29
DG
625
626 /* init CPUs */
627 if (cpu_model == NULL) {
6b7a2cf6 628 cpu_model = kvm_enabled() ? "host" : "POWER7";
9fdf0c29
DG
629 }
630 for (i = 0; i < smp_cpus; i++) {
05769733
AF
631 cpu = cpu_ppc_init(cpu_model);
632 if (cpu == NULL) {
9fdf0c29
DG
633 fprintf(stderr, "Unable to find PowerPC CPU definition\n");
634 exit(1);
635 }
05769733
AF
636 env = &cpu->env;
637
9fdf0c29
DG
638 /* Set time-base frequency to 512 MHz */
639 cpu_ppc_tb_init(env, TIMEBASE_FREQ);
5b2038e0 640 qemu_register_reset(spapr_cpu_reset, cpu);
9fdf0c29
DG
641
642 env->hreset_vector = 0x60;
643 env->hreset_excp_prefix = 0;
c7a5c0c9 644 env->gpr[3] = env->cpu_index;
9fdf0c29
DG
645 }
646
647 /* allocate RAM */
f73a2575 648 spapr->ram_limit = ram_size;
354ac20a
DG
649 if (spapr->ram_limit > rma_alloc_size) {
650 ram_addr_t nonrma_base = rma_alloc_size;
651 ram_addr_t nonrma_size = spapr->ram_limit - rma_alloc_size;
652
c5705a77
AK
653 memory_region_init_ram(ram, "ppc_spapr.ram", nonrma_size);
654 vmstate_register_ram_global(ram);
354ac20a
DG
655 memory_region_add_subregion(sysmem, nonrma_base, ram);
656 }
9fdf0c29 657
f43e3525
DG
658 /* allocate hash page table. For now we always make this 16mb,
659 * later we should probably make it scale to the size of guest
660 * RAM */
a3467baa 661 spapr->htab_size = 1ULL << (pteg_shift + 7);
f61b4bed 662 spapr->htab = qemu_memalign(spapr->htab_size, spapr->htab_size);
f43e3525 663
c7a5c0c9 664 for (env = first_cpu; env != NULL; env = env->next_cpu) {
a3467baa 665 env->external_htab = spapr->htab;
c7a5c0c9 666 env->htab_base = -1;
a3467baa 667 env->htab_mask = spapr->htab_size - 1;
f61b4bed
AG
668
669 /* Tell KVM that we're in PAPR mode */
670 env->spr[SPR_SDR1] = (unsigned long)spapr->htab |
671 ((pteg_shift + 7) - 18);
672 env->spr[SPR_HIOR] = 0;
673
674 if (kvm_enabled()) {
675 kvmppc_set_papr(env);
676 }
f43e3525
DG
677 }
678
39ac8455 679 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "spapr-rtas.bin");
a3467baa 680 spapr->rtas_size = load_image_targphys(filename, spapr->rtas_addr,
4d8d5467 681 rtas_limit - spapr->rtas_addr);
a3467baa 682 if (spapr->rtas_size < 0) {
39ac8455
DG
683 hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
684 exit(1);
685 }
4d8d5467
BH
686 if (spapr->rtas_size > RTAS_MAX_SIZE) {
687 hw_error("RTAS too big ! 0x%lx bytes (max is 0x%x)\n",
688 spapr->rtas_size, RTAS_MAX_SIZE);
689 exit(1);
690 }
7267c094 691 g_free(filename);
39ac8455 692
4d8d5467 693
b5cec4c5 694 /* Set up Interrupt Controller */
c7a5c0c9 695 spapr->icp = xics_system_init(XICS_IRQS);
e6c866d4 696 spapr->next_irq = 16;
b5cec4c5 697
ad0ebb91
DG
698 /* Set up IOMMU */
699 spapr_iommu_init();
700
b5cec4c5 701 /* Set up VIO bus */
4040ab72
DG
702 spapr->vio_bus = spapr_vio_bus_init();
703
277f9acf 704 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
4040ab72 705 if (serial_hds[i]) {
d601fac4 706 spapr_vty_create(spapr->vio_bus, serial_hds[i]);
4040ab72
DG
707 }
708 }
9fdf0c29 709
3384f95c
DG
710 /* Set up PCI */
711 spapr_create_phb(spapr, "pci", SPAPR_PCI_BUID,
712 SPAPR_PCI_MEM_WIN_ADDR,
713 SPAPR_PCI_MEM_WIN_SIZE,
714 SPAPR_PCI_IO_WIN_ADDR);
715
277f9acf 716 for (i = 0; i < nb_nics; i++) {
8d90ad90
DG
717 NICInfo *nd = &nd_table[i];
718
719 if (!nd->model) {
7267c094 720 nd->model = g_strdup("ibmveth");
8d90ad90
DG
721 }
722
723 if (strcmp(nd->model, "ibmveth") == 0) {
d601fac4 724 spapr_vlan_create(spapr->vio_bus, nd);
8d90ad90 725 } else {
3384f95c 726 pci_nic_init_nofail(&nd_table[i], nd->model, NULL);
8d90ad90
DG
727 }
728 }
729
6e270446 730 for (i = 0; i <= drive_get_max_bus(IF_SCSI); i++) {
d601fac4 731 spapr_vscsi_create(spapr->vio_bus);
6e270446
BH
732 }
733
f28359d8
LZ
734 /* Graphics */
735 if (spapr_vga_init(QLIST_FIRST(&spapr->phbs)->host_state.bus)) {
3fc5acde 736 spapr->has_graphics = true;
f28359d8
LZ
737 }
738
4d8d5467
BH
739 if (rma_size < (MIN_RMA_SLOF << 20)) {
740 fprintf(stderr, "qemu: pSeries SLOF firmware requires >= "
741 "%ldM guest RMA (Real Mode Area memory)\n", MIN_RMA_SLOF);
742 exit(1);
743 }
744
9fdf0c29
DG
745 if (kernel_filename) {
746 uint64_t lowaddr = 0;
747
9fdf0c29
DG
748 kernel_size = load_elf(kernel_filename, translate_kernel_address, NULL,
749 NULL, &lowaddr, NULL, 1, ELF_MACHINE, 0);
750 if (kernel_size < 0) {
a3467baa
DG
751 kernel_size = load_image_targphys(kernel_filename,
752 KERNEL_LOAD_ADDR,
4d8d5467 753 load_limit - KERNEL_LOAD_ADDR);
9fdf0c29
DG
754 }
755 if (kernel_size < 0) {
756 fprintf(stderr, "qemu: could not load kernel '%s'\n",
757 kernel_filename);
758 exit(1);
759 }
760
761 /* load initrd */
762 if (initrd_filename) {
4d8d5467
BH
763 /* Try to locate the initrd in the gap between the kernel
764 * and the firmware. Add a bit of space just in case
765 */
766 initrd_base = (KERNEL_LOAD_ADDR + kernel_size + 0x1ffff) & ~0xffff;
9fdf0c29 767 initrd_size = load_image_targphys(initrd_filename, initrd_base,
4d8d5467 768 load_limit - initrd_base);
9fdf0c29
DG
769 if (initrd_size < 0) {
770 fprintf(stderr, "qemu: could not load initial ram disk '%s'\n",
771 initrd_filename);
772 exit(1);
773 }
774 } else {
775 initrd_base = 0;
776 initrd_size = 0;
777 }
4d8d5467 778 }
a3467baa 779
4d8d5467
BH
780 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, FW_FILE_NAME);
781 fw_size = load_image_targphys(filename, 0, FW_MAX_SIZE);
782 if (fw_size < 0) {
783 hw_error("qemu: could not load LPAR rtas '%s'\n", filename);
784 exit(1);
785 }
786 g_free(filename);
4d8d5467
BH
787
788 spapr->entry_point = 0x100;
789
790 /* SLOF will startup the secondary CPUs using RTAS */
791 for (env = first_cpu; env != NULL; env = env->next_cpu) {
792 env->halted = 1;
9fdf0c29
DG
793 }
794
795 /* Prepare the device tree */
354ac20a 796 spapr->fdt_skel = spapr_create_fdt_skel(cpu_model, rma_size,
a3467baa 797 initrd_base, initrd_size,
4d8d5467 798 kernel_size,
a3467baa
DG
799 boot_device, kernel_cmdline,
800 pteg_shift + 7);
801 assert(spapr->fdt_skel != NULL);
9fdf0c29 802
a3467baa 803 qemu_register_reset(spapr_reset, spapr);
9fdf0c29
DG
804}
805
806static QEMUMachine spapr_machine = {
807 .name = "pseries",
808 .desc = "pSeries Logical Partition (PAPR compliant)",
809 .init = ppc_spapr_init,
810 .max_cpus = MAX_CPUS,
9fdf0c29 811 .no_parallel = 1,
6e270446 812 .use_scsi = 1,
9fdf0c29
DG
813};
814
815static void spapr_machine_init(void)
816{
817 qemu_register_machine(&spapr_machine);
818}
819
820machine_init(spapr_machine_init);