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
, int local
, int ssize
,
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 old_pmd
= pmd_val(*pmdp
);
37 /* If PMD busy, retry the access */
38 if (unlikely(old_pmd
& _PAGE_BUSY
))
40 /* If PMD is trans splitting retry the access */
41 if (unlikely(old_pmd
& _PAGE_SPLITTING
))
43 /* If PMD permissions don't match, take page fault */
44 if (unlikely(access
& ~old_pmd
))
47 * Try to lock the PTE, add ACCESSED and DIRTY if it was
50 new_pmd
= old_pmd
| _PAGE_BUSY
| _PAGE_ACCESSED
;
51 if (access
& _PAGE_RW
)
52 new_pmd
|= _PAGE_DIRTY
;
53 } while (old_pmd
!= __cmpxchg_u64((unsigned long *)pmdp
,
56 * PP bits. _PAGE_USER is already PP bit 0x2, so we only
57 * need to add in 0x1 if it's a read-only user page
59 rflags
= new_pmd
& _PAGE_USER
;
60 if ((new_pmd
& _PAGE_USER
) && !((new_pmd
& _PAGE_RW
) &&
61 (new_pmd
& _PAGE_DIRTY
)))
64 * _PAGE_EXEC -> HW_NO_EXEC since it's inverted
66 rflags
|= ((new_pmd
& _PAGE_EXEC
) ? 0 : HPTE_R_N
);
69 if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE
)) {
72 * No CPU has hugepages but lacks no execute, so we
73 * don't need to worry about that case
75 rflags
= hash_page_do_lazy_icache(rflags
, __pte(old_pte
), trap
);
79 * Find the slot index details for this ea, using base page size.
81 shift
= mmu_psize_defs
[psize
].shift
;
82 index
= (ea
& ~HPAGE_PMD_MASK
) >> shift
;
83 BUG_ON(index
>= 4096);
85 vpn
= hpt_vpn(ea
, vsid
, ssize
);
86 hash
= hpt_hash(vpn
, shift
, ssize
);
87 hpte_slot_array
= get_hpte_slot_array(pmdp
);
89 valid
= hpte_valid(hpte_slot_array
, index
);
91 /* update the hpte bits */
92 hidx
= hpte_hash_index(hpte_slot_array
, index
);
93 if (hidx
& _PTEIDX_SECONDARY
)
95 slot
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
96 slot
+= hidx
& _PTEIDX_GROUP_IX
;
98 ret
= ppc_md
.hpte_updatepp(slot
, rflags
, vpn
,
99 psize
, lpsize
, ssize
, local
);
101 * We failed to update, try to insert a new entry.
105 * large pte is marked busy, so we can be sure
106 * nobody is looking at hpte_slot_array. hence we can
107 * safely update this here.
110 new_pmd
&= ~_PAGE_HPTEFLAGS
;
111 hpte_slot_array
[index
] = 0;
113 /* clear the busy bits and set the hash pte bits */
114 new_pmd
= (new_pmd
& ~_PAGE_HPTEFLAGS
) | _PAGE_HASHPTE
;
118 unsigned long hpte_group
;
120 /* insert new entry */
121 pa
= pmd_pfn(__pmd(old_pmd
)) << PAGE_SHIFT
;
123 hpte_group
= ((hash
& htab_hash_mask
) * HPTES_PER_GROUP
) & ~0x7UL
;
125 /* clear the busy bits and set the hash pte bits */
126 new_pmd
= (new_pmd
& ~_PAGE_HPTEFLAGS
) | _PAGE_HASHPTE
;
128 /* Add in WIMG bits */
129 rflags
|= (new_pmd
& (_PAGE_WRITETHRU
| _PAGE_NO_CACHE
|
130 _PAGE_COHERENT
| _PAGE_GUARDED
));
132 /* Insert into the hash table, primary slot */
133 slot
= ppc_md
.hpte_insert(hpte_group
, vpn
, pa
, rflags
, 0,
134 psize
, lpsize
, ssize
);
136 * Primary is full, try the secondary
138 if (unlikely(slot
== -1)) {
139 hpte_group
= ((~hash
& htab_hash_mask
) *
140 HPTES_PER_GROUP
) & ~0x7UL
;
141 slot
= ppc_md
.hpte_insert(hpte_group
, vpn
, pa
,
142 rflags
, HPTE_V_SECONDARY
,
143 psize
, lpsize
, ssize
);
146 hpte_group
= ((hash
& htab_hash_mask
) *
147 HPTES_PER_GROUP
) & ~0x7UL
;
149 ppc_md
.hpte_remove(hpte_group
);
154 * Hypervisor failure. Restore old pmd and return -1
155 * similar to __hash_page_*
157 if (unlikely(slot
== -2)) {
158 *pmdp
= __pmd(old_pmd
);
159 hash_failure_debug(ea
, access
, vsid
, trap
, ssize
,
160 psize
, lpsize
, old_pmd
);
164 * large pte is marked busy, so we can be sure
165 * nobody is looking at hpte_slot_array. hence we can
166 * safely update this here.
168 mark_hpte_slot_valid(hpte_slot_array
, index
, slot
);
171 * No need to use ldarx/stdcx here
173 *pmdp
= __pmd(new_pmd
& ~_PAGE_BUSY
);