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
14 * TILE Huge TLB Page Support for Kernel.
15 * Taken from i386 hugetlb implementation:
16 * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
19 #include <linux/init.h>
22 #include <linux/hugetlb.h>
23 #include <linux/pagemap.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/sysctl.h>
27 #include <linux/mman.h>
29 #include <asm/tlbflush.h>
30 #include <asm/setup.h>
32 #ifdef CONFIG_HUGETLB_SUPER_PAGES
35 * Provide an additional huge page size (in addition to the regular default
36 * huge page size) if no "hugepagesz" arguments are specified.
37 * Note that it must be smaller than the default huge page size so
38 * that it's possible to allocate them on demand from the buddy allocator.
39 * You can change this to 64K (on a 16K build), 256K, 1M, or 4M,
40 * or not define it at all.
42 #define ADDITIONAL_HUGE_SIZE (1024 * 1024UL)
44 /* "Extra" page-size multipliers, one per level of the page table. */
45 int huge_shift
[HUGE_SHIFT_ENTRIES
] = {
46 #ifdef ADDITIONAL_HUGE_SIZE
47 #define ADDITIONAL_HUGE_SHIFT __builtin_ctzl(ADDITIONAL_HUGE_SIZE / PAGE_SIZE)
48 [HUGE_SHIFT_PAGE
] = ADDITIONAL_HUGE_SHIFT
54 pte_t
*huge_pte_alloc(struct mm_struct
*mm
,
55 unsigned long addr
, unsigned long sz
)
60 addr
&= -sz
; /* Mask off any low bits in the address. */
62 pgd
= pgd_offset(mm
, addr
);
63 pud
= pud_alloc(mm
, pgd
, addr
);
65 #ifdef CONFIG_HUGETLB_SUPER_PAGES
66 if (sz
>= PGDIR_SIZE
) {
67 BUG_ON(sz
!= PGDIR_SIZE
&&
68 sz
!= PGDIR_SIZE
<< huge_shift
[HUGE_SHIFT_PGDIR
]);
71 pmd_t
*pmd
= pmd_alloc(mm
, pud
, addr
);
73 BUG_ON(sz
!= PMD_SIZE
&&
74 sz
!= (PMD_SIZE
<< huge_shift
[HUGE_SHIFT_PMD
]));
78 if (sz
!= PAGE_SIZE
<< huge_shift
[HUGE_SHIFT_PAGE
])
79 panic("Unexpected page size %#lx\n", sz
);
80 return pte_alloc_map(mm
, NULL
, pmd
, addr
);
84 BUG_ON(sz
!= PMD_SIZE
);
85 return (pte_t
*) pmd_alloc(mm
, pud
, addr
);
89 static pte_t
*get_pte(pte_t
*base
, int index
, int level
)
91 pte_t
*ptep
= base
+ index
;
92 #ifdef CONFIG_HUGETLB_SUPER_PAGES
93 if (!pte_present(*ptep
) && huge_shift
[level
] != 0) {
94 unsigned long mask
= -1UL << huge_shift
[level
];
95 pte_t
*super_ptep
= base
+ (index
& mask
);
96 pte_t pte
= *super_ptep
;
97 if (pte_present(pte
) && pte_super(pte
))
104 pte_t
*huge_pte_offset(struct mm_struct
*mm
, unsigned long addr
)
109 #ifdef CONFIG_HUGETLB_SUPER_PAGES
113 /* Get the top-level page table entry. */
114 pgd
= (pgd_t
*)get_pte((pte_t
*)mm
->pgd
, pgd_index(addr
), 0);
116 /* We don't have four levels. */
117 pud
= pud_offset(pgd
, addr
);
118 #ifndef __PAGETABLE_PUD_FOLDED
119 # error support fourth page table level
121 if (!pud_present(*pud
))
124 /* Check for an L0 huge PTE, if we have three levels. */
125 #ifndef __PAGETABLE_PMD_FOLDED
129 pmd
= (pmd_t
*)get_pte((pte_t
*)pud_page_vaddr(*pud
),
131 if (!pmd_present(*pmd
))
134 pmd
= pmd_offset(pud
, addr
);
137 /* Check for an L1 huge PTE. */
141 #ifdef CONFIG_HUGETLB_SUPER_PAGES
142 /* Check for an L2 huge PTE. */
143 pte
= get_pte((pte_t
*)pmd_page_vaddr(*pmd
), pte_index(addr
), 2);
144 if (!pte_present(*pte
))
153 struct page
*follow_huge_addr(struct mm_struct
*mm
, unsigned long address
,
156 return ERR_PTR(-EINVAL
);
159 int pmd_huge(pmd_t pmd
)
161 return !!(pmd_val(pmd
) & _PAGE_HUGE_PAGE
);
164 int pud_huge(pud_t pud
)
166 return !!(pud_val(pud
) & _PAGE_HUGE_PAGE
);
169 int pmd_huge_support(void)
174 struct page
*follow_huge_pmd(struct mm_struct
*mm
, unsigned long address
,
175 pmd_t
*pmd
, int write
)
179 page
= pte_page(*(pte_t
*)pmd
);
181 page
+= ((address
& ~PMD_MASK
) >> PAGE_SHIFT
);
185 struct page
*follow_huge_pud(struct mm_struct
*mm
, unsigned long address
,
186 pud_t
*pud
, int write
)
190 page
= pte_page(*(pte_t
*)pud
);
192 page
+= ((address
& ~PUD_MASK
) >> PAGE_SHIFT
);
196 int huge_pmd_unshare(struct mm_struct
*mm
, unsigned long *addr
, pte_t
*ptep
)
201 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
202 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file
*file
,
203 unsigned long addr
, unsigned long len
,
204 unsigned long pgoff
, unsigned long flags
)
206 struct hstate
*h
= hstate_file(file
);
207 struct vm_unmapped_area_info info
;
211 info
.low_limit
= TASK_UNMAPPED_BASE
;
212 info
.high_limit
= TASK_SIZE
;
213 info
.align_mask
= PAGE_MASK
& ~huge_page_mask(h
);
214 info
.align_offset
= 0;
215 return vm_unmapped_area(&info
);
218 static unsigned long hugetlb_get_unmapped_area_topdown(struct file
*file
,
219 unsigned long addr0
, unsigned long len
,
220 unsigned long pgoff
, unsigned long flags
)
222 struct hstate
*h
= hstate_file(file
);
223 struct vm_unmapped_area_info info
;
226 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
228 info
.low_limit
= PAGE_SIZE
;
229 info
.high_limit
= current
->mm
->mmap_base
;
230 info
.align_mask
= PAGE_MASK
& ~huge_page_mask(h
);
231 info
.align_offset
= 0;
232 addr
= vm_unmapped_area(&info
);
235 * A failed mmap() very likely causes application failure,
236 * so fall back to the bottom-up function here. This scenario
237 * can happen with large stack limits and large mmap()
240 if (addr
& ~PAGE_MASK
) {
241 VM_BUG_ON(addr
!= -ENOMEM
);
243 info
.low_limit
= TASK_UNMAPPED_BASE
;
244 info
.high_limit
= TASK_SIZE
;
245 addr
= vm_unmapped_area(&info
);
251 unsigned long hugetlb_get_unmapped_area(struct file
*file
, unsigned long addr
,
252 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
254 struct hstate
*h
= hstate_file(file
);
255 struct mm_struct
*mm
= current
->mm
;
256 struct vm_area_struct
*vma
;
258 if (len
& ~huge_page_mask(h
))
263 if (flags
& MAP_FIXED
) {
264 if (prepare_hugepage_range(file
, addr
, len
))
270 addr
= ALIGN(addr
, huge_page_size(h
));
271 vma
= find_vma(mm
, addr
);
272 if (TASK_SIZE
- len
>= addr
&&
273 (!vma
|| addr
+ len
<= vma
->vm_start
))
276 if (current
->mm
->get_unmapped_area
== arch_get_unmapped_area
)
277 return hugetlb_get_unmapped_area_bottomup(file
, addr
, len
,
280 return hugetlb_get_unmapped_area_topdown(file
, addr
, len
,
283 #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
285 #ifdef CONFIG_HUGETLB_SUPER_PAGES
286 static __init
int __setup_hugepagesz(unsigned long ps
)
288 int log_ps
= __builtin_ctzl(ps
);
289 int level
, base_shift
;
291 if ((1UL << log_ps
) != ps
|| (log_ps
& 1) != 0) {
292 pr_warn("Not enabling %ld byte huge pages;"
293 " must be a power of four.\n", ps
);
297 if (ps
> 64*1024*1024*1024UL) {
298 pr_warn("Not enabling %ld MB huge pages;"
299 " largest legal value is 64 GB .\n", ps
>> 20);
301 } else if (ps
>= PUD_SIZE
) {
302 static long hv_jpage_size
;
303 if (hv_jpage_size
== 0)
304 hv_jpage_size
= hv_sysconf(HV_SYSCONF_PAGE_SIZE_JUMBO
);
305 if (hv_jpage_size
!= PUD_SIZE
) {
306 pr_warn("Not enabling >= %ld MB huge pages:"
307 " hypervisor reports size %ld\n",
308 PUD_SIZE
>> 20, hv_jpage_size
);
312 base_shift
= PUD_SHIFT
;
313 } else if (ps
>= PMD_SIZE
) {
315 base_shift
= PMD_SHIFT
;
316 } else if (ps
> PAGE_SIZE
) {
318 base_shift
= PAGE_SHIFT
;
320 pr_err("hugepagesz: huge page size %ld too small\n", ps
);
324 if (log_ps
!= base_shift
) {
325 int shift_val
= log_ps
- base_shift
;
326 if (huge_shift
[level
] != 0) {
327 int old_shift
= base_shift
+ huge_shift
[level
];
328 pr_warn("Not enabling %ld MB huge pages;"
329 " already have size %ld MB.\n",
330 ps
>> 20, (1UL << old_shift
) >> 20);
333 if (hv_set_pte_super_shift(level
, shift_val
) != 0) {
334 pr_warn("Not enabling %ld MB huge pages;"
335 " no hypervisor support.\n", ps
>> 20);
338 printk(KERN_DEBUG
"Enabled %ld MB huge pages\n", ps
>> 20);
339 huge_shift
[level
] = shift_val
;
342 hugetlb_add_hstate(log_ps
- PAGE_SHIFT
);
347 static bool saw_hugepagesz
;
349 static __init
int setup_hugepagesz(char *opt
)
351 if (!saw_hugepagesz
) {
352 saw_hugepagesz
= true;
353 memset(huge_shift
, 0, sizeof(huge_shift
));
355 return __setup_hugepagesz(memparse(opt
, NULL
));
357 __setup("hugepagesz=", setup_hugepagesz
);
359 #ifdef ADDITIONAL_HUGE_SIZE
361 * Provide an additional huge page size if no "hugepagesz" args are given.
362 * In that case, all the cores have properly set up their hv super_shift
363 * already, but we need to notify the hugetlb code to enable the
364 * new huge page size from the Linux point of view.
366 static __init
int add_default_hugepagesz(void)
368 if (!saw_hugepagesz
) {
369 BUILD_BUG_ON(ADDITIONAL_HUGE_SIZE
>= PMD_SIZE
||
370 ADDITIONAL_HUGE_SIZE
<= PAGE_SIZE
);
371 BUILD_BUG_ON((PAGE_SIZE
<< ADDITIONAL_HUGE_SHIFT
) !=
372 ADDITIONAL_HUGE_SIZE
);
373 BUILD_BUG_ON(ADDITIONAL_HUGE_SHIFT
& 1);
374 hugetlb_add_hstate(ADDITIONAL_HUGE_SHIFT
);
378 arch_initcall(add_default_hugepagesz
);
381 #endif /* CONFIG_HUGETLB_SUPER_PAGES */