2 * Copyright IBM Corporation, 2013
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
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.
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.
16 * PPC64 THP Support for hash based MMUs
19 #include <asm/machdep.h>
21 int __hash_page_thp(unsigned long ea, unsigned long access, unsigned long vsid,
22 pmd_t *pmdp, unsigned long trap, unsigned long flags,
23 int ssize, unsigned int psize)
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;
33 * atomically mark the linux large page PMD busy and dirty
36 pmd_t pmd = READ_ONCE(*pmdp);
38 old_pmd = pmd_val(pmd);
39 /* If PMD busy, retry the access */
40 if (unlikely(old_pmd & _PAGE_BUSY))
42 /* If PMD is trans splitting retry the access */
43 if (unlikely(old_pmd & _PAGE_SPLITTING))
45 /* If PMD permissions don't match, take page fault */
46 if (unlikely(access & ~old_pmd))
49 * Try to lock the PTE, add ACCESSED and DIRTY if it was
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,
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
61 rflags = new_pmd & _PAGE_USER;
62 if ((new_pmd & _PAGE_USER) && !((new_pmd & _PAGE_RW) &&
63 (new_pmd & _PAGE_DIRTY)))
66 * _PAGE_EXEC -> HW_NO_EXEC since it's inverted
68 rflags |= ((new_pmd & _PAGE_EXEC) ? 0 : HPTE_R_N);
71 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
74 * No CPU has hugepages but lacks no execute, so we
75 * don't need to worry about that case
77 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
81 * Find the slot index details for this ea, using base page size.
83 shift = mmu_psize_defs[psize].shift;
84 index = (ea & ~HPAGE_PMD_MASK) >> shift;
85 BUG_ON(index >= 4096);
87 vpn = hpt_vpn(ea, vsid, ssize);
88 hash = hpt_hash(vpn, shift, ssize);
89 hpte_slot_array = get_hpte_slot_array(pmdp);
90 if (psize == MMU_PAGE_4K) {
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.
96 if ((old_pmd & _PAGE_HASHPTE) && !(old_pmd & _PAGE_COMBO))
97 flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K,
101 valid = hpte_valid(hpte_slot_array, index);
103 /* update the hpte bits */
104 hidx = hpte_hash_index(hpte_slot_array, index);
105 if (hidx & _PTEIDX_SECONDARY)
107 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
108 slot += hidx & _PTEIDX_GROUP_IX;
110 ret = ppc_md.hpte_updatepp(slot, rflags, vpn,
111 psize, lpsize, ssize, flags);
113 * We failed to update, try to insert a new entry.
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.
122 hpte_slot_array[index] = 0;
127 unsigned long hpte_group;
129 /* insert new entry */
130 pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;
131 new_pmd |= _PAGE_HASHPTE;
133 /* Add in WIMG bits */
134 rflags |= (new_pmd & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
137 * enable the memory coherence always
141 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
143 /* Insert into the hash table, primary slot */
144 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
145 psize, lpsize, ssize);
147 * Primary is full, try the secondary
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);
157 hpte_group = ((hash & htab_hash_mask) *
158 HPTES_PER_GROUP) & ~0x7UL;
160 ppc_md.hpte_remove(hpte_group);
165 * Hypervisor failure. Restore old pmd and return -1
166 * similar to __hash_page_*
168 if (unlikely(slot == -2)) {
169 *pmdp = __pmd(old_pmd);
170 hash_failure_debug(ea, access, vsid, trap, ssize,
171 psize, lpsize, old_pmd);
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.
179 mark_hpte_slot_valid(hpte_slot_array, index, slot);
182 * Mark the pte with _PAGE_COMBO, if we are trying to hash it with
185 if (psize == MMU_PAGE_4K)
186 new_pmd |= _PAGE_COMBO;
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
193 *pmdp = __pmd(new_pmd & ~_PAGE_BUSY);