e500_hugetlbpage.c 4.0 KB

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