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