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