Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * This file contains the routines setting up the linux page tables. |
| 3 | * -- paulus |
| 4 | * |
| 5 | * Derived from arch/ppc/mm/init.c: |
| 6 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) |
| 7 | * |
| 8 | * Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au) |
| 9 | * and Cort Dougan (PReP) (cort@cs.nmt.edu) |
| 10 | * Copyright (C) 1996 Paul Mackerras |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 11 | * |
| 12 | * Derived from "arch/i386/mm/init.c" |
| 13 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or |
| 16 | * modify it under the terms of the GNU General Public License |
| 17 | * as published by the Free Software Foundation; either version |
| 18 | * 2 of the License, or (at your option) any later version. |
| 19 | * |
| 20 | */ |
| 21 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/mm.h> |
| 26 | #include <linux/vmalloc.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/highmem.h> |
| 29 | |
| 30 | #include <asm/pgtable.h> |
| 31 | #include <asm/pgalloc.h> |
| 32 | #include <asm/io.h> |
| 33 | |
| 34 | #include "mmu_decl.h" |
| 35 | |
| 36 | unsigned long ioremap_base; |
| 37 | unsigned long ioremap_bot; |
Olaf Hering | 920573b | 2006-03-14 21:21:11 +0100 | [diff] [blame] | 38 | EXPORT_SYMBOL(ioremap_bot); /* aka VMALLOC_END */ |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 39 | |
| 40 | #if defined(CONFIG_6xx) || defined(CONFIG_POWER3) |
| 41 | #define HAVE_BATS 1 |
| 42 | #endif |
| 43 | |
| 44 | #if defined(CONFIG_FSL_BOOKE) |
| 45 | #define HAVE_TLBCAM 1 |
| 46 | #endif |
| 47 | |
| 48 | extern char etext[], _stext[]; |
| 49 | |
| 50 | #ifdef CONFIG_SMP |
| 51 | extern void hash_page_sync(void); |
| 52 | #endif |
| 53 | |
| 54 | #ifdef HAVE_BATS |
| 55 | extern unsigned long v_mapped_by_bats(unsigned long va); |
| 56 | extern unsigned long p_mapped_by_bats(unsigned long pa); |
| 57 | void setbat(int index, unsigned long virt, unsigned long phys, |
| 58 | unsigned int size, int flags); |
| 59 | |
| 60 | #else /* !HAVE_BATS */ |
| 61 | #define v_mapped_by_bats(x) (0UL) |
| 62 | #define p_mapped_by_bats(x) (0UL) |
| 63 | #endif /* HAVE_BATS */ |
| 64 | |
| 65 | #ifdef HAVE_TLBCAM |
| 66 | extern unsigned int tlbcam_index; |
| 67 | extern unsigned long v_mapped_by_tlbcam(unsigned long va); |
| 68 | extern unsigned long p_mapped_by_tlbcam(unsigned long pa); |
| 69 | #else /* !HAVE_TLBCAM */ |
| 70 | #define v_mapped_by_tlbcam(x) (0UL) |
| 71 | #define p_mapped_by_tlbcam(x) (0UL) |
| 72 | #endif /* HAVE_TLBCAM */ |
| 73 | |
| 74 | #ifdef CONFIG_PTE_64BIT |
| 75 | /* 44x uses an 8kB pgdir because it has 8-byte Linux PTEs. */ |
| 76 | #define PGDIR_ORDER 1 |
| 77 | #else |
| 78 | #define PGDIR_ORDER 0 |
| 79 | #endif |
| 80 | |
| 81 | pgd_t *pgd_alloc(struct mm_struct *mm) |
| 82 | { |
| 83 | pgd_t *ret; |
| 84 | |
| 85 | ret = (pgd_t *)__get_free_pages(GFP_KERNEL|__GFP_ZERO, PGDIR_ORDER); |
| 86 | return ret; |
| 87 | } |
| 88 | |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 89 | void pgd_free(struct mm_struct *mm, pgd_t *pgd) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 90 | { |
| 91 | free_pages((unsigned long)pgd, PGDIR_ORDER); |
| 92 | } |
| 93 | |
Kumar Gala | f1aed92 | 2007-05-23 07:49:37 -0500 | [diff] [blame] | 94 | __init_refok pte_t *pte_alloc_one_kernel(struct mm_struct *mm, unsigned long address) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 95 | { |
| 96 | pte_t *pte; |
| 97 | extern int mem_init_done; |
| 98 | extern void *early_get_page(void); |
| 99 | |
| 100 | if (mem_init_done) { |
| 101 | pte = (pte_t *)__get_free_page(GFP_KERNEL|__GFP_REPEAT|__GFP_ZERO); |
| 102 | } else { |
| 103 | pte = (pte_t *)early_get_page(); |
| 104 | if (pte) |
| 105 | clear_page(pte); |
| 106 | } |
| 107 | return pte; |
| 108 | } |
| 109 | |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame^] | 110 | pgtable_t pte_alloc_one(struct mm_struct *mm, unsigned long address) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 111 | { |
| 112 | struct page *ptepage; |
| 113 | |
| 114 | #ifdef CONFIG_HIGHPTE |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame^] | 115 | gfp_t flags = GFP_KERNEL | __GFP_HIGHMEM | __GFP_REPEAT | __GFP_ZERO; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 116 | #else |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame^] | 117 | gfp_t flags = GFP_KERNEL | __GFP_REPEAT | __GFP_ZERO; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | ptepage = alloc_pages(flags, 0); |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame^] | 121 | if (!ptepage) |
| 122 | return NULL; |
| 123 | pgtable_page_ctor(ptepage); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 124 | return ptepage; |
| 125 | } |
| 126 | |
Benjamin Herrenschmidt | 5e54197 | 2008-02-04 22:29:14 -0800 | [diff] [blame] | 127 | void pte_free_kernel(struct mm_struct *mm, pte_t *pte) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 128 | { |
| 129 | #ifdef CONFIG_SMP |
| 130 | hash_page_sync(); |
| 131 | #endif |
| 132 | free_page((unsigned long)pte); |
| 133 | } |
| 134 | |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame^] | 135 | void pte_free(struct mm_struct *mm, pgtable_t ptepage) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 136 | { |
| 137 | #ifdef CONFIG_SMP |
| 138 | hash_page_sync(); |
| 139 | #endif |
Martin Schwidefsky | 2f569af | 2008-02-08 04:22:04 -0800 | [diff] [blame^] | 140 | pgtable_page_dtor(ptepage); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 141 | __free_page(ptepage); |
| 142 | } |
| 143 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 144 | void __iomem * |
| 145 | ioremap(phys_addr_t addr, unsigned long size) |
| 146 | { |
| 147 | return __ioremap(addr, size, _PAGE_NO_CACHE); |
| 148 | } |
Olaf Hering | 920573b | 2006-03-14 21:21:11 +0100 | [diff] [blame] | 149 | EXPORT_SYMBOL(ioremap); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 150 | |
| 151 | void __iomem * |
Benjamin Herrenschmidt | 68a6435 | 2006-11-13 09:27:39 +1100 | [diff] [blame] | 152 | ioremap_flags(phys_addr_t addr, unsigned long size, unsigned long flags) |
| 153 | { |
| 154 | return __ioremap(addr, size, flags); |
| 155 | } |
| 156 | EXPORT_SYMBOL(ioremap_flags); |
| 157 | |
| 158 | void __iomem * |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 159 | __ioremap(phys_addr_t addr, unsigned long size, unsigned long flags) |
| 160 | { |
| 161 | unsigned long v, i; |
| 162 | phys_addr_t p; |
| 163 | int err; |
| 164 | |
| 165 | /* |
| 166 | * Choose an address to map it to. |
| 167 | * Once the vmalloc system is running, we use it. |
| 168 | * Before then, we use space going down from ioremap_base |
| 169 | * (ioremap_bot records where we're up to). |
| 170 | */ |
| 171 | p = addr & PAGE_MASK; |
| 172 | size = PAGE_ALIGN(addr + size) - p; |
| 173 | |
| 174 | /* |
| 175 | * If the address lies within the first 16 MB, assume it's in ISA |
| 176 | * memory space |
| 177 | */ |
| 178 | if (p < 16*1024*1024) |
| 179 | p += _ISA_MEM_BASE; |
| 180 | |
| 181 | /* |
| 182 | * Don't allow anybody to remap normal RAM that we're using. |
| 183 | * mem_init() sets high_memory so only do the check after that. |
| 184 | */ |
Paul Mackerras | 7c8c6b9 | 2005-10-06 12:23:33 +1000 | [diff] [blame] | 185 | if (mem_init_done && (p < virt_to_phys(high_memory))) { |
David Gibson | 37f01d6 | 2007-04-24 15:05:18 +1000 | [diff] [blame] | 186 | printk("__ioremap(): phys addr 0x%llx is RAM lr %p\n", |
| 187 | (unsigned long long)p, __builtin_return_address(0)); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | if (size == 0) |
| 192 | return NULL; |
| 193 | |
| 194 | /* |
| 195 | * Is it already mapped? Perhaps overlapped by a previous |
| 196 | * BAT mapping. If the whole area is mapped then we're done, |
| 197 | * otherwise remap it since we want to keep the virt addrs for |
| 198 | * each request contiguous. |
| 199 | * |
| 200 | * We make the assumption here that if the bottom and top |
| 201 | * of the range we want are mapped then it's mapped to the |
| 202 | * same virt address (and this is contiguous). |
| 203 | * -- Cort |
| 204 | */ |
| 205 | if ((v = p_mapped_by_bats(p)) /*&& p_mapped_by_bats(p+size-1)*/ ) |
| 206 | goto out; |
| 207 | |
| 208 | if ((v = p_mapped_by_tlbcam(p))) |
| 209 | goto out; |
| 210 | |
| 211 | if (mem_init_done) { |
| 212 | struct vm_struct *area; |
| 213 | area = get_vm_area(size, VM_IOREMAP); |
| 214 | if (area == 0) |
| 215 | return NULL; |
| 216 | v = (unsigned long) area->addr; |
| 217 | } else { |
| 218 | v = (ioremap_bot -= size); |
| 219 | } |
| 220 | |
| 221 | if ((flags & _PAGE_PRESENT) == 0) |
| 222 | flags |= _PAGE_KERNEL; |
| 223 | if (flags & _PAGE_NO_CACHE) |
| 224 | flags |= _PAGE_GUARDED; |
| 225 | |
| 226 | /* |
| 227 | * Should check if it is a candidate for a BAT mapping |
| 228 | */ |
| 229 | |
| 230 | err = 0; |
| 231 | for (i = 0; i < size && err == 0; i += PAGE_SIZE) |
| 232 | err = map_page(v+i, p+i, flags); |
| 233 | if (err) { |
| 234 | if (mem_init_done) |
| 235 | vunmap((void *)v); |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | out: |
| 240 | return (void __iomem *) (v + ((unsigned long)addr & ~PAGE_MASK)); |
| 241 | } |
Olaf Hering | 920573b | 2006-03-14 21:21:11 +0100 | [diff] [blame] | 242 | EXPORT_SYMBOL(__ioremap); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 243 | |
| 244 | void iounmap(volatile void __iomem *addr) |
| 245 | { |
| 246 | /* |
| 247 | * If mapped by BATs then there is nothing to do. |
| 248 | * Calling vfree() generates a benign warning. |
| 249 | */ |
| 250 | if (v_mapped_by_bats((unsigned long)addr)) return; |
| 251 | |
| 252 | if (addr > high_memory && (unsigned long) addr < ioremap_bot) |
| 253 | vunmap((void *) (PAGE_MASK & (unsigned long)addr)); |
| 254 | } |
Olaf Hering | 920573b | 2006-03-14 21:21:11 +0100 | [diff] [blame] | 255 | EXPORT_SYMBOL(iounmap); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 256 | |
Benjamin Herrenschmidt | 68a6435 | 2006-11-13 09:27:39 +1100 | [diff] [blame] | 257 | int map_page(unsigned long va, phys_addr_t pa, int flags) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 258 | { |
| 259 | pmd_t *pd; |
| 260 | pte_t *pg; |
| 261 | int err = -ENOMEM; |
| 262 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 263 | /* Use upper 10 bits of VA to index the first level map */ |
David Gibson | d1953c8 | 2007-05-08 12:46:49 +1000 | [diff] [blame] | 264 | pd = pmd_offset(pud_offset(pgd_offset_k(va), va), va); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 265 | /* Use middle 10 bits of VA to index the second-level map */ |
Paul Mackerras | e2f2e58 | 2005-10-31 14:40:03 +1100 | [diff] [blame] | 266 | pg = pte_alloc_kernel(pd, va); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 267 | if (pg != 0) { |
| 268 | err = 0; |
Benjamin Herrenschmidt | 3be4e69 | 2007-04-12 15:30:21 +1000 | [diff] [blame] | 269 | /* The PTE should never be already set nor present in the |
| 270 | * hash table |
| 271 | */ |
| 272 | BUG_ON(pte_val(*pg) & (_PAGE_PRESENT | _PAGE_HASHPTE)); |
| 273 | set_pte_at(&init_mm, va, pg, pfn_pte(pa >> PAGE_SHIFT, |
| 274 | __pgprot(flags))); |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 275 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 276 | return err; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Map in all of physical memory starting at KERNELBASE. |
| 281 | */ |
| 282 | void __init mapin_ram(void) |
| 283 | { |
| 284 | unsigned long v, p, s, f; |
Benjamin Herrenschmidt | ee4f2ea | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 285 | int ktext; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 286 | |
| 287 | s = mmu_mapin_ram(); |
| 288 | v = KERNELBASE + s; |
| 289 | p = PPC_MEMSTART + s; |
| 290 | for (; s < total_lowmem; s += PAGE_SIZE) { |
Benjamin Herrenschmidt | ee4f2ea | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 291 | ktext = ((char *) v >= _stext && (char *) v < etext); |
| 292 | f = ktext ?_PAGE_RAM_TEXT : _PAGE_RAM; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 293 | map_page(v, p, f); |
Benjamin Herrenschmidt | ee4f2ea | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 294 | #ifdef CONFIG_PPC_STD_MMU_32 |
| 295 | if (ktext) |
| 296 | hash_preload(&init_mm, v, 0, 0x300); |
| 297 | #endif |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 298 | v += PAGE_SIZE; |
| 299 | p += PAGE_SIZE; |
| 300 | } |
| 301 | } |
| 302 | |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 303 | /* Scan the real Linux page tables and return a PTE pointer for |
| 304 | * a virtual address in a context. |
| 305 | * Returns true (1) if PTE was found, zero otherwise. The pointer to |
| 306 | * the PTE pointer is unmodified if PTE is not found. |
| 307 | */ |
| 308 | int |
Eugene Surovegin | bab70a4 | 2006-03-28 10:13:12 -0800 | [diff] [blame] | 309 | get_pteptr(struct mm_struct *mm, unsigned long addr, pte_t **ptep, pmd_t **pmdp) |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 310 | { |
| 311 | pgd_t *pgd; |
David Gibson | d1953c8 | 2007-05-08 12:46:49 +1000 | [diff] [blame] | 312 | pud_t *pud; |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 313 | pmd_t *pmd; |
| 314 | pte_t *pte; |
| 315 | int retval = 0; |
| 316 | |
| 317 | pgd = pgd_offset(mm, addr & PAGE_MASK); |
| 318 | if (pgd) { |
David Gibson | d1953c8 | 2007-05-08 12:46:49 +1000 | [diff] [blame] | 319 | pud = pud_offset(pgd, addr & PAGE_MASK); |
| 320 | if (pud && pud_present(*pud)) { |
| 321 | pmd = pmd_offset(pud, addr & PAGE_MASK); |
| 322 | if (pmd_present(*pmd)) { |
| 323 | pte = pte_offset_map(pmd, addr & PAGE_MASK); |
| 324 | if (pte) { |
| 325 | retval = 1; |
| 326 | *ptep = pte; |
| 327 | if (pmdp) |
| 328 | *pmdp = pmd; |
| 329 | /* XXX caller needs to do pte_unmap, yuck */ |
| 330 | } |
| 331 | } |
| 332 | } |
Paul Mackerras | 14cf11a | 2005-09-26 16:04:21 +1000 | [diff] [blame] | 333 | } |
| 334 | return(retval); |
| 335 | } |
| 336 | |
Benjamin Herrenschmidt | 88df6e9 | 2007-04-12 15:30:22 +1000 | [diff] [blame] | 337 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 338 | |
| 339 | static int __change_page_attr(struct page *page, pgprot_t prot) |
| 340 | { |
| 341 | pte_t *kpte; |
| 342 | pmd_t *kpmd; |
| 343 | unsigned long address; |
| 344 | |
| 345 | BUG_ON(PageHighMem(page)); |
| 346 | address = (unsigned long)page_address(page); |
| 347 | |
| 348 | if (v_mapped_by_bats(address) || v_mapped_by_tlbcam(address)) |
| 349 | return 0; |
| 350 | if (!get_pteptr(&init_mm, address, &kpte, &kpmd)) |
| 351 | return -EINVAL; |
| 352 | set_pte_at(&init_mm, address, kpte, mk_pte(page, prot)); |
| 353 | wmb(); |
| 354 | flush_HPTE(0, address, pmd_val(*kpmd)); |
| 355 | pte_unmap(kpte); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Change the page attributes of an page in the linear mapping. |
| 362 | * |
| 363 | * THIS CONFLICTS WITH BAT MAPPINGS, DEBUG USE ONLY |
| 364 | */ |
| 365 | static int change_page_attr(struct page *page, int numpages, pgprot_t prot) |
| 366 | { |
| 367 | int i, err = 0; |
| 368 | unsigned long flags; |
| 369 | |
| 370 | local_irq_save(flags); |
| 371 | for (i = 0; i < numpages; i++, page++) { |
| 372 | err = __change_page_attr(page, prot); |
| 373 | if (err) |
| 374 | break; |
| 375 | } |
| 376 | local_irq_restore(flags); |
| 377 | return err; |
| 378 | } |
| 379 | |
| 380 | |
| 381 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 382 | { |
| 383 | if (PageHighMem(page)) |
| 384 | return; |
| 385 | |
| 386 | change_page_attr(page, numpages, enable ? PAGE_KERNEL : __pgprot(0)); |
| 387 | } |
| 388 | #endif /* CONFIG_DEBUG_PAGEALLOC */ |