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