1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 #ifndef _ASM_PGTABLE_BITS_H
6 #define _ASM_PGTABLE_BITS_H
7 
8 /* Page table bits */
9 #define	_PAGE_VALID_SHIFT	0
10 #define	_PAGE_ACCESSED_SHIFT	0  /* Reuse Valid for Accessed */
11 #define	_PAGE_DIRTY_SHIFT	1
12 #define	_PAGE_PLV_SHIFT		2  /* 2~3, two bits */
13 #define	_CACHE_SHIFT		4  /* 4~5, two bits */
14 #define	_PAGE_GLOBAL_SHIFT	6
15 #define	_PAGE_HUGE_SHIFT	6  /* HUGE is a PMD bit */
16 #define	_PAGE_PRESENT_SHIFT	7
17 #define	_PAGE_WRITE_SHIFT	8
18 #define	_PAGE_MODIFIED_SHIFT	9
19 #define	_PAGE_PROTNONE_SHIFT	10
20 #define	_PAGE_SPECIAL_SHIFT	11
21 #define	_PAGE_HGLOBAL_SHIFT	12 /* HGlobal is a PMD bit */
22 #define	_PAGE_PFN_SHIFT		12
23 #define	_PAGE_SWP_EXCLUSIVE_SHIFT 23
24 #define	_PAGE_PFN_END_SHIFT	48
25 #define	_PAGE_PRESENT_INVALID_SHIFT 60
26 #define	_PAGE_NO_READ_SHIFT	61
27 #define	_PAGE_NO_EXEC_SHIFT	62
28 #define	_PAGE_RPLV_SHIFT	63
29 
30 /* Used by software */
31 #define _PAGE_PRESENT		(_ULCAST_(1) << _PAGE_PRESENT_SHIFT)
32 #define _PAGE_PRESENT_INVALID	(_ULCAST_(1) << _PAGE_PRESENT_INVALID_SHIFT)
33 #define _PAGE_WRITE		(_ULCAST_(1) << _PAGE_WRITE_SHIFT)
34 #define _PAGE_ACCESSED		(_ULCAST_(1) << _PAGE_ACCESSED_SHIFT)
35 #define _PAGE_MODIFIED		(_ULCAST_(1) << _PAGE_MODIFIED_SHIFT)
36 #define _PAGE_PROTNONE		(_ULCAST_(1) << _PAGE_PROTNONE_SHIFT)
37 #define _PAGE_SPECIAL		(_ULCAST_(1) << _PAGE_SPECIAL_SHIFT)
38 
39 /* We borrow bit 23 to store the exclusive marker in swap PTEs. */
40 #define _PAGE_SWP_EXCLUSIVE	(_ULCAST_(1) << _PAGE_SWP_EXCLUSIVE_SHIFT)
41 
42 /* Used by TLB hardware (placed in EntryLo*) */
43 #define _PAGE_VALID		(_ULCAST_(1) << _PAGE_VALID_SHIFT)
44 #define _PAGE_DIRTY		(_ULCAST_(1) << _PAGE_DIRTY_SHIFT)
45 #define _PAGE_PLV		(_ULCAST_(3) << _PAGE_PLV_SHIFT)
46 #define _PAGE_GLOBAL		(_ULCAST_(1) << _PAGE_GLOBAL_SHIFT)
47 #define _PAGE_HUGE		(_ULCAST_(1) << _PAGE_HUGE_SHIFT)
48 #define _PAGE_HGLOBAL		(_ULCAST_(1) << _PAGE_HGLOBAL_SHIFT)
49 #define _PAGE_NO_READ		(_ULCAST_(1) << _PAGE_NO_READ_SHIFT)
50 #define _PAGE_NO_EXEC		(_ULCAST_(1) << _PAGE_NO_EXEC_SHIFT)
51 #define _PAGE_RPLV		(_ULCAST_(1) << _PAGE_RPLV_SHIFT)
52 #define _CACHE_MASK		(_ULCAST_(3) << _CACHE_SHIFT)
53 #define PFN_PTE_SHIFT		(PAGE_SHIFT - 12 + _PAGE_PFN_SHIFT)
54 
55 #define _PAGE_USER	(PLV_USER << _PAGE_PLV_SHIFT)
56 #define _PAGE_KERN	(PLV_KERN << _PAGE_PLV_SHIFT)
57 
58 #define _PFN_MASK (~((_ULCAST_(1) << (PFN_PTE_SHIFT)) - 1) & \
59 		  ((_ULCAST_(1) << (_PAGE_PFN_END_SHIFT)) - 1))
60 
61 /*
62  * Cache attributes
63  */
64 #ifndef _CACHE_SUC
65 #define _CACHE_SUC			(0<<_CACHE_SHIFT) /* Strong-ordered UnCached */
66 #endif
67 #ifndef _CACHE_CC
68 #define _CACHE_CC			(1<<_CACHE_SHIFT) /* Coherent Cached */
69 #endif
70 #ifndef _CACHE_WUC
71 #define _CACHE_WUC			(2<<_CACHE_SHIFT) /* Weak-ordered UnCached */
72 #endif
73 
74 #define __READABLE	(_PAGE_VALID)
75 #define __WRITEABLE	(_PAGE_DIRTY | _PAGE_WRITE)
76 
77 #define _PAGE_CHG_MASK	(_PAGE_MODIFIED | _PAGE_SPECIAL | _PFN_MASK | _CACHE_MASK | _PAGE_PLV)
78 #define _HPAGE_CHG_MASK	(_PAGE_MODIFIED | _PAGE_SPECIAL | _PFN_MASK | _CACHE_MASK | _PAGE_PLV | _PAGE_HUGE)
79 
80 #define PAGE_NONE	__pgprot(_PAGE_PROTNONE | _PAGE_NO_READ | \
81 				 _PAGE_USER | _CACHE_CC)
82 #define PAGE_SHARED	__pgprot(_PAGE_PRESENT | _PAGE_WRITE | \
83 				 _PAGE_USER | _CACHE_CC)
84 #define PAGE_READONLY	__pgprot(_PAGE_PRESENT | _PAGE_USER | _CACHE_CC)
85 
86 #define PAGE_KERNEL	__pgprot(_PAGE_PRESENT | __READABLE | __WRITEABLE | \
87 				 _PAGE_GLOBAL | _PAGE_KERN | _CACHE_CC)
88 #define PAGE_KERNEL_SUC __pgprot(_PAGE_PRESENT | __READABLE | __WRITEABLE | \
89 				 _PAGE_GLOBAL | _PAGE_KERN |  _CACHE_SUC)
90 #define PAGE_KERNEL_WUC __pgprot(_PAGE_PRESENT | __READABLE | __WRITEABLE | \
91 				 _PAGE_GLOBAL | _PAGE_KERN |  _CACHE_WUC)
92 
93 #ifndef __ASSEMBLY__
94 
95 #define _PAGE_IOREMAP		pgprot_val(PAGE_KERNEL_SUC)
96 
97 #define pgprot_noncached pgprot_noncached
98 
99 static inline pgprot_t pgprot_noncached(pgprot_t _prot)
100 {
101 	unsigned long prot = pgprot_val(_prot);
102 
103 	prot = (prot & ~_CACHE_MASK) | _CACHE_SUC;
104 
105 	return __pgprot(prot);
106 }
107 
108 extern bool wc_enabled;
109 
110 #define pgprot_writecombine pgprot_writecombine
111 
112 static inline pgprot_t pgprot_writecombine(pgprot_t _prot)
113 {
114 	unsigned long prot = pgprot_val(_prot);
115 
116 	prot = (prot & ~_CACHE_MASK) | (wc_enabled ? _CACHE_WUC : _CACHE_SUC);
117 
118 	return __pgprot(prot);
119 }
120 
121 #endif /* !__ASSEMBLY__ */
122 
123 #endif /* _ASM_PGTABLE_BITS_H */
124