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