hugetlbpage-hash64.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. extern long hpte_insert_repeating(unsigned long hash, unsigned long vpn,
  16. unsigned long pa, unsigned long rlags,
  17. unsigned long vflags, int psize, int ssize);
  18. int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
  19. pte_t *ptep, unsigned long trap, unsigned long flags,
  20. int ssize, unsigned int shift, unsigned int mmu_psize)
  21. {
  22. unsigned long vpn;
  23. unsigned long old_pte, new_pte;
  24. unsigned long rflags, pa, sz;
  25. long slot;
  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. /* At this point, we have a pte (old_pte) which can be used to build
  30. * or update an HPTE. There are 2 cases:
  31. *
  32. * 1. There is a valid (present) pte with no associated HPTE (this is
  33. * the most common case)
  34. * 2. There is a valid (present) pte with an associated HPTE. The
  35. * current values of the pp bits in the HPTE prevent access
  36. * because we are doing software DIRTY bit management and the
  37. * page is currently not DIRTY.
  38. */
  39. do {
  40. old_pte = pte_val(*ptep);
  41. /* If PTE busy, retry the access */
  42. if (unlikely(old_pte & H_PAGE_BUSY))
  43. return 0;
  44. /* If PTE permissions don't match, take page fault */
  45. if (unlikely(!check_pte_access(access, old_pte)))
  46. return 1;
  47. /* Try to lock the PTE, add ACCESSED and DIRTY if it was
  48. * a write access */
  49. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
  50. if (access & _PAGE_WRITE)
  51. new_pte |= _PAGE_DIRTY;
  52. } while(!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  53. rflags = htab_convert_pte_flags(new_pte);
  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 & H_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 & H_PAGE_F_SECOND)
  65. hash = ~hash;
  66. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  67. slot += (old_pte & H_PAGE_F_GIX) >> H_PAGE_F_GIX_SHIFT;
  68. if (mmu_hash_ops.hpte_updatepp(slot, rflags, vpn, mmu_psize,
  69. mmu_psize, ssize, flags) == -1)
  70. old_pte &= ~_PAGE_HPTEFLAGS;
  71. }
  72. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  73. unsigned long hash = hpt_hash(vpn, shift, ssize);
  74. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  75. /* clear HPTE slot informations in new PTE */
  76. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
  77. slot = hpte_insert_repeating(hash, vpn, pa, rflags, 0,
  78. mmu_psize, ssize);
  79. /*
  80. * Hypervisor failure. Restore old pte and return -1
  81. * similar to __hash_page_*
  82. */
  83. if (unlikely(slot == -2)) {
  84. *ptep = __pte(old_pte);
  85. hash_failure_debug(ea, access, vsid, trap, ssize,
  86. mmu_psize, mmu_psize, old_pte);
  87. return -1;
  88. }
  89. new_pte |= (slot << H_PAGE_F_GIX_SHIFT) &
  90. (H_PAGE_F_SECOND | H_PAGE_F_GIX);
  91. }
  92. /*
  93. * No need to use ldarx/stdcx here
  94. */
  95. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  96. return 0;
  97. }
  98. #if defined(CONFIG_PPC_64K_PAGES) && defined(CONFIG_DEBUG_VM)
  99. /*
  100. * This enables us to catch the wrong page directory format
  101. * Moved here so that we can use WARN() in the call.
  102. */
  103. int hugepd_ok(hugepd_t hpd)
  104. {
  105. bool is_hugepd;
  106. /*
  107. * We should not find this format in page directory, warn otherwise.
  108. */
  109. is_hugepd = (((hpd.pd & 0x3) == 0x0) && ((hpd.pd & HUGEPD_SHIFT_MASK) != 0));
  110. WARN(is_hugepd, "Found wrong page directory format\n");
  111. return 0;
  112. }
  113. #endif