Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Jack Steiner | 34d8a38 | 2008-07-29 22:33:54 -0700 | [diff] [blame^] | 16 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> |
| 18 | #include <linux/sysctl.h> |
vignesh babu | 9be26f4 | 2007-06-07 15:27:46 +0530 | [diff] [blame] | 19 | #include <linux/log2.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/mman.h> |
| 21 | #include <asm/pgalloc.h> |
| 22 | #include <asm/tlb.h> |
| 23 | #include <asm/tlbflush.h> |
| 24 | |
Jack Steiner | 34d8a38 | 2008-07-29 22:33:54 -0700 | [diff] [blame^] | 25 | unsigned int hpage_shift = HPAGE_SHIFT_DEFAULT; |
| 26 | EXPORT_SYMBOL(hpage_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 28 | pte_t * |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 29 | huge_pte_alloc(struct mm_struct *mm, unsigned long addr, unsigned long sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | { |
| 31 | unsigned long taddr = htlbpage_to_page(addr); |
| 32 | pgd_t *pgd; |
| 33 | pud_t *pud; |
| 34 | pmd_t *pmd; |
| 35 | pte_t *pte = NULL; |
| 36 | |
| 37 | pgd = pgd_offset(mm, taddr); |
| 38 | pud = pud_alloc(mm, pgd, taddr); |
| 39 | if (pud) { |
| 40 | pmd = pmd_alloc(mm, pud, taddr); |
| 41 | if (pmd) |
| 42 | pte = pte_alloc_map(mm, pmd, taddr); |
| 43 | } |
| 44 | return pte; |
| 45 | } |
| 46 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 47 | pte_t * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | huge_pte_offset (struct mm_struct *mm, unsigned long addr) |
| 49 | { |
| 50 | unsigned long taddr = htlbpage_to_page(addr); |
| 51 | pgd_t *pgd; |
| 52 | pud_t *pud; |
| 53 | pmd_t *pmd; |
| 54 | pte_t *pte = NULL; |
| 55 | |
| 56 | pgd = pgd_offset(mm, taddr); |
| 57 | if (pgd_present(*pgd)) { |
| 58 | pud = pud_offset(pgd, taddr); |
| 59 | if (pud_present(*pud)) { |
| 60 | pmd = pmd_offset(pud, taddr); |
| 61 | if (pmd_present(*pmd)) |
| 62 | pte = pte_offset_map(pmd, taddr); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return pte; |
| 67 | } |
| 68 | |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 69 | int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) |
| 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #define mk_pte_huge(entry) { pte_val(entry) |= _PAGE_P; } |
| 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | /* |
David Gibson | 42b88be | 2006-03-22 00:09:01 -0800 | [diff] [blame] | 77 | * Don't actually need to do any preparation, but need to make sure |
| 78 | * the address is in the right region. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | */ |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 80 | int prepare_hugepage_range(struct file *file, |
| 81 | unsigned long addr, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | if (len & ~HPAGE_MASK) |
| 84 | return -EINVAL; |
| 85 | if (addr & ~HPAGE_MASK) |
| 86 | return -EINVAL; |
Peter Chubb | 0a41e25 | 2005-08-16 19:54:00 -0700 | [diff] [blame] | 87 | if (REGION_NUMBER(addr) != RGN_HPAGE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return -EINVAL; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | struct page *follow_huge_addr(struct mm_struct *mm, unsigned long addr, int write) |
| 94 | { |
| 95 | struct page *page; |
| 96 | pte_t *ptep; |
| 97 | |
Peter Chubb | 0a41e25 | 2005-08-16 19:54:00 -0700 | [diff] [blame] | 98 | if (REGION_NUMBER(addr) != RGN_HPAGE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | return ERR_PTR(-EINVAL); |
| 100 | |
| 101 | ptep = huge_pte_offset(mm, addr); |
| 102 | if (!ptep || pte_none(*ptep)) |
| 103 | return NULL; |
| 104 | page = pte_page(*ptep); |
| 105 | page += ((addr & ~HPAGE_MASK) >> PAGE_SHIFT); |
| 106 | return page; |
| 107 | } |
| 108 | int pmd_huge(pmd_t pmd) |
| 109 | { |
| 110 | return 0; |
| 111 | } |
Andi Kleen | ceb8687 | 2008-07-23 21:27:50 -0700 | [diff] [blame] | 112 | |
| 113 | int pud_huge(pud_t pud) |
| 114 | { |
| 115 | return 0; |
| 116 | } |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | struct page * |
| 119 | follow_huge_pmd(struct mm_struct *mm, unsigned long address, pmd_t *pmd, int write) |
| 120 | { |
| 121 | return NULL; |
| 122 | } |
| 123 | |
Jan Beulich | 42b7772 | 2008-07-23 21:27:10 -0700 | [diff] [blame] | 124 | void hugetlb_free_pgd_range(struct mmu_gather *tlb, |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 125 | unsigned long addr, unsigned long end, |
| 126 | unsigned long floor, unsigned long ceiling) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 128 | /* |
Chen, Kenneth W | 2332c9a | 2006-03-22 10:49:00 -0800 | [diff] [blame] | 129 | * This is called to free hugetlb page tables. |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 130 | * |
| 131 | * The offset of these addresses from the base of the hugetlb |
| 132 | * region must be scaled down by HPAGE_SIZE/PAGE_SIZE so that |
| 133 | * the standard free_pgd_range will free the right page tables. |
| 134 | * |
| 135 | * If floor and ceiling are also in the hugetlb region, they |
| 136 | * must likewise be scaled down; but if outside, left unchanged. |
| 137 | */ |
| 138 | |
| 139 | addr = htlbpage_to_page(addr); |
| 140 | end = htlbpage_to_page(end); |
Chen, Kenneth W | 2332c9a | 2006-03-22 10:49:00 -0800 | [diff] [blame] | 141 | if (REGION_NUMBER(floor) == RGN_HPAGE) |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 142 | floor = htlbpage_to_page(floor); |
Chen, Kenneth W | 2332c9a | 2006-03-22 10:49:00 -0800 | [diff] [blame] | 143 | if (REGION_NUMBER(ceiling) == RGN_HPAGE) |
Hugh Dickins | 3bf5ee9 | 2005-04-19 13:29:16 -0700 | [diff] [blame] | 144 | ceiling = htlbpage_to_page(ceiling); |
| 145 | |
| 146 | free_pgd_range(tlb, addr, end, floor, ceiling); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, |
| 150 | unsigned long pgoff, unsigned long flags) |
| 151 | { |
| 152 | struct vm_area_struct *vmm; |
| 153 | |
| 154 | if (len > RGN_MAP_LIMIT) |
| 155 | return -ENOMEM; |
| 156 | if (len & ~HPAGE_MASK) |
| 157 | return -EINVAL; |
Benjamin Herrenschmidt | afa3739 | 2007-05-06 14:50:09 -0700 | [diff] [blame] | 158 | |
| 159 | /* Handle MAP_FIXED */ |
| 160 | if (flags & MAP_FIXED) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 161 | if (prepare_hugepage_range(file, addr, len)) |
Benjamin Herrenschmidt | afa3739 | 2007-05-06 14:50:09 -0700 | [diff] [blame] | 162 | return -EINVAL; |
| 163 | return addr; |
| 164 | } |
| 165 | |
Peter Chubb | 0a41e25 | 2005-08-16 19:54:00 -0700 | [diff] [blame] | 166 | /* This code assumes that RGN_HPAGE != 0. */ |
| 167 | if ((REGION_NUMBER(addr) != RGN_HPAGE) || (addr & (HPAGE_SIZE - 1))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | addr = HPAGE_REGION_BASE; |
| 169 | else |
| 170 | addr = ALIGN(addr, HPAGE_SIZE); |
| 171 | for (vmm = find_vma(current->mm, addr); ; vmm = vmm->vm_next) { |
| 172 | /* At this point: (!vmm || addr < vmm->vm_end). */ |
| 173 | if (REGION_OFFSET(addr) + len > RGN_MAP_LIMIT) |
| 174 | return -ENOMEM; |
| 175 | if (!vmm || (addr + len) <= vmm->vm_start) |
| 176 | return addr; |
| 177 | addr = ALIGN(vmm->vm_end, HPAGE_SIZE); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static int __init hugetlb_setup_sz(char *str) |
| 182 | { |
| 183 | u64 tr_pages; |
| 184 | unsigned long long size; |
| 185 | |
| 186 | if (ia64_pal_vm_page_size(&tr_pages, NULL) != 0) |
| 187 | /* |
| 188 | * shouldn't happen, but just in case. |
| 189 | */ |
| 190 | tr_pages = 0x15557000UL; |
| 191 | |
| 192 | size = memparse(str, &str); |
vignesh babu | 9be26f4 | 2007-06-07 15:27:46 +0530 | [diff] [blame] | 193 | if (*str || !is_power_of_2(size) || !(tr_pages & size) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | size <= PAGE_SIZE || |
| 195 | size >= (1UL << PAGE_SHIFT << MAX_ORDER)) { |
| 196 | printk(KERN_WARNING "Invalid huge page size specified\n"); |
| 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | hpage_shift = __ffs(size); |
| 201 | /* |
| 202 | * boot cpu already executed ia64_mmu_init, and has HPAGE_SHIFT_DEFAULT |
| 203 | * override here with new page shift. |
| 204 | */ |
| 205 | ia64_set_rr(HPAGE_REGION_BASE, hpage_shift << 2); |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 206 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 208 | early_param("hugepagesz", hugetlb_setup_sz); |