hash64_4k.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 & _PAGE_BUSY))
  34. return 0;
  35. /* If PTE permissions don't match, take page fault */
  36. if (unlikely(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 _PAGE_COMBO
  42. */
  43. new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED | _PAGE_HASHPTE;
  44. if (access & _PAGE_RW)
  45. new_pte |= _PAGE_DIRTY;
  46. } while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
  47. old_pte, new_pte));
  48. /*
  49. * PP bits. _PAGE_USER is already PP bit 0x2, so we only
  50. * need to add in 0x1 if it's a read-only user page
  51. */
  52. rflags = htab_convert_pte_flags(new_pte);
  53. if (!cpu_has_feature(CPU_FTR_NOEXECUTE) &&
  54. !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  55. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  56. vpn = hpt_vpn(ea, vsid, ssize);
  57. if (unlikely(old_pte & _PAGE_HASHPTE)) {
  58. /*
  59. * There MIGHT be an HPTE for this pte
  60. */
  61. hash = hpt_hash(vpn, shift, ssize);
  62. if (old_pte & _PAGE_F_SECOND)
  63. hash = ~hash;
  64. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  65. slot += (old_pte & _PAGE_F_GIX) >> _PAGE_F_GIX_SHIFT;
  66. if (ppc_md.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_4K,
  67. MMU_PAGE_4K, ssize, flags) == -1)
  68. old_pte &= ~_PAGE_HPTEFLAGS;
  69. }
  70. if (likely(!(old_pte & _PAGE_HASHPTE))) {
  71. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  72. hash = hpt_hash(vpn, shift, ssize);
  73. repeat:
  74. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  75. /* Insert into the hash table, primary slot */
  76. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  77. MMU_PAGE_4K, MMU_PAGE_4K, ssize);
  78. /*
  79. * Primary is full, try the secondary
  80. */
  81. if (unlikely(slot == -1)) {
  82. hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  83. slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
  84. rflags, HPTE_V_SECONDARY,
  85. MMU_PAGE_4K, MMU_PAGE_4K, ssize);
  86. if (slot == -1) {
  87. if (mftb() & 0x1)
  88. hpte_group = ((hash & htab_hash_mask) *
  89. HPTES_PER_GROUP) & ~0x7UL;
  90. ppc_md.hpte_remove(hpte_group);
  91. /*
  92. * FIXME!! Should be try the group from which we removed ?
  93. */
  94. goto repeat;
  95. }
  96. }
  97. /*
  98. * Hypervisor failure. Restore old pmd and return -1
  99. * similar to __hash_page_*
  100. */
  101. if (unlikely(slot == -2)) {
  102. *ptep = __pte(old_pte);
  103. hash_failure_debug(ea, access, vsid, trap, ssize,
  104. MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
  105. return -1;
  106. }
  107. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  108. new_pte |= (slot << _PAGE_F_GIX_SHIFT) & (_PAGE_F_SECOND | _PAGE_F_GIX);
  109. }
  110. *ptep = __pte(new_pte & ~_PAGE_BUSY);
  111. return 0;
  112. }