xref: /openbmc/linux/arch/loongarch/include/asm/page.h (revision 2cc39179)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 #ifndef _ASM_PAGE_H
6 #define _ASM_PAGE_H
7 
8 #include <linux/const.h>
9 
10 /*
11  * PAGE_SHIFT determines the page size
12  */
13 #ifdef CONFIG_PAGE_SIZE_4KB
14 #define PAGE_SHIFT	12
15 #endif
16 #ifdef CONFIG_PAGE_SIZE_16KB
17 #define PAGE_SHIFT	14
18 #endif
19 #ifdef CONFIG_PAGE_SIZE_64KB
20 #define PAGE_SHIFT	16
21 #endif
22 #define PAGE_SIZE	(_AC(1, UL) << PAGE_SHIFT)
23 #define PAGE_MASK	(~(PAGE_SIZE - 1))
24 
25 #define HPAGE_SHIFT	(PAGE_SHIFT + PAGE_SHIFT - 3)
26 #define HPAGE_SIZE	(_AC(1, UL) << HPAGE_SHIFT)
27 #define HPAGE_MASK	(~(HPAGE_SIZE - 1))
28 #define HUGETLB_PAGE_ORDER	(HPAGE_SHIFT - PAGE_SHIFT)
29 
30 #ifndef __ASSEMBLY__
31 
32 #include <linux/kernel.h>
33 #include <linux/pfn.h>
34 
35 #define MAX_DMA32_PFN  (1UL << (32 - PAGE_SHIFT))
36 
37 /*
38  * It's normally defined only for FLATMEM config but it's
39  * used in our early mem init code for all memory models.
40  * So always define it.
41  */
42 #define ARCH_PFN_OFFSET	PFN_UP(PHYS_OFFSET)
43 
44 extern void clear_page(void *page);
45 extern void copy_page(void *to, void *from);
46 
47 #define clear_user_page(page, vaddr, pg)	clear_page(page)
48 #define copy_user_page(to, from, vaddr, pg)	copy_page(to, from)
49 
50 extern unsigned long shm_align_mask;
51 
52 struct page;
53 struct vm_area_struct;
54 void copy_user_highpage(struct page *to, struct page *from,
55 	      unsigned long vaddr, struct vm_area_struct *vma);
56 
57 #define __HAVE_ARCH_COPY_USER_HIGHPAGE
58 
59 typedef struct { unsigned long pte; } pte_t;
60 #define pte_val(x)	((x).pte)
61 #define __pte(x)	((pte_t) { (x) })
62 typedef struct page *pgtable_t;
63 
64 typedef struct { unsigned long pgd; } pgd_t;
65 #define pgd_val(x)	((x).pgd)
66 #define __pgd(x)	((pgd_t) { (x) })
67 
68 /*
69  * Manipulate page protection bits
70  */
71 typedef struct { unsigned long pgprot; } pgprot_t;
72 #define pgprot_val(x)	((x).pgprot)
73 #define __pgprot(x)	((pgprot_t) { (x) })
74 #define pte_pgprot(x)	__pgprot(pte_val(x) & ~_PFN_MASK)
75 
76 #define ptep_buddy(x)	((pte_t *)((unsigned long)(x) ^ sizeof(pte_t)))
77 
78 /*
79  * __pa()/__va() should be used only during mem init.
80  */
81 #define __pa(x)		PHYSADDR(x)
82 #define __va(x)		((void *)((unsigned long)(x) + PAGE_OFFSET - PHYS_OFFSET))
83 
84 #define pfn_to_kaddr(pfn)	__va((pfn) << PAGE_SHIFT)
85 
86 #ifdef CONFIG_FLATMEM
87 
88 static inline int pfn_valid(unsigned long pfn)
89 {
90 	/* avoid <linux/mm.h> include hell */
91 	extern unsigned long max_mapnr;
92 	unsigned long pfn_offset = ARCH_PFN_OFFSET;
93 
94 	return pfn >= pfn_offset && pfn < max_mapnr;
95 }
96 
97 #endif
98 
99 #define virt_to_pfn(kaddr)	PFN_DOWN(virt_to_phys((void *)(kaddr)))
100 #define virt_to_page(kaddr)	pfn_to_page(virt_to_pfn(kaddr))
101 
102 extern int __virt_addr_valid(volatile void *kaddr);
103 #define virt_addr_valid(kaddr)	__virt_addr_valid((volatile void *)(kaddr))
104 
105 #define VM_DATA_DEFAULT_FLAGS \
106 	(VM_READ | VM_WRITE | \
107 	 ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0) | \
108 	 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
109 
110 #include <asm-generic/memory_model.h>
111 #include <asm-generic/getorder.h>
112 
113 #endif /* !__ASSEMBLY__ */
114 
115 #endif /* _ASM_PAGE_H */
116