Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright IBM Corporation, 2013 |
| 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.1 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 | /* |
| 16 | * PPC64 THP Support for hash based MMUs |
| 17 | */ |
| 18 | #include <linux/mm.h> |
| 19 | #include <asm/machdep.h> |
| 20 | |
| 21 | int __hash_page_thp(unsigned long ea, unsigned long access, unsigned long vsid, |
Aneesh Kumar K.V | aefa568 | 2014-12-04 11:00:14 +0530 | [diff] [blame] | 22 | pmd_t *pmdp, unsigned long trap, unsigned long flags, |
| 23 | int ssize, unsigned int psize) |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 24 | { |
| 25 | unsigned int index, valid; |
| 26 | unsigned char *hpte_slot_array; |
| 27 | unsigned long rflags, pa, hidx; |
| 28 | unsigned long old_pmd, new_pmd; |
| 29 | int ret, lpsize = MMU_PAGE_16M; |
| 30 | unsigned long vpn, hash, shift, slot; |
| 31 | |
| 32 | /* |
| 33 | * atomically mark the linux large page PMD busy and dirty |
| 34 | */ |
| 35 | do { |
Michael Ellerman | 4f9c53c | 2015-03-25 20:11:57 +1100 | [diff] [blame] | 36 | pmd_t pmd = READ_ONCE(*pmdp); |
Aneesh Kumar K.V | 7e46724 | 2014-08-13 12:32:02 +0530 | [diff] [blame] | 37 | |
| 38 | old_pmd = pmd_val(pmd); |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 39 | /* If PMD busy, retry the access */ |
| 40 | if (unlikely(old_pmd & _PAGE_BUSY)) |
| 41 | return 0; |
Aneesh Kumar K.V | 0ac52dd | 2013-06-20 14:30:22 +0530 | [diff] [blame] | 42 | /* If PMD is trans splitting retry the access */ |
| 43 | if (unlikely(old_pmd & _PAGE_SPLITTING)) |
| 44 | return 0; |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 45 | /* If PMD permissions don't match, take page fault */ |
| 46 | if (unlikely(access & ~old_pmd)) |
| 47 | return 1; |
| 48 | /* |
| 49 | * Try to lock the PTE, add ACCESSED and DIRTY if it was |
| 50 | * a write access |
| 51 | */ |
| 52 | new_pmd = old_pmd | _PAGE_BUSY | _PAGE_ACCESSED; |
| 53 | if (access & _PAGE_RW) |
| 54 | new_pmd |= _PAGE_DIRTY; |
| 55 | } while (old_pmd != __cmpxchg_u64((unsigned long *)pmdp, |
| 56 | old_pmd, new_pmd)); |
| 57 | /* |
| 58 | * PP bits. _PAGE_USER is already PP bit 0x2, so we only |
| 59 | * need to add in 0x1 if it's a read-only user page |
| 60 | */ |
| 61 | rflags = new_pmd & _PAGE_USER; |
| 62 | if ((new_pmd & _PAGE_USER) && !((new_pmd & _PAGE_RW) && |
| 63 | (new_pmd & _PAGE_DIRTY))) |
| 64 | rflags |= 0x1; |
| 65 | /* |
| 66 | * _PAGE_EXEC -> HW_NO_EXEC since it's inverted |
| 67 | */ |
| 68 | rflags |= ((new_pmd & _PAGE_EXEC) ? 0 : HPTE_R_N); |
| 69 | |
| 70 | #if 0 |
| 71 | if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) { |
| 72 | |
| 73 | /* |
| 74 | * No CPU has hugepages but lacks no execute, so we |
| 75 | * don't need to worry about that case |
| 76 | */ |
| 77 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); |
| 78 | } |
| 79 | #endif |
| 80 | /* |
| 81 | * Find the slot index details for this ea, using base page size. |
| 82 | */ |
| 83 | shift = mmu_psize_defs[psize].shift; |
| 84 | index = (ea & ~HPAGE_PMD_MASK) >> shift; |
| 85 | BUG_ON(index >= 4096); |
| 86 | |
| 87 | vpn = hpt_vpn(ea, vsid, ssize); |
| 88 | hash = hpt_hash(vpn, shift, ssize); |
| 89 | hpte_slot_array = get_hpte_slot_array(pmdp); |
Aneesh Kumar K.V | 629149f | 2014-08-13 12:31:59 +0530 | [diff] [blame] | 90 | if (psize == MMU_PAGE_4K) { |
| 91 | /* |
| 92 | * invalidate the old hpte entry if we have that mapped via 64K |
| 93 | * base page size. This is because demote_segment won't flush |
| 94 | * hash page table entries. |
| 95 | */ |
| 96 | if ((old_pmd & _PAGE_HASHPTE) && !(old_pmd & _PAGE_COMBO)) |
Aneesh Kumar K.V | f1581bf | 2014-11-02 21:15:27 +0530 | [diff] [blame] | 97 | flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K, |
Aneesh Kumar K.V | aefa568 | 2014-12-04 11:00:14 +0530 | [diff] [blame] | 98 | ssize, flags); |
Aneesh Kumar K.V | 629149f | 2014-08-13 12:31:59 +0530 | [diff] [blame] | 99 | } |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 100 | |
| 101 | valid = hpte_valid(hpte_slot_array, index); |
| 102 | if (valid) { |
| 103 | /* update the hpte bits */ |
| 104 | hidx = hpte_hash_index(hpte_slot_array, index); |
| 105 | if (hidx & _PTEIDX_SECONDARY) |
| 106 | hash = ~hash; |
| 107 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; |
| 108 | slot += hidx & _PTEIDX_GROUP_IX; |
| 109 | |
| 110 | ret = ppc_md.hpte_updatepp(slot, rflags, vpn, |
Aneesh Kumar K.V | aefa568 | 2014-12-04 11:00:14 +0530 | [diff] [blame] | 111 | psize, lpsize, ssize, flags); |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 112 | /* |
| 113 | * We failed to update, try to insert a new entry. |
| 114 | */ |
| 115 | if (ret == -1) { |
| 116 | /* |
| 117 | * large pte is marked busy, so we can be sure |
| 118 | * nobody is looking at hpte_slot_array. hence we can |
| 119 | * safely update this here. |
| 120 | */ |
| 121 | valid = 0; |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 122 | hpte_slot_array[index] = 0; |
Aneesh Kumar K.V | 629149f | 2014-08-13 12:31:59 +0530 | [diff] [blame] | 123 | } |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (!valid) { |
| 127 | unsigned long hpte_group; |
| 128 | |
| 129 | /* insert new entry */ |
| 130 | pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT; |
Aneesh Kumar K.V | 629149f | 2014-08-13 12:31:59 +0530 | [diff] [blame] | 131 | new_pmd |= _PAGE_HASHPTE; |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 132 | |
| 133 | /* Add in WIMG bits */ |
| 134 | rflags |= (new_pmd & (_PAGE_WRITETHRU | _PAGE_NO_CACHE | |
Aneesh Kumar K.V | c8c06f5 | 2013-11-18 14:58:10 +0530 | [diff] [blame] | 135 | _PAGE_GUARDED)); |
| 136 | /* |
| 137 | * enable the memory coherence always |
| 138 | */ |
| 139 | rflags |= HPTE_R_M; |
Aneesh Kumar K.V | 629149f | 2014-08-13 12:31:59 +0530 | [diff] [blame] | 140 | repeat: |
| 141 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 142 | |
| 143 | /* Insert into the hash table, primary slot */ |
| 144 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0, |
| 145 | psize, lpsize, ssize); |
| 146 | /* |
| 147 | * Primary is full, try the secondary |
| 148 | */ |
| 149 | if (unlikely(slot == -1)) { |
| 150 | hpte_group = ((~hash & htab_hash_mask) * |
| 151 | HPTES_PER_GROUP) & ~0x7UL; |
| 152 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, |
| 153 | rflags, HPTE_V_SECONDARY, |
| 154 | psize, lpsize, ssize); |
| 155 | if (slot == -1) { |
| 156 | if (mftb() & 0x1) |
| 157 | hpte_group = ((hash & htab_hash_mask) * |
| 158 | HPTES_PER_GROUP) & ~0x7UL; |
| 159 | |
| 160 | ppc_md.hpte_remove(hpte_group); |
| 161 | goto repeat; |
| 162 | } |
| 163 | } |
| 164 | /* |
| 165 | * Hypervisor failure. Restore old pmd and return -1 |
| 166 | * similar to __hash_page_* |
| 167 | */ |
| 168 | if (unlikely(slot == -2)) { |
| 169 | *pmdp = __pmd(old_pmd); |
| 170 | hash_failure_debug(ea, access, vsid, trap, ssize, |
| 171 | psize, lpsize, old_pmd); |
| 172 | return -1; |
| 173 | } |
| 174 | /* |
| 175 | * large pte is marked busy, so we can be sure |
| 176 | * nobody is looking at hpte_slot_array. hence we can |
| 177 | * safely update this here. |
| 178 | */ |
| 179 | mark_hpte_slot_valid(hpte_slot_array, index, slot); |
| 180 | } |
| 181 | /* |
Aneesh Kumar K.V | 629149f | 2014-08-13 12:31:59 +0530 | [diff] [blame] | 182 | * Mark the pte with _PAGE_COMBO, if we are trying to hash it with |
| 183 | * base page size 4k. |
| 184 | */ |
| 185 | if (psize == MMU_PAGE_4K) |
| 186 | new_pmd |= _PAGE_COMBO; |
| 187 | /* |
Aneesh Kumar K.V | b0aa44a | 2014-08-13 12:31:57 +0530 | [diff] [blame] | 188 | * The hpte valid is stored in the pgtable whose address is in the |
| 189 | * second half of the PMD. Order this against clearing of the busy bit in |
| 190 | * huge pmd. |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 191 | */ |
Aneesh Kumar K.V | b0aa44a | 2014-08-13 12:31:57 +0530 | [diff] [blame] | 192 | smp_wmb(); |
Aneesh Kumar K.V | 6d492ec | 2013-06-20 14:30:21 +0530 | [diff] [blame] | 193 | *pmdp = __pmd(new_pmd & ~_PAGE_BUSY); |
| 194 | return 0; |
| 195 | } |