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/sched/mm.h>
23 #include <linux/hugetlb.h>
24 #include <linux/pagemap.h>
25 #include <linux/slab.h>
26 #include <linux/err.h>
27 #include <linux/sysctl.h>
28 #include <linux/mman.h>
30 #include <asm/tlbflush.h>
31 #include <asm/setup.h>
33 #ifdef CONFIG_HUGETLB_SUPER_PAGES
36 * Provide an additional huge page size (in addition to the regular default
37 * huge page size) if no "hugepagesz" arguments are specified.
38 * Note that it must be smaller than the default huge page size so
39 * that it's possible to allocate them on demand from the buddy allocator.
40 * You can change this to 64K (on a 16K build), 256K, 1M, or 4M,
41 * or not define it at all.
43 #define ADDITIONAL_HUGE_SIZE (1024 * 1024UL)
45 /* "Extra" page-size multipliers, one per level of the page table. */
46 int huge_shift
[HUGE_SHIFT_ENTRIES
] = {
47 #ifdef ADDITIONAL_HUGE_SIZE
48 #define ADDITIONAL_HUGE_SHIFT __builtin_ctzl(ADDITIONAL_HUGE_SIZE / PAGE_SIZE)
49 [HUGE_SHIFT_PAGE
] = ADDITIONAL_HUGE_SHIFT
55 pte_t
*huge_pte_alloc(struct mm_struct
*mm
,
56 unsigned long addr
, unsigned long sz
)
61 addr
&= -sz
; /* Mask off any low bits in the address. */
63 pgd
= pgd_offset(mm
, addr
);
64 pud
= pud_alloc(mm
, pgd
, addr
);
66 #ifdef CONFIG_HUGETLB_SUPER_PAGES
67 if (sz
>= PGDIR_SIZE
) {
68 BUG_ON(sz
!= PGDIR_SIZE
&&
69 sz
!= PGDIR_SIZE
<< huge_shift
[HUGE_SHIFT_PGDIR
]);
72 pmd_t
*pmd
= pmd_alloc(mm
, pud
, addr
);
74 BUG_ON(sz
!= PMD_SIZE
&&
75 sz
!= (PMD_SIZE
<< huge_shift
[HUGE_SHIFT_PMD
]));
79 if (sz
!= PAGE_SIZE
<< huge_shift
[HUGE_SHIFT_PAGE
])
80 panic("Unexpected page size %#lx\n", sz
);
81 return pte_alloc_map(mm
, pmd
, addr
);
85 BUG_ON(sz
!= PMD_SIZE
);
86 return (pte_t
*) pmd_alloc(mm
, pud
, addr
);
90 static pte_t
*get_pte(pte_t
*base
, int index
, int level
)
92 pte_t
*ptep
= base
+ index
;
93 #ifdef CONFIG_HUGETLB_SUPER_PAGES
94 if (!pte_present(*ptep
) && huge_shift
[level
] != 0) {
95 unsigned long mask
= -1UL << huge_shift
[level
];
96 pte_t
*super_ptep
= base
+ (index
& mask
);
97 pte_t pte
= *super_ptep
;
98 if (pte_present(pte
) && pte_super(pte
))
105 pte_t
*huge_pte_offset(struct mm_struct
*mm
,
106 unsigned long addr
, unsigned long sz
)
111 #ifdef CONFIG_HUGETLB_SUPER_PAGES
115 /* Get the top-level page table entry. */
116 pgd
= (pgd_t
*)get_pte((pte_t
*)mm
->pgd
, pgd_index(addr
), 0);
118 /* We don't have four levels. */
119 pud
= pud_offset(pgd
, addr
);
120 #ifndef __PAGETABLE_PUD_FOLDED
121 # error support fourth page table level
123 if (!pud_present(*pud
))
126 /* Check for an L0 huge PTE, if we have three levels. */
127 #ifndef __PAGETABLE_PMD_FOLDED
131 pmd
= (pmd_t
*)get_pte((pte_t
*)pud_page_vaddr(*pud
),
133 if (!pmd_present(*pmd
))
136 pmd
= pmd_offset(pud
, addr
);
139 /* Check for an L1 huge PTE. */
143 #ifdef CONFIG_HUGETLB_SUPER_PAGES
144 /* Check for an L2 huge PTE. */
145 pte
= get_pte((pte_t
*)pmd_page_vaddr(*pmd
), pte_index(addr
), 2);
146 if (!pte_present(*pte
))
155 int pmd_huge(pmd_t pmd
)
157 return !!(pmd_val(pmd
) & _PAGE_HUGE_PAGE
);
160 int pud_huge(pud_t pud
)
162 return !!(pud_val(pud
) & _PAGE_HUGE_PAGE
);
165 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
166 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file
*file
,
167 unsigned long addr
, unsigned long len
,
168 unsigned long pgoff
, unsigned long flags
)
170 struct hstate
*h
= hstate_file(file
);
171 struct vm_unmapped_area_info info
;
175 info
.low_limit
= TASK_UNMAPPED_BASE
;
176 info
.high_limit
= TASK_SIZE
;
177 info
.align_mask
= PAGE_MASK
& ~huge_page_mask(h
);
178 info
.align_offset
= 0;
179 return vm_unmapped_area(&info
);
182 static unsigned long hugetlb_get_unmapped_area_topdown(struct file
*file
,
183 unsigned long addr0
, unsigned long len
,
184 unsigned long pgoff
, unsigned long flags
)
186 struct hstate
*h
= hstate_file(file
);
187 struct vm_unmapped_area_info info
;
190 info
.flags
= VM_UNMAPPED_AREA_TOPDOWN
;
192 info
.low_limit
= PAGE_SIZE
;
193 info
.high_limit
= current
->mm
->mmap_base
;
194 info
.align_mask
= PAGE_MASK
& ~huge_page_mask(h
);
195 info
.align_offset
= 0;
196 addr
= vm_unmapped_area(&info
);
199 * A failed mmap() very likely causes application failure,
200 * so fall back to the bottom-up function here. This scenario
201 * can happen with large stack limits and large mmap()
204 if (addr
& ~PAGE_MASK
) {
205 VM_BUG_ON(addr
!= -ENOMEM
);
207 info
.low_limit
= TASK_UNMAPPED_BASE
;
208 info
.high_limit
= TASK_SIZE
;
209 addr
= vm_unmapped_area(&info
);
215 unsigned long hugetlb_get_unmapped_area(struct file
*file
, unsigned long addr
,
216 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
218 struct hstate
*h
= hstate_file(file
);
219 struct mm_struct
*mm
= current
->mm
;
220 struct vm_area_struct
*vma
;
222 if (len
& ~huge_page_mask(h
))
227 if (flags
& MAP_FIXED
) {
228 if (prepare_hugepage_range(file
, addr
, len
))
234 addr
= ALIGN(addr
, huge_page_size(h
));
235 vma
= find_vma(mm
, addr
);
236 if (TASK_SIZE
- len
>= addr
&&
237 (!vma
|| addr
+ len
<= vm_start_gap(vma
)))
240 if (current
->mm
->get_unmapped_area
== arch_get_unmapped_area
)
241 return hugetlb_get_unmapped_area_bottomup(file
, addr
, len
,
244 return hugetlb_get_unmapped_area_topdown(file
, addr
, len
,
247 #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
249 #ifdef CONFIG_HUGETLB_SUPER_PAGES
250 static __init
int __setup_hugepagesz(unsigned long ps
)
252 int log_ps
= __builtin_ctzl(ps
);
253 int level
, base_shift
;
255 if ((1UL << log_ps
) != ps
|| (log_ps
& 1) != 0) {
256 pr_warn("Not enabling %ld byte huge pages; must be a power of four\n",
261 if (ps
> 64*1024*1024*1024UL) {
262 pr_warn("Not enabling %ld MB huge pages; largest legal value is 64 GB\n",
265 } else if (ps
>= PUD_SIZE
) {
266 static long hv_jpage_size
;
267 if (hv_jpage_size
== 0)
268 hv_jpage_size
= hv_sysconf(HV_SYSCONF_PAGE_SIZE_JUMBO
);
269 if (hv_jpage_size
!= PUD_SIZE
) {
270 pr_warn("Not enabling >= %ld MB huge pages: hypervisor reports size %ld\n",
271 PUD_SIZE
>> 20, hv_jpage_size
);
275 base_shift
= PUD_SHIFT
;
276 } else if (ps
>= PMD_SIZE
) {
278 base_shift
= PMD_SHIFT
;
279 } else if (ps
> PAGE_SIZE
) {
281 base_shift
= PAGE_SHIFT
;
283 pr_err("hugepagesz: huge page size %ld too small\n", ps
);
287 if (log_ps
!= base_shift
) {
288 int shift_val
= log_ps
- base_shift
;
289 if (huge_shift
[level
] != 0) {
290 int old_shift
= base_shift
+ huge_shift
[level
];
291 pr_warn("Not enabling %ld MB huge pages; already have size %ld MB\n",
292 ps
>> 20, (1UL << old_shift
) >> 20);
295 if (hv_set_pte_super_shift(level
, shift_val
) != 0) {
296 pr_warn("Not enabling %ld MB huge pages; no hypervisor support\n",
300 printk(KERN_DEBUG
"Enabled %ld MB huge pages\n", ps
>> 20);
301 huge_shift
[level
] = shift_val
;
304 hugetlb_add_hstate(log_ps
- PAGE_SHIFT
);
309 static bool saw_hugepagesz
;
311 static __init
int setup_hugepagesz(char *opt
)
315 if (!saw_hugepagesz
) {
316 saw_hugepagesz
= true;
317 memset(huge_shift
, 0, sizeof(huge_shift
));
319 rc
= __setup_hugepagesz(memparse(opt
, NULL
));
324 __setup("hugepagesz=", setup_hugepagesz
);
326 #ifdef ADDITIONAL_HUGE_SIZE
328 * Provide an additional huge page size if no "hugepagesz" args are given.
329 * In that case, all the cores have properly set up their hv super_shift
330 * already, but we need to notify the hugetlb code to enable the
331 * new huge page size from the Linux point of view.
333 static __init
int add_default_hugepagesz(void)
335 if (!saw_hugepagesz
) {
336 BUILD_BUG_ON(ADDITIONAL_HUGE_SIZE
>= PMD_SIZE
||
337 ADDITIONAL_HUGE_SIZE
<= PAGE_SIZE
);
338 BUILD_BUG_ON((PAGE_SIZE
<< ADDITIONAL_HUGE_SHIFT
) !=
339 ADDITIONAL_HUGE_SIZE
);
340 BUILD_BUG_ON(ADDITIONAL_HUGE_SHIFT
& 1);
341 hugetlb_add_hstate(ADDITIONAL_HUGE_SHIFT
);
345 arch_initcall(add_default_hugepagesz
);
348 #endif /* CONFIG_HUGETLB_SUPER_PAGES */