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> |
| 18 | |
| 19 | int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid, |
| 20 | pte_t *ptep, unsigned long trap, unsigned long flags, |
| 21 | int ssize, int subpg_prot) |
| 22 | { |
| 23 | real_pte_t rpte; |
| 24 | unsigned long *hidxp; |
| 25 | unsigned long hpte_group; |
| 26 | unsigned int subpg_index; |
| 27 | unsigned long rflags, pa, hidx; |
| 28 | unsigned long old_pte, new_pte, subpg_pte; |
| 29 | unsigned long vpn, hash, slot; |
| 30 | unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift; |
| 31 | |
| 32 | /* |
| 33 | * atomically mark the linux large page PTE busy and dirty |
| 34 | */ |
| 35 | do { |
| 36 | pte_t pte = READ_ONCE(*ptep); |
| 37 | |
| 38 | old_pte = pte_val(pte); |
| 39 | /* If PTE busy, retry the access */ |
| 40 | if (unlikely(old_pte & _PAGE_BUSY)) |
| 41 | return 0; |
| 42 | /* If PTE permissions don't match, take page fault */ |
| 43 | if (unlikely(access & ~old_pte)) |
| 44 | return 1; |
| 45 | /* |
| 46 | * Try to lock the PTE, add ACCESSED and DIRTY if it was |
| 47 | * a write access. Since this is 4K insert of 64K page size |
| 48 | * also add _PAGE_COMBO |
| 49 | */ |
| 50 | new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED | _PAGE_COMBO; |
| 51 | if (access & _PAGE_RW) |
| 52 | new_pte |= _PAGE_DIRTY; |
| 53 | } while (old_pte != __cmpxchg_u64((unsigned long *)ptep, |
| 54 | old_pte, new_pte)); |
| 55 | /* |
| 56 | * Handle the subpage protection bits |
| 57 | */ |
| 58 | subpg_pte = new_pte & ~subpg_prot; |
| 59 | /* |
| 60 | * PP bits. _PAGE_USER is already PP bit 0x2, so we only |
| 61 | * need to add in 0x1 if it's a read-only user page |
| 62 | */ |
| 63 | rflags = subpg_pte & _PAGE_USER; |
| 64 | if ((subpg_pte & _PAGE_USER) && !((subpg_pte & _PAGE_RW) && |
| 65 | (subpg_pte & _PAGE_DIRTY))) |
| 66 | rflags |= 0x1; |
| 67 | /* |
| 68 | * _PAGE_EXEC -> HW_NO_EXEC since it's inverted |
| 69 | */ |
| 70 | rflags |= ((subpg_pte & _PAGE_EXEC) ? 0 : HPTE_R_N); |
| 71 | /* |
| 72 | * Always add C and Memory coherence bit |
| 73 | */ |
| 74 | rflags |= HPTE_R_C | HPTE_R_M; |
| 75 | /* |
| 76 | * Add in WIMG bits |
| 77 | */ |
| 78 | rflags |= (subpg_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE | |
| 79 | _PAGE_COHERENT | _PAGE_GUARDED)); |
| 80 | |
| 81 | if (!cpu_has_feature(CPU_FTR_NOEXECUTE) && |
| 82 | !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) { |
| 83 | |
| 84 | /* |
| 85 | * No CPU has hugepages but lacks no execute, so we |
| 86 | * don't need to worry about that case |
| 87 | */ |
| 88 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); |
| 89 | } |
| 90 | |
| 91 | subpg_index = (ea & (PAGE_SIZE - 1)) >> shift; |
| 92 | vpn = hpt_vpn(ea, vsid, ssize); |
| 93 | rpte = __real_pte(__pte(old_pte), ptep); |
| 94 | /* |
| 95 | *None of the sub 4k page is hashed |
| 96 | */ |
| 97 | if (!(old_pte & _PAGE_HASHPTE)) |
| 98 | goto htab_insert_hpte; |
| 99 | /* |
| 100 | * Check if the pte was already inserted into the hash table |
| 101 | * as a 64k HW page, and invalidate the 64k HPTE if so. |
| 102 | */ |
| 103 | if (!(old_pte & _PAGE_COMBO)) { |
| 104 | flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags); |
| 105 | old_pte &= ~_PAGE_HPTE_SUB; |
| 106 | goto htab_insert_hpte; |
| 107 | } |
| 108 | /* |
| 109 | * Check for sub page valid and update |
| 110 | */ |
| 111 | if (__rpte_sub_valid(rpte, subpg_index)) { |
| 112 | int ret; |
| 113 | |
| 114 | hash = hpt_hash(vpn, shift, ssize); |
| 115 | hidx = __rpte_to_hidx(rpte, subpg_index); |
| 116 | if (hidx & _PTEIDX_SECONDARY) |
| 117 | hash = ~hash; |
| 118 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 119 | slot += hidx & _PTEIDX_GROUP_IX; |
| 120 | |
| 121 | ret = ppc_md.hpte_updatepp(slot, rflags, vpn, |
| 122 | MMU_PAGE_4K, MMU_PAGE_4K, |
| 123 | ssize, flags); |
| 124 | /* |
| 125 | *if we failed because typically the HPTE wasn't really here |
| 126 | * we try an insertion. |
| 127 | */ |
| 128 | if (ret == -1) |
| 129 | goto htab_insert_hpte; |
| 130 | |
| 131 | *ptep = __pte(new_pte & ~_PAGE_BUSY); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | htab_insert_hpte: |
| 136 | /* |
| 137 | * handle _PAGE_4K_PFN case |
| 138 | */ |
| 139 | if (old_pte & _PAGE_4K_PFN) { |
| 140 | /* |
| 141 | * All the sub 4k page have the same |
| 142 | * physical address. |
| 143 | */ |
| 144 | pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT; |
| 145 | } else { |
| 146 | pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT; |
| 147 | pa += (subpg_index << shift); |
| 148 | } |
| 149 | hash = hpt_hash(vpn, shift, ssize); |
| 150 | repeat: |
| 151 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 152 | |
| 153 | /* Insert into the hash table, primary slot */ |
| 154 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0, |
| 155 | MMU_PAGE_4K, MMU_PAGE_4K, ssize); |
| 156 | /* |
| 157 | * Primary is full, try the secondary |
| 158 | */ |
| 159 | if (unlikely(slot == -1)) { |
| 160 | hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
| 161 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, |
| 162 | rflags, HPTE_V_SECONDARY, |
| 163 | MMU_PAGE_4K, MMU_PAGE_4K, ssize); |
| 164 | if (slot == -1) { |
| 165 | if (mftb() & 0x1) |
| 166 | hpte_group = ((hash & htab_hash_mask) * |
| 167 | HPTES_PER_GROUP) & ~0x7UL; |
| 168 | ppc_md.hpte_remove(hpte_group); |
| 169 | /* |
| 170 | * FIXME!! Should be try the group from which we removed ? |
| 171 | */ |
| 172 | goto repeat; |
| 173 | } |
| 174 | } |
| 175 | /* |
| 176 | * Hypervisor failure. Restore old pmd and return -1 |
| 177 | * similar to __hash_page_* |
| 178 | */ |
| 179 | if (unlikely(slot == -2)) { |
| 180 | *ptep = __pte(old_pte); |
| 181 | hash_failure_debug(ea, access, vsid, trap, ssize, |
| 182 | MMU_PAGE_4K, MMU_PAGE_4K, old_pte); |
| 183 | return -1; |
| 184 | } |
| 185 | /* |
| 186 | * Insert slot number & secondary bit in PTE second half, |
| 187 | * clear _PAGE_BUSY and set appropriate HPTE slot bit |
| 188 | * Since we have _PAGE_BUSY set on ptep, we can be sure |
| 189 | * nobody is undating hidx. |
| 190 | */ |
| 191 | hidxp = (unsigned long *)(ptep + PTRS_PER_PTE); |
| 192 | /* __real_pte use pte_val() any idea why ? FIXME!! */ |
| 193 | rpte.hidx &= ~(0xfUL << (subpg_index << 2)); |
| 194 | *hidxp = rpte.hidx | (slot << (subpg_index << 2)); |
| 195 | new_pte |= (_PAGE_HPTE_SUB0 >> subpg_index); |
| 196 | /* |
| 197 | * check __real_pte for details on matching smp_rmb() |
| 198 | */ |
| 199 | smp_wmb(); |
| 200 | *ptep = __pte(new_pte & ~_PAGE_BUSY); |
| 201 | return 0; |
| 202 | } |