e500_tlb.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2008-2011 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: Yu Liu, yu.liu@freescale.com
  5. *
  6. * Description:
  7. * This file is based on arch/powerpc/kvm/44x_tlb.h,
  8. * by Hollis Blanchard <hollisb@us.ibm.com>.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License, version 2, as
  12. * published by the Free Software Foundation.
  13. */
  14. #ifndef __KVM_E500_TLB_H__
  15. #define __KVM_E500_TLB_H__
  16. #include <linux/kvm_host.h>
  17. #include <asm/mmu-book3e.h>
  18. #include <asm/tlb.h>
  19. #include <asm/kvm_e500.h>
  20. #define KVM_E500_TLB0_WAY_SIZE_BIT 7 /* Fixed */
  21. #define KVM_E500_TLB0_WAY_SIZE (1UL << KVM_E500_TLB0_WAY_SIZE_BIT)
  22. #define KVM_E500_TLB0_WAY_SIZE_MASK (KVM_E500_TLB0_WAY_SIZE - 1)
  23. #define KVM_E500_TLB0_WAY_NUM_BIT 1 /* No greater than 7 */
  24. #define KVM_E500_TLB0_WAY_NUM (1UL << KVM_E500_TLB0_WAY_NUM_BIT)
  25. #define KVM_E500_TLB0_WAY_NUM_MASK (KVM_E500_TLB0_WAY_NUM - 1)
  26. #define KVM_E500_TLB0_SIZE (KVM_E500_TLB0_WAY_SIZE * KVM_E500_TLB0_WAY_NUM)
  27. #define KVM_E500_TLB1_SIZE 16
  28. #define index_of(tlbsel, esel) (((tlbsel) << 16) | ((esel) & 0xFFFF))
  29. #define tlbsel_of(index) ((index) >> 16)
  30. #define esel_of(index) ((index) & 0xFFFF)
  31. #define E500_TLB_USER_PERM_MASK (MAS3_UX|MAS3_UR|MAS3_UW)
  32. #define E500_TLB_SUPER_PERM_MASK (MAS3_SX|MAS3_SR|MAS3_SW)
  33. #define MAS2_ATTRIB_MASK \
  34. (MAS2_X0 | MAS2_X1)
  35. #define MAS3_ATTRIB_MASK \
  36. (MAS3_U0 | MAS3_U1 | MAS3_U2 | MAS3_U3 \
  37. | E500_TLB_USER_PERM_MASK | E500_TLB_SUPER_PERM_MASK)
  38. extern void kvmppc_dump_tlbs(struct kvm_vcpu *);
  39. extern int kvmppc_e500_emul_mt_mmucsr0(struct kvmppc_vcpu_e500 *, ulong);
  40. extern int kvmppc_e500_emul_tlbwe(struct kvm_vcpu *);
  41. extern int kvmppc_e500_emul_tlbre(struct kvm_vcpu *);
  42. extern int kvmppc_e500_emul_tlbivax(struct kvm_vcpu *, int, int);
  43. extern int kvmppc_e500_emul_tlbsx(struct kvm_vcpu *, int);
  44. extern int kvmppc_e500_tlb_search(struct kvm_vcpu *, gva_t, unsigned int, int);
  45. extern void kvmppc_e500_tlb_put(struct kvm_vcpu *);
  46. extern void kvmppc_e500_tlb_load(struct kvm_vcpu *, int);
  47. extern int kvmppc_e500_tlb_init(struct kvmppc_vcpu_e500 *);
  48. extern void kvmppc_e500_tlb_uninit(struct kvmppc_vcpu_e500 *);
  49. extern void kvmppc_e500_tlb_setup(struct kvmppc_vcpu_e500 *);
  50. extern void kvmppc_e500_recalc_shadow_pid(struct kvmppc_vcpu_e500 *);
  51. /* TLB helper functions */
  52. static inline unsigned int get_tlb_size(const struct tlbe *tlbe)
  53. {
  54. return (tlbe->mas1 >> 7) & 0x1f;
  55. }
  56. static inline gva_t get_tlb_eaddr(const struct tlbe *tlbe)
  57. {
  58. return tlbe->mas2 & 0xfffff000;
  59. }
  60. static inline u64 get_tlb_bytes(const struct tlbe *tlbe)
  61. {
  62. unsigned int pgsize = get_tlb_size(tlbe);
  63. return 1ULL << 10 << pgsize;
  64. }
  65. static inline gva_t get_tlb_end(const struct tlbe *tlbe)
  66. {
  67. u64 bytes = get_tlb_bytes(tlbe);
  68. return get_tlb_eaddr(tlbe) + bytes - 1;
  69. }
  70. static inline u64 get_tlb_raddr(const struct tlbe *tlbe)
  71. {
  72. u64 rpn = tlbe->mas7;
  73. return (rpn << 32) | (tlbe->mas3 & 0xfffff000);
  74. }
  75. static inline unsigned int get_tlb_tid(const struct tlbe *tlbe)
  76. {
  77. return (tlbe->mas1 >> 16) & 0xff;
  78. }
  79. static inline unsigned int get_tlb_ts(const struct tlbe *tlbe)
  80. {
  81. return (tlbe->mas1 >> 12) & 0x1;
  82. }
  83. static inline unsigned int get_tlb_v(const struct tlbe *tlbe)
  84. {
  85. return (tlbe->mas1 >> 31) & 0x1;
  86. }
  87. static inline unsigned int get_tlb_iprot(const struct tlbe *tlbe)
  88. {
  89. return (tlbe->mas1 >> 30) & 0x1;
  90. }
  91. static inline unsigned int get_cur_pid(struct kvm_vcpu *vcpu)
  92. {
  93. return vcpu->arch.pid & 0xff;
  94. }
  95. static inline unsigned int get_cur_as(struct kvm_vcpu *vcpu)
  96. {
  97. return !!(vcpu->arch.shared->msr & (MSR_IS | MSR_DS));
  98. }
  99. static inline unsigned int get_cur_pr(struct kvm_vcpu *vcpu)
  100. {
  101. return !!(vcpu->arch.shared->msr & MSR_PR);
  102. }
  103. static inline unsigned int get_cur_spid(
  104. const struct kvmppc_vcpu_e500 *vcpu_e500)
  105. {
  106. return (vcpu_e500->mas6 >> 16) & 0xff;
  107. }
  108. static inline unsigned int get_cur_sas(
  109. const struct kvmppc_vcpu_e500 *vcpu_e500)
  110. {
  111. return vcpu_e500->mas6 & 0x1;
  112. }
  113. static inline unsigned int get_tlb_tlbsel(
  114. const struct kvmppc_vcpu_e500 *vcpu_e500)
  115. {
  116. /*
  117. * Manual says that tlbsel has 2 bits wide.
  118. * Since we only have two TLBs, only lower bit is used.
  119. */
  120. return (vcpu_e500->mas0 >> 28) & 0x1;
  121. }
  122. static inline unsigned int get_tlb_nv_bit(
  123. const struct kvmppc_vcpu_e500 *vcpu_e500)
  124. {
  125. return vcpu_e500->mas0 & 0xfff;
  126. }
  127. static inline unsigned int get_tlb_esel_bit(
  128. const struct kvmppc_vcpu_e500 *vcpu_e500)
  129. {
  130. return (vcpu_e500->mas0 >> 16) & 0xfff;
  131. }
  132. static inline unsigned int get_tlb_esel(
  133. const struct kvmppc_vcpu_e500 *vcpu_e500,
  134. int tlbsel)
  135. {
  136. unsigned int esel = get_tlb_esel_bit(vcpu_e500);
  137. if (tlbsel == 0) {
  138. esel &= KVM_E500_TLB0_WAY_NUM_MASK;
  139. esel |= ((vcpu_e500->mas2 >> 12) & KVM_E500_TLB0_WAY_SIZE_MASK)
  140. << KVM_E500_TLB0_WAY_NUM_BIT;
  141. } else {
  142. esel &= KVM_E500_TLB1_SIZE - 1;
  143. }
  144. return esel;
  145. }
  146. static inline int tlbe_is_host_safe(const struct kvm_vcpu *vcpu,
  147. const struct tlbe *tlbe)
  148. {
  149. gpa_t gpa;
  150. if (!get_tlb_v(tlbe))
  151. return 0;
  152. /* Does it match current guest AS? */
  153. /* XXX what about IS != DS? */
  154. if (get_tlb_ts(tlbe) != !!(vcpu->arch.shared->msr & MSR_IS))
  155. return 0;
  156. gpa = get_tlb_raddr(tlbe);
  157. if (!gfn_to_memslot(vcpu->kvm, gpa >> PAGE_SHIFT))
  158. /* Mapping is not for RAM. */
  159. return 0;
  160. return 1;
  161. }
  162. #endif /* __KVM_E500_TLB_H__ */