book3s_64_mmu_hv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. *
  15. * Copyright 2010 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  16. */
  17. #include <linux/types.h>
  18. #include <linux/string.h>
  19. #include <linux/kvm.h>
  20. #include <linux/kvm_host.h>
  21. #include <linux/highmem.h>
  22. #include <linux/gfp.h>
  23. #include <linux/slab.h>
  24. #include <linux/hugetlb.h>
  25. #include <linux/vmalloc.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/kvm_ppc.h>
  28. #include <asm/kvm_book3s.h>
  29. #include <asm/mmu-hash64.h>
  30. #include <asm/hvcall.h>
  31. #include <asm/synch.h>
  32. #include <asm/ppc-opcode.h>
  33. #include <asm/cputable.h>
  34. /* Pages in the VRMA are 16MB pages */
  35. #define VRMA_PAGE_ORDER 24
  36. #define VRMA_VSID 0x1ffffffUL /* 1TB VSID reserved for VRMA */
  37. /* POWER7 has 10-bit LPIDs, PPC970 has 6-bit LPIDs */
  38. #define MAX_LPID_970 63
  39. #define NR_LPIDS (LPID_RSVD + 1)
  40. unsigned long lpid_inuse[BITS_TO_LONGS(NR_LPIDS)];
  41. long kvmppc_alloc_hpt(struct kvm *kvm)
  42. {
  43. unsigned long hpt;
  44. unsigned long lpid;
  45. struct revmap_entry *rev;
  46. /* Allocate guest's hashed page table */
  47. hpt = __get_free_pages(GFP_KERNEL|__GFP_ZERO|__GFP_REPEAT|__GFP_NOWARN,
  48. HPT_ORDER - PAGE_SHIFT);
  49. if (!hpt) {
  50. pr_err("kvm_alloc_hpt: Couldn't alloc HPT\n");
  51. return -ENOMEM;
  52. }
  53. kvm->arch.hpt_virt = hpt;
  54. /* Allocate reverse map array */
  55. rev = vmalloc(sizeof(struct revmap_entry) * HPT_NPTE);
  56. if (!rev) {
  57. pr_err("kvmppc_alloc_hpt: Couldn't alloc reverse map array\n");
  58. goto out_freehpt;
  59. }
  60. kvm->arch.revmap = rev;
  61. /* Allocate the guest's logical partition ID */
  62. do {
  63. lpid = find_first_zero_bit(lpid_inuse, NR_LPIDS);
  64. if (lpid >= NR_LPIDS) {
  65. pr_err("kvm_alloc_hpt: No LPIDs free\n");
  66. goto out_freeboth;
  67. }
  68. } while (test_and_set_bit(lpid, lpid_inuse));
  69. kvm->arch.sdr1 = __pa(hpt) | (HPT_ORDER - 18);
  70. kvm->arch.lpid = lpid;
  71. pr_info("KVM guest htab at %lx, LPID %lx\n", hpt, lpid);
  72. return 0;
  73. out_freeboth:
  74. vfree(rev);
  75. out_freehpt:
  76. free_pages(hpt, HPT_ORDER - PAGE_SHIFT);
  77. return -ENOMEM;
  78. }
  79. void kvmppc_free_hpt(struct kvm *kvm)
  80. {
  81. clear_bit(kvm->arch.lpid, lpid_inuse);
  82. vfree(kvm->arch.revmap);
  83. free_pages(kvm->arch.hpt_virt, HPT_ORDER - PAGE_SHIFT);
  84. }
  85. void kvmppc_map_vrma(struct kvm *kvm, struct kvm_userspace_memory_region *mem)
  86. {
  87. unsigned long i;
  88. unsigned long npages = kvm->arch.ram_npages;
  89. unsigned long pfn;
  90. unsigned long *hpte;
  91. unsigned long hash;
  92. unsigned long porder = kvm->arch.ram_porder;
  93. struct revmap_entry *rev;
  94. struct kvmppc_pginfo *pginfo = kvm->arch.ram_pginfo;
  95. if (!pginfo)
  96. return;
  97. /* VRMA can't be > 1TB */
  98. if (npages > 1ul << (40 - porder))
  99. npages = 1ul << (40 - porder);
  100. /* Can't use more than 1 HPTE per HPTEG */
  101. if (npages > HPT_NPTEG)
  102. npages = HPT_NPTEG;
  103. for (i = 0; i < npages; ++i) {
  104. pfn = pginfo[i].pfn;
  105. if (!pfn)
  106. break;
  107. /* can't use hpt_hash since va > 64 bits */
  108. hash = (i ^ (VRMA_VSID ^ (VRMA_VSID << 25))) & HPT_HASH_MASK;
  109. /*
  110. * We assume that the hash table is empty and no
  111. * vcpus are using it at this stage. Since we create
  112. * at most one HPTE per HPTEG, we just assume entry 7
  113. * is available and use it.
  114. */
  115. hash = (hash << 3) + 7;
  116. hpte = (unsigned long *) (kvm->arch.hpt_virt + (hash << 4));
  117. /* HPTE low word - RPN, protection, etc. */
  118. hpte[1] = (pfn << PAGE_SHIFT) | HPTE_R_R | HPTE_R_C |
  119. HPTE_R_M | PP_RWXX;
  120. smp_wmb();
  121. hpte[0] = HPTE_V_1TB_SEG | (VRMA_VSID << (40 - 16)) |
  122. (i << (VRMA_PAGE_ORDER - 16)) | HPTE_V_BOLTED |
  123. HPTE_V_LARGE | HPTE_V_VALID;
  124. /* Reverse map info */
  125. rev = &kvm->arch.revmap[hash];
  126. rev->guest_rpte = (i << porder) | HPTE_R_R | HPTE_R_C |
  127. HPTE_R_M | PP_RWXX;
  128. }
  129. }
  130. int kvmppc_mmu_hv_init(void)
  131. {
  132. unsigned long host_lpid, rsvd_lpid;
  133. if (!cpu_has_feature(CPU_FTR_HVMODE))
  134. return -EINVAL;
  135. memset(lpid_inuse, 0, sizeof(lpid_inuse));
  136. if (cpu_has_feature(CPU_FTR_ARCH_206)) {
  137. host_lpid = mfspr(SPRN_LPID); /* POWER7 */
  138. rsvd_lpid = LPID_RSVD;
  139. } else {
  140. host_lpid = 0; /* PPC970 */
  141. rsvd_lpid = MAX_LPID_970;
  142. }
  143. set_bit(host_lpid, lpid_inuse);
  144. /* rsvd_lpid is reserved for use in partition switching */
  145. set_bit(rsvd_lpid, lpid_inuse);
  146. return 0;
  147. }
  148. void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
  149. {
  150. }
  151. static void kvmppc_mmu_book3s_64_hv_reset_msr(struct kvm_vcpu *vcpu)
  152. {
  153. kvmppc_set_msr(vcpu, MSR_SF | MSR_ME);
  154. }
  155. static int kvmppc_mmu_book3s_64_hv_xlate(struct kvm_vcpu *vcpu, gva_t eaddr,
  156. struct kvmppc_pte *gpte, bool data)
  157. {
  158. return -ENOENT;
  159. }
  160. void kvmppc_mmu_book3s_hv_init(struct kvm_vcpu *vcpu)
  161. {
  162. struct kvmppc_mmu *mmu = &vcpu->arch.mmu;
  163. if (cpu_has_feature(CPU_FTR_ARCH_206))
  164. vcpu->arch.slb_nr = 32; /* POWER7 */
  165. else
  166. vcpu->arch.slb_nr = 64;
  167. mmu->xlate = kvmppc_mmu_book3s_64_hv_xlate;
  168. mmu->reset_msr = kvmppc_mmu_book3s_64_hv_reset_msr;
  169. vcpu->arch.hflags |= BOOK3S_HFLAG_SLB;
  170. }