1 /* 2 * This file contains ioremap and related functions for 64-bit machines. 3 * 4 * Derived from arch/ppc64/mm/init.c 5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) 6 * 7 * Modifications by Paul Mackerras (PowerMac) (paulus@samba.org) 8 * and Cort Dougan (PReP) (cort@cs.nmt.edu) 9 * Copyright (C) 1996 Paul Mackerras 10 * 11 * Derived from "arch/i386/mm/init.c" 12 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds 13 * 14 * Dave Engebretsen <engebret@us.ibm.com> 15 * Rework for PPC64 port. 16 * 17 * This program is free software; you can redistribute it and/or 18 * modify it under the terms of the GNU General Public License 19 * as published by the Free Software Foundation; either version 20 * 2 of the License, or (at your option) any later version. 21 * 22 */ 23 24 #include <linux/signal.h> 25 #include <linux/sched.h> 26 #include <linux/kernel.h> 27 #include <linux/errno.h> 28 #include <linux/string.h> 29 #include <linux/export.h> 30 #include <linux/types.h> 31 #include <linux/mman.h> 32 #include <linux/mm.h> 33 #include <linux/swap.h> 34 #include <linux/stddef.h> 35 #include <linux/vmalloc.h> 36 #include <linux/memblock.h> 37 #include <linux/slab.h> 38 #include <linux/hugetlb.h> 39 40 #include <asm/pgalloc.h> 41 #include <asm/page.h> 42 #include <asm/prom.h> 43 #include <asm/io.h> 44 #include <asm/mmu_context.h> 45 #include <asm/pgtable.h> 46 #include <asm/mmu.h> 47 #include <asm/smp.h> 48 #include <asm/machdep.h> 49 #include <asm/tlb.h> 50 #include <asm/trace.h> 51 #include <asm/processor.h> 52 #include <asm/cputable.h> 53 #include <asm/sections.h> 54 #include <asm/firmware.h> 55 #include <asm/dma.h> 56 #include <asm/powernv.h> 57 58 #include "mmu_decl.h" 59 60 #ifdef CONFIG_PPC_BOOK3S_64 61 #if TASK_SIZE_USER64 > (1UL << (ESID_BITS + SID_SHIFT)) 62 #error TASK_SIZE_USER64 exceeds user VSID range 63 #endif 64 #endif 65 66 #ifdef CONFIG_PPC_BOOK3S_64 67 /* 68 * partition table and process table for ISA 3.0 69 */ 70 struct prtb_entry *process_tb; 71 struct patb_entry *partition_tb; 72 /* 73 * page table size 74 */ 75 unsigned long __pte_index_size; 76 EXPORT_SYMBOL(__pte_index_size); 77 unsigned long __pmd_index_size; 78 EXPORT_SYMBOL(__pmd_index_size); 79 unsigned long __pud_index_size; 80 EXPORT_SYMBOL(__pud_index_size); 81 unsigned long __pgd_index_size; 82 EXPORT_SYMBOL(__pgd_index_size); 83 unsigned long __pmd_cache_index; 84 EXPORT_SYMBOL(__pmd_cache_index); 85 unsigned long __pud_cache_index; 86 EXPORT_SYMBOL(__pud_cache_index); 87 unsigned long __pte_table_size; 88 EXPORT_SYMBOL(__pte_table_size); 89 unsigned long __pmd_table_size; 90 EXPORT_SYMBOL(__pmd_table_size); 91 unsigned long __pud_table_size; 92 EXPORT_SYMBOL(__pud_table_size); 93 unsigned long __pgd_table_size; 94 EXPORT_SYMBOL(__pgd_table_size); 95 unsigned long __pmd_val_bits; 96 EXPORT_SYMBOL(__pmd_val_bits); 97 unsigned long __pud_val_bits; 98 EXPORT_SYMBOL(__pud_val_bits); 99 unsigned long __pgd_val_bits; 100 EXPORT_SYMBOL(__pgd_val_bits); 101 unsigned long __kernel_virt_start; 102 EXPORT_SYMBOL(__kernel_virt_start); 103 unsigned long __kernel_virt_size; 104 EXPORT_SYMBOL(__kernel_virt_size); 105 unsigned long __vmalloc_start; 106 EXPORT_SYMBOL(__vmalloc_start); 107 unsigned long __vmalloc_end; 108 EXPORT_SYMBOL(__vmalloc_end); 109 unsigned long __kernel_io_start; 110 EXPORT_SYMBOL(__kernel_io_start); 111 struct page *vmemmap; 112 EXPORT_SYMBOL(vmemmap); 113 unsigned long __pte_frag_nr; 114 EXPORT_SYMBOL(__pte_frag_nr); 115 unsigned long __pte_frag_size_shift; 116 EXPORT_SYMBOL(__pte_frag_size_shift); 117 unsigned long ioremap_bot; 118 #else /* !CONFIG_PPC_BOOK3S_64 */ 119 unsigned long ioremap_bot = IOREMAP_BASE; 120 #endif 121 122 /** 123 * __ioremap_at - Low level function to establish the page tables 124 * for an IO mapping 125 */ 126 void __iomem * __ioremap_at(phys_addr_t pa, void *ea, unsigned long size, 127 unsigned long flags) 128 { 129 unsigned long i; 130 131 /* Make sure we have the base flags */ 132 if ((flags & _PAGE_PRESENT) == 0) 133 flags |= pgprot_val(PAGE_KERNEL); 134 135 /* We don't support the 4K PFN hack with ioremap */ 136 if (flags & H_PAGE_4K_PFN) 137 return NULL; 138 139 WARN_ON(pa & ~PAGE_MASK); 140 WARN_ON(((unsigned long)ea) & ~PAGE_MASK); 141 WARN_ON(size & ~PAGE_MASK); 142 143 for (i = 0; i < size; i += PAGE_SIZE) 144 if (map_kernel_page((unsigned long)ea+i, pa+i, flags)) 145 return NULL; 146 147 return (void __iomem *)ea; 148 } 149 150 /** 151 * __iounmap_from - Low level function to tear down the page tables 152 * for an IO mapping. This is used for mappings that 153 * are manipulated manually, like partial unmapping of 154 * PCI IOs or ISA space. 155 */ 156 void __iounmap_at(void *ea, unsigned long size) 157 { 158 WARN_ON(((unsigned long)ea) & ~PAGE_MASK); 159 WARN_ON(size & ~PAGE_MASK); 160 161 unmap_kernel_range((unsigned long)ea, size); 162 } 163 164 void __iomem * __ioremap_caller(phys_addr_t addr, unsigned long size, 165 unsigned long flags, void *caller) 166 { 167 phys_addr_t paligned; 168 void __iomem *ret; 169 170 /* 171 * Choose an address to map it to. 172 * Once the imalloc system is running, we use it. 173 * Before that, we map using addresses going 174 * up from ioremap_bot. imalloc will use 175 * the addresses from ioremap_bot through 176 * IMALLOC_END 177 * 178 */ 179 paligned = addr & PAGE_MASK; 180 size = PAGE_ALIGN(addr + size) - paligned; 181 182 if ((size == 0) || (paligned == 0)) 183 return NULL; 184 185 if (slab_is_available()) { 186 struct vm_struct *area; 187 188 area = __get_vm_area_caller(size, VM_IOREMAP, 189 ioremap_bot, IOREMAP_END, 190 caller); 191 if (area == NULL) 192 return NULL; 193 194 area->phys_addr = paligned; 195 ret = __ioremap_at(paligned, area->addr, size, flags); 196 if (!ret) 197 vunmap(area->addr); 198 } else { 199 ret = __ioremap_at(paligned, (void *)ioremap_bot, size, flags); 200 if (ret) 201 ioremap_bot += size; 202 } 203 204 if (ret) 205 ret += addr & ~PAGE_MASK; 206 return ret; 207 } 208 209 void __iomem * __ioremap(phys_addr_t addr, unsigned long size, 210 unsigned long flags) 211 { 212 return __ioremap_caller(addr, size, flags, __builtin_return_address(0)); 213 } 214 215 void __iomem * ioremap(phys_addr_t addr, unsigned long size) 216 { 217 unsigned long flags = pgprot_val(pgprot_noncached(__pgprot(0))); 218 void *caller = __builtin_return_address(0); 219 220 if (ppc_md.ioremap) 221 return ppc_md.ioremap(addr, size, flags, caller); 222 return __ioremap_caller(addr, size, flags, caller); 223 } 224 225 void __iomem * ioremap_wc(phys_addr_t addr, unsigned long size) 226 { 227 unsigned long flags = pgprot_val(pgprot_noncached_wc(__pgprot(0))); 228 void *caller = __builtin_return_address(0); 229 230 if (ppc_md.ioremap) 231 return ppc_md.ioremap(addr, size, flags, caller); 232 return __ioremap_caller(addr, size, flags, caller); 233 } 234 235 void __iomem * ioremap_prot(phys_addr_t addr, unsigned long size, 236 unsigned long flags) 237 { 238 void *caller = __builtin_return_address(0); 239 240 /* writeable implies dirty for kernel addresses */ 241 if (flags & _PAGE_WRITE) 242 flags |= _PAGE_DIRTY; 243 244 /* we don't want to let _PAGE_EXEC leak out */ 245 flags &= ~_PAGE_EXEC; 246 /* 247 * Force kernel mapping. 248 */ 249 flags &= ~_PAGE_USER; 250 flags |= _PAGE_PRIVILEGED; 251 252 if (ppc_md.ioremap) 253 return ppc_md.ioremap(addr, size, flags, caller); 254 return __ioremap_caller(addr, size, flags, caller); 255 } 256 257 258 /* 259 * Unmap an IO region and remove it from imalloc'd list. 260 * Access to IO memory should be serialized by driver. 261 */ 262 void __iounmap(volatile void __iomem *token) 263 { 264 void *addr; 265 266 if (!slab_is_available()) 267 return; 268 269 addr = (void *) ((unsigned long __force) 270 PCI_FIX_ADDR(token) & PAGE_MASK); 271 if ((unsigned long)addr < ioremap_bot) { 272 printk(KERN_WARNING "Attempt to iounmap early bolted mapping" 273 " at 0x%p\n", addr); 274 return; 275 } 276 vunmap(addr); 277 } 278 279 void iounmap(volatile void __iomem *token) 280 { 281 if (ppc_md.iounmap) 282 ppc_md.iounmap(token); 283 else 284 __iounmap(token); 285 } 286 287 EXPORT_SYMBOL(ioremap); 288 EXPORT_SYMBOL(ioremap_wc); 289 EXPORT_SYMBOL(ioremap_prot); 290 EXPORT_SYMBOL(__ioremap); 291 EXPORT_SYMBOL(__ioremap_at); 292 EXPORT_SYMBOL(iounmap); 293 EXPORT_SYMBOL(__iounmap); 294 EXPORT_SYMBOL(__iounmap_at); 295 296 #ifndef __PAGETABLE_PUD_FOLDED 297 /* 4 level page table */ 298 struct page *pgd_page(pgd_t pgd) 299 { 300 if (pgd_huge(pgd)) 301 return pte_page(pgd_pte(pgd)); 302 return virt_to_page(pgd_page_vaddr(pgd)); 303 } 304 #endif 305 306 struct page *pud_page(pud_t pud) 307 { 308 if (pud_huge(pud)) 309 return pte_page(pud_pte(pud)); 310 return virt_to_page(pud_page_vaddr(pud)); 311 } 312 313 /* 314 * For hugepage we have pfn in the pmd, we use PTE_RPN_SHIFT bits for flags 315 * For PTE page, we have a PTE_FRAG_SIZE (4K) aligned virtual address. 316 */ 317 struct page *pmd_page(pmd_t pmd) 318 { 319 if (pmd_trans_huge(pmd) || pmd_huge(pmd) || pmd_devmap(pmd)) 320 return pte_page(pmd_pte(pmd)); 321 return virt_to_page(pmd_page_vaddr(pmd)); 322 } 323 324 #ifdef CONFIG_PPC_64K_PAGES 325 static pte_t *get_from_cache(struct mm_struct *mm) 326 { 327 void *pte_frag, *ret; 328 329 spin_lock(&mm->page_table_lock); 330 ret = mm->context.pte_frag; 331 if (ret) { 332 pte_frag = ret + PTE_FRAG_SIZE; 333 /* 334 * If we have taken up all the fragments mark PTE page NULL 335 */ 336 if (((unsigned long)pte_frag & ~PAGE_MASK) == 0) 337 pte_frag = NULL; 338 mm->context.pte_frag = pte_frag; 339 } 340 spin_unlock(&mm->page_table_lock); 341 return (pte_t *)ret; 342 } 343 344 static pte_t *__alloc_for_cache(struct mm_struct *mm, int kernel) 345 { 346 void *ret = NULL; 347 struct page *page; 348 349 if (!kernel) { 350 page = alloc_page(PGALLOC_GFP | __GFP_ACCOUNT); 351 if (!page) 352 return NULL; 353 if (!pgtable_page_ctor(page)) { 354 __free_page(page); 355 return NULL; 356 } 357 } else { 358 page = alloc_page(PGALLOC_GFP); 359 if (!page) 360 return NULL; 361 } 362 363 ret = page_address(page); 364 spin_lock(&mm->page_table_lock); 365 /* 366 * If we find pgtable_page set, we return 367 * the allocated page with single fragement 368 * count. 369 */ 370 if (likely(!mm->context.pte_frag)) { 371 set_page_count(page, PTE_FRAG_NR); 372 mm->context.pte_frag = ret + PTE_FRAG_SIZE; 373 } 374 spin_unlock(&mm->page_table_lock); 375 376 return (pte_t *)ret; 377 } 378 379 pte_t *pte_fragment_alloc(struct mm_struct *mm, unsigned long vmaddr, int kernel) 380 { 381 pte_t *pte; 382 383 pte = get_from_cache(mm); 384 if (pte) 385 return pte; 386 387 return __alloc_for_cache(mm, kernel); 388 } 389 #endif /* CONFIG_PPC_64K_PAGES */ 390 391 void pte_fragment_free(unsigned long *table, int kernel) 392 { 393 struct page *page = virt_to_page(table); 394 if (put_page_testzero(page)) { 395 if (!kernel) 396 pgtable_page_dtor(page); 397 free_unref_page(page); 398 } 399 } 400 401 #ifdef CONFIG_SMP 402 void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int shift) 403 { 404 unsigned long pgf = (unsigned long)table; 405 406 BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE); 407 pgf |= shift; 408 tlb_remove_table(tlb, (void *)pgf); 409 } 410 411 void __tlb_remove_table(void *_table) 412 { 413 void *table = (void *)((unsigned long)_table & ~MAX_PGTABLE_INDEX_SIZE); 414 unsigned shift = (unsigned long)_table & MAX_PGTABLE_INDEX_SIZE; 415 416 if (!shift) 417 /* PTE page needs special handling */ 418 pte_fragment_free(table, 0); 419 else { 420 BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE); 421 kmem_cache_free(PGT_CACHE(shift), table); 422 } 423 } 424 #else 425 void pgtable_free_tlb(struct mmu_gather *tlb, void *table, int shift) 426 { 427 if (!shift) { 428 /* PTE page needs special handling */ 429 pte_fragment_free(table, 0); 430 } else { 431 BUG_ON(shift > MAX_PGTABLE_INDEX_SIZE); 432 kmem_cache_free(PGT_CACHE(shift), table); 433 } 434 } 435 #endif 436 437 #ifdef CONFIG_PPC_BOOK3S_64 438 void __init mmu_partition_table_init(void) 439 { 440 unsigned long patb_size = 1UL << PATB_SIZE_SHIFT; 441 unsigned long ptcr; 442 443 BUILD_BUG_ON_MSG((PATB_SIZE_SHIFT > 36), "Partition table size too large."); 444 partition_tb = __va(memblock_alloc_base(patb_size, patb_size, 445 MEMBLOCK_ALLOC_ANYWHERE)); 446 447 /* Initialize the Partition Table with no entries */ 448 memset((void *)partition_tb, 0, patb_size); 449 450 /* 451 * update partition table control register, 452 * 64 K size. 453 */ 454 ptcr = __pa(partition_tb) | (PATB_SIZE_SHIFT - 12); 455 mtspr(SPRN_PTCR, ptcr); 456 powernv_set_nmmu_ptcr(ptcr); 457 } 458 459 void mmu_partition_table_set_entry(unsigned int lpid, unsigned long dw0, 460 unsigned long dw1) 461 { 462 unsigned long old = be64_to_cpu(partition_tb[lpid].patb0); 463 464 partition_tb[lpid].patb0 = cpu_to_be64(dw0); 465 partition_tb[lpid].patb1 = cpu_to_be64(dw1); 466 467 /* 468 * Global flush of TLBs and partition table caches for this lpid. 469 * The type of flush (hash or radix) depends on what the previous 470 * use of this partition ID was, not the new use. 471 */ 472 asm volatile("ptesync" : : : "memory"); 473 if (old & PATB_HR) { 474 asm volatile(PPC_TLBIE_5(%0,%1,2,0,1) : : 475 "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid)); 476 asm volatile(PPC_TLBIE_5(%0,%1,2,1,1) : : 477 "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid)); 478 trace_tlbie(lpid, 0, TLBIEL_INVAL_SET_LPID, lpid, 2, 0, 1); 479 } else { 480 asm volatile(PPC_TLBIE_5(%0,%1,2,0,0) : : 481 "r" (TLBIEL_INVAL_SET_LPID), "r" (lpid)); 482 trace_tlbie(lpid, 0, TLBIEL_INVAL_SET_LPID, lpid, 2, 0, 0); 483 } 484 asm volatile("eieio; tlbsync; ptesync" : : : "memory"); 485 } 486 EXPORT_SYMBOL_GPL(mmu_partition_table_set_entry); 487 #endif /* CONFIG_PPC_BOOK3S_64 */ 488 489 #ifdef CONFIG_STRICT_KERNEL_RWX 490 void mark_rodata_ro(void) 491 { 492 if (!mmu_has_feature(MMU_FTR_KERNEL_RO)) { 493 pr_warn("Warning: Unable to mark rodata read only on this CPU.\n"); 494 return; 495 } 496 497 if (radix_enabled()) 498 radix__mark_rodata_ro(); 499 else 500 hash__mark_rodata_ro(); 501 } 502 503 void mark_initmem_nx(void) 504 { 505 if (radix_enabled()) 506 radix__mark_initmem_nx(); 507 else 508 hash__mark_initmem_nx(); 509 } 510 #endif 511