hash64_4k.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright IBM Corporation, 2015
  3. * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. */
  14. #include <linux/mm.h>
  15. #include <asm/machdep.h>
  16. #include <asm/mmu.h>
  17. int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
  18. pte_t *ptep, unsigned long trap, unsigned long flags,
  19. int ssize, int subpg_prot)
  20. {
  21. unsigned long hpte_group;
  22. unsigned long rflags, pa;
  23. unsigned long old_pte, new_pte;
  24. unsigned long vpn, hash, slot;
  25. unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
  26. /*
  27. * atomically mark the linux large page PTE busy and dirty
  28. */
  29. do {
  30. pte_t pte = READ_ONCE(*ptep);
  31. old_pte = pte_val(pte);
  32. /* If PTE busy, retry the access */
  33. if (unlikely(old_pte & H_PAGE_BUSY))
  34. return 0;
  35. /* If PTE permissions don't match, take page fault */
  36. if (unlikely(!check_pte_access(access, old_pte)))
  37. return 1;
  38. /*
  39. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  40. * a write access. Since this is 4K insert of 64K page size
  41. * also add H_PAGE_COMBO
  42. */
  43. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
  44. if (access & _PAGE_WRITE)
  45. new_pte |= _PAGE_DIRTY;
  46. } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  47. /*
  48. * PP bits. _PAGE_USER is already PP bit 0x2, so we only
  49. * need to add in 0x1 if it's a read-only user page
  50. */
  51. rflags = htab_convert_pte_flags(new_pte);
  52. if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
  53. !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  54. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  55. vpn = hpt_vpn(ea, vsid, ssize);
  56. if (unlikely(old_pte & H_PAGE_HASHPTE)) {
  57. /*
  58. * There MIGHT be an HPTE for this pte
  59. */
  60. hash = hpt_hash(vpn, shift, ssize);
  61. if (old_pte & H_PAGE_F_SECOND)
  62. hash = ~hash;
  63. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  64. slot += (old_pte & H_PAGE_F_GIX) >> H_PAGE_F_GIX_SHIFT;
  65. if (mmu_hash_ops.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_4K,
  66. MMU_PAGE_4K, ssize, flags) == -1)
  67. old_pte &= ~_PAGE_HPTEFLAGS;
  68. }
  69. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  70. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  71. hash = hpt_hash(vpn, shift, ssize);
  72. repeat:
  73. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  74. /* Insert into the hash table, primary slot */
  75. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  76. MMU_PAGE_4K, MMU_PAGE_4K, ssize);
  77. /*
  78. * Primary is full, try the secondary
  79. */
  80. if (unlikely(slot == -1)) {
  81. hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  82. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  83. rflags,
  84. HPTE_V_SECONDARY,
  85. MMU_PAGE_4K,
  86. MMU_PAGE_4K, ssize);
  87. if (slot == -1) {
  88. if (mftb() & 0x1)
  89. hpte_group = ((hash & htab_hash_mask) *
  90. HPTES_PER_GROUP) & ~0x7UL;
  91. mmu_hash_ops.hpte_remove(hpte_group);
  92. /*
  93. * FIXME!! Should be try the group from which we removed ?
  94. */
  95. goto repeat;
  96. }
  97. }
  98. /*
  99. * Hypervisor failure. Restore old pte and return -1
  100. * similar to __hash_page_*
  101. */
  102. if (unlikely(slot == -2)) {
  103. *ptep = __pte(old_pte);
  104. hash_failure_debug(ea, access, vsid, trap, ssize,
  105. MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
  106. return -1;
  107. }
  108. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
  109. new_pte |= (slot << H_PAGE_F_GIX_SHIFT) &
  110. (H_PAGE_F_SECOND | H_PAGE_F_GIX);
  111. }
  112. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  113. return 0;
  114. }