Remove obsolete #include <linux/config.h>
[linux-3.10.git] / arch / ia64 / mm / hugetlbpage.c
1 /*
2  * IA-64 Huge TLB Page Support for Kernel.
3  *
4  * Copyright (C) 2002-2004 Rohit Seth <rohit.seth@intel.com>
5  * Copyright (C) 2003-2004 Ken Chen <kenneth.w.chen@intel.com>
6  *
7  * Sep, 2003: add numa support
8  * Feb, 2004: dynamic hugetlb page size via boot parameter
9  */
10
11 #include <linux/init.h>
12 #include <linux/fs.h>
13 #include <linux/mm.h>
14 #include <linux/hugetlb.h>
15 #include <linux/pagemap.h>
16 #include <linux/smp_lock.h>
17 #include <linux/slab.h>
18 #include <linux/sysctl.h>
19 #include <asm/mman.h>
20 #include <asm/pgalloc.h>
21 #include <asm/tlb.h>
22 #include <asm/tlbflush.h>
23
24 unsigned int hpage_shift=HPAGE_SHIFT_DEFAULT;
25
26 pte_t *
27 huge_pte_alloc (struct mm_struct *mm, unsigned long addr)
28 {
29         unsigned long taddr = htlbpage_to_page(addr);
30         pgd_t *pgd;
31         pud_t *pud;
32         pmd_t *pmd;
33         pte_t *pte = NULL;
34
35         pgd = pgd_offset(mm, taddr);
36         pud = pud_alloc(mm, pgd, taddr);
37         if (pud) {
38                 pmd = pmd_alloc(mm, pud, taddr);
39                 if (pmd)
40                         pte = pte_alloc_map(mm, pmd, taddr);
41         }
42         return pte;
43 }
44
45 pte_t *
46 huge_pte_offset (struct mm_struct *mm, unsigned long addr)
47 {
48         unsigned long taddr = htlbpage_to_page(addr);
49         pgd_t *pgd;
50         pud_t *pud;
51         pmd_t *pmd;
52         pte_t *pte = NULL;
53
54         pgd = pgd_offset(mm, taddr);
55         if (pgd_present(*pgd)) {
56                 pud = pud_offset(pgd, taddr);
57                 if (pud_present(*pud)) {
58                         pmd = pmd_offset(pud, taddr);
59                         if (pmd_present(*pmd))
60                                 pte = pte_offset_map(pmd, taddr);
61                 }
62         }
63
64         return pte;
65 }
66
67 #define mk_pte_huge(entry) { pte_val(entry) |= _PAGE_P; }
68
69 /*
70  * Don't actually need to do any preparation, but need to make sure
71  * the address is in the right region.
72  */
73 int prepare_hugepage_range(unsigned long addr, unsigned long len)
74 {
75         if (len & ~HPAGE_MASK)
76                 return -EINVAL;
77         if (addr & ~HPAGE_MASK)
78                 return -EINVAL;
79         if (REGION_NUMBER(addr) != RGN_HPAGE)
80                 return -EINVAL;
81
82         return 0;
83 }
84
85 struct page *follow_huge_addr(struct mm_struct *mm, unsigned long addr, int write)
86 {
87         struct page *page;
88         pte_t *ptep;
89
90         if (REGION_NUMBER(addr) != RGN_HPAGE)
91                 return ERR_PTR(-EINVAL);
92
93         ptep = huge_pte_offset(mm, addr);
94         if (!ptep || pte_none(*ptep))
95                 return NULL;
96         page = pte_page(*ptep);
97         page += ((addr & ~HPAGE_MASK) >> PAGE_SHIFT);
98         return page;
99 }
100 int pmd_huge(pmd_t pmd)
101 {
102         return 0;
103 }
104 struct page *
105 follow_huge_pmd(struct mm_struct *mm, unsigned long address, pmd_t *pmd, int write)
106 {
107         return NULL;
108 }
109
110 void hugetlb_free_pgd_range(struct mmu_gather **tlb,
111                         unsigned long addr, unsigned long end,
112                         unsigned long floor, unsigned long ceiling)
113 {
114         /*
115          * This is called to free hugetlb page tables.
116          *
117          * The offset of these addresses from the base of the hugetlb
118          * region must be scaled down by HPAGE_SIZE/PAGE_SIZE so that
119          * the standard free_pgd_range will free the right page tables.
120          *
121          * If floor and ceiling are also in the hugetlb region, they
122          * must likewise be scaled down; but if outside, left unchanged.
123          */
124
125         addr = htlbpage_to_page(addr);
126         end  = htlbpage_to_page(end);
127         if (REGION_NUMBER(floor) == RGN_HPAGE)
128                 floor = htlbpage_to_page(floor);
129         if (REGION_NUMBER(ceiling) == RGN_HPAGE)
130                 ceiling = htlbpage_to_page(ceiling);
131
132         free_pgd_range(tlb, addr, end, floor, ceiling);
133 }
134
135 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
136                 unsigned long pgoff, unsigned long flags)
137 {
138         struct vm_area_struct *vmm;
139
140         if (len > RGN_MAP_LIMIT)
141                 return -ENOMEM;
142         if (len & ~HPAGE_MASK)
143                 return -EINVAL;
144         /* This code assumes that RGN_HPAGE != 0. */
145         if ((REGION_NUMBER(addr) != RGN_HPAGE) || (addr & (HPAGE_SIZE - 1)))
146                 addr = HPAGE_REGION_BASE;
147         else
148                 addr = ALIGN(addr, HPAGE_SIZE);
149         for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
150                 /* At this point:  (!vmm || addr < vmm->vm_end). */
151                 if (REGION_OFFSET(addr) + len > RGN_MAP_LIMIT)
152                         return -ENOMEM;
153                 if (!vmm || (addr + len) <= vmm->vm_start)
154                         return addr;
155                 addr = ALIGN(vmm->vm_end, HPAGE_SIZE);
156         }
157 }
158
159 static int __init hugetlb_setup_sz(char *str)
160 {
161         u64 tr_pages;
162         unsigned long long size;
163
164         if (ia64_pal_vm_page_size(&tr_pages, NULL) != 0)
165                 /*
166                  * shouldn't happen, but just in case.
167                  */
168                 tr_pages = 0x15557000UL;
169
170         size = memparse(str, &str);
171         if (*str || (size & (size-1)) || !(tr_pages & size) ||
172                 size <= PAGE_SIZE ||
173                 size >= (1UL << PAGE_SHIFT << MAX_ORDER)) {
174                 printk(KERN_WARNING "Invalid huge page size specified\n");
175                 return 1;
176         }
177
178         hpage_shift = __ffs(size);
179         /*
180          * boot cpu already executed ia64_mmu_init, and has HPAGE_SHIFT_DEFAULT
181          * override here with new page shift.
182          */
183         ia64_set_rr(HPAGE_REGION_BASE, hpage_shift << 2);
184         return 1;
185 }
186 __setup("hugepagesz=", hugetlb_setup_sz);