2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
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.
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>
17 #include <asm/cputable.h>
18 #include <asm/kvm_ppc.h>
19 #include <asm/kvm_book3s.h>
21 #define KVM_LINEAR_RMA 0
22 #define KVM_LINEAR_HPT 1
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);
28 /*************** RMA *************/
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.
38 static unsigned long kvm_rma_size = 64 << 20; /* 64MB */
39 static unsigned long kvm_rma_count;
41 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
42 Assumes POWER7 or PPC970. */
43 static inline int lpcr_rmls(unsigned long rma_size)
46 case 32ul << 20: /* 32 MB */
47 if (cpu_has_feature(CPU_FTR_ARCH_206))
48 return 8; /* only supported on POWER7 */
50 case 64ul << 20: /* 64 MB */
52 case 128ul << 20: /* 128 MB */
54 case 256ul << 20: /* 256 MB */
56 case 1ul << 30: /* 1 GB */
58 case 16ul << 30: /* 16 GB */
60 case 256ul << 30: /* 256 GB */
67 static int __init early_parse_rma_size(char *p)
72 kvm_rma_size = memparse(p, &p);
76 early_param("kvm_rma_size", early_parse_rma_size);
78 static int __init early_parse_rma_count(char *p)
83 kvm_rma_count = simple_strtoul(p, NULL, 0);
87 early_param("kvm_rma_count", early_parse_rma_count);
89 struct kvmppc_linear_info *kvm_alloc_rma(void)
91 return kvm_alloc_linear(KVM_LINEAR_RMA);
93 EXPORT_SYMBOL_GPL(kvm_alloc_rma);
95 void kvm_release_rma(struct kvmppc_linear_info *ri)
97 kvm_release_linear(ri);
99 EXPORT_SYMBOL_GPL(kvm_release_rma);
101 /*************** HPT *************/
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.
109 static unsigned long kvm_hpt_count;
111 static int __init early_parse_hpt_count(char *p)
116 kvm_hpt_count = simple_strtoul(p, NULL, 0);
120 early_param("kvm_hpt_count", early_parse_hpt_count);
122 struct kvmppc_linear_info *kvm_alloc_hpt(void)
124 return kvm_alloc_linear(KVM_LINEAR_HPT);
126 EXPORT_SYMBOL_GPL(kvm_alloc_hpt);
128 void kvm_release_hpt(struct kvmppc_linear_info *li)
130 kvm_release_linear(li);
132 EXPORT_SYMBOL_GPL(kvm_release_hpt);
134 /*************** generic *************/
136 static LIST_HEAD(free_linears);
137 static DEFINE_SPINLOCK(linear_lock);
139 static void __init kvm_linear_init_one(ulong size, int count, int type)
142 unsigned long j, npages;
146 struct kvmppc_linear_info *linear_info;
151 typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "HPT";
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,
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);
166 pg = pfn_to_page(linear_info[i].base_pfn);
167 for (j = 0; j < npages; ++j) {
168 atomic_inc(&pg->_count);
174 static struct kvmppc_linear_info *kvm_alloc_linear(int type)
176 struct kvmppc_linear_info *ri, *ret;
179 spin_lock(&linear_lock);
180 list_for_each_entry(ri, &free_linears, list) {
181 if (ri->type != type)
185 atomic_inc(&ri->use_count);
186 memset(ri->base_virt, 0, ri->npages << PAGE_SHIFT);
190 spin_unlock(&linear_lock);
194 static void kvm_release_linear(struct kvmppc_linear_info *ri)
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);
205 * Called at boot time while the bootmem allocator is active,
206 * to allocate contiguous physical memory for the hash page
209 void __init kvm_linear_init(void)
212 kvm_linear_init_one(1 << HPT_ORDER, kvm_hpt_count, KVM_LINEAR_HPT);
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))
220 if (!kvm_rma_size || !kvm_rma_count)
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);
229 kvm_linear_init_one(kvm_rma_size, kvm_rma_count, KVM_LINEAR_RMA);