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> 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> 16 17 #include <asm/cputable.h> 18 #include <asm/kvm_ppc.h> 19 #include <asm/kvm_book3s.h> 20 21 #define KVM_LINEAR_RMA 0 22 23 static void __init kvm_linear_init_one(ulong size, int count, int type); 24 static struct kvmppc_linear_info *kvm_alloc_linear(int type); 25 static void kvm_release_linear(struct kvmppc_linear_info *ri); 26 27 /*************** RMA *************/ 28 29 /* 30 * This maintains a list of RMAs (real mode areas) for KVM guests to use. 31 * Each RMA has to be physically contiguous and of a size that the 32 * hardware supports. PPC970 and POWER7 support 64MB, 128MB and 256MB, 33 * and other larger sizes. Since we are unlikely to be allocate that 34 * much physically contiguous memory after the system is up and running, 35 * we preallocate a set of RMAs in early boot for KVM to use. 36 */ 37 static unsigned long kvm_rma_size = 64 << 20; /* 64MB */ 38 static unsigned long kvm_rma_count; 39 40 /* Work out RMLS (real mode limit selector) field value for a given RMA size. 41 Assumes POWER7 or PPC970. */ 42 static inline int lpcr_rmls(unsigned long rma_size) 43 { 44 switch (rma_size) { 45 case 32ul << 20: /* 32 MB */ 46 if (cpu_has_feature(CPU_FTR_ARCH_206)) 47 return 8; /* only supported on POWER7 */ 48 return -1; 49 case 64ul << 20: /* 64 MB */ 50 return 3; 51 case 128ul << 20: /* 128 MB */ 52 return 7; 53 case 256ul << 20: /* 256 MB */ 54 return 4; 55 case 1ul << 30: /* 1 GB */ 56 return 2; 57 case 16ul << 30: /* 16 GB */ 58 return 1; 59 case 256ul << 30: /* 256 GB */ 60 return 0; 61 default: 62 return -1; 63 } 64 } 65 66 static int __init early_parse_rma_size(char *p) 67 { 68 if (!p) 69 return 1; 70 71 kvm_rma_size = memparse(p, &p); 72 73 return 0; 74 } 75 early_param("kvm_rma_size", early_parse_rma_size); 76 77 static int __init early_parse_rma_count(char *p) 78 { 79 if (!p) 80 return 1; 81 82 kvm_rma_count = simple_strtoul(p, NULL, 0); 83 84 return 0; 85 } 86 early_param("kvm_rma_count", early_parse_rma_count); 87 88 struct kvmppc_linear_info *kvm_alloc_rma(void) 89 { 90 return kvm_alloc_linear(KVM_LINEAR_RMA); 91 } 92 EXPORT_SYMBOL_GPL(kvm_alloc_rma); 93 94 void kvm_release_rma(struct kvmppc_linear_info *ri) 95 { 96 kvm_release_linear(ri); 97 } 98 EXPORT_SYMBOL_GPL(kvm_release_rma); 99 100 /*************** generic *************/ 101 102 static LIST_HEAD(free_linears); 103 static DEFINE_SPINLOCK(linear_lock); 104 105 static void __init kvm_linear_init_one(ulong size, int count, int type) 106 { 107 unsigned long i; 108 unsigned long j, npages; 109 void *linear; 110 struct page *pg; 111 const char *typestr; 112 struct kvmppc_linear_info *linear_info; 113 114 if (!count) 115 return; 116 117 typestr = (type == KVM_LINEAR_RMA) ? "RMA" : ""; 118 119 npages = size >> PAGE_SHIFT; 120 linear_info = alloc_bootmem(count * sizeof(struct kvmppc_linear_info)); 121 for (i = 0; i < count; ++i) { 122 linear = alloc_bootmem_align(size, size); 123 pr_info("Allocated KVM %s at %p (%ld MB)\n", typestr, linear, 124 size >> 20); 125 linear_info[i].base_virt = linear; 126 linear_info[i].base_pfn = __pa(linear) >> PAGE_SHIFT; 127 linear_info[i].npages = npages; 128 linear_info[i].type = type; 129 list_add_tail(&linear_info[i].list, &free_linears); 130 atomic_set(&linear_info[i].use_count, 0); 131 132 pg = pfn_to_page(linear_info[i].base_pfn); 133 for (j = 0; j < npages; ++j) { 134 atomic_inc(&pg->_count); 135 ++pg; 136 } 137 } 138 } 139 140 static struct kvmppc_linear_info *kvm_alloc_linear(int type) 141 { 142 struct kvmppc_linear_info *ri; 143 144 ri = NULL; 145 spin_lock(&linear_lock); 146 list_for_each_entry(ri, &free_linears, list) { 147 if (ri->type != type) 148 continue; 149 150 list_del(&ri->list); 151 atomic_inc(&ri->use_count); 152 break; 153 } 154 spin_unlock(&linear_lock); 155 memset(ri->base_virt, 0, ri->npages << PAGE_SHIFT); 156 return ri; 157 } 158 159 static void kvm_release_linear(struct kvmppc_linear_info *ri) 160 { 161 if (atomic_dec_and_test(&ri->use_count)) { 162 spin_lock(&linear_lock); 163 list_add_tail(&ri->list, &free_linears); 164 spin_unlock(&linear_lock); 165 166 } 167 } 168 169 /* 170 * Called at boot time while the bootmem allocator is active, 171 * to allocate contiguous physical memory for the hash page 172 * tables for guests. 173 */ 174 void __init kvm_linear_init(void) 175 { 176 /* RMA */ 177 /* Only do this on PPC970 in HV mode */ 178 if (!cpu_has_feature(CPU_FTR_HVMODE) || 179 !cpu_has_feature(CPU_FTR_ARCH_201)) 180 return; 181 182 if (!kvm_rma_size || !kvm_rma_count) 183 return; 184 185 /* Check that the requested size is one supported in hardware */ 186 if (lpcr_rmls(kvm_rma_size) < 0) { 187 pr_err("RMA size of 0x%lx not supported\n", kvm_rma_size); 188 return; 189 } 190 191 kvm_linear_init_one(kvm_rma_size, kvm_rma_count, KVM_LINEAR_RMA); 192 } 193