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