1 /* 2 * Copyright (C) 2009 Chen Liqin <liqin.chen@sunplusct.com> 3 * Copyright (C) 2012 Regents of the University of California 4 * Copyright (C) 2017 SiFive 5 * Copyright (C) 2017 XiaojingZhu <zhuxiaoj@ict.ac.cn> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation, version 2. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 */ 16 17 #ifndef _ASM_RISCV_PAGE_H 18 #define _ASM_RISCV_PAGE_H 19 20 #include <linux/pfn.h> 21 #include <linux/const.h> 22 23 #define PAGE_SHIFT (12) 24 #define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT) 25 #define PAGE_MASK (~(PAGE_SIZE - 1)) 26 27 /* 28 * PAGE_OFFSET -- the first address of the first page of memory. 29 * When not using MMU this corresponds to the first free page in 30 * physical memory (aligned on a page boundary). 31 */ 32 #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL) 33 34 #define KERN_VIRT_SIZE (-PAGE_OFFSET) 35 36 #ifndef __ASSEMBLY__ 37 38 #define PAGE_UP(addr) (((addr)+((PAGE_SIZE)-1))&(~((PAGE_SIZE)-1))) 39 #define PAGE_DOWN(addr) ((addr)&(~((PAGE_SIZE)-1))) 40 41 /* align addr on a size boundary - adjust address up/down if needed */ 42 #define _ALIGN_UP(addr, size) (((addr)+((size)-1))&(~((size)-1))) 43 #define _ALIGN_DOWN(addr, size) ((addr)&(~((size)-1))) 44 45 /* align addr on a size boundary - adjust address up if needed */ 46 #define _ALIGN(addr, size) _ALIGN_UP(addr, size) 47 48 #define clear_page(pgaddr) memset((pgaddr), 0, PAGE_SIZE) 49 #define copy_page(to, from) memcpy((to), (from), PAGE_SIZE) 50 51 #define clear_user_page(pgaddr, vaddr, page) memset((pgaddr), 0, PAGE_SIZE) 52 #define copy_user_page(vto, vfrom, vaddr, topg) \ 53 memcpy((vto), (vfrom), PAGE_SIZE) 54 55 /* 56 * Use struct definitions to apply C type checking 57 */ 58 59 /* Page Global Directory entry */ 60 typedef struct { 61 unsigned long pgd; 62 } pgd_t; 63 64 /* Page Table entry */ 65 typedef struct { 66 unsigned long pte; 67 } pte_t; 68 69 typedef struct { 70 unsigned long pgprot; 71 } pgprot_t; 72 73 typedef struct page *pgtable_t; 74 75 #define pte_val(x) ((x).pte) 76 #define pgd_val(x) ((x).pgd) 77 #define pgprot_val(x) ((x).pgprot) 78 79 #define __pte(x) ((pte_t) { (x) }) 80 #define __pgd(x) ((pgd_t) { (x) }) 81 #define __pgprot(x) ((pgprot_t) { (x) }) 82 83 #ifdef CONFIG_64BIT 84 #define PTE_FMT "%016lx" 85 #else 86 #define PTE_FMT "%08lx" 87 #endif 88 89 extern unsigned long va_pa_offset; 90 extern unsigned long pfn_base; 91 92 extern unsigned long max_low_pfn; 93 extern unsigned long min_low_pfn; 94 95 #define __pa(x) ((unsigned long)(x) - va_pa_offset) 96 #define __va(x) ((void *)((unsigned long) (x) + va_pa_offset)) 97 98 #define phys_to_pfn(phys) (PFN_DOWN(phys)) 99 #define pfn_to_phys(pfn) (PFN_PHYS(pfn)) 100 101 #define virt_to_pfn(vaddr) (phys_to_pfn(__pa(vaddr))) 102 #define pfn_to_virt(pfn) (__va(pfn_to_phys(pfn))) 103 104 #define virt_to_page(vaddr) (pfn_to_page(virt_to_pfn(vaddr))) 105 #define page_to_virt(page) (pfn_to_virt(page_to_pfn(page))) 106 107 #define page_to_phys(page) (pfn_to_phys(page_to_pfn(page))) 108 #define page_to_bus(page) (page_to_phys(page)) 109 #define phys_to_page(paddr) (pfn_to_page(phys_to_pfn(paddr))) 110 111 #define pfn_valid(pfn) \ 112 (((pfn) >= pfn_base) && (((pfn)-pfn_base) < max_mapnr)) 113 114 #define ARCH_PFN_OFFSET (pfn_base) 115 116 #endif /* __ASSEMBLY__ */ 117 118 #define virt_addr_valid(vaddr) (pfn_valid(virt_to_pfn(vaddr))) 119 120 #define VM_DATA_DEFAULT_FLAGS (VM_READ | VM_WRITE | \ 121 VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) 122 123 #include <asm-generic/memory_model.h> 124 #include <asm-generic/getorder.h> 125 126 /* vDSO support */ 127 /* We do define AT_SYSINFO_EHDR but don't use the gate mechanism */ 128 #define __HAVE_ARCH_GATE_AREA 129 130 #endif /* _ASM_RISCV_PAGE_H */ 131