xref: /kvmtool/powerpc/kvm.c (revision d0fb441d92e5eb40ff3e059e110e9524d9c08ff7)
163e158a0SMatt Evans /*
263e158a0SMatt Evans  * PPC64 (SPAPR) platform support
363e158a0SMatt Evans  *
463e158a0SMatt Evans  * Copyright 2011 Matt Evans <matt@ozlabs.org>, IBM Corporation.
563e158a0SMatt Evans  *
6d391177aSMatt Evans  * Portions of FDT setup borrowed from QEMU, copyright 2010 David Gibson, IBM
7d391177aSMatt Evans  * Corporation.
8d391177aSMatt Evans  *
963e158a0SMatt Evans  * This program is free software; you can redistribute it and/or modify it
1063e158a0SMatt Evans  * under the terms of the GNU General Public License version 2 as published
1163e158a0SMatt Evans  * by the Free Software Foundation.
1263e158a0SMatt Evans  */
1363e158a0SMatt Evans 
141299331aSWill Deacon #include "kvm/fdt.h"
1563e158a0SMatt Evans #include "kvm/kvm.h"
1663e158a0SMatt Evans #include "kvm/util.h"
17d391177aSMatt Evans #include "cpu_info.h"
1863e158a0SMatt Evans 
19be76823fSMatt Evans #include "spapr.h"
207295767dSMatt Evans #include "spapr_hvcons.h"
21c481cfd5SMatt Evans #include "spapr_pci.h"
22be76823fSMatt Evans 
2363e158a0SMatt Evans #include <linux/kvm.h>
2463e158a0SMatt Evans 
2563e158a0SMatt Evans #include <sys/types.h>
2663e158a0SMatt Evans #include <sys/ioctl.h>
2763e158a0SMatt Evans #include <sys/mman.h>
2863e158a0SMatt Evans #include <stdbool.h>
2963e158a0SMatt Evans #include <stdlib.h>
3063e158a0SMatt Evans #include <string.h>
3163e158a0SMatt Evans #include <unistd.h>
3263e158a0SMatt Evans #include <stdio.h>
3363e158a0SMatt Evans #include <fcntl.h>
3463e158a0SMatt Evans #include <asm/unistd.h>
3563e158a0SMatt Evans #include <errno.h>
3663e158a0SMatt Evans 
3763e158a0SMatt Evans #include <linux/byteorder.h>
38d391177aSMatt Evans 
39d391177aSMatt Evans #define HPT_ORDER 24
4063e158a0SMatt Evans 
4163e158a0SMatt Evans #define HUGETLBFS_PATH "/var/lib/hugetlbfs/global/pagesize-16MB/"
4263e158a0SMatt Evans 
43f17e5a37SMatt Evans #define PHANDLE_XICP		0x00001111
44f17e5a37SMatt Evans 
4563e158a0SMatt Evans static char kern_cmdline[2048];
4663e158a0SMatt Evans 
4763e158a0SMatt Evans struct kvm_ext kvm_req_ext[] = {
48f17e5a37SMatt Evans 	{ DEFINE_KVM_EXT(KVM_CAP_PPC_UNSET_IRQ) },
49f17e5a37SMatt Evans 	{ DEFINE_KVM_EXT(KVM_CAP_PPC_IRQ_LEVEL) },
5063e158a0SMatt Evans 	{ 0, 0 }
5163e158a0SMatt Evans };
5263e158a0SMatt Evans 
53d391177aSMatt Evans static uint32_t mfpvr(void)
54d391177aSMatt Evans {
55d391177aSMatt Evans 	uint32_t r;
56d391177aSMatt Evans 	asm volatile ("mfpvr %0" : "=r"(r));
57d391177aSMatt Evans 	return r;
58d391177aSMatt Evans }
59d391177aSMatt Evans 
6063e158a0SMatt Evans bool kvm__arch_cpu_supports_vm(void)
6163e158a0SMatt Evans {
6263e158a0SMatt Evans 	return true;
6363e158a0SMatt Evans }
6463e158a0SMatt Evans 
6563e158a0SMatt Evans void kvm__init_ram(struct kvm *kvm)
6663e158a0SMatt Evans {
6763e158a0SMatt Evans 	u64	phys_start, phys_size;
6863e158a0SMatt Evans 	void	*host_mem;
6963e158a0SMatt Evans 
7063e158a0SMatt Evans 	phys_start = 0;
7163e158a0SMatt Evans 	phys_size  = kvm->ram_size;
7263e158a0SMatt Evans 	host_mem   = kvm->ram_start;
7363e158a0SMatt Evans 
7463e158a0SMatt Evans 	/*
7563e158a0SMatt Evans 	 * We put MMIO at PPC_MMIO_START, high up.  Make sure that this doesn't
7663e158a0SMatt Evans 	 * crash into the end of RAM -- on PPC64 at least, this is so high
7763e158a0SMatt Evans 	 * (63TB!) that this is unlikely.
7863e158a0SMatt Evans 	 */
7963e158a0SMatt Evans 	if (phys_size >= PPC_MMIO_START)
8063e158a0SMatt Evans 		die("Too much memory (%lld, what a nice problem): "
8163e158a0SMatt Evans 		    "overlaps MMIO!\n",
8263e158a0SMatt Evans 		    phys_size);
8363e158a0SMatt Evans 
8463e158a0SMatt Evans 	kvm__register_mem(kvm, phys_start, phys_size, host_mem);
8563e158a0SMatt Evans }
8663e158a0SMatt Evans 
8763e158a0SMatt Evans void kvm__arch_set_cmdline(char *cmdline, bool video)
8863e158a0SMatt Evans {
8963e158a0SMatt Evans 	/* We don't need anything unusual in here. */
9063e158a0SMatt Evans }
9163e158a0SMatt Evans 
9263e158a0SMatt Evans /* Architecture-specific KVM init */
937eff9f49SWanlong Gao void kvm__arch_init(struct kvm *kvm, const char *hugetlbfs_path, u64 ram_size)
9463e158a0SMatt Evans {
9563e158a0SMatt Evans 	int cap_ppc_rma;
96df129a0aSMatt Evans 	unsigned long hpt;
9763e158a0SMatt Evans 
9863e158a0SMatt Evans 	kvm->ram_size		= ram_size;
9963e158a0SMatt Evans 
1008cd50b93SMichael Ellerman 	/* Map "default" hugetblfs path to the standard 16M mount point */
1018cd50b93SMichael Ellerman 	if (hugetlbfs_path && !strcmp(hugetlbfs_path, "default"))
10263e158a0SMatt Evans 		hugetlbfs_path = HUGETLBFS_PATH;
1038cd50b93SMichael Ellerman 
1043ebd8e0bSMichael Ellerman 	kvm->ram_start = mmap_anon_or_hugetlbfs(kvm, hugetlbfs_path, kvm->ram_size);
1058cd50b93SMichael Ellerman 
10663e158a0SMatt Evans 	if (kvm->ram_start == MAP_FAILED)
10763e158a0SMatt Evans 		die("Couldn't map %lld bytes for RAM (%d)\n",
10863e158a0SMatt Evans 		    kvm->ram_size, errno);
10963e158a0SMatt Evans 
11063e158a0SMatt Evans 	/* FDT goes at top of memory, RTAS just below */
11142ac24f9SSasha Levin 	kvm->arch.fdt_gra = kvm->ram_size - FDT_MAX_SIZE;
11263e158a0SMatt Evans 	/* FIXME: Not all PPC systems have RTAS */
11342ac24f9SSasha Levin 	kvm->arch.rtas_gra = kvm->arch.fdt_gra - RTAS_MAX_SIZE;
11463e158a0SMatt Evans 	madvise(kvm->ram_start, kvm->ram_size, MADV_MERGEABLE);
11563e158a0SMatt Evans 
116df129a0aSMatt Evans 	/* FIXME:  SPAPR-PR specific; allocate a guest HPT. */
117df129a0aSMatt Evans 	if (posix_memalign((void **)&hpt, (1<<HPT_ORDER), (1<<HPT_ORDER)))
118df129a0aSMatt Evans 		die("Can't allocate %d bytes for HPT\n", (1<<HPT_ORDER));
119df129a0aSMatt Evans 
12042ac24f9SSasha Levin 	kvm->arch.sdr1 = ((hpt + 0x3ffffULL) & ~0x3ffffULL) | (HPT_ORDER-18);
121df129a0aSMatt Evans 
12242ac24f9SSasha Levin 	kvm->arch.pvr = mfpvr();
123d391177aSMatt Evans 
12463e158a0SMatt Evans 	/* FIXME: This is book3s-specific */
12563e158a0SMatt Evans 	cap_ppc_rma = ioctl(kvm->sys_fd, KVM_CHECK_EXTENSION, KVM_CAP_PPC_RMA);
12663e158a0SMatt Evans 	if (cap_ppc_rma == 2)
12763e158a0SMatt Evans 		die("Need contiguous RMA allocation on this hardware, "
12863e158a0SMatt Evans 		    "which is not yet supported.");
129be76823fSMatt Evans 
130be76823fSMatt Evans 	/* Do these before FDT setup, IRQ setup, etc. */
131be76823fSMatt Evans 	/* FIXME: SPAPR-specific */
132be76823fSMatt Evans 	hypercall_init();
133be76823fSMatt Evans 	register_core_rtas();
1347295767dSMatt Evans 	/* Now that hypercalls are initialised, register a couple for the console: */
1357295767dSMatt Evans 	spapr_hvcons_init();
136c481cfd5SMatt Evans 	spapr_create_phb(kvm, "pci", SPAPR_PCI_BUID,
137c481cfd5SMatt Evans 			 SPAPR_PCI_MEM_WIN_ADDR,
138c481cfd5SMatt Evans 			 SPAPR_PCI_MEM_WIN_SIZE,
139c481cfd5SMatt Evans 			 SPAPR_PCI_IO_WIN_ADDR,
140c481cfd5SMatt Evans 			 SPAPR_PCI_IO_WIN_SIZE);
14163e158a0SMatt Evans }
14263e158a0SMatt Evans 
143e56e2de7SLai Jiangshan void kvm__arch_delete_ram(struct kvm *kvm)
144e56e2de7SLai Jiangshan {
145e56e2de7SLai Jiangshan 	munmap(kvm->ram_start, kvm->ram_size);
146e56e2de7SLai Jiangshan }
147e56e2de7SLai Jiangshan 
14863e158a0SMatt Evans void kvm__irq_trigger(struct kvm *kvm, int irq)
14963e158a0SMatt Evans {
15063e158a0SMatt Evans 	kvm__irq_line(kvm, irq, 1);
15163e158a0SMatt Evans 	kvm__irq_line(kvm, irq, 0);
15263e158a0SMatt Evans }
15363e158a0SMatt Evans 
15412c406a8SJonathan Austin void kvm__arch_read_term(struct kvm *kvm)
1557295767dSMatt Evans {
1567295767dSMatt Evans 	/* FIXME: Should register callbacks to platform-specific polls */
1577295767dSMatt Evans 	spapr_hvcons_poll(kvm);
1587295767dSMatt Evans }
1597295767dSMatt Evans 
160004f7684SAndre Przywara bool kvm__arch_load_kernel_image(struct kvm *kvm, int fd_kernel, int fd_initrd,
161004f7684SAndre Przywara 				 const char *kernel_cmdline)
16263e158a0SMatt Evans {
16363e158a0SMatt Evans 	void *p;
16463e158a0SMatt Evans 	void *k_start;
165*d0fb441dSAndre Przywara 	ssize_t filesize;
16663e158a0SMatt Evans 
16763e158a0SMatt Evans 	if (lseek(fd_kernel, 0, SEEK_SET) < 0)
16863e158a0SMatt Evans 		die_perror("lseek");
16963e158a0SMatt Evans 
17063e158a0SMatt Evans 	p = k_start = guest_flat_to_host(kvm, KERNEL_LOAD_ADDR);
17163e158a0SMatt Evans 
172*d0fb441dSAndre Przywara 	filesize = read_file(fd_kernel, p, INITRD_LOAD_ADDR - KERNEL_LOAD_ADDR);
173*d0fb441dSAndre Przywara 	if (filesize < 0) {
174*d0fb441dSAndre Przywara 		if (errno == ENOMEM)
175*d0fb441dSAndre Przywara 			die("Kernel overlaps initrd!");
17663e158a0SMatt Evans 
177*d0fb441dSAndre Przywara 		die_perror("kernel read");
178*d0fb441dSAndre Przywara 	}
179*d0fb441dSAndre Przywara 	pr_info("Loaded kernel to 0x%x (%ld bytes)", KERNEL_LOAD_ADDR,
180*d0fb441dSAndre Przywara 		filesize);
18163e158a0SMatt Evans 	if (fd_initrd != -1) {
18263e158a0SMatt Evans 		if (lseek(fd_initrd, 0, SEEK_SET) < 0)
18363e158a0SMatt Evans 			die_perror("lseek");
18463e158a0SMatt Evans 
18563e158a0SMatt Evans 		if (p-k_start > INITRD_LOAD_ADDR)
18663e158a0SMatt Evans 			die("Kernel overlaps initrd!");
18763e158a0SMatt Evans 
18863e158a0SMatt Evans 		/* Round up kernel size to 8byte alignment, and load initrd right after. */
189*d0fb441dSAndre Przywara 		p = guest_flat_to_host(kvm, INITRD_LOAD_ADDR);
19063e158a0SMatt Evans 
191*d0fb441dSAndre Przywara 		filesize = read_file(fd_initrd, p,
192*d0fb441dSAndre Przywara 			       (kvm->ram_start + kvm->ram_size) - p);
193*d0fb441dSAndre Przywara 		if (filesize < 0) {
194*d0fb441dSAndre Przywara 			if (errno == ENOMEM)
19563e158a0SMatt Evans 				die("initrd too big to contain in guest RAM.\n");
196*d0fb441dSAndre Przywara 			die_perror("initrd read");
197*d0fb441dSAndre Przywara 		}
19863e158a0SMatt Evans 
19963e158a0SMatt Evans 		pr_info("Loaded initrd to 0x%x (%ld bytes)",
200*d0fb441dSAndre Przywara 			INITRD_LOAD_ADDR, filesize);
20142ac24f9SSasha Levin 		kvm->arch.initrd_gra = INITRD_LOAD_ADDR;
202*d0fb441dSAndre Przywara 		kvm->arch.initrd_size = filesize;
20363e158a0SMatt Evans 	} else {
20442ac24f9SSasha Levin 		kvm->arch.initrd_size = 0;
20563e158a0SMatt Evans 	}
20663e158a0SMatt Evans 	strncpy(kern_cmdline, kernel_cmdline, 2048);
20763e158a0SMatt Evans 	kern_cmdline[2047] = '\0';
20863e158a0SMatt Evans 
20963e158a0SMatt Evans 	return true;
21063e158a0SMatt Evans }
21163e158a0SMatt Evans 
212de494ec5SMichael Ellerman struct fdt_prop {
213de494ec5SMichael Ellerman 	void *value;
214de494ec5SMichael Ellerman 	int size;
215de494ec5SMichael Ellerman };
216de494ec5SMichael Ellerman 
217de494ec5SMichael Ellerman static void generate_segment_page_sizes(struct kvm_ppc_smmu_info *info, struct fdt_prop *prop)
218de494ec5SMichael Ellerman {
219de494ec5SMichael Ellerman 	struct kvm_ppc_one_seg_page_size *sps;
220de494ec5SMichael Ellerman 	int i, j, size;
221de494ec5SMichael Ellerman 	u32 *p;
222de494ec5SMichael Ellerman 
223de494ec5SMichael Ellerman 	for (size = 0, i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
224de494ec5SMichael Ellerman 		sps = &info->sps[i];
225de494ec5SMichael Ellerman 
226de494ec5SMichael Ellerman 		if (sps->page_shift == 0)
227de494ec5SMichael Ellerman 			break;
228de494ec5SMichael Ellerman 
229de494ec5SMichael Ellerman 		/* page shift, slb enc & count */
230de494ec5SMichael Ellerman 		size += 3;
231de494ec5SMichael Ellerman 
232de494ec5SMichael Ellerman 		for (j = 0; j < KVM_PPC_PAGE_SIZES_MAX_SZ; j++) {
233de494ec5SMichael Ellerman 			if (info->sps[i].enc[j].page_shift == 0)
234de494ec5SMichael Ellerman 				break;
235de494ec5SMichael Ellerman 
236de494ec5SMichael Ellerman 			/* page shift & pte enc */
237de494ec5SMichael Ellerman 			size += 2;
238de494ec5SMichael Ellerman 		}
239de494ec5SMichael Ellerman 	}
240de494ec5SMichael Ellerman 
241de494ec5SMichael Ellerman 	if (!size) {
242de494ec5SMichael Ellerman 		prop->value = NULL;
243de494ec5SMichael Ellerman 		prop->size = 0;
244de494ec5SMichael Ellerman 		return;
245de494ec5SMichael Ellerman 	}
246de494ec5SMichael Ellerman 
247de494ec5SMichael Ellerman 	/* Convert size to bytes */
248de494ec5SMichael Ellerman 	prop->size = size * sizeof(u32);
249de494ec5SMichael Ellerman 
250de494ec5SMichael Ellerman 	prop->value = malloc(prop->size);
251de494ec5SMichael Ellerman 	if (!prop->value)
252de494ec5SMichael Ellerman 		die_perror("malloc failed");
253de494ec5SMichael Ellerman 
254de494ec5SMichael Ellerman 	p = (u32 *)prop->value;
255de494ec5SMichael Ellerman 	for (i = 0; i < KVM_PPC_PAGE_SIZES_MAX_SZ; i++) {
256de494ec5SMichael Ellerman 		sps = &info->sps[i];
257de494ec5SMichael Ellerman 
258de494ec5SMichael Ellerman 		if (sps->page_shift == 0)
259de494ec5SMichael Ellerman 			break;
260de494ec5SMichael Ellerman 
261de494ec5SMichael Ellerman 		*p++ = sps->page_shift;
262de494ec5SMichael Ellerman 		*p++ = sps->slb_enc;
263de494ec5SMichael Ellerman 
264de494ec5SMichael Ellerman 		for (j = 0; j < KVM_PPC_PAGE_SIZES_MAX_SZ; j++)
265de494ec5SMichael Ellerman 			if (!info->sps[i].enc[j].page_shift)
266de494ec5SMichael Ellerman 				break;
267de494ec5SMichael Ellerman 
268de494ec5SMichael Ellerman 		*p++ = j;	/* count of enc */
269de494ec5SMichael Ellerman 
270de494ec5SMichael Ellerman 		for (j = 0; j < KVM_PPC_PAGE_SIZES_MAX_SZ; j++) {
271de494ec5SMichael Ellerman 			if (!info->sps[i].enc[j].page_shift)
272de494ec5SMichael Ellerman 				break;
273de494ec5SMichael Ellerman 
274de494ec5SMichael Ellerman 			*p++ = info->sps[i].enc[j].page_shift;
275de494ec5SMichael Ellerman 			*p++ = info->sps[i].enc[j].pte_enc;
276de494ec5SMichael Ellerman 		}
277de494ec5SMichael Ellerman 	}
278de494ec5SMichael Ellerman }
279de494ec5SMichael Ellerman 
280d391177aSMatt Evans #define SMT_THREADS 4
281d391177aSMatt Evans 
282d391177aSMatt Evans /*
283d391177aSMatt Evans  * Set up the FDT for the kernel: This function is currently fairly SPAPR-heavy,
284d391177aSMatt Evans  * and whilst most PPC targets will require CPU/memory nodes, others like RTAS
285d391177aSMatt Evans  * should eventually be added separately.
286d391177aSMatt Evans  */
287fc935584SMichael Ellerman static int setup_fdt(struct kvm *kvm)
28863e158a0SMatt Evans {
289d391177aSMatt Evans 	uint64_t 	mem_reg_property[] = { 0, cpu_to_be64(kvm->ram_size) };
290d391177aSMatt Evans 	int 		smp_cpus = kvm->nrcpus;
291f17e5a37SMatt Evans 	uint32_t	int_server_ranges_prop[] = {0, cpu_to_be32(smp_cpus)};
292d391177aSMatt Evans 	char 		hypertas_prop_kvm[] = "hcall-pft\0hcall-term\0"
293d391177aSMatt Evans 		"hcall-dabr\0hcall-interrupt\0hcall-tce\0hcall-vio\0"
294d391177aSMatt Evans 		"hcall-splpar\0hcall-bulk";
295d391177aSMatt Evans 	int 		i, j;
296d391177aSMatt Evans 	char 		cpu_name[30];
297d391177aSMatt Evans 	u8		staging_fdt[FDT_MAX_SIZE];
2984a75c603SMichael Ellerman 	struct cpu_info *cpu_info = find_cpu_info(kvm);
299de494ec5SMichael Ellerman 	struct fdt_prop segment_page_sizes;
30038baf777SMichael Ellerman 	u32 segment_sizes_1T[] = {0x1c, 0x28, 0xffffffff, 0xffffffff};
30163e158a0SMatt Evans 
30242ac24f9SSasha Levin 	/* Generate an appropriate DT at kvm->arch.fdt_gra */
30342ac24f9SSasha Levin 	void *fdt_dest = guest_flat_to_host(kvm, kvm->arch.fdt_gra);
304d391177aSMatt Evans 	void *fdt = staging_fdt;
305d391177aSMatt Evans 
306d391177aSMatt Evans 	_FDT(fdt_create(fdt, FDT_MAX_SIZE));
307d391177aSMatt Evans 	_FDT(fdt_finish_reservemap(fdt));
308d391177aSMatt Evans 
309d391177aSMatt Evans 	_FDT(fdt_begin_node(fdt, ""));
310d391177aSMatt Evans 
311d391177aSMatt Evans 	_FDT(fdt_property_string(fdt, "device_type", "chrp"));
312d391177aSMatt Evans 	_FDT(fdt_property_string(fdt, "model", "IBM pSeries (kvmtool)"));
313d391177aSMatt Evans 	_FDT(fdt_property_cell(fdt, "#address-cells", 0x2));
314d391177aSMatt Evans 	_FDT(fdt_property_cell(fdt, "#size-cells", 0x2));
315d391177aSMatt Evans 
316be76823fSMatt Evans 	/* RTAS */
317be76823fSMatt Evans 	_FDT(fdt_begin_node(fdt, "rtas"));
318be76823fSMatt Evans 	/* This is what the kernel uses to switch 'We're an LPAR'! */
319be76823fSMatt Evans         _FDT(fdt_property(fdt, "ibm,hypertas-functions", hypertas_prop_kvm,
320be76823fSMatt Evans                            sizeof(hypertas_prop_kvm)));
32142ac24f9SSasha Levin 	_FDT(fdt_property_cell(fdt, "linux,rtas-base", kvm->arch.rtas_gra));
32242ac24f9SSasha Levin 	_FDT(fdt_property_cell(fdt, "linux,rtas-entry", kvm->arch.rtas_gra));
32342ac24f9SSasha Levin 	_FDT(fdt_property_cell(fdt, "rtas-size", kvm->arch.rtas_size));
324be76823fSMatt Evans 	/* Now add properties for all RTAS tokens: */
325be76823fSMatt Evans 	if (spapr_rtas_fdt_setup(kvm, fdt))
326be76823fSMatt Evans 		die("Couldn't create RTAS FDT properties\n");
327be76823fSMatt Evans 
328be76823fSMatt Evans 	_FDT(fdt_end_node(fdt));
329be76823fSMatt Evans 
330d391177aSMatt Evans 	/* /chosen */
331d391177aSMatt Evans 	_FDT(fdt_begin_node(fdt, "chosen"));
332d391177aSMatt Evans 	/* cmdline */
333d391177aSMatt Evans 	_FDT(fdt_property_string(fdt, "bootargs", kern_cmdline));
334d391177aSMatt Evans 	/* Initrd */
33542ac24f9SSasha Levin 	if (kvm->arch.initrd_size != 0) {
33642ac24f9SSasha Levin 		uint32_t ird_st_prop = cpu_to_be32(kvm->arch.initrd_gra);
33742ac24f9SSasha Levin 		uint32_t ird_end_prop = cpu_to_be32(kvm->arch.initrd_gra +
33842ac24f9SSasha Levin 						    kvm->arch.initrd_size);
339d391177aSMatt Evans 		_FDT(fdt_property(fdt, "linux,initrd-start",
340d391177aSMatt Evans 				   &ird_st_prop, sizeof(ird_st_prop)));
341d391177aSMatt Evans 		_FDT(fdt_property(fdt, "linux,initrd-end",
342d391177aSMatt Evans 				   &ird_end_prop, sizeof(ird_end_prop)));
343d391177aSMatt Evans 	}
3447295767dSMatt Evans 
3457295767dSMatt Evans 	/*
3467295767dSMatt Evans 	 * stdout-path: This is assuming we're using the HV console.  Also, the
3477295767dSMatt Evans 	 * address is hardwired until we do a VIO bus.
3487295767dSMatt Evans 	 */
3497295767dSMatt Evans 	_FDT(fdt_property_string(fdt, "linux,stdout-path",
3507295767dSMatt Evans 				 "/vdevice/vty@30000000"));
351d391177aSMatt Evans 	_FDT(fdt_end_node(fdt));
352d391177aSMatt Evans 
353d391177aSMatt Evans 	/*
354d391177aSMatt Evans 	 * Memory: We don't alloc. a separate RMA yet.  If we ever need to
355d391177aSMatt Evans 	 * (CAP_PPC_RMA == 2) then have one memory node for 0->RMAsize, and
356d391177aSMatt Evans 	 * another RMAsize->endOfMem.
357d391177aSMatt Evans 	 */
358d391177aSMatt Evans 	_FDT(fdt_begin_node(fdt, "memory@0"));
359d391177aSMatt Evans 	_FDT(fdt_property_string(fdt, "device_type", "memory"));
360d391177aSMatt Evans 	_FDT(fdt_property(fdt, "reg", mem_reg_property,
361d391177aSMatt Evans 			  sizeof(mem_reg_property)));
362d391177aSMatt Evans 	_FDT(fdt_end_node(fdt));
363d391177aSMatt Evans 
364de494ec5SMichael Ellerman 	generate_segment_page_sizes(&cpu_info->mmu_info, &segment_page_sizes);
365de494ec5SMichael Ellerman 
366d391177aSMatt Evans 	/* CPUs */
367d391177aSMatt Evans 	_FDT(fdt_begin_node(fdt, "cpus"));
368d391177aSMatt Evans 	_FDT(fdt_property_cell(fdt, "#address-cells", 0x1));
369d391177aSMatt Evans 	_FDT(fdt_property_cell(fdt, "#size-cells", 0x0));
370d391177aSMatt Evans 
371d391177aSMatt Evans 	for (i = 0; i < smp_cpus; i += SMT_THREADS) {
372d391177aSMatt Evans 		int32_t pft_size_prop[] = { 0, HPT_ORDER };
373d391177aSMatt Evans 		uint32_t servers_prop[SMT_THREADS];
374d391177aSMatt Evans 		uint32_t gservers_prop[SMT_THREADS * 2];
375d391177aSMatt Evans 		int threads = (smp_cpus - i) >= SMT_THREADS ? SMT_THREADS :
376d391177aSMatt Evans 			smp_cpus - i;
377d391177aSMatt Evans 
378d391177aSMatt Evans 		sprintf(cpu_name, "PowerPC,%s@%d", cpu_info->name, i);
379d391177aSMatt Evans 		_FDT(fdt_begin_node(fdt, cpu_name));
380d391177aSMatt Evans 		sprintf(cpu_name, "PowerPC,%s", cpu_info->name);
381d391177aSMatt Evans 		_FDT(fdt_property_string(fdt, "name", cpu_name));
382d391177aSMatt Evans 		_FDT(fdt_property_string(fdt, "device_type", "cpu"));
383d391177aSMatt Evans 
384d391177aSMatt Evans 		_FDT(fdt_property_cell(fdt, "reg", i));
38542ac24f9SSasha Levin 		_FDT(fdt_property_cell(fdt, "cpu-version", kvm->arch.pvr));
386d391177aSMatt Evans 
387d391177aSMatt Evans 		_FDT(fdt_property_cell(fdt, "dcache-block-size", cpu_info->d_bsize));
388d391177aSMatt Evans 		_FDT(fdt_property_cell(fdt, "icache-block-size", cpu_info->i_bsize));
389d391177aSMatt Evans 
390bf4bc902SMichael Ellerman 		if (cpu_info->tb_freq)
391d391177aSMatt Evans 			_FDT(fdt_property_cell(fdt, "timebase-frequency", cpu_info->tb_freq));
392bf4bc902SMichael Ellerman 
393d391177aSMatt Evans 		/* Lies, but safeish lies! */
394d391177aSMatt Evans 		_FDT(fdt_property_cell(fdt, "clock-frequency", 0xddbab200));
395d391177aSMatt Evans 
396cb90966cSMichael Ellerman 		if (cpu_info->mmu_info.slb_size)
397cb90966cSMichael Ellerman 			_FDT(fdt_property_cell(fdt, "ibm,slb-size", cpu_info->mmu_info.slb_size));
398cb90966cSMichael Ellerman 
399d391177aSMatt Evans 		/*
400d391177aSMatt Evans 		 * HPT size is hardwired; KVM currently fixes it at 16MB but the
401d391177aSMatt Evans 		 * moment that changes we'll need to read it out of the kernel.
402d391177aSMatt Evans 		 */
403d391177aSMatt Evans 		_FDT(fdt_property(fdt, "ibm,pft-size", pft_size_prop,
404d391177aSMatt Evans 				  sizeof(pft_size_prop)));
405d391177aSMatt Evans 
406d391177aSMatt Evans 		_FDT(fdt_property_string(fdt, "status", "okay"));
407d391177aSMatt Evans 		_FDT(fdt_property(fdt, "64-bit", NULL, 0));
408d391177aSMatt Evans 		/* A server for each thread in this core */
409d391177aSMatt Evans 		for (j = 0; j < SMT_THREADS; j++) {
410d391177aSMatt Evans 			servers_prop[j] = cpu_to_be32(i+j);
411d391177aSMatt Evans 			/*
412d391177aSMatt Evans 			 * Hack borrowed from QEMU, direct the group queues back
413d391177aSMatt Evans 			 * to cpu 0:
414d391177aSMatt Evans 			 */
415d391177aSMatt Evans 			gservers_prop[j*2] = cpu_to_be32(i+j);
416d391177aSMatt Evans 			gservers_prop[j*2 + 1] = 0;
417d391177aSMatt Evans 		}
418d391177aSMatt Evans 		_FDT(fdt_property(fdt, "ibm,ppc-interrupt-server#s",
419d391177aSMatt Evans 				   servers_prop, threads * sizeof(uint32_t)));
420d391177aSMatt Evans 		_FDT(fdt_property(fdt, "ibm,ppc-interrupt-gserver#s",
421d391177aSMatt Evans 				  gservers_prop,
422d391177aSMatt Evans 				  threads * 2 * sizeof(uint32_t)));
423de494ec5SMichael Ellerman 
424de494ec5SMichael Ellerman 		if (segment_page_sizes.value)
425d391177aSMatt Evans 			_FDT(fdt_property(fdt, "ibm,segment-page-sizes",
426de494ec5SMichael Ellerman 					  segment_page_sizes.value,
427de494ec5SMichael Ellerman 					  segment_page_sizes.size));
428de494ec5SMichael Ellerman 
42938baf777SMichael Ellerman 		if (cpu_info->mmu_info.flags & KVM_PPC_1T_SEGMENTS)
430d391177aSMatt Evans 			_FDT(fdt_property(fdt, "ibm,processor-segment-sizes",
43138baf777SMichael Ellerman 					  segment_sizes_1T, sizeof(segment_sizes_1T)));
43238baf777SMichael Ellerman 
433d391177aSMatt Evans 		/* VSX / DFP options: */
434d391177aSMatt Evans 		if (cpu_info->flags & CPUINFO_FLAG_VMX)
435d391177aSMatt Evans 			_FDT(fdt_property_cell(fdt, "ibm,vmx",
436d391177aSMatt Evans 					       (cpu_info->flags &
437d391177aSMatt Evans 						CPUINFO_FLAG_VSX) ? 2 : 1));
438d391177aSMatt Evans 		if (cpu_info->flags & CPUINFO_FLAG_DFP)
439d391177aSMatt Evans 			_FDT(fdt_property_cell(fdt, "ibm,dfp", 0x1));
440d391177aSMatt Evans 		_FDT(fdt_end_node(fdt));
441d391177aSMatt Evans 	}
442d391177aSMatt Evans 	_FDT(fdt_end_node(fdt));
443d391177aSMatt Evans 
444f17e5a37SMatt Evans 	/* IRQ controller */
445f17e5a37SMatt Evans 	_FDT(fdt_begin_node(fdt, "interrupt-controller@0"));
446f17e5a37SMatt Evans 
447f17e5a37SMatt Evans 	_FDT(fdt_property_string(fdt, "device_type",
448f17e5a37SMatt Evans 				 "PowerPC-External-Interrupt-Presentation"));
449f17e5a37SMatt Evans 	_FDT(fdt_property_string(fdt, "compatible", "IBM,ppc-xicp"));
450f17e5a37SMatt Evans 	_FDT(fdt_property_cell(fdt, "reg", 0));
451f17e5a37SMatt Evans 	_FDT(fdt_property(fdt, "interrupt-controller", NULL, 0));
452f17e5a37SMatt Evans 	_FDT(fdt_property(fdt, "ibm,interrupt-server-ranges",
453f17e5a37SMatt Evans 			   int_server_ranges_prop,
454f17e5a37SMatt Evans 			   sizeof(int_server_ranges_prop)));
455f17e5a37SMatt Evans 	_FDT(fdt_property_cell(fdt, "#interrupt-cells", 2));
456f17e5a37SMatt Evans 	_FDT(fdt_property_cell(fdt, "linux,phandle", PHANDLE_XICP));
457f17e5a37SMatt Evans 	_FDT(fdt_property_cell(fdt, "phandle", PHANDLE_XICP));
458f17e5a37SMatt Evans 	_FDT(fdt_end_node(fdt));
459f17e5a37SMatt Evans 
4607295767dSMatt Evans 	/*
4617295767dSMatt Evans 	 * VIO: See comment in linux,stdout-path; we don't yet represent a VIO
4627295767dSMatt Evans 	 * bus/address allocation so addresses are hardwired here.
4637295767dSMatt Evans 	 */
4647295767dSMatt Evans 	_FDT(fdt_begin_node(fdt, "vdevice"));
4657295767dSMatt Evans 	_FDT(fdt_property_cell(fdt, "#address-cells", 0x1));
4667295767dSMatt Evans 	_FDT(fdt_property_cell(fdt, "#size-cells", 0x0));
4677295767dSMatt Evans 	_FDT(fdt_property_string(fdt, "device_type", "vdevice"));
4687295767dSMatt Evans 	_FDT(fdt_property_string(fdt, "compatible", "IBM,vdevice"));
4697295767dSMatt Evans 	_FDT(fdt_begin_node(fdt, "vty@30000000"));
4707295767dSMatt Evans 	_FDT(fdt_property_string(fdt, "name", "vty"));
4717295767dSMatt Evans 	_FDT(fdt_property_string(fdt, "device_type", "serial"));
4727295767dSMatt Evans 	_FDT(fdt_property_string(fdt, "compatible", "hvterm1"));
4737295767dSMatt Evans 	_FDT(fdt_property_cell(fdt, "reg", 0x30000000));
4747295767dSMatt Evans 	_FDT(fdt_end_node(fdt));
4757295767dSMatt Evans 	_FDT(fdt_end_node(fdt));
4767295767dSMatt Evans 
477d391177aSMatt Evans 	/* Finalise: */
478d391177aSMatt Evans 	_FDT(fdt_end_node(fdt)); /* Root node */
479d391177aSMatt Evans 	_FDT(fdt_finish(fdt));
480d391177aSMatt Evans 
481d391177aSMatt Evans 	_FDT(fdt_open_into(fdt, fdt_dest, FDT_MAX_SIZE));
482c481cfd5SMatt Evans 
483c481cfd5SMatt Evans 	/* PCI */
484c481cfd5SMatt Evans 	if (spapr_populate_pci_devices(kvm, PHANDLE_XICP, fdt_dest))
485c481cfd5SMatt Evans 		die("Fail populating PCI device nodes");
486c481cfd5SMatt Evans 
48742ac24f9SSasha Levin 	_FDT(fdt_add_mem_rsv(fdt_dest, kvm->arch.rtas_gra, kvm->arch.rtas_size));
488d391177aSMatt Evans 	_FDT(fdt_pack(fdt_dest));
489de494ec5SMichael Ellerman 
490de494ec5SMichael Ellerman 	free(segment_page_sizes.value);
491fc935584SMichael Ellerman 
492fc935584SMichael Ellerman 	return 0;
49363e158a0SMatt Evans }
494fc935584SMichael Ellerman firmware_init(setup_fdt);
49563e158a0SMatt Evans 
49663e158a0SMatt Evans /**
49763e158a0SMatt Evans  * kvm__arch_setup_firmware
49863e158a0SMatt Evans  */
499f7f9d02bSCyrill Gorcunov int kvm__arch_setup_firmware(struct kvm *kvm)
50063e158a0SMatt Evans {
501be76823fSMatt Evans 	/*
502be76823fSMatt Evans 	 * Set up RTAS stub.  All it is is a single hypercall:
503be76823fSMatt Evans 	 *  0:   7c 64 1b 78     mr      r4,r3
504be76823fSMatt Evans 	 *  4:   3c 60 00 00     lis     r3,0
505be76823fSMatt Evans 	 *  8:   60 63 f0 00     ori     r3,r3,61440
506be76823fSMatt Evans 	 *  c:   44 00 00 22     sc      1
507be76823fSMatt Evans 	 * 10:   4e 80 00 20     blr
508be76823fSMatt Evans 	 */
50942ac24f9SSasha Levin 	uint32_t *rtas = guest_flat_to_host(kvm, kvm->arch.rtas_gra);
510be76823fSMatt Evans 
511be76823fSMatt Evans 	rtas[0] = 0x7c641b78;
512be76823fSMatt Evans 	rtas[1] = 0x3c600000;
513be76823fSMatt Evans 	rtas[2] = 0x6063f000;
514be76823fSMatt Evans 	rtas[3] = 0x44000022;
515be76823fSMatt Evans 	rtas[4] = 0x4e800020;
51642ac24f9SSasha Levin 	kvm->arch.rtas_size = 20;
517be76823fSMatt Evans 
518be76823fSMatt Evans 	pr_info("Set up %ld bytes of RTAS at 0x%lx\n",
51942ac24f9SSasha Levin 		kvm->arch.rtas_size, kvm->arch.rtas_gra);
52063e158a0SMatt Evans 
52163e158a0SMatt Evans 	/* Load SLOF */
52263e158a0SMatt Evans 
523f7f9d02bSCyrill Gorcunov 	return 0;
52463e158a0SMatt Evans }
5251add9f73SSasha Levin 
5261add9f73SSasha Levin int kvm__arch_free_firmware(struct kvm *kvm)
5271add9f73SSasha Levin {
5281add9f73SSasha Levin 	return 0;
5291add9f73SSasha Levin }
530