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:
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, unsigned long addr)
110 #ifdef CONFIG_HUGETLB_SUPER_PAGES
114 /* Get the top-level page table entry. */
115 pgd = (pgd_t *)get_pte((pte_t *)mm->pgd, pgd_index(addr), 0);
117 /* We don't have four levels. */
118 pud = pud_offset(pgd, addr);
119 #ifndef __PAGETABLE_PUD_FOLDED
120 # error support fourth page table level
122 if (!pud_present(*pud))
125 /* Check for an L0 huge PTE, if we have three levels. */
126 #ifndef __PAGETABLE_PMD_FOLDED
130 pmd = (pmd_t *)get_pte((pte_t *)pud_page_vaddr(*pud),
132 if (!pmd_present(*pmd))
135 pmd = pmd_offset(pud, addr);
138 /* Check for an L1 huge PTE. */
142 #ifdef CONFIG_HUGETLB_SUPER_PAGES
143 /* Check for an L2 huge PTE. */
144 pte = get_pte((pte_t *)pmd_page_vaddr(*pmd), pte_index(addr), 2);
145 if (!pte_present(*pte))
154 int pmd_huge(pmd_t pmd)
156 return !!(pmd_val(pmd) & _PAGE_HUGE_PAGE);
159 int pud_huge(pud_t pud)
161 return !!(pud_val(pud) & _PAGE_HUGE_PAGE);
164 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
165 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
166 unsigned long addr, unsigned long len,
167 unsigned long pgoff, unsigned long flags)
169 struct hstate *h = hstate_file(file);
170 struct vm_unmapped_area_info info;
174 info.low_limit = TASK_UNMAPPED_BASE;
175 info.high_limit = TASK_SIZE;
176 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
177 info.align_offset = 0;
178 return vm_unmapped_area(&info);
181 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
182 unsigned long addr0, unsigned long len,
183 unsigned long pgoff, unsigned long flags)
185 struct hstate *h = hstate_file(file);
186 struct vm_unmapped_area_info info;
189 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
191 info.low_limit = PAGE_SIZE;
192 info.high_limit = current->mm->mmap_base;
193 info.align_mask = PAGE_MASK & ~huge_page_mask(h);
194 info.align_offset = 0;
195 addr = vm_unmapped_area(&info);
198 * A failed mmap() very likely causes application failure,
199 * so fall back to the bottom-up function here. This scenario
200 * can happen with large stack limits and large mmap()
203 if (addr & ~PAGE_MASK) {
204 VM_BUG_ON(addr != -ENOMEM);
206 info.low_limit = TASK_UNMAPPED_BASE;
207 info.high_limit = TASK_SIZE;
208 addr = vm_unmapped_area(&info);
214 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
215 unsigned long len, unsigned long pgoff, unsigned long flags)
217 struct hstate *h = hstate_file(file);
218 struct mm_struct *mm = current->mm;
219 struct vm_area_struct *vma;
221 if (len & ~huge_page_mask(h))
226 if (flags & MAP_FIXED) {
227 if (prepare_hugepage_range(file, addr, len))
233 addr = ALIGN(addr, huge_page_size(h));
234 vma = find_vma(mm, addr);
235 if (TASK_SIZE - len >= addr &&
236 (!vma || addr + len <= vma->vm_start))
239 if (current->mm->get_unmapped_area == arch_get_unmapped_area)
240 return hugetlb_get_unmapped_area_bottomup(file, addr, len,
243 return hugetlb_get_unmapped_area_topdown(file, addr, len,
246 #endif /* HAVE_ARCH_HUGETLB_UNMAPPED_AREA */
248 #ifdef CONFIG_HUGETLB_SUPER_PAGES
249 static __init int __setup_hugepagesz(unsigned long ps)
251 int log_ps = __builtin_ctzl(ps);
252 int level, base_shift;
254 if ((1UL << log_ps) != ps || (log_ps & 1) != 0) {
255 pr_warn("Not enabling %ld byte huge pages; must be a power of four\n",
260 if (ps > 64*1024*1024*1024UL) {
261 pr_warn("Not enabling %ld MB huge pages; largest legal value is 64 GB\n",
264 } else if (ps >= PUD_SIZE) {
265 static long hv_jpage_size;
266 if (hv_jpage_size == 0)
267 hv_jpage_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_JUMBO);
268 if (hv_jpage_size != PUD_SIZE) {
269 pr_warn("Not enabling >= %ld MB huge pages: hypervisor reports size %ld\n",
270 PUD_SIZE >> 20, hv_jpage_size);
274 base_shift = PUD_SHIFT;
275 } else if (ps >= PMD_SIZE) {
277 base_shift = PMD_SHIFT;
278 } else if (ps > PAGE_SIZE) {
280 base_shift = PAGE_SHIFT;
282 pr_err("hugepagesz: huge page size %ld too small\n", ps);
286 if (log_ps != base_shift) {
287 int shift_val = log_ps - base_shift;
288 if (huge_shift[level] != 0) {
289 int old_shift = base_shift + huge_shift[level];
290 pr_warn("Not enabling %ld MB huge pages; already have size %ld MB\n",
291 ps >> 20, (1UL << old_shift) >> 20);
294 if (hv_set_pte_super_shift(level, shift_val) != 0) {
295 pr_warn("Not enabling %ld MB huge pages; no hypervisor support\n",
299 printk(KERN_DEBUG "Enabled %ld MB huge pages\n", ps >> 20);
300 huge_shift[level] = shift_val;
303 hugetlb_add_hstate(log_ps - PAGE_SHIFT);
308 static bool saw_hugepagesz;
310 static __init int setup_hugepagesz(char *opt)
314 if (!saw_hugepagesz) {
315 saw_hugepagesz = true;
316 memset(huge_shift, 0, sizeof(huge_shift));
318 rc = __setup_hugepagesz(memparse(opt, NULL));
323 __setup("hugepagesz=", setup_hugepagesz);
325 #ifdef ADDITIONAL_HUGE_SIZE
327 * Provide an additional huge page size if no "hugepagesz" args are given.
328 * In that case, all the cores have properly set up their hv super_shift
329 * already, but we need to notify the hugetlb code to enable the
330 * new huge page size from the Linux point of view.
332 static __init int add_default_hugepagesz(void)
334 if (!saw_hugepagesz) {
335 BUILD_BUG_ON(ADDITIONAL_HUGE_SIZE >= PMD_SIZE ||
336 ADDITIONAL_HUGE_SIZE <= PAGE_SIZE);
337 BUILD_BUG_ON((PAGE_SIZE << ADDITIONAL_HUGE_SHIFT) !=
338 ADDITIONAL_HUGE_SIZE);
339 BUILD_BUG_ON(ADDITIONAL_HUGE_SHIFT & 1);
340 hugetlb_add_hstate(ADDITIONAL_HUGE_SHIFT);
344 arch_initcall(add_default_hugepagesz);
347 #endif /* CONFIG_HUGETLB_SUPER_PAGES */