2 * arch/metag/mm/hugetlbpage.c
4 * METAG HugeTLB page support.
8 * Cloned from sparc64 by Paul Mundt.
13 #include <linux/init.h>
16 #include <linux/hugetlb.h>
17 #include <linux/pagemap.h>
18 #include <linux/sysctl.h>
21 #include <asm/pgalloc.h>
23 #include <asm/tlbflush.h>
24 #include <asm/cacheflush.h>
27 * If the arch doesn't supply something else, assume that hugepage
28 * size aligned regions are ok without further preparation.
30 int prepare_hugepage_range(struct file *file, unsigned long addr,
33 struct mm_struct *mm = current->mm;
34 struct hstate *h = hstate_file(file);
35 struct vm_area_struct *vma;
37 if (len & ~huge_page_mask(h))
39 if (addr & ~huge_page_mask(h))
41 if (TASK_SIZE - len < addr)
44 vma = find_vma(mm, ALIGN_HUGEPT(addr));
45 if (vma && !(vma->vm_flags & MAP_HUGETLB))
48 vma = find_vma(mm, addr);
50 if (addr + len > vma->vm_start)
52 if (!(vma->vm_flags & MAP_HUGETLB) &&
53 (ALIGN_HUGEPT(addr + len) > vma->vm_start))
59 pte_t *huge_pte_alloc(struct mm_struct *mm,
60 unsigned long addr, unsigned long sz)
67 pgd = pgd_offset(mm, addr);
68 pud = pud_offset(pgd, addr);
69 pmd = pmd_offset(pud, addr);
70 pte = pte_alloc_map(mm, NULL, pmd, addr);
71 pgd->pgd &= ~_PAGE_SZ_MASK;
72 pgd->pgd |= _PAGE_SZHUGE;
77 pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
84 pgd = pgd_offset(mm, addr);
85 pud = pud_offset(pgd, addr);
86 pmd = pmd_offset(pud, addr);
87 pte = pte_offset_kernel(pmd, addr);
92 int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
97 int pmd_huge(pmd_t pmd)
99 return pmd_page_shift(pmd) > PAGE_SHIFT;
102 int pud_huge(pud_t pud)
107 struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address,
108 pmd_t *pmd, int write)
113 #ifdef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
116 * Look for an unmapped area starting after another hugetlb vma.
117 * There are guaranteed to be no huge pte's spare if all the huge pages are
118 * full size (4MB), so in that case compile out this search.
120 #if HPAGE_SHIFT == HUGEPT_SHIFT
121 static inline unsigned long
122 hugetlb_get_unmapped_area_existing(unsigned long len)
128 hugetlb_get_unmapped_area_existing(unsigned long len)
130 struct mm_struct *mm = current->mm;
131 struct vm_area_struct *vma;
132 unsigned long start_addr, addr;
135 if (mm->context.part_huge) {
136 start_addr = mm->context.part_huge;
139 start_addr = TASK_UNMAPPED_BASE;
145 for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
146 if ((!vma && !after_huge) || TASK_SIZE - len < addr) {
148 * Start a new search - just in case we missed
151 if (start_addr != TASK_UNMAPPED_BASE) {
152 start_addr = TASK_UNMAPPED_BASE;
157 /* skip ahead if we've aligned right over some vmas */
158 if (vma && vma->vm_end <= addr)
160 /* space before the next vma? */
161 if (after_huge && (!vma || ALIGN_HUGEPT(addr + len)
163 unsigned long end = addr + len;
164 if (end & HUGEPT_MASK)
165 mm->context.part_huge = end;
166 else if (addr == mm->context.part_huge)
167 mm->context.part_huge = 0;
170 if (vma->vm_flags & MAP_HUGETLB) {
171 /* space after a huge vma in 2nd level page table? */
172 if (vma->vm_end & HUGEPT_MASK) {
174 /* no need to align to the next PT block */
180 addr = ALIGN_HUGEPT(vma->vm_end);
185 /* Do a full search to find an area without any nearby normal pages. */
187 hugetlb_get_unmapped_area_new_pmd(unsigned long len)
189 struct vm_unmapped_area_info info;
193 info.low_limit = TASK_UNMAPPED_BASE;
194 info.high_limit = TASK_SIZE;
195 info.align_mask = PAGE_MASK & HUGEPT_MASK;
196 info.align_offset = 0;
197 return vm_unmapped_area(&info);
201 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
202 unsigned long len, unsigned long pgoff, unsigned long flags)
204 struct hstate *h = hstate_file(file);
206 if (len & ~huge_page_mask(h))
211 if (flags & MAP_FIXED) {
212 if (prepare_hugepage_range(file, addr, len))
218 addr = ALIGN(addr, huge_page_size(h));
219 if (!prepare_hugepage_range(file, addr, len))
224 * Look for an existing hugetlb vma with space after it (this is to to
225 * minimise fragmentation caused by huge pages.
227 addr = hugetlb_get_unmapped_area_existing(len);
232 * Find an unmapped naturally aligned set of 4MB blocks that we can use
235 return hugetlb_get_unmapped_area_new_pmd(len);
238 #endif /*HAVE_ARCH_HUGETLB_UNMAPPED_AREA*/
240 /* necessary for boot time 4MB huge page allocation */
241 static __init int setup_hugepagesz(char *opt)
243 unsigned long ps = memparse(opt, &opt);
244 if (ps == (1 << HPAGE_SHIFT)) {
245 hugetlb_add_hstate(HPAGE_SHIFT - PAGE_SHIFT);
247 pr_err("hugepagesz: Unsupported page size %lu M\n",
253 __setup("hugepagesz=", setup_hugepagesz);