2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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
15 #ifndef _ASM_TILE_PAGE_H
16 #define _ASM_TILE_PAGE_H
18 #include <linux/const.h>
19 #include <hv/hypervisor.h>
20 #include <arch/chip.h>
22 /* PAGE_SHIFT and HPAGE_SHIFT determine the page sizes. */
23 #if defined(CONFIG_PAGE_SIZE_16KB)
25 #define CTX_PAGE_FLAG HV_CTX_PG_SM_16K
26 #elif defined(CONFIG_PAGE_SIZE_64KB)
28 #define CTX_PAGE_FLAG HV_CTX_PG_SM_64K
30 #define PAGE_SHIFT HV_LOG2_DEFAULT_PAGE_SIZE_SMALL
31 #define CTX_PAGE_FLAG 0
33 #define HPAGE_SHIFT HV_LOG2_DEFAULT_PAGE_SIZE_LARGE
35 #define PAGE_SIZE (_AC(1, UL) << PAGE_SHIFT)
36 #define HPAGE_SIZE (_AC(1, UL) << HPAGE_SHIFT)
38 #define PAGE_MASK (~(PAGE_SIZE - 1))
39 #define HPAGE_MASK (~(HPAGE_SIZE - 1))
42 * We do define AT_SYSINFO_EHDR to support vDSO,
43 * but don't use the gate mechanism.
45 #define __HAVE_ARCH_GATE_AREA 1
48 * If the Kconfig doesn't specify, set a maximum zone order that
49 * is enough so that we can create huge pages from small pages given
50 * the respective sizes of the two page types. See <linux/mmzone.h>.
52 #ifndef CONFIG_FORCE_MAX_ZONEORDER
53 #define CONFIG_FORCE_MAX_ZONEORDER (HPAGE_SHIFT - PAGE_SHIFT + 1)
58 #include <linux/types.h>
59 #include <linux/string.h>
63 static inline void clear_page(void *page
)
65 memset(page
, 0, PAGE_SIZE
);
68 static inline void copy_page(void *to
, void *from
)
70 memcpy(to
, from
, PAGE_SIZE
);
73 static inline void clear_user_page(void *page
, unsigned long vaddr
,
79 static inline void copy_user_page(void *to
, void *from
, unsigned long vaddr
,
86 * Hypervisor page tables are made of the same basic structure.
91 typedef HV_PTE pgprot_t
;
94 * User L2 page tables are managed as one L2 page table per page,
95 * because we use the page allocator for them. This keeps the allocation
96 * simple, but it's also inefficient, since L2 page tables are much smaller
97 * than pages (currently 2KB vs 64KB). So we should revisit this.
99 typedef struct page
*pgtable_t
;
101 /* Must be a macro since it is used to create constants. */
102 #define __pgprot(val) hv_pte(val)
104 /* Rarely-used initializers, typically with a "zero" value. */
105 #define __pte(x) hv_pte(x)
106 #define __pgd(x) hv_pte(x)
108 static inline u64
pgprot_val(pgprot_t pgprot
)
110 return hv_pte_val(pgprot
);
113 static inline u64
pte_val(pte_t pte
)
115 return hv_pte_val(pte
);
118 static inline u64
pgd_val(pgd_t pgd
)
120 return hv_pte_val(pgd
);
125 typedef HV_PTE pmd_t
;
127 #define __pmd(x) hv_pte(x)
129 static inline u64
pmd_val(pmd_t pmd
)
131 return hv_pte_val(pmd
);
136 static inline __attribute_const__
int get_order(unsigned long size
)
138 return BITS_PER_LONG
- __builtin_clzl((size
- 1) >> PAGE_SHIFT
);
141 #endif /* !__ASSEMBLY__ */
143 #define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT)
145 #define HUGE_MAX_HSTATE 6
147 #ifdef CONFIG_HUGETLB_PAGE
148 #define HAVE_ARCH_HUGETLB_UNMAPPED_AREA
151 /* Allow overriding how much VA or PA the kernel will use. */
152 #define MAX_PA_WIDTH CHIP_PA_WIDTH()
153 #define MAX_VA_WIDTH CHIP_VA_WIDTH()
155 /* Each memory controller has PAs distinct in their high bits. */
156 #define NR_PA_HIGHBIT_SHIFT (MAX_PA_WIDTH - CHIP_LOG_NUM_MSHIMS())
157 #define NR_PA_HIGHBIT_VALUES (1 << CHIP_LOG_NUM_MSHIMS())
158 #define __pa_to_highbits(pa) ((phys_addr_t)(pa) >> NR_PA_HIGHBIT_SHIFT)
159 #define __pfn_to_highbits(pfn) ((pfn) >> (NR_PA_HIGHBIT_SHIFT - PAGE_SHIFT))
164 * We reserve the lower half of memory for user-space programs, and the
165 * upper half for system code. We re-map all of physical memory in the
166 * upper half, which takes a quarter of our VA space. Then we have
167 * the vmalloc regions. The supervisor code lives at the highest address,
168 * with the hypervisor above that.
170 * Loadable kernel modules are placed immediately after the static
171 * supervisor code, with each being allocated a 256MB region of
172 * address space, so we don't have to worry about the range of "jal"
173 * and other branch instructions.
175 * For now we keep life simple and just allocate one pmd (4GB) for vmalloc.
176 * Similarly, for now we don't play any struct page mapping games.
179 #if MAX_PA_WIDTH + 2 > MAX_VA_WIDTH
180 # error Too much PA to map with the VA available!
183 #define PAGE_OFFSET (-(_AC(1, UL) << (MAX_VA_WIDTH - 1)))
184 #define KERNEL_HIGH_VADDR _AC(0xfffffff800000000, UL) /* high 32GB */
185 #define FIXADDR_BASE (KERNEL_HIGH_VADDR - 0x300000000) /* 4 GB */
186 #define FIXADDR_TOP (KERNEL_HIGH_VADDR - 0x200000000) /* 4 GB */
187 #define _VMALLOC_START FIXADDR_TOP
188 #define MEM_SV_START (KERNEL_HIGH_VADDR - 0x100000000) /* 256 MB */
189 #define MEM_MODULE_START (MEM_SV_START + (256*1024*1024)) /* 256 MB */
190 #define MEM_MODULE_END (MEM_MODULE_START + (256*1024*1024))
192 #else /* !__tilegx__ */
195 * A PAGE_OFFSET of 0xC0000000 means that the kernel has
196 * a virtual address space of one gigabyte, which limits the
197 * amount of physical memory you can use to about 768MB.
198 * If you want more physical memory than this then see the CONFIG_HIGHMEM
199 * option in the kernel configuration.
201 * The top 16MB chunk in the table below is unavailable to Linux. Since
202 * the kernel interrupt vectors must live at ether 0xfe000000 or 0xfd000000
203 * (depending on whether the kernel is at PL2 or Pl1), we map all of the
204 * bottom of RAM at this address with a huge page table entry to minimize
205 * its ITLB footprint (as well as at PAGE_OFFSET). The last architected
206 * requirement is that user interrupt vectors live at 0xfc000000, so we
207 * make that range of memory available to user processes. The remaining
208 * regions are sized as shown; the first four addresses use the PL 1
209 * values, and after that, we show "typical" values, since the actual
210 * addresses depend on kernel #defines.
212 * MEM_HV_START 0xfe000000
213 * MEM_SV_START (kernel code) 0xfd000000
214 * MEM_USER_INTRPT (user vector) 0xfc000000
215 * FIX_KMAP_xxx 0xfa000000 (via NR_CPUS * KM_TYPE_NR)
216 * PKMAP_BASE 0xf9000000 (via LAST_PKMAP)
217 * VMALLOC_START 0xf7000000 (via VMALLOC_RESERVE)
218 * mapped LOWMEM 0xc0000000
221 #define MEM_USER_INTRPT _AC(0xfc000000, UL)
222 #define MEM_SV_START _AC(0xfd000000, UL)
223 #define MEM_HV_START _AC(0xfe000000, UL)
225 #define INTRPT_SIZE 0x4000
227 /* Tolerate page size larger than the architecture interrupt region size. */
228 #if PAGE_SIZE > INTRPT_SIZE
230 #define INTRPT_SIZE PAGE_SIZE
233 #define KERNEL_HIGH_VADDR MEM_USER_INTRPT
234 #define FIXADDR_TOP (KERNEL_HIGH_VADDR - PAGE_SIZE)
236 #define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL)
238 /* On 32-bit architectures we mix kernel modules in with other vmaps. */
239 #define MEM_MODULE_START VMALLOC_START
240 #define MEM_MODULE_END VMALLOC_END
242 #endif /* __tilegx__ */
244 #if !defined(__ASSEMBLY__) && !defined(VDSO_BUILD)
246 #ifdef CONFIG_HIGHMEM
248 /* Map kernel virtual addresses to page frames, in HPAGE_SIZE chunks. */
249 extern unsigned long pbase_map
[];
250 extern void *vbase_map
[];
252 static inline unsigned long kaddr_to_pfn(const volatile void *_kaddr
)
254 unsigned long kaddr
= (unsigned long)_kaddr
;
255 return pbase_map
[kaddr
>> HPAGE_SHIFT
] +
256 ((kaddr
& (HPAGE_SIZE
- 1)) >> PAGE_SHIFT
);
259 static inline void *pfn_to_kaddr(unsigned long pfn
)
261 return vbase_map
[__pfn_to_highbits(pfn
)] + (pfn
<< PAGE_SHIFT
);
264 static inline phys_addr_t
virt_to_phys(const volatile void *kaddr
)
266 unsigned long pfn
= kaddr_to_pfn(kaddr
);
267 return ((phys_addr_t
)pfn
<< PAGE_SHIFT
) +
268 ((unsigned long)kaddr
& (PAGE_SIZE
-1));
271 static inline void *phys_to_virt(phys_addr_t paddr
)
273 return pfn_to_kaddr(paddr
>> PAGE_SHIFT
) + (paddr
& (PAGE_SIZE
-1));
276 /* With HIGHMEM, we pack PAGE_OFFSET through high_memory with all valid VAs. */
277 static inline int virt_addr_valid(const volatile void *kaddr
)
279 extern void *high_memory
; /* copied from <linux/mm.h> */
280 return ((unsigned long)kaddr
>= PAGE_OFFSET
&& kaddr
< high_memory
);
283 #else /* !CONFIG_HIGHMEM */
285 static inline unsigned long kaddr_to_pfn(const volatile void *kaddr
)
287 return ((unsigned long)kaddr
- PAGE_OFFSET
) >> PAGE_SHIFT
;
290 static inline void *pfn_to_kaddr(unsigned long pfn
)
292 return (void *)((pfn
<< PAGE_SHIFT
) + PAGE_OFFSET
);
295 static inline phys_addr_t
virt_to_phys(const volatile void *kaddr
)
297 return (phys_addr_t
)((unsigned long)kaddr
- PAGE_OFFSET
);
300 static inline void *phys_to_virt(phys_addr_t paddr
)
302 return (void *)((unsigned long)paddr
+ PAGE_OFFSET
);
305 /* Check that the given address is within some mapped range of PAs. */
306 #define virt_addr_valid(kaddr) pfn_valid(kaddr_to_pfn(kaddr))
308 #endif /* !CONFIG_HIGHMEM */
310 /* All callers are not consistent in how they call these functions. */
311 #define __pa(kaddr) virt_to_phys((void *)(unsigned long)(kaddr))
312 #define __va(paddr) phys_to_virt((phys_addr_t)(paddr))
314 extern int devmem_is_allowed(unsigned long pagenr
);
316 #ifdef CONFIG_FLATMEM
317 static inline int pfn_valid(unsigned long pfn
)
319 return pfn
< max_mapnr
;
323 /* Provide as macros since these require some other headers included. */
324 #define page_to_pa(page) ((phys_addr_t)(page_to_pfn(page)) << PAGE_SHIFT)
325 #define virt_to_page(kaddr) pfn_to_page(kaddr_to_pfn((void *)(kaddr)))
326 #define page_to_virt(page) pfn_to_kaddr(page_to_pfn(page))
329 extern pte_t
*virt_to_pte(struct mm_struct
*mm
, unsigned long addr
);
330 extern pte_t
*virt_to_kpte(unsigned long kaddr
);
332 #endif /* !__ASSEMBLY__ */
334 #define VM_DATA_DEFAULT_FLAGS \
335 (VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
337 #include <asm-generic/memory_model.h>
339 #endif /* _ASM_TILE_PAGE_H */