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