162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  arch/arm/include/asm/pgtable-2level.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Copyright (C) 1995-2002 Russell King
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci#ifndef _ASM_PGTABLE_2LEVEL_H
862306a36Sopenharmony_ci#define _ASM_PGTABLE_2LEVEL_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#define __PAGETABLE_PMD_FOLDED 1
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/*
1362306a36Sopenharmony_ci * Hardware-wise, we have a two level page table structure, where the first
1462306a36Sopenharmony_ci * level has 4096 entries, and the second level has 256 entries.  Each entry
1562306a36Sopenharmony_ci * is one 32-bit word.  Most of the bits in the second level entry are used
1662306a36Sopenharmony_ci * by hardware, and there aren't any "accessed" and "dirty" bits.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * Linux on the other hand has a three level page table structure, which can
1962306a36Sopenharmony_ci * be wrapped to fit a two level page table structure easily - using the PGD
2062306a36Sopenharmony_ci * and PTE only.  However, Linux also expects one "PTE" table per page, and
2162306a36Sopenharmony_ci * at least a "dirty" bit.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * Therefore, we tweak the implementation slightly - we tell Linux that we
2462306a36Sopenharmony_ci * have 2048 entries in the first level, each of which is 8 bytes (iow, two
2562306a36Sopenharmony_ci * hardware pointers to the second level.)  The second level contains two
2662306a36Sopenharmony_ci * hardware PTE tables arranged contiguously, preceded by Linux versions
2762306a36Sopenharmony_ci * which contain the state information Linux needs.  We, therefore, end up
2862306a36Sopenharmony_ci * with 512 entries in the "PTE" level.
2962306a36Sopenharmony_ci *
3062306a36Sopenharmony_ci * This leads to the page tables having the following layout:
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci *    pgd             pte
3362306a36Sopenharmony_ci * |        |
3462306a36Sopenharmony_ci * +--------+
3562306a36Sopenharmony_ci * |        |       +------------+ +0
3662306a36Sopenharmony_ci * +- - - - +       | Linux pt 0 |
3762306a36Sopenharmony_ci * |        |       +------------+ +1024
3862306a36Sopenharmony_ci * +--------+ +0    | Linux pt 1 |
3962306a36Sopenharmony_ci * |        |-----> +------------+ +2048
4062306a36Sopenharmony_ci * +- - - - + +4    |  h/w pt 0  |
4162306a36Sopenharmony_ci * |        |-----> +------------+ +3072
4262306a36Sopenharmony_ci * +--------+ +8    |  h/w pt 1  |
4362306a36Sopenharmony_ci * |        |       +------------+ +4096
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * See L_PTE_xxx below for definitions of bits in the "Linux pt", and
4662306a36Sopenharmony_ci * PTE_xxx for definitions of bits appearing in the "h/w pt".
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * PMD_xxx definitions refer to bits in the first level page table.
4962306a36Sopenharmony_ci *
5062306a36Sopenharmony_ci * The "dirty" bit is emulated by only granting hardware write permission
5162306a36Sopenharmony_ci * iff the page is marked "writable" and "dirty" in the Linux PTE.  This
5262306a36Sopenharmony_ci * means that a write to a clean page will cause a permission fault, and
5362306a36Sopenharmony_ci * the Linux MM layer will mark the page dirty via handle_pte_fault().
5462306a36Sopenharmony_ci * For the hardware to notice the permission change, the TLB entry must
5562306a36Sopenharmony_ci * be flushed, and ptep_set_access_flags() does that for us.
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * The "accessed" or "young" bit is emulated by a similar method; we only
5862306a36Sopenharmony_ci * allow accesses to the page if the "young" bit is set.  Accesses to the
5962306a36Sopenharmony_ci * page will cause a fault, and handle_pte_fault() will set the young bit
6062306a36Sopenharmony_ci * for us as long as the page is marked present in the corresponding Linux
6162306a36Sopenharmony_ci * PTE entry.  Again, ptep_set_access_flags() will ensure that the TLB is
6262306a36Sopenharmony_ci * up to date.
6362306a36Sopenharmony_ci *
6462306a36Sopenharmony_ci * However, when the "young" bit is cleared, we deny access to the page
6562306a36Sopenharmony_ci * by clearing the hardware PTE.  Currently Linux does not flush the TLB
6662306a36Sopenharmony_ci * for us in this case, which means the TLB will retain the transation
6762306a36Sopenharmony_ci * until either the TLB entry is evicted under pressure, or a context
6862306a36Sopenharmony_ci * switch which changes the user space mapping occurs.
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_ci#define PTRS_PER_PTE		512
7162306a36Sopenharmony_ci#define PTRS_PER_PMD		1
7262306a36Sopenharmony_ci#define PTRS_PER_PGD		2048
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci#define PTE_HWTABLE_PTRS	(PTRS_PER_PTE)
7562306a36Sopenharmony_ci#define PTE_HWTABLE_OFF		(PTE_HWTABLE_PTRS * sizeof(pte_t))
7662306a36Sopenharmony_ci#define PTE_HWTABLE_SIZE	(PTRS_PER_PTE * sizeof(u32))
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci#define MAX_POSSIBLE_PHYSMEM_BITS	32
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/*
8162306a36Sopenharmony_ci * PMD_SHIFT determines the size of the area a second-level page table can map
8262306a36Sopenharmony_ci * PGDIR_SHIFT determines what a third-level page table entry can map
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ci#define PMD_SHIFT		21
8562306a36Sopenharmony_ci#define PGDIR_SHIFT		21
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci#define PMD_SIZE		(1UL << PMD_SHIFT)
8862306a36Sopenharmony_ci#define PMD_MASK		(~(PMD_SIZE-1))
8962306a36Sopenharmony_ci#define PGDIR_SIZE		(1UL << PGDIR_SHIFT)
9062306a36Sopenharmony_ci#define PGDIR_MASK		(~(PGDIR_SIZE-1))
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci/*
9362306a36Sopenharmony_ci * section address mask and size definitions.
9462306a36Sopenharmony_ci */
9562306a36Sopenharmony_ci#define SECTION_SHIFT		20
9662306a36Sopenharmony_ci#define SECTION_SIZE		(1UL << SECTION_SHIFT)
9762306a36Sopenharmony_ci#define SECTION_MASK		(~(SECTION_SIZE-1))
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci/*
10062306a36Sopenharmony_ci * ARMv6 supersection address mask and size definitions.
10162306a36Sopenharmony_ci */
10262306a36Sopenharmony_ci#define SUPERSECTION_SHIFT	24
10362306a36Sopenharmony_ci#define SUPERSECTION_SIZE	(1UL << SUPERSECTION_SHIFT)
10462306a36Sopenharmony_ci#define SUPERSECTION_MASK	(~(SUPERSECTION_SIZE-1))
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci#define USER_PTRS_PER_PGD	(TASK_SIZE / PGDIR_SIZE)
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci/*
10962306a36Sopenharmony_ci * "Linux" PTE definitions.
11062306a36Sopenharmony_ci *
11162306a36Sopenharmony_ci * We keep two sets of PTEs - the hardware and the linux version.
11262306a36Sopenharmony_ci * This allows greater flexibility in the way we map the Linux bits
11362306a36Sopenharmony_ci * onto the hardware tables, and allows us to have YOUNG and DIRTY
11462306a36Sopenharmony_ci * bits.
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * The PTE table pointer refers to the hardware entries; the "Linux"
11762306a36Sopenharmony_ci * entries are stored 1024 bytes below.
11862306a36Sopenharmony_ci */
11962306a36Sopenharmony_ci#define L_PTE_VALID		(_AT(pteval_t, 1) << 0)		/* Valid */
12062306a36Sopenharmony_ci#define L_PTE_PRESENT		(_AT(pteval_t, 1) << 0)
12162306a36Sopenharmony_ci#define L_PTE_YOUNG		(_AT(pteval_t, 1) << 1)
12262306a36Sopenharmony_ci#define L_PTE_DIRTY		(_AT(pteval_t, 1) << 6)
12362306a36Sopenharmony_ci#define L_PTE_RDONLY		(_AT(pteval_t, 1) << 7)
12462306a36Sopenharmony_ci#define L_PTE_USER		(_AT(pteval_t, 1) << 8)
12562306a36Sopenharmony_ci#define L_PTE_XN		(_AT(pteval_t, 1) << 9)
12662306a36Sopenharmony_ci#define L_PTE_SHARED		(_AT(pteval_t, 1) << 10)	/* shared(v6), coherent(xsc3) */
12762306a36Sopenharmony_ci#define L_PTE_NONE		(_AT(pteval_t, 1) << 11)
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci/* We borrow bit 7 to store the exclusive marker in swap PTEs. */
13062306a36Sopenharmony_ci#define L_PTE_SWP_EXCLUSIVE	L_PTE_RDONLY
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci/*
13362306a36Sopenharmony_ci * These are the memory types, defined to be compatible with
13462306a36Sopenharmony_ci * pre-ARMv6 CPUs cacheable and bufferable bits: n/a,n/a,C,B
13562306a36Sopenharmony_ci * ARMv6+ without TEX remapping, they are a table index.
13662306a36Sopenharmony_ci * ARMv6+ with TEX remapping, they correspond to n/a,TEX(0),C,B
13762306a36Sopenharmony_ci *
13862306a36Sopenharmony_ci * MT type		Pre-ARMv6	ARMv6+ type / cacheable status
13962306a36Sopenharmony_ci * UNCACHED		Uncached	Strongly ordered
14062306a36Sopenharmony_ci * BUFFERABLE		Bufferable	Normal memory / non-cacheable
14162306a36Sopenharmony_ci * WRITETHROUGH		Writethrough	Normal memory / write through
14262306a36Sopenharmony_ci * WRITEBACK		Writeback	Normal memory / write back, read alloc
14362306a36Sopenharmony_ci * MINICACHE		Minicache	N/A
14462306a36Sopenharmony_ci * WRITEALLOC		Writeback	Normal memory / write back, write alloc
14562306a36Sopenharmony_ci * DEV_SHARED		Uncached	Device memory (shared)
14662306a36Sopenharmony_ci * DEV_NONSHARED	Uncached	Device memory (non-shared)
14762306a36Sopenharmony_ci * DEV_WC		Bufferable	Normal memory / non-cacheable
14862306a36Sopenharmony_ci * DEV_CACHED		Writeback	Normal memory / write back, read alloc
14962306a36Sopenharmony_ci * VECTORS		Variable	Normal memory / variable
15062306a36Sopenharmony_ci *
15162306a36Sopenharmony_ci * All normal memory mappings have the following properties:
15262306a36Sopenharmony_ci * - reads can be repeated with no side effects
15362306a36Sopenharmony_ci * - repeated reads return the last value written
15462306a36Sopenharmony_ci * - reads can fetch additional locations without side effects
15562306a36Sopenharmony_ci * - writes can be repeated (in certain cases) with no side effects
15662306a36Sopenharmony_ci * - writes can be merged before accessing the target
15762306a36Sopenharmony_ci * - unaligned accesses can be supported
15862306a36Sopenharmony_ci *
15962306a36Sopenharmony_ci * All device mappings have the following properties:
16062306a36Sopenharmony_ci * - no access speculation
16162306a36Sopenharmony_ci * - no repetition (eg, on return from an exception)
16262306a36Sopenharmony_ci * - number, order and size of accesses are maintained
16362306a36Sopenharmony_ci * - unaligned accesses are "unpredictable"
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_ci#define L_PTE_MT_UNCACHED	(_AT(pteval_t, 0x00) << 2)	/* 0000 */
16662306a36Sopenharmony_ci#define L_PTE_MT_BUFFERABLE	(_AT(pteval_t, 0x01) << 2)	/* 0001 */
16762306a36Sopenharmony_ci#define L_PTE_MT_WRITETHROUGH	(_AT(pteval_t, 0x02) << 2)	/* 0010 */
16862306a36Sopenharmony_ci#define L_PTE_MT_WRITEBACK	(_AT(pteval_t, 0x03) << 2)	/* 0011 */
16962306a36Sopenharmony_ci#define L_PTE_MT_MINICACHE	(_AT(pteval_t, 0x06) << 2)	/* 0110 (sa1100, xscale) */
17062306a36Sopenharmony_ci#define L_PTE_MT_WRITEALLOC	(_AT(pteval_t, 0x07) << 2)	/* 0111 */
17162306a36Sopenharmony_ci#define L_PTE_MT_DEV_SHARED	(_AT(pteval_t, 0x04) << 2)	/* 0100 */
17262306a36Sopenharmony_ci#define L_PTE_MT_DEV_NONSHARED	(_AT(pteval_t, 0x0c) << 2)	/* 1100 */
17362306a36Sopenharmony_ci#define L_PTE_MT_DEV_WC		(_AT(pteval_t, 0x09) << 2)	/* 1001 */
17462306a36Sopenharmony_ci#define L_PTE_MT_DEV_CACHED	(_AT(pteval_t, 0x0b) << 2)	/* 1011 */
17562306a36Sopenharmony_ci#define L_PTE_MT_VECTORS	(_AT(pteval_t, 0x0f) << 2)	/* 1111 */
17662306a36Sopenharmony_ci#define L_PTE_MT_MASK		(_AT(pteval_t, 0x0f) << 2)
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci#ifndef __ASSEMBLY__
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci/*
18162306a36Sopenharmony_ci * The "pud_xxx()" functions here are trivial when the pmd is folded into
18262306a36Sopenharmony_ci * the pud: the pud entry is never bad, always exists, and can't be set or
18362306a36Sopenharmony_ci * cleared.
18462306a36Sopenharmony_ci */
18562306a36Sopenharmony_cistatic inline int pud_none(pud_t pud)
18662306a36Sopenharmony_ci{
18762306a36Sopenharmony_ci	return 0;
18862306a36Sopenharmony_ci}
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_cistatic inline int pud_bad(pud_t pud)
19162306a36Sopenharmony_ci{
19262306a36Sopenharmony_ci	return 0;
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic inline int pud_present(pud_t pud)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	return 1;
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic inline void pud_clear(pud_t *pudp)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic inline void set_pud(pud_t *pudp, pud_t pud)
20562306a36Sopenharmony_ci{
20662306a36Sopenharmony_ci}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_cistatic inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	return (pmd_t *)pud;
21162306a36Sopenharmony_ci}
21262306a36Sopenharmony_ci#define pmd_offset pmd_offset
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci#define pmd_pfn(pmd)		(__phys_to_pfn(pmd_val(pmd) & PHYS_MASK))
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci#define pmd_large(pmd)		(pmd_val(pmd) & 2)
21762306a36Sopenharmony_ci#define pmd_leaf(pmd)		(pmd_val(pmd) & 2)
21862306a36Sopenharmony_ci#define pmd_bad(pmd)		(pmd_val(pmd) & 2)
21962306a36Sopenharmony_ci#define pmd_present(pmd)	(pmd_val(pmd))
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci#define copy_pmd(pmdpd,pmdps)		\
22262306a36Sopenharmony_ci	do {				\
22362306a36Sopenharmony_ci		pmdpd[0] = pmdps[0];	\
22462306a36Sopenharmony_ci		pmdpd[1] = pmdps[1];	\
22562306a36Sopenharmony_ci		flush_pmd_entry(pmdpd);	\
22662306a36Sopenharmony_ci	} while (0)
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci#define pmd_clear(pmdp)			\
22962306a36Sopenharmony_ci	do {				\
23062306a36Sopenharmony_ci		pmdp[0] = __pmd(0);	\
23162306a36Sopenharmony_ci		pmdp[1] = __pmd(0);	\
23262306a36Sopenharmony_ci		clean_pmd_entry(pmdp);	\
23362306a36Sopenharmony_ci	} while (0)
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci/* we don't need complex calculations here as the pmd is folded into the pgd */
23662306a36Sopenharmony_ci#define pmd_addr_end(addr,end) (end)
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci#define set_pte_ext(ptep,pte,ext) cpu_set_pte_ext(ptep,pte,ext)
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci/*
24162306a36Sopenharmony_ci * We don't have huge page support for short descriptors, for the moment
24262306a36Sopenharmony_ci * define empty stubs for use by pin_page_for_write.
24362306a36Sopenharmony_ci */
24462306a36Sopenharmony_ci#define pmd_hugewillfault(pmd)	(0)
24562306a36Sopenharmony_ci#define pmd_thp_or_huge(pmd)	(0)
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci#endif /* _ASM_PGTABLE_2LEVEL_H */
250