hugetlbpage-book3e.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * PPC Huge TLB Page Support for Book3E MMU
  3. *
  4. * Copyright (C) 2009 David Gibson, IBM Corporation.
  5. * Copyright (C) 2011 Becky Bruce, Freescale Semiconductor
  6. *
  7. */
  8. #include <linux/mm.h>
  9. #include <linux/hugetlb.h>
  10. #include <asm/mmu.h>
  11. #ifdef CONFIG_PPC_FSL_BOOK3E
  12. #ifdef CONFIG_PPC64
  13. static inline int tlb1_next(void)
  14. {
  15. struct paca_struct *paca = get_paca();
  16. struct tlb_core_data *tcd;
  17. int this, next;
  18. tcd = paca->tcd_ptr;
  19. this = tcd->esel_next;
  20. next = this + 1;
  21. if (next >= tcd->esel_max)
  22. next = tcd->esel_first;
  23. tcd->esel_next = next;
  24. return this;
  25. }
  26. #else
  27. static inline int tlb1_next(void)
  28. {
  29. int index, ncams;
  30. ncams = mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY;
  31. index = this_cpu_read(next_tlbcam_idx);
  32. /* Just round-robin the entries and wrap when we hit the end */
  33. if (unlikely(index == ncams - 1))
  34. __this_cpu_write(next_tlbcam_idx, tlbcam_index);
  35. else
  36. __this_cpu_inc(next_tlbcam_idx);
  37. return index;
  38. }
  39. #endif /* !PPC64 */
  40. #endif /* FSL */
  41. static inline int mmu_get_tsize(int psize)
  42. {
  43. return mmu_psize_defs[psize].enc;
  44. }
  45. #if defined(CONFIG_PPC_FSL_BOOK3E) && defined(CONFIG_PPC64)
  46. #include <asm/paca.h>
  47. static inline void book3e_tlb_lock(void)
  48. {
  49. struct paca_struct *paca = get_paca();
  50. unsigned long tmp;
  51. int token = smp_processor_id() + 1;
  52. /*
  53. * Besides being unnecessary in the absence of SMT, this
  54. * check prevents trying to do lbarx/stbcx. on e5500 which
  55. * doesn't implement either feature.
  56. */
  57. if (!cpu_has_feature(CPU_FTR_SMT))
  58. return;
  59. asm volatile("1: lbarx %0, 0, %1;"
  60. "cmpwi %0, 0;"
  61. "bne 2f;"
  62. "stbcx. %2, 0, %1;"
  63. "bne 1b;"
  64. "b 3f;"
  65. "2: lbzx %0, 0, %1;"
  66. "cmpwi %0, 0;"
  67. "bne 2b;"
  68. "b 1b;"
  69. "3:"
  70. : "=&r" (tmp)
  71. : "r" (&paca->tcd_ptr->lock), "r" (token)
  72. : "memory");
  73. }
  74. static inline void book3e_tlb_unlock(void)
  75. {
  76. struct paca_struct *paca = get_paca();
  77. if (!cpu_has_feature(CPU_FTR_SMT))
  78. return;
  79. isync();
  80. paca->tcd_ptr->lock = 0;
  81. }
  82. #else
  83. static inline void book3e_tlb_lock(void)
  84. {
  85. }
  86. static inline void book3e_tlb_unlock(void)
  87. {
  88. }
  89. #endif
  90. static inline int book3e_tlb_exists(unsigned long ea, unsigned long pid)
  91. {
  92. int found = 0;
  93. mtspr(SPRN_MAS6, pid << 16);
  94. if (mmu_has_feature(MMU_FTR_USE_TLBRSRV)) {
  95. asm volatile(
  96. "li %0,0\n"
  97. "tlbsx. 0,%1\n"
  98. "bne 1f\n"
  99. "li %0,1\n"
  100. "1:\n"
  101. : "=&r"(found) : "r"(ea));
  102. } else {
  103. asm volatile(
  104. "tlbsx 0,%1\n"
  105. "mfspr %0,0x271\n"
  106. "srwi %0,%0,31\n"
  107. : "=&r"(found) : "r"(ea));
  108. }
  109. return found;
  110. }
  111. void book3e_hugetlb_preload(struct vm_area_struct *vma, unsigned long ea,
  112. pte_t pte)
  113. {
  114. unsigned long mas1, mas2;
  115. u64 mas7_3;
  116. unsigned long psize, tsize, shift;
  117. unsigned long flags;
  118. struct mm_struct *mm;
  119. #ifdef CONFIG_PPC_FSL_BOOK3E
  120. int index;
  121. #endif
  122. if (unlikely(is_kernel_addr(ea)))
  123. return;
  124. mm = vma->vm_mm;
  125. #ifdef CONFIG_PPC_MM_SLICES
  126. psize = get_slice_psize(mm, ea);
  127. tsize = mmu_get_tsize(psize);
  128. shift = mmu_psize_defs[psize].shift;
  129. #else
  130. psize = vma_mmu_pagesize(vma);
  131. shift = __ilog2(psize);
  132. tsize = shift - 10;
  133. #endif
  134. /*
  135. * We can't be interrupted while we're setting up the MAS
  136. * regusters or after we've confirmed that no tlb exists.
  137. */
  138. local_irq_save(flags);
  139. book3e_tlb_lock();
  140. if (unlikely(book3e_tlb_exists(ea, mm->context.id))) {
  141. book3e_tlb_unlock();
  142. local_irq_restore(flags);
  143. return;
  144. }
  145. #ifdef CONFIG_PPC_FSL_BOOK3E
  146. /* We have to use the CAM(TLB1) on FSL parts for hugepages */
  147. index = tlb1_next();
  148. mtspr(SPRN_MAS0, MAS0_ESEL(index) | MAS0_TLBSEL(1));
  149. #endif
  150. mas1 = MAS1_VALID | MAS1_TID(mm->context.id) | MAS1_TSIZE(tsize);
  151. mas2 = ea & ~((1UL << shift) - 1);
  152. mas2 |= (pte_val(pte) >> PTE_WIMGE_SHIFT) & MAS2_WIMGE_MASK;
  153. mas7_3 = (u64)pte_pfn(pte) << PAGE_SHIFT;
  154. mas7_3 |= (pte_val(pte) >> PTE_BAP_SHIFT) & MAS3_BAP_MASK;
  155. if (!pte_dirty(pte))
  156. mas7_3 &= ~(MAS3_SW|MAS3_UW);
  157. mtspr(SPRN_MAS1, mas1);
  158. mtspr(SPRN_MAS2, mas2);
  159. if (mmu_has_feature(MMU_FTR_USE_PAIRED_MAS)) {
  160. mtspr(SPRN_MAS7_MAS3, mas7_3);
  161. } else {
  162. if (mmu_has_feature(MMU_FTR_BIG_PHYS))
  163. mtspr(SPRN_MAS7, upper_32_bits(mas7_3));
  164. mtspr(SPRN_MAS3, lower_32_bits(mas7_3));
  165. }
  166. asm volatile ("tlbwe");
  167. book3e_tlb_unlock();
  168. local_irq_restore(flags);
  169. }
  170. void flush_hugetlb_page(struct vm_area_struct *vma, unsigned long vmaddr)
  171. {
  172. struct hstate *hstate = hstate_file(vma->vm_file);
  173. unsigned long tsize = huge_page_shift(hstate) - 10;
  174. __flush_tlb_page(vma->vm_mm, vmaddr, tsize, 0);
  175. }