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> |
Aneesh Kumar K.V | bf680d5 | 2015-12-01 09:06:45 +0530 | [diff] [blame] | 18 | /* |
| 19 | * index from 0 - 15 |
| 20 | */ |
| 21 | bool __rpte_sub_valid(real_pte_t rpte, unsigned long index) |
| 22 | { |
| 23 | unsigned long g_idx; |
| 24 | unsigned long ptev = pte_val(rpte.pte); |
| 25 | |
| 26 | g_idx = (ptev & _PAGE_COMBO_VALID) >> _PAGE_F_GIX_SHIFT; |
| 27 | index = index >> 2; |
| 28 | if (g_idx & (0x1 << index)) |
| 29 | return true; |
| 30 | else |
| 31 | return false; |
| 32 | } |
| 33 | /* |
| 34 | * index from 0 - 15 |
| 35 | */ |
| 36 | static unsigned long mark_subptegroup_valid(unsigned long ptev, unsigned long index) |
| 37 | { |
| 38 | unsigned long g_idx; |
| 39 | |
| 40 | if (!(ptev & _PAGE_COMBO)) |
| 41 | return ptev; |
| 42 | index = index >> 2; |
| 43 | g_idx = 0x1 << index; |
| 44 | |
| 45 | return ptev | (g_idx << _PAGE_F_GIX_SHIFT); |
| 46 | } |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 47 | |
| 48 | int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, |
| 49 | pte_t *ptep, unsigned long trap, unsigned long flags, |
| 50 | int ssize, int subpg_prot) |
| 51 | { |
| 52 | real_pte_t rpte; |
| 53 | unsigned long *hidxp; |
| 54 | unsigned long hpte_group; |
| 55 | unsigned int subpg_index; |
| 56 | unsigned long rflags, pa, hidx; |
| 57 | unsigned long old_pte, new_pte, subpg_pte; |
| 58 | unsigned long vpn, hash, slot; |
| 59 | unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift; |
| 60 | |
| 61 | /* |
| 62 | * atomically mark the linux large page PTE busy and dirty |
| 63 | */ |
| 64 | do { |
| 65 | pte_t pte = READ_ONCE(*ptep); |
| 66 | |
| 67 | old_pte = pte_val(pte); |
| 68 | /* If PTE busy, retry the access */ |
| 69 | if (unlikely(old_pte & _PAGE_BUSY)) |
| 70 | return 0; |
| 71 | /* If PTE permissions don't match, take page fault */ |
| 72 | if (unlikely(access & ~old_pte)) |
| 73 | return 1; |
| 74 | /* |
| 75 | * Try to lock the PTE, add ACCESSED and DIRTY if it was |
| 76 | * a write access. Since this is 4K insert of 64K page size |
| 77 | * also add _PAGE_COMBO |
| 78 | */ |
| 79 | new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED | _PAGE_COMBO; |
| 80 | if (access & _PAGE_RW) |
| 81 | new_pte |= _PAGE_DIRTY; |
| 82 | } while (old_pte != __cmpxchg_u64((unsigned long *)ptep, |
| 83 | old_pte, new_pte)); |
| 84 | /* |
| 85 | * Handle the subpage protection bits |
| 86 | */ |
| 87 | subpg_pte = new_pte & ~subpg_prot; |
Aneesh Kumar K.V | c6a3c49 | 2015-12-01 09:06:50 +0530 | [diff] [blame] | 88 | rflags = htab_convert_pte_flags(subpg_pte); |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 89 | |
| 90 | if (!cpu_has_feature(CPU_FTR_NOEXECUTE) && |
| 91 | !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) { |
| 92 | |
| 93 | /* |
| 94 | * No CPU has hugepages but lacks no execute, so we |
| 95 | * don't need to worry about that case |
| 96 | */ |
| 97 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); |
| 98 | } |
| 99 | |
| 100 | subpg_index = (ea & (PAGE_SIZE - 1)) >> shift; |
| 101 | vpn = hpt_vpn(ea, vsid, ssize); |
| 102 | rpte = __real_pte(__pte(old_pte), ptep); |
| 103 | /* |
| 104 | *None of the sub 4k page is hashed |
| 105 | */ |
| 106 | if (!(old_pte & _PAGE_HASHPTE)) |
| 107 | goto htab_insert_hpte; |
| 108 | /* |
| 109 | * Check if the pte was already inserted into the hash table |
| 110 | * as a 64k HW page, and invalidate the 64k HPTE if so. |
| 111 | */ |
| 112 | if (!(old_pte & _PAGE_COMBO)) { |
| 113 | flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags); |
Aneesh Kumar K.V | bf680d5 | 2015-12-01 09:06:45 +0530 | [diff] [blame] | 114 | old_pte &= ~_PAGE_HASHPTE | _PAGE_F_GIX | _PAGE_F_SECOND; |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 115 | goto htab_insert_hpte; |
| 116 | } |
| 117 | /* |
| 118 | * Check for sub page valid and update |
| 119 | */ |
| 120 | if (__rpte_sub_valid(rpte, subpg_index)) { |
| 121 | int ret; |
| 122 | |
| 123 | hash = hpt_hash(vpn, shift, ssize); |
| 124 | hidx = __rpte_to_hidx(rpte, subpg_index); |
| 125 | if (hidx & _PTEIDX_SECONDARY) |
| 126 | hash = ~hash; |
| 127 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 128 | slot += hidx & _PTEIDX_GROUP_IX; |
| 129 | |
| 130 | ret = ppc_md.hpte_updatepp(slot, rflags, vpn, |
| 131 | MMU_PAGE_4K, MMU_PAGE_4K, |
| 132 | ssize, flags); |
| 133 | /* |
| 134 | *if we failed because typically the HPTE wasn't really here |
| 135 | * we try an insertion. |
| 136 | */ |
| 137 | if (ret == -1) |
| 138 | goto htab_insert_hpte; |
| 139 | |
| 140 | *ptep = __pte(new_pte & ~_PAGE_BUSY); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | htab_insert_hpte: |
| 145 | /* |
| 146 | * handle _PAGE_4K_PFN case |
| 147 | */ |
| 148 | if (old_pte & _PAGE_4K_PFN) { |
| 149 | /* |
| 150 | * All the sub 4k page have the same |
| 151 | * physical address. |
| 152 | */ |
| 153 | pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT; |
| 154 | } else { |
| 155 | pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; |
| 156 | pa += (subpg_index << shift); |
| 157 | } |
| 158 | hash = hpt_hash(vpn, shift, ssize); |
| 159 | repeat: |
| 160 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 161 | |
| 162 | /* Insert into the hash table, primary slot */ |
| 163 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0, |
| 164 | MMU_PAGE_4K, MMU_PAGE_4K, ssize); |
| 165 | /* |
| 166 | * Primary is full, try the secondary |
| 167 | */ |
| 168 | if (unlikely(slot == -1)) { |
| 169 | hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 170 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, |
| 171 | rflags, HPTE_V_SECONDARY, |
| 172 | MMU_PAGE_4K, MMU_PAGE_4K, ssize); |
| 173 | if (slot == -1) { |
| 174 | if (mftb() & 0x1) |
| 175 | hpte_group = ((hash & htab_hash_mask) * |
| 176 | HPTES_PER_GROUP) & ~0x7UL; |
| 177 | ppc_md.hpte_remove(hpte_group); |
| 178 | /* |
| 179 | * FIXME!! Should be try the group from which we removed ? |
| 180 | */ |
| 181 | goto repeat; |
| 182 | } |
| 183 | } |
| 184 | /* |
| 185 | * Hypervisor failure. Restore old pmd and return -1 |
| 186 | * similar to __hash_page_* |
| 187 | */ |
| 188 | if (unlikely(slot == -2)) { |
| 189 | *ptep = __pte(old_pte); |
| 190 | hash_failure_debug(ea, access, vsid, trap, ssize, |
| 191 | MMU_PAGE_4K, MMU_PAGE_4K, old_pte); |
| 192 | return -1; |
| 193 | } |
| 194 | /* |
| 195 | * Insert slot number & secondary bit in PTE second half, |
| 196 | * clear _PAGE_BUSY and set appropriate HPTE slot bit |
| 197 | * Since we have _PAGE_BUSY set on ptep, we can be sure |
| 198 | * nobody is undating hidx. |
| 199 | */ |
| 200 | hidxp = (unsigned long *)(ptep + PTRS_PER_PTE); |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 201 | rpte.hidx &= ~(0xfUL << (subpg_index << 2)); |
| 202 | *hidxp = rpte.hidx | (slot << (subpg_index << 2)); |
Aneesh Kumar K.V | bf680d5 | 2015-12-01 09:06:45 +0530 | [diff] [blame] | 203 | new_pte = mark_subptegroup_valid(new_pte, subpg_index); |
| 204 | new_pte |= _PAGE_HASHPTE; |
Aneesh Kumar K.V | 91f1da9 | 2015-12-01 09:06:43 +0530 | [diff] [blame] | 205 | /* |
| 206 | * check __real_pte for details on matching smp_rmb() |
| 207 | */ |
| 208 | smp_wmb(); |
| 209 | *ptep = __pte(new_pte & ~_PAGE_BUSY); |
| 210 | return 0; |
| 211 | } |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 212 | |
| 213 | int __hash_page_64K(unsigned long ea, unsigned long access, |
| 214 | unsigned long vsid, pte_t *ptep, unsigned long trap, |
| 215 | unsigned long flags, int ssize) |
| 216 | { |
| 217 | |
| 218 | unsigned long hpte_group; |
| 219 | unsigned long rflags, pa; |
| 220 | unsigned long old_pte, new_pte; |
| 221 | unsigned long vpn, hash, slot; |
| 222 | unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift; |
| 223 | |
| 224 | /* |
| 225 | * atomically mark the linux large page PTE busy and dirty |
| 226 | */ |
| 227 | do { |
| 228 | pte_t pte = READ_ONCE(*ptep); |
| 229 | |
| 230 | old_pte = pte_val(pte); |
| 231 | /* If PTE busy, retry the access */ |
| 232 | if (unlikely(old_pte & _PAGE_BUSY)) |
| 233 | return 0; |
| 234 | /* If PTE permissions don't match, take page fault */ |
| 235 | if (unlikely(access & ~old_pte)) |
| 236 | return 1; |
| 237 | /* |
| 238 | * Check if PTE has the cache-inhibit bit set |
| 239 | * If so, bail out and refault as a 4k page |
| 240 | */ |
| 241 | if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) && |
| 242 | unlikely(old_pte & _PAGE_NO_CACHE)) |
| 243 | return 0; |
| 244 | /* |
| 245 | * Try to lock the PTE, add ACCESSED and DIRTY if it was |
| 246 | * a write access. Since this is 4K insert of 64K page size |
| 247 | * also add _PAGE_COMBO |
| 248 | */ |
| 249 | new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED; |
| 250 | if (access & _PAGE_RW) |
| 251 | new_pte |= _PAGE_DIRTY; |
| 252 | } while (old_pte != __cmpxchg_u64((unsigned long *)ptep, |
| 253 | old_pte, new_pte)); |
Aneesh Kumar K.V | c6a3c49 | 2015-12-01 09:06:50 +0530 | [diff] [blame] | 254 | |
| 255 | rflags = htab_convert_pte_flags(new_pte); |
Aneesh Kumar K.V | 89ff725 | 2015-12-01 09:06:48 +0530 | [diff] [blame] | 256 | |
| 257 | if (!cpu_has_feature(CPU_FTR_NOEXECUTE) && |
| 258 | !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) |
| 259 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); |
| 260 | |
| 261 | vpn = hpt_vpn(ea, vsid, ssize); |
| 262 | if (unlikely(old_pte & _PAGE_HASHPTE)) { |
| 263 | /* |
| 264 | * There MIGHT be an HPTE for this pte |
| 265 | */ |
| 266 | hash = hpt_hash(vpn, shift, ssize); |
| 267 | if (old_pte & _PAGE_F_SECOND) |
| 268 | hash = ~hash; |
| 269 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 270 | slot += (old_pte & _PAGE_F_GIX) >> _PAGE_F_GIX_SHIFT; |
| 271 | |
| 272 | if (ppc_md.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_64K, |
| 273 | MMU_PAGE_64K, ssize, flags) == -1) |
| 274 | old_pte &= ~_PAGE_HPTEFLAGS; |
| 275 | } |
| 276 | |
| 277 | if (likely(!(old_pte & _PAGE_HASHPTE))) { |
| 278 | |
| 279 | pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; |
| 280 | hash = hpt_hash(vpn, shift, ssize); |
| 281 | |
| 282 | repeat: |
| 283 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 284 | |
| 285 | /* Insert into the hash table, primary slot */ |
| 286 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0, |
| 287 | MMU_PAGE_64K, MMU_PAGE_64K, ssize); |
| 288 | /* |
| 289 | * Primary is full, try the secondary |
| 290 | */ |
| 291 | if (unlikely(slot == -1)) { |
| 292 | hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 293 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, |
| 294 | rflags, HPTE_V_SECONDARY, |
| 295 | MMU_PAGE_64K, MMU_PAGE_64K, ssize); |
| 296 | if (slot == -1) { |
| 297 | if (mftb() & 0x1) |
| 298 | hpte_group = ((hash & htab_hash_mask) * |
| 299 | HPTES_PER_GROUP) & ~0x7UL; |
| 300 | ppc_md.hpte_remove(hpte_group); |
| 301 | /* |
| 302 | * FIXME!! Should be try the group from which we removed ? |
| 303 | */ |
| 304 | goto repeat; |
| 305 | } |
| 306 | } |
| 307 | /* |
| 308 | * Hypervisor failure. Restore old pmd and return -1 |
| 309 | * similar to __hash_page_* |
| 310 | */ |
| 311 | if (unlikely(slot == -2)) { |
| 312 | *ptep = __pte(old_pte); |
| 313 | hash_failure_debug(ea, access, vsid, trap, ssize, |
| 314 | MMU_PAGE_64K, MMU_PAGE_64K, old_pte); |
| 315 | return -1; |
| 316 | } |
| 317 | new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE; |
| 318 | new_pte |= (slot << _PAGE_F_GIX_SHIFT) & (_PAGE_F_SECOND | _PAGE_F_GIX); |
| 319 | } |
| 320 | *ptep = __pte(new_pte & ~_PAGE_BUSY); |
| 321 | return 0; |
| 322 | } |