Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Tilera Corporation. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation, version 2. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but |
| 9 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or |
| 11 | * NON INFRINGEMENT. See the GNU General Public License for |
| 12 | * more details. |
| 13 | */ |
| 14 | |
| 15 | #ifndef _ASM_TILE_PAGE_H |
| 16 | #define _ASM_TILE_PAGE_H |
| 17 | |
| 18 | #include <linux/const.h> |
Chris Metcalf | 5386e73 | 2011-05-02 16:21:12 -0400 | [diff] [blame] | 19 | #include <hv/hypervisor.h> |
| 20 | #include <arch/chip.h> |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 21 | |
| 22 | /* PAGE_SHIFT and HPAGE_SHIFT determine the page sizes. */ |
Chris Metcalf | c1b27ab | 2015-12-22 12:28:51 -0500 | [diff] [blame^] | 23 | #if defined(CONFIG_PAGE_SIZE_4KB) /* tilepro only */ |
| 24 | #define PAGE_SHIFT 12 |
| 25 | #define CTX_PAGE_FLAG HV_CTX_PG_SM_4K |
| 26 | #elif defined(CONFIG_PAGE_SIZE_16KB) |
Chris Metcalf | d5d14ed | 2012-03-29 13:58:43 -0400 | [diff] [blame] | 27 | #define PAGE_SHIFT 14 |
| 28 | #define CTX_PAGE_FLAG HV_CTX_PG_SM_16K |
| 29 | #elif defined(CONFIG_PAGE_SIZE_64KB) |
| 30 | #define PAGE_SHIFT 16 |
| 31 | #define CTX_PAGE_FLAG HV_CTX_PG_SM_64K |
| 32 | #else |
Chris Metcalf | c1b27ab | 2015-12-22 12:28:51 -0500 | [diff] [blame^] | 33 | #error Page size not specified in Kconfig |
Chris Metcalf | d5d14ed | 2012-03-29 13:58:43 -0400 | [diff] [blame] | 34 | #endif |
| 35 | #define HPAGE_SHIFT HV_LOG2_DEFAULT_PAGE_SIZE_LARGE |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 36 | |
| 37 | #define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT) |
| 38 | #define HPAGE_SIZE (_AC(1, UL) << HPAGE_SHIFT) |
| 39 | |
| 40 | #define PAGE_MASK (~(PAGE_SIZE - 1)) |
| 41 | #define HPAGE_MASK (~(HPAGE_SIZE - 1)) |
| 42 | |
Chris Metcalf | 76c567f | 2011-02-28 16:37:34 -0500 | [diff] [blame] | 43 | /* |
| 44 | * If the Kconfig doesn't specify, set a maximum zone order that |
| 45 | * is enough so that we can create huge pages from small pages given |
| 46 | * the respective sizes of the two page types. See <linux/mmzone.h>. |
| 47 | */ |
| 48 | #ifndef CONFIG_FORCE_MAX_ZONEORDER |
| 49 | #define CONFIG_FORCE_MAX_ZONEORDER (HPAGE_SHIFT - PAGE_SHIFT + 1) |
| 50 | #endif |
| 51 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 52 | #ifndef __ASSEMBLY__ |
| 53 | |
| 54 | #include <linux/types.h> |
| 55 | #include <linux/string.h> |
| 56 | |
| 57 | struct page; |
| 58 | |
| 59 | static inline void clear_page(void *page) |
| 60 | { |
| 61 | memset(page, 0, PAGE_SIZE); |
| 62 | } |
| 63 | |
| 64 | static inline void copy_page(void *to, void *from) |
| 65 | { |
| 66 | memcpy(to, from, PAGE_SIZE); |
| 67 | } |
| 68 | |
| 69 | static inline void clear_user_page(void *page, unsigned long vaddr, |
| 70 | struct page *pg) |
| 71 | { |
| 72 | clear_page(page); |
| 73 | } |
| 74 | |
| 75 | static inline void copy_user_page(void *to, void *from, unsigned long vaddr, |
| 76 | struct page *topage) |
| 77 | { |
| 78 | copy_page(to, from); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Hypervisor page tables are made of the same basic structure. |
| 83 | */ |
| 84 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 85 | typedef HV_PTE pte_t; |
| 86 | typedef HV_PTE pgd_t; |
| 87 | typedef HV_PTE pgprot_t; |
| 88 | |
| 89 | /* |
| 90 | * User L2 page tables are managed as one L2 page table per page, |
| 91 | * because we use the page allocator for them. This keeps the allocation |
Chris Metcalf | 621b195 | 2012-04-01 14:04:21 -0400 | [diff] [blame] | 92 | * simple, but it's also inefficient, since L2 page tables are much smaller |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 93 | * than pages (currently 2KB vs 64KB). So we should revisit this. |
| 94 | */ |
| 95 | typedef struct page *pgtable_t; |
| 96 | |
| 97 | /* Must be a macro since it is used to create constants. */ |
| 98 | #define __pgprot(val) hv_pte(val) |
| 99 | |
Chris Metcalf | 28d7174 | 2011-05-02 16:06:42 -0400 | [diff] [blame] | 100 | /* Rarely-used initializers, typically with a "zero" value. */ |
| 101 | #define __pte(x) hv_pte(x) |
| 102 | #define __pgd(x) hv_pte(x) |
| 103 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 104 | static inline u64 pgprot_val(pgprot_t pgprot) |
| 105 | { |
| 106 | return hv_pte_val(pgprot); |
| 107 | } |
| 108 | |
| 109 | static inline u64 pte_val(pte_t pte) |
| 110 | { |
| 111 | return hv_pte_val(pte); |
| 112 | } |
| 113 | |
| 114 | static inline u64 pgd_val(pgd_t pgd) |
| 115 | { |
| 116 | return hv_pte_val(pgd); |
| 117 | } |
| 118 | |
| 119 | #ifdef __tilegx__ |
| 120 | |
| 121 | typedef HV_PTE pmd_t; |
| 122 | |
Chris Metcalf | 28d7174 | 2011-05-02 16:06:42 -0400 | [diff] [blame] | 123 | #define __pmd(x) hv_pte(x) |
| 124 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 125 | static inline u64 pmd_val(pmd_t pmd) |
| 126 | { |
| 127 | return hv_pte_val(pmd); |
| 128 | } |
| 129 | |
| 130 | #endif |
| 131 | |
Chris Metcalf | c745a8a | 2010-08-13 08:52:19 -0400 | [diff] [blame] | 132 | static inline __attribute_const__ int get_order(unsigned long size) |
| 133 | { |
| 134 | return BITS_PER_LONG - __builtin_clzl((size - 1) >> PAGE_SHIFT); |
| 135 | } |
| 136 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 137 | #endif /* !__ASSEMBLY__ */ |
| 138 | |
| 139 | #define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT) |
| 140 | |
Chris Metcalf | 621b195 | 2012-04-01 14:04:21 -0400 | [diff] [blame] | 141 | #define HUGE_MAX_HSTATE 6 |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 142 | |
| 143 | #ifdef CONFIG_HUGETLB_PAGE |
| 144 | #define HAVE_ARCH_HUGETLB_UNMAPPED_AREA |
| 145 | #endif |
| 146 | |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 147 | /* Allow overriding how much VA or PA the kernel will use. */ |
| 148 | #define MAX_PA_WIDTH CHIP_PA_WIDTH() |
| 149 | #define MAX_VA_WIDTH CHIP_VA_WIDTH() |
| 150 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 151 | /* Each memory controller has PAs distinct in their high bits. */ |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 152 | #define NR_PA_HIGHBIT_SHIFT (MAX_PA_WIDTH - CHIP_LOG_NUM_MSHIMS()) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 153 | #define NR_PA_HIGHBIT_VALUES (1 << CHIP_LOG_NUM_MSHIMS()) |
| 154 | #define __pa_to_highbits(pa) ((phys_addr_t)(pa) >> NR_PA_HIGHBIT_SHIFT) |
| 155 | #define __pfn_to_highbits(pfn) ((pfn) >> (NR_PA_HIGHBIT_SHIFT - PAGE_SHIFT)) |
| 156 | |
| 157 | #ifdef __tilegx__ |
| 158 | |
| 159 | /* |
| 160 | * We reserve the lower half of memory for user-space programs, and the |
| 161 | * upper half for system code. We re-map all of physical memory in the |
| 162 | * upper half, which takes a quarter of our VA space. Then we have |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 163 | * the vmalloc regions. The supervisor code lives at the highest address, |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 164 | * with the hypervisor above that. |
| 165 | * |
| 166 | * Loadable kernel modules are placed immediately after the static |
| 167 | * supervisor code, with each being allocated a 256MB region of |
| 168 | * address space, so we don't have to worry about the range of "jal" |
| 169 | * and other branch instructions. |
| 170 | * |
| 171 | * For now we keep life simple and just allocate one pmd (4GB) for vmalloc. |
| 172 | * Similarly, for now we don't play any struct page mapping games. |
| 173 | */ |
| 174 | |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 175 | #if MAX_PA_WIDTH + 2 > MAX_VA_WIDTH |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 176 | # error Too much PA to map with the VA available! |
| 177 | #endif |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 178 | |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 179 | #define PAGE_OFFSET (-(_AC(1, UL) << (MAX_VA_WIDTH - 1))) |
| 180 | #define KERNEL_HIGH_VADDR _AC(0xfffffff800000000, UL) /* high 32GB */ |
Chris Metcalf | 4b12909 | 2013-09-11 13:57:15 -0400 | [diff] [blame] | 181 | #define FIXADDR_BASE (KERNEL_HIGH_VADDR - 0x300000000) /* 4 GB */ |
| 182 | #define FIXADDR_TOP (KERNEL_HIGH_VADDR - 0x200000000) /* 4 GB */ |
Chris Metcalf | bbaa22c | 2012-06-13 14:46:40 -0400 | [diff] [blame] | 183 | #define _VMALLOC_START FIXADDR_TOP |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 184 | #define MEM_SV_START (KERNEL_HIGH_VADDR - 0x100000000) /* 256 MB */ |
| 185 | #define MEM_MODULE_START (MEM_SV_START + (256*1024*1024)) /* 256 MB */ |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 186 | #define MEM_MODULE_END (MEM_MODULE_START + (256*1024*1024)) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 187 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 188 | #else /* !__tilegx__ */ |
| 189 | |
| 190 | /* |
| 191 | * A PAGE_OFFSET of 0xC0000000 means that the kernel has |
| 192 | * a virtual address space of one gigabyte, which limits the |
| 193 | * amount of physical memory you can use to about 768MB. |
| 194 | * If you want more physical memory than this then see the CONFIG_HIGHMEM |
| 195 | * option in the kernel configuration. |
| 196 | * |
Chris Metcalf | a78c942 | 2010-10-14 16:23:03 -0400 | [diff] [blame] | 197 | * The top 16MB chunk in the table below is unavailable to Linux. Since |
| 198 | * the kernel interrupt vectors must live at ether 0xfe000000 or 0xfd000000 |
| 199 | * (depending on whether the kernel is at PL2 or Pl1), we map all of the |
| 200 | * bottom of RAM at this address with a huge page table entry to minimize |
| 201 | * its ITLB footprint (as well as at PAGE_OFFSET). The last architected |
| 202 | * requirement is that user interrupt vectors live at 0xfc000000, so we |
| 203 | * make that range of memory available to user processes. The remaining |
| 204 | * regions are sized as shown; the first four addresses use the PL 1 |
| 205 | * values, and after that, we show "typical" values, since the actual |
| 206 | * addresses depend on kernel #defines. |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 207 | * |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 208 | * MEM_HV_START 0xfe000000 |
| 209 | * MEM_SV_START (kernel code) 0xfd000000 |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 210 | * MEM_USER_INTRPT (user vector) 0xfc000000 |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 211 | * FIX_KMAP_xxx 0xfa000000 (via NR_CPUS * KM_TYPE_NR) |
| 212 | * PKMAP_BASE 0xf9000000 (via LAST_PKMAP) |
| 213 | * VMALLOC_START 0xf7000000 (via VMALLOC_RESERVE) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 214 | * mapped LOWMEM 0xc0000000 |
| 215 | */ |
| 216 | |
| 217 | #define MEM_USER_INTRPT _AC(0xfc000000, UL) |
Chris Metcalf | acbde1d | 2013-09-03 14:41:36 -0400 | [diff] [blame] | 218 | #define MEM_SV_START _AC(0xfd000000, UL) |
| 219 | #define MEM_HV_START _AC(0xfe000000, UL) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 220 | |
| 221 | #define INTRPT_SIZE 0x4000 |
| 222 | |
| 223 | /* Tolerate page size larger than the architecture interrupt region size. */ |
| 224 | #if PAGE_SIZE > INTRPT_SIZE |
| 225 | #undef INTRPT_SIZE |
| 226 | #define INTRPT_SIZE PAGE_SIZE |
| 227 | #endif |
| 228 | |
| 229 | #define KERNEL_HIGH_VADDR MEM_USER_INTRPT |
| 230 | #define FIXADDR_TOP (KERNEL_HIGH_VADDR - PAGE_SIZE) |
| 231 | |
| 232 | #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL) |
| 233 | |
| 234 | /* On 32-bit architectures we mix kernel modules in with other vmaps. */ |
| 235 | #define MEM_MODULE_START VMALLOC_START |
| 236 | #define MEM_MODULE_END VMALLOC_END |
| 237 | |
| 238 | #endif /* __tilegx__ */ |
| 239 | |
Chris Metcalf | 4a556f4 | 2013-08-07 15:33:32 -0400 | [diff] [blame] | 240 | #if !defined(__ASSEMBLY__) && !defined(VDSO_BUILD) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 241 | |
| 242 | #ifdef CONFIG_HIGHMEM |
| 243 | |
| 244 | /* Map kernel virtual addresses to page frames, in HPAGE_SIZE chunks. */ |
| 245 | extern unsigned long pbase_map[]; |
| 246 | extern void *vbase_map[]; |
| 247 | |
| 248 | static inline unsigned long kaddr_to_pfn(const volatile void *_kaddr) |
| 249 | { |
| 250 | unsigned long kaddr = (unsigned long)_kaddr; |
| 251 | return pbase_map[kaddr >> HPAGE_SHIFT] + |
| 252 | ((kaddr & (HPAGE_SIZE - 1)) >> PAGE_SHIFT); |
| 253 | } |
| 254 | |
| 255 | static inline void *pfn_to_kaddr(unsigned long pfn) |
| 256 | { |
| 257 | return vbase_map[__pfn_to_highbits(pfn)] + (pfn << PAGE_SHIFT); |
| 258 | } |
| 259 | |
| 260 | static inline phys_addr_t virt_to_phys(const volatile void *kaddr) |
| 261 | { |
| 262 | unsigned long pfn = kaddr_to_pfn(kaddr); |
| 263 | return ((phys_addr_t)pfn << PAGE_SHIFT) + |
| 264 | ((unsigned long)kaddr & (PAGE_SIZE-1)); |
| 265 | } |
| 266 | |
| 267 | static inline void *phys_to_virt(phys_addr_t paddr) |
| 268 | { |
| 269 | return pfn_to_kaddr(paddr >> PAGE_SHIFT) + (paddr & (PAGE_SIZE-1)); |
| 270 | } |
| 271 | |
| 272 | /* With HIGHMEM, we pack PAGE_OFFSET through high_memory with all valid VAs. */ |
| 273 | static inline int virt_addr_valid(const volatile void *kaddr) |
| 274 | { |
| 275 | extern void *high_memory; /* copied from <linux/mm.h> */ |
| 276 | return ((unsigned long)kaddr >= PAGE_OFFSET && kaddr < high_memory); |
| 277 | } |
| 278 | |
| 279 | #else /* !CONFIG_HIGHMEM */ |
| 280 | |
| 281 | static inline unsigned long kaddr_to_pfn(const volatile void *kaddr) |
| 282 | { |
| 283 | return ((unsigned long)kaddr - PAGE_OFFSET) >> PAGE_SHIFT; |
| 284 | } |
| 285 | |
| 286 | static inline void *pfn_to_kaddr(unsigned long pfn) |
| 287 | { |
| 288 | return (void *)((pfn << PAGE_SHIFT) + PAGE_OFFSET); |
| 289 | } |
| 290 | |
| 291 | static inline phys_addr_t virt_to_phys(const volatile void *kaddr) |
| 292 | { |
| 293 | return (phys_addr_t)((unsigned long)kaddr - PAGE_OFFSET); |
| 294 | } |
| 295 | |
| 296 | static inline void *phys_to_virt(phys_addr_t paddr) |
| 297 | { |
| 298 | return (void *)((unsigned long)paddr + PAGE_OFFSET); |
| 299 | } |
| 300 | |
| 301 | /* Check that the given address is within some mapped range of PAs. */ |
| 302 | #define virt_addr_valid(kaddr) pfn_valid(kaddr_to_pfn(kaddr)) |
| 303 | |
| 304 | #endif /* !CONFIG_HIGHMEM */ |
| 305 | |
| 306 | /* All callers are not consistent in how they call these functions. */ |
| 307 | #define __pa(kaddr) virt_to_phys((void *)(unsigned long)(kaddr)) |
| 308 | #define __va(paddr) phys_to_virt((phys_addr_t)(paddr)) |
| 309 | |
| 310 | extern int devmem_is_allowed(unsigned long pagenr); |
| 311 | |
| 312 | #ifdef CONFIG_FLATMEM |
| 313 | static inline int pfn_valid(unsigned long pfn) |
| 314 | { |
| 315 | return pfn < max_mapnr; |
| 316 | } |
| 317 | #endif |
| 318 | |
| 319 | /* Provide as macros since these require some other headers included. */ |
| 320 | #define page_to_pa(page) ((phys_addr_t)(page_to_pfn(page)) << PAGE_SHIFT) |
Chris Metcalf | 28d7174 | 2011-05-02 16:06:42 -0400 | [diff] [blame] | 321 | #define virt_to_page(kaddr) pfn_to_page(kaddr_to_pfn((void *)(kaddr))) |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 322 | #define page_to_virt(page) pfn_to_kaddr(page_to_pfn(page)) |
| 323 | |
| 324 | struct mm_struct; |
| 325 | extern pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr); |
Chris Metcalf | 640710a | 2013-08-12 15:08:09 -0400 | [diff] [blame] | 326 | extern pte_t *virt_to_kpte(unsigned long kaddr); |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 327 | |
| 328 | #endif /* !__ASSEMBLY__ */ |
| 329 | |
| 330 | #define VM_DATA_DEFAULT_FLAGS \ |
| 331 | (VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) |
| 332 | |
| 333 | #include <asm-generic/memory_model.h> |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 334 | |
Chris Metcalf | 867e359 | 2010-05-28 23:09:12 -0400 | [diff] [blame] | 335 | #endif /* _ASM_TILE_PAGE_H */ |