Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 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 | |
| 15 | #include <linux/mm.h> |
| 16 | #include <asm/machdep.h> |
| 17 | #include <asm/mmu.h> |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Return true, if the entry has a slot value which |
| 21 | * the software considers as invalid. |
| 22 | */ |
| 23 | static inline bool hpte_soft_invalid(unsigned long hidx) |
| 24 | { |
| 25 | return ((hidx & 0xfUL) == 0xfUL); |
| 26 | } |
| 27 | |
Aneesh Kumar K.V | bf680d5 | 2015-12-01 09:06:45 +0530 | [diff] [blame] | 28 | /* |
| 29 | * index from 0 - 15 |
| 30 | */ |
| 31 | bool __rpte_sub_valid(real_pte_t rpte, unsigned long index) |
| 32 | { |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 33 | return !(hpte_soft_invalid(__rpte_to_hidx(rpte, index))); |
Aneesh Kumar K.V | bf680d5 | 2015-12-01 09:06:45 +0530 | [diff] [blame] | 34 | } |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 35 | |
| 36 | int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, |
| 37 | pte_t *ptep, unsigned long trap, unsigned long flags, |
| 38 | int ssize, int subpg_prot) |
| 39 | { |
| 40 | real_pte_t rpte; |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 41 | unsigned long hpte_group; |
| 42 | unsigned int subpg_index; |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 43 | unsigned long rflags, pa; |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 44 | unsigned long old_pte, new_pte, subpg_pte; |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 45 | unsigned long vpn, hash, slot, gslot; |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 46 | unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift; |
| 47 | |
| 48 | /* |
| 49 | * atomically mark the linux large page PTE busy and dirty |
| 50 | */ |
| 51 | do { |
| 52 | pte_t pte = READ_ONCE(*ptep); |
| 53 | |
| 54 | old_pte = pte_val(pte); |
| 55 | /* If PTE busy, retry the access */ |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 56 | if (unlikely(old_pte & H_PAGE_BUSY)) |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 57 | return 0; |
| 58 | /* If PTE permissions don't match, take page fault */ |
Aneesh Kumar K.V | ac29c64 | 2016-04-29 23:25:34 +1000 | [diff] [blame] | 59 | if (unlikely(!check_pte_access(access, old_pte))) |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 60 | return 1; |
| 61 | /* |
| 62 | * Try to lock the PTE, add ACCESSED and DIRTY if it was |
| 63 | * a write access. Since this is 4K insert of 64K page size |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 64 | * also add H_PAGE_COMBO |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 65 | */ |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 66 | new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED | H_PAGE_COMBO; |
Aneesh Kumar K.V | c7d5484 | 2016-04-29 23:25:30 +1000 | [diff] [blame] | 67 | if (access & _PAGE_WRITE) |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 68 | new_pte |= _PAGE_DIRTY; |
Michael Ellerman | 3910a7f | 2016-04-29 23:25:27 +1000 | [diff] [blame] | 69 | } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte))); |
| 70 | |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 71 | /* |
| 72 | * Handle the subpage protection bits |
| 73 | */ |
| 74 | subpg_pte = new_pte & ~subpg_prot; |
Aneesh Kumar K.V | c6a3c49 | 2015-12-01 09:06:50 +0530 | [diff] [blame] | 75 | rflags = htab_convert_pte_flags(subpg_pte); |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 76 | |
Benjamin Herrenschmidt | dd7b2f0 | 2016-11-29 13:13:46 +1100 | [diff] [blame] | 77 | if (cpu_has_feature(CPU_FTR_NOEXECUTE) && |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 78 | !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) { |
| 79 | |
| 80 | /* |
| 81 | * No CPU has hugepages but lacks no execute, so we |
| 82 | * don't need to worry about that case |
| 83 | */ |
| 84 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); |
| 85 | } |
| 86 | |
| 87 | subpg_index = (ea & (PAGE_SIZE - 1)) >> shift; |
| 88 | vpn = hpt_vpn(ea, vsid, ssize); |
Aneesh Kumar K.V | ff31e10 | 2018-02-11 20:30:08 +0530 | [diff] [blame] | 89 | rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE); |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 90 | /* |
| 91 | *None of the sub 4k page is hashed |
| 92 | */ |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 93 | if (!(old_pte & H_PAGE_HASHPTE)) |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 94 | goto htab_insert_hpte; |
| 95 | /* |
| 96 | * Check if the pte was already inserted into the hash table |
| 97 | * as a 64k HW page, and invalidate the 64k HPTE if so. |
| 98 | */ |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 99 | if (!(old_pte & H_PAGE_COMBO)) { |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 100 | flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags); |
Aneesh Kumar K.V | 9ab3ac2 | 2016-02-20 20:41:54 +0530 | [diff] [blame] | 101 | /* |
| 102 | * clear the old slot details from the old and new pte. |
| 103 | * On hash insert failure we use old pte value and we don't |
| 104 | * want slot information there if we have a insert failure. |
| 105 | */ |
Ram Pai | bf9a95f | 2017-11-06 00:50:48 -0800 | [diff] [blame] | 106 | old_pte &= ~H_PAGE_HASHPTE; |
| 107 | new_pte &= ~H_PAGE_HASHPTE; |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 108 | goto htab_insert_hpte; |
| 109 | } |
| 110 | /* |
| 111 | * Check for sub page valid and update |
| 112 | */ |
| 113 | if (__rpte_sub_valid(rpte, subpg_index)) { |
| 114 | int ret; |
| 115 | |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 116 | gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, |
| 117 | subpg_index); |
| 118 | ret = mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 119 | MMU_PAGE_4K, MMU_PAGE_4K, |
| 120 | ssize, flags); |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 121 | |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 122 | /* |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 123 | * If we failed because typically the HPTE wasn't really here |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 124 | * we try an insertion. |
| 125 | */ |
| 126 | if (ret == -1) |
| 127 | goto htab_insert_hpte; |
| 128 | |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 129 | *ptep = __pte(new_pte & ~H_PAGE_BUSY); |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | htab_insert_hpte: |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Initialize all hidx entries to invalid value, the first time |
| 137 | * the PTE is about to allocate a 4K HPTE. |
| 138 | */ |
| 139 | if (!(old_pte & H_PAGE_COMBO)) |
| 140 | rpte.hidx = INVALID_RPTE_HIDX; |
| 141 | |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 142 | /* |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 143 | * handle H_PAGE_4K_PFN case |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 144 | */ |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 145 | if (old_pte & H_PAGE_4K_PFN) { |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 146 | /* |
| 147 | * All the sub 4k page have the same |
| 148 | * physical address. |
| 149 | */ |
| 150 | pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT; |
| 151 | } else { |
| 152 | pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; |
| 153 | pa += (subpg_index << shift); |
| 154 | } |
| 155 | hash = hpt_hash(vpn, shift, ssize); |
| 156 | repeat: |
| 157 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 158 | |
| 159 | /* Insert into the hash table, primary slot */ |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 160 | slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0, |
| 161 | MMU_PAGE_4K, MMU_PAGE_4K, ssize); |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 162 | /* |
| 163 | * Primary is full, try the secondary |
| 164 | */ |
| 165 | if (unlikely(slot == -1)) { |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 166 | bool soft_invalid; |
| 167 | |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 168 | hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 169 | slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, |
| 170 | rflags, HPTE_V_SECONDARY, |
| 171 | MMU_PAGE_4K, MMU_PAGE_4K, |
| 172 | ssize); |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 173 | |
| 174 | soft_invalid = hpte_soft_invalid(slot); |
| 175 | if (unlikely(soft_invalid)) { |
| 176 | /* |
| 177 | * We got a valid slot from a hardware point of view. |
| 178 | * but we cannot use it, because we use this special |
| 179 | * value; as defined by hpte_soft_invalid(), to track |
| 180 | * invalid slots. We cannot use it. So invalidate it. |
| 181 | */ |
| 182 | gslot = slot & _PTEIDX_GROUP_IX; |
| 183 | mmu_hash_ops.hpte_invalidate(hpte_group + gslot, vpn, |
| 184 | MMU_PAGE_4K, MMU_PAGE_4K, |
| 185 | ssize, 0); |
| 186 | } |
| 187 | |
| 188 | if (unlikely(slot == -1 || soft_invalid)) { |
| 189 | /* |
| 190 | * For soft invalid slot, let's ensure that we release a |
| 191 | * slot from the primary, with the hope that we will |
| 192 | * acquire that slot next time we try. This will ensure |
| 193 | * that we do not get the same soft-invalid slot. |
| 194 | */ |
| 195 | if (soft_invalid || (mftb() & 0x1)) |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 196 | hpte_group = ((hash & htab_hash_mask) * |
| 197 | HPTES_PER_GROUP) & ~0x7UL; |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 198 | |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 199 | mmu_hash_ops.hpte_remove(hpte_group); |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 200 | /* |
| 201 | * FIXME!! Should be try the group from which we removed ? |
| 202 | */ |
| 203 | goto repeat; |
| 204 | } |
| 205 | } |
| 206 | /* |
Aneesh Kumar K.V | e9a6814 | 2016-03-01 12:59:17 +0530 | [diff] [blame] | 207 | * Hypervisor failure. Restore old pte and return -1 |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 208 | * similar to __hash_page_* |
| 209 | */ |
| 210 | if (unlikely(slot == -2)) { |
| 211 | *ptep = __pte(old_pte); |
| 212 | hash_failure_debug(ea, access, vsid, trap, ssize, |
| 213 | MMU_PAGE_4K, MMU_PAGE_4K, old_pte); |
| 214 | return -1; |
| 215 | } |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 216 | |
Aneesh Kumar K.V | ff31e10 | 2018-02-11 20:30:08 +0530 | [diff] [blame] | 217 | new_pte |= pte_set_hidx(ptep, rpte, subpg_index, slot, PTRS_PER_PTE); |
Ram Pai | 9d2edb1 | 2017-11-06 00:50:47 -0800 | [diff] [blame] | 218 | new_pte |= H_PAGE_HASHPTE; |
| 219 | |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 220 | *ptep = __pte(new_pte & ~H_PAGE_BUSY); |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 221 | return 0; |
| 222 | } |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 223 | |
| 224 | int __hash_page_64K(unsigned long ea, unsigned long access, |
| 225 | unsigned long vsid, pte_t *ptep, unsigned long trap, |
| 226 | unsigned long flags, int ssize) |
| 227 | { |
Ram Pai | bf9a95f | 2017-11-06 00:50:48 -0800 | [diff] [blame] | 228 | real_pte_t rpte; |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 229 | unsigned long hpte_group; |
| 230 | unsigned long rflags, pa; |
| 231 | unsigned long old_pte, new_pte; |
| 232 | unsigned long vpn, hash, slot; |
| 233 | unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift; |
| 234 | |
| 235 | /* |
| 236 | * atomically mark the linux large page PTE busy and dirty |
| 237 | */ |
| 238 | do { |
| 239 | pte_t pte = READ_ONCE(*ptep); |
| 240 | |
| 241 | old_pte = pte_val(pte); |
| 242 | /* If PTE busy, retry the access */ |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 243 | if (unlikely(old_pte & H_PAGE_BUSY)) |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 244 | return 0; |
| 245 | /* If PTE permissions don't match, take page fault */ |
Aneesh Kumar K.V | ac29c64 | 2016-04-29 23:25:34 +1000 | [diff] [blame] | 246 | if (unlikely(!check_pte_access(access, old_pte))) |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 247 | return 1; |
| 248 | /* |
| 249 | * Check if PTE has the cache-inhibit bit set |
| 250 | * If so, bail out and refault as a 4k page |
| 251 | */ |
| 252 | if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) && |
Aneesh Kumar K.V | 30bda41 | 2016-04-29 23:25:38 +1000 | [diff] [blame] | 253 | unlikely(pte_ci(pte))) |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 254 | return 0; |
| 255 | /* |
| 256 | * Try to lock the PTE, add ACCESSED and DIRTY if it was |
Paul Mackerras | 1ec3f93 | 2016-02-22 13:41:12 +1100 | [diff] [blame] | 257 | * a write access. |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 258 | */ |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 259 | new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED; |
Aneesh Kumar K.V | c7d5484 | 2016-04-29 23:25:30 +1000 | [diff] [blame] | 260 | if (access & _PAGE_WRITE) |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 261 | new_pte |= _PAGE_DIRTY; |
Michael Ellerman | 3910a7f | 2016-04-29 23:25:27 +1000 | [diff] [blame] | 262 | } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte))); |
Aneesh Kumar K.V | c6a3c49 | 2015-12-01 09:06:50 +0530 | [diff] [blame] | 263 | |
| 264 | rflags = htab_convert_pte_flags(new_pte); |
Aneesh Kumar K.V | ff31e10 | 2018-02-11 20:30:08 +0530 | [diff] [blame] | 265 | rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE); |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 266 | |
Benjamin Herrenschmidt | dd7b2f0 | 2016-11-29 13:13:46 +1100 | [diff] [blame] | 267 | if (cpu_has_feature(CPU_FTR_NOEXECUTE) && |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 268 | !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) |
| 269 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); |
| 270 | |
| 271 | vpn = hpt_vpn(ea, vsid, ssize); |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 272 | if (unlikely(old_pte & H_PAGE_HASHPTE)) { |
Ram Pai | bf9a95f | 2017-11-06 00:50:48 -0800 | [diff] [blame] | 273 | unsigned long gslot; |
| 274 | |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 275 | /* |
| 276 | * There MIGHT be an HPTE for this pte |
| 277 | */ |
Ram Pai | bf9a95f | 2017-11-06 00:50:48 -0800 | [diff] [blame] | 278 | gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0); |
| 279 | if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_64K, |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 280 | MMU_PAGE_64K, ssize, |
| 281 | flags) == -1) |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 282 | old_pte &= ~_PAGE_HPTEFLAGS; |
| 283 | } |
| 284 | |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 285 | if (likely(!(old_pte & H_PAGE_HASHPTE))) { |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 286 | |
| 287 | pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; |
| 288 | hash = hpt_hash(vpn, shift, ssize); |
| 289 | |
| 290 | repeat: |
| 291 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 292 | |
| 293 | /* Insert into the hash table, primary slot */ |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 294 | slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0, |
| 295 | MMU_PAGE_64K, MMU_PAGE_64K, |
| 296 | ssize); |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 297 | /* |
| 298 | * Primary is full, try the secondary |
| 299 | */ |
| 300 | if (unlikely(slot == -1)) { |
| 301 | hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 302 | slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, |
| 303 | rflags, |
| 304 | HPTE_V_SECONDARY, |
| 305 | MMU_PAGE_64K, |
| 306 | MMU_PAGE_64K, ssize); |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 307 | if (slot == -1) { |
| 308 | if (mftb() & 0x1) |
| 309 | hpte_group = ((hash & htab_hash_mask) * |
| 310 | HPTES_PER_GROUP) & ~0x7UL; |
Benjamin Herrenschmidt | 7025776 | 2016-07-05 15:03:58 +1000 | [diff] [blame] | 311 | mmu_hash_ops.hpte_remove(hpte_group); |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 312 | /* |
| 313 | * FIXME!! Should be try the group from which we removed ? |
| 314 | */ |
| 315 | goto repeat; |
| 316 | } |
| 317 | } |
| 318 | /* |
Aneesh Kumar K.V | e9a6814 | 2016-03-01 12:59:17 +0530 | [diff] [blame] | 319 | * Hypervisor failure. Restore old pte and return -1 |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 320 | * similar to __hash_page_* |
| 321 | */ |
| 322 | if (unlikely(slot == -2)) { |
| 323 | *ptep = __pte(old_pte); |
| 324 | hash_failure_debug(ea, access, vsid, trap, ssize, |
| 325 | MMU_PAGE_64K, MMU_PAGE_64K, old_pte); |
| 326 | return -1; |
| 327 | } |
Ram Pai | bf9a95f | 2017-11-06 00:50:48 -0800 | [diff] [blame] | 328 | |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 329 | new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE; |
Aneesh Kumar K.V | ff31e10 | 2018-02-11 20:30:08 +0530 | [diff] [blame] | 330 | new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE); |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 331 | } |
Aneesh Kumar K.V | 945537d | 2016-04-29 23:25:45 +1000 | [diff] [blame] | 332 | *ptep = __pte(new_pte & ~H_PAGE_BUSY); |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 333 | return 0; |
| 334 | } |