1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * arch/arm/include/asm/pgtable-2level.h
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 1995-2002 Russell King
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun #ifndef _ASM_PGTABLE_2LEVEL_H
8*4882a593Smuzhiyun #define _ASM_PGTABLE_2LEVEL_H
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #define __PAGETABLE_PMD_FOLDED 1
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun /*
13*4882a593Smuzhiyun * Hardware-wise, we have a two level page table structure, where the first
14*4882a593Smuzhiyun * level has 4096 entries, and the second level has 256 entries. Each entry
15*4882a593Smuzhiyun * is one 32-bit word. Most of the bits in the second level entry are used
16*4882a593Smuzhiyun * by hardware, and there aren't any "accessed" and "dirty" bits.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * Linux on the other hand has a three level page table structure, which can
19*4882a593Smuzhiyun * be wrapped to fit a two level page table structure easily - using the PGD
20*4882a593Smuzhiyun * and PTE only. However, Linux also expects one "PTE" table per page, and
21*4882a593Smuzhiyun * at least a "dirty" bit.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Therefore, we tweak the implementation slightly - we tell Linux that we
24*4882a593Smuzhiyun * have 2048 entries in the first level, each of which is 8 bytes (iow, two
25*4882a593Smuzhiyun * hardware pointers to the second level.) The second level contains two
26*4882a593Smuzhiyun * hardware PTE tables arranged contiguously, preceded by Linux versions
27*4882a593Smuzhiyun * which contain the state information Linux needs. We, therefore, end up
28*4882a593Smuzhiyun * with 512 entries in the "PTE" level.
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * This leads to the page tables having the following layout:
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * pgd pte
33*4882a593Smuzhiyun * | |
34*4882a593Smuzhiyun * +--------+
35*4882a593Smuzhiyun * | | +------------+ +0
36*4882a593Smuzhiyun * +- - - - + | Linux pt 0 |
37*4882a593Smuzhiyun * | | +------------+ +1024
38*4882a593Smuzhiyun * +--------+ +0 | Linux pt 1 |
39*4882a593Smuzhiyun * | |-----> +------------+ +2048
40*4882a593Smuzhiyun * +- - - - + +4 | h/w pt 0 |
41*4882a593Smuzhiyun * | |-----> +------------+ +3072
42*4882a593Smuzhiyun * +--------+ +8 | h/w pt 1 |
43*4882a593Smuzhiyun * | | +------------+ +4096
44*4882a593Smuzhiyun *
45*4882a593Smuzhiyun * See L_PTE_xxx below for definitions of bits in the "Linux pt", and
46*4882a593Smuzhiyun * PTE_xxx for definitions of bits appearing in the "h/w pt".
47*4882a593Smuzhiyun *
48*4882a593Smuzhiyun * PMD_xxx definitions refer to bits in the first level page table.
49*4882a593Smuzhiyun *
50*4882a593Smuzhiyun * The "dirty" bit is emulated by only granting hardware write permission
51*4882a593Smuzhiyun * iff the page is marked "writable" and "dirty" in the Linux PTE. This
52*4882a593Smuzhiyun * means that a write to a clean page will cause a permission fault, and
53*4882a593Smuzhiyun * the Linux MM layer will mark the page dirty via handle_pte_fault().
54*4882a593Smuzhiyun * For the hardware to notice the permission change, the TLB entry must
55*4882a593Smuzhiyun * be flushed, and ptep_set_access_flags() does that for us.
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * The "accessed" or "young" bit is emulated by a similar method; we only
58*4882a593Smuzhiyun * allow accesses to the page if the "young" bit is set. Accesses to the
59*4882a593Smuzhiyun * page will cause a fault, and handle_pte_fault() will set the young bit
60*4882a593Smuzhiyun * for us as long as the page is marked present in the corresponding Linux
61*4882a593Smuzhiyun * PTE entry. Again, ptep_set_access_flags() will ensure that the TLB is
62*4882a593Smuzhiyun * up to date.
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * However, when the "young" bit is cleared, we deny access to the page
65*4882a593Smuzhiyun * by clearing the hardware PTE. Currently Linux does not flush the TLB
66*4882a593Smuzhiyun * for us in this case, which means the TLB will retain the transation
67*4882a593Smuzhiyun * until either the TLB entry is evicted under pressure, or a context
68*4882a593Smuzhiyun * switch which changes the user space mapping occurs.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun #define PTRS_PER_PTE 512
71*4882a593Smuzhiyun #define PTRS_PER_PMD 1
72*4882a593Smuzhiyun #define PTRS_PER_PGD 2048
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun #define PTE_HWTABLE_PTRS (PTRS_PER_PTE)
75*4882a593Smuzhiyun #define PTE_HWTABLE_OFF (PTE_HWTABLE_PTRS * sizeof(pte_t))
76*4882a593Smuzhiyun #define PTE_HWTABLE_SIZE (PTRS_PER_PTE * sizeof(u32))
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #define MAX_POSSIBLE_PHYSMEM_BITS 32
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * PMD_SHIFT determines the size of the area a second-level page table can map
82*4882a593Smuzhiyun * PGDIR_SHIFT determines what a third-level page table entry can map
83*4882a593Smuzhiyun */
84*4882a593Smuzhiyun #define PMD_SHIFT 21
85*4882a593Smuzhiyun #define PGDIR_SHIFT 21
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun #define PMD_SIZE (1UL << PMD_SHIFT)
88*4882a593Smuzhiyun #define PMD_MASK (~(PMD_SIZE-1))
89*4882a593Smuzhiyun #define PGDIR_SIZE (1UL << PGDIR_SHIFT)
90*4882a593Smuzhiyun #define PGDIR_MASK (~(PGDIR_SIZE-1))
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * section address mask and size definitions.
94*4882a593Smuzhiyun */
95*4882a593Smuzhiyun #define SECTION_SHIFT 20
96*4882a593Smuzhiyun #define SECTION_SIZE (1UL << SECTION_SHIFT)
97*4882a593Smuzhiyun #define SECTION_MASK (~(SECTION_SIZE-1))
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun * ARMv6 supersection address mask and size definitions.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun #define SUPERSECTION_SHIFT 24
103*4882a593Smuzhiyun #define SUPERSECTION_SIZE (1UL << SUPERSECTION_SHIFT)
104*4882a593Smuzhiyun #define SUPERSECTION_MASK (~(SUPERSECTION_SIZE-1))
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun #define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * "Linux" PTE definitions.
110*4882a593Smuzhiyun *
111*4882a593Smuzhiyun * We keep two sets of PTEs - the hardware and the linux version.
112*4882a593Smuzhiyun * This allows greater flexibility in the way we map the Linux bits
113*4882a593Smuzhiyun * onto the hardware tables, and allows us to have YOUNG and DIRTY
114*4882a593Smuzhiyun * bits.
115*4882a593Smuzhiyun *
116*4882a593Smuzhiyun * The PTE table pointer refers to the hardware entries; the "Linux"
117*4882a593Smuzhiyun * entries are stored 1024 bytes below.
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun #define L_PTE_VALID (_AT(pteval_t, 1) << 0) /* Valid */
120*4882a593Smuzhiyun #define L_PTE_PRESENT (_AT(pteval_t, 1) << 0)
121*4882a593Smuzhiyun #define L_PTE_YOUNG (_AT(pteval_t, 1) << 1)
122*4882a593Smuzhiyun #define L_PTE_DIRTY (_AT(pteval_t, 1) << 6)
123*4882a593Smuzhiyun #define L_PTE_RDONLY (_AT(pteval_t, 1) << 7)
124*4882a593Smuzhiyun #define L_PTE_USER (_AT(pteval_t, 1) << 8)
125*4882a593Smuzhiyun #define L_PTE_XN (_AT(pteval_t, 1) << 9)
126*4882a593Smuzhiyun #define L_PTE_SHARED (_AT(pteval_t, 1) << 10) /* shared(v6), coherent(xsc3) */
127*4882a593Smuzhiyun #define L_PTE_NONE (_AT(pteval_t, 1) << 11)
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun * These are the memory types, defined to be compatible with
131*4882a593Smuzhiyun * pre-ARMv6 CPUs cacheable and bufferable bits: n/a,n/a,C,B
132*4882a593Smuzhiyun * ARMv6+ without TEX remapping, they are a table index.
133*4882a593Smuzhiyun * ARMv6+ with TEX remapping, they correspond to n/a,TEX(0),C,B
134*4882a593Smuzhiyun *
135*4882a593Smuzhiyun * MT type Pre-ARMv6 ARMv6+ type / cacheable status
136*4882a593Smuzhiyun * UNCACHED Uncached Strongly ordered
137*4882a593Smuzhiyun * BUFFERABLE Bufferable Normal memory / non-cacheable
138*4882a593Smuzhiyun * WRITETHROUGH Writethrough Normal memory / write through
139*4882a593Smuzhiyun * WRITEBACK Writeback Normal memory / write back, read alloc
140*4882a593Smuzhiyun * MINICACHE Minicache N/A
141*4882a593Smuzhiyun * WRITEALLOC Writeback Normal memory / write back, write alloc
142*4882a593Smuzhiyun * DEV_SHARED Uncached Device memory (shared)
143*4882a593Smuzhiyun * DEV_NONSHARED Uncached Device memory (non-shared)
144*4882a593Smuzhiyun * DEV_WC Bufferable Normal memory / non-cacheable
145*4882a593Smuzhiyun * DEV_CACHED Writeback Normal memory / write back, read alloc
146*4882a593Smuzhiyun * VECTORS Variable Normal memory / variable
147*4882a593Smuzhiyun *
148*4882a593Smuzhiyun * All normal memory mappings have the following properties:
149*4882a593Smuzhiyun * - reads can be repeated with no side effects
150*4882a593Smuzhiyun * - repeated reads return the last value written
151*4882a593Smuzhiyun * - reads can fetch additional locations without side effects
152*4882a593Smuzhiyun * - writes can be repeated (in certain cases) with no side effects
153*4882a593Smuzhiyun * - writes can be merged before accessing the target
154*4882a593Smuzhiyun * - unaligned accesses can be supported
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * All device mappings have the following properties:
157*4882a593Smuzhiyun * - no access speculation
158*4882a593Smuzhiyun * - no repetition (eg, on return from an exception)
159*4882a593Smuzhiyun * - number, order and size of accesses are maintained
160*4882a593Smuzhiyun * - unaligned accesses are "unpredictable"
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun #define L_PTE_MT_UNCACHED (_AT(pteval_t, 0x00) << 2) /* 0000 */
163*4882a593Smuzhiyun #define L_PTE_MT_BUFFERABLE (_AT(pteval_t, 0x01) << 2) /* 0001 */
164*4882a593Smuzhiyun #define L_PTE_MT_WRITETHROUGH (_AT(pteval_t, 0x02) << 2) /* 0010 */
165*4882a593Smuzhiyun #define L_PTE_MT_WRITEBACK (_AT(pteval_t, 0x03) << 2) /* 0011 */
166*4882a593Smuzhiyun #define L_PTE_MT_MINICACHE (_AT(pteval_t, 0x06) << 2) /* 0110 (sa1100, xscale) */
167*4882a593Smuzhiyun #define L_PTE_MT_WRITEALLOC (_AT(pteval_t, 0x07) << 2) /* 0111 */
168*4882a593Smuzhiyun #define L_PTE_MT_DEV_SHARED (_AT(pteval_t, 0x04) << 2) /* 0100 */
169*4882a593Smuzhiyun #define L_PTE_MT_DEV_NONSHARED (_AT(pteval_t, 0x0c) << 2) /* 1100 */
170*4882a593Smuzhiyun #define L_PTE_MT_DEV_WC (_AT(pteval_t, 0x09) << 2) /* 1001 */
171*4882a593Smuzhiyun #define L_PTE_MT_DEV_CACHED (_AT(pteval_t, 0x0b) << 2) /* 1011 */
172*4882a593Smuzhiyun #define L_PTE_MT_VECTORS (_AT(pteval_t, 0x0f) << 2) /* 1111 */
173*4882a593Smuzhiyun #define L_PTE_MT_MASK (_AT(pteval_t, 0x0f) << 2)
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun #ifndef __ASSEMBLY__
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * The "pud_xxx()" functions here are trivial when the pmd is folded into
179*4882a593Smuzhiyun * the pud: the pud entry is never bad, always exists, and can't be set or
180*4882a593Smuzhiyun * cleared.
181*4882a593Smuzhiyun */
182*4882a593Smuzhiyun #define pud_none(pud) (0)
183*4882a593Smuzhiyun #define pud_bad(pud) (0)
184*4882a593Smuzhiyun #define pud_present(pud) (1)
185*4882a593Smuzhiyun #define pud_clear(pudp) do { } while (0)
186*4882a593Smuzhiyun #define set_pud(pud,pudp) do { } while (0)
187*4882a593Smuzhiyun
pmd_offset(pud_t * pud,unsigned long addr)188*4882a593Smuzhiyun static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun return (pmd_t *)pud;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun #define pmd_offset pmd_offset
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun #define pmd_large(pmd) (pmd_val(pmd) & 2)
195*4882a593Smuzhiyun #define pmd_leaf(pmd) (pmd_val(pmd) & 2)
196*4882a593Smuzhiyun #define pmd_bad(pmd) (pmd_val(pmd) & 2)
197*4882a593Smuzhiyun #define pmd_present(pmd) (pmd_val(pmd))
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun #define copy_pmd(pmdpd,pmdps) \
200*4882a593Smuzhiyun do { \
201*4882a593Smuzhiyun pmdpd[0] = pmdps[0]; \
202*4882a593Smuzhiyun pmdpd[1] = pmdps[1]; \
203*4882a593Smuzhiyun flush_pmd_entry(pmdpd); \
204*4882a593Smuzhiyun } while (0)
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun #define pmd_clear(pmdp) \
207*4882a593Smuzhiyun do { \
208*4882a593Smuzhiyun pmdp[0] = __pmd(0); \
209*4882a593Smuzhiyun pmdp[1] = __pmd(0); \
210*4882a593Smuzhiyun clean_pmd_entry(pmdp); \
211*4882a593Smuzhiyun } while (0)
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /* we don't need complex calculations here as the pmd is folded into the pgd */
214*4882a593Smuzhiyun #define pmd_addr_end(addr,end) (end)
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun #define set_pte_ext(ptep,pte,ext) cpu_set_pte_ext(ptep,pte,ext)
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun * We don't have huge page support for short descriptors, for the moment
220*4882a593Smuzhiyun * define empty stubs for use by pin_page_for_write.
221*4882a593Smuzhiyun */
222*4882a593Smuzhiyun #define pmd_hugewillfault(pmd) (0)
223*4882a593Smuzhiyun #define pmd_thp_or_huge(pmd) (0)
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun #endif /* __ASSEMBLY__ */
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun #endif /* _ASM_PGTABLE_2LEVEL_H */
228