1 /* SPDX-License-Identifier: GPL-2.0-only */
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>
9 #ifndef _ASM_RISCV_PAGE_H
10 #define _ASM_RISCV_PAGE_H
12 #include <linux/pfn.h>
13 #include <linux/const.h>
15 #define PAGE_SHIFT (12)
16 #define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT)
17 #define PAGE_MASK (~(PAGE_SIZE - 1))
20 #define HUGE_MAX_HSTATE 2
22 #define HUGE_MAX_HSTATE 1
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)
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).
34 #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL)
36 #define KERN_VIRT_SIZE (-PAGE_OFFSET)
40 #define clear_page(pgaddr) memset((pgaddr), 0, PAGE_SIZE)
41 #define copy_page(to, from) memcpy((to), (from), PAGE_SIZE)
43 #define clear_user_page(pgaddr, vaddr, page) memset((pgaddr), 0, PAGE_SIZE)
44 #define copy_user_page(vto, vfrom, vaddr, topg) \
45 memcpy((vto), (vfrom), PAGE_SIZE)
48 * Use struct definitions to apply C type checking
51 /* Page Global Directory entry */
56 /* Page Table entry */
65 typedef struct page *pgtable_t;
67 #define pte_val(x) ((x).pte)
68 #define pgd_val(x) ((x).pgd)
69 #define pgprot_val(x) ((x).pgprot)
71 #define __pte(x) ((pte_t) { (x) })
72 #define __pgd(x) ((pgd_t) { (x) })
73 #define __pgprot(x) ((pgprot_t) { (x) })
76 #define PTE_FMT "%016lx"
78 #define PTE_FMT "%08lx"
82 extern unsigned long va_pa_offset;
84 extern unsigned long va_kernel_pa_offset;
86 extern unsigned long va_kernel_xip_pa_offset;
87 extern unsigned long pfn_base;
88 extern uintptr_t load_sz;
89 #define ARCH_PFN_OFFSET (pfn_base)
91 #define va_pa_offset 0
93 #define va_kernel_pa_offset 0
95 #define va_kernel_xip_pa_offset 0
96 #define ARCH_PFN_OFFSET (PAGE_OFFSET >> PAGE_SHIFT)
97 #endif /* CONFIG_MMU */
99 extern unsigned long kernel_virt_addr;
102 #define is_kernel_mapping(x) \
103 ((x) >= kernel_virt_addr && (x) < (kernel_virt_addr + load_sz))
104 #define is_linear_mapping(x) \
105 ((x) >= PAGE_OFFSET && (x) < kernel_virt_addr)
107 #define linear_mapping_pa_to_va(x) ((void *)((unsigned long)(x) + va_pa_offset))
108 #define kernel_mapping_pa_to_va(y) ({ \
109 unsigned long _y = y; \
110 (_y >= CONFIG_PHYS_RAM_BASE) ? \
111 (void *)((unsigned long)(_y) + va_kernel_pa_offset + XIP_OFFSET) : \
112 (void *)((unsigned long)(_y) + va_kernel_xip_pa_offset); \
114 #define __pa_to_va_nodebug(x) linear_mapping_pa_to_va(x)
116 #define linear_mapping_va_to_pa(x) ((unsigned long)(x) - va_pa_offset)
117 #define kernel_mapping_va_to_pa(y) ({ \
118 unsigned long _y = y; \
119 (_y < kernel_virt_addr + XIP_OFFSET) ? \
120 ((unsigned long)(_y) - va_kernel_xip_pa_offset) : \
121 ((unsigned long)(_y) - va_kernel_pa_offset - XIP_OFFSET); \
124 #define __va_to_pa_nodebug(x) ({ \
125 unsigned long _x = x; \
126 is_linear_mapping(_x) ? \
127 linear_mapping_va_to_pa(_x) : kernel_mapping_va_to_pa(_x); \
130 #define is_kernel_mapping(x) \
131 ((x) >= kernel_virt_addr && (x) < (kernel_virt_addr + load_sz))
132 #define is_linear_mapping(x) \
135 #define __pa_to_va_nodebug(x) ((void *)((unsigned long) (x) + va_pa_offset))
136 #define __va_to_pa_nodebug(x) ((unsigned long)(x) - va_pa_offset)
137 #endif /* CONFIG_64BIT */
139 #ifdef CONFIG_DEBUG_VIRTUAL
140 extern phys_addr_t __virt_to_phys(unsigned long x);
141 extern phys_addr_t __phys_addr_symbol(unsigned long x);
143 #define __virt_to_phys(x) __va_to_pa_nodebug(x)
144 #define __phys_addr_symbol(x) __va_to_pa_nodebug(x)
145 #endif /* CONFIG_DEBUG_VIRTUAL */
147 #define __pa_symbol(x) __phys_addr_symbol(RELOC_HIDE((unsigned long)(x), 0))
148 #define __pa(x) __virt_to_phys((unsigned long)(x))
149 #define __va(x) ((void *)__pa_to_va_nodebug((phys_addr_t)(x)))
151 #define phys_to_pfn(phys) (PFN_DOWN(phys))
152 #define pfn_to_phys(pfn) (PFN_PHYS(pfn))
154 #define virt_to_pfn(vaddr) (phys_to_pfn(__pa(vaddr)))
155 #define pfn_to_virt(pfn) (__va(pfn_to_phys(pfn)))
157 #define virt_to_page(vaddr) (pfn_to_page(virt_to_pfn(vaddr)))
158 #define page_to_virt(page) (pfn_to_virt(page_to_pfn(page)))
160 #define page_to_phys(page) (pfn_to_phys(page_to_pfn(page)))
161 #define page_to_bus(page) (page_to_phys(page))
162 #define phys_to_page(paddr) (pfn_to_page(phys_to_pfn(paddr)))
164 #ifdef CONFIG_FLATMEM
165 #define pfn_valid(pfn) \
166 (((pfn) >= ARCH_PFN_OFFSET) && (((pfn) - ARCH_PFN_OFFSET) < max_mapnr))
169 #endif /* __ASSEMBLY__ */
171 #define virt_addr_valid(vaddr) ({ \
172 unsigned long _addr = (unsigned long)vaddr; \
173 (unsigned long)(_addr) >= PAGE_OFFSET && pfn_valid(virt_to_pfn(_addr)); \
176 #define VM_DATA_DEFAULT_FLAGS VM_DATA_FLAGS_NON_EXEC
178 #include <asm-generic/memory_model.h>
179 #include <asm-generic/getorder.h>
181 #endif /* _ASM_RISCV_PAGE_H */