Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 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> |
Paul Gortmaker | 66b15db | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 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 | |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 21 | #define KVM_LINEAR_RMA 0 |
Alexander Graf | d2a1b48 | 2012-01-16 19:12:11 +0100 | [diff] [blame] | 22 | #define KVM_LINEAR_HPT 1 |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 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 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 28 | int kvm_hpt_order = KVM_DEFAULT_HPT_ORDER; |
| 29 | EXPORT_SYMBOL_GPL(kvm_hpt_order); |
| 30 | |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 31 | /*************** RMA *************/ |
| 32 | |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 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 | |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 44 | /* Work out RMLS (real mode limit selector) field value for a given RMA size. |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 45 | Assumes POWER7 or PPC970. */ |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 46 | static inline int lpcr_rmls(unsigned long rma_size) |
| 47 | { |
| 48 | switch (rma_size) { |
| 49 | case 32ul << 20: /* 32 MB */ |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 50 | if (cpu_has_feature(CPU_FTR_ARCH_206)) |
| 51 | return 8; /* only supported on POWER7 */ |
| 52 | return -1; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 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 | |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 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 | |
Alexander Graf | d2a1b48 | 2012-01-16 19:12:11 +0100 | [diff] [blame] | 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 | |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 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) |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 143 | { |
| 144 | unsigned long i; |
| 145 | unsigned long j, npages; |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 146 | void *linear; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 147 | struct page *pg; |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 148 | const char *typestr; |
| 149 | struct kvmppc_linear_info *linear_info; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 150 | |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 151 | if (!count) |
| 152 | return; |
| 153 | |
Alexander Graf | d2a1b48 | 2012-01-16 19:12:11 +0100 | [diff] [blame] | 154 | typestr = (type == KVM_LINEAR_RMA) ? "RMA" : "HPT"; |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 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); |
Paul Mackerras | 1340f3e8 | 2012-08-06 00:04:14 +0000 | [diff] [blame] | 160 | pr_debug("Allocated KVM %s at %p (%ld MB)\n", typestr, linear, |
| 161 | size >> 20); |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 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); |
| 170 | for (j = 0; j < npages; ++j) { |
| 171 | atomic_inc(&pg->_count); |
| 172 | ++pg; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static struct kvmppc_linear_info *kvm_alloc_linear(int type) |
| 178 | { |
Paul Mackerras | b4e5122 | 2012-02-03 00:45:02 +0000 | [diff] [blame] | 179 | struct kvmppc_linear_info *ri, *ret; |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 180 | |
Paul Mackerras | b4e5122 | 2012-02-03 00:45:02 +0000 | [diff] [blame] | 181 | ret = NULL; |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 182 | 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 Mackerras | b4e5122 | 2012-02-03 00:45:02 +0000 | [diff] [blame] | 189 | memset(ri->base_virt, 0, ri->npages << PAGE_SHIFT); |
| 190 | ret = ri; |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 191 | break; |
| 192 | } |
| 193 | spin_unlock(&linear_lock); |
Paul Mackerras | b4e5122 | 2012-02-03 00:45:02 +0000 | [diff] [blame] | 194 | return ret; |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static 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 | */ |
| 212 | void __init kvm_linear_init(void) |
| 213 | { |
Alexander Graf | d2a1b48 | 2012-01-16 19:12:11 +0100 | [diff] [blame] | 214 | /* HPT */ |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 215 | kvm_linear_init_one(1 << kvm_hpt_order, kvm_hpt_count, KVM_LINEAR_HPT); |
Alexander Graf | d2a1b48 | 2012-01-16 19:12:11 +0100 | [diff] [blame] | 216 | |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 217 | /* RMA */ |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 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)) |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 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 | |
Alexander Graf | b4e7061 | 2012-01-16 16:50:10 +0100 | [diff] [blame] | 232 | kvm_linear_init_one(kvm_rma_size, kvm_rma_count, KVM_LINEAR_RMA); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 233 | } |