blob: c304d0767919b76cde9579f77088f5ecd93e991f [file] [log] [blame]
Paul Mackerras047ea782005-11-19 20:17:32 +11001#ifndef _ASM_POWERPC_PGTABLE_H
2#define _ASM_POWERPC_PGTABLE_H
Arnd Bergmann88ced032005-12-16 22:43:46 +01003#ifdef __KERNEL__
Paul Mackerras047ea782005-11-19 20:17:32 +11004
David Gibson9c709f32007-06-13 14:52:56 +10005#ifndef __ASSEMBLY__
Aneesh Kumar K.Vc34a51c2013-11-18 14:58:13 +05306#include <linux/mmdebug.h>
Scott Wood1c980252014-08-08 18:40:42 -05007#include <linux/mmzone.h>
David Gibson9c709f32007-06-13 14:52:56 +10008#include <asm/processor.h> /* For TASK_SIZE */
9#include <asm/mmu.h>
10#include <asm/page.h>
Benjamin Herrenschmidt8d30c142009-02-10 16:02:37 +000011
David Gibson9c709f32007-06-13 14:52:56 +100012struct mm_struct;
Benjamin Herrenschmidt8d30c142009-02-10 16:02:37 +000013
David Gibson9c709f32007-06-13 14:52:56 +100014#endif /* !__ASSEMBLY__ */
15
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +053016#ifdef CONFIG_PPC_BOOK3S
17#include <asm/book3s/pgtable.h>
18#else
David Gibsonf88df142007-04-30 16:30:56 +100019#if defined(CONFIG_PPC64)
20# include <asm/pgtable-ppc64.h>
Paul Mackerras047ea782005-11-19 20:17:32 +110021#else
David Gibsonf88df142007-04-30 16:30:56 +100022# include <asm/pgtable-ppc32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#endif
Aneesh Kumar K.V3dfcb3152015-12-01 09:06:28 +053024#endif /* !CONFIG_PPC_BOOK3S */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Aneesh Kumar K.Vcc3665a2013-04-28 09:37:27 +000026/*
27 * We save the slot number & secondary bit in the second half of the
28 * PTE page. We use the 8 bytes per each pte entry.
29 */
30#define PTE_PAGE_HIDX_OFFSET (PTRS_PER_PTE * 8)
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#ifndef __ASSEMBLY__
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +000033
Aneesh Kumar K.V78f1dbd2012-09-10 02:52:57 +000034#include <asm/tlbflush.h>
35
Benjamin Herrenschmidt71087002009-03-19 19:34:09 +000036/* Generic accessors to PTE bits */
LEROY Christophea7b9f672015-01-19 17:04:38 +010037static inline int pte_write(pte_t pte)
38{ return (pte_val(pte) & (_PAGE_RW | _PAGE_RO)) != _PAGE_RO; }
Benjamin Herrenschmidt71087002009-03-19 19:34:09 +000039static inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
40static inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
Benjamin Herrenschmidt71087002009-03-19 19:34:09 +000041static inline int pte_special(pte_t pte) { return pte_val(pte) & _PAGE_SPECIAL; }
Benjamin Herrenschmidt71087002009-03-19 19:34:09 +000042static inline int pte_none(pte_t pte) { return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; }
43static inline pgprot_t pte_pgprot(pte_t pte) { return __pgprot(pte_val(pte) & PAGE_PROT_BITS); }
44
Aneesh Kumar K.Vc34a51c2013-11-18 14:58:13 +053045#ifdef CONFIG_NUMA_BALANCING
Mel Gormane7bb4b6d2015-02-12 14:58:19 -080046/*
47 * These work without NUMA balancing but the kernel does not care. See the
48 * comment in include/asm-generic/pgtable.h . On powerpc, this will only
49 * work for user pages and always return true for kernel pages.
50 */
51static inline int pte_protnone(pte_t pte)
52{
53 return (pte_val(pte) &
54 (_PAGE_PRESENT | _PAGE_USER)) == _PAGE_PRESENT;
55}
56
57static inline int pmd_protnone(pmd_t pmd)
58{
59 return pte_protnone(pmd_pte(pmd));
60}
Mel Gorman21d9ee32015-02-12 14:58:32 -080061#endif /* CONFIG_NUMA_BALANCING */
Aneesh Kumar K.Vc34a51c2013-11-18 14:58:13 +053062
63static inline int pte_present(pte_t pte)
64{
65 return pte_val(pte) & _PAGE_PRESENT;
66}
Aneesh Kumar K.Vc34a51c2013-11-18 14:58:13 +053067
Benjamin Herrenschmidt71087002009-03-19 19:34:09 +000068/* Conversion functions: convert a page and protection to a page entry,
69 * and a page entry and page directory to the page they refer to.
70 *
71 * Even if PTEs can be unsigned long long, a PFN is always an unsigned
72 * long for now.
73 */
74static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot) {
75 return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) |
76 pgprot_val(pgprot)); }
77static inline unsigned long pte_pfn(pte_t pte) {
78 return pte_val(pte) >> PTE_RPN_SHIFT; }
79
80/* Keep these as a macros to avoid include dependency mess */
81#define pte_page(x) pfn_to_page(pte_pfn(x))
82#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
83
84/* Generic modifiers for PTE bits */
85static inline pte_t pte_wrprotect(pte_t pte) {
LEROY Christophea7b9f672015-01-19 17:04:38 +010086 pte_val(pte) &= ~(_PAGE_RW | _PAGE_HWWRITE);
87 pte_val(pte) |= _PAGE_RO; return pte; }
Benjamin Herrenschmidt71087002009-03-19 19:34:09 +000088static inline pte_t pte_mkclean(pte_t pte) {
89 pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HWWRITE); return pte; }
90static inline pte_t pte_mkold(pte_t pte) {
91 pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
92static inline pte_t pte_mkwrite(pte_t pte) {
LEROY Christophea7b9f672015-01-19 17:04:38 +010093 pte_val(pte) &= ~_PAGE_RO;
Benjamin Herrenschmidt71087002009-03-19 19:34:09 +000094 pte_val(pte) |= _PAGE_RW; return pte; }
95static inline pte_t pte_mkdirty(pte_t pte) {
96 pte_val(pte) |= _PAGE_DIRTY; return pte; }
97static inline pte_t pte_mkyoung(pte_t pte) {
98 pte_val(pte) |= _PAGE_ACCESSED; return pte; }
99static inline pte_t pte_mkspecial(pte_t pte) {
100 pte_val(pte) |= _PAGE_SPECIAL; return pte; }
101static inline pte_t pte_mkhuge(pte_t pte) {
102 return pte; }
103static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
104{
105 pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot);
106 return pte;
107}
108
109
Benjamin Herrenschmidt8d30c142009-02-10 16:02:37 +0000110/* Insert a PTE, top-level function is out of line. It uses an inline
111 * low level function in the respective pgtable-* files
112 */
113extern void set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
114 pte_t pte);
115
116/* This low level function performs the actual PTE insertion
117 * Setting the PTE depends on the MMU type and other factors. It's
118 * an horrible mess that I'm not going to try to clean up now but
119 * I'm keeping it in one place rather than spread around
120 */
121static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
122 pte_t *ptep, pte_t pte, int percpu)
123{
124#if defined(CONFIG_PPC_STD_MMU_32) && defined(CONFIG_SMP) && !defined(CONFIG_PTE_64BIT)
125 /* First case is 32-bit Hash MMU in SMP mode with 32-bit PTEs. We use the
126 * helper pte_update() which does an atomic update. We need to do that
127 * because a concurrent invalidation can clear _PAGE_HASHPTE. If it's a
128 * per-CPU PTE such as a kmap_atomic, we do a simple update preserving
129 * the hash bits instead (ie, same as the non-SMP case)
130 */
131 if (percpu)
132 *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE)
133 | (pte_val(pte) & ~_PAGE_HASHPTE));
134 else
135 pte_update(ptep, ~_PAGE_HASHPTE, pte_val(pte));
136
Paul Mackerras1660e9d2009-08-17 14:36:32 +1000137#elif defined(CONFIG_PPC32) && defined(CONFIG_PTE_64BIT)
138 /* Second case is 32-bit with 64-bit PTE. In this case, we
Benjamin Herrenschmidt8d30c142009-02-10 16:02:37 +0000139 * can just store as long as we do the two halves in the right order
140 * with a barrier in between. This is possible because we take care,
141 * in the hash code, to pre-invalidate if the PTE was already hashed,
142 * which synchronizes us with any concurrent invalidation.
143 * In the percpu case, we also fallback to the simple update preserving
144 * the hash bits
145 */
146 if (percpu) {
147 *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE)
148 | (pte_val(pte) & ~_PAGE_HASHPTE));
149 return;
150 }
151#if _PAGE_HASHPTE != 0
152 if (pte_val(*ptep) & _PAGE_HASHPTE)
153 flush_hash_entry(mm, ptep, addr);
154#endif
155 __asm__ __volatile__("\
156 stw%U0%X0 %2,%0\n\
157 eieio\n\
158 stw%U0%X0 %L2,%1"
159 : "=m" (*ptep), "=m" (*((unsigned char *)ptep+4))
160 : "r" (pte) : "memory");
161
162#elif defined(CONFIG_PPC_STD_MMU_32)
163 /* Third case is 32-bit hash table in UP mode, we need to preserve
164 * the _PAGE_HASHPTE bit since we may not have invalidated the previous
165 * translation in the hash yet (done in a subsequent flush_tlb_xxx())
166 * and see we need to keep track that this PTE needs invalidating
167 */
168 *ptep = __pte((pte_val(*ptep) & _PAGE_HASHPTE)
169 | (pte_val(pte) & ~_PAGE_HASHPTE));
170
171#else
172 /* Anything else just stores the PTE normally. That covers all 64-bit
Paul Mackerras1660e9d2009-08-17 14:36:32 +1000173 * cases, and 32-bit non-hash with 32-bit PTEs.
Benjamin Herrenschmidt8d30c142009-02-10 16:02:37 +0000174 */
175 *ptep = pte;
Scott Wood0d61f0b2015-07-18 14:24:57 -0500176
177#ifdef CONFIG_PPC_BOOK3E_64
178 /*
179 * With hardware tablewalk, a sync is needed to ensure that
180 * subsequent accesses see the PTE we just wrote. Unlike userspace
181 * mappings, we can't tolerate spurious faults, so make sure
182 * the new PTE will be seen the first time.
183 */
184 if (is_kernel_addr(addr))
185 mb();
186#endif
Benjamin Herrenschmidt8d30c142009-02-10 16:02:37 +0000187#endif
188}
189
190
191#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
192extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
193 pte_t *ptep, pte_t entry, int dirty);
194
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000195/*
196 * Macro to mark a page protection value as "uncacheable".
197 */
198
199#define _PAGE_CACHE_CTL (_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \
200 _PAGE_WRITETHRU)
201
202#define pgprot_noncached(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
203 _PAGE_NO_CACHE | _PAGE_GUARDED))
204
205#define pgprot_noncached_wc(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
206 _PAGE_NO_CACHE))
207
208#define pgprot_cached(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
209 _PAGE_COHERENT))
210
211#define pgprot_cached_wthru(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
212 _PAGE_COHERENT | _PAGE_WRITETHRU))
213
Geoff Thorpe09c188c2011-10-27 02:58:45 +0000214#define pgprot_cached_noncoherent(prot) \
215 (__pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL))
216
Anton Blanchardfe3cc0d92011-02-28 20:00:47 +0000217#define pgprot_writecombine pgprot_noncached_wc
Benjamin Herrenschmidt64b3d0e2008-12-18 19:13:51 +0000218
219struct file;
220extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
221 unsigned long size, pgprot_t vma_prot);
222#define __HAVE_PHYS_MEM_ACCESS_PROT
223
David Gibson9c709f32007-06-13 14:52:56 +1000224/*
225 * ZERO_PAGE is a global shared page that is always zero: used
226 * for zero-mapped memory areas etc..
227 */
228extern unsigned long empty_zero_page[];
229#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
230
231extern pgd_t swapper_pg_dir[];
232
Scott Wood1c980252014-08-08 18:40:42 -0500233void limit_zone_pfn(enum zone_type zone, unsigned long max_pfn);
234int dma_pfn_limit_to_zone(u64 pfn_limit);
David Gibson9c709f32007-06-13 14:52:56 +1000235extern void paging_init(void);
236
237/*
238 * kern_addr_valid is intended to indicate whether an address is a valid
239 * kernel address. Most 32-bit archs define it as always true (like this)
240 * but most 64-bit archs actually perform a test. What should we do here?
241 */
242#define kern_addr_valid(addr) (1)
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#include <asm-generic/pgtable.h>
Benjamin Herrenschmidt1e3519f2008-07-25 16:21:11 +1000245
246
247/*
248 * This gets called at the end of handling a page fault, when
249 * the kernel has put a new PTE into the page table for the process.
250 * We use it to ensure coherency between the i-cache and d-cache
251 * for the page which has just been mapped in.
252 * On machines which use an MMU hash table, we use this to put a
253 * corresponding HPTE into the hash table ahead of time, instead of
254 * waiting for the inevitable extra hash-table miss exception.
255 */
Russell King4b3073e2009-12-18 16:40:18 +0000256extern void update_mmu_cache(struct vm_area_struct *, unsigned long, pte_t *);
Benjamin Herrenschmidt1e3519f2008-07-25 16:21:11 +1000257
Aneesh Kumar K.Ve2b3d202013-04-28 09:37:30 +0000258extern int gup_hugepte(pte_t *ptep, unsigned long sz, unsigned long addr,
Aneesh Kumar K.Vb30e7592014-11-05 21:57:41 +0530259 unsigned long end, int write,
260 struct page **pages, int *nr);
Aneesh Kumar K.V074c2ea2013-06-20 14:30:15 +0530261#ifndef CONFIG_TRANSPARENT_HUGEPAGE
262#define pmd_large(pmd) 0
263#define has_transparent_hugepage() 0
264#endif
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530265pte_t *__find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +0530266 bool *is_thp, unsigned *shift);
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530267static inline pte_t *find_linux_pte_or_hugepte(pgd_t *pgdir, unsigned long ea,
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +0530268 bool *is_thp, unsigned *shift)
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530269{
270 if (!arch_irqs_disabled()) {
271 pr_info("%s called with irq enabled\n", __func__);
272 dump_stack();
273 }
Aneesh Kumar K.V891121e2015-10-09 08:32:21 +0530274 return __find_linux_pte_or_hugepte(pgdir, ea, is_thp, shift);
Aneesh Kumar K.V691e95f2015-03-30 10:41:03 +0530275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#endif /* __ASSEMBLY__ */
277
Arnd Bergmann88ced032005-12-16 22:43:46 +0100278#endif /* __KERNEL__ */
Paul Mackerras047ea782005-11-19 20:17:32 +1100279#endif /* _ASM_POWERPC_PGTABLE_H */