book3s_hv_builtin.c 5.0 KB

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