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