162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _SUN3_PGTABLE_H
362306a36Sopenharmony_ci#define _SUN3_PGTABLE_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <asm/sun3mmu.h>
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef __ASSEMBLY__
862306a36Sopenharmony_ci#include <asm/virtconvert.h>
962306a36Sopenharmony_ci#include <linux/linkage.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/*
1262306a36Sopenharmony_ci * This file contains all the things which change drastically for the sun3
1362306a36Sopenharmony_ci * pagetable stuff, to avoid making too much of a mess of the generic m68k
1462306a36Sopenharmony_ci * `pgtable.h'; this should only be included from the generic file. --m
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci/* For virtual address to physical address conversion */
1862306a36Sopenharmony_ci#define VTOP(addr)	__pa(addr)
1962306a36Sopenharmony_ci#define PTOV(addr)	__va(addr)
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#endif	/* !__ASSEMBLY__ */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/* These need to be defined for compatibility although the sun3 doesn't use them */
2562306a36Sopenharmony_ci#define _PAGE_NOCACHE030 0x040
2662306a36Sopenharmony_ci#define _CACHEMASK040   (~0x060)
2762306a36Sopenharmony_ci#define _PAGE_NOCACHE_S 0x040
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/* Page protection values within PTE. */
3062306a36Sopenharmony_ci#define SUN3_PAGE_VALID     (0x80000000)
3162306a36Sopenharmony_ci#define SUN3_PAGE_WRITEABLE (0x40000000)
3262306a36Sopenharmony_ci#define SUN3_PAGE_SYSTEM    (0x20000000)
3362306a36Sopenharmony_ci#define SUN3_PAGE_NOCACHE   (0x10000000)
3462306a36Sopenharmony_ci#define SUN3_PAGE_ACCESSED  (0x02000000)
3562306a36Sopenharmony_ci#define SUN3_PAGE_MODIFIED  (0x01000000)
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* Externally used page protection values. */
3962306a36Sopenharmony_ci#define _PAGE_PRESENT	(SUN3_PAGE_VALID)
4062306a36Sopenharmony_ci#define _PAGE_ACCESSED	(SUN3_PAGE_ACCESSED)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/* Compound page protection values. */
4362306a36Sopenharmony_ci//todo: work out which ones *should* have SUN3_PAGE_NOCACHE and fix...
4462306a36Sopenharmony_ci// is it just PAGE_KERNEL and PAGE_SHARED?
4562306a36Sopenharmony_ci#define PAGE_NONE	__pgprot(SUN3_PAGE_VALID \
4662306a36Sopenharmony_ci				 | SUN3_PAGE_ACCESSED \
4762306a36Sopenharmony_ci				 | SUN3_PAGE_NOCACHE)
4862306a36Sopenharmony_ci#define PAGE_SHARED	__pgprot(SUN3_PAGE_VALID \
4962306a36Sopenharmony_ci				 | SUN3_PAGE_WRITEABLE \
5062306a36Sopenharmony_ci				 | SUN3_PAGE_ACCESSED \
5162306a36Sopenharmony_ci				 | SUN3_PAGE_NOCACHE)
5262306a36Sopenharmony_ci#define PAGE_COPY	__pgprot(SUN3_PAGE_VALID \
5362306a36Sopenharmony_ci				 | SUN3_PAGE_ACCESSED \
5462306a36Sopenharmony_ci				 | SUN3_PAGE_NOCACHE)
5562306a36Sopenharmony_ci#define PAGE_READONLY	__pgprot(SUN3_PAGE_VALID \
5662306a36Sopenharmony_ci				 | SUN3_PAGE_ACCESSED \
5762306a36Sopenharmony_ci				 | SUN3_PAGE_NOCACHE)
5862306a36Sopenharmony_ci#define PAGE_KERNEL	__pgprot(SUN3_PAGE_VALID \
5962306a36Sopenharmony_ci				 | SUN3_PAGE_WRITEABLE \
6062306a36Sopenharmony_ci				 | SUN3_PAGE_SYSTEM \
6162306a36Sopenharmony_ci				 | SUN3_PAGE_NOCACHE \
6262306a36Sopenharmony_ci				 | SUN3_PAGE_ACCESSED \
6362306a36Sopenharmony_ci				 | SUN3_PAGE_MODIFIED)
6462306a36Sopenharmony_ci#define PAGE_INIT	__pgprot(SUN3_PAGE_VALID \
6562306a36Sopenharmony_ci				 | SUN3_PAGE_WRITEABLE \
6662306a36Sopenharmony_ci				 | SUN3_PAGE_SYSTEM \
6762306a36Sopenharmony_ci				 | SUN3_PAGE_NOCACHE)
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci/* Use these fake page-protections on PMDs. */
7062306a36Sopenharmony_ci#define SUN3_PMD_VALID	(0x00000001)
7162306a36Sopenharmony_ci#define SUN3_PMD_MASK	(0x0000003F)
7262306a36Sopenharmony_ci#define SUN3_PMD_MAGIC	(0x0000002B)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/* We borrow bit 6 to store the exclusive marker in swap PTEs. */
7562306a36Sopenharmony_ci#define _PAGE_SWP_EXCLUSIVE	0x040
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#ifndef __ASSEMBLY__
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/*
8062306a36Sopenharmony_ci * Conversion functions: convert a page and protection to a page entry,
8162306a36Sopenharmony_ci * and a page entry and page directory to the page they refer to.
8262306a36Sopenharmony_ci */
8362306a36Sopenharmony_ci#define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot))
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistatic inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	pte_val(pte) = (pte_val(pte) & SUN3_PAGE_CHG_MASK) | pgprot_val(newprot);
8862306a36Sopenharmony_ci	return pte;
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci#define pmd_set(pmdp,ptep) do {} while (0)
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#define __pte_page(pte) \
9462306a36Sopenharmony_ci(__va ((pte_val (pte) & SUN3_PAGE_PGNUM_MASK) << PAGE_SHIFT))
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_cistatic inline unsigned long pmd_page_vaddr(pmd_t pmd)
9762306a36Sopenharmony_ci{
9862306a36Sopenharmony_ci	return (unsigned long)__va(pmd_val(pmd) & PAGE_MASK);
9962306a36Sopenharmony_ci}
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_cistatic inline int pte_none (pte_t pte) { return !pte_val (pte); }
10262306a36Sopenharmony_cistatic inline int pte_present (pte_t pte) { return pte_val (pte) & SUN3_PAGE_VALID; }
10362306a36Sopenharmony_cistatic inline void pte_clear (struct mm_struct *mm, unsigned long addr, pte_t *ptep)
10462306a36Sopenharmony_ci{
10562306a36Sopenharmony_ci	pte_val (*ptep) = 0;
10662306a36Sopenharmony_ci}
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci#define PFN_PTE_SHIFT		0
10962306a36Sopenharmony_ci#define pte_pfn(pte)            (pte_val(pte) & SUN3_PAGE_PGNUM_MASK)
11062306a36Sopenharmony_ci#define pfn_pte(pfn, pgprot) \
11162306a36Sopenharmony_ci({ pte_t __pte; pte_val(__pte) = pfn | pgprot_val(pgprot); __pte; })
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci#define pte_page(pte)		virt_to_page(__pte_page(pte))
11462306a36Sopenharmony_ci#define pmd_pfn(pmd)		(pmd_val(pmd) >> PAGE_SHIFT)
11562306a36Sopenharmony_ci#define pmd_page(pmd)		virt_to_page((void *)pmd_page_vaddr(pmd))
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic inline int pmd_none2 (pmd_t *pmd) { return !pmd_val (*pmd); }
11962306a36Sopenharmony_ci#define pmd_none(pmd) pmd_none2(&(pmd))
12062306a36Sopenharmony_ci//static inline int pmd_bad (pmd_t pmd) { return (pmd_val (pmd) & SUN3_PMD_MASK) != SUN3_PMD_MAGIC; }
12162306a36Sopenharmony_cistatic inline int pmd_bad2 (pmd_t *pmd) { return 0; }
12262306a36Sopenharmony_ci#define pmd_bad(pmd) pmd_bad2(&(pmd))
12362306a36Sopenharmony_cistatic inline int pmd_present2 (pmd_t *pmd) { return pmd_val (*pmd) & SUN3_PMD_VALID; }
12462306a36Sopenharmony_ci/* #define pmd_present(pmd) pmd_present2(&(pmd)) */
12562306a36Sopenharmony_ci#define pmd_present(pmd) (!pmd_none2(&(pmd)))
12662306a36Sopenharmony_cistatic inline void pmd_clear (pmd_t *pmdp) { pmd_val (*pmdp) = 0; }
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci#define pte_ERROR(e) \
13062306a36Sopenharmony_ci	pr_err("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, pte_val(e))
13162306a36Sopenharmony_ci#define pgd_ERROR(e) \
13262306a36Sopenharmony_ci	pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e))
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci/*
13662306a36Sopenharmony_ci * The following only work if pte_present() is true.
13762306a36Sopenharmony_ci * Undefined behaviour if not...
13862306a36Sopenharmony_ci * [we have the full set here even if they don't change from m68k]
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_cistatic inline int pte_write(pte_t pte)		{ return pte_val(pte) & SUN3_PAGE_WRITEABLE; }
14162306a36Sopenharmony_cistatic inline int pte_dirty(pte_t pte)		{ return pte_val(pte) & SUN3_PAGE_MODIFIED; }
14262306a36Sopenharmony_cistatic inline int pte_young(pte_t pte)		{ return pte_val(pte) & SUN3_PAGE_ACCESSED; }
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_cistatic inline pte_t pte_wrprotect(pte_t pte)	{ pte_val(pte) &= ~SUN3_PAGE_WRITEABLE; return pte; }
14562306a36Sopenharmony_cistatic inline pte_t pte_mkclean(pte_t pte)	{ pte_val(pte) &= ~SUN3_PAGE_MODIFIED; return pte; }
14662306a36Sopenharmony_cistatic inline pte_t pte_mkold(pte_t pte)	{ pte_val(pte) &= ~SUN3_PAGE_ACCESSED; return pte; }
14762306a36Sopenharmony_cistatic inline pte_t pte_mkwrite_novma(pte_t pte){ pte_val(pte) |= SUN3_PAGE_WRITEABLE; return pte; }
14862306a36Sopenharmony_cistatic inline pte_t pte_mkdirty(pte_t pte)	{ pte_val(pte) |= SUN3_PAGE_MODIFIED; return pte; }
14962306a36Sopenharmony_cistatic inline pte_t pte_mkyoung(pte_t pte)	{ pte_val(pte) |= SUN3_PAGE_ACCESSED; return pte; }
15062306a36Sopenharmony_cistatic inline pte_t pte_mknocache(pte_t pte)	{ pte_val(pte) |= SUN3_PAGE_NOCACHE; return pte; }
15162306a36Sopenharmony_ci// use this version when caches work...
15262306a36Sopenharmony_ci//static inline pte_t pte_mkcache(pte_t pte)	{ pte_val(pte) &= SUN3_PAGE_NOCACHE; return pte; }
15362306a36Sopenharmony_ci// until then, use:
15462306a36Sopenharmony_cistatic inline pte_t pte_mkcache(pte_t pte)	{ return pte; }
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciextern pgd_t swapper_pg_dir[PTRS_PER_PGD];
15762306a36Sopenharmony_ciextern pgd_t kernel_pg_dir[PTRS_PER_PGD];
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci/*
16062306a36Sopenharmony_ci * Encode/decode swap entries and swap PTEs. Swap PTEs are all PTEs that
16162306a36Sopenharmony_ci * are !pte_none() && !pte_present().
16262306a36Sopenharmony_ci *
16362306a36Sopenharmony_ci * Format of swap PTEs:
16462306a36Sopenharmony_ci *
16562306a36Sopenharmony_ci *   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
16662306a36Sopenharmony_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
16762306a36Sopenharmony_ci *   0 <--------------------- offset ----------------> E <- type -->
16862306a36Sopenharmony_ci *
16962306a36Sopenharmony_ci *   E is the exclusive marker that is not stored in swap entries.
17062306a36Sopenharmony_ci */
17162306a36Sopenharmony_ci#define __swp_type(x)		((x).val & 0x3f)
17262306a36Sopenharmony_ci#define __swp_offset(x)		(((x).val) >> 7)
17362306a36Sopenharmony_ci#define __swp_entry(type, offset) ((swp_entry_t) { (((type) & 0x3f) | \
17462306a36Sopenharmony_ci						   (((offset) << 7) & ~SUN3_PAGE_VALID)) })
17562306a36Sopenharmony_ci#define __pte_to_swp_entry(pte)	((swp_entry_t) { pte_val(pte) })
17662306a36Sopenharmony_ci#define __swp_entry_to_pte(x)	((pte_t) { (x).val })
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cistatic inline int pte_swp_exclusive(pte_t pte)
17962306a36Sopenharmony_ci{
18062306a36Sopenharmony_ci	return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
18162306a36Sopenharmony_ci}
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_cistatic inline pte_t pte_swp_mkexclusive(pte_t pte)
18462306a36Sopenharmony_ci{
18562306a36Sopenharmony_ci	pte_val(pte) |= _PAGE_SWP_EXCLUSIVE;
18662306a36Sopenharmony_ci	return pte;
18762306a36Sopenharmony_ci}
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_cistatic inline pte_t pte_swp_clear_exclusive(pte_t pte)
19062306a36Sopenharmony_ci{
19162306a36Sopenharmony_ci	pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE;
19262306a36Sopenharmony_ci	return pte;
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci#endif	/* !__ASSEMBLY__ */
19662306a36Sopenharmony_ci#endif	/* !_SUN3_PGTABLE_H */
197