hash_4k.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright IBM Corporation, 2015
  3. * Author Aneesh Kumar K.V <[email protected]>
  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. real_pte_t rpte;
  22. unsigned long hpte_group;
  23. unsigned long rflags, pa;
  24. unsigned long old_pte, new_pte;
  25. unsigned long vpn, hash, slot;
  26. unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
  27. /*
  28. * atomically mark the linux large page PTE busy and dirty
  29. */
  30. do {
  31. pte_t pte = READ_ONCE(*ptep);
  32. old_pte = pte_val(pte);
  33. /* If PTE busy, retry the access */
  34. if (unlikely(old_pte & H_PAGE_BUSY))
  35. return 0;
  36. /* If PTE permissions don't match, take page fault */
  37. if (unlikely(!check_pte_access(access, old_pte)))
  38. return 1;
  39. /*
  40. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  41. * a write access. Since this is 4K insert of 64K page size
  42. * also add H_PAGE_COMBO
  43. */
  44. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
  45. if (access & _PAGE_WRITE)
  46. new_pte |= _PAGE_DIRTY;
  47. } while (!pte_xchg(ptep, __pte(old_pte), __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, flags);
  53. rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
  54. if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
  55. !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  56. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  57. vpn = hpt_vpn(ea, vsid, ssize);
  58. if (unlikely(old_pte & H_PAGE_HASHPTE)) {
  59. /*
  60. * There MIGHT be an HPTE for this pte
  61. */
  62. unsigned long gslot = pte_get_hash_gslot(vpn, shift, ssize,
  63. rpte, 0);
  64. if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_4K,
  65. MMU_PAGE_4K, ssize, flags) == -1)
  66. old_pte &= ~_PAGE_HPTEFLAGS;
  67. }
  68. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  69. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  70. hash = hpt_hash(vpn, shift, ssize);
  71. repeat:
  72. hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  73. /* Insert into the hash table, primary slot */
  74. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  75. MMU_PAGE_4K, MMU_PAGE_4K, ssize);
  76. /*
  77. * Primary is full, try the secondary
  78. */
  79. if (unlikely(slot == -1)) {
  80. hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
  81. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  82. rflags,
  83. HPTE_V_SECONDARY,
  84. MMU_PAGE_4K,
  85. MMU_PAGE_4K, ssize);
  86. if (slot == -1) {
  87. if (mftb() & 0x1)
  88. hpte_group = (hash & htab_hash_mask) *
  89. HPTES_PER_GROUP;
  90. mmu_hash_ops.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 pte 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) | H_PAGE_HASHPTE;
  108. new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
  109. }
  110. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  111. return 0;
  112. }