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, | |
22 | pmd_t *pmdp, unsigned long trap, int local, int ssize, | |
23 | unsigned int psize) | |
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 { | |
7e467245 AK |
36 | pmd_t pmd = ACCESS_ONCE(*pmdp); |
37 | ||
38 | old_pmd = pmd_val(pmd); | |
6d492ecc AK |
39 | /* If PMD busy, retry the access */ |
40 | if (unlikely(old_pmd & _PAGE_BUSY)) | |
41 | return 0; | |
0ac52dd7 AK |
42 | /* If PMD is trans splitting retry the access */ |
43 | if (unlikely(old_pmd & _PAGE_SPLITTING)) | |
44 | return 0; | |
6d492ecc AK |
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); | |
629149fa AK |
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)) | |
f1581bf1 | 97 | flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K, |
d557b098 | 98 | ssize, local); |
629149fa | 99 | } |
6d492ecc AK |
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, | |
111 | psize, lpsize, ssize, local); | |
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; | |
6d492ecc | 122 | hpte_slot_array[index] = 0; |
629149fa | 123 | } |
6d492ecc AK |
124 | } |
125 | ||
126 | if (!valid) { | |
127 | unsigned long hpte_group; | |
128 | ||
129 | /* insert new entry */ | |
130 | pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT; | |
629149fa | 131 | new_pmd |= _PAGE_HASHPTE; |
6d492ecc AK |
132 | |
133 | /* Add in WIMG bits */ | |
134 | rflags |= (new_pmd & (_PAGE_WRITETHRU | _PAGE_NO_CACHE | | |
c8c06f5a AK |
135 | _PAGE_GUARDED)); |
136 | /* | |
137 | * enable the memory coherence always | |
138 | */ | |
139 | rflags |= HPTE_R_M; | |
629149fa AK |
140 | repeat: |
141 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; | |
6d492ecc AK |
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 | } | |
629149fa AK |
181 | /* |
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; | |
6d492ecc | 187 | /* |
b0aa44a3 AK |
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. | |
6d492ecc | 191 | */ |
b0aa44a3 | 192 | smp_wmb(); |
6d492ecc AK |
193 | *pmdp = __pmd(new_pmd & ~_PAGE_BUSY); |
194 | return 0; | |
195 | } |