blob: e1b60f56f2a1eb73bdd00ebc36faa44e5e3a0aa2 [file] [log] [blame]
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00001/*
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>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040011#include <linux/export.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000012#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
Alexander Grafb4e70612012-01-16 16:50:10 +010021#define KVM_LINEAR_RMA 0
Alexander Grafd2a1b482012-01-16 19:12:11 +010022#define KVM_LINEAR_HPT 1
Alexander Grafb4e70612012-01-16 16:50:10 +010023
24static void __init kvm_linear_init_one(ulong size, int count, int type);
25static struct kvmppc_linear_info *kvm_alloc_linear(int type);
26static void kvm_release_linear(struct kvmppc_linear_info *ri);
27
28/*************** RMA *************/
29
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000030/*
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 */
38static unsigned long kvm_rma_size = 64 << 20; /* 64MB */
39static unsigned long kvm_rma_count;
40
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000041/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +000042 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000043static inline int lpcr_rmls(unsigned long rma_size)
44{
45 switch (rma_size) {
46 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +000047 if (cpu_has_feature(CPU_FTR_ARCH_206))
48 return 8; /* only supported on POWER7 */
49 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000050 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
Alexander Grafb4e70612012-01-16 16:50:10 +010067static 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}
76early_param("kvm_rma_size", early_parse_rma_size);
77
78static 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}
87early_param("kvm_rma_count", early_parse_rma_count);
88
89struct kvmppc_linear_info *kvm_alloc_rma(void)
90{
91 return kvm_alloc_linear(KVM_LINEAR_RMA);
92}
93EXPORT_SYMBOL_GPL(kvm_alloc_rma);
94
95void kvm_release_rma(struct kvmppc_linear_info *ri)
96{
97 kvm_release_linear(ri);
98}
99EXPORT_SYMBOL_GPL(kvm_release_rma);
100
Alexander Grafd2a1b482012-01-16 19:12:11 +0100101/*************** 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
109static unsigned long kvm_hpt_count;
110
111static 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}
120early_param("kvm_hpt_count", early_parse_hpt_count);
121
122struct kvmppc_linear_info *kvm_alloc_hpt(void)
123{
124 return kvm_alloc_linear(KVM_LINEAR_HPT);
125}
126EXPORT_SYMBOL_GPL(kvm_alloc_hpt);
127
128void kvm_release_hpt(struct kvmppc_linear_info *li)
129{
130 kvm_release_linear(li);
131}
132EXPORT_SYMBOL_GPL(kvm_release_hpt);
133
Alexander Grafb4e70612012-01-16 16:50:10 +0100134/*************** generic *************/
135
136static LIST_HEAD(free_linears);
137static DEFINE_SPINLOCK(linear_lock);
138
139static void __init kvm_linear_init_one(ulong size, int count, int type)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000140{
141 unsigned long i;
142 unsigned long j, npages;
Alexander Grafb4e70612012-01-16 16:50:10 +0100143 void *linear;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000144 struct page *pg;
Alexander Grafb4e70612012-01-16 16:50:10 +0100145 const char *typestr;
146 struct kvmppc_linear_info *linear_info;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000147
Alexander Grafb4e70612012-01-16 16:50:10 +0100148 if (!count)
149 return;
150
Alexander Grafd2a1b482012-01-16 19:12:11 +0100151 typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "HPT";
Alexander Grafb4e70612012-01-16 16:50:10 +0100152
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
174static struct kvmppc_linear_info *kvm_alloc_linear(int type)
175{
Paul Mackerrasb4e51222012-02-03 00:45:02 +0000176 struct kvmppc_linear_info *ri, *ret;
Alexander Grafb4e70612012-01-16 16:50:10 +0100177
Paul Mackerrasb4e51222012-02-03 00:45:02 +0000178 ret = NULL;
Alexander Grafb4e70612012-01-16 16:50:10 +0100179 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);
Paul Mackerrasb4e51222012-02-03 00:45:02 +0000186 memset(ri->base_virt, 0, ri->npages << PAGE_SHIFT);
187 ret = ri;
Alexander Grafb4e70612012-01-16 16:50:10 +0100188 break;
189 }
190 spin_unlock(&linear_lock);
Paul Mackerrasb4e51222012-02-03 00:45:02 +0000191 return ret;
Alexander Grafb4e70612012-01-16 16:50:10 +0100192}
193
194static 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 */
209void __init kvm_linear_init(void)
210{
Alexander Grafd2a1b482012-01-16 19:12:11 +0100211 /* HPT */
212 kvm_linear_init_one(1 << HPT_ORDER, kvm_hpt_count, KVM_LINEAR_HPT);
213
Alexander Grafb4e70612012-01-16 16:50:10 +0100214 /* RMA */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000215 /* Only do this on PPC970 in HV mode */
216 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
217 !cpu_has_feature(CPU_FTR_ARCH_201))
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000218 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
Alexander Grafb4e70612012-01-16 16:50:10 +0100229 kvm_linear_init_one(kvm_rma_size, kvm_rma_count, KVM_LINEAR_RMA);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000230}