1 /*
2  * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License, version 2, as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/kvm_host.h>
10 #include <linux/preempt.h>
11 #include <linux/export.h>
12 #include <linux/sched.h>
13 #include <linux/spinlock.h>
14 #include <linux/bootmem.h>
15 #include <linux/init.h>
16 
17 #include <asm/cputable.h>
18 #include <asm/kvm_ppc.h>
19 #include <asm/kvm_book3s.h>
20 
21 #define KVM_LINEAR_RMA		0
22 #define KVM_LINEAR_HPT		1
23 
24 static void __init kvm_linear_init_one(ulong size, int count, int type);
25 static struct kvmppc_linear_info *kvm_alloc_linear(int type);
26 static void kvm_release_linear(struct kvmppc_linear_info *ri);
27 
28 /*************** RMA *************/
29 
30 /*
31  * This maintains a list of RMAs (real mode areas) for KVM guests to use.
32  * Each RMA has to be physically contiguous and of a size that the
33  * hardware supports.  PPC970 and POWER7 support 64MB, 128MB and 256MB,
34  * and other larger sizes.  Since we are unlikely to be allocate that
35  * much physically contiguous memory after the system is up and running,
36  * we preallocate a set of RMAs in early boot for KVM to use.
37  */
38 static unsigned long kvm_rma_size = 64 << 20;	/* 64MB */
39 static unsigned long kvm_rma_count;
40 
41 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
42    Assumes POWER7 or PPC970. */
lpcr_rmls(unsigned long rma_size)43 static inline int lpcr_rmls(unsigned long rma_size)
44 {
45 	switch (rma_size) {
46 	case 32ul << 20:	/* 32 MB */
47 		if (cpu_has_feature(CPU_FTR_ARCH_206))
48 			return 8;	/* only supported on POWER7 */
49 		return -1;
50 	case 64ul << 20:	/* 64 MB */
51 		return 3;
52 	case 128ul << 20:	/* 128 MB */
53 		return 7;
54 	case 256ul << 20:	/* 256 MB */
55 		return 4;
56 	case 1ul << 30:		/* 1 GB */
57 		return 2;
58 	case 16ul << 30:	/* 16 GB */
59 		return 1;
60 	case 256ul << 30:	/* 256 GB */
61 		return 0;
62 	default:
63 		return -1;
64 	}
65 }
66 
early_parse_rma_size(char * p)67 static int __init early_parse_rma_size(char *p)
68 {
69 	if (!p)
70 		return 1;
71 
72 	kvm_rma_size = memparse(p, &p);
73 
74 	return 0;
75 }
76 early_param("kvm_rma_size", early_parse_rma_size);
77 
early_parse_rma_count(char * p)78 static int __init early_parse_rma_count(char *p)
79 {
80 	if (!p)
81 		return 1;
82 
83 	kvm_rma_count = simple_strtoul(p, NULL, 0);
84 
85 	return 0;
86 }
87 early_param("kvm_rma_count", early_parse_rma_count);
88 
kvm_alloc_rma(void)89 struct kvmppc_linear_info *kvm_alloc_rma(void)
90 {
91 	return kvm_alloc_linear(KVM_LINEAR_RMA);
92 }
93 EXPORT_SYMBOL_GPL(kvm_alloc_rma);
94 
kvm_release_rma(struct kvmppc_linear_info * ri)95 void kvm_release_rma(struct kvmppc_linear_info *ri)
96 {
97 	kvm_release_linear(ri);
98 }
99 EXPORT_SYMBOL_GPL(kvm_release_rma);
100 
101 /*************** HPT *************/
102 
103 /*
104  * This maintains a list of big linear HPT tables that contain the GVA->HPA
105  * memory mappings. If we don't reserve those early on, we might not be able
106  * to get a big (usually 16MB) linear memory region from the kernel anymore.
107  */
108 
109 static unsigned long kvm_hpt_count;
110 
early_parse_hpt_count(char * p)111 static int __init early_parse_hpt_count(char *p)
112 {
113 	if (!p)
114 		return 1;
115 
116 	kvm_hpt_count = simple_strtoul(p, NULL, 0);
117 
118 	return 0;
119 }
120 early_param("kvm_hpt_count", early_parse_hpt_count);
121 
kvm_alloc_hpt(void)122 struct kvmppc_linear_info *kvm_alloc_hpt(void)
123 {
124 	return kvm_alloc_linear(KVM_LINEAR_HPT);
125 }
126 EXPORT_SYMBOL_GPL(kvm_alloc_hpt);
127 
kvm_release_hpt(struct kvmppc_linear_info * li)128 void kvm_release_hpt(struct kvmppc_linear_info *li)
129 {
130 	kvm_release_linear(li);
131 }
132 EXPORT_SYMBOL_GPL(kvm_release_hpt);
133 
134 /*************** generic *************/
135 
136 static LIST_HEAD(free_linears);
137 static DEFINE_SPINLOCK(linear_lock);
138 
kvm_linear_init_one(ulong size,int count,int type)139 static void __init kvm_linear_init_one(ulong size, int count, int type)
140 {
141 	unsigned long i;
142 	unsigned long j, npages;
143 	void *linear;
144 	struct page *pg;
145 	const char *typestr;
146 	struct kvmppc_linear_info *linear_info;
147 
148 	if (!count)
149 		return;
150 
151 	typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "HPT";
152 
153 	npages = size >> PAGE_SHIFT;
154 	linear_info = alloc_bootmem(count * sizeof(struct kvmppc_linear_info));
155 	for (i = 0; i < count; ++i) {
156 		linear = alloc_bootmem_align(size, size);
157 		pr_info("Allocated KVM %s at %p (%ld MB)\n", typestr, linear,
158 			size >> 20);
159 		linear_info[i].base_virt = linear;
160 		linear_info[i].base_pfn = __pa(linear) >> PAGE_SHIFT;
161 		linear_info[i].npages = npages;
162 		linear_info[i].type = type;
163 		list_add_tail(&linear_info[i].list, &free_linears);
164 		atomic_set(&linear_info[i].use_count, 0);
165 
166 		pg = pfn_to_page(linear_info[i].base_pfn);
167 		for (j = 0; j < npages; ++j) {
168 			atomic_inc(&pg->_count);
169 			++pg;
170 		}
171 	}
172 }
173 
kvm_alloc_linear(int type)174 static struct kvmppc_linear_info *kvm_alloc_linear(int type)
175 {
176 	struct kvmppc_linear_info *ri, *ret;
177 
178 	ret = NULL;
179 	spin_lock(&linear_lock);
180 	list_for_each_entry(ri, &free_linears, list) {
181 		if (ri->type != type)
182 			continue;
183 
184 		list_del(&ri->list);
185 		atomic_inc(&ri->use_count);
186 		memset(ri->base_virt, 0, ri->npages << PAGE_SHIFT);
187 		ret = ri;
188 		break;
189 	}
190 	spin_unlock(&linear_lock);
191 	return ret;
192 }
193 
kvm_release_linear(struct kvmppc_linear_info * ri)194 static void kvm_release_linear(struct kvmppc_linear_info *ri)
195 {
196 	if (atomic_dec_and_test(&ri->use_count)) {
197 		spin_lock(&linear_lock);
198 		list_add_tail(&ri->list, &free_linears);
199 		spin_unlock(&linear_lock);
200 
201 	}
202 }
203 
204 /*
205  * Called at boot time while the bootmem allocator is active,
206  * to allocate contiguous physical memory for the hash page
207  * tables for guests.
208  */
kvm_linear_init(void)209 void __init kvm_linear_init(void)
210 {
211 	/* HPT */
212 	kvm_linear_init_one(1 << HPT_ORDER, kvm_hpt_count, KVM_LINEAR_HPT);
213 
214 	/* RMA */
215 	/* Only do this on PPC970 in HV mode */
216 	if (!cpu_has_feature(CPU_FTR_HVMODE) ||
217 	    !cpu_has_feature(CPU_FTR_ARCH_201))
218 		return;
219 
220 	if (!kvm_rma_size || !kvm_rma_count)
221 		return;
222 
223 	/* Check that the requested size is one supported in hardware */
224 	if (lpcr_rmls(kvm_rma_size) < 0) {
225 		pr_err("RMA size of 0x%lx not supported\n", kvm_rma_size);
226 		return;
227 	}
228 
229 	kvm_linear_init_one(kvm_rma_size, kvm_rma_count, KVM_LINEAR_RMA);
230 }
231