book3s_hv_builtin.c 5.6 KB

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