blob: c750943f961ecd2d3fcbb98e0b5699df6ff30e4b [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
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 Metcalf5386e732011-05-02 16:21:12 -040019#include <hv/hypervisor.h>
20#include <arch/chip.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040021
22/* PAGE_SHIFT and HPAGE_SHIFT determine the page sizes. */
Chris Metcalfd5d14ed2012-03-29 13:58:43 -040023#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
Chris Metcalf867e3592010-05-28 23:09:12 -040034
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
Chris Metcalf76c567f2011-02-28 16:37:34 -050041/*
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
Chris Metcalf867e3592010-05-28 23:09:12 -040050#ifndef __ASSEMBLY__
51
52#include <linux/types.h>
53#include <linux/string.h>
54
55struct page;
56
57static inline void clear_page(void *page)
58{
59 memset(page, 0, PAGE_SIZE);
60}
61
62static inline void copy_page(void *to, void *from)
63{
64 memcpy(to, from, PAGE_SIZE);
65}
66
67static inline void clear_user_page(void *page, unsigned long vaddr,
68 struct page *pg)
69{
70 clear_page(page);
71}
72
73static 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
Chris Metcalf867e3592010-05-28 23:09:12 -040083typedef HV_PTE pte_t;
84typedef HV_PTE pgd_t;
85typedef 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 and makes it potentially useful to implement HIGHPTE at some point.
91 * However, it's also inefficient, since L2 page tables are much smaller
92 * than pages (currently 2KB vs 64KB). So we should revisit this.
93 */
94typedef struct page *pgtable_t;
95
96/* Must be a macro since it is used to create constants. */
97#define __pgprot(val) hv_pte(val)
98
Chris Metcalf28d71742011-05-02 16:06:42 -040099/* Rarely-used initializers, typically with a "zero" value. */
100#define __pte(x) hv_pte(x)
101#define __pgd(x) hv_pte(x)
102
Chris Metcalf867e3592010-05-28 23:09:12 -0400103static inline u64 pgprot_val(pgprot_t pgprot)
104{
105 return hv_pte_val(pgprot);
106}
107
108static inline u64 pte_val(pte_t pte)
109{
110 return hv_pte_val(pte);
111}
112
113static inline u64 pgd_val(pgd_t pgd)
114{
115 return hv_pte_val(pgd);
116}
117
118#ifdef __tilegx__
119
120typedef HV_PTE pmd_t;
121
Chris Metcalf28d71742011-05-02 16:06:42 -0400122#define __pmd(x) hv_pte(x)
123
Chris Metcalf867e3592010-05-28 23:09:12 -0400124static inline u64 pmd_val(pmd_t pmd)
125{
126 return hv_pte_val(pmd);
127}
128
129#endif
130
Chris Metcalfc745a8a2010-08-13 08:52:19 -0400131static inline __attribute_const__ int get_order(unsigned long size)
132{
133 return BITS_PER_LONG - __builtin_clzl((size - 1) >> PAGE_SHIFT);
134}
135
Chris Metcalf867e3592010-05-28 23:09:12 -0400136#endif /* !__ASSEMBLY__ */
137
138#define HUGETLB_PAGE_ORDER (HPAGE_SHIFT - PAGE_SHIFT)
139
140#define HUGE_MAX_HSTATE 2
141
142#ifdef CONFIG_HUGETLB_PAGE
143#define HAVE_ARCH_HUGETLB_UNMAPPED_AREA
144#endif
145
146/* Each memory controller has PAs distinct in their high bits. */
147#define NR_PA_HIGHBIT_SHIFT (CHIP_PA_WIDTH() - CHIP_LOG_NUM_MSHIMS())
148#define NR_PA_HIGHBIT_VALUES (1 << CHIP_LOG_NUM_MSHIMS())
149#define __pa_to_highbits(pa) ((phys_addr_t)(pa) >> NR_PA_HIGHBIT_SHIFT)
150#define __pfn_to_highbits(pfn) ((pfn) >> (NR_PA_HIGHBIT_SHIFT - PAGE_SHIFT))
151
152#ifdef __tilegx__
153
154/*
155 * We reserve the lower half of memory for user-space programs, and the
156 * upper half for system code. We re-map all of physical memory in the
157 * upper half, which takes a quarter of our VA space. Then we have
158 * the vmalloc regions. The supervisor code lives at 0xfffffff700000000,
159 * with the hypervisor above that.
160 *
161 * Loadable kernel modules are placed immediately after the static
162 * supervisor code, with each being allocated a 256MB region of
163 * address space, so we don't have to worry about the range of "jal"
164 * and other branch instructions.
165 *
166 * For now we keep life simple and just allocate one pmd (4GB) for vmalloc.
167 * Similarly, for now we don't play any struct page mapping games.
168 */
169
170#if CHIP_PA_WIDTH() + 2 > CHIP_VA_WIDTH()
171# error Too much PA to map with the VA available!
172#endif
173#define HALF_VA_SPACE (_AC(1, UL) << (CHIP_VA_WIDTH() - 1))
174
175#define MEM_LOW_END (HALF_VA_SPACE - 1) /* low half */
176#define MEM_HIGH_START (-HALF_VA_SPACE) /* high half */
177#define PAGE_OFFSET MEM_HIGH_START
178#define _VMALLOC_START _AC(0xfffffff500000000, UL) /* 4 GB */
179#define HUGE_VMAP_BASE _AC(0xfffffff600000000, UL) /* 4 GB */
180#define MEM_SV_START _AC(0xfffffff700000000, UL) /* 256 MB */
181#define MEM_SV_INTRPT MEM_SV_START
182#define MEM_MODULE_START _AC(0xfffffff710000000, UL) /* 256 MB */
183#define MEM_MODULE_END (MEM_MODULE_START + (256*1024*1024))
184#define MEM_HV_START _AC(0xfffffff800000000, UL) /* 32 GB */
185
186/* Highest DTLB address we will use */
187#define KERNEL_HIGH_VADDR MEM_SV_START
188
189/* Since we don't currently provide any fixmaps, we use an impossible VA. */
190#define FIXADDR_TOP MEM_HV_START
191
192#else /* !__tilegx__ */
193
194/*
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.
200 *
Chris Metcalfa78c9422010-10-14 16:23:03 -0400201 * 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.
Chris Metcalf867e3592010-05-28 23:09:12 -0400211 *
Chris Metcalf867e3592010-05-28 23:09:12 -0400212 * MEM_HV_INTRPT 0xfe000000
213 * MEM_SV_INTRPT (kernel code) 0xfd000000
214 * MEM_USER_INTRPT (user vector) 0xfc000000
215 * FIX_KMAP_xxx 0xf8000000 (via NR_CPUS * KM_TYPE_NR)
216 * PKMAP_BASE 0xf7000000 (via LAST_PKMAP)
217 * HUGE_VMAP 0xf3000000 (via CONFIG_NR_HUGE_VMAPS)
218 * VMALLOC_START 0xf0000000 (via __VMALLOC_RESERVE)
219 * mapped LOWMEM 0xc0000000
220 */
221
222#define MEM_USER_INTRPT _AC(0xfc000000, UL)
Chris Metcalfa78c9422010-10-14 16:23:03 -0400223#if CONFIG_KERNEL_PL == 1
Chris Metcalf867e3592010-05-28 23:09:12 -0400224#define MEM_SV_INTRPT _AC(0xfd000000, UL)
225#define MEM_HV_INTRPT _AC(0xfe000000, UL)
Chris Metcalfa78c9422010-10-14 16:23:03 -0400226#else
227#define MEM_GUEST_INTRPT _AC(0xfd000000, UL)
228#define MEM_SV_INTRPT _AC(0xfe000000, UL)
229#define MEM_HV_INTRPT _AC(0xff000000, UL)
230#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400231
232#define INTRPT_SIZE 0x4000
233
234/* Tolerate page size larger than the architecture interrupt region size. */
235#if PAGE_SIZE > INTRPT_SIZE
236#undef INTRPT_SIZE
237#define INTRPT_SIZE PAGE_SIZE
238#endif
239
240#define KERNEL_HIGH_VADDR MEM_USER_INTRPT
241#define FIXADDR_TOP (KERNEL_HIGH_VADDR - PAGE_SIZE)
242
243#define PAGE_OFFSET _AC(CONFIG_PAGE_OFFSET, UL)
244
245/* On 32-bit architectures we mix kernel modules in with other vmaps. */
246#define MEM_MODULE_START VMALLOC_START
247#define MEM_MODULE_END VMALLOC_END
248
249#endif /* __tilegx__ */
250
251#ifndef __ASSEMBLY__
252
253#ifdef CONFIG_HIGHMEM
254
255/* Map kernel virtual addresses to page frames, in HPAGE_SIZE chunks. */
256extern unsigned long pbase_map[];
257extern void *vbase_map[];
258
259static inline unsigned long kaddr_to_pfn(const volatile void *_kaddr)
260{
261 unsigned long kaddr = (unsigned long)_kaddr;
262 return pbase_map[kaddr >> HPAGE_SHIFT] +
263 ((kaddr & (HPAGE_SIZE - 1)) >> PAGE_SHIFT);
264}
265
266static inline void *pfn_to_kaddr(unsigned long pfn)
267{
268 return vbase_map[__pfn_to_highbits(pfn)] + (pfn << PAGE_SHIFT);
269}
270
271static inline phys_addr_t virt_to_phys(const volatile void *kaddr)
272{
273 unsigned long pfn = kaddr_to_pfn(kaddr);
274 return ((phys_addr_t)pfn << PAGE_SHIFT) +
275 ((unsigned long)kaddr & (PAGE_SIZE-1));
276}
277
278static inline void *phys_to_virt(phys_addr_t paddr)
279{
280 return pfn_to_kaddr(paddr >> PAGE_SHIFT) + (paddr & (PAGE_SIZE-1));
281}
282
283/* With HIGHMEM, we pack PAGE_OFFSET through high_memory with all valid VAs. */
284static inline int virt_addr_valid(const volatile void *kaddr)
285{
286 extern void *high_memory; /* copied from <linux/mm.h> */
287 return ((unsigned long)kaddr >= PAGE_OFFSET && kaddr < high_memory);
288}
289
290#else /* !CONFIG_HIGHMEM */
291
292static inline unsigned long kaddr_to_pfn(const volatile void *kaddr)
293{
294 return ((unsigned long)kaddr - PAGE_OFFSET) >> PAGE_SHIFT;
295}
296
297static inline void *pfn_to_kaddr(unsigned long pfn)
298{
299 return (void *)((pfn << PAGE_SHIFT) + PAGE_OFFSET);
300}
301
302static inline phys_addr_t virt_to_phys(const volatile void *kaddr)
303{
304 return (phys_addr_t)((unsigned long)kaddr - PAGE_OFFSET);
305}
306
307static inline void *phys_to_virt(phys_addr_t paddr)
308{
309 return (void *)((unsigned long)paddr + PAGE_OFFSET);
310}
311
312/* Check that the given address is within some mapped range of PAs. */
313#define virt_addr_valid(kaddr) pfn_valid(kaddr_to_pfn(kaddr))
314
315#endif /* !CONFIG_HIGHMEM */
316
317/* All callers are not consistent in how they call these functions. */
318#define __pa(kaddr) virt_to_phys((void *)(unsigned long)(kaddr))
319#define __va(paddr) phys_to_virt((phys_addr_t)(paddr))
320
321extern int devmem_is_allowed(unsigned long pagenr);
322
323#ifdef CONFIG_FLATMEM
324static inline int pfn_valid(unsigned long pfn)
325{
326 return pfn < max_mapnr;
327}
328#endif
329
330/* Provide as macros since these require some other headers included. */
331#define page_to_pa(page) ((phys_addr_t)(page_to_pfn(page)) << PAGE_SHIFT)
Chris Metcalf28d71742011-05-02 16:06:42 -0400332#define virt_to_page(kaddr) pfn_to_page(kaddr_to_pfn((void *)(kaddr)))
Chris Metcalf867e3592010-05-28 23:09:12 -0400333#define page_to_virt(page) pfn_to_kaddr(page_to_pfn(page))
334
335struct mm_struct;
336extern pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr);
337
338#endif /* !__ASSEMBLY__ */
339
340#define VM_DATA_DEFAULT_FLAGS \
341 (VM_READ | VM_WRITE | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC)
342
343#include <asm-generic/memory_model.h>
Chris Metcalf867e3592010-05-28 23:09:12 -0400344
Chris Metcalf867e3592010-05-28 23:09:12 -0400345#endif /* _ASM_TILE_PAGE_H */