blob: 5ca674b74737bd7bc4b3d3e03d58d534b8d44633 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/hugetlb.h>
15#include <linux/pagemap.h>
Jack Steiner34d8a382008-07-29 22:33:54 -070016#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/sysctl.h>
vignesh babu9be26f42007-06-07 15:27:46 +053018#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/mman.h>
20#include <asm/pgalloc.h>
21#include <asm/tlb.h>
22#include <asm/tlbflush.h>
23
Jack Steiner34d8a382008-07-29 22:33:54 -070024unsigned int hpage_shift = HPAGE_SHIFT_DEFAULT;
25EXPORT_SYMBOL(hpage_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Gibson63551ae2005-06-21 17:14:44 -070027pte_t *
Andi Kleena5516432008-07-23 21:27:41 -070028huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
30 unsigned long taddr = htlbpage_to_page(addr);
31 pgd_t *pgd;
32 pud_t *pud;
33 pmd_t *pmd;
34 pte_t *pte = NULL;
35
36 pgd = pgd_offset(mm, taddr);
37 pud = pud_alloc(mm, pgd, taddr);
38 if (pud) {
39 pmd = pmd_alloc(mm, pud, taddr);
40 if (pmd)
Andrea Arcangeli8ac1f832011-01-13 15:46:43 -080041 pte = pte_alloc_map(mm, NULL, pmd, taddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43 return pte;
44}
45
David Gibson63551ae2005-06-21 17:14:44 -070046pte_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -070047huge_pte_offset (struct mm_struct *mm, unsigned long addr)
48{
49 unsigned long taddr = htlbpage_to_page(addr);
50 pgd_t *pgd;
51 pud_t *pud;
52 pmd_t *pmd;
53 pte_t *pte = NULL;
54
55 pgd = pgd_offset(mm, taddr);
56 if (pgd_present(*pgd)) {
57 pud = pud_offset(pgd, taddr);
58 if (pud_present(*pud)) {
59 pmd = pmd_offset(pud, taddr);
60 if (pmd_present(*pmd))
61 pte = pte_offset_map(pmd, taddr);
62 }
63 }
64
65 return pte;
66}
67
Chen, Kenneth W39dde652006-12-06 20:32:03 -080068int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep)
69{
70 return 0;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define mk_pte_huge(entry) { pte_val(entry) |= _PAGE_P; }
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
David Gibson42b88be2006-03-22 00:09:01 -080076 * Don't actually need to do any preparation, but need to make sure
77 * the address is in the right region.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
Andi Kleena5516432008-07-23 21:27:41 -070079int prepare_hugepage_range(struct file *file,
80 unsigned long addr, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 if (len & ~HPAGE_MASK)
83 return -EINVAL;
84 if (addr & ~HPAGE_MASK)
85 return -EINVAL;
Peter Chubb0a41e252005-08-16 19:54:00 -070086 if (REGION_NUMBER(addr) != RGN_HPAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -EINVAL;
88
89 return 0;
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct page *follow_huge_addr(struct mm_struct *mm, unsigned long addr, int write)
93{
94 struct page *page;
95 pte_t *ptep;
96
Peter Chubb0a41e252005-08-16 19:54:00 -070097 if (REGION_NUMBER(addr) != RGN_HPAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return ERR_PTR(-EINVAL);
99
100 ptep = huge_pte_offset(mm, addr);
101 if (!ptep || pte_none(*ptep))
102 return NULL;
103 page = pte_page(*ptep);
104 page += ((addr & ~HPAGE_MASK) >> PAGE_SHIFT);
105 return page;
106}
107int pmd_huge(pmd_t pmd)
108{
109 return 0;
110}
Andi Kleenceb86872008-07-23 21:27:50 -0700111
112int pud_huge(pud_t pud)
113{
114 return 0;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117struct page *
118follow_huge_pmd(struct mm_struct *mm, unsigned long address, pmd_t *pmd, int write)
119{
120 return NULL;
121}
122
Jan Beulich42b77722008-07-23 21:27:10 -0700123void hugetlb_free_pgd_range(struct mmu_gather *tlb,
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700124 unsigned long addr, unsigned long end,
125 unsigned long floor, unsigned long ceiling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700127 /*
Chen, Kenneth W2332c9a2006-03-22 10:49:00 -0800128 * This is called to free hugetlb page tables.
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700129 *
130 * The offset of these addresses from the base of the hugetlb
131 * region must be scaled down by HPAGE_SIZE/PAGE_SIZE so that
132 * the standard free_pgd_range will free the right page tables.
133 *
134 * If floor and ceiling are also in the hugetlb region, they
135 * must likewise be scaled down; but if outside, left unchanged.
136 */
137
138 addr = htlbpage_to_page(addr);
139 end = htlbpage_to_page(end);
Chen, Kenneth W2332c9a2006-03-22 10:49:00 -0800140 if (REGION_NUMBER(floor) == RGN_HPAGE)
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700141 floor = htlbpage_to_page(floor);
Chen, Kenneth W2332c9a2006-03-22 10:49:00 -0800142 if (REGION_NUMBER(ceiling) == RGN_HPAGE)
Hugh Dickins3bf5ee92005-04-19 13:29:16 -0700143 ceiling = htlbpage_to_page(ceiling);
144
145 free_pgd_range(tlb, addr, end, floor, ceiling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
149 unsigned long pgoff, unsigned long flags)
150{
151 struct vm_area_struct *vmm;
152
153 if (len > RGN_MAP_LIMIT)
154 return -ENOMEM;
155 if (len & ~HPAGE_MASK)
156 return -EINVAL;
Benjamin Herrenschmidtafa37392007-05-06 14:50:09 -0700157
158 /* Handle MAP_FIXED */
159 if (flags & MAP_FIXED) {
Andi Kleena5516432008-07-23 21:27:41 -0700160 if (prepare_hugepage_range(file, addr, len))
Benjamin Herrenschmidtafa37392007-05-06 14:50:09 -0700161 return -EINVAL;
162 return addr;
163 }
164
Peter Chubb0a41e252005-08-16 19:54:00 -0700165 /* This code assumes that RGN_HPAGE != 0. */
166 if ((REGION_NUMBER(addr) != RGN_HPAGE) || (addr & (HPAGE_SIZE - 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 addr = HPAGE_REGION_BASE;
168 else
169 addr = ALIGN(addr, HPAGE_SIZE);
170 for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) {
171 /* At this point: (!vmm || addr < vmm->vm_end). */
172 if (REGION_OFFSET(addr) + len > RGN_MAP_LIMIT)
173 return -ENOMEM;
174 if (!vmm || (addr + len) <= vmm->vm_start)
175 return addr;
176 addr = ALIGN(vmm->vm_end, HPAGE_SIZE);
177 }
178}
179
180static int __init hugetlb_setup_sz(char *str)
181{
182 u64 tr_pages;
183 unsigned long long size;
184
185 if (ia64_pal_vm_page_size(&tr_pages, NULL) != 0)
186 /*
187 * shouldn't happen, but just in case.
188 */
189 tr_pages = 0x15557000UL;
190
191 size = memparse(str, &str);
vignesh babu9be26f42007-06-07 15:27:46 +0530192 if (*str || !is_power_of_2(size) || !(tr_pages & size) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 size <= PAGE_SIZE ||
194 size >= (1UL << PAGE_SHIFT << MAX_ORDER)) {
195 printk(KERN_WARNING "Invalid huge page size specified\n");
196 return 1;
197 }
198
199 hpage_shift = __ffs(size);
200 /*
201 * boot cpu already executed ia64_mmu_init, and has HPAGE_SHIFT_DEFAULT
202 * override here with new page shift.
203 */
204 ia64_set_rr(HPAGE_REGION_BASE, hpage_shift << 2);
Mel Gormand9c23402007-10-16 01:26:01 -0700205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
Mel Gormand9c23402007-10-16 01:26:01 -0700207early_param("hugepagesz", hugetlb_setup_sz);