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