Commit | Line | Data |
---|---|---|
6d492ecc AK |
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, | |
aefa5688 AK |
22 | pmd_t *pmdp, unsigned long trap, unsigned long flags, |
23 | int ssize, unsigned int psize) | |
6d492ecc AK |
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 { | |
4f9c53c8 | 36 | pmd_t pmd = READ_ONCE(*pmdp); |
7e467245 AK |
37 | |
38 | old_pmd = pmd_val(pmd); | |
6d492ecc | 39 | /* If PMD busy, retry the access */ |
945537df | 40 | if (unlikely(old_pmd & H_PAGE_BUSY)) |
6d492ecc AK |
41 | return 0; |
42 | /* If PMD permissions don't match, take page fault */ | |
ac29c640 | 43 | if (unlikely(!check_pte_access(access, old_pmd))) |
6d492ecc AK |
44 | return 1; |
45 | /* | |
46 | * Try to lock the PTE, add ACCESSED and DIRTY if it was | |
47 | * a write access | |
48 | */ | |
945537df | 49 | new_pmd = old_pmd | H_PAGE_BUSY | _PAGE_ACCESSED; |
c7d54842 | 50 | if (access & _PAGE_WRITE) |
6d492ecc | 51 | new_pmd |= _PAGE_DIRTY; |
5dc1ef85 AK |
52 | } while (!pmd_xchg(pmdp, __pmd(old_pmd), __pmd(new_pmd))); |
53 | ||
c6a3c495 | 54 | rflags = htab_convert_pte_flags(new_pmd); |
6d492ecc AK |
55 | |
56 | #if 0 | |
57 | if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) { | |
58 | ||
59 | /* | |
60 | * No CPU has hugepages but lacks no execute, so we | |
61 | * don't need to worry about that case | |
62 | */ | |
63 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); | |
64 | } | |
65 | #endif | |
66 | /* | |
67 | * Find the slot index details for this ea, using base page size. | |
68 | */ | |
69 | shift = mmu_psize_defs[psize].shift; | |
70 | index = (ea & ~HPAGE_PMD_MASK) >> shift; | |
4dcbd88e | 71 | BUG_ON(index >= PTE_FRAG_SIZE); |
6d492ecc AK |
72 | |
73 | vpn = hpt_vpn(ea, vsid, ssize); | |
6d492ecc | 74 | hpte_slot_array = get_hpte_slot_array(pmdp); |
629149fa AK |
75 | if (psize == MMU_PAGE_4K) { |
76 | /* | |
77 | * invalidate the old hpte entry if we have that mapped via 64K | |
78 | * base page size. This is because demote_segment won't flush | |
79 | * hash page table entries. | |
80 | */ | |
945537df | 81 | if ((old_pmd & H_PAGE_HASHPTE) && !(old_pmd & H_PAGE_COMBO)) { |
f1581bf1 | 82 | flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K, |
aefa5688 | 83 | ssize, flags); |
9ab3ac23 AK |
84 | /* |
85 | * With THP, we also clear the slot information with | |
86 | * respect to all the 64K hash pte mapping the 16MB | |
87 | * page. They are all invalid now. This make sure we | |
88 | * don't find the slot valid when we fault with 4k | |
89 | * base page size. | |
90 | * | |
91 | */ | |
92 | memset(hpte_slot_array, 0, PTE_FRAG_SIZE); | |
93 | } | |
629149fa | 94 | } |
6d492ecc AK |
95 | |
96 | valid = hpte_valid(hpte_slot_array, index); | |
97 | if (valid) { | |
98 | /* update the hpte bits */ | |
36b35d5d | 99 | hash = hpt_hash(vpn, shift, ssize); |
6d492ecc AK |
100 | hidx = hpte_hash_index(hpte_slot_array, index); |
101 | if (hidx & _PTEIDX_SECONDARY) | |
102 | hash = ~hash; | |
103 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; | |
104 | slot += hidx & _PTEIDX_GROUP_IX; | |
105 | ||
106 | ret = ppc_md.hpte_updatepp(slot, rflags, vpn, | |
aefa5688 | 107 | psize, lpsize, ssize, flags); |
6d492ecc AK |
108 | /* |
109 | * We failed to update, try to insert a new entry. | |
110 | */ | |
111 | if (ret == -1) { | |
112 | /* | |
113 | * large pte is marked busy, so we can be sure | |
114 | * nobody is looking at hpte_slot_array. hence we can | |
115 | * safely update this here. | |
116 | */ | |
117 | valid = 0; | |
6d492ecc | 118 | hpte_slot_array[index] = 0; |
629149fa | 119 | } |
6d492ecc AK |
120 | } |
121 | ||
122 | if (!valid) { | |
123 | unsigned long hpte_group; | |
124 | ||
36b35d5d | 125 | hash = hpt_hash(vpn, shift, ssize); |
6d492ecc AK |
126 | /* insert new entry */ |
127 | pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT; | |
945537df | 128 | new_pmd |= H_PAGE_HASHPTE; |
6d492ecc | 129 | |
629149fa AK |
130 | repeat: |
131 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; | |
6d492ecc AK |
132 | |
133 | /* Insert into the hash table, primary slot */ | |
134 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0, | |
135 | psize, lpsize, ssize); | |
136 | /* | |
137 | * Primary is full, try the secondary | |
138 | */ | |
139 | if (unlikely(slot == -1)) { | |
140 | hpte_group = ((~hash & htab_hash_mask) * | |
141 | HPTES_PER_GROUP) & ~0x7UL; | |
142 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, | |
143 | rflags, HPTE_V_SECONDARY, | |
144 | psize, lpsize, ssize); | |
145 | if (slot == -1) { | |
146 | if (mftb() & 0x1) | |
147 | hpte_group = ((hash & htab_hash_mask) * | |
148 | HPTES_PER_GROUP) & ~0x7UL; | |
149 | ||
150 | ppc_md.hpte_remove(hpte_group); | |
151 | goto repeat; | |
152 | } | |
153 | } | |
154 | /* | |
155 | * Hypervisor failure. Restore old pmd and return -1 | |
156 | * similar to __hash_page_* | |
157 | */ | |
158 | if (unlikely(slot == -2)) { | |
159 | *pmdp = __pmd(old_pmd); | |
160 | hash_failure_debug(ea, access, vsid, trap, ssize, | |
161 | psize, lpsize, old_pmd); | |
162 | return -1; | |
163 | } | |
164 | /* | |
165 | * large pte is marked busy, so we can be sure | |
166 | * nobody is looking at hpte_slot_array. hence we can | |
167 | * safely update this here. | |
168 | */ | |
169 | mark_hpte_slot_valid(hpte_slot_array, index, slot); | |
170 | } | |
629149fa | 171 | /* |
945537df | 172 | * Mark the pte with H_PAGE_COMBO, if we are trying to hash it with |
629149fa AK |
173 | * base page size 4k. |
174 | */ | |
175 | if (psize == MMU_PAGE_4K) | |
945537df | 176 | new_pmd |= H_PAGE_COMBO; |
6d492ecc | 177 | /* |
b0aa44a3 AK |
178 | * The hpte valid is stored in the pgtable whose address is in the |
179 | * second half of the PMD. Order this against clearing of the busy bit in | |
180 | * huge pmd. | |
6d492ecc | 181 | */ |
b0aa44a3 | 182 | smp_wmb(); |
945537df | 183 | *pmdp = __pmd(new_pmd & ~H_PAGE_BUSY); |
6d492ecc AK |
184 | return 0; |
185 | } |