162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci/*
762306a36Sopenharmony_ci * page table flags for software walked/managed MMUv3 (ARC700) and MMUv4 (HS)
862306a36Sopenharmony_ci * There correspond to the corresponding bits in the TLB
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#ifndef _ASM_ARC_PGTABLE_BITS_ARCV2_H
1262306a36Sopenharmony_ci#define _ASM_ARC_PGTABLE_BITS_ARCV2_H
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#ifdef CONFIG_ARC_CACHE_PAGES
1562306a36Sopenharmony_ci#define _PAGE_CACHEABLE		(1 << 0)  /* Cached (H) */
1662306a36Sopenharmony_ci#else
1762306a36Sopenharmony_ci#define _PAGE_CACHEABLE		0
1862306a36Sopenharmony_ci#endif
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define _PAGE_EXECUTE		(1 << 1)  /* User Execute  (H) */
2162306a36Sopenharmony_ci#define _PAGE_WRITE		(1 << 2)  /* User Write    (H) */
2262306a36Sopenharmony_ci#define _PAGE_READ		(1 << 3)  /* User Read     (H) */
2362306a36Sopenharmony_ci#define _PAGE_ACCESSED		(1 << 4)  /* Accessed      (s) */
2462306a36Sopenharmony_ci#define _PAGE_DIRTY		(1 << 5)  /* Modified      (s) */
2562306a36Sopenharmony_ci#define _PAGE_SPECIAL		(1 << 6)
2662306a36Sopenharmony_ci#define _PAGE_GLOBAL		(1 << 8)  /* ASID agnostic (H) */
2762306a36Sopenharmony_ci#define _PAGE_PRESENT		(1 << 9)  /* PTE/TLB Valid (H) */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/* We borrow bit 5 to store the exclusive marker in swap PTEs. */
3062306a36Sopenharmony_ci#define _PAGE_SWP_EXCLUSIVE	_PAGE_DIRTY
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#ifdef CONFIG_ARC_MMU_V4
3362306a36Sopenharmony_ci#define _PAGE_HW_SZ		(1 << 10)  /* Normal/super (H) */
3462306a36Sopenharmony_ci#else
3562306a36Sopenharmony_ci#define _PAGE_HW_SZ		0
3662306a36Sopenharmony_ci#endif
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* Defaults for every user page */
3962306a36Sopenharmony_ci#define ___DEF		(_PAGE_PRESENT | _PAGE_CACHEABLE)
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* Set of bits not changed in pte_modify */
4262306a36Sopenharmony_ci#define _PAGE_CHG_MASK	(PAGE_MASK_PHYS | _PAGE_ACCESSED | _PAGE_DIRTY | \
4362306a36Sopenharmony_ci							   _PAGE_SPECIAL)
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* More Abbrevaited helpers */
4662306a36Sopenharmony_ci#define PAGE_U_NONE     __pgprot(___DEF)
4762306a36Sopenharmony_ci#define PAGE_U_R        __pgprot(___DEF | _PAGE_READ)
4862306a36Sopenharmony_ci#define PAGE_U_W_R      __pgprot(___DEF | _PAGE_READ | _PAGE_WRITE)
4962306a36Sopenharmony_ci#define PAGE_U_X_R      __pgprot(___DEF | _PAGE_READ | _PAGE_EXECUTE)
5062306a36Sopenharmony_ci#define PAGE_U_X_W_R    __pgprot(___DEF \
5162306a36Sopenharmony_ci				| _PAGE_READ | _PAGE_WRITE | _PAGE_EXECUTE)
5262306a36Sopenharmony_ci#define PAGE_KERNEL     __pgprot(___DEF | _PAGE_GLOBAL \
5362306a36Sopenharmony_ci				| _PAGE_READ | _PAGE_WRITE | _PAGE_EXECUTE)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define PAGE_SHARED	PAGE_U_W_R
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci#define pgprot_noncached(prot)	(__pgprot(pgprot_val(prot) & ~_PAGE_CACHEABLE))
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/*
6062306a36Sopenharmony_ci * Mapping of vm_flags (Generic VM) to PTE flags (arch specific)
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * Certain cases have 1:1 mapping
6362306a36Sopenharmony_ci *  e.g. __P101 means VM_READ, VM_EXEC and !VM_SHARED
6462306a36Sopenharmony_ci *       which directly corresponds to  PAGE_U_X_R
6562306a36Sopenharmony_ci *
6662306a36Sopenharmony_ci * Other rules which cause the divergence from 1:1 mapping
6762306a36Sopenharmony_ci *
6862306a36Sopenharmony_ci *  1. Although ARC700 can do exclusive execute/write protection (meaning R
6962306a36Sopenharmony_ci *     can be tracked independet of X/W unlike some other CPUs), still to
7062306a36Sopenharmony_ci *     keep things consistent with other archs:
7162306a36Sopenharmony_ci *      -Write implies Read:   W => R
7262306a36Sopenharmony_ci *      -Execute implies Read: X => R
7362306a36Sopenharmony_ci *
7462306a36Sopenharmony_ci *  2. Pvt Writable doesn't have Write Enabled initially: Pvt-W => !W
7562306a36Sopenharmony_ci *     This is to enable COW mechanism
7662306a36Sopenharmony_ci */
7762306a36Sopenharmony_ci	/* xwr */
7862306a36Sopenharmony_ci#ifndef __ASSEMBLY__
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci#define pte_write(pte)		(pte_val(pte) & _PAGE_WRITE)
8162306a36Sopenharmony_ci#define pte_dirty(pte)		(pte_val(pte) & _PAGE_DIRTY)
8262306a36Sopenharmony_ci#define pte_young(pte)		(pte_val(pte) & _PAGE_ACCESSED)
8362306a36Sopenharmony_ci#define pte_special(pte)	(pte_val(pte) & _PAGE_SPECIAL)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#define PTE_BIT_FUNC(fn, op) \
8662306a36Sopenharmony_ci	static inline pte_t pte_##fn(pte_t pte) { pte_val(pte) op; return pte; }
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ciPTE_BIT_FUNC(mknotpresent,     &= ~(_PAGE_PRESENT));
8962306a36Sopenharmony_ciPTE_BIT_FUNC(wrprotect,	&= ~(_PAGE_WRITE));
9062306a36Sopenharmony_ciPTE_BIT_FUNC(mkwrite_novma,	|= (_PAGE_WRITE));
9162306a36Sopenharmony_ciPTE_BIT_FUNC(mkclean,	&= ~(_PAGE_DIRTY));
9262306a36Sopenharmony_ciPTE_BIT_FUNC(mkdirty,	|= (_PAGE_DIRTY));
9362306a36Sopenharmony_ciPTE_BIT_FUNC(mkold,	&= ~(_PAGE_ACCESSED));
9462306a36Sopenharmony_ciPTE_BIT_FUNC(mkyoung,	|= (_PAGE_ACCESSED));
9562306a36Sopenharmony_ciPTE_BIT_FUNC(mkspecial,	|= (_PAGE_SPECIAL));
9662306a36Sopenharmony_ciPTE_BIT_FUNC(mkhuge,	|= (_PAGE_HW_SZ));
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
10162306a36Sopenharmony_ci}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistruct vm_fault;
10462306a36Sopenharmony_civoid update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
10562306a36Sopenharmony_ci		unsigned long address, pte_t *ptep, unsigned int nr);
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci#define update_mmu_cache(vma, addr, ptep) \
10862306a36Sopenharmony_ci	update_mmu_cache_range(NULL, vma, addr, ptep, 1)
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/*
11162306a36Sopenharmony_ci * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
11262306a36Sopenharmony_ci * are !pte_none() && !pte_present().
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * Format of swap PTEs:
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci *   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
11762306a36Sopenharmony_ci *   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
11862306a36Sopenharmony_ci *   <-------------- offset -------------> <--- zero --> E < type ->
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci *   E is the exclusive marker that is not stored in swap entries.
12162306a36Sopenharmony_ci *   The zero'ed bits include _PAGE_PRESENT.
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_ci#define __swp_entry(type, off)		((swp_entry_t) \
12462306a36Sopenharmony_ci					{ ((type) & 0x1f) | ((off) << 13) })
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci/* Decode a PTE containing swap "identifier "into constituents */
12762306a36Sopenharmony_ci#define __swp_type(pte_lookalike)	(((pte_lookalike).val) & 0x1f)
12862306a36Sopenharmony_ci#define __swp_offset(pte_lookalike)	((pte_lookalike).val >> 13)
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#define __pte_to_swp_entry(pte)		((swp_entry_t) { pte_val(pte) })
13162306a36Sopenharmony_ci#define __swp_entry_to_pte(x)		((pte_t) { (x).val })
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_cistatic inline int pte_swp_exclusive(pte_t pte)
13462306a36Sopenharmony_ci{
13562306a36Sopenharmony_ci	return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
13662306a36Sopenharmony_ci}
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciPTE_BIT_FUNC(swp_mkexclusive, |= (_PAGE_SWP_EXCLUSIVE));
13962306a36Sopenharmony_ciPTE_BIT_FUNC(swp_clear_exclusive, &= ~(_PAGE_SWP_EXCLUSIVE));
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
14262306a36Sopenharmony_ci#include <asm/hugepage.h>
14362306a36Sopenharmony_ci#endif
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci#endif
148