hugetlbpage-hash64.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <rohit.seth@intel.com>
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/hugetlb.h>
  12. #include <asm/pgtable.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/machdep.h>
  16. extern long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
  17. unsigned long pa, unsigned long rlags,
  18. unsigned long vflags, int psize, int ssize);
  19. int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
  20. pte_t *ptep, unsigned long trap, unsigned long flags,
  21. int ssize, unsigned int shift, unsigned int mmu_psize)
  22. {
  23. real_pte_t rpte;
  24. unsigned long vpn;
  25. unsigned long old_pte, new_pte;
  26. unsigned long rflags, pa, sz;
  27. long slot, offset;
  28. BUG_ON(shift != mmu_psize_defs[mmu_psize].shift);
  29. /* Search the Linux page table for a match with va */
  30. vpn = hpt_vpn(ea, vsid, ssize);
  31. /* At this point, we have a pte (old_pte) which can be used to build
  32. * or update an HPTE. There are 2 cases:
  33. *
  34. * 1. There is a valid (present) pte with no associated HPTE (this is
  35. * the most common case)
  36. * 2. There is a valid (present) pte with an associated HPTE. The
  37. * current values of the pp bits in the HPTE prevent access
  38. * because we are doing software DIRTY bit management and the
  39. * page is currently not DIRTY.
  40. */
  41. do {
  42. old_pte = pte_val(*ptep);
  43. /* If PTE busy, retry the access */
  44. if (unlikely(old_pte & H_PAGE_BUSY))
  45. return 0;
  46. /* If PTE permissions don't match, take page fault */
  47. if (unlikely(!check_pte_access(access, old_pte)))
  48. return 1;
  49. /* Try to lock the PTE, add ACCESSED and DIRTY if it was
  50. * a write access */
  51. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
  52. if (access & _PAGE_WRITE)
  53. new_pte |= _PAGE_DIRTY;
  54. } while(!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  55. /* Make sure this is a hugetlb entry */
  56. if (old_pte & (H_PAGE_THP_HUGE | _PAGE_DEVMAP))
  57. return 0;
  58. rflags = htab_convert_pte_flags(new_pte);
  59. if (unlikely(mmu_psize == MMU_PAGE_16G))
  60. offset = PTRS_PER_PUD;
  61. else
  62. offset = PTRS_PER_PMD;
  63. rpte = __real_pte(__pte(old_pte), ptep, offset);
  64. sz = ((1UL) << shift);
  65. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  66. /* No CPU has hugepages but lacks no execute, so we
  67. * don't need to worry about that case */
  68. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  69. /* Check if pte already has an hpte (case 2) */
  70. if (unlikely(old_pte & H_PAGE_HASHPTE)) {
  71. /* There MIGHT be an HPTE for this pte */
  72. unsigned long gslot;
  73. gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0);
  74. if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, mmu_psize,
  75. mmu_psize, ssize, flags) == -1)
  76. old_pte &= ~_PAGE_HPTEFLAGS;
  77. }
  78. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  79. unsigned long hash = hpt_hash(vpn, shift, ssize);
  80. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  81. /* clear HPTE slot informations in new PTE */
  82. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
  83. slot = hpte_insert_repeating(hash, vpn, pa, rflags, 0,
  84. mmu_psize, ssize);
  85. /*
  86. * Hypervisor failure. Restore old pte and return -1
  87. * similar to __hash_page_*
  88. */
  89. if (unlikely(slot == -2)) {
  90. *ptep = __pte(old_pte);
  91. hash_failure_debug(ea, access, vsid, trap, ssize,
  92. mmu_psize, mmu_psize, old_pte);
  93. return -1;
  94. }
  95. new_pte |= pte_set_hidx(ptep, rpte, 0, slot, offset);
  96. }
  97. /*
  98. * No need to use ldarx/stdcx here
  99. */
  100. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  101. return 0;
  102. }