1013de2d6SGuo Ren /* SPDX-License-Identifier: GPL-2.0 */ 2013de2d6SGuo Ren 3013de2d6SGuo Ren #ifndef __ASM_CSKY_PGTABLE_H 4013de2d6SGuo Ren #define __ASM_CSKY_PGTABLE_H 5013de2d6SGuo Ren 6013de2d6SGuo Ren #include <asm/fixmap.h> 7f136008fSGuo Ren #include <asm/memory.h> 8013de2d6SGuo Ren #include <asm/addrspace.h> 9013de2d6SGuo Ren #include <abi/pgtable-bits.h> 10013de2d6SGuo Ren #include <asm-generic/pgtable-nopmd.h> 11013de2d6SGuo Ren 12013de2d6SGuo Ren #define PGDIR_SHIFT 22 13013de2d6SGuo Ren #define PGDIR_SIZE (1UL << PGDIR_SHIFT) 14013de2d6SGuo Ren #define PGDIR_MASK (~(PGDIR_SIZE-1)) 15013de2d6SGuo Ren 160c8a32eeSGuo Ren #define USER_PTRS_PER_PGD (PAGE_OFFSET/PGDIR_SIZE) 17013de2d6SGuo Ren 18013de2d6SGuo Ren /* 19013de2d6SGuo Ren * C-SKY is two-level paging structure: 20013de2d6SGuo Ren */ 21013de2d6SGuo Ren 22f7536442SMike Rapoport #define PTRS_PER_PGD (PAGE_SIZE / sizeof(pgd_t)) 23013de2d6SGuo Ren #define PTRS_PER_PMD 1 24a008a300SMike Rapoport #define PTRS_PER_PTE (PAGE_SIZE / sizeof(pte_t)) 25013de2d6SGuo Ren 26013de2d6SGuo Ren #define pte_ERROR(e) \ 27013de2d6SGuo Ren pr_err("%s:%d: bad pte %08lx.\n", __FILE__, __LINE__, (e).pte_low) 28013de2d6SGuo Ren #define pgd_ERROR(e) \ 29013de2d6SGuo Ren pr_err("%s:%d: bad pgd %08lx.\n", __FILE__, __LINE__, pgd_val(e)) 30013de2d6SGuo Ren 31*e724e7aaSMatthew Wilcox (Oracle) #define PFN_PTE_SHIFT PAGE_SHIFT 327106c51eSMike Rapoport #define pmd_pfn(pmd) (pmd_phys(pmd) >> PAGE_SHIFT) 33013de2d6SGuo Ren #define pmd_page(pmd) (pfn_to_page(pmd_phys(pmd) >> PAGE_SHIFT)) 34013de2d6SGuo Ren #define pte_clear(mm, addr, ptep) set_pte((ptep), \ 357b513cf2SGuo Ren (((unsigned int) addr >= PAGE_OFFSET) ? __pte(_PAGE_GLOBAL) : __pte(0))) 3662eebea6SGuo Ren #define pte_none(pte) (!(pte_val(pte) & ~_PAGE_GLOBAL)) 37013de2d6SGuo Ren #define pte_present(pte) (pte_val(pte) & _PAGE_PRESENT) 38013de2d6SGuo Ren #define pte_pfn(x) ((unsigned long)((x).pte_low >> PAGE_SHIFT)) 39013de2d6SGuo Ren #define pfn_pte(pfn, prot) __pte(((unsigned long long)(pfn) << PAGE_SHIFT) \ 40013de2d6SGuo Ren | pgprot_val(prot)) 41013de2d6SGuo Ren 42013de2d6SGuo Ren #define __pte_to_swp_entry(pte) ((swp_entry_t) { pte_val(pte) }) 43013de2d6SGuo Ren #define __swp_entry_to_pte(x) ((pte_t) { (x).val }) 44013de2d6SGuo Ren 45013de2d6SGuo Ren #define pte_page(x) pfn_to_page(pte_pfn(x)) 46013de2d6SGuo Ren #define __mk_pte(page_nr, pgprot) __pte(((page_nr) << PAGE_SHIFT) | \ 47013de2d6SGuo Ren pgprot_val(pgprot)) 48013de2d6SGuo Ren 49013de2d6SGuo Ren /* 50a8fac05aSGuo Ren * C-SKY only has VALID and DIRTY bit in hardware. So we need to use the 51a8fac05aSGuo Ren * two bits emulate PRESENT, READ, WRITE, EXEC, MODIFIED, ACCESSED. 52013de2d6SGuo Ren */ 535e144c42SGuo Ren #define _PAGE_BASE (_PAGE_PRESENT | _PAGE_ACCESSED) 545e144c42SGuo Ren 55af94002bSGuo Ren #define PAGE_NONE __pgprot(_PAGE_PROT_NONE) 56a8fac05aSGuo Ren #define PAGE_READ __pgprot(_PAGE_BASE | _PAGE_READ | \ 57a8fac05aSGuo Ren _CACHE_CACHED) 58a8fac05aSGuo Ren #define PAGE_WRITE __pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_WRITE | \ 59a8fac05aSGuo Ren _CACHE_CACHED) 60a8fac05aSGuo Ren #define PAGE_SHARED PAGE_WRITE 61a8fac05aSGuo Ren 62a8fac05aSGuo Ren #define PAGE_KERNEL __pgprot(_PAGE_BASE | _PAGE_READ | _PAGE_VALID | \ 63a8fac05aSGuo Ren _PAGE_WRITE | _PAGE_DIRTY | _PAGE_MODIFIED | \ 64a8fac05aSGuo Ren _PAGE_GLOBAL | \ 65013de2d6SGuo Ren _CACHE_CACHED) 66013de2d6SGuo Ren 67a8fac05aSGuo Ren #define _PAGE_IOREMAP (_PAGE_BASE | _PAGE_READ | _PAGE_VALID | \ 68a8fac05aSGuo Ren _PAGE_WRITE | _PAGE_DIRTY | _PAGE_MODIFIED | \ 69a8fac05aSGuo Ren _PAGE_GLOBAL | \ 700055f67bSChristoph Hellwig _CACHE_UNCACHED | _PAGE_SO) 710055f67bSChristoph Hellwig 72af94002bSGuo Ren #define _PAGE_CHG_MASK (~(unsigned long) \ 73af94002bSGuo Ren (_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE | \ 74af94002bSGuo Ren _CACHE_MASK | _PAGE_GLOBAL)) 75af94002bSGuo Ren 76af94002bSGuo Ren #define MAX_SWAPFILES_CHECK() \ 77af94002bSGuo Ren BUILD_BUG_ON(MAX_SWAPFILES_SHIFT != 5) 78af94002bSGuo Ren 79013de2d6SGuo Ren extern unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]; 80013de2d6SGuo Ren #define ZERO_PAGE(vaddr) (virt_to_page(empty_zero_page)) 81013de2d6SGuo Ren 82013de2d6SGuo Ren extern void load_pgd(unsigned long pg_dir); 83013de2d6SGuo Ren extern pte_t invalid_pte_table[PTRS_PER_PTE]; 84013de2d6SGuo Ren 85013de2d6SGuo Ren static inline void set_pte(pte_t *p, pte_t pte) 86013de2d6SGuo Ren { 87013de2d6SGuo Ren *p = pte; 88013de2d6SGuo Ren #if defined(CONFIG_CPU_NEED_TLBSYNC) 89013de2d6SGuo Ren dcache_wb_line((u32)p); 90013de2d6SGuo Ren #endif 91013de2d6SGuo Ren /* prevent out of order excution */ 92013de2d6SGuo Ren smp_mb(); 93013de2d6SGuo Ren } 94013de2d6SGuo Ren 95013de2d6SGuo Ren static inline pte_t *pmd_page_vaddr(pmd_t pmd) 96013de2d6SGuo Ren { 97013de2d6SGuo Ren unsigned long ptr; 98013de2d6SGuo Ren 99013de2d6SGuo Ren ptr = pmd_val(pmd); 100013de2d6SGuo Ren 101013de2d6SGuo Ren return __va(ptr); 102013de2d6SGuo Ren } 103013de2d6SGuo Ren 104013de2d6SGuo Ren #define pmd_phys(pmd) pmd_val(pmd) 105013de2d6SGuo Ren 106013de2d6SGuo Ren static inline void set_pmd(pmd_t *p, pmd_t pmd) 107013de2d6SGuo Ren { 108013de2d6SGuo Ren *p = pmd; 109013de2d6SGuo Ren #if defined(CONFIG_CPU_NEED_TLBSYNC) 110013de2d6SGuo Ren dcache_wb_line((u32)p); 111013de2d6SGuo Ren #endif 112013de2d6SGuo Ren /* prevent specul excute */ 113013de2d6SGuo Ren smp_mb(); 114013de2d6SGuo Ren } 115013de2d6SGuo Ren 116013de2d6SGuo Ren 117013de2d6SGuo Ren static inline int pmd_none(pmd_t pmd) 118013de2d6SGuo Ren { 119013de2d6SGuo Ren return pmd_val(pmd) == __pa(invalid_pte_table); 120013de2d6SGuo Ren } 121013de2d6SGuo Ren 122013de2d6SGuo Ren #define pmd_bad(pmd) (pmd_val(pmd) & ~PAGE_MASK) 123013de2d6SGuo Ren 124013de2d6SGuo Ren static inline int pmd_present(pmd_t pmd) 125013de2d6SGuo Ren { 126013de2d6SGuo Ren return (pmd_val(pmd) != __pa(invalid_pte_table)); 127013de2d6SGuo Ren } 128013de2d6SGuo Ren 129013de2d6SGuo Ren static inline void pmd_clear(pmd_t *p) 130013de2d6SGuo Ren { 131013de2d6SGuo Ren pmd_val(*p) = (__pa(invalid_pte_table)); 132013de2d6SGuo Ren #if defined(CONFIG_CPU_NEED_TLBSYNC) 133013de2d6SGuo Ren dcache_wb_line((u32)p); 134013de2d6SGuo Ren #endif 135013de2d6SGuo Ren } 136013de2d6SGuo Ren 137013de2d6SGuo Ren /* 138013de2d6SGuo Ren * The following only work if pte_present() is true. 139013de2d6SGuo Ren * Undefined behaviour if not.. 140013de2d6SGuo Ren */ 141013de2d6SGuo Ren static inline int pte_read(pte_t pte) 142013de2d6SGuo Ren { 143013de2d6SGuo Ren return pte.pte_low & _PAGE_READ; 144013de2d6SGuo Ren } 145013de2d6SGuo Ren 146013de2d6SGuo Ren static inline int pte_write(pte_t pte) 147013de2d6SGuo Ren { 148013de2d6SGuo Ren return (pte).pte_low & _PAGE_WRITE; 149013de2d6SGuo Ren } 150013de2d6SGuo Ren 151013de2d6SGuo Ren static inline int pte_dirty(pte_t pte) 152013de2d6SGuo Ren { 153013de2d6SGuo Ren return (pte).pte_low & _PAGE_MODIFIED; 154013de2d6SGuo Ren } 155013de2d6SGuo Ren 156013de2d6SGuo Ren static inline int pte_young(pte_t pte) 157013de2d6SGuo Ren { 158013de2d6SGuo Ren return (pte).pte_low & _PAGE_ACCESSED; 159013de2d6SGuo Ren } 160013de2d6SGuo Ren 161013de2d6SGuo Ren static inline pte_t pte_wrprotect(pte_t pte) 162013de2d6SGuo Ren { 163013de2d6SGuo Ren pte_val(pte) &= ~(_PAGE_WRITE | _PAGE_DIRTY); 164013de2d6SGuo Ren return pte; 165013de2d6SGuo Ren } 166013de2d6SGuo Ren 167013de2d6SGuo Ren static inline pte_t pte_mkclean(pte_t pte) 168013de2d6SGuo Ren { 169013de2d6SGuo Ren pte_val(pte) &= ~(_PAGE_MODIFIED|_PAGE_DIRTY); 170013de2d6SGuo Ren return pte; 171013de2d6SGuo Ren } 172013de2d6SGuo Ren 173013de2d6SGuo Ren static inline pte_t pte_mkold(pte_t pte) 174013de2d6SGuo Ren { 175013de2d6SGuo Ren pte_val(pte) &= ~(_PAGE_ACCESSED|_PAGE_VALID); 176013de2d6SGuo Ren return pte; 177013de2d6SGuo Ren } 178013de2d6SGuo Ren 179013de2d6SGuo Ren static inline pte_t pte_mkwrite(pte_t pte) 180013de2d6SGuo Ren { 181013de2d6SGuo Ren pte_val(pte) |= _PAGE_WRITE; 182013de2d6SGuo Ren if (pte_val(pte) & _PAGE_MODIFIED) 183013de2d6SGuo Ren pte_val(pte) |= _PAGE_DIRTY; 184013de2d6SGuo Ren return pte; 185013de2d6SGuo Ren } 186013de2d6SGuo Ren 187013de2d6SGuo Ren static inline pte_t pte_mkdirty(pte_t pte) 188013de2d6SGuo Ren { 189013de2d6SGuo Ren pte_val(pte) |= _PAGE_MODIFIED; 190013de2d6SGuo Ren if (pte_val(pte) & _PAGE_WRITE) 191013de2d6SGuo Ren pte_val(pte) |= _PAGE_DIRTY; 192013de2d6SGuo Ren return pte; 193013de2d6SGuo Ren } 194013de2d6SGuo Ren 195013de2d6SGuo Ren static inline pte_t pte_mkyoung(pte_t pte) 196013de2d6SGuo Ren { 197013de2d6SGuo Ren pte_val(pte) |= _PAGE_ACCESSED; 198013de2d6SGuo Ren if (pte_val(pte) & _PAGE_READ) 199013de2d6SGuo Ren pte_val(pte) |= _PAGE_VALID; 200013de2d6SGuo Ren return pte; 201013de2d6SGuo Ren } 202013de2d6SGuo Ren 20341e0d491SDavid Hildenbrand static inline int pte_swp_exclusive(pte_t pte) 20441e0d491SDavid Hildenbrand { 20541e0d491SDavid Hildenbrand return pte_val(pte) & _PAGE_SWP_EXCLUSIVE; 20641e0d491SDavid Hildenbrand } 20741e0d491SDavid Hildenbrand 20841e0d491SDavid Hildenbrand static inline pte_t pte_swp_mkexclusive(pte_t pte) 20941e0d491SDavid Hildenbrand { 21041e0d491SDavid Hildenbrand pte_val(pte) |= _PAGE_SWP_EXCLUSIVE; 21141e0d491SDavid Hildenbrand return pte; 21241e0d491SDavid Hildenbrand } 21341e0d491SDavid Hildenbrand 21441e0d491SDavid Hildenbrand static inline pte_t pte_swp_clear_exclusive(pte_t pte) 21541e0d491SDavid Hildenbrand { 21641e0d491SDavid Hildenbrand pte_val(pte) &= ~_PAGE_SWP_EXCLUSIVE; 21741e0d491SDavid Hildenbrand return pte; 21841e0d491SDavid Hildenbrand } 21941e0d491SDavid Hildenbrand 22076d21d18SGuo Ren #define __HAVE_PHYS_MEM_ACCESS_PROT 22176d21d18SGuo Ren struct file; 22276d21d18SGuo Ren extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, 22376d21d18SGuo Ren unsigned long size, pgprot_t vma_prot); 22476d21d18SGuo Ren 225013de2d6SGuo Ren /* 226013de2d6SGuo Ren * Macro to make mark a page protection value as "uncacheable". Note 227013de2d6SGuo Ren * that "protection" is really a misnomer here as the protection value 228013de2d6SGuo Ren * contains the memory attribute bits, dirty bits, and various other 229013de2d6SGuo Ren * bits as well. 230013de2d6SGuo Ren */ 231013de2d6SGuo Ren #define pgprot_noncached pgprot_noncached 232013de2d6SGuo Ren 233013de2d6SGuo Ren static inline pgprot_t pgprot_noncached(pgprot_t _prot) 234013de2d6SGuo Ren { 235013de2d6SGuo Ren unsigned long prot = pgprot_val(_prot); 236013de2d6SGuo Ren 2377f80fe20SGuo Ren prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED | _PAGE_SO; 2387f80fe20SGuo Ren 2397f80fe20SGuo Ren return __pgprot(prot); 2407f80fe20SGuo Ren } 2417f80fe20SGuo Ren 2427f80fe20SGuo Ren #define pgprot_writecombine pgprot_writecombine 2437f80fe20SGuo Ren static inline pgprot_t pgprot_writecombine(pgprot_t _prot) 2447f80fe20SGuo Ren { 2457f80fe20SGuo Ren unsigned long prot = pgprot_val(_prot); 2467f80fe20SGuo Ren 247013de2d6SGuo Ren prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED; 248013de2d6SGuo Ren 249013de2d6SGuo Ren return __pgprot(prot); 250013de2d6SGuo Ren } 251013de2d6SGuo Ren 252013de2d6SGuo Ren /* 253013de2d6SGuo Ren * Conversion functions: convert a page and protection to a page entry, 254013de2d6SGuo Ren * and a page entry and page directory to the page they refer to. 255013de2d6SGuo Ren */ 256013de2d6SGuo Ren #define mk_pte(page, pgprot) pfn_pte(page_to_pfn(page), (pgprot)) 257013de2d6SGuo Ren static inline pte_t pte_modify(pte_t pte, pgprot_t newprot) 258013de2d6SGuo Ren { 259013de2d6SGuo Ren return __pte((pte_val(pte) & _PAGE_CHG_MASK) | 260013de2d6SGuo Ren (pgprot_val(newprot))); 261013de2d6SGuo Ren } 262013de2d6SGuo Ren 263013de2d6SGuo Ren extern pgd_t swapper_pg_dir[PTRS_PER_PGD]; 264013de2d6SGuo Ren extern void paging_init(void); 265013de2d6SGuo Ren 266*e724e7aaSMatthew Wilcox (Oracle) void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma, 267*e724e7aaSMatthew Wilcox (Oracle) unsigned long address, pte_t *pte, unsigned int nr); 268*e724e7aaSMatthew Wilcox (Oracle) #define update_mmu_cache(vma, addr, ptep) \ 269*e724e7aaSMatthew Wilcox (Oracle) update_mmu_cache_range(NULL, vma, addr, ptep, 1) 270013de2d6SGuo Ren 271013de2d6SGuo Ren #define io_remap_pfn_range(vma, vaddr, pfn, size, prot) \ 272013de2d6SGuo Ren remap_pfn_range(vma, vaddr, pfn, size, prot) 273013de2d6SGuo Ren 274013de2d6SGuo Ren #endif /* __ASM_CSKY_PGTABLE_H */ 275