18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * include/asm-xtensa/pgtable.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2001 - 2013 Tensilica Inc.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _XTENSA_PGTABLE_H
98c2ecf20Sopenharmony_ci#define _XTENSA_PGTABLE_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/page.h>
128c2ecf20Sopenharmony_ci#include <asm/kmem_layout.h>
138c2ecf20Sopenharmony_ci#include <asm-generic/pgtable-nopmd.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * We only use two ring levels, user and kernel space.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
208c2ecf20Sopenharmony_ci#define USER_RING		1	/* user ring level */
218c2ecf20Sopenharmony_ci#else
228c2ecf20Sopenharmony_ci#define USER_RING		0
238c2ecf20Sopenharmony_ci#endif
248c2ecf20Sopenharmony_ci#define KERNEL_RING		0	/* kernel ring level */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * The Xtensa architecture port of Linux has a two-level page table system,
288c2ecf20Sopenharmony_ci * i.e. the logical three-level Linux page table layout is folded.
298c2ecf20Sopenharmony_ci * Each task has the following memory page tables:
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci *   PGD table (page directory), ie. 3rd-level page table:
328c2ecf20Sopenharmony_ci *	One page (4 kB) of 1024 (PTRS_PER_PGD) pointers to PTE tables
338c2ecf20Sopenharmony_ci *	(Architectures that don't have the PMD folded point to the PMD tables)
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci *	The pointer to the PGD table for a given task can be retrieved from
368c2ecf20Sopenharmony_ci *	the task structure (struct task_struct*) t, e.g. current():
378c2ecf20Sopenharmony_ci *	  (t->mm ? t->mm : t->active_mm)->pgd
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci *   PMD tables (page middle-directory), ie. 2nd-level page tables:
408c2ecf20Sopenharmony_ci *	Absent for the Xtensa architecture (folded, PTRS_PER_PMD == 1).
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci *   PTE tables (page table entry), ie. 1st-level page tables:
438c2ecf20Sopenharmony_ci *	One page (4 kB) of 1024 (PTRS_PER_PTE) PTEs with a special PTE
448c2ecf20Sopenharmony_ci *	invalid_pte_table for absent mappings.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * The individual pages are 4 kB big with special pages for the empty_zero_page.
478c2ecf20Sopenharmony_ci */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define PGDIR_SHIFT	22
508c2ecf20Sopenharmony_ci#define PGDIR_SIZE	(1UL << PGDIR_SHIFT)
518c2ecf20Sopenharmony_ci#define PGDIR_MASK	(~(PGDIR_SIZE-1))
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/*
548c2ecf20Sopenharmony_ci * Entries per page directory level: we use two-level, so
558c2ecf20Sopenharmony_ci * we don't really have any PMD directory physically.
568c2ecf20Sopenharmony_ci */
578c2ecf20Sopenharmony_ci#define PTRS_PER_PTE		1024
588c2ecf20Sopenharmony_ci#define PTRS_PER_PTE_SHIFT	10
598c2ecf20Sopenharmony_ci#define PTRS_PER_PGD		1024
608c2ecf20Sopenharmony_ci#define PGD_ORDER		0
618c2ecf20Sopenharmony_ci#define USER_PTRS_PER_PGD	(TASK_SIZE/PGDIR_SIZE)
628c2ecf20Sopenharmony_ci#define FIRST_USER_ADDRESS	0UL
638c2ecf20Sopenharmony_ci#define FIRST_USER_PGD_NR	(FIRST_USER_ADDRESS >> PGDIR_SHIFT)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
668c2ecf20Sopenharmony_ci/*
678c2ecf20Sopenharmony_ci * Virtual memory area. We keep a distance to other memory regions to be
688c2ecf20Sopenharmony_ci * on the safe side. We also use this area for cache aliasing.
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_ci#define VMALLOC_START		(XCHAL_KSEG_CACHED_VADDR - 0x10000000)
718c2ecf20Sopenharmony_ci#define VMALLOC_END		(VMALLOC_START + 0x07FEFFFF)
728c2ecf20Sopenharmony_ci#define TLBTEMP_BASE_1		(VMALLOC_START + 0x08000000)
738c2ecf20Sopenharmony_ci#define TLBTEMP_BASE_2		(TLBTEMP_BASE_1 + DCACHE_WAY_SIZE)
748c2ecf20Sopenharmony_ci#if 2 * DCACHE_WAY_SIZE > ICACHE_WAY_SIZE
758c2ecf20Sopenharmony_ci#define TLBTEMP_SIZE		(2 * DCACHE_WAY_SIZE)
768c2ecf20Sopenharmony_ci#else
778c2ecf20Sopenharmony_ci#define TLBTEMP_SIZE		ICACHE_WAY_SIZE
788c2ecf20Sopenharmony_ci#endif
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#else
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define VMALLOC_START		__XTENSA_UL_CONST(0)
838c2ecf20Sopenharmony_ci#define VMALLOC_END		__XTENSA_UL_CONST(0xffffffff)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#endif
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/*
888c2ecf20Sopenharmony_ci * For the Xtensa architecture, the PTE layout is as follows:
898c2ecf20Sopenharmony_ci *
908c2ecf20Sopenharmony_ci *		31------12  11  10-9   8-6  5-4  3-2  1-0
918c2ecf20Sopenharmony_ci *		+-----------------------------------------+
928c2ecf20Sopenharmony_ci *		|           |   Software   |   HARDWARE   |
938c2ecf20Sopenharmony_ci *		|    PPN    |          ADW | RI |Attribute|
948c2ecf20Sopenharmony_ci *		+-----------------------------------------+
958c2ecf20Sopenharmony_ci *   pte_none	|             MBZ          | 01 | 11 | 00 |
968c2ecf20Sopenharmony_ci *		+-----------------------------------------+
978c2ecf20Sopenharmony_ci *   present	|    PPN    | 0 | 00 | ADW | RI | CA | wx |
988c2ecf20Sopenharmony_ci *		+- - - - - - - - - - - - - - - - - - - - -+
998c2ecf20Sopenharmony_ci *   (PAGE_NONE)|    PPN    | 0 | 00 | ADW | 01 | 11 | 11 |
1008c2ecf20Sopenharmony_ci *		+-----------------------------------------+
1018c2ecf20Sopenharmony_ci *   swap	|     index     |   type   | 01 | 11 | 00 |
1028c2ecf20Sopenharmony_ci *		+-----------------------------------------+
1038c2ecf20Sopenharmony_ci *
1048c2ecf20Sopenharmony_ci * For T1050 hardware and earlier the layout differs for present and (PAGE_NONE)
1058c2ecf20Sopenharmony_ci *		+-----------------------------------------+
1068c2ecf20Sopenharmony_ci *   present	|    PPN    | 0 | 00 | ADW | RI | CA | w1 |
1078c2ecf20Sopenharmony_ci *		+-----------------------------------------+
1088c2ecf20Sopenharmony_ci *   (PAGE_NONE)|    PPN    | 0 | 00 | ADW | 01 | 01 | 00 |
1098c2ecf20Sopenharmony_ci *		+-----------------------------------------+
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci *  Legend:
1128c2ecf20Sopenharmony_ci *   PPN        Physical Page Number
1138c2ecf20Sopenharmony_ci *   ADW	software: accessed (young) / dirty / writable
1148c2ecf20Sopenharmony_ci *   RI         ring (0=privileged, 1=user, 2 and 3 are unused)
1158c2ecf20Sopenharmony_ci *   CA		cache attribute: 00 bypass, 01 writeback, 10 writethrough
1168c2ecf20Sopenharmony_ci *		(11 is invalid and used to mark pages that are not present)
1178c2ecf20Sopenharmony_ci *   w		page is writable (hw)
1188c2ecf20Sopenharmony_ci *   x		page is executable (hw)
1198c2ecf20Sopenharmony_ci *   index      swap offset / PAGE_SIZE (bit 11-31: 21 bits -> 8 GB)
1208c2ecf20Sopenharmony_ci *		(note that the index is always non-zero)
1218c2ecf20Sopenharmony_ci *   type       swap type (5 bits -> 32 types)
1228c2ecf20Sopenharmony_ci *
1238c2ecf20Sopenharmony_ci *  Notes:
1248c2ecf20Sopenharmony_ci *   - (PROT_NONE) is a special case of 'present' but causes an exception for
1258c2ecf20Sopenharmony_ci *     any access (read, write, and execute).
1268c2ecf20Sopenharmony_ci *   - 'multihit-exception' has the highest priority of all MMU exceptions,
1278c2ecf20Sopenharmony_ci *     so the ring must be set to 'RING_USER' even for 'non-present' pages.
1288c2ecf20Sopenharmony_ci *   - on older hardware, the exectuable flag was not supported and
1298c2ecf20Sopenharmony_ci *     used as a 'valid' flag, so it needs to be always set.
1308c2ecf20Sopenharmony_ci *   - we need to keep track of certain flags in software (dirty and young)
1318c2ecf20Sopenharmony_ci *     to do this, we use write exceptions and have a separate software w-flag.
1328c2ecf20Sopenharmony_ci *   - attribute value 1101 (and 1111 on T1050 and earlier) is reserved
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci#define _PAGE_ATTRIB_MASK	0xf
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci#define _PAGE_HW_EXEC		(1<<0)	/* hardware: page is executable */
1388c2ecf20Sopenharmony_ci#define _PAGE_HW_WRITE		(1<<1)	/* hardware: page is writable */
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#define _PAGE_CA_BYPASS		(0<<2)	/* bypass, non-speculative */
1418c2ecf20Sopenharmony_ci#define _PAGE_CA_WB		(1<<2)	/* write-back */
1428c2ecf20Sopenharmony_ci#define _PAGE_CA_WT		(2<<2)	/* write-through */
1438c2ecf20Sopenharmony_ci#define _PAGE_CA_MASK		(3<<2)
1448c2ecf20Sopenharmony_ci#define _PAGE_CA_INVALID	(3<<2)
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/* We use invalid attribute values to distinguish special pte entries */
1478c2ecf20Sopenharmony_ci#if XCHAL_HW_VERSION_MAJOR < 2000
1488c2ecf20Sopenharmony_ci#define _PAGE_HW_VALID		0x01	/* older HW needed this bit set */
1498c2ecf20Sopenharmony_ci#define _PAGE_NONE		0x04
1508c2ecf20Sopenharmony_ci#else
1518c2ecf20Sopenharmony_ci#define _PAGE_HW_VALID		0x00
1528c2ecf20Sopenharmony_ci#define _PAGE_NONE		0x0f
1538c2ecf20Sopenharmony_ci#endif
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci#define _PAGE_USER		(1<<4)	/* user access (ring=1) */
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci/* Software */
1588c2ecf20Sopenharmony_ci#define _PAGE_WRITABLE_BIT	6
1598c2ecf20Sopenharmony_ci#define _PAGE_WRITABLE		(1<<6)	/* software: page writable */
1608c2ecf20Sopenharmony_ci#define _PAGE_DIRTY		(1<<7)	/* software: page dirty */
1618c2ecf20Sopenharmony_ci#define _PAGE_ACCESSED		(1<<8)	/* software: page accessed (read) */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#define _PAGE_CHG_MASK	   (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
1668c2ecf20Sopenharmony_ci#define _PAGE_PRESENT	   (_PAGE_HW_VALID | _PAGE_CA_WB | _PAGE_ACCESSED)
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci#define PAGE_NONE	   __pgprot(_PAGE_NONE | _PAGE_USER)
1698c2ecf20Sopenharmony_ci#define PAGE_COPY	   __pgprot(_PAGE_PRESENT | _PAGE_USER)
1708c2ecf20Sopenharmony_ci#define PAGE_COPY_EXEC	   __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
1718c2ecf20Sopenharmony_ci#define PAGE_READONLY	   __pgprot(_PAGE_PRESENT | _PAGE_USER)
1728c2ecf20Sopenharmony_ci#define PAGE_READONLY_EXEC __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_HW_EXEC)
1738c2ecf20Sopenharmony_ci#define PAGE_SHARED	   __pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE)
1748c2ecf20Sopenharmony_ci#define PAGE_SHARED_EXEC \
1758c2ecf20Sopenharmony_ci	__pgprot(_PAGE_PRESENT | _PAGE_USER | _PAGE_WRITABLE | _PAGE_HW_EXEC)
1768c2ecf20Sopenharmony_ci#define PAGE_KERNEL	   __pgprot(_PAGE_PRESENT | _PAGE_HW_WRITE)
1778c2ecf20Sopenharmony_ci#define PAGE_KERNEL_RO	   __pgprot(_PAGE_PRESENT)
1788c2ecf20Sopenharmony_ci#define PAGE_KERNEL_EXEC   __pgprot(_PAGE_PRESENT|_PAGE_HW_WRITE|_PAGE_HW_EXEC)
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci#if (DCACHE_WAY_SIZE > PAGE_SIZE)
1818c2ecf20Sopenharmony_ci# define _PAGE_DIRECTORY   (_PAGE_HW_VALID | _PAGE_ACCESSED | _PAGE_CA_BYPASS)
1828c2ecf20Sopenharmony_ci#else
1838c2ecf20Sopenharmony_ci# define _PAGE_DIRECTORY   (_PAGE_HW_VALID | _PAGE_ACCESSED | _PAGE_CA_WB)
1848c2ecf20Sopenharmony_ci#endif
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci#else /* no mmu */
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci# define _PAGE_CHG_MASK  (PAGE_MASK | _PAGE_ACCESSED | _PAGE_DIRTY)
1898c2ecf20Sopenharmony_ci# define PAGE_NONE       __pgprot(0)
1908c2ecf20Sopenharmony_ci# define PAGE_SHARED     __pgprot(0)
1918c2ecf20Sopenharmony_ci# define PAGE_COPY       __pgprot(0)
1928c2ecf20Sopenharmony_ci# define PAGE_READONLY   __pgprot(0)
1938c2ecf20Sopenharmony_ci# define PAGE_KERNEL     __pgprot(0)
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci#endif
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/*
1988c2ecf20Sopenharmony_ci * On certain configurations of Xtensa MMUs (eg. the initial Linux config),
1998c2ecf20Sopenharmony_ci * the MMU can't do page protection for execute, and considers that the same as
2008c2ecf20Sopenharmony_ci * read.  Also, write permissions may imply read permissions.
2018c2ecf20Sopenharmony_ci * What follows is the closest we can get by reasonable means..
2028c2ecf20Sopenharmony_ci * See linux/mm/mmap.c for protection_map[] array that uses these definitions.
2038c2ecf20Sopenharmony_ci */
2048c2ecf20Sopenharmony_ci#define __P000	PAGE_NONE		/* private --- */
2058c2ecf20Sopenharmony_ci#define __P001	PAGE_READONLY		/* private --r */
2068c2ecf20Sopenharmony_ci#define __P010	PAGE_COPY		/* private -w- */
2078c2ecf20Sopenharmony_ci#define __P011	PAGE_COPY		/* private -wr */
2088c2ecf20Sopenharmony_ci#define __P100	PAGE_READONLY_EXEC	/* private x-- */
2098c2ecf20Sopenharmony_ci#define __P101	PAGE_READONLY_EXEC	/* private x-r */
2108c2ecf20Sopenharmony_ci#define __P110	PAGE_COPY_EXEC		/* private xw- */
2118c2ecf20Sopenharmony_ci#define __P111	PAGE_COPY_EXEC		/* private xwr */
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci#define __S000	PAGE_NONE		/* shared  --- */
2148c2ecf20Sopenharmony_ci#define __S001	PAGE_READONLY		/* shared  --r */
2158c2ecf20Sopenharmony_ci#define __S010	PAGE_SHARED		/* shared  -w- */
2168c2ecf20Sopenharmony_ci#define __S011	PAGE_SHARED		/* shared  -wr */
2178c2ecf20Sopenharmony_ci#define __S100	PAGE_READONLY_EXEC	/* shared  x-- */
2188c2ecf20Sopenharmony_ci#define __S101	PAGE_READONLY_EXEC	/* shared  x-r */
2198c2ecf20Sopenharmony_ci#define __S110	PAGE_SHARED_EXEC	/* shared  xw- */
2208c2ecf20Sopenharmony_ci#define __S111	PAGE_SHARED_EXEC	/* shared  xwr */
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci#define pte_ERROR(e) \
2258c2ecf20Sopenharmony_ci	printk("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
2268c2ecf20Sopenharmony_ci#define pgd_ERROR(e) \
2278c2ecf20Sopenharmony_ci	printk("%s:%d: bad pgd entry %08lx.\n", __FILE__, __LINE__, pgd_val(e))
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ciextern unsigned long empty_zero_page[1024];
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci#define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page))
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
2348c2ecf20Sopenharmony_ciextern pgd_t swapper_pg_dir[PAGE_SIZE/sizeof(pgd_t)];
2358c2ecf20Sopenharmony_ciextern void paging_init(void);
2368c2ecf20Sopenharmony_ci#else
2378c2ecf20Sopenharmony_ci# define swapper_pg_dir NULL
2388c2ecf20Sopenharmony_cistatic inline void paging_init(void) { }
2398c2ecf20Sopenharmony_ci#endif
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci/*
2428c2ecf20Sopenharmony_ci * The pmd contains the kernel virtual address of the pte page.
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_ci#define pmd_page_vaddr(pmd) ((unsigned long)(pmd_val(pmd) & PAGE_MASK))
2458c2ecf20Sopenharmony_ci#define pmd_page(pmd) virt_to_page(pmd_val(pmd))
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/*
2488c2ecf20Sopenharmony_ci * pte status.
2498c2ecf20Sopenharmony_ci */
2508c2ecf20Sopenharmony_ci# define pte_none(pte)	 (pte_val(pte) == (_PAGE_CA_INVALID | _PAGE_USER))
2518c2ecf20Sopenharmony_ci#if XCHAL_HW_VERSION_MAJOR < 2000
2528c2ecf20Sopenharmony_ci# define pte_present(pte) ((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_CA_INVALID)
2538c2ecf20Sopenharmony_ci#else
2548c2ecf20Sopenharmony_ci# define pte_present(pte)						\
2558c2ecf20Sopenharmony_ci	(((pte_val(pte) & _PAGE_CA_MASK) != _PAGE_CA_INVALID)		\
2568c2ecf20Sopenharmony_ci	 || ((pte_val(pte) & _PAGE_ATTRIB_MASK) == _PAGE_NONE))
2578c2ecf20Sopenharmony_ci#endif
2588c2ecf20Sopenharmony_ci#define pte_clear(mm,addr,ptep)						\
2598c2ecf20Sopenharmony_ci	do { update_pte(ptep, __pte(_PAGE_CA_INVALID | _PAGE_USER)); } while (0)
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci#define pmd_none(pmd)	 (!pmd_val(pmd))
2628c2ecf20Sopenharmony_ci#define pmd_present(pmd) (pmd_val(pmd) & PAGE_MASK)
2638c2ecf20Sopenharmony_ci#define pmd_bad(pmd)	 (pmd_val(pmd) & ~PAGE_MASK)
2648c2ecf20Sopenharmony_ci#define pmd_clear(pmdp)	 do { set_pmd(pmdp, __pmd(0)); } while (0)
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic inline int pte_write(pte_t pte) { return pte_val(pte) & _PAGE_WRITABLE; }
2678c2ecf20Sopenharmony_cistatic inline int pte_dirty(pte_t pte) { return pte_val(pte) & _PAGE_DIRTY; }
2688c2ecf20Sopenharmony_cistatic inline int pte_young(pte_t pte) { return pte_val(pte) & _PAGE_ACCESSED; }
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic inline pte_t pte_wrprotect(pte_t pte)
2718c2ecf20Sopenharmony_ci	{ pte_val(pte) &= ~(_PAGE_WRITABLE | _PAGE_HW_WRITE); return pte; }
2728c2ecf20Sopenharmony_cistatic inline pte_t pte_mkclean(pte_t pte)
2738c2ecf20Sopenharmony_ci	{ pte_val(pte) &= ~(_PAGE_DIRTY | _PAGE_HW_WRITE); return pte; }
2748c2ecf20Sopenharmony_cistatic inline pte_t pte_mkold(pte_t pte)
2758c2ecf20Sopenharmony_ci	{ pte_val(pte) &= ~_PAGE_ACCESSED; return pte; }
2768c2ecf20Sopenharmony_cistatic inline pte_t pte_mkdirty(pte_t pte)
2778c2ecf20Sopenharmony_ci	{ pte_val(pte) |= _PAGE_DIRTY; return pte; }
2788c2ecf20Sopenharmony_cistatic inline pte_t pte_mkyoung(pte_t pte)
2798c2ecf20Sopenharmony_ci	{ pte_val(pte) |= _PAGE_ACCESSED; return pte; }
2808c2ecf20Sopenharmony_cistatic inline pte_t pte_mkwrite(pte_t pte)
2818c2ecf20Sopenharmony_ci	{ pte_val(pte) |= _PAGE_WRITABLE; return pte; }
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci#define pgprot_noncached(prot) (__pgprot(pgprot_val(prot) & ~_PAGE_CA_MASK))
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci/*
2868c2ecf20Sopenharmony_ci * Conversion functions: convert a page and protection to a page entry,
2878c2ecf20Sopenharmony_ci * and a page entry and page directory to the page they refer to.
2888c2ecf20Sopenharmony_ci */
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci#define pte_pfn(pte)		(pte_val(pte) >> PAGE_SHIFT)
2918c2ecf20Sopenharmony_ci#define pte_same(a,b)		(pte_val(a) == pte_val(b))
2928c2ecf20Sopenharmony_ci#define pte_page(x)		pfn_to_page(pte_pfn(x))
2938c2ecf20Sopenharmony_ci#define pfn_pte(pfn, prot)	__pte(((pfn) << PAGE_SHIFT) | pgprot_val(prot))
2948c2ecf20Sopenharmony_ci#define mk_pte(page, prot)	pfn_pte(page_to_pfn(page), prot)
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
2978c2ecf20Sopenharmony_ci{
2988c2ecf20Sopenharmony_ci	return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci/*
3028c2ecf20Sopenharmony_ci * Certain architectures need to do special things when pte's
3038c2ecf20Sopenharmony_ci * within a page table are directly modified.  Thus, the following
3048c2ecf20Sopenharmony_ci * hook is made available.
3058c2ecf20Sopenharmony_ci */
3068c2ecf20Sopenharmony_cistatic inline void update_pte(pte_t *ptep, pte_t pteval)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	*ptep = pteval;
3098c2ecf20Sopenharmony_ci#if (DCACHE_WAY_SIZE > PAGE_SIZE) && XCHAL_DCACHE_IS_WRITEBACK
3108c2ecf20Sopenharmony_ci	__asm__ __volatile__ ("dhwb %0, 0" :: "a" (ptep));
3118c2ecf20Sopenharmony_ci#endif
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_cistruct mm_struct;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cistatic inline void
3188c2ecf20Sopenharmony_ciset_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep, pte_t pteval)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	update_pte(ptep, pteval);
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistatic inline void set_pte(pte_t *ptep, pte_t pteval)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	update_pte(ptep, pteval);
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic inline void
3298c2ecf20Sopenharmony_ciset_pmd(pmd_t *pmdp, pmd_t pmdval)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	*pmdp = pmdval;
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistruct vm_area_struct;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic inline int
3378c2ecf20Sopenharmony_ciptep_test_and_clear_young(struct vm_area_struct *vma, unsigned long addr,
3388c2ecf20Sopenharmony_ci			  pte_t *ptep)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	pte_t pte = *ptep;
3418c2ecf20Sopenharmony_ci	if (!pte_young(pte))
3428c2ecf20Sopenharmony_ci		return 0;
3438c2ecf20Sopenharmony_ci	update_pte(ptep, pte_mkold(pte));
3448c2ecf20Sopenharmony_ci	return 1;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic inline pte_t
3488c2ecf20Sopenharmony_ciptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	pte_t pte = *ptep;
3518c2ecf20Sopenharmony_ci	pte_clear(mm, addr, ptep);
3528c2ecf20Sopenharmony_ci	return pte;
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic inline void
3568c2ecf20Sopenharmony_ciptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	pte_t pte = *ptep;
3598c2ecf20Sopenharmony_ci	update_pte(ptep, pte_wrprotect(pte));
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci/*
3638c2ecf20Sopenharmony_ci * Encode and decode a swap and file entry.
3648c2ecf20Sopenharmony_ci */
3658c2ecf20Sopenharmony_ci#define SWP_TYPE_BITS		5
3668c2ecf20Sopenharmony_ci#define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS)
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci#define __swp_type(entry)	(((entry).val >> 6) & 0x1f)
3698c2ecf20Sopenharmony_ci#define __swp_offset(entry)	((entry).val >> 11)
3708c2ecf20Sopenharmony_ci#define __swp_entry(type,offs)	\
3718c2ecf20Sopenharmony_ci	((swp_entry_t){((type) << 6) | ((offs) << 11) | \
3728c2ecf20Sopenharmony_ci	 _PAGE_CA_INVALID | _PAGE_USER})
3738c2ecf20Sopenharmony_ci#define __pte_to_swp_entry(pte)	((swp_entry_t) { pte_val(pte) })
3748c2ecf20Sopenharmony_ci#define __swp_entry_to_pte(x)	((pte_t) { (x).val })
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci#endif /*  !defined (__ASSEMBLY__) */
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci#ifdef __ASSEMBLY__
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci/* Assembly macro _PGD_INDEX is the same as C pgd_index(unsigned long),
3828c2ecf20Sopenharmony_ci *                _PGD_OFFSET as C pgd_offset(struct mm_struct*, unsigned long),
3838c2ecf20Sopenharmony_ci *                _PMD_OFFSET as C pmd_offset(pgd_t*, unsigned long)
3848c2ecf20Sopenharmony_ci *                _PTE_OFFSET as C pte_offset(pmd_t*, unsigned long)
3858c2ecf20Sopenharmony_ci *
3868c2ecf20Sopenharmony_ci * Note: We require an additional temporary register which can be the same as
3878c2ecf20Sopenharmony_ci *       the register that holds the address.
3888c2ecf20Sopenharmony_ci *
3898c2ecf20Sopenharmony_ci * ((pte_t*) ((unsigned long)(pmd_val(*pmd) & PAGE_MASK)) + pte_index(addr))
3908c2ecf20Sopenharmony_ci *
3918c2ecf20Sopenharmony_ci */
3928c2ecf20Sopenharmony_ci#define _PGD_INDEX(rt,rs)	extui	rt, rs, PGDIR_SHIFT, 32-PGDIR_SHIFT
3938c2ecf20Sopenharmony_ci#define _PTE_INDEX(rt,rs)	extui	rt, rs, PAGE_SHIFT, PTRS_PER_PTE_SHIFT
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci#define _PGD_OFFSET(mm,adr,tmp)		l32i	mm, mm, MM_PGD;		\
3968c2ecf20Sopenharmony_ci					_PGD_INDEX(tmp, adr);		\
3978c2ecf20Sopenharmony_ci					addx4	mm, tmp, mm
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci#define _PTE_OFFSET(pmd,adr,tmp)	_PTE_INDEX(tmp, adr);		\
4008c2ecf20Sopenharmony_ci					srli	pmd, pmd, PAGE_SHIFT;	\
4018c2ecf20Sopenharmony_ci					slli	pmd, pmd, PAGE_SHIFT;	\
4028c2ecf20Sopenharmony_ci					addx4	pmd, tmp, pmd
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci#else
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci#define kern_addr_valid(addr)	(1)
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ciextern  void update_mmu_cache(struct vm_area_struct * vma,
4098c2ecf20Sopenharmony_ci			      unsigned long address, pte_t *ptep);
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_citypedef pte_t *pte_addr_t;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci#endif /* !defined (__ASSEMBLY__) */
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
4168c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTEP_GET_AND_CLEAR
4178c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTEP_SET_WRPROTECT
4188c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTEP_MKDIRTY
4198c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTE_SAME
4208c2ecf20Sopenharmony_ci/* We provide our own get_unmapped_area to cope with
4218c2ecf20Sopenharmony_ci * SHM area cache aliasing for userland.
4228c2ecf20Sopenharmony_ci */
4238c2ecf20Sopenharmony_ci#define HAVE_ARCH_UNMAPPED_AREA
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci#endif /* _XTENSA_PGTABLE_H */
426