1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright (C) 2009 Chen Liqin <liqin.chen@sunplusct.com> 4 * Copyright (C) 2012 Regents of the University of California 5 * Copyright (C) 2017 SiFive 6 * Copyright (C) 2017 XiaojingZhu <zhuxiaoj@ict.ac.cn> 7 */ 8 9 #ifndef _ASM_RISCV_PAGE_H 10 #define _ASM_RISCV_PAGE_H 11 12 #include <linux/pfn.h> 13 #include <linux/const.h> 14 15 #define PAGE_SHIFT (12) 16 #define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT) 17 #define PAGE_MASK (~(PAGE_SIZE - 1)) 18 19 #ifdef CONFIG_64BIT 20 #define HUGE_MAX_HSTATE 2 21 #else 22 #define HUGE_MAX_HSTATE 1 23 #endif 24 #define HPAGE_SHIFT PMD_SHIFT 25 #define HPAGE_SIZE (_AC(1, UL) << HPAGE_SHIFT) 26 #define HPAGE_MASK (~(HPAGE_SIZE - 1)) 27 #define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT) 28 29 /* 30 * PAGE_OFFSET -- the first address of the first page of memory. 31 * When not using MMU this corresponds to the first free page in 32 * physical memory (aligned on a page boundary). 33 */ 34 #ifdef CONFIG_64BIT 35 #ifdef CONFIG_MMU 36 #define PAGE_OFFSET kernel_map.page_offset 37 #else 38 #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL) 39 #endif 40 /* 41 * By default, CONFIG_PAGE_OFFSET value corresponds to SV48 address space so 42 * define the PAGE_OFFSET value for SV39. 43 */ 44 #define PAGE_OFFSET_L3 _AC(0xffffffd800000000, UL) 45 #else 46 #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL) 47 #endif /* CONFIG_64BIT */ 48 49 #ifndef __ASSEMBLY__ 50 51 #define clear_page(pgaddr) memset((pgaddr), 0, PAGE_SIZE) 52 #define copy_page(to, from) memcpy((to), (from), PAGE_SIZE) 53 54 #define clear_user_page(pgaddr, vaddr, page) memset((pgaddr), 0, PAGE_SIZE) 55 #define copy_user_page(vto, vfrom, vaddr, topg) \ 56 memcpy((vto), (vfrom), PAGE_SIZE) 57 58 /* 59 * Use struct definitions to apply C type checking 60 */ 61 62 /* Page Global Directory entry */ 63 typedef struct { 64 unsigned long pgd; 65 } pgd_t; 66 67 /* Page Table entry */ 68 typedef struct { 69 unsigned long pte; 70 } pte_t; 71 72 typedef struct { 73 unsigned long pgprot; 74 } pgprot_t; 75 76 typedef struct page *pgtable_t; 77 78 #define pte_val(x) ((x).pte) 79 #define pgd_val(x) ((x).pgd) 80 #define pgprot_val(x) ((x).pgprot) 81 82 #define __pte(x) ((pte_t) { (x) }) 83 #define __pgd(x) ((pgd_t) { (x) }) 84 #define __pgprot(x) ((pgprot_t) { (x) }) 85 86 #ifdef CONFIG_64BIT 87 #define PTE_FMT "%016lx" 88 #else 89 #define PTE_FMT "%08lx" 90 #endif 91 92 #ifdef CONFIG_MMU 93 extern unsigned long riscv_pfn_base; 94 #define ARCH_PFN_OFFSET (riscv_pfn_base) 95 #else 96 #define ARCH_PFN_OFFSET (PAGE_OFFSET >> PAGE_SHIFT) 97 #endif /* CONFIG_MMU */ 98 99 struct kernel_mapping { 100 unsigned long page_offset; 101 unsigned long virt_addr; 102 uintptr_t phys_addr; 103 uintptr_t size; 104 /* Offset between linear mapping virtual address and kernel load address */ 105 unsigned long va_pa_offset; 106 /* Offset between kernel mapping virtual address and kernel load address */ 107 unsigned long va_kernel_pa_offset; 108 unsigned long va_kernel_xip_pa_offset; 109 #ifdef CONFIG_XIP_KERNEL 110 uintptr_t xiprom; 111 uintptr_t xiprom_sz; 112 #endif 113 }; 114 115 extern struct kernel_mapping kernel_map; 116 extern phys_addr_t phys_ram_base; 117 118 #define is_kernel_mapping(x) \ 119 ((x) >= kernel_map.virt_addr && (x) < (kernel_map.virt_addr + kernel_map.size)) 120 121 #define is_linear_mapping(x) \ 122 ((x) >= PAGE_OFFSET && (!IS_ENABLED(CONFIG_64BIT) || (x) < PAGE_OFFSET + KERN_VIRT_SIZE)) 123 124 #define linear_mapping_pa_to_va(x) ((void *)((unsigned long)(x) + kernel_map.va_pa_offset)) 125 #define kernel_mapping_pa_to_va(y) ({ \ 126 unsigned long _y = y; \ 127 (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < phys_ram_base) ? \ 128 (void *)((unsigned long)(_y) + kernel_map.va_kernel_xip_pa_offset) : \ 129 (void *)((unsigned long)(_y) + kernel_map.va_kernel_pa_offset + XIP_OFFSET); \ 130 }) 131 #define __pa_to_va_nodebug(x) linear_mapping_pa_to_va(x) 132 133 #define linear_mapping_va_to_pa(x) ((unsigned long)(x) - kernel_map.va_pa_offset) 134 #define kernel_mapping_va_to_pa(y) ({ \ 135 unsigned long _y = y; \ 136 (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \ 137 ((unsigned long)(_y) - kernel_map.va_kernel_xip_pa_offset) : \ 138 ((unsigned long)(_y) - kernel_map.va_kernel_pa_offset - XIP_OFFSET); \ 139 }) 140 141 #define __va_to_pa_nodebug(x) ({ \ 142 unsigned long _x = x; \ 143 is_linear_mapping(_x) ? \ 144 linear_mapping_va_to_pa(_x) : kernel_mapping_va_to_pa(_x); \ 145 }) 146 147 #ifdef CONFIG_DEBUG_VIRTUAL 148 extern phys_addr_t __virt_to_phys(unsigned long x); 149 extern phys_addr_t __phys_addr_symbol(unsigned long x); 150 #else 151 #define __virt_to_phys(x) __va_to_pa_nodebug(x) 152 #define __phys_addr_symbol(x) __va_to_pa_nodebug(x) 153 #endif /* CONFIG_DEBUG_VIRTUAL */ 154 155 #define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0)) 156 #define __pa(x) __virt_to_phys((unsigned long)(x)) 157 #define __va(x) ((void *)__pa_to_va_nodebug((phys_addr_t)(x))) 158 159 #define phys_to_pfn(phys) (PFN_DOWN(phys)) 160 #define pfn_to_phys(pfn) (PFN_PHYS(pfn)) 161 162 #define virt_to_pfn(vaddr) (phys_to_pfn(__pa(vaddr))) 163 #define pfn_to_virt(pfn) (__va(pfn_to_phys(pfn))) 164 165 #define virt_to_page(vaddr) (pfn_to_page(virt_to_pfn(vaddr))) 166 #define page_to_virt(page) (pfn_to_virt(page_to_pfn(page))) 167 168 #define page_to_phys(page) (pfn_to_phys(page_to_pfn(page))) 169 #define page_to_bus(page) (page_to_phys(page)) 170 #define phys_to_page(paddr) (pfn_to_page(phys_to_pfn(paddr))) 171 172 #define sym_to_pfn(x) __phys_to_pfn(__pa_symbol(x)) 173 174 #ifdef CONFIG_FLATMEM 175 #define pfn_valid(pfn) \ 176 (((pfn) >= ARCH_PFN_OFFSET) && (((pfn) - ARCH_PFN_OFFSET) < max_mapnr)) 177 #endif 178 179 #endif /* __ASSEMBLY__ */ 180 181 #define virt_addr_valid(vaddr) ({ \ 182 unsigned long _addr = (unsigned long)vaddr; \ 183 (unsigned long)(_addr) >= PAGE_OFFSET && pfn_valid(virt_to_pfn(_addr)); \ 184 }) 185 186 #define VM_DATA_DEFAULT_FLAGS VM_DATA_FLAGS_NON_EXEC 187 188 #include <asm-generic/memory_model.h> 189 #include <asm-generic/getorder.h> 190 191 #endif /* _ASM_RISCV_PAGE_H */ 192