1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/mm.h>
16#include <asm/machdep.h>
17#include <asm/mmu.h>
18
19int __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)
22{
23 real_pte_t rpte;
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;
29
30
31
32
33 do {
34 pte_t pte = READ_ONCE(*ptep);
35
36 old_pte = pte_val(pte);
37
38 if (unlikely(old_pte & H_PAGE_BUSY))
39 return 0;
40
41 if (unlikely(!check_pte_access(access, old_pte)))
42 return 1;
43
44
45
46
47
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)));
52
53
54
55
56
57 rflags = htab_convert_pte_flags(new_pte);
58 rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
59
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);
63
64 vpn = hpt_vpn(ea, vsid, ssize);
65 if (unlikely(old_pte & H_PAGE_HASHPTE)) {
66
67
68
69 unsigned long gslot = pte_get_hash_gslot(vpn, shift, ssize,
70 rpte, 0);
71
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;
75 }
76
77 if (likely(!(old_pte & H_PAGE_HASHPTE))) {
78
79 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
80 hash = hpt_hash(vpn, shift, ssize);
81
82repeat:
83 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
84
85
86 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
87 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
88
89
90
91 if (unlikely(slot == -1)) {
92 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
93 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
94 rflags,
95 HPTE_V_SECONDARY,
96 MMU_PAGE_4K,
97 MMU_PAGE_4K, ssize);
98 if (slot == -1) {
99 if (mftb() & 0x1)
100 hpte_group = ((hash & htab_hash_mask) *
101 HPTES_PER_GROUP) & ~0x7UL;
102 mmu_hash_ops.hpte_remove(hpte_group);
103
104
105
106 goto repeat;
107 }
108 }
109
110
111
112
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);
117 return -1;
118 }
119 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
120 new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
121 }
122 *ptep = __pte(new_pte & ~H_PAGE_BUSY);
123 return 0;
124}
125