162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_NOHASH_32_PTE_8xx_H
362306a36Sopenharmony_ci#define _ASM_POWERPC_NOHASH_32_PTE_8xx_H
462306a36Sopenharmony_ci#ifdef __KERNEL__
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci/*
762306a36Sopenharmony_ci * The PowerPC MPC8xx uses a TLB with hardware assisted, software tablewalk.
862306a36Sopenharmony_ci * We also use the two level tables, but we can put the real bits in them
962306a36Sopenharmony_ci * needed for the TLB and tablewalk.  These definitions require Mx_CTR.PPM = 0,
1062306a36Sopenharmony_ci * Mx_CTR.PPCS = 0, and MD_CTR.TWAM = 1.  The level 2 descriptor has
1162306a36Sopenharmony_ci * additional page protection (when Mx_CTR.PPCS = 1) that allows TLB hit
1262306a36Sopenharmony_ci * based upon user/super access.  The TLB does not have accessed nor write
1362306a36Sopenharmony_ci * protect.  We assume that if the TLB get loaded with an entry it is
1462306a36Sopenharmony_ci * accessed, and overload the changed bit for write protect.  We use
1562306a36Sopenharmony_ci * two bits in the software pte that are supposed to be set to zero in
1662306a36Sopenharmony_ci * the TLB entry (24 and 25) for these indicators.  Although the level 1
1762306a36Sopenharmony_ci * descriptor contains the guarded and writethrough/copyback bits, we can
1862306a36Sopenharmony_ci * set these at the page level since they get copied from the Mx_TWC
1962306a36Sopenharmony_ci * register when the TLB entry is loaded.  We will use bit 27 for guard, since
2062306a36Sopenharmony_ci * that is where it exists in the MD_TWC, and bit 26 for writethrough.
2162306a36Sopenharmony_ci * These will get masked from the level 2 descriptor at TLB load time, and
2262306a36Sopenharmony_ci * copied to the MD_TWC before it gets loaded.
2362306a36Sopenharmony_ci * Large page sizes added.  We currently support two sizes, 4K and 8M.
2462306a36Sopenharmony_ci * This also allows a TLB hander optimization because we can directly
2562306a36Sopenharmony_ci * load the PMD into MD_TWC.  The 8M pages are only used for kernel
2662306a36Sopenharmony_ci * mapping of well known areas.  The PMD (PGD) entries contain control
2762306a36Sopenharmony_ci * flags in addition to the address, so care must be taken that the
2862306a36Sopenharmony_ci * software no longer assumes these are only pointers.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/* Definitions for 8xx embedded chips. */
3262306a36Sopenharmony_ci#define _PAGE_PRESENT	0x0001	/* V: Page is valid */
3362306a36Sopenharmony_ci#define _PAGE_NO_CACHE	0x0002	/* CI: cache inhibit */
3462306a36Sopenharmony_ci#define _PAGE_SH	0x0004	/* SH: No ASID (context) compare */
3562306a36Sopenharmony_ci#define _PAGE_SPS	0x0008	/* SPS: Small Page Size (1 if 16k, 512k or 8M)*/
3662306a36Sopenharmony_ci#define _PAGE_DIRTY	0x0100	/* C: page changed */
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* These 4 software bits must be masked out when the L2 entry is loaded
3962306a36Sopenharmony_ci * into the TLB.
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_ci#define _PAGE_GUARDED	0x0010	/* Copied to L1 G entry in DTLB */
4262306a36Sopenharmony_ci#define _PAGE_ACCESSED	0x0020	/* Copied to L1 APG 1 entry in I/DTLB */
4362306a36Sopenharmony_ci#define _PAGE_EXEC	0x0040	/* Copied to PP (bit 21) in ITLB */
4462306a36Sopenharmony_ci#define _PAGE_SPECIAL	0x0080	/* SW entry */
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define _PAGE_NA	0x0200	/* Supervisor NA, User no access */
4762306a36Sopenharmony_ci#define _PAGE_RO	0x0600	/* Supervisor RO, User no access */
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#define _PAGE_HUGE	0x0800	/* Copied to L1 PS bit 29 */
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* cache related flags non existing on 8xx */
5262306a36Sopenharmony_ci#define _PAGE_COHERENT	0
5362306a36Sopenharmony_ci#define _PAGE_WRITETHRU	0
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define _PAGE_KERNEL_RO		(_PAGE_SH | _PAGE_RO)
5662306a36Sopenharmony_ci#define _PAGE_KERNEL_ROX	(_PAGE_SH | _PAGE_RO | _PAGE_EXEC)
5762306a36Sopenharmony_ci#define _PAGE_KERNEL_RW		(_PAGE_SH | _PAGE_DIRTY)
5862306a36Sopenharmony_ci#define _PAGE_KERNEL_RWX	(_PAGE_SH | _PAGE_DIRTY | _PAGE_EXEC)
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#define _PMD_PRESENT	0x0001
6162306a36Sopenharmony_ci#define _PMD_PRESENT_MASK	_PMD_PRESENT
6262306a36Sopenharmony_ci#define _PMD_BAD	0x0f90
6362306a36Sopenharmony_ci#define _PMD_PAGE_MASK	0x000c
6462306a36Sopenharmony_ci#define _PMD_PAGE_8M	0x000c
6562306a36Sopenharmony_ci#define _PMD_PAGE_512K	0x0004
6662306a36Sopenharmony_ci#define _PMD_ACCESSED	0x0020	/* APG 1 */
6762306a36Sopenharmony_ci#define _PMD_USER	0x0040	/* APG 2 */
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci#define _PTE_NONE_MASK	0
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#ifdef CONFIG_PPC_16K_PAGES
7262306a36Sopenharmony_ci#define _PAGE_PSIZE	_PAGE_SPS
7362306a36Sopenharmony_ci#else
7462306a36Sopenharmony_ci#define _PAGE_PSIZE		0
7562306a36Sopenharmony_ci#endif
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define _PAGE_BASE_NC	(_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_PSIZE)
7862306a36Sopenharmony_ci#define _PAGE_BASE	(_PAGE_BASE_NC)
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/* Permission masks used to generate the __P and __S table */
8162306a36Sopenharmony_ci#define PAGE_NONE	__pgprot(_PAGE_BASE | _PAGE_NA)
8262306a36Sopenharmony_ci#define PAGE_SHARED	__pgprot(_PAGE_BASE)
8362306a36Sopenharmony_ci#define PAGE_SHARED_X	__pgprot(_PAGE_BASE | _PAGE_EXEC)
8462306a36Sopenharmony_ci#define PAGE_COPY	__pgprot(_PAGE_BASE | _PAGE_RO)
8562306a36Sopenharmony_ci#define PAGE_COPY_X	__pgprot(_PAGE_BASE | _PAGE_RO | _PAGE_EXEC)
8662306a36Sopenharmony_ci#define PAGE_READONLY	__pgprot(_PAGE_BASE | _PAGE_RO)
8762306a36Sopenharmony_ci#define PAGE_READONLY_X	__pgprot(_PAGE_BASE | _PAGE_RO | _PAGE_EXEC)
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci#ifndef __ASSEMBLY__
9062306a36Sopenharmony_cistatic inline pte_t pte_wrprotect(pte_t pte)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	return __pte(pte_val(pte) | _PAGE_RO);
9362306a36Sopenharmony_ci}
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci#define pte_wrprotect pte_wrprotect
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistatic inline int pte_read(pte_t pte)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	return (pte_val(pte) & _PAGE_RO) != _PAGE_NA;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#define pte_read pte_read
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cistatic inline int pte_write(pte_t pte)
10562306a36Sopenharmony_ci{
10662306a36Sopenharmony_ci	return !(pte_val(pte) & _PAGE_RO);
10762306a36Sopenharmony_ci}
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#define pte_write pte_write
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_cistatic inline pte_t pte_mkwrite_novma(pte_t pte)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_RO);
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci#define pte_mkwrite_novma pte_mkwrite_novma
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic inline bool pte_user(pte_t pte)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	return !(pte_val(pte) & _PAGE_SH);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci#define pte_user pte_user
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_cistatic inline pte_t pte_mkprivileged(pte_t pte)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	return __pte(pte_val(pte) | _PAGE_SH);
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci#define pte_mkprivileged pte_mkprivileged
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_cistatic inline pte_t pte_mkuser(pte_t pte)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_SH);
13562306a36Sopenharmony_ci}
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci#define pte_mkuser pte_mkuser
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistatic inline pte_t pte_mkhuge(pte_t pte)
14062306a36Sopenharmony_ci{
14162306a36Sopenharmony_ci	return __pte(pte_val(pte) | _PAGE_SPS | _PAGE_HUGE);
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci#define pte_mkhuge pte_mkhuge
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_cistatic inline pte_basic_t pte_update(struct mm_struct *mm, unsigned long addr, pte_t *p,
14762306a36Sopenharmony_ci				     unsigned long clr, unsigned long set, int huge);
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cistatic inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	pte_update(mm, addr, ptep, 0, _PAGE_RO, 0);
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_ci#define ptep_set_wrprotect ptep_set_wrprotect
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cistatic inline void __ptep_set_access_flags(struct vm_area_struct *vma, pte_t *ptep,
15662306a36Sopenharmony_ci					   pte_t entry, unsigned long address, int psize)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	unsigned long set = pte_val(entry) & (_PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_EXEC);
15962306a36Sopenharmony_ci	unsigned long clr = ~pte_val(entry) & _PAGE_RO;
16062306a36Sopenharmony_ci	int huge = psize > mmu_virtual_psize ? 1 : 0;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	pte_update(vma->vm_mm, address, ptep, clr, set, huge);
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	flush_tlb_page(vma, address);
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci#define __ptep_set_access_flags __ptep_set_access_flags
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistatic inline unsigned long pgd_leaf_size(pgd_t pgd)
16962306a36Sopenharmony_ci{
17062306a36Sopenharmony_ci	if (pgd_val(pgd) & _PMD_PAGE_8M)
17162306a36Sopenharmony_ci		return SZ_8M;
17262306a36Sopenharmony_ci	return SZ_4M;
17362306a36Sopenharmony_ci}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci#define pgd_leaf_size pgd_leaf_size
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_cistatic inline unsigned long pte_leaf_size(pte_t pte)
17862306a36Sopenharmony_ci{
17962306a36Sopenharmony_ci	pte_basic_t val = pte_val(pte);
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	if (val & _PAGE_HUGE)
18262306a36Sopenharmony_ci		return SZ_512K;
18362306a36Sopenharmony_ci	if (val & _PAGE_SPS)
18462306a36Sopenharmony_ci		return SZ_16K;
18562306a36Sopenharmony_ci	return SZ_4K;
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci#define pte_leaf_size pte_leaf_size
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci#endif
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci#endif /* __KERNEL__ */
19362306a36Sopenharmony_ci#endif /*  _ASM_POWERPC_NOHASH_32_PTE_8xx_H */
194