hugetlbpage.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PPC64 Huge TLB Page Support for hash based MMUs (POWER4 and later)
  4. *
  5. * Copyright (C) 2003 David Gibson, IBM Corporation.
  6. *
  7. * Based on the IA-32 version:
  8. * Copyright (C) 2002, Rohit Seth <[email protected]>
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/hugetlb.h>
  12. #include <asm/cacheflush.h>
  13. #include <asm/machdep.h>
  14. unsigned int hpage_shift;
  15. EXPORT_SYMBOL(hpage_shift);
  16. #ifdef CONFIG_PPC_64S_HASH_MMU
  17. int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
  18. pte_t *ptep, unsigned long trap, unsigned long flags,
  19. int ssize, unsigned int shift, unsigned int mmu_psize)
  20. {
  21. real_pte_t rpte;
  22. unsigned long vpn;
  23. unsigned long old_pte, new_pte;
  24. unsigned long rflags, pa;
  25. long slot, offset;
  26. BUG_ON(shift != mmu_psize_defs[mmu_psize].shift);
  27. /* Search the Linux page table for a match with va */
  28. vpn = hpt_vpn(ea, vsid, ssize);
  29. /*
  30. * At this point, we have a pte (old_pte) which can be used to build
  31. * or update an HPTE. There are 2 cases:
  32. *
  33. * 1. There is a valid (present) pte with no associated HPTE (this is
  34. * the most common case)
  35. * 2. There is a valid (present) pte with an associated HPTE. The
  36. * current values of the pp bits in the HPTE prevent access
  37. * because we are doing software DIRTY bit management and the
  38. * page is currently not DIRTY.
  39. */
  40. do {
  41. old_pte = pte_val(*ptep);
  42. /* If PTE busy, retry the access */
  43. if (unlikely(old_pte & H_PAGE_BUSY))
  44. return 0;
  45. /* If PTE permissions don't match, take page fault */
  46. if (unlikely(!check_pte_access(access, old_pte)))
  47. return 1;
  48. /*
  49. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  50. * a write access
  51. */
  52. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
  53. if (access & _PAGE_WRITE)
  54. new_pte |= _PAGE_DIRTY;
  55. } while(!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  56. /* Make sure this is a hugetlb entry */
  57. if (old_pte & (H_PAGE_THP_HUGE | _PAGE_DEVMAP))
  58. return 0;
  59. rflags = htab_convert_pte_flags(new_pte, flags);
  60. if (unlikely(mmu_psize == MMU_PAGE_16G))
  61. offset = PTRS_PER_PUD;
  62. else
  63. offset = PTRS_PER_PMD;
  64. rpte = __real_pte(__pte(old_pte), ptep, offset);
  65. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  66. /*
  67. * No CPU has hugepages but lacks no execute, so we
  68. * don't need to worry about that case
  69. */
  70. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  71. /* Check if pte already has an hpte (case 2) */
  72. if (unlikely(old_pte & H_PAGE_HASHPTE)) {
  73. /* There MIGHT be an HPTE for this pte */
  74. unsigned long gslot;
  75. gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0);
  76. if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, mmu_psize,
  77. mmu_psize, ssize, flags) == -1)
  78. old_pte &= ~_PAGE_HPTEFLAGS;
  79. }
  80. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  81. unsigned long hash = hpt_hash(vpn, shift, ssize);
  82. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  83. /* clear HPTE slot informations in new PTE */
  84. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
  85. slot = hpte_insert_repeating(hash, vpn, pa, rflags, 0,
  86. mmu_psize, ssize);
  87. /*
  88. * Hypervisor failure. Restore old pte and return -1
  89. * similar to __hash_page_*
  90. */
  91. if (unlikely(slot == -2)) {
  92. *ptep = __pte(old_pte);
  93. hash_failure_debug(ea, access, vsid, trap, ssize,
  94. mmu_psize, mmu_psize, old_pte);
  95. return -1;
  96. }
  97. new_pte |= pte_set_hidx(ptep, rpte, 0, slot, offset);
  98. }
  99. /*
  100. * No need to use ldarx/stdcx here
  101. */
  102. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  103. return 0;
  104. }
  105. #endif
  106. pte_t huge_ptep_modify_prot_start(struct vm_area_struct *vma,
  107. unsigned long addr, pte_t *ptep)
  108. {
  109. unsigned long pte_val;
  110. /*
  111. * Clear the _PAGE_PRESENT so that no hardware parallel update is
  112. * possible. Also keep the pte_present true so that we don't take
  113. * wrong fault.
  114. */
  115. pte_val = pte_update(vma->vm_mm, addr, ptep,
  116. _PAGE_PRESENT, _PAGE_INVALID, 1);
  117. return __pte(pte_val);
  118. }
  119. void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, unsigned long addr,
  120. pte_t *ptep, pte_t old_pte, pte_t pte)
  121. {
  122. if (radix_enabled())
  123. return radix__huge_ptep_modify_prot_commit(vma, addr, ptep,
  124. old_pte, pte);
  125. set_huge_pte_at(vma->vm_mm, addr, ptep, pte);
  126. }
  127. void __init hugetlbpage_init_defaultsize(void)
  128. {
  129. /* Set default large page size. Currently, we pick 16M or 1M
  130. * depending on what is available
  131. */
  132. if (mmu_psize_defs[MMU_PAGE_16M].shift)
  133. hpage_shift = mmu_psize_defs[MMU_PAGE_16M].shift;
  134. else if (mmu_psize_defs[MMU_PAGE_1M].shift)
  135. hpage_shift = mmu_psize_defs[MMU_PAGE_1M].shift;
  136. else if (mmu_psize_defs[MMU_PAGE_2M].shift)
  137. hpage_shift = mmu_psize_defs[MMU_PAGE_2M].shift;
  138. }