book3s_hv_builtin.c 5.7 KB

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