book3s_hv_builtin.c 5.5 KB

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