book3s_hv_builtin.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/init.h>
  15. #include <linux/memblock.h>
  16. #include <linux/sizes.h>
  17. #include <linux/cma.h>
  18. #include <linux/bitops.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. static struct cma *kvm_cma;
  33. static int __init early_parse_kvm_cma_resv(char *p)
  34. {
  35. pr_debug("%s(%s)\n", __func__, p);
  36. if (!p)
  37. return -EINVAL;
  38. return kstrtoul(p, 0, &kvm_cma_resv_ratio);
  39. }
  40. early_param("kvm_cma_resv_ratio", early_parse_kvm_cma_resv);
  41. struct page *kvm_alloc_hpt(unsigned long nr_pages)
  42. {
  43. VM_BUG_ON(order_base_2(nr_pages) < KVM_CMA_CHUNK_ORDER - PAGE_SHIFT);
  44. return cma_alloc(kvm_cma, nr_pages, order_base_2(HPT_ALIGN_PAGES));
  45. }
  46. EXPORT_SYMBOL_GPL(kvm_alloc_hpt);
  47. void kvm_release_hpt(struct page *page, unsigned long nr_pages)
  48. {
  49. cma_release(kvm_cma, page, nr_pages);
  50. }
  51. EXPORT_SYMBOL_GPL(kvm_release_hpt);
  52. /**
  53. * kvm_cma_reserve() - reserve area for kvm hash pagetable
  54. *
  55. * This function reserves memory from early allocator. It should be
  56. * called by arch specific code once the memblock allocator
  57. * has been activated and all other subsystems have already allocated/reserved
  58. * memory.
  59. */
  60. void __init kvm_cma_reserve(void)
  61. {
  62. unsigned long align_size;
  63. struct memblock_region *reg;
  64. phys_addr_t selected_size = 0;
  65. /*
  66. * We need CMA reservation only when we are in HV mode
  67. */
  68. if (!cpu_has_feature(CPU_FTR_HVMODE))
  69. return;
  70. /*
  71. * We cannot use memblock_phys_mem_size() here, because
  72. * memblock_analyze() has not been called yet.
  73. */
  74. for_each_memblock(memory, reg)
  75. selected_size += memblock_region_memory_end_pfn(reg) -
  76. memblock_region_memory_base_pfn(reg);
  77. selected_size = (selected_size * kvm_cma_resv_ratio / 100) << PAGE_SHIFT;
  78. if (selected_size) {
  79. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  80. (unsigned long)selected_size / SZ_1M);
  81. align_size = HPT_ALIGN_PAGES << PAGE_SHIFT;
  82. cma_declare_contiguous(0, selected_size, 0, align_size,
  83. KVM_CMA_CHUNK_ORDER - PAGE_SHIFT, false, &kvm_cma);
  84. }
  85. }
  86. /*
  87. * Real-mode H_CONFER implementation.
  88. * We check if we are the only vcpu out of this virtual core
  89. * still running in the guest and not ceded. If so, we pop up
  90. * to the virtual-mode implementation; if not, just return to
  91. * the guest.
  92. */
  93. long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
  94. unsigned int yield_count)
  95. {
  96. struct kvmppc_vcore *vc = vcpu->arch.vcore;
  97. int threads_running;
  98. int threads_ceded;
  99. int threads_conferring;
  100. u64 stop = get_tb() + 10 * tb_ticks_per_usec;
  101. int rv = H_SUCCESS; /* => don't yield */
  102. set_bit(vcpu->arch.ptid, &vc->conferring_threads);
  103. while ((get_tb() < stop) && (VCORE_EXIT_COUNT(vc) == 0)) {
  104. threads_running = VCORE_ENTRY_COUNT(vc);
  105. threads_ceded = hweight32(vc->napping_threads);
  106. threads_conferring = hweight32(vc->conferring_threads);
  107. if (threads_ceded + threads_conferring >= threads_running) {
  108. rv = H_TOO_HARD; /* => do yield */
  109. break;
  110. }
  111. }
  112. clear_bit(vcpu->arch.ptid, &vc->conferring_threads);
  113. return rv;
  114. }
  115. /*
  116. * When running HV mode KVM we need to block certain operations while KVM VMs
  117. * exist in the system. We use a counter of VMs to track this.
  118. *
  119. * One of the operations we need to block is onlining of secondaries, so we
  120. * protect hv_vm_count with get/put_online_cpus().
  121. */
  122. static atomic_t hv_vm_count;
  123. void kvm_hv_vm_activated(void)
  124. {
  125. get_online_cpus();
  126. atomic_inc(&hv_vm_count);
  127. put_online_cpus();
  128. }
  129. EXPORT_SYMBOL_GPL(kvm_hv_vm_activated);
  130. void kvm_hv_vm_deactivated(void)
  131. {
  132. get_online_cpus();
  133. atomic_dec(&hv_vm_count);
  134. put_online_cpus();
  135. }
  136. EXPORT_SYMBOL_GPL(kvm_hv_vm_deactivated);
  137. bool kvm_hv_mode_active(void)
  138. {
  139. return atomic_read(&hv_vm_count) != 0;
  140. }
  141. extern int hcall_real_table[], hcall_real_table_end[];
  142. int kvmppc_hcall_impl_hv_realmode(unsigned long cmd)
  143. {
  144. cmd /= 4;
  145. if (cmd < hcall_real_table_end - hcall_real_table &&
  146. hcall_real_table[cmd])
  147. return 1;
  148. return 0;
  149. }
  150. EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode);