xref: /openbmc/linux/arch/parisc/include/asm/pgtable.h (revision 5bf196f1)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2deae26bfSKyle McMartin #ifndef _PARISC_PGTABLE_H
3deae26bfSKyle McMartin #define _PARISC_PGTABLE_H
4deae26bfSKyle McMartin 
5bbcb03a9SQian Cai #include <asm/page.h>
6d96885e2SMike Rapoport 
7d96885e2SMike Rapoport #if CONFIG_PGTABLE_LEVELS == 3
8d96885e2SMike Rapoport #include <asm-generic/pgtable-nopud.h>
9d96885e2SMike Rapoport #elif CONFIG_PGTABLE_LEVELS == 2
10d96885e2SMike Rapoport #include <asm-generic/pgtable-nopmd.h>
11d96885e2SMike Rapoport #endif
12deae26bfSKyle McMartin 
13deae26bfSKyle McMartin #include <asm/fixmap.h>
14deae26bfSKyle McMartin 
15deae26bfSKyle McMartin #ifndef __ASSEMBLY__
16deae26bfSKyle McMartin /*
17deae26bfSKyle McMartin  * we simulate an x86-style page table for the linux mm code
18deae26bfSKyle McMartin  */
19deae26bfSKyle McMartin 
20deae26bfSKyle McMartin #include <linux/bitops.h>
2111537421SJames Bottomley #include <linux/spinlock.h>
227139bc15SJohn David Anglin #include <linux/mm_types.h>
23deae26bfSKyle McMartin #include <asm/processor.h>
24deae26bfSKyle McMartin #include <asm/cache.h>
25deae26bfSKyle McMartin 
26b37d1c18SMikulas Patocka /* This is for the serialization of PxTLB broadcasts. At least on the N class
27b37d1c18SMikulas Patocka  * systems, only one PxTLB inter processor broadcast can be active at any one
28b7795074SHelge Deller  * time on the Merced bus. */
29b37d1c18SMikulas Patocka extern spinlock_t pa_tlb_flush_lock;
30b37d1c18SMikulas Patocka #if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
31b37d1c18SMikulas Patocka extern int pa_serialize_tlb_flushes;
32b37d1c18SMikulas Patocka #else
33b37d1c18SMikulas Patocka #define pa_serialize_tlb_flushes        (0)
34b37d1c18SMikulas Patocka #endif
35b37d1c18SMikulas Patocka 
36b37d1c18SMikulas Patocka #define purge_tlb_start(flags)  do { \
37b37d1c18SMikulas Patocka 	if (pa_serialize_tlb_flushes)	\
38b37d1c18SMikulas Patocka 		spin_lock_irqsave(&pa_tlb_flush_lock, flags); \
39b37d1c18SMikulas Patocka 	else \
40b37d1c18SMikulas Patocka 		local_irq_save(flags);	\
41b37d1c18SMikulas Patocka 	} while (0)
42b37d1c18SMikulas Patocka #define purge_tlb_end(flags)	do { \
43b37d1c18SMikulas Patocka 	if (pa_serialize_tlb_flushes)	\
44b37d1c18SMikulas Patocka 		spin_unlock_irqrestore(&pa_tlb_flush_lock, flags); \
45b37d1c18SMikulas Patocka 	else \
46b37d1c18SMikulas Patocka 		local_irq_restore(flags); \
47b37d1c18SMikulas Patocka 	} while (0)
48b37d1c18SMikulas Patocka 
49b37d1c18SMikulas Patocka /* Purge data and instruction TLB entries. The TLB purge instructions
50b37d1c18SMikulas Patocka  * are slow on SMP machines since the purge must be broadcast to all CPUs.
5101ab6057SJohn David Anglin  */
5201ab6057SJohn David Anglin 
purge_tlb_entries(struct mm_struct * mm,unsigned long addr)5301ab6057SJohn David Anglin static inline void purge_tlb_entries(struct mm_struct *mm, unsigned long addr)
5401ab6057SJohn David Anglin {
55b37d1c18SMikulas Patocka 	unsigned long flags;
56b37d1c18SMikulas Patocka 
57b37d1c18SMikulas Patocka 	purge_tlb_start(flags);
58360bd6c6SHelge Deller 	mtsp(mm->context.space_id, SR_TEMP1);
59360bd6c6SHelge Deller 	pdtlb(SR_TEMP1, addr);
60360bd6c6SHelge Deller 	pitlb(SR_TEMP1, addr);
61b37d1c18SMikulas Patocka 	purge_tlb_end(flags);
6201ab6057SJohn David Anglin }
6301ab6057SJohn David Anglin 
6438860b2cSJohn David Anglin extern void __update_cache(pte_t pte);
6538860b2cSJohn David Anglin 
66deae26bfSKyle McMartin /* Certain architectures need to do special things when PTEs
67deae26bfSKyle McMartin  * within a page table are directly modified.  Thus, the following
68deae26bfSKyle McMartin  * hook is made available.
69deae26bfSKyle McMartin  */
70deae26bfSKyle McMartin #define set_pte(pteptr, pteval)			\
71deae26bfSKyle McMartin 	do {					\
72deae26bfSKyle McMartin 		*(pteptr) = (pteval);		\
7338860b2cSJohn David Anglin 		mb();				\
74deae26bfSKyle McMartin 	} while(0)
757139bc15SJohn David Anglin 
76deae26bfSKyle McMartin #endif /* !__ASSEMBLY__ */
77deae26bfSKyle McMartin 
78deae26bfSKyle McMartin #define pte_ERROR(e) \
79deae26bfSKyle McMartin 	printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
80d96885e2SMike Rapoport #if CONFIG_PGTABLE_LEVELS == 3
81deae26bfSKyle McMartin #define pmd_ERROR(e) \
82deae26bfSKyle McMartin 	printk("%s:%d: bad pmd %08lx.\n", __FILE__, __LINE__, (unsigned long)pmd_val(e))
83d96885e2SMike Rapoport #endif
84deae26bfSKyle McMartin #define pgd_ERROR(e) \
85deae26bfSKyle McMartin 	printk("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, (unsigned long)pgd_val(e))
86deae26bfSKyle McMartin 
87deae26bfSKyle McMartin /* This is the size of the initially mapped kernel memory */
8865bf34f5SHelge Deller #if defined(CONFIG_64BIT)
8965bf34f5SHelge Deller #define KERNEL_INITIAL_ORDER	26	/* 1<<26 = 64MB */
90332b42e4SHelge Deller #else
9165bf34f5SHelge Deller #define KERNEL_INITIAL_ORDER	25	/* 1<<25 = 32MB */
92332b42e4SHelge Deller #endif
93deae26bfSKyle McMartin #define KERNEL_INITIAL_SIZE	(1 << KERNEL_INITIAL_ORDER)
94deae26bfSKyle McMartin 
95f24ffde4SKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS == 3
967bf82eb3SMatthew Wilcox (Oracle) #define PMD_TABLE_ORDER	1
974501a7a0SMike Rapoport #define PGD_TABLE_ORDER	0
98deae26bfSKyle McMartin #else
994501a7a0SMike Rapoport #define PGD_TABLE_ORDER	1
100deae26bfSKyle McMartin #endif
101deae26bfSKyle McMartin 
102deae26bfSKyle McMartin /* Definitions for 3rd level (we use PLD here for Page Lower directory
103deae26bfSKyle McMartin  * because PTE_SHIFT is used lower down to mean shift that has to be
104deae26bfSKyle McMartin  * done to get usable bits out of the PTE) */
105deae26bfSKyle McMartin #define PLD_SHIFT	PAGE_SHIFT
106deae26bfSKyle McMartin #define PLD_SIZE	PAGE_SIZE
107deae26bfSKyle McMartin #define BITS_PER_PTE	(PAGE_SHIFT - BITS_PER_PTE_ENTRY)
108deae26bfSKyle McMartin #define PTRS_PER_PTE    (1UL << BITS_PER_PTE)
109deae26bfSKyle McMartin 
110deae26bfSKyle McMartin /* Definitions for 2nd level */
111d96885e2SMike Rapoport #if CONFIG_PGTABLE_LEVELS == 3
112deae26bfSKyle McMartin #define PMD_SHIFT       (PLD_SHIFT + BITS_PER_PTE)
113deae26bfSKyle McMartin #define PMD_SIZE	(1UL << PMD_SHIFT)
114deae26bfSKyle McMartin #define PMD_MASK	(~(PMD_SIZE-1))
1157bf82eb3SMatthew Wilcox (Oracle) #define BITS_PER_PMD	(PAGE_SHIFT + PMD_TABLE_ORDER - BITS_PER_PMD_ENTRY)
116d96885e2SMike Rapoport #define PTRS_PER_PMD    (1UL << BITS_PER_PMD)
117deae26bfSKyle McMartin #else
118deae26bfSKyle McMartin #define BITS_PER_PMD	0
119deae26bfSKyle McMartin #endif
120deae26bfSKyle McMartin 
121deae26bfSKyle McMartin /* Definitions for 1st level */
122d96885e2SMike Rapoport #define PGDIR_SHIFT	(PLD_SHIFT + BITS_PER_PTE + BITS_PER_PMD)
1234501a7a0SMike Rapoport #if (PGDIR_SHIFT + PAGE_SHIFT + PGD_TABLE_ORDER - BITS_PER_PGD_ENTRY) > BITS_PER_LONG
12448d27cb2SHelge Deller #define BITS_PER_PGD	(BITS_PER_LONG - PGDIR_SHIFT)
12548d27cb2SHelge Deller #else
1264501a7a0SMike Rapoport #define BITS_PER_PGD	(PAGE_SHIFT + PGD_TABLE_ORDER - BITS_PER_PGD_ENTRY)
12748d27cb2SHelge Deller #endif
128deae26bfSKyle McMartin #define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
129deae26bfSKyle McMartin #define PGDIR_MASK	(~(PGDIR_SIZE-1))
130deae26bfSKyle McMartin #define PTRS_PER_PGD    (1UL << BITS_PER_PGD)
131deae26bfSKyle McMartin #define USER_PTRS_PER_PGD       PTRS_PER_PGD
132deae26bfSKyle McMartin 
13348d27cb2SHelge Deller #ifdef CONFIG_64BIT
134deae26bfSKyle McMartin #define MAX_ADDRBITS	(PGDIR_SHIFT + BITS_PER_PGD)
135deae26bfSKyle McMartin #define MAX_ADDRESS	(1UL << MAX_ADDRBITS)
136deae26bfSKyle McMartin #define SPACEID_SHIFT	(MAX_ADDRBITS - 32)
13748d27cb2SHelge Deller #else
13848d27cb2SHelge Deller #define MAX_ADDRBITS	(BITS_PER_LONG)
139234ff4c5SHelge Deller #define MAX_ADDRESS	(1ULL << MAX_ADDRBITS)
14048d27cb2SHelge Deller #define SPACEID_SHIFT	0
14148d27cb2SHelge Deller #endif
142deae26bfSKyle McMartin 
143deae26bfSKyle McMartin /* This calculates the number of initial pages we need for the initial
144deae26bfSKyle McMartin  * page tables */
145fe94cb1aSHelge Deller #if (KERNEL_INITIAL_ORDER) >= (PLD_SHIFT + BITS_PER_PTE)
146fe94cb1aSHelge Deller # define PT_INITIAL	(1 << (KERNEL_INITIAL_ORDER - PLD_SHIFT - BITS_PER_PTE))
147deae26bfSKyle McMartin #else
148deae26bfSKyle McMartin # define PT_INITIAL	(1)  /* all initial PTEs fit into one page */
149deae26bfSKyle McMartin #endif
150deae26bfSKyle McMartin 
151deae26bfSKyle McMartin /*
152deae26bfSKyle McMartin  * pgd entries used up by user/kernel:
153deae26bfSKyle McMartin  */
154deae26bfSKyle McMartin 
155deae26bfSKyle McMartin /* NB: The tlb miss handlers make certain assumptions about the order */
156deae26bfSKyle McMartin /*     of the following bits, so be careful (One example, bits 25-31  */
157deae26bfSKyle McMartin /*     are moved together in one instruction).                        */
158deae26bfSKyle McMartin 
159deae26bfSKyle McMartin #define _PAGE_READ_BIT     31   /* (0x001) read access allowed */
160deae26bfSKyle McMartin #define _PAGE_WRITE_BIT    30   /* (0x002) write access allowed */
161deae26bfSKyle McMartin #define _PAGE_EXEC_BIT     29   /* (0x004) execute access allowed */
162deae26bfSKyle McMartin #define _PAGE_GATEWAY_BIT  28   /* (0x008) privilege promotion allowed */
163deae26bfSKyle McMartin #define _PAGE_DMB_BIT      27   /* (0x010) Data Memory Break enable (B bit) */
164deae26bfSKyle McMartin #define _PAGE_DIRTY_BIT    26   /* (0x020) Page Dirty (D bit) */
165deae26bfSKyle McMartin #define _PAGE_REFTRAP_BIT  25   /* (0x040) Page Ref. Trap enable (T bit) */
166deae26bfSKyle McMartin #define _PAGE_NO_CACHE_BIT 24   /* (0x080) Uncached Page (U bit) */
167deae26bfSKyle McMartin #define _PAGE_ACCESSED_BIT 23   /* (0x100) Software: Page Accessed */
168deae26bfSKyle McMartin #define _PAGE_PRESENT_BIT  22   /* (0x200) Software: translation valid */
1691f25ad26SHelge Deller #define _PAGE_HPAGE_BIT    21   /* (0x400) Software: Huge Page */
170deae26bfSKyle McMartin #define _PAGE_USER_BIT     20   /* (0x800) Software: User accessible page */
17170be49f2SHelge Deller #ifdef CONFIG_HUGETLB_PAGE
17270be49f2SHelge Deller #define _PAGE_SPECIAL_BIT  _PAGE_DMB_BIT  /* DMB feature is currently unused */
17370be49f2SHelge Deller #else
17470be49f2SHelge Deller #define _PAGE_SPECIAL_BIT  _PAGE_HPAGE_BIT /* use unused HUGE PAGE bit */
17570be49f2SHelge Deller #endif
176deae26bfSKyle McMartin 
177deae26bfSKyle McMartin /* N.B. The bits are defined in terms of a 32 bit word above, so the */
178deae26bfSKyle McMartin /*      following macro is ok for both 32 and 64 bit.                */
179deae26bfSKyle McMartin 
180deae26bfSKyle McMartin #define xlate_pabit(x) (31 - x)
181deae26bfSKyle McMartin 
182deae26bfSKyle McMartin /* this defines the shift to the usable bits in the PTE it is set so
183deae26bfSKyle McMartin  * that the valid bits _PAGE_PRESENT_BIT and _PAGE_USER_BIT are set
184deae26bfSKyle McMartin  * to zero */
185deae26bfSKyle McMartin #define PTE_SHIFT	   	xlate_pabit(_PAGE_USER_BIT)
186deae26bfSKyle McMartin 
187deae26bfSKyle McMartin /* PFN_PTE_SHIFT defines the shift of a PTE value to access the PFN field */
188deae26bfSKyle McMartin #define PFN_PTE_SHIFT		12
189deae26bfSKyle McMartin 
190deae26bfSKyle McMartin #define _PAGE_READ     (1 << xlate_pabit(_PAGE_READ_BIT))
191deae26bfSKyle McMartin #define _PAGE_WRITE    (1 << xlate_pabit(_PAGE_WRITE_BIT))
192deae26bfSKyle McMartin #define _PAGE_RW       (_PAGE_READ | _PAGE_WRITE)
193deae26bfSKyle McMartin #define _PAGE_EXEC     (1 << xlate_pabit(_PAGE_EXEC_BIT))
194deae26bfSKyle McMartin #define _PAGE_GATEWAY  (1 << xlate_pabit(_PAGE_GATEWAY_BIT))
195deae26bfSKyle McMartin #define _PAGE_DMB      (1 << xlate_pabit(_PAGE_DMB_BIT))
196deae26bfSKyle McMartin #define _PAGE_DIRTY    (1 << xlate_pabit(_PAGE_DIRTY_BIT))
197deae26bfSKyle McMartin #define _PAGE_REFTRAP  (1 << xlate_pabit(_PAGE_REFTRAP_BIT))
198deae26bfSKyle McMartin #define _PAGE_NO_CACHE (1 << xlate_pabit(_PAGE_NO_CACHE_BIT))
199deae26bfSKyle McMartin #define _PAGE_ACCESSED (1 << xlate_pabit(_PAGE_ACCESSED_BIT))
200deae26bfSKyle McMartin #define _PAGE_PRESENT  (1 << xlate_pabit(_PAGE_PRESENT_BIT))
2011f25ad26SHelge Deller #define _PAGE_HUGE     (1 << xlate_pabit(_PAGE_HPAGE_BIT))
202deae26bfSKyle McMartin #define _PAGE_USER     (1 << xlate_pabit(_PAGE_USER_BIT))
20370be49f2SHelge Deller #define _PAGE_SPECIAL  (1 << xlate_pabit(_PAGE_SPECIAL_BIT))
204deae26bfSKyle McMartin 
205deae26bfSKyle McMartin #define _PAGE_TABLE	(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | _PAGE_DIRTY | _PAGE_ACCESSED)
206df24e178SHelge Deller #define _PAGE_CHG_MASK	(PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY | _PAGE_SPECIAL)
207d7dd2ff1SJames Bottomley #define _PAGE_KERNEL_RO	(_PAGE_PRESENT | _PAGE_READ | _PAGE_DIRTY | _PAGE_ACCESSED)
208d7dd2ff1SJames Bottomley #define _PAGE_KERNEL_EXEC	(_PAGE_KERNEL_RO | _PAGE_EXEC)
209d7dd2ff1SJames Bottomley #define _PAGE_KERNEL_RWX	(_PAGE_KERNEL_EXEC | _PAGE_WRITE)
210d7dd2ff1SJames Bottomley #define _PAGE_KERNEL		(_PAGE_KERNEL_RO | _PAGE_WRITE)
211deae26bfSKyle McMartin 
2126d239fc7SDavid Hildenbrand /* We borrow bit 23 to store the exclusive marker in swap PTEs. */
2136d239fc7SDavid Hildenbrand #define _PAGE_SWP_EXCLUSIVE	_PAGE_ACCESSED
2146d239fc7SDavid Hildenbrand 
215deae26bfSKyle McMartin /* The pgd/pmd contains a ptr (in phys addr space); since all pgds/pmds
216deae26bfSKyle McMartin  * are page-aligned, we don't care about the PAGE_OFFSET bits, except
217deae26bfSKyle McMartin  * for a few meta-information bits, so we shift the address to be
218deae26bfSKyle McMartin  * able to effectively address 40/42/44-bits of physical address space
219deae26bfSKyle McMartin  * depending on 4k/16k/64k PAGE_SIZE */
220deae26bfSKyle McMartin #define _PxD_PRESENT_BIT   31
221b7795074SHelge Deller #define _PxD_VALID_BIT     30
222deae26bfSKyle McMartin 
223deae26bfSKyle McMartin #define PxD_FLAG_PRESENT  (1 << xlate_pabit(_PxD_PRESENT_BIT))
224deae26bfSKyle McMartin #define PxD_FLAG_VALID    (1 << xlate_pabit(_PxD_VALID_BIT))
225deae26bfSKyle McMartin #define PxD_FLAG_MASK     (0xf)
226deae26bfSKyle McMartin #define PxD_FLAG_SHIFT    (4)
2271f25ad26SHelge Deller #define PxD_VALUE_SHIFT   (PFN_PTE_SHIFT-PxD_FLAG_SHIFT)
228deae26bfSKyle McMartin 
229deae26bfSKyle McMartin #ifndef __ASSEMBLY__
230deae26bfSKyle McMartin 
2314dd5b673SJohn David Anglin #define PAGE_NONE	__pgprot(_PAGE_PRESENT | _PAGE_USER)
2324dd5b673SJohn David Anglin #define PAGE_SHARED	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | _PAGE_WRITE)
233deae26bfSKyle McMartin /* Others seem to make this executable, I don't know if that's correct
234deae26bfSKyle McMartin    or not.  The stack is mapped this way though so this is necessary
235deae26bfSKyle McMartin    in the short term - dhd@linuxcare.com, 2000-08-08 */
2364dd5b673SJohn David Anglin #define PAGE_READONLY	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ)
2374dd5b673SJohn David Anglin #define PAGE_WRITEONLY  __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITE)
2384dd5b673SJohn David Anglin #define PAGE_EXECREAD   __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | _PAGE_EXEC)
239deae26bfSKyle McMartin #define PAGE_COPY       PAGE_EXECREAD
2404dd5b673SJohn David Anglin #define PAGE_RWX        __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_READ | _PAGE_WRITE | _PAGE_EXEC)
241deae26bfSKyle McMartin #define PAGE_KERNEL	__pgprot(_PAGE_KERNEL)
242d7dd2ff1SJames Bottomley #define PAGE_KERNEL_EXEC	__pgprot(_PAGE_KERNEL_EXEC)
243d7dd2ff1SJames Bottomley #define PAGE_KERNEL_RWX	__pgprot(_PAGE_KERNEL_RWX)
244d7dd2ff1SJames Bottomley #define PAGE_KERNEL_RO	__pgprot(_PAGE_KERNEL_RO)
245deae26bfSKyle McMartin #define PAGE_KERNEL_UNC	__pgprot(_PAGE_KERNEL | _PAGE_NO_CACHE)
2464dd5b673SJohn David Anglin #define PAGE_GATEWAY    __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_GATEWAY| _PAGE_READ)
247deae26bfSKyle McMartin 
248deae26bfSKyle McMartin 
249deae26bfSKyle McMartin /*
250deae26bfSKyle McMartin  * We could have an execute only page using "gateway - promote to priv
251deae26bfSKyle McMartin  * level 3", but that is kind of silly. So, the way things are defined
252deae26bfSKyle McMartin  * now, we must always have read permission for pages with execute
253deae26bfSKyle McMartin  * permission. For the fun of it we'll go ahead and support write only
254deae26bfSKyle McMartin  * pages.
255deae26bfSKyle McMartin  */
256deae26bfSKyle McMartin 
257deae26bfSKyle McMartin 	 /*xwr*/
258deae26bfSKyle McMartin 
259deae26bfSKyle McMartin extern pgd_t swapper_pg_dir[]; /* declared in init_task.c */
260deae26bfSKyle McMartin 
261deae26bfSKyle McMartin /* initial page tables for 0-8MB for kernel */
262deae26bfSKyle McMartin 
263deae26bfSKyle McMartin extern pte_t pg0[];
264deae26bfSKyle McMartin 
265deae26bfSKyle McMartin /* zero page used for uninitialized stuff */
266deae26bfSKyle McMartin 
267deae26bfSKyle McMartin extern unsigned long *empty_zero_page;
268deae26bfSKyle McMartin 
269deae26bfSKyle McMartin /*
270deae26bfSKyle McMartin  * ZERO_PAGE is a global shared page that is always zero: used
271deae26bfSKyle McMartin  * for zero-mapped memory areas etc..
272deae26bfSKyle McMartin  */
273deae26bfSKyle McMartin 
274deae26bfSKyle McMartin #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
275deae26bfSKyle McMartin 
2768b4ae334SJames Bottomley #define pte_none(x)     (pte_val(x) == 0)
277deae26bfSKyle McMartin #define pte_present(x)	(pte_val(x) & _PAGE_PRESENT)
27838860b2cSJohn David Anglin #define pte_user(x)	(pte_val(x) & _PAGE_USER)
279e70bbca6SMatthew Wilcox (Oracle) #define pte_clear(mm, addr, xp)  set_pte(xp, __pte(0))
280deae26bfSKyle McMartin 
281deae26bfSKyle McMartin #define pmd_flag(x)	(pmd_val(x) & PxD_FLAG_MASK)
282deae26bfSKyle McMartin #define pmd_address(x)	((unsigned long)(pmd_val(x) &~ PxD_FLAG_MASK) << PxD_VALUE_SHIFT)
283d96885e2SMike Rapoport #define pud_flag(x)	(pud_val(x) & PxD_FLAG_MASK)
284d96885e2SMike Rapoport #define pud_address(x)	((unsigned long)(pud_val(x) &~ PxD_FLAG_MASK) << PxD_VALUE_SHIFT)
285deae26bfSKyle McMartin #define pgd_flag(x)	(pgd_val(x) & PxD_FLAG_MASK)
286deae26bfSKyle McMartin #define pgd_address(x)	((unsigned long)(pgd_val(x) &~ PxD_FLAG_MASK) << PxD_VALUE_SHIFT)
287deae26bfSKyle McMartin 
288deae26bfSKyle McMartin #define pmd_none(x)	(!pmd_val(x))
289deae26bfSKyle McMartin #define pmd_bad(x)	(!(pmd_flag(x) & PxD_FLAG_VALID))
290deae26bfSKyle McMartin #define pmd_present(x)	(pmd_flag(x) & PxD_FLAG_PRESENT)
pmd_clear(pmd_t * pmd)291deae26bfSKyle McMartin static inline void pmd_clear(pmd_t *pmd) {
292d96885e2SMike Rapoport 		set_pmd(pmd,  __pmd(0));
293deae26bfSKyle McMartin }
294deae26bfSKyle McMartin 
295deae26bfSKyle McMartin 
296deae26bfSKyle McMartin 
297f24ffde4SKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS == 3
2989cf6fa24SAneesh Kumar K.V #define pud_pgtable(pud) ((pmd_t *) __va(pud_address(pud)))
2999cf6fa24SAneesh Kumar K.V #define pud_page(pud)	virt_to_page((void *)pud_pgtable(pud))
300deae26bfSKyle McMartin 
301deae26bfSKyle McMartin /* For 64 bit we have three level tables */
302deae26bfSKyle McMartin 
303d96885e2SMike Rapoport #define pud_none(x)     (!pud_val(x))
304d96885e2SMike Rapoport #define pud_bad(x)      (!(pud_flag(x) & PxD_FLAG_VALID))
305d96885e2SMike Rapoport #define pud_present(x)  (pud_flag(x) & PxD_FLAG_PRESENT)
pud_clear(pud_t * pud)306d96885e2SMike Rapoport static inline void pud_clear(pud_t *pud) {
307d96885e2SMike Rapoport 	set_pud(pud, __pud(0));
308deae26bfSKyle McMartin }
309deae26bfSKyle McMartin #endif
310deae26bfSKyle McMartin 
311deae26bfSKyle McMartin /*
312deae26bfSKyle McMartin  * The following only work if pte_present() is true.
313deae26bfSKyle McMartin  * Undefined behaviour if not..
314deae26bfSKyle McMartin  */
pte_dirty(pte_t pte)315deae26bfSKyle McMartin static inline int pte_dirty(pte_t pte)		{ return pte_val(pte) & _PAGE_DIRTY; }
pte_young(pte_t pte)316deae26bfSKyle McMartin static inline int pte_young(pte_t pte)		{ return pte_val(pte) & _PAGE_ACCESSED; }
pte_write(pte_t pte)317deae26bfSKyle McMartin static inline int pte_write(pte_t pte)		{ return pte_val(pte) & _PAGE_WRITE; }
pte_special(pte_t pte)318df24e178SHelge Deller static inline int pte_special(pte_t pte)	{ return pte_val(pte) & _PAGE_SPECIAL; }
319deae26bfSKyle McMartin 
pte_mkclean(pte_t pte)320deae26bfSKyle McMartin static inline pte_t pte_mkclean(pte_t pte)	{ pte_val(pte) &= ~_PAGE_DIRTY; return pte; }
pte_mkold(pte_t pte)321deae26bfSKyle McMartin static inline pte_t pte_mkold(pte_t pte)	{ pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
pte_wrprotect(pte_t pte)322deae26bfSKyle McMartin static inline pte_t pte_wrprotect(pte_t pte)	{ pte_val(pte) &= ~_PAGE_WRITE; return pte; }
pte_mkdirty(pte_t pte)323deae26bfSKyle McMartin static inline pte_t pte_mkdirty(pte_t pte)	{ pte_val(pte) |= _PAGE_DIRTY; return pte; }
pte_mkyoung(pte_t pte)324deae26bfSKyle McMartin static inline pte_t pte_mkyoung(pte_t pte)	{ pte_val(pte) |= _PAGE_ACCESSED; return pte; }
pte_mkwrite_novma(pte_t pte)3252f0584f3SRick Edgecombe static inline pte_t pte_mkwrite_novma(pte_t pte)	{ pte_val(pte) |= _PAGE_WRITE; return pte; }
pte_mkspecial(pte_t pte)326df24e178SHelge Deller static inline pte_t pte_mkspecial(pte_t pte)	{ pte_val(pte) |= _PAGE_SPECIAL; return pte; }
327deae26bfSKyle McMartin 
328deae26bfSKyle McMartin /*
3291f25ad26SHelge Deller  * Huge pte definitions.
3301f25ad26SHelge Deller  */
3311f25ad26SHelge Deller #ifdef CONFIG_HUGETLB_PAGE
3321f25ad26SHelge Deller #define pte_huge(pte)           (pte_val(pte) & _PAGE_HUGE)
33378c0cbffSHelge Deller #define pte_mkhuge(pte)         (__pte(pte_val(pte) | \
33478c0cbffSHelge Deller 				 (parisc_requires_coherency() ? 0 : _PAGE_HUGE)))
3351f25ad26SHelge Deller #else
3361f25ad26SHelge Deller #define pte_huge(pte)           (0)
3371f25ad26SHelge Deller #define pte_mkhuge(pte)         (pte)
3381f25ad26SHelge Deller #endif
3391f25ad26SHelge Deller 
3401f25ad26SHelge Deller 
3411f25ad26SHelge Deller /*
342deae26bfSKyle McMartin  * Conversion functions: convert a page and protection to a page entry,
343deae26bfSKyle McMartin  * and a page entry and page directory to the page they refer to.
344deae26bfSKyle McMartin  */
345deae26bfSKyle McMartin #define __mk_pte(addr,pgprot) \
346deae26bfSKyle McMartin ({									\
347deae26bfSKyle McMartin 	pte_t __pte;							\
348deae26bfSKyle McMartin 									\
349deae26bfSKyle McMartin 	pte_val(__pte) = ((((addr)>>PAGE_SHIFT)<<PFN_PTE_SHIFT) + pgprot_val(pgprot));	\
350deae26bfSKyle McMartin 									\
351deae26bfSKyle McMartin 	__pte;								\
352deae26bfSKyle McMartin })
353deae26bfSKyle McMartin 
354deae26bfSKyle McMartin #define mk_pte(page, pgprot)	pfn_pte(page_to_pfn(page), (pgprot))
355deae26bfSKyle McMartin 
pfn_pte(unsigned long pfn,pgprot_t pgprot)356deae26bfSKyle McMartin static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot)
357deae26bfSKyle McMartin {
358deae26bfSKyle McMartin 	pte_t pte;
359deae26bfSKyle McMartin 	pte_val(pte) = (pfn << PFN_PTE_SHIFT) | pgprot_val(pgprot);
360deae26bfSKyle McMartin 	return pte;
361deae26bfSKyle McMartin }
362deae26bfSKyle McMartin 
pte_modify(pte_t pte,pgprot_t newprot)363deae26bfSKyle McMartin static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
364deae26bfSKyle McMartin { pte_val(pte) = (pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot); return pte; }
365deae26bfSKyle McMartin 
366deae26bfSKyle McMartin /* Permanent address of a page.  On parisc we don't have highmem. */
367deae26bfSKyle McMartin 
368deae26bfSKyle McMartin #define pte_pfn(x)		(pte_val(x) >> PFN_PTE_SHIFT)
369deae26bfSKyle McMartin 
370deae26bfSKyle McMartin #define pte_page(pte)		(pfn_to_page(pte_pfn(pte)))
371deae26bfSKyle McMartin 
pmd_page_vaddr(pmd_t pmd)372974b9b2cSMike Rapoport static inline unsigned long pmd_page_vaddr(pmd_t pmd)
373974b9b2cSMike Rapoport {
374974b9b2cSMike Rapoport 	return ((unsigned long) __va(pmd_address(pmd)));
375974b9b2cSMike Rapoport }
376deae26bfSKyle McMartin 
3777106c51eSMike Rapoport #define pmd_pfn(pmd)	(pmd_address(pmd) >> PAGE_SHIFT)
378deae26bfSKyle McMartin #define __pmd_page(pmd) ((unsigned long) __va(pmd_address(pmd)))
379deae26bfSKyle McMartin #define pmd_page(pmd)	virt_to_page((void *)__pmd_page(pmd))
380deae26bfSKyle McMartin 
381deae26bfSKyle McMartin /* Find an entry in the second-level page table.. */
382deae26bfSKyle McMartin 
383deae26bfSKyle McMartin extern void paging_init (void);
384deae26bfSKyle McMartin 
set_ptes(struct mm_struct * mm,unsigned long addr,pte_t * ptep,pte_t pte,unsigned int nr)385e70bbca6SMatthew Wilcox (Oracle) static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
386e70bbca6SMatthew Wilcox (Oracle) 		pte_t *ptep, pte_t pte, unsigned int nr)
387e70bbca6SMatthew Wilcox (Oracle) {
388e70bbca6SMatthew Wilcox (Oracle) 	if (pte_present(pte) && pte_user(pte))
389e70bbca6SMatthew Wilcox (Oracle) 		__update_cache(pte);
390e70bbca6SMatthew Wilcox (Oracle) 	for (;;) {
391e70bbca6SMatthew Wilcox (Oracle) 		*ptep = pte;
392e70bbca6SMatthew Wilcox (Oracle) 		purge_tlb_entries(mm, addr);
393e70bbca6SMatthew Wilcox (Oracle) 		if (--nr == 0)
394e70bbca6SMatthew Wilcox (Oracle) 			break;
395e70bbca6SMatthew Wilcox (Oracle) 		ptep++;
396e70bbca6SMatthew Wilcox (Oracle) 		pte_val(pte) += 1 << PFN_PTE_SHIFT;
397e70bbca6SMatthew Wilcox (Oracle) 		addr += PAGE_SIZE;
398e70bbca6SMatthew Wilcox (Oracle) 	}
399e70bbca6SMatthew Wilcox (Oracle) }
400e70bbca6SMatthew Wilcox (Oracle) #define set_ptes set_ptes
401e70bbca6SMatthew Wilcox (Oracle) 
402deae26bfSKyle McMartin /* Used for deferring calls to flush_dcache_page() */
403deae26bfSKyle McMartin 
404deae26bfSKyle McMartin #define PG_dcache_dirty         PG_arch_1
405deae26bfSKyle McMartin 
406e70bbca6SMatthew Wilcox (Oracle) #define update_mmu_cache_range(vmf, vma, addr, ptep, nr) __update_cache(*ptep)
407e70bbca6SMatthew Wilcox (Oracle) #define update_mmu_cache(vma, addr, ptep) __update_cache(*ptep)
408deae26bfSKyle McMartin 
4096d239fc7SDavid Hildenbrand /*
4106d239fc7SDavid Hildenbrand  * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
4116d239fc7SDavid Hildenbrand  * are !pte_none() && !pte_present().
4126d239fc7SDavid Hildenbrand  *
4136d239fc7SDavid Hildenbrand  * Format of swap PTEs (32bit):
4146d239fc7SDavid Hildenbrand  *
4156d239fc7SDavid Hildenbrand  *                         1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
4166d239fc7SDavid Hildenbrand  *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
4176d239fc7SDavid Hildenbrand  *   <---------------- offset -----------------> P E <ofs> < type ->
4186d239fc7SDavid Hildenbrand  *
4196d239fc7SDavid Hildenbrand  *   E is the exclusive marker that is not stored in swap entries.
4206d239fc7SDavid Hildenbrand  *   _PAGE_PRESENT (P) must be 0.
4216d239fc7SDavid Hildenbrand  *
4226d239fc7SDavid Hildenbrand  *   For the 64bit version, the offset is extended by 32bit.
4236d239fc7SDavid Hildenbrand  */
424deae26bfSKyle McMartin #define __swp_type(x)                     ((x).val & 0x1f)
4256f9e9884SHelge Deller #define __swp_offset(x)                   ( (((x).val >> 5) & 0x7) | \
4266f9e9884SHelge Deller 					  (((x).val >> 10) << 3) )
4276d239fc7SDavid Hildenbrand #define __swp_entry(type, offset)         ((swp_entry_t) { \
4286d239fc7SDavid Hildenbrand 					    ((type) & 0x1f) | \
4296f9e9884SHelge Deller 					    ((offset & 0x7) << 5) | \
4306f9e9884SHelge Deller 					    ((offset >> 3) << 10) })
431deae26bfSKyle McMartin #define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) })
432deae26bfSKyle McMartin #define __swp_entry_to_pte(x)		((pte_t) { (x).val })
433deae26bfSKyle McMartin 
pte_swp_exclusive(pte_t pte)4346d239fc7SDavid Hildenbrand static inline int pte_swp_exclusive(pte_t pte)
4356d239fc7SDavid Hildenbrand {
4366d239fc7SDavid Hildenbrand 	return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
4376d239fc7SDavid Hildenbrand }
4386d239fc7SDavid Hildenbrand 
pte_swp_mkexclusive(pte_t pte)4396d239fc7SDavid Hildenbrand static inline pte_t pte_swp_mkexclusive(pte_t pte)
4406d239fc7SDavid Hildenbrand {
4416d239fc7SDavid Hildenbrand 	pte_val(pte) |= _PAGE_SWP_EXCLUSIVE;
4426d239fc7SDavid Hildenbrand 	return pte;
4436d239fc7SDavid Hildenbrand }
4446d239fc7SDavid Hildenbrand 
pte_swp_clear_exclusive(pte_t pte)4456d239fc7SDavid Hildenbrand static inline pte_t pte_swp_clear_exclusive(pte_t pte)
4466d239fc7SDavid Hildenbrand {
4476d239fc7SDavid Hildenbrand 	pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE;
4486d239fc7SDavid Hildenbrand 	return pte;
4496d239fc7SDavid Hildenbrand }
4506d239fc7SDavid Hildenbrand 
ptep_get(pte_t * ptep)4515bf196f1SJohn David Anglin static inline pte_t ptep_get(pte_t *ptep)
4525bf196f1SJohn David Anglin {
4535bf196f1SJohn David Anglin 	return READ_ONCE(*ptep);
4545bf196f1SJohn David Anglin }
4555bf196f1SJohn David Anglin #define ptep_get ptep_get
4565bf196f1SJohn David Anglin 
ptep_test_and_clear_young(struct vm_area_struct * vma,unsigned long addr,pte_t * ptep)457deae26bfSKyle McMartin static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep)
458deae26bfSKyle McMartin {
459bda079d3SJohn David Anglin 	pte_t pte;
460bda079d3SJohn David Anglin 
4615bf196f1SJohn David Anglin 	pte = ptep_get(ptep);
462bda079d3SJohn David Anglin 	if (!pte_young(pte)) {
463bda079d3SJohn David Anglin 		return 0;
464bda079d3SJohn David Anglin 	}
465e70bbca6SMatthew Wilcox (Oracle) 	set_pte(ptep, pte_mkold(pte));
466bda079d3SJohn David Anglin 	return 1;
467bda079d3SJohn David Anglin }
468deae26bfSKyle McMartin 
4695bf196f1SJohn David Anglin int ptep_clear_flush_young(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep);
4705bf196f1SJohn David Anglin pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptep);
4715bf196f1SJohn David Anglin 
472deae26bfSKyle McMartin struct mm_struct;
ptep_set_wrprotect(struct mm_struct * mm,unsigned long addr,pte_t * ptep)473deae26bfSKyle McMartin static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
474deae26bfSKyle McMartin {
475e70bbca6SMatthew Wilcox (Oracle) 	set_pte(ptep, pte_wrprotect(*ptep));
476deae26bfSKyle McMartin }
477deae26bfSKyle McMartin 
478deae26bfSKyle McMartin #define pte_same(A,B)	(pte_val(A) == pte_val(B))
479deae26bfSKyle McMartin 
480deae26bfSKyle McMartin #endif /* !__ASSEMBLY__ */
481deae26bfSKyle McMartin 
482deae26bfSKyle McMartin 
483deae26bfSKyle McMartin /* TLB page size encoding - see table 3-1 in parisc20.pdf */
484deae26bfSKyle McMartin #define _PAGE_SIZE_ENCODING_4K		0
485deae26bfSKyle McMartin #define _PAGE_SIZE_ENCODING_16K		1
486deae26bfSKyle McMartin #define _PAGE_SIZE_ENCODING_64K		2
487deae26bfSKyle McMartin #define _PAGE_SIZE_ENCODING_256K	3
488deae26bfSKyle McMartin #define _PAGE_SIZE_ENCODING_1M		4
489deae26bfSKyle McMartin #define _PAGE_SIZE_ENCODING_4M		5
490deae26bfSKyle McMartin #define _PAGE_SIZE_ENCODING_16M		6
491deae26bfSKyle McMartin #define _PAGE_SIZE_ENCODING_64M		7
492deae26bfSKyle McMartin 
493deae26bfSKyle McMartin #if defined(CONFIG_PARISC_PAGE_SIZE_4KB)
494deae26bfSKyle McMartin # define _PAGE_SIZE_ENCODING_DEFAULT _PAGE_SIZE_ENCODING_4K
495deae26bfSKyle McMartin #elif defined(CONFIG_PARISC_PAGE_SIZE_16KB)
496deae26bfSKyle McMartin # define _PAGE_SIZE_ENCODING_DEFAULT _PAGE_SIZE_ENCODING_16K
497deae26bfSKyle McMartin #elif defined(CONFIG_PARISC_PAGE_SIZE_64KB)
498deae26bfSKyle McMartin # define _PAGE_SIZE_ENCODING_DEFAULT _PAGE_SIZE_ENCODING_64K
499deae26bfSKyle McMartin #endif
500deae26bfSKyle McMartin 
501deae26bfSKyle McMartin 
502deae26bfSKyle McMartin #define pgprot_noncached(prot) __pgprot(pgprot_val(prot) | _PAGE_NO_CACHE)
503deae26bfSKyle McMartin 
504deae26bfSKyle McMartin /* We provide our own get_unmapped_area to provide cache coherency */
505deae26bfSKyle McMartin 
506deae26bfSKyle McMartin #define HAVE_ARCH_UNMAPPED_AREA
5079dabf60dSHelge Deller #define HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
508deae26bfSKyle McMartin 
509deae26bfSKyle McMartin #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
5105bf196f1SJohn David Anglin #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
5115bf196f1SJohn David Anglin #define __HAVE_ARCH_PTEP_CLEAR_FLUSH
512deae26bfSKyle McMartin #define __HAVE_ARCH_PTEP_SET_WRPROTECT
513deae26bfSKyle McMartin #define __HAVE_ARCH_PTE_SAME
514deae26bfSKyle McMartin 
515deae26bfSKyle McMartin #endif /* _PARISC_PGTABLE_H */
516