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