blob: ec0a9e5de1005331e7372a5c76d3a153d2a4dfd5 [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
Paul Mackerras32fad282012-05-04 02:32:53 +000028int kvm_hpt_order = KVM_DEFAULT_HPT_ORDER;
29EXPORT_SYMBOL_GPL(kvm_hpt_order);
30
Alexander Grafb4e70612012-01-16 16:50:10 +010031/*************** RMA *************/
32
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000033/*
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 */
41static unsigned long kvm_rma_size = 64 << 20; /* 64MB */
42static unsigned long kvm_rma_count;
43
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000044/* Work out RMLS (real mode limit selector) field value for a given RMA size.
Paul Mackerras9e368f22011-06-29 00:40:08 +000045 Assumes POWER7 or PPC970. */
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000046static inline int lpcr_rmls(unsigned long rma_size)
47{
48 switch (rma_size) {
49 case 32ul << 20: /* 32 MB */
Paul Mackerras9e368f22011-06-29 00:40:08 +000050 if (cpu_has_feature(CPU_FTR_ARCH_206))
51 return 8; /* only supported on POWER7 */
52 return -1;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000053 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
Alexander Grafb4e70612012-01-16 16:50:10 +010070static 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}
79early_param("kvm_rma_size", early_parse_rma_size);
80
81static 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}
90early_param("kvm_rma_count", early_parse_rma_count);
91
92struct kvmppc_linear_info *kvm_alloc_rma(void)
93{
94 return kvm_alloc_linear(KVM_LINEAR_RMA);
95}
96EXPORT_SYMBOL_GPL(kvm_alloc_rma);
97
98void kvm_release_rma(struct kvmppc_linear_info *ri)
99{
100 kvm_release_linear(ri);
101}
102EXPORT_SYMBOL_GPL(kvm_release_rma);
103
Alexander Grafd2a1b482012-01-16 19:12:11 +0100104/*************** 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
112static unsigned long kvm_hpt_count;
113
114static 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}
123early_param("kvm_hpt_count", early_parse_hpt_count);
124
125struct kvmppc_linear_info *kvm_alloc_hpt(void)
126{
127 return kvm_alloc_linear(KVM_LINEAR_HPT);
128}
129EXPORT_SYMBOL_GPL(kvm_alloc_hpt);
130
131void kvm_release_hpt(struct kvmppc_linear_info *li)
132{
133 kvm_release_linear(li);
134}
135EXPORT_SYMBOL_GPL(kvm_release_hpt);
136
Alexander Grafb4e70612012-01-16 16:50:10 +0100137/*************** generic *************/
138
139static LIST_HEAD(free_linears);
140static DEFINE_SPINLOCK(linear_lock);
141
142static void __init kvm_linear_init_one(ulong size, int count, int type)
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000143{
144 unsigned long i;
145 unsigned long j, npages;
Alexander Grafb4e70612012-01-16 16:50:10 +0100146 void *linear;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000147 struct page *pg;
Alexander Grafb4e70612012-01-16 16:50:10 +0100148 const char *typestr;
149 struct kvmppc_linear_info *linear_info;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000150
Alexander Grafb4e70612012-01-16 16:50:10 +0100151 if (!count)
152 return;
153
Alexander Grafd2a1b482012-01-16 19:12:11 +0100154 typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "HPT";
Alexander Grafb4e70612012-01-16 16:50:10 +0100155
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);
Paul Mackerras1340f3e82012-08-06 00:04:14 +0000160 pr_debug("Allocated KVM %s at %p (%ld MB)\n", typestr, linear,
161 size >> 20);
Alexander Grafb4e70612012-01-16 16:50:10 +0100162 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);
170 for (j = 0; j < npages; ++j) {
171 atomic_inc(&pg->_count);
172 ++pg;
173 }
174 }
175}
176
177static struct kvmppc_linear_info *kvm_alloc_linear(int type)
178{
Paul Mackerrasb4e51222012-02-03 00:45:02 +0000179 struct kvmppc_linear_info *ri, *ret;
Alexander Grafb4e70612012-01-16 16:50:10 +0100180
Paul Mackerrasb4e51222012-02-03 00:45:02 +0000181 ret = NULL;
Alexander Grafb4e70612012-01-16 16:50:10 +0100182 spin_lock(&linear_lock);
183 list_for_each_entry(ri, &free_linears, list) {
184 if (ri->type != type)
185 continue;
186
187 list_del(&ri->list);
188 atomic_inc(&ri->use_count);
Paul Mackerrasb4e51222012-02-03 00:45:02 +0000189 memset(ri->base_virt, 0, ri->npages << PAGE_SHIFT);
190 ret = ri;
Alexander Grafb4e70612012-01-16 16:50:10 +0100191 break;
192 }
193 spin_unlock(&linear_lock);
Paul Mackerrasb4e51222012-02-03 00:45:02 +0000194 return ret;
Alexander Grafb4e70612012-01-16 16:50:10 +0100195}
196
197static void kvm_release_linear(struct kvmppc_linear_info *ri)
198{
199 if (atomic_dec_and_test(&ri->use_count)) {
200 spin_lock(&linear_lock);
201 list_add_tail(&ri->list, &free_linears);
202 spin_unlock(&linear_lock);
203
204 }
205}
206
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 */
212void __init kvm_linear_init(void)
213{
Alexander Grafd2a1b482012-01-16 19:12:11 +0100214 /* HPT */
Paul Mackerras32fad282012-05-04 02:32:53 +0000215 kvm_linear_init_one(1 << kvm_hpt_order, kvm_hpt_count, KVM_LINEAR_HPT);
Alexander Grafd2a1b482012-01-16 19:12:11 +0100216
Alexander Grafb4e70612012-01-16 16:50:10 +0100217 /* RMA */
Paul Mackerras9e368f22011-06-29 00:40:08 +0000218 /* Only do this on PPC970 in HV mode */
219 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
220 !cpu_has_feature(CPU_FTR_ARCH_201))
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000221 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
Alexander Grafb4e70612012-01-16 16:50:10 +0100232 kvm_linear_init_one(kvm_rma_size, kvm_rma_count, KVM_LINEAR_RMA);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +0000233}