hugetlbpage-hash64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. rflags = htab_convert_pte_flags(new_pte);
  56. if (unlikely(mmu_psize == MMU_PAGE_16G))
  57. offset = PTRS_PER_PUD;
  58. else
  59. offset = PTRS_PER_PMD;
  60. rpte = __real_pte(__pte(old_pte), ptep, offset);
  61. sz = ((1UL) << shift);
  62. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  63. /* No CPU has hugepages but lacks no execute, so we
  64. * don't need to worry about that case */
  65. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  66. /* Check if pte already has an hpte (case 2) */
  67. if (unlikely(old_pte & H_PAGE_HASHPTE)) {
  68. /* There MIGHT be an HPTE for this pte */
  69. unsigned long gslot;
  70. gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0);
  71. if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, mmu_psize,
  72. mmu_psize, ssize, flags) == -1)
  73. old_pte &= ~_PAGE_HPTEFLAGS;
  74. }
  75. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  76. unsigned long hash = hpt_hash(vpn, shift, ssize);
  77. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  78. /* clear HPTE slot informations in new PTE */
  79. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
  80. slot = hpte_insert_repeating(hash, vpn, pa, rflags, 0,
  81. mmu_psize, ssize);
  82. /*
  83. * Hypervisor failure. Restore old pte and return -1
  84. * similar to __hash_page_*
  85. */
  86. if (unlikely(slot == -2)) {
  87. *ptep = __pte(old_pte);
  88. hash_failure_debug(ea, access, vsid, trap, ssize,
  89. mmu_psize, mmu_psize, old_pte);
  90. return -1;
  91. }
  92. new_pte |= pte_set_hidx(ptep, rpte, 0, slot, offset);
  93. }
  94. /*
  95. * No need to use ldarx/stdcx here
  96. */
  97. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  98. return 0;
  99. }