hugetlbpage-hash64.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * PPC64 Huge TLB Page Support for hash based MMUs (POWER4 and later)
  3. *
  4. * Copyright (C) 2003 David Gibson, IBM Corporation.
  5. *
  6. * Based on the IA-32 version:
  7. * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/hugetlb.h>
  11. #include <asm/pgtable.h>
  12. #include <asm/pgalloc.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/machdep.h>
  15. int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
  16. pte_t *ptep, unsigned long trap, int local, int ssize,
  17. unsigned int shift, unsigned int mmu_psize)
  18. {
  19. unsigned long vpn;
  20. unsigned long old_pte, new_pte;
  21. unsigned long rflags, pa, sz;
  22. long slot;
  23. BUG_ON(shift != mmu_psize_defs[mmu_psize].shift);
  24. /* Search the Linux page table for a match with va */
  25. vpn = hpt_vpn(ea, vsid, ssize);
  26. /* At this point, we have a pte (old_pte) which can be used to build
  27. * or update an HPTE. There are 2 cases:
  28. *
  29. * 1. There is a valid (present) pte with no associated HPTE (this is
  30. * the most common case)
  31. * 2. There is a valid (present) pte with an associated HPTE. The
  32. * current values of the pp bits in the HPTE prevent access
  33. * because we are doing software DIRTY bit management and the
  34. * page is currently not DIRTY.
  35. */
  36. do {
  37. old_pte = pte_val(*ptep);
  38. /* If PTE busy, retry the access */
  39. if (unlikely(old_pte & _PAGE_BUSY))
  40. return 0;
  41. /* If PTE permissions don't match, take page fault */
  42. if (unlikely(access & ~old_pte))
  43. return 1;
  44. /* Try to lock the PTE, add ACCESSED and DIRTY if it was
  45. * a write access */
  46. new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED;
  47. if (access & _PAGE_RW)
  48. new_pte |= _PAGE_DIRTY;
  49. } while(old_pte != __cmpxchg_u64((unsigned long *)ptep,
  50. old_pte, new_pte));
  51. rflags = 0x2 | (!(new_pte & _PAGE_RW));
  52. /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
  53. rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
  54. sz = ((1UL) << shift);
  55. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  56. /* No CPU has hugepages but lacks no execute, so we
  57. * don't need to worry about that case */
  58. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  59. /* Check if pte already has an hpte (case 2) */
  60. if (unlikely(old_pte & _PAGE_HASHPTE)) {
  61. /* There MIGHT be an HPTE for this pte */
  62. unsigned long hash, slot;
  63. hash = hpt_hash(vpn, shift, ssize);
  64. if (old_pte & _PAGE_F_SECOND)
  65. hash = ~hash;
  66. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  67. slot += (old_pte & _PAGE_F_GIX) >> 12;
  68. if (ppc_md.hpte_updatepp(slot, rflags, vpn, mmu_psize,
  69. ssize, local) == -1)
  70. old_pte &= ~_PAGE_HPTEFLAGS;
  71. }
  72. if (likely(!(old_pte & _PAGE_HASHPTE))) {
  73. unsigned long hash = hpt_hash(vpn, shift, ssize);
  74. unsigned long hpte_group;
  75. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  76. repeat:
  77. hpte_group = ((hash & htab_hash_mask) *
  78. HPTES_PER_GROUP) & ~0x7UL;
  79. /* clear HPTE slot informations in new PTE */
  80. #ifdef CONFIG_PPC_64K_PAGES
  81. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HPTE_SUB0;
  82. #else
  83. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  84. #endif
  85. /* Add in WIMG bits */
  86. rflags |= (new_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
  87. _PAGE_COHERENT | _PAGE_GUARDED));
  88. /* Insert into the hash table, primary slot */
  89. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  90. mmu_psize, ssize);
  91. /* Primary is full, try the secondary */
  92. if (unlikely(slot == -1)) {
  93. hpte_group = ((~hash & htab_hash_mask) *
  94. HPTES_PER_GROUP) & ~0x7UL;
  95. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags,
  96. HPTE_V_SECONDARY,
  97. mmu_psize, ssize);
  98. if (slot == -1) {
  99. if (mftb() & 0x1)
  100. hpte_group = ((hash & htab_hash_mask) *
  101. HPTES_PER_GROUP)&~0x7UL;
  102. ppc_md.hpte_remove(hpte_group);
  103. goto repeat;
  104. }
  105. }
  106. /*
  107. * Hypervisor failure. Restore old pte and return -1
  108. * similar to __hash_page_*
  109. */
  110. if (unlikely(slot == -2)) {
  111. *ptep = __pte(old_pte);
  112. hash_failure_debug(ea, access, vsid, trap, ssize,
  113. mmu_psize, old_pte);
  114. return -1;
  115. }
  116. new_pte |= (slot << 12) & (_PAGE_F_SECOND | _PAGE_F_GIX);
  117. }
  118. /*
  119. * No need to use ldarx/stdcx here
  120. */
  121. *ptep = __pte(new_pte & ~_PAGE_BUSY);
  122. return 0;
  123. }