book3s_hv_builtin.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/cpu.h>
  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. #include <linux/memblock.h>
  17. #include <linux/sizes.h>
  18. #include <linux/cma.h>
  19. #include <asm/cputable.h>
  20. #include <asm/kvm_ppc.h>
  21. #include <asm/kvm_book3s.h>
  22. #define KVM_CMA_CHUNK_ORDER 18
  23. /*
  24. * Hash page table alignment on newer cpus(CPU_FTR_ARCH_206)
  25. * should be power of 2.
  26. */
  27. #define HPT_ALIGN_PAGES ((1 << 18) >> PAGE_SHIFT) /* 256k */
  28. /*
  29. * By default we reserve 5% of memory for hash pagetable allocation.
  30. */
  31. static unsigned long kvm_cma_resv_ratio = 5;
  32. /*
  33. * We allocate RMAs (real mode areas) for KVM guests from the KVM CMA area.
  34. * Each RMA has to be physically contiguous and of a size that the
  35. * hardware supports. PPC970 and POWER7 support 64MB, 128MB and 256MB,
  36. * and other larger sizes. Since we are unlikely to be allocate that
  37. * much physically contiguous memory after the system is up and running,
  38. * we preallocate a set of RMAs in early boot using CMA.
  39. * should be power of 2.
  40. */
  41. unsigned long kvm_rma_pages = (1 << 27) >> PAGE_SHIFT; /* 128MB */
  42. EXPORT_SYMBOL_GPL(kvm_rma_pages);
  43. static struct cma *kvm_cma;
  44. /* Work out RMLS (real mode limit selector) field value for a given RMA size.
  45. Assumes POWER7 or PPC970. */
  46. static inline int lpcr_rmls(unsigned long rma_size)
  47. {
  48. switch (rma_size) {
  49. case 32ul << 20: /* 32 MB */
  50. if (cpu_has_feature(CPU_FTR_ARCH_206))
  51. return 8; /* only supported on POWER7 */
  52. return -1;
  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. static int __init early_parse_rma_size(char *p)
  70. {
  71. unsigned long kvm_rma_size;
  72. pr_debug("%s(%s)\n", __func__, p);
  73. if (!p)
  74. return -EINVAL;
  75. kvm_rma_size = memparse(p, &p);
  76. /*
  77. * Check that the requested size is one supported in hardware
  78. */
  79. if (lpcr_rmls(kvm_rma_size) < 0) {
  80. pr_err("RMA size of 0x%lx not supported\n", kvm_rma_size);
  81. return -EINVAL;
  82. }
  83. kvm_rma_pages = kvm_rma_size >> PAGE_SHIFT;
  84. return 0;
  85. }
  86. early_param("kvm_rma_size", early_parse_rma_size);
  87. struct kvm_rma_info *kvm_alloc_rma()
  88. {
  89. struct page *page;
  90. struct kvm_rma_info *ri;
  91. ri = kmalloc(sizeof(struct kvm_rma_info), GFP_KERNEL);
  92. if (!ri)
  93. return NULL;
  94. page = cma_alloc(kvm_cma, kvm_rma_pages, order_base_2(kvm_rma_pages));
  95. if (!page)
  96. goto err_out;
  97. atomic_set(&ri->use_count, 1);
  98. ri->base_pfn = page_to_pfn(page);
  99. return ri;
  100. err_out:
  101. kfree(ri);
  102. return NULL;
  103. }
  104. EXPORT_SYMBOL_GPL(kvm_alloc_rma);
  105. void kvm_release_rma(struct kvm_rma_info *ri)
  106. {
  107. if (atomic_dec_and_test(&ri->use_count)) {
  108. cma_release(kvm_cma, pfn_to_page(ri->base_pfn), kvm_rma_pages);
  109. kfree(ri);
  110. }
  111. }
  112. EXPORT_SYMBOL_GPL(kvm_release_rma);
  113. static int __init early_parse_kvm_cma_resv(char *p)
  114. {
  115. pr_debug("%s(%s)\n", __func__, p);
  116. if (!p)
  117. return -EINVAL;
  118. return kstrtoul(p, 0, &kvm_cma_resv_ratio);
  119. }
  120. early_param("kvm_cma_resv_ratio", early_parse_kvm_cma_resv);
  121. struct page *kvm_alloc_hpt(unsigned long nr_pages)
  122. {
  123. unsigned long align_pages = HPT_ALIGN_PAGES;
  124. VM_BUG_ON(order_base_2(nr_pages) < KVM_CMA_CHUNK_ORDER - PAGE_SHIFT);
  125. /* Old CPUs require HPT aligned on a multiple of its size */
  126. if (!cpu_has_feature(CPU_FTR_ARCH_206))
  127. align_pages = nr_pages;
  128. return cma_alloc(kvm_cma, nr_pages, order_base_2(align_pages));
  129. }
  130. EXPORT_SYMBOL_GPL(kvm_alloc_hpt);
  131. void kvm_release_hpt(struct page *page, unsigned long nr_pages)
  132. {
  133. cma_release(kvm_cma, page, nr_pages);
  134. }
  135. EXPORT_SYMBOL_GPL(kvm_release_hpt);
  136. /**
  137. * kvm_cma_reserve() - reserve area for kvm hash pagetable
  138. *
  139. * This function reserves memory from early allocator. It should be
  140. * called by arch specific code once the early allocator (memblock or bootmem)
  141. * has been activated and all other subsystems have already allocated/reserved
  142. * memory.
  143. */
  144. void __init kvm_cma_reserve(void)
  145. {
  146. unsigned long align_size;
  147. struct memblock_region *reg;
  148. phys_addr_t selected_size = 0;
  149. /*
  150. * We need CMA reservation only when we are in HV mode
  151. */
  152. if (!cpu_has_feature(CPU_FTR_HVMODE))
  153. return;
  154. /*
  155. * We cannot use memblock_phys_mem_size() here, because
  156. * memblock_analyze() has not been called yet.
  157. */
  158. for_each_memblock(memory, reg)
  159. selected_size += memblock_region_memory_end_pfn(reg) -
  160. memblock_region_memory_base_pfn(reg);
  161. selected_size = (selected_size * kvm_cma_resv_ratio / 100) << PAGE_SHIFT;
  162. if (selected_size) {
  163. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  164. (unsigned long)selected_size / SZ_1M);
  165. /*
  166. * Old CPUs require HPT aligned on a multiple of its size. So for them
  167. * make the alignment as max size we could request.
  168. */
  169. if (!cpu_has_feature(CPU_FTR_ARCH_206))
  170. align_size = __rounddown_pow_of_two(selected_size);
  171. else
  172. align_size = HPT_ALIGN_PAGES << PAGE_SHIFT;
  173. align_size = max(kvm_rma_pages << PAGE_SHIFT, align_size);
  174. cma_declare_contiguous(0, selected_size, 0, align_size,
  175. KVM_CMA_CHUNK_ORDER - PAGE_SHIFT, false, &kvm_cma);
  176. }
  177. }
  178. /*
  179. * When running HV mode KVM we need to block certain operations while KVM VMs
  180. * exist in the system. We use a counter of VMs to track this.
  181. *
  182. * One of the operations we need to block is onlining of secondaries, so we
  183. * protect hv_vm_count with get/put_online_cpus().
  184. */
  185. static atomic_t hv_vm_count;
  186. void kvm_hv_vm_activated(void)
  187. {
  188. get_online_cpus();
  189. atomic_inc(&hv_vm_count);
  190. put_online_cpus();
  191. }
  192. EXPORT_SYMBOL_GPL(kvm_hv_vm_activated);
  193. void kvm_hv_vm_deactivated(void)
  194. {
  195. get_online_cpus();
  196. atomic_dec(&hv_vm_count);
  197. put_online_cpus();
  198. }
  199. EXPORT_SYMBOL_GPL(kvm_hv_vm_deactivated);
  200. bool kvm_hv_mode_active(void)
  201. {
  202. return atomic_read(&hv_vm_count) != 0;
  203. }
  204. extern int hcall_real_table[], hcall_real_table_end[];
  205. int kvmppc_hcall_impl_hv_realmode(unsigned long cmd)
  206. {
  207. cmd /= 4;
  208. if (cmd < hcall_real_table_end - hcall_real_table &&
  209. hcall_real_table[cmd])
  210. return 1;
  211. return 0;
  212. }
  213. EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode);