2 * Copyright IBM Corporation, 2015
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 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 #include <asm/machdep.h>
19 int __hash_page_4K(unsigned long ea
, unsigned long access
, unsigned long vsid
,
20 pte_t
*ptep
, unsigned long trap
, unsigned long flags
,
21 int ssize
, int subpg_prot
)
24 unsigned long hpte_group
;
25 unsigned long rflags
, pa
;
26 unsigned long old_pte
, new_pte
;
27 unsigned long vpn
, hash
, slot
;
28 unsigned long shift
= mmu_psize_defs
[MMU_PAGE_4K
].shift
;
31 * atomically mark the linux large page PTE busy and dirty
34 pte_t pte
= READ_ONCE(*ptep
);
36 old_pte
= pte_val(pte
);
37 /* If PTE busy, retry the access */
38 if (unlikely(old_pte
& H_PAGE_BUSY
))
40 /* If PTE permissions don't match, take page fault */
41 if (unlikely(!check_pte_access(access
, old_pte
)))
44 * Try to lock the PTE, add ACCESSED and DIRTY if it was
45 * a write access. Since this is 4K insert of 64K page size
46 * also add H_PAGE_COMBO
48 new_pte
= old_pte
| H_PAGE_BUSY
| _PAGE_ACCESSED
;
49 if (access
& _PAGE_WRITE
)
50 new_pte
|= _PAGE_DIRTY
;
51 } while (!pte_xchg(ptep
, __pte(old_pte
), __pte(new_pte
)));
54 * PP bits. _PAGE_USER is already PP bit 0x2, so we only
55 * need to add in 0x1 if it's a read-only user page
57 rflags
= htab_convert_pte_flags(new_pte
, flags
);
58 rpte
= __real_pte(__pte(old_pte
), ptep
, PTRS_PER_PTE
);
60 if (cpu_has_feature(CPU_FTR_NOEXECUTE
) &&
61 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE
))
62 rflags
= hash_page_do_lazy_icache(rflags
, __pte(old_pte
), trap
);
64 vpn
= hpt_vpn(ea
, vsid
, ssize
);
65 if (unlikely(old_pte
& H_PAGE_HASHPTE
)) {
67 * There MIGHT be an HPTE for this pte
69 unsigned long gslot
= pte_get_hash_gslot(vpn
, shift
, ssize
,
72 if (mmu_hash_ops
.hpte_updatepp(gslot
, rflags
, vpn
, MMU_PAGE_4K
,
73 MMU_PAGE_4K
, ssize
, flags
) == -1)
74 old_pte
&= ~_PAGE_HPTEFLAGS
;
77 if (likely(!(old_pte
& H_PAGE_HASHPTE
))) {
79 pa
= pte_pfn(__pte(old_pte
)) << PAGE_SHIFT
;
80 hash
= hpt_hash(vpn
, shift
, ssize
);
83 hpte_group
= (hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
85 /* Insert into the hash table, primary slot */
86 slot
= mmu_hash_ops
.hpte_insert(hpte_group
, vpn
, pa
, rflags
, 0,
87 MMU_PAGE_4K
, MMU_PAGE_4K
, ssize
);
89 * Primary is full, try the secondary
91 if (unlikely(slot
== -1)) {
92 hpte_group
= (~hash
& htab_hash_mask
) * HPTES_PER_GROUP
;
93 slot
= mmu_hash_ops
.hpte_insert(hpte_group
, vpn
, pa
,
100 hpte_group
= (hash
& htab_hash_mask
) *
102 mmu_hash_ops
.hpte_remove(hpte_group
);
104 * FIXME!! Should be try the group from which we removed ?
110 * Hypervisor failure. Restore old pte and return -1
111 * similar to __hash_page_*
113 if (unlikely(slot
== -2)) {
114 *ptep
= __pte(old_pte
);
115 hash_failure_debug(ea
, access
, vsid
, trap
, ssize
,
116 MMU_PAGE_4K
, MMU_PAGE_4K
, old_pte
);
119 new_pte
= (new_pte
& ~_PAGE_HPTEFLAGS
) | H_PAGE_HASHPTE
;
120 new_pte
|= pte_set_hidx(ptep
, rpte
, 0, slot
, PTRS_PER_PTE
);
122 *ptep
= __pte(new_pte
& ~H_PAGE_BUSY
);