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 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)); | |
c6a3c495 | 57 | rflags = htab_convert_pte_flags(new_pmd); |
6d492ecc AK |
58 | |
59 | #if 0 | |
60 | if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) { | |
61 | ||
62 | /* | |
63 | * No CPU has hugepages but lacks no execute, so we | |
64 | * don't need to worry about that case | |
65 | */ | |
66 | rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap); | |
67 | } | |
68 | #endif | |
69 | /* | |
70 | * Find the slot index details for this ea, using base page size. | |
71 | */ | |
72 | shift = mmu_psize_defs[psize].shift; | |
73 | index = (ea & ~HPAGE_PMD_MASK) >> shift; | |
74 | BUG_ON(index >= 4096); | |
75 | ||
76 | vpn = hpt_vpn(ea, vsid, ssize); | |
6d492ecc | 77 | hpte_slot_array = get_hpte_slot_array(pmdp); |
629149fa AK |
78 | if (psize == MMU_PAGE_4K) { |
79 | /* | |
80 | * invalidate the old hpte entry if we have that mapped via 64K | |
81 | * base page size. This is because demote_segment won't flush | |
82 | * hash page table entries. | |
83 | */ | |
84 | if ((old_pmd & _PAGE_HASHPTE) && !(old_pmd & _PAGE_COMBO)) | |
f1581bf1 | 85 | flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K, |
aefa5688 | 86 | ssize, flags); |
629149fa | 87 | } |
6d492ecc AK |
88 | |
89 | valid = hpte_valid(hpte_slot_array, index); | |
90 | if (valid) { | |
91 | /* update the hpte bits */ | |
36b35d5d | 92 | hash = hpt_hash(vpn, shift, ssize); |
6d492ecc AK |
93 | hidx = hpte_hash_index(hpte_slot_array, index); |
94 | if (hidx & _PTEIDX_SECONDARY) | |
95 | hash = ~hash; | |
96 | slot = (hash & htab_hash_mask) * HPTES_PER_GROUP; | |
97 | slot += hidx & _PTEIDX_GROUP_IX; | |
98 | ||
99 | ret = ppc_md.hpte_updatepp(slot, rflags, vpn, | |
aefa5688 | 100 | psize, lpsize, ssize, flags); |
6d492ecc AK |
101 | /* |
102 | * We failed to update, try to insert a new entry. | |
103 | */ | |
104 | if (ret == -1) { | |
105 | /* | |
106 | * large pte is marked busy, so we can be sure | |
107 | * nobody is looking at hpte_slot_array. hence we can | |
108 | * safely update this here. | |
109 | */ | |
110 | valid = 0; | |
6d492ecc | 111 | hpte_slot_array[index] = 0; |
629149fa | 112 | } |
6d492ecc AK |
113 | } |
114 | ||
115 | if (!valid) { | |
116 | unsigned long hpte_group; | |
117 | ||
36b35d5d | 118 | hash = hpt_hash(vpn, shift, ssize); |
6d492ecc AK |
119 | /* insert new entry */ |
120 | pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT; | |
629149fa | 121 | new_pmd |= _PAGE_HASHPTE; |
6d492ecc | 122 | |
629149fa AK |
123 | repeat: |
124 | hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL; | |
6d492ecc AK |
125 | |
126 | /* Insert into the hash table, primary slot */ | |
127 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0, | |
128 | psize, lpsize, ssize); | |
129 | /* | |
130 | * Primary is full, try the secondary | |
131 | */ | |
132 | if (unlikely(slot == -1)) { | |
133 | hpte_group = ((~hash & htab_hash_mask) * | |
134 | HPTES_PER_GROUP) & ~0x7UL; | |
135 | slot = ppc_md.hpte_insert(hpte_group, vpn, pa, | |
136 | rflags, HPTE_V_SECONDARY, | |
137 | psize, lpsize, ssize); | |
138 | if (slot == -1) { | |
139 | if (mftb() & 0x1) | |
140 | hpte_group = ((hash & htab_hash_mask) * | |
141 | HPTES_PER_GROUP) & ~0x7UL; | |
142 | ||
143 | ppc_md.hpte_remove(hpte_group); | |
144 | goto repeat; | |
145 | } | |
146 | } | |
147 | /* | |
148 | * Hypervisor failure. Restore old pmd and return -1 | |
149 | * similar to __hash_page_* | |
150 | */ | |
151 | if (unlikely(slot == -2)) { | |
152 | *pmdp = __pmd(old_pmd); | |
153 | hash_failure_debug(ea, access, vsid, trap, ssize, | |
154 | psize, lpsize, old_pmd); | |
155 | return -1; | |
156 | } | |
157 | /* | |
158 | * large pte is marked busy, so we can be sure | |
159 | * nobody is looking at hpte_slot_array. hence we can | |
160 | * safely update this here. | |
161 | */ | |
162 | mark_hpte_slot_valid(hpte_slot_array, index, slot); | |
163 | } | |
629149fa AK |
164 | /* |
165 | * Mark the pte with _PAGE_COMBO, if we are trying to hash it with | |
166 | * base page size 4k. | |
167 | */ | |
168 | if (psize == MMU_PAGE_4K) | |
169 | new_pmd |= _PAGE_COMBO; | |
6d492ecc | 170 | /* |
b0aa44a3 AK |
171 | * The hpte valid is stored in the pgtable whose address is in the |
172 | * second half of the PMD. Order this against clearing of the busy bit in | |
173 | * huge pmd. | |
6d492ecc | 174 | */ |
b0aa44a3 | 175 | smp_wmb(); |
6d492ecc AK |
176 | *pmdp = __pmd(new_pmd & ~_PAGE_BUSY); |
177 | return 0; | |
178 | } |