at v3.8 1076 lines 32 kB view raw
1/* 2 * Copyright (C) 1995 Linus Torvalds 3 * Copyright 2010 Tilera Corporation. All Rights Reserved. 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation, version 2. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or 12 * NON INFRINGEMENT. See the GNU General Public License for 13 * more details. 14 */ 15 16#include <linux/module.h> 17#include <linux/signal.h> 18#include <linux/sched.h> 19#include <linux/kernel.h> 20#include <linux/errno.h> 21#include <linux/string.h> 22#include <linux/types.h> 23#include <linux/ptrace.h> 24#include <linux/mman.h> 25#include <linux/mm.h> 26#include <linux/hugetlb.h> 27#include <linux/swap.h> 28#include <linux/smp.h> 29#include <linux/init.h> 30#include <linux/highmem.h> 31#include <linux/pagemap.h> 32#include <linux/poison.h> 33#include <linux/bootmem.h> 34#include <linux/slab.h> 35#include <linux/proc_fs.h> 36#include <linux/efi.h> 37#include <linux/memory_hotplug.h> 38#include <linux/uaccess.h> 39#include <asm/mmu_context.h> 40#include <asm/processor.h> 41#include <asm/pgtable.h> 42#include <asm/pgalloc.h> 43#include <asm/dma.h> 44#include <asm/fixmap.h> 45#include <asm/tlb.h> 46#include <asm/tlbflush.h> 47#include <asm/sections.h> 48#include <asm/setup.h> 49#include <asm/homecache.h> 50#include <hv/hypervisor.h> 51#include <arch/chip.h> 52 53#include "migrate.h" 54 55#define clear_pgd(pmdptr) (*(pmdptr) = hv_pte(0)) 56 57#ifndef __tilegx__ 58unsigned long VMALLOC_RESERVE = CONFIG_VMALLOC_RESERVE; 59EXPORT_SYMBOL(VMALLOC_RESERVE); 60#endif 61 62/* Create an L2 page table */ 63static pte_t * __init alloc_pte(void) 64{ 65 return __alloc_bootmem(L2_KERNEL_PGTABLE_SIZE, HV_PAGE_TABLE_ALIGN, 0); 66} 67 68/* 69 * L2 page tables per controller. We allocate these all at once from 70 * the bootmem allocator and store them here. This saves on kernel L2 71 * page table memory, compared to allocating a full 64K page per L2 72 * page table, and also means that in cases where we use huge pages, 73 * we are guaranteed to later be able to shatter those huge pages and 74 * switch to using these page tables instead, without requiring 75 * further allocation. Each l2_ptes[] entry points to the first page 76 * table for the first hugepage-size piece of memory on the 77 * controller; other page tables are just indexed directly, i.e. the 78 * L2 page tables are contiguous in memory for each controller. 79 */ 80static pte_t *l2_ptes[MAX_NUMNODES]; 81static int num_l2_ptes[MAX_NUMNODES]; 82 83static void init_prealloc_ptes(int node, int pages) 84{ 85 BUG_ON(pages & (PTRS_PER_PTE - 1)); 86 if (pages) { 87 num_l2_ptes[node] = pages; 88 l2_ptes[node] = __alloc_bootmem(pages * sizeof(pte_t), 89 HV_PAGE_TABLE_ALIGN, 0); 90 } 91} 92 93pte_t *get_prealloc_pte(unsigned long pfn) 94{ 95 int node = pfn_to_nid(pfn); 96 pfn &= ~(-1UL << (NR_PA_HIGHBIT_SHIFT - PAGE_SHIFT)); 97 BUG_ON(node >= MAX_NUMNODES); 98 BUG_ON(pfn >= num_l2_ptes[node]); 99 return &l2_ptes[node][pfn]; 100} 101 102/* 103 * What caching do we expect pages from the heap to have when 104 * they are allocated during bootup? (Once we've installed the 105 * "real" swapper_pg_dir.) 106 */ 107static int initial_heap_home(void) 108{ 109#if CHIP_HAS_CBOX_HOME_MAP() 110 if (hash_default) 111 return PAGE_HOME_HASH; 112#endif 113 return smp_processor_id(); 114} 115 116/* 117 * Place a pointer to an L2 page table in a middle page 118 * directory entry. 119 */ 120static void __init assign_pte(pmd_t *pmd, pte_t *page_table) 121{ 122 phys_addr_t pa = __pa(page_table); 123 unsigned long l2_ptfn = pa >> HV_LOG2_PAGE_TABLE_ALIGN; 124 pte_t pteval = hv_pte_set_ptfn(__pgprot(_PAGE_TABLE), l2_ptfn); 125 BUG_ON((pa & (HV_PAGE_TABLE_ALIGN-1)) != 0); 126 pteval = pte_set_home(pteval, initial_heap_home()); 127 *(pte_t *)pmd = pteval; 128 if (page_table != (pte_t *)pmd_page_vaddr(*pmd)) 129 BUG(); 130} 131 132#ifdef __tilegx__ 133 134static inline pmd_t *alloc_pmd(void) 135{ 136 return __alloc_bootmem(L1_KERNEL_PGTABLE_SIZE, HV_PAGE_TABLE_ALIGN, 0); 137} 138 139static inline void assign_pmd(pud_t *pud, pmd_t *pmd) 140{ 141 assign_pte((pmd_t *)pud, (pte_t *)pmd); 142} 143 144#endif /* __tilegx__ */ 145 146/* Replace the given pmd with a full PTE table. */ 147void __init shatter_pmd(pmd_t *pmd) 148{ 149 pte_t *pte = get_prealloc_pte(pte_pfn(*(pte_t *)pmd)); 150 assign_pte(pmd, pte); 151} 152 153#ifdef __tilegx__ 154static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va) 155{ 156 pud_t *pud = pud_offset(&pgtables[pgd_index(va)], va); 157 if (pud_none(*pud)) 158 assign_pmd(pud, alloc_pmd()); 159 return pmd_offset(pud, va); 160} 161#else 162static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va) 163{ 164 return pmd_offset(pud_offset(&pgtables[pgd_index(va)], va), va); 165} 166#endif 167 168/* 169 * This function initializes a certain range of kernel virtual memory 170 * with new bootmem page tables, everywhere page tables are missing in 171 * the given range. 172 */ 173 174/* 175 * NOTE: The pagetables are allocated contiguous on the physical space 176 * so we can cache the place of the first one and move around without 177 * checking the pgd every time. 178 */ 179static void __init page_table_range_init(unsigned long start, 180 unsigned long end, pgd_t *pgd) 181{ 182 unsigned long vaddr; 183 start = round_down(start, PMD_SIZE); 184 end = round_up(end, PMD_SIZE); 185 for (vaddr = start; vaddr < end; vaddr += PMD_SIZE) { 186 pmd_t *pmd = get_pmd(pgd, vaddr); 187 if (pmd_none(*pmd)) 188 assign_pte(pmd, alloc_pte()); 189 } 190} 191 192 193#if CHIP_HAS_CBOX_HOME_MAP() 194 195static int __initdata ktext_hash = 1; /* .text pages */ 196static int __initdata kdata_hash = 1; /* .data and .bss pages */ 197int __write_once hash_default = 1; /* kernel allocator pages */ 198EXPORT_SYMBOL(hash_default); 199int __write_once kstack_hash = 1; /* if no homecaching, use h4h */ 200#endif /* CHIP_HAS_CBOX_HOME_MAP */ 201 202/* 203 * CPUs to use to for striping the pages of kernel data. If hash-for-home 204 * is available, this is only relevant if kcache_hash sets up the 205 * .data and .bss to be page-homed, and we don't want the default mode 206 * of using the full set of kernel cpus for the striping. 207 */ 208static __initdata struct cpumask kdata_mask; 209static __initdata int kdata_arg_seen; 210 211int __write_once kdata_huge; /* if no homecaching, small pages */ 212 213 214/* Combine a generic pgprot_t with cache home to get a cache-aware pgprot. */ 215static pgprot_t __init construct_pgprot(pgprot_t prot, int home) 216{ 217 prot = pte_set_home(prot, home); 218#if CHIP_HAS_CBOX_HOME_MAP() 219 if (home == PAGE_HOME_IMMUTABLE) { 220 if (ktext_hash) 221 prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_HASH_L3); 222 else 223 prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_NO_L3); 224 } 225#endif 226 return prot; 227} 228 229/* 230 * For a given kernel data VA, how should it be cached? 231 * We return the complete pgprot_t with caching bits set. 232 */ 233static pgprot_t __init init_pgprot(ulong address) 234{ 235 int cpu; 236 unsigned long page; 237 enum { CODE_DELTA = MEM_SV_INTRPT - PAGE_OFFSET }; 238 239#if CHIP_HAS_CBOX_HOME_MAP() 240 /* For kdata=huge, everything is just hash-for-home. */ 241 if (kdata_huge) 242 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH); 243#endif 244 245 /* We map the aliased pages of permanent text inaccessible. */ 246 if (address < (ulong) _sinittext - CODE_DELTA) 247 return PAGE_NONE; 248 249 /* 250 * We map read-only data non-coherent for performance. We could 251 * use neighborhood caching on TILE64, but it's not clear it's a win. 252 */ 253 if ((address >= (ulong) __start_rodata && 254 address < (ulong) __end_rodata) || 255 address == (ulong) empty_zero_page) { 256 return construct_pgprot(PAGE_KERNEL_RO, PAGE_HOME_IMMUTABLE); 257 } 258 259#ifndef __tilegx__ 260#if !ATOMIC_LOCKS_FOUND_VIA_TABLE() 261 /* Force the atomic_locks[] array page to be hash-for-home. */ 262 if (address == (ulong) atomic_locks) 263 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH); 264#endif 265#endif 266 267 /* 268 * Everything else that isn't data or bss is heap, so mark it 269 * with the initial heap home (hash-for-home, or this cpu). This 270 * includes any addresses after the loaded image and any address before 271 * _einitdata, since we already captured the case of text before 272 * _sinittext, and __pa(einittext) is approximately __pa(sinitdata). 273 * 274 * All the LOWMEM pages that we mark this way will get their 275 * struct page homecache properly marked later, in set_page_homes(). 276 * The HIGHMEM pages we leave with a default zero for their 277 * homes, but with a zero free_time we don't have to actually 278 * do a flush action the first time we use them, either. 279 */ 280 if (address >= (ulong) _end || address < (ulong) _einitdata) 281 return construct_pgprot(PAGE_KERNEL, initial_heap_home()); 282 283#if CHIP_HAS_CBOX_HOME_MAP() 284 /* Use hash-for-home if requested for data/bss. */ 285 if (kdata_hash) 286 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH); 287#endif 288 289 /* 290 * Make the w1data homed like heap to start with, to avoid 291 * making it part of the page-striped data area when we're just 292 * going to convert it to read-only soon anyway. 293 */ 294 if (address >= (ulong)__w1data_begin && address < (ulong)__w1data_end) 295 return construct_pgprot(PAGE_KERNEL, initial_heap_home()); 296 297 /* 298 * Otherwise we just hand out consecutive cpus. To avoid 299 * requiring this function to hold state, we just walk forward from 300 * _sdata by PAGE_SIZE, skipping the readonly and init data, to reach 301 * the requested address, while walking cpu home around kdata_mask. 302 * This is typically no more than a dozen or so iterations. 303 */ 304 page = (((ulong)__w1data_end) + PAGE_SIZE - 1) & PAGE_MASK; 305 BUG_ON(address < page || address >= (ulong)_end); 306 cpu = cpumask_first(&kdata_mask); 307 for (; page < address; page += PAGE_SIZE) { 308 if (page >= (ulong)&init_thread_union && 309 page < (ulong)&init_thread_union + THREAD_SIZE) 310 continue; 311 if (page == (ulong)empty_zero_page) 312 continue; 313#ifndef __tilegx__ 314#if !ATOMIC_LOCKS_FOUND_VIA_TABLE() 315 if (page == (ulong)atomic_locks) 316 continue; 317#endif 318#endif 319 cpu = cpumask_next(cpu, &kdata_mask); 320 if (cpu == NR_CPUS) 321 cpu = cpumask_first(&kdata_mask); 322 } 323 return construct_pgprot(PAGE_KERNEL, cpu); 324} 325 326/* 327 * This function sets up how we cache the kernel text. If we have 328 * hash-for-home support, normally that is used instead (see the 329 * kcache_hash boot flag for more information). But if we end up 330 * using a page-based caching technique, this option sets up the 331 * details of that. In addition, the "ktext=nocache" option may 332 * always be used to disable local caching of text pages, if desired. 333 */ 334 335static int __initdata ktext_arg_seen; 336static int __initdata ktext_small; 337static int __initdata ktext_local; 338static int __initdata ktext_all; 339static int __initdata ktext_nondataplane; 340static int __initdata ktext_nocache; 341static struct cpumask __initdata ktext_mask; 342 343static int __init setup_ktext(char *str) 344{ 345 if (str == NULL) 346 return -EINVAL; 347 348 /* If you have a leading "nocache", turn off ktext caching */ 349 if (strncmp(str, "nocache", 7) == 0) { 350 ktext_nocache = 1; 351 pr_info("ktext: disabling local caching of kernel text\n"); 352 str += 7; 353 if (*str == ',') 354 ++str; 355 if (*str == '\0') 356 return 0; 357 } 358 359 ktext_arg_seen = 1; 360 361 /* Default setting on Tile64: use a huge page */ 362 if (strcmp(str, "huge") == 0) 363 pr_info("ktext: using one huge locally cached page\n"); 364 365 /* Pay TLB cost but get no cache benefit: cache small pages locally */ 366 else if (strcmp(str, "local") == 0) { 367 ktext_small = 1; 368 ktext_local = 1; 369 pr_info("ktext: using small pages with local caching\n"); 370 } 371 372 /* Neighborhood cache ktext pages on all cpus. */ 373 else if (strcmp(str, "all") == 0) { 374 ktext_small = 1; 375 ktext_all = 1; 376 pr_info("ktext: using maximal caching neighborhood\n"); 377 } 378 379 380 /* Neighborhood ktext pages on specified mask */ 381 else if (cpulist_parse(str, &ktext_mask) == 0) { 382 char buf[NR_CPUS * 5]; 383 cpulist_scnprintf(buf, sizeof(buf), &ktext_mask); 384 if (cpumask_weight(&ktext_mask) > 1) { 385 ktext_small = 1; 386 pr_info("ktext: using caching neighborhood %s " 387 "with small pages\n", buf); 388 } else { 389 pr_info("ktext: caching on cpu %s with one huge page\n", 390 buf); 391 } 392 } 393 394 else if (*str) 395 return -EINVAL; 396 397 return 0; 398} 399 400early_param("ktext", setup_ktext); 401 402 403static inline pgprot_t ktext_set_nocache(pgprot_t prot) 404{ 405 if (!ktext_nocache) 406 prot = hv_pte_set_nc(prot); 407#if CHIP_HAS_NC_AND_NOALLOC_BITS() 408 else 409 prot = hv_pte_set_no_alloc_l2(prot); 410#endif 411 return prot; 412} 413 414/* Temporary page table we use for staging. */ 415static pgd_t pgtables[PTRS_PER_PGD] 416 __attribute__((aligned(HV_PAGE_TABLE_ALIGN))); 417 418/* 419 * This maps the physical memory to kernel virtual address space, a total 420 * of max_low_pfn pages, by creating page tables starting from address 421 * PAGE_OFFSET. 422 * 423 * This routine transitions us from using a set of compiled-in large 424 * pages to using some more precise caching, including removing access 425 * to code pages mapped at PAGE_OFFSET (executed only at MEM_SV_START) 426 * marking read-only data as locally cacheable, striping the remaining 427 * .data and .bss across all the available tiles, and removing access 428 * to pages above the top of RAM (thus ensuring a page fault from a bad 429 * virtual address rather than a hypervisor shoot down for accessing 430 * memory outside the assigned limits). 431 */ 432static void __init kernel_physical_mapping_init(pgd_t *pgd_base) 433{ 434 unsigned long long irqmask; 435 unsigned long address, pfn; 436 pmd_t *pmd; 437 pte_t *pte; 438 int pte_ofs; 439 const struct cpumask *my_cpu_mask = cpumask_of(smp_processor_id()); 440 struct cpumask kstripe_mask; 441 int rc, i; 442 443#if CHIP_HAS_CBOX_HOME_MAP() 444 if (ktext_arg_seen && ktext_hash) { 445 pr_warning("warning: \"ktext\" boot argument ignored" 446 " if \"kcache_hash\" sets up text hash-for-home\n"); 447 ktext_small = 0; 448 } 449 450 if (kdata_arg_seen && kdata_hash) { 451 pr_warning("warning: \"kdata\" boot argument ignored" 452 " if \"kcache_hash\" sets up data hash-for-home\n"); 453 } 454 455 if (kdata_huge && !hash_default) { 456 pr_warning("warning: disabling \"kdata=huge\"; requires" 457 " kcache_hash=all or =allbutstack\n"); 458 kdata_huge = 0; 459 } 460#endif 461 462 /* 463 * Set up a mask for cpus to use for kernel striping. 464 * This is normally all cpus, but minus dataplane cpus if any. 465 * If the dataplane covers the whole chip, we stripe over 466 * the whole chip too. 467 */ 468 cpumask_copy(&kstripe_mask, cpu_possible_mask); 469 if (!kdata_arg_seen) 470 kdata_mask = kstripe_mask; 471 472 /* Allocate and fill in L2 page tables */ 473 for (i = 0; i < MAX_NUMNODES; ++i) { 474#ifdef CONFIG_HIGHMEM 475 unsigned long end_pfn = node_lowmem_end_pfn[i]; 476#else 477 unsigned long end_pfn = node_end_pfn[i]; 478#endif 479 unsigned long end_huge_pfn = 0; 480 481 /* Pre-shatter the last huge page to allow per-cpu pages. */ 482 if (kdata_huge) 483 end_huge_pfn = end_pfn - (HPAGE_SIZE >> PAGE_SHIFT); 484 485 pfn = node_start_pfn[i]; 486 487 /* Allocate enough memory to hold L2 page tables for node. */ 488 init_prealloc_ptes(i, end_pfn - pfn); 489 490 address = (unsigned long) pfn_to_kaddr(pfn); 491 while (pfn < end_pfn) { 492 BUG_ON(address & (HPAGE_SIZE-1)); 493 pmd = get_pmd(pgtables, address); 494 pte = get_prealloc_pte(pfn); 495 if (pfn < end_huge_pfn) { 496 pgprot_t prot = init_pgprot(address); 497 *(pte_t *)pmd = pte_mkhuge(pfn_pte(pfn, prot)); 498 for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE; 499 pfn++, pte_ofs++, address += PAGE_SIZE) 500 pte[pte_ofs] = pfn_pte(pfn, prot); 501 } else { 502 if (kdata_huge) 503 printk(KERN_DEBUG "pre-shattered huge" 504 " page at %#lx\n", address); 505 for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE; 506 pfn++, pte_ofs++, address += PAGE_SIZE) { 507 pgprot_t prot = init_pgprot(address); 508 pte[pte_ofs] = pfn_pte(pfn, prot); 509 } 510 assign_pte(pmd, pte); 511 } 512 } 513 } 514 515 /* 516 * Set or check ktext_map now that we have cpu_possible_mask 517 * and kstripe_mask to work with. 518 */ 519 if (ktext_all) 520 cpumask_copy(&ktext_mask, cpu_possible_mask); 521 else if (ktext_nondataplane) 522 ktext_mask = kstripe_mask; 523 else if (!cpumask_empty(&ktext_mask)) { 524 /* Sanity-check any mask that was requested */ 525 struct cpumask bad; 526 cpumask_andnot(&bad, &ktext_mask, cpu_possible_mask); 527 cpumask_and(&ktext_mask, &ktext_mask, cpu_possible_mask); 528 if (!cpumask_empty(&bad)) { 529 char buf[NR_CPUS * 5]; 530 cpulist_scnprintf(buf, sizeof(buf), &bad); 531 pr_info("ktext: not using unavailable cpus %s\n", buf); 532 } 533 if (cpumask_empty(&ktext_mask)) { 534 pr_warning("ktext: no valid cpus; caching on %d.\n", 535 smp_processor_id()); 536 cpumask_copy(&ktext_mask, 537 cpumask_of(smp_processor_id())); 538 } 539 } 540 541 address = MEM_SV_INTRPT; 542 pmd = get_pmd(pgtables, address); 543 pfn = 0; /* code starts at PA 0 */ 544 if (ktext_small) { 545 /* Allocate an L2 PTE for the kernel text */ 546 int cpu = 0; 547 pgprot_t prot = construct_pgprot(PAGE_KERNEL_EXEC, 548 PAGE_HOME_IMMUTABLE); 549 550 if (ktext_local) { 551 if (ktext_nocache) 552 prot = hv_pte_set_mode(prot, 553 HV_PTE_MODE_UNCACHED); 554 else 555 prot = hv_pte_set_mode(prot, 556 HV_PTE_MODE_CACHE_NO_L3); 557 } else { 558 prot = hv_pte_set_mode(prot, 559 HV_PTE_MODE_CACHE_TILE_L3); 560 cpu = cpumask_first(&ktext_mask); 561 562 prot = ktext_set_nocache(prot); 563 } 564 565 BUG_ON(address != (unsigned long)_stext); 566 pte = NULL; 567 for (; address < (unsigned long)_einittext; 568 pfn++, address += PAGE_SIZE) { 569 pte_ofs = pte_index(address); 570 if (pte_ofs == 0) { 571 if (pte) 572 assign_pte(pmd++, pte); 573 pte = alloc_pte(); 574 } 575 if (!ktext_local) { 576 prot = set_remote_cache_cpu(prot, cpu); 577 cpu = cpumask_next(cpu, &ktext_mask); 578 if (cpu == NR_CPUS) 579 cpu = cpumask_first(&ktext_mask); 580 } 581 pte[pte_ofs] = pfn_pte(pfn, prot); 582 } 583 if (pte) 584 assign_pte(pmd, pte); 585 } else { 586 pte_t pteval = pfn_pte(0, PAGE_KERNEL_EXEC); 587 pteval = pte_mkhuge(pteval); 588#if CHIP_HAS_CBOX_HOME_MAP() 589 if (ktext_hash) { 590 pteval = hv_pte_set_mode(pteval, 591 HV_PTE_MODE_CACHE_HASH_L3); 592 pteval = ktext_set_nocache(pteval); 593 } else 594#endif /* CHIP_HAS_CBOX_HOME_MAP() */ 595 if (cpumask_weight(&ktext_mask) == 1) { 596 pteval = set_remote_cache_cpu(pteval, 597 cpumask_first(&ktext_mask)); 598 pteval = hv_pte_set_mode(pteval, 599 HV_PTE_MODE_CACHE_TILE_L3); 600 pteval = ktext_set_nocache(pteval); 601 } else if (ktext_nocache) 602 pteval = hv_pte_set_mode(pteval, 603 HV_PTE_MODE_UNCACHED); 604 else 605 pteval = hv_pte_set_mode(pteval, 606 HV_PTE_MODE_CACHE_NO_L3); 607 for (; address < (unsigned long)_einittext; 608 pfn += PFN_DOWN(HPAGE_SIZE), address += HPAGE_SIZE) 609 *(pte_t *)(pmd++) = pfn_pte(pfn, pteval); 610 } 611 612 /* Set swapper_pgprot here so it is flushed to memory right away. */ 613 swapper_pgprot = init_pgprot((unsigned long)swapper_pg_dir); 614 615 /* 616 * Since we may be changing the caching of the stack and page 617 * table itself, we invoke an assembly helper to do the 618 * following steps: 619 * 620 * - flush the cache so we start with an empty slate 621 * - install pgtables[] as the real page table 622 * - flush the TLB so the new page table takes effect 623 */ 624 irqmask = interrupt_mask_save_mask(); 625 interrupt_mask_set_mask(-1ULL); 626 rc = flush_and_install_context(__pa(pgtables), 627 init_pgprot((unsigned long)pgtables), 628 __get_cpu_var(current_asid), 629 cpumask_bits(my_cpu_mask)); 630 interrupt_mask_restore_mask(irqmask); 631 BUG_ON(rc != 0); 632 633 /* Copy the page table back to the normal swapper_pg_dir. */ 634 memcpy(pgd_base, pgtables, sizeof(pgtables)); 635 __install_page_table(pgd_base, __get_cpu_var(current_asid), 636 swapper_pgprot); 637 638 /* 639 * We just read swapper_pgprot and thus brought it into the cache, 640 * with its new home & caching mode. When we start the other CPUs, 641 * they're going to reference swapper_pgprot via their initial fake 642 * VA-is-PA mappings, which cache everything locally. At that 643 * time, if it's in our cache with a conflicting home, the 644 * simulator's coherence checker will complain. So, flush it out 645 * of our cache; we're not going to ever use it again anyway. 646 */ 647 __insn_finv(&swapper_pgprot); 648} 649 650/* 651 * devmem_is_allowed() checks to see if /dev/mem access to a certain address 652 * is valid. The argument is a physical page number. 653 * 654 * On Tile, the only valid things for which we can just hand out unchecked 655 * PTEs are the kernel code and data. Anything else might change its 656 * homing with time, and we wouldn't know to adjust the /dev/mem PTEs. 657 * Note that init_thread_union is released to heap soon after boot, 658 * so we include it in the init data. 659 * 660 * For TILE-Gx, we might want to consider allowing access to PA 661 * regions corresponding to PCI space, etc. 662 */ 663int devmem_is_allowed(unsigned long pagenr) 664{ 665 return pagenr < kaddr_to_pfn(_end) && 666 !(pagenr >= kaddr_to_pfn(&init_thread_union) || 667 pagenr < kaddr_to_pfn(_einitdata)) && 668 !(pagenr >= kaddr_to_pfn(_sinittext) || 669 pagenr <= kaddr_to_pfn(_einittext-1)); 670} 671 672#ifdef CONFIG_HIGHMEM 673static void __init permanent_kmaps_init(pgd_t *pgd_base) 674{ 675 pgd_t *pgd; 676 pud_t *pud; 677 pmd_t *pmd; 678 pte_t *pte; 679 unsigned long vaddr; 680 681 vaddr = PKMAP_BASE; 682 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base); 683 684 pgd = swapper_pg_dir + pgd_index(vaddr); 685 pud = pud_offset(pgd, vaddr); 686 pmd = pmd_offset(pud, vaddr); 687 pte = pte_offset_kernel(pmd, vaddr); 688 pkmap_page_table = pte; 689} 690#endif /* CONFIG_HIGHMEM */ 691 692 693#ifndef CONFIG_64BIT 694static void __init init_free_pfn_range(unsigned long start, unsigned long end) 695{ 696 unsigned long pfn; 697 struct page *page = pfn_to_page(start); 698 699 for (pfn = start; pfn < end; ) { 700 /* Optimize by freeing pages in large batches */ 701 int order = __ffs(pfn); 702 int count, i; 703 struct page *p; 704 705 if (order >= MAX_ORDER) 706 order = MAX_ORDER-1; 707 count = 1 << order; 708 while (pfn + count > end) { 709 count >>= 1; 710 --order; 711 } 712 for (p = page, i = 0; i < count; ++i, ++p) { 713 __ClearPageReserved(p); 714 /* 715 * Hacky direct set to avoid unnecessary 716 * lock take/release for EVERY page here. 717 */ 718 p->_count.counter = 0; 719 p->_mapcount.counter = -1; 720 } 721 init_page_count(page); 722 __free_pages(page, order); 723 totalram_pages += count; 724 725 page += count; 726 pfn += count; 727 } 728} 729 730static void __init set_non_bootmem_pages_init(void) 731{ 732 struct zone *z; 733 for_each_zone(z) { 734 unsigned long start, end; 735 int nid = z->zone_pgdat->node_id; 736#ifdef CONFIG_HIGHMEM 737 int idx = zone_idx(z); 738#endif 739 740 start = z->zone_start_pfn; 741 end = start + z->spanned_pages; 742 start = max(start, node_free_pfn[nid]); 743 start = max(start, max_low_pfn); 744 745#ifdef CONFIG_HIGHMEM 746 if (idx == ZONE_HIGHMEM) 747 totalhigh_pages += z->spanned_pages; 748#endif 749 if (kdata_huge) { 750 unsigned long percpu_pfn = node_percpu_pfn[nid]; 751 if (start < percpu_pfn && end > percpu_pfn) 752 end = percpu_pfn; 753 } 754#ifdef CONFIG_PCI 755 if (start <= pci_reserve_start_pfn && 756 end > pci_reserve_start_pfn) { 757 if (end > pci_reserve_end_pfn) 758 init_free_pfn_range(pci_reserve_end_pfn, end); 759 end = pci_reserve_start_pfn; 760 } 761#endif 762 init_free_pfn_range(start, end); 763 } 764} 765#endif 766 767/* 768 * paging_init() sets up the page tables - note that all of lowmem is 769 * already mapped by head.S. 770 */ 771void __init paging_init(void) 772{ 773#ifdef __tilegx__ 774 pud_t *pud; 775#endif 776 pgd_t *pgd_base = swapper_pg_dir; 777 778 kernel_physical_mapping_init(pgd_base); 779 780 /* 781 * Fixed mappings, only the page table structure has to be 782 * created - mappings will be set by set_fixmap(): 783 */ 784 page_table_range_init(fix_to_virt(__end_of_fixed_addresses - 1), 785 FIXADDR_TOP, pgd_base); 786 787#ifdef CONFIG_HIGHMEM 788 permanent_kmaps_init(pgd_base); 789#endif 790 791#ifdef __tilegx__ 792 /* 793 * Since GX allocates just one pmd_t array worth of vmalloc space, 794 * we go ahead and allocate it statically here, then share it 795 * globally. As a result we don't have to worry about any task 796 * changing init_mm once we get up and running, and there's no 797 * need for e.g. vmalloc_sync_all(). 798 */ 799 BUILD_BUG_ON(pgd_index(VMALLOC_START) != pgd_index(VMALLOC_END - 1)); 800 pud = pud_offset(pgd_base + pgd_index(VMALLOC_START), VMALLOC_START); 801 assign_pmd(pud, alloc_pmd()); 802#endif 803} 804 805 806/* 807 * Walk the kernel page tables and derive the page_home() from 808 * the PTEs, so that set_pte() can properly validate the caching 809 * of all PTEs it sees. 810 */ 811void __init set_page_homes(void) 812{ 813} 814 815static void __init set_max_mapnr_init(void) 816{ 817#ifdef CONFIG_FLATMEM 818 max_mapnr = max_low_pfn; 819#endif 820} 821 822void __init mem_init(void) 823{ 824 int codesize, datasize, initsize; 825 int i; 826#ifndef __tilegx__ 827 void *last; 828#endif 829 830#ifdef CONFIG_FLATMEM 831 BUG_ON(!mem_map); 832#endif 833 834#ifdef CONFIG_HIGHMEM 835 /* check that fixmap and pkmap do not overlap */ 836 if (PKMAP_ADDR(LAST_PKMAP-1) >= FIXADDR_START) { 837 pr_err("fixmap and kmap areas overlap" 838 " - this will crash\n"); 839 pr_err("pkstart: %lxh pkend: %lxh fixstart %lxh\n", 840 PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP-1), 841 FIXADDR_START); 842 BUG(); 843 } 844#endif 845 846 set_max_mapnr_init(); 847 848 /* this will put all bootmem onto the freelists */ 849 totalram_pages += free_all_bootmem(); 850 851#ifndef CONFIG_64BIT 852 /* count all remaining LOWMEM and give all HIGHMEM to page allocator */ 853 set_non_bootmem_pages_init(); 854#endif 855 856 codesize = (unsigned long)&_etext - (unsigned long)&_text; 857 datasize = (unsigned long)&_end - (unsigned long)&_sdata; 858 initsize = (unsigned long)&_einittext - (unsigned long)&_sinittext; 859 initsize += (unsigned long)&_einitdata - (unsigned long)&_sinitdata; 860 861 pr_info("Memory: %luk/%luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n", 862 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10), 863 num_physpages << (PAGE_SHIFT-10), 864 codesize >> 10, 865 datasize >> 10, 866 initsize >> 10, 867 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10)) 868 ); 869 870 /* 871 * In debug mode, dump some interesting memory mappings. 872 */ 873#ifdef CONFIG_HIGHMEM 874 printk(KERN_DEBUG " KMAP %#lx - %#lx\n", 875 FIXADDR_START, FIXADDR_TOP + PAGE_SIZE - 1); 876 printk(KERN_DEBUG " PKMAP %#lx - %#lx\n", 877 PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP) - 1); 878#endif 879#ifdef CONFIG_HUGEVMAP 880 printk(KERN_DEBUG " HUGEMAP %#lx - %#lx\n", 881 HUGE_VMAP_BASE, HUGE_VMAP_END - 1); 882#endif 883 printk(KERN_DEBUG " VMALLOC %#lx - %#lx\n", 884 _VMALLOC_START, _VMALLOC_END - 1); 885#ifdef __tilegx__ 886 for (i = MAX_NUMNODES-1; i >= 0; --i) { 887 struct pglist_data *node = &node_data[i]; 888 if (node->node_present_pages) { 889 unsigned long start = (unsigned long) 890 pfn_to_kaddr(node->node_start_pfn); 891 unsigned long end = start + 892 (node->node_present_pages << PAGE_SHIFT); 893 printk(KERN_DEBUG " MEM%d %#lx - %#lx\n", 894 i, start, end - 1); 895 } 896 } 897#else 898 last = high_memory; 899 for (i = MAX_NUMNODES-1; i >= 0; --i) { 900 if ((unsigned long)vbase_map[i] != -1UL) { 901 printk(KERN_DEBUG " LOWMEM%d %#lx - %#lx\n", 902 i, (unsigned long) (vbase_map[i]), 903 (unsigned long) (last-1)); 904 last = vbase_map[i]; 905 } 906 } 907#endif 908 909#ifndef __tilegx__ 910 /* 911 * Convert from using one lock for all atomic operations to 912 * one per cpu. 913 */ 914 __init_atomic_per_cpu(); 915#endif 916} 917 918/* 919 * this is for the non-NUMA, single node SMP system case. 920 * Specifically, in the case of x86, we will always add 921 * memory to the highmem for now. 922 */ 923#ifndef CONFIG_NEED_MULTIPLE_NODES 924int arch_add_memory(u64 start, u64 size) 925{ 926 struct pglist_data *pgdata = &contig_page_data; 927 struct zone *zone = pgdata->node_zones + MAX_NR_ZONES-1; 928 unsigned long start_pfn = start >> PAGE_SHIFT; 929 unsigned long nr_pages = size >> PAGE_SHIFT; 930 931 return __add_pages(zone, start_pfn, nr_pages); 932} 933 934int remove_memory(u64 start, u64 size) 935{ 936 return -EINVAL; 937} 938#endif 939 940struct kmem_cache *pgd_cache; 941 942void __init pgtable_cache_init(void) 943{ 944 pgd_cache = kmem_cache_create("pgd", SIZEOF_PGD, SIZEOF_PGD, 0, NULL); 945 if (!pgd_cache) 946 panic("pgtable_cache_init(): Cannot create pgd cache"); 947} 948 949#if !CHIP_HAS_COHERENT_LOCAL_CACHE() 950/* 951 * The __w1data area holds data that is only written during initialization, 952 * and is read-only and thus freely cacheable thereafter. Fix the page 953 * table entries that cover that region accordingly. 954 */ 955static void mark_w1data_ro(void) 956{ 957 /* Loop over page table entries */ 958 unsigned long addr = (unsigned long)__w1data_begin; 959 BUG_ON((addr & (PAGE_SIZE-1)) != 0); 960 for (; addr <= (unsigned long)__w1data_end - 1; addr += PAGE_SIZE) { 961 unsigned long pfn = kaddr_to_pfn((void *)addr); 962 pte_t *ptep = virt_to_pte(NULL, addr); 963 BUG_ON(pte_huge(*ptep)); /* not relevant for kdata_huge */ 964 set_pte_at(&init_mm, addr, ptep, pfn_pte(pfn, PAGE_KERNEL_RO)); 965 } 966} 967#endif 968 969#ifdef CONFIG_DEBUG_PAGEALLOC 970static long __write_once initfree; 971#else 972static long __write_once initfree = 1; 973#endif 974 975/* Select whether to free (1) or mark unusable (0) the __init pages. */ 976static int __init set_initfree(char *str) 977{ 978 long val; 979 if (strict_strtol(str, 0, &val) == 0) { 980 initfree = val; 981 pr_info("initfree: %s free init pages\n", 982 initfree ? "will" : "won't"); 983 } 984 return 1; 985} 986__setup("initfree=", set_initfree); 987 988static void free_init_pages(char *what, unsigned long begin, unsigned long end) 989{ 990 unsigned long addr = (unsigned long) begin; 991 992 if (kdata_huge && !initfree) { 993 pr_warning("Warning: ignoring initfree=0:" 994 " incompatible with kdata=huge\n"); 995 initfree = 1; 996 } 997 end = (end + PAGE_SIZE - 1) & PAGE_MASK; 998 local_flush_tlb_pages(NULL, begin, PAGE_SIZE, end - begin); 999 for (addr = begin; addr < end; addr += PAGE_SIZE) { 1000 /* 1001 * Note we just reset the home here directly in the 1002 * page table. We know this is safe because our caller 1003 * just flushed the caches on all the other cpus, 1004 * and they won't be touching any of these pages. 1005 */ 1006 int pfn = kaddr_to_pfn((void *)addr); 1007 struct page *page = pfn_to_page(pfn); 1008 pte_t *ptep = virt_to_pte(NULL, addr); 1009 if (!initfree) { 1010 /* 1011 * If debugging page accesses then do not free 1012 * this memory but mark them not present - any 1013 * buggy init-section access will create a 1014 * kernel page fault: 1015 */ 1016 pte_clear(&init_mm, addr, ptep); 1017 continue; 1018 } 1019 __ClearPageReserved(page); 1020 init_page_count(page); 1021 if (pte_huge(*ptep)) 1022 BUG_ON(!kdata_huge); 1023 else 1024 set_pte_at(&init_mm, addr, ptep, 1025 pfn_pte(pfn, PAGE_KERNEL)); 1026 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE); 1027 free_page(addr); 1028 totalram_pages++; 1029 } 1030 pr_info("Freeing %s: %ldk freed\n", what, (end - begin) >> 10); 1031} 1032 1033void free_initmem(void) 1034{ 1035 const unsigned long text_delta = MEM_SV_INTRPT - PAGE_OFFSET; 1036 1037 /* 1038 * Evict the dirty initdata on the boot cpu, evict the w1data 1039 * wherever it's homed, and evict all the init code everywhere. 1040 * We are guaranteed that no one will touch the init pages any 1041 * more, and although other cpus may be touching the w1data, 1042 * we only actually change the caching on tile64, which won't 1043 * be keeping local copies in the other tiles' caches anyway. 1044 */ 1045 homecache_evict(&cpu_cacheable_map); 1046 1047 /* Free the data pages that we won't use again after init. */ 1048 free_init_pages("unused kernel data", 1049 (unsigned long)_sinitdata, 1050 (unsigned long)_einitdata); 1051 1052 /* 1053 * Free the pages mapped from 0xc0000000 that correspond to code 1054 * pages from MEM_SV_INTRPT that we won't use again after init. 1055 */ 1056 free_init_pages("unused kernel text", 1057 (unsigned long)_sinittext - text_delta, 1058 (unsigned long)_einittext - text_delta); 1059 1060#if !CHIP_HAS_COHERENT_LOCAL_CACHE() 1061 /* 1062 * Upgrade the .w1data section to globally cached. 1063 * We don't do this on tilepro, since the cache architecture 1064 * pretty much makes it irrelevant, and in any case we end 1065 * up having racing issues with other tiles that may touch 1066 * the data after we flush the cache but before we update 1067 * the PTEs and flush the TLBs, causing sharer shootdowns 1068 * later. Even though this is to clean data, it seems like 1069 * an unnecessary complication. 1070 */ 1071 mark_w1data_ro(); 1072#endif 1073 1074 /* Do a global TLB flush so everyone sees the changes. */ 1075 flush_tlb_all(); 1076}