Disintegrate asm/system.h for Tile
[linux-2.6.git] / arch / tile / mm / init.c
blob830c4908ea76cf4c7156777db8a8cced9fb887f9
1 /*
2 * Copyright (C) 1995 Linus Torvalds
3 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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.
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>
53 #include "migrate.h"
55 #define clear_pgd(pmdptr) (*(pmdptr) = hv_pte(0))
57 #ifndef __tilegx__
58 unsigned long VMALLOC_RESERVE = CONFIG_VMALLOC_RESERVE;
59 EXPORT_SYMBOL(VMALLOC_RESERVE);
60 #endif
62 /* Create an L2 page table */
63 static pte_t * __init alloc_pte(void)
65 return __alloc_bootmem(L2_KERNEL_PGTABLE_SIZE, HV_PAGE_TABLE_ALIGN, 0);
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.
80 static pte_t *l2_ptes[MAX_NUMNODES];
81 static int num_l2_ptes[MAX_NUMNODES];
83 static void init_prealloc_ptes(int node, int pages)
85 BUG_ON(pages & (HV_L2_ENTRIES-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);
93 pte_t *get_prealloc_pte(unsigned long pfn)
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];
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.)
107 static int initial_heap_home(void)
109 #if CHIP_HAS_CBOX_HOME_MAP()
110 if (hash_default)
111 return PAGE_HOME_HASH;
112 #endif
113 return smp_processor_id();
117 * Place a pointer to an L2 page table in a middle page
118 * directory entry.
120 static void __init assign_pte(pmd_t *pmd, pte_t *page_table)
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();
132 #ifdef __tilegx__
134 #if HV_L1_SIZE != HV_L2_SIZE
135 # error Rework assumption that L1 and L2 page tables are same size.
136 #endif
138 /* Since pmd_t arrays and pte_t arrays are the same size, just use casts. */
139 static inline pmd_t *alloc_pmd(void)
141 return (pmd_t *)alloc_pte();
144 static inline void assign_pmd(pud_t *pud, pmd_t *pmd)
146 assign_pte((pmd_t *)pud, (pte_t *)pmd);
149 #endif /* __tilegx__ */
151 /* Replace the given pmd with a full PTE table. */
152 void __init shatter_pmd(pmd_t *pmd)
154 pte_t *pte = get_prealloc_pte(pte_pfn(*(pte_t *)pmd));
155 assign_pte(pmd, pte);
158 #ifdef CONFIG_HIGHMEM
160 * This function initializes a certain range of kernel virtual memory
161 * with new bootmem page tables, everywhere page tables are missing in
162 * the given range.
166 * NOTE: The pagetables are allocated contiguous on the physical space
167 * so we can cache the place of the first one and move around without
168 * checking the pgd every time.
170 static void __init page_table_range_init(unsigned long start,
171 unsigned long end, pgd_t *pgd_base)
173 pgd_t *pgd;
174 int pgd_idx;
175 unsigned long vaddr;
177 vaddr = start;
178 pgd_idx = pgd_index(vaddr);
179 pgd = pgd_base + pgd_idx;
181 for ( ; (pgd_idx < PTRS_PER_PGD) && (vaddr != end); pgd++, pgd_idx++) {
182 pmd_t *pmd = pmd_offset(pud_offset(pgd, vaddr), vaddr);
183 if (pmd_none(*pmd))
184 assign_pte(pmd, alloc_pte());
185 vaddr += PMD_SIZE;
188 #endif /* CONFIG_HIGHMEM */
191 #if CHIP_HAS_CBOX_HOME_MAP()
193 static int __initdata ktext_hash = 1; /* .text pages */
194 static int __initdata kdata_hash = 1; /* .data and .bss pages */
195 int __write_once hash_default = 1; /* kernel allocator pages */
196 EXPORT_SYMBOL(hash_default);
197 int __write_once kstack_hash = 1; /* if no homecaching, use h4h */
198 #endif /* CHIP_HAS_CBOX_HOME_MAP */
201 * CPUs to use to for striping the pages of kernel data. If hash-for-home
202 * is available, this is only relevant if kcache_hash sets up the
203 * .data and .bss to be page-homed, and we don't want the default mode
204 * of using the full set of kernel cpus for the striping.
206 static __initdata struct cpumask kdata_mask;
207 static __initdata int kdata_arg_seen;
209 int __write_once kdata_huge; /* if no homecaching, small pages */
212 /* Combine a generic pgprot_t with cache home to get a cache-aware pgprot. */
213 static pgprot_t __init construct_pgprot(pgprot_t prot, int home)
215 prot = pte_set_home(prot, home);
216 #if CHIP_HAS_CBOX_HOME_MAP()
217 if (home == PAGE_HOME_IMMUTABLE) {
218 if (ktext_hash)
219 prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_HASH_L3);
220 else
221 prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_NO_L3);
223 #endif
224 return prot;
228 * For a given kernel data VA, how should it be cached?
229 * We return the complete pgprot_t with caching bits set.
231 static pgprot_t __init init_pgprot(ulong address)
233 int cpu;
234 unsigned long page;
235 enum { CODE_DELTA = MEM_SV_INTRPT - PAGE_OFFSET };
237 #if CHIP_HAS_CBOX_HOME_MAP()
238 /* For kdata=huge, everything is just hash-for-home. */
239 if (kdata_huge)
240 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
241 #endif
243 /* We map the aliased pages of permanent text inaccessible. */
244 if (address < (ulong) _sinittext - CODE_DELTA)
245 return PAGE_NONE;
248 * We map read-only data non-coherent for performance. We could
249 * use neighborhood caching on TILE64, but it's not clear it's a win.
251 if ((address >= (ulong) __start_rodata &&
252 address < (ulong) __end_rodata) ||
253 address == (ulong) empty_zero_page) {
254 return construct_pgprot(PAGE_KERNEL_RO, PAGE_HOME_IMMUTABLE);
257 /* As a performance optimization, keep the boot init stack here. */
258 if (address >= (ulong)&init_thread_union &&
259 address < (ulong)&init_thread_union + THREAD_SIZE)
260 return construct_pgprot(PAGE_KERNEL, smp_processor_id());
262 #ifndef __tilegx__
263 #if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
264 /* Force the atomic_locks[] array page to be hash-for-home. */
265 if (address == (ulong) atomic_locks)
266 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
267 #endif
268 #endif
271 * Everything else that isn't data or bss is heap, so mark it
272 * with the initial heap home (hash-for-home, or this cpu). This
273 * includes any addresses after the loaded image and any address before
274 * _einitdata, since we already captured the case of text before
275 * _sinittext, and __pa(einittext) is approximately __pa(sinitdata).
277 * All the LOWMEM pages that we mark this way will get their
278 * struct page homecache properly marked later, in set_page_homes().
279 * The HIGHMEM pages we leave with a default zero for their
280 * homes, but with a zero free_time we don't have to actually
281 * do a flush action the first time we use them, either.
283 if (address >= (ulong) _end || address < (ulong) _einitdata)
284 return construct_pgprot(PAGE_KERNEL, initial_heap_home());
286 #if CHIP_HAS_CBOX_HOME_MAP()
287 /* Use hash-for-home if requested for data/bss. */
288 if (kdata_hash)
289 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
290 #endif
293 * Make the w1data homed like heap to start with, to avoid
294 * making it part of the page-striped data area when we're just
295 * going to convert it to read-only soon anyway.
297 if (address >= (ulong)__w1data_begin && address < (ulong)__w1data_end)
298 return construct_pgprot(PAGE_KERNEL, initial_heap_home());
301 * Otherwise we just hand out consecutive cpus. To avoid
302 * requiring this function to hold state, we just walk forward from
303 * _sdata by PAGE_SIZE, skipping the readonly and init data, to reach
304 * the requested address, while walking cpu home around kdata_mask.
305 * This is typically no more than a dozen or so iterations.
307 page = (((ulong)__w1data_end) + PAGE_SIZE - 1) & PAGE_MASK;
308 BUG_ON(address < page || address >= (ulong)_end);
309 cpu = cpumask_first(&kdata_mask);
310 for (; page < address; page += PAGE_SIZE) {
311 if (page >= (ulong)&init_thread_union &&
312 page < (ulong)&init_thread_union + THREAD_SIZE)
313 continue;
314 if (page == (ulong)empty_zero_page)
315 continue;
316 #ifndef __tilegx__
317 #if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
318 if (page == (ulong)atomic_locks)
319 continue;
320 #endif
321 #endif
322 cpu = cpumask_next(cpu, &kdata_mask);
323 if (cpu == NR_CPUS)
324 cpu = cpumask_first(&kdata_mask);
326 return construct_pgprot(PAGE_KERNEL, cpu);
330 * This function sets up how we cache the kernel text. If we have
331 * hash-for-home support, normally that is used instead (see the
332 * kcache_hash boot flag for more information). But if we end up
333 * using a page-based caching technique, this option sets up the
334 * details of that. In addition, the "ktext=nocache" option may
335 * always be used to disable local caching of text pages, if desired.
338 static int __initdata ktext_arg_seen;
339 static int __initdata ktext_small;
340 static int __initdata ktext_local;
341 static int __initdata ktext_all;
342 static int __initdata ktext_nondataplane;
343 static int __initdata ktext_nocache;
344 static struct cpumask __initdata ktext_mask;
346 static int __init setup_ktext(char *str)
348 if (str == NULL)
349 return -EINVAL;
351 /* If you have a leading "nocache", turn off ktext caching */
352 if (strncmp(str, "nocache", 7) == 0) {
353 ktext_nocache = 1;
354 pr_info("ktext: disabling local caching of kernel text\n");
355 str += 7;
356 if (*str == ',')
357 ++str;
358 if (*str == '\0')
359 return 0;
362 ktext_arg_seen = 1;
364 /* Default setting on Tile64: use a huge page */
365 if (strcmp(str, "huge") == 0)
366 pr_info("ktext: using one huge locally cached page\n");
368 /* Pay TLB cost but get no cache benefit: cache small pages locally */
369 else if (strcmp(str, "local") == 0) {
370 ktext_small = 1;
371 ktext_local = 1;
372 pr_info("ktext: using small pages with local caching\n");
375 /* Neighborhood cache ktext pages on all cpus. */
376 else if (strcmp(str, "all") == 0) {
377 ktext_small = 1;
378 ktext_all = 1;
379 pr_info("ktext: using maximal caching neighborhood\n");
383 /* Neighborhood ktext pages on specified mask */
384 else if (cpulist_parse(str, &ktext_mask) == 0) {
385 char buf[NR_CPUS * 5];
386 cpulist_scnprintf(buf, sizeof(buf), &ktext_mask);
387 if (cpumask_weight(&ktext_mask) > 1) {
388 ktext_small = 1;
389 pr_info("ktext: using caching neighborhood %s "
390 "with small pages\n", buf);
391 } else {
392 pr_info("ktext: caching on cpu %s with one huge page\n",
393 buf);
397 else if (*str)
398 return -EINVAL;
400 return 0;
403 early_param("ktext", setup_ktext);
406 static inline pgprot_t ktext_set_nocache(pgprot_t prot)
408 if (!ktext_nocache)
409 prot = hv_pte_set_nc(prot);
410 #if CHIP_HAS_NC_AND_NOALLOC_BITS()
411 else
412 prot = hv_pte_set_no_alloc_l2(prot);
413 #endif
414 return prot;
417 #ifndef __tilegx__
418 static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
420 return pmd_offset(pud_offset(&pgtables[pgd_index(va)], va), va);
422 #else
423 static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
425 pud_t *pud = pud_offset(&pgtables[pgd_index(va)], va);
426 if (pud_none(*pud))
427 assign_pmd(pud, alloc_pmd());
428 return pmd_offset(pud, va);
430 #endif
432 /* Temporary page table we use for staging. */
433 static pgd_t pgtables[PTRS_PER_PGD]
434 __attribute__((aligned(HV_PAGE_TABLE_ALIGN)));
437 * This maps the physical memory to kernel virtual address space, a total
438 * of max_low_pfn pages, by creating page tables starting from address
439 * PAGE_OFFSET.
441 * This routine transitions us from using a set of compiled-in large
442 * pages to using some more precise caching, including removing access
443 * to code pages mapped at PAGE_OFFSET (executed only at MEM_SV_START)
444 * marking read-only data as locally cacheable, striping the remaining
445 * .data and .bss across all the available tiles, and removing access
446 * to pages above the top of RAM (thus ensuring a page fault from a bad
447 * virtual address rather than a hypervisor shoot down for accessing
448 * memory outside the assigned limits).
450 static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
452 unsigned long address, pfn;
453 pmd_t *pmd;
454 pte_t *pte;
455 int pte_ofs;
456 const struct cpumask *my_cpu_mask = cpumask_of(smp_processor_id());
457 struct cpumask kstripe_mask;
458 int rc, i;
460 #if CHIP_HAS_CBOX_HOME_MAP()
461 if (ktext_arg_seen && ktext_hash) {
462 pr_warning("warning: \"ktext\" boot argument ignored"
463 " if \"kcache_hash\" sets up text hash-for-home\n");
464 ktext_small = 0;
467 if (kdata_arg_seen && kdata_hash) {
468 pr_warning("warning: \"kdata\" boot argument ignored"
469 " if \"kcache_hash\" sets up data hash-for-home\n");
472 if (kdata_huge && !hash_default) {
473 pr_warning("warning: disabling \"kdata=huge\"; requires"
474 " kcache_hash=all or =allbutstack\n");
475 kdata_huge = 0;
477 #endif
480 * Set up a mask for cpus to use for kernel striping.
481 * This is normally all cpus, but minus dataplane cpus if any.
482 * If the dataplane covers the whole chip, we stripe over
483 * the whole chip too.
485 cpumask_copy(&kstripe_mask, cpu_possible_mask);
486 if (!kdata_arg_seen)
487 kdata_mask = kstripe_mask;
489 /* Allocate and fill in L2 page tables */
490 for (i = 0; i < MAX_NUMNODES; ++i) {
491 #ifdef CONFIG_HIGHMEM
492 unsigned long end_pfn = node_lowmem_end_pfn[i];
493 #else
494 unsigned long end_pfn = node_end_pfn[i];
495 #endif
496 unsigned long end_huge_pfn = 0;
498 /* Pre-shatter the last huge page to allow per-cpu pages. */
499 if (kdata_huge)
500 end_huge_pfn = end_pfn - (HPAGE_SIZE >> PAGE_SHIFT);
502 pfn = node_start_pfn[i];
504 /* Allocate enough memory to hold L2 page tables for node. */
505 init_prealloc_ptes(i, end_pfn - pfn);
507 address = (unsigned long) pfn_to_kaddr(pfn);
508 while (pfn < end_pfn) {
509 BUG_ON(address & (HPAGE_SIZE-1));
510 pmd = get_pmd(pgtables, address);
511 pte = get_prealloc_pte(pfn);
512 if (pfn < end_huge_pfn) {
513 pgprot_t prot = init_pgprot(address);
514 *(pte_t *)pmd = pte_mkhuge(pfn_pte(pfn, prot));
515 for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE;
516 pfn++, pte_ofs++, address += PAGE_SIZE)
517 pte[pte_ofs] = pfn_pte(pfn, prot);
518 } else {
519 if (kdata_huge)
520 printk(KERN_DEBUG "pre-shattered huge"
521 " page at %#lx\n", address);
522 for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE;
523 pfn++, pte_ofs++, address += PAGE_SIZE) {
524 pgprot_t prot = init_pgprot(address);
525 pte[pte_ofs] = pfn_pte(pfn, prot);
527 assign_pte(pmd, pte);
533 * Set or check ktext_map now that we have cpu_possible_mask
534 * and kstripe_mask to work with.
536 if (ktext_all)
537 cpumask_copy(&ktext_mask, cpu_possible_mask);
538 else if (ktext_nondataplane)
539 ktext_mask = kstripe_mask;
540 else if (!cpumask_empty(&ktext_mask)) {
541 /* Sanity-check any mask that was requested */
542 struct cpumask bad;
543 cpumask_andnot(&bad, &ktext_mask, cpu_possible_mask);
544 cpumask_and(&ktext_mask, &ktext_mask, cpu_possible_mask);
545 if (!cpumask_empty(&bad)) {
546 char buf[NR_CPUS * 5];
547 cpulist_scnprintf(buf, sizeof(buf), &bad);
548 pr_info("ktext: not using unavailable cpus %s\n", buf);
550 if (cpumask_empty(&ktext_mask)) {
551 pr_warning("ktext: no valid cpus; caching on %d.\n",
552 smp_processor_id());
553 cpumask_copy(&ktext_mask,
554 cpumask_of(smp_processor_id()));
558 address = MEM_SV_INTRPT;
559 pmd = get_pmd(pgtables, address);
560 if (ktext_small) {
561 /* Allocate an L2 PTE for the kernel text */
562 int cpu = 0;
563 pgprot_t prot = construct_pgprot(PAGE_KERNEL_EXEC,
564 PAGE_HOME_IMMUTABLE);
566 if (ktext_local) {
567 if (ktext_nocache)
568 prot = hv_pte_set_mode(prot,
569 HV_PTE_MODE_UNCACHED);
570 else
571 prot = hv_pte_set_mode(prot,
572 HV_PTE_MODE_CACHE_NO_L3);
573 } else {
574 prot = hv_pte_set_mode(prot,
575 HV_PTE_MODE_CACHE_TILE_L3);
576 cpu = cpumask_first(&ktext_mask);
578 prot = ktext_set_nocache(prot);
581 BUG_ON(address != (unsigned long)_stext);
582 pfn = 0; /* code starts at PA 0 */
583 pte = alloc_pte();
584 for (pte_ofs = 0; address < (unsigned long)_einittext;
585 pfn++, pte_ofs++, address += PAGE_SIZE) {
586 if (!ktext_local) {
587 prot = set_remote_cache_cpu(prot, cpu);
588 cpu = cpumask_next(cpu, &ktext_mask);
589 if (cpu == NR_CPUS)
590 cpu = cpumask_first(&ktext_mask);
592 pte[pte_ofs] = pfn_pte(pfn, prot);
594 assign_pte(pmd, pte);
595 } else {
596 pte_t pteval = pfn_pte(0, PAGE_KERNEL_EXEC);
597 pteval = pte_mkhuge(pteval);
598 #if CHIP_HAS_CBOX_HOME_MAP()
599 if (ktext_hash) {
600 pteval = hv_pte_set_mode(pteval,
601 HV_PTE_MODE_CACHE_HASH_L3);
602 pteval = ktext_set_nocache(pteval);
603 } else
604 #endif /* CHIP_HAS_CBOX_HOME_MAP() */
605 if (cpumask_weight(&ktext_mask) == 1) {
606 pteval = set_remote_cache_cpu(pteval,
607 cpumask_first(&ktext_mask));
608 pteval = hv_pte_set_mode(pteval,
609 HV_PTE_MODE_CACHE_TILE_L3);
610 pteval = ktext_set_nocache(pteval);
611 } else if (ktext_nocache)
612 pteval = hv_pte_set_mode(pteval,
613 HV_PTE_MODE_UNCACHED);
614 else
615 pteval = hv_pte_set_mode(pteval,
616 HV_PTE_MODE_CACHE_NO_L3);
617 *(pte_t *)pmd = pteval;
620 /* Set swapper_pgprot here so it is flushed to memory right away. */
621 swapper_pgprot = init_pgprot((unsigned long)swapper_pg_dir);
624 * Since we may be changing the caching of the stack and page
625 * table itself, we invoke an assembly helper to do the
626 * following steps:
628 * - flush the cache so we start with an empty slate
629 * - install pgtables[] as the real page table
630 * - flush the TLB so the new page table takes effect
632 rc = flush_and_install_context(__pa(pgtables),
633 init_pgprot((unsigned long)pgtables),
634 __get_cpu_var(current_asid),
635 cpumask_bits(my_cpu_mask));
636 BUG_ON(rc != 0);
638 /* Copy the page table back to the normal swapper_pg_dir. */
639 memcpy(pgd_base, pgtables, sizeof(pgtables));
640 __install_page_table(pgd_base, __get_cpu_var(current_asid),
641 swapper_pgprot);
644 * We just read swapper_pgprot and thus brought it into the cache,
645 * with its new home & caching mode. When we start the other CPUs,
646 * they're going to reference swapper_pgprot via their initial fake
647 * VA-is-PA mappings, which cache everything locally. At that
648 * time, if it's in our cache with a conflicting home, the
649 * simulator's coherence checker will complain. So, flush it out
650 * of our cache; we're not going to ever use it again anyway.
652 __insn_finv(&swapper_pgprot);
656 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
657 * is valid. The argument is a physical page number.
659 * On Tile, the only valid things for which we can just hand out unchecked
660 * PTEs are the kernel code and data. Anything else might change its
661 * homing with time, and we wouldn't know to adjust the /dev/mem PTEs.
662 * Note that init_thread_union is released to heap soon after boot,
663 * so we include it in the init data.
665 * For TILE-Gx, we might want to consider allowing access to PA
666 * regions corresponding to PCI space, etc.
668 int devmem_is_allowed(unsigned long pagenr)
670 return pagenr < kaddr_to_pfn(_end) &&
671 !(pagenr >= kaddr_to_pfn(&init_thread_union) ||
672 pagenr < kaddr_to_pfn(_einitdata)) &&
673 !(pagenr >= kaddr_to_pfn(_sinittext) ||
674 pagenr <= kaddr_to_pfn(_einittext-1));
677 #ifdef CONFIG_HIGHMEM
678 static void __init permanent_kmaps_init(pgd_t *pgd_base)
680 pgd_t *pgd;
681 pud_t *pud;
682 pmd_t *pmd;
683 pte_t *pte;
684 unsigned long vaddr;
686 vaddr = PKMAP_BASE;
687 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
689 pgd = swapper_pg_dir + pgd_index(vaddr);
690 pud = pud_offset(pgd, vaddr);
691 pmd = pmd_offset(pud, vaddr);
692 pte = pte_offset_kernel(pmd, vaddr);
693 pkmap_page_table = pte;
695 #endif /* CONFIG_HIGHMEM */
698 static void __init init_free_pfn_range(unsigned long start, unsigned long end)
700 unsigned long pfn;
701 struct page *page = pfn_to_page(start);
703 for (pfn = start; pfn < end; ) {
704 /* Optimize by freeing pages in large batches */
705 int order = __ffs(pfn);
706 int count, i;
707 struct page *p;
709 if (order >= MAX_ORDER)
710 order = MAX_ORDER-1;
711 count = 1 << order;
712 while (pfn + count > end) {
713 count >>= 1;
714 --order;
716 for (p = page, i = 0; i < count; ++i, ++p) {
717 __ClearPageReserved(p);
719 * Hacky direct set to avoid unnecessary
720 * lock take/release for EVERY page here.
722 p->_count.counter = 0;
723 p->_mapcount.counter = -1;
725 init_page_count(page);
726 __free_pages(page, order);
727 totalram_pages += count;
729 page += count;
730 pfn += count;
734 static void __init set_non_bootmem_pages_init(void)
736 struct zone *z;
737 for_each_zone(z) {
738 unsigned long start, end;
739 int nid = z->zone_pgdat->node_id;
740 int idx = zone_idx(z);
742 start = z->zone_start_pfn;
743 if (start == 0)
744 continue; /* bootmem */
745 end = start + z->spanned_pages;
746 if (idx == ZONE_NORMAL) {
747 BUG_ON(start != node_start_pfn[nid]);
748 start = node_free_pfn[nid];
750 #ifdef CONFIG_HIGHMEM
751 if (idx == ZONE_HIGHMEM)
752 totalhigh_pages += z->spanned_pages;
753 #endif
754 if (kdata_huge) {
755 unsigned long percpu_pfn = node_percpu_pfn[nid];
756 if (start < percpu_pfn && end > percpu_pfn)
757 end = percpu_pfn;
759 #ifdef CONFIG_PCI
760 if (start <= pci_reserve_start_pfn &&
761 end > pci_reserve_start_pfn) {
762 if (end > pci_reserve_end_pfn)
763 init_free_pfn_range(pci_reserve_end_pfn, end);
764 end = pci_reserve_start_pfn;
766 #endif
767 init_free_pfn_range(start, end);
772 * paging_init() sets up the page tables - note that all of lowmem is
773 * already mapped by head.S.
775 void __init paging_init(void)
777 #ifdef CONFIG_HIGHMEM
778 unsigned long vaddr, end;
779 #endif
780 #ifdef __tilegx__
781 pud_t *pud;
782 #endif
783 pgd_t *pgd_base = swapper_pg_dir;
785 kernel_physical_mapping_init(pgd_base);
787 #ifdef CONFIG_HIGHMEM
789 * Fixed mappings, only the page table structure has to be
790 * created - mappings will be set by set_fixmap():
792 vaddr = __fix_to_virt(__end_of_fixed_addresses - 1) & PMD_MASK;
793 end = (FIXADDR_TOP + PMD_SIZE - 1) & PMD_MASK;
794 page_table_range_init(vaddr, end, pgd_base);
795 permanent_kmaps_init(pgd_base);
796 #endif
798 #ifdef __tilegx__
800 * Since GX allocates just one pmd_t array worth of vmalloc space,
801 * we go ahead and allocate it statically here, then share it
802 * globally. As a result we don't have to worry about any task
803 * changing init_mm once we get up and running, and there's no
804 * need for e.g. vmalloc_sync_all().
806 BUILD_BUG_ON(pgd_index(VMALLOC_START) != pgd_index(VMALLOC_END));
807 pud = pud_offset(pgd_base + pgd_index(VMALLOC_START), VMALLOC_START);
808 assign_pmd(pud, alloc_pmd());
809 #endif
814 * Walk the kernel page tables and derive the page_home() from
815 * the PTEs, so that set_pte() can properly validate the caching
816 * of all PTEs it sees.
818 void __init set_page_homes(void)
822 static void __init set_max_mapnr_init(void)
824 #ifdef CONFIG_FLATMEM
825 max_mapnr = max_low_pfn;
826 #endif
829 void __init mem_init(void)
831 int codesize, datasize, initsize;
832 int i;
833 #ifndef __tilegx__
834 void *last;
835 #endif
837 #ifdef CONFIG_FLATMEM
838 BUG_ON(!mem_map);
839 #endif
841 #ifdef CONFIG_HIGHMEM
842 /* check that fixmap and pkmap do not overlap */
843 if (PKMAP_ADDR(LAST_PKMAP-1) >= FIXADDR_START) {
844 pr_err("fixmap and kmap areas overlap"
845 " - this will crash\n");
846 pr_err("pkstart: %lxh pkend: %lxh fixstart %lxh\n",
847 PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP-1),
848 FIXADDR_START);
849 BUG();
851 #endif
853 set_max_mapnr_init();
855 /* this will put all bootmem onto the freelists */
856 totalram_pages += free_all_bootmem();
858 /* count all remaining LOWMEM and give all HIGHMEM to page allocator */
859 set_non_bootmem_pages_init();
861 codesize = (unsigned long)&_etext - (unsigned long)&_text;
862 datasize = (unsigned long)&_end - (unsigned long)&_sdata;
863 initsize = (unsigned long)&_einittext - (unsigned long)&_sinittext;
864 initsize += (unsigned long)&_einitdata - (unsigned long)&_sinitdata;
866 pr_info("Memory: %luk/%luk available (%dk kernel code, %dk data, %dk init, %ldk highmem)\n",
867 (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
868 num_physpages << (PAGE_SHIFT-10),
869 codesize >> 10,
870 datasize >> 10,
871 initsize >> 10,
872 (unsigned long) (totalhigh_pages << (PAGE_SHIFT-10))
876 * In debug mode, dump some interesting memory mappings.
878 #ifdef CONFIG_HIGHMEM
879 printk(KERN_DEBUG " KMAP %#lx - %#lx\n",
880 FIXADDR_START, FIXADDR_TOP + PAGE_SIZE - 1);
881 printk(KERN_DEBUG " PKMAP %#lx - %#lx\n",
882 PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP) - 1);
883 #endif
884 #ifdef CONFIG_HUGEVMAP
885 printk(KERN_DEBUG " HUGEMAP %#lx - %#lx\n",
886 HUGE_VMAP_BASE, HUGE_VMAP_END - 1);
887 #endif
888 printk(KERN_DEBUG " VMALLOC %#lx - %#lx\n",
889 _VMALLOC_START, _VMALLOC_END - 1);
890 #ifdef __tilegx__
891 for (i = MAX_NUMNODES-1; i >= 0; --i) {
892 struct pglist_data *node = &node_data[i];
893 if (node->node_present_pages) {
894 unsigned long start = (unsigned long)
895 pfn_to_kaddr(node->node_start_pfn);
896 unsigned long end = start +
897 (node->node_present_pages << PAGE_SHIFT);
898 printk(KERN_DEBUG " MEM%d %#lx - %#lx\n",
899 i, start, end - 1);
902 #else
903 last = high_memory;
904 for (i = MAX_NUMNODES-1; i >= 0; --i) {
905 if ((unsigned long)vbase_map[i] != -1UL) {
906 printk(KERN_DEBUG " LOWMEM%d %#lx - %#lx\n",
907 i, (unsigned long) (vbase_map[i]),
908 (unsigned long) (last-1));
909 last = vbase_map[i];
912 #endif
914 #ifndef __tilegx__
916 * Convert from using one lock for all atomic operations to
917 * one per cpu.
919 __init_atomic_per_cpu();
920 #endif
924 * this is for the non-NUMA, single node SMP system case.
925 * Specifically, in the case of x86, we will always add
926 * memory to the highmem for now.
928 #ifndef CONFIG_NEED_MULTIPLE_NODES
929 int arch_add_memory(u64 start, u64 size)
931 struct pglist_data *pgdata = &contig_page_data;
932 struct zone *zone = pgdata->node_zones + MAX_NR_ZONES-1;
933 unsigned long start_pfn = start >> PAGE_SHIFT;
934 unsigned long nr_pages = size >> PAGE_SHIFT;
936 return __add_pages(zone, start_pfn, nr_pages);
939 int remove_memory(u64 start, u64 size)
941 return -EINVAL;
943 #endif
945 struct kmem_cache *pgd_cache;
947 void __init pgtable_cache_init(void)
949 pgd_cache = kmem_cache_create("pgd", SIZEOF_PGD, SIZEOF_PGD, 0, NULL);
950 if (!pgd_cache)
951 panic("pgtable_cache_init(): Cannot create pgd cache");
954 #if !CHIP_HAS_COHERENT_LOCAL_CACHE()
956 * The __w1data area holds data that is only written during initialization,
957 * and is read-only and thus freely cacheable thereafter. Fix the page
958 * table entries that cover that region accordingly.
960 static void mark_w1data_ro(void)
962 /* Loop over page table entries */
963 unsigned long addr = (unsigned long)__w1data_begin;
964 BUG_ON((addr & (PAGE_SIZE-1)) != 0);
965 for (; addr <= (unsigned long)__w1data_end - 1; addr += PAGE_SIZE) {
966 unsigned long pfn = kaddr_to_pfn((void *)addr);
967 pte_t *ptep = virt_to_pte(NULL, addr);
968 BUG_ON(pte_huge(*ptep)); /* not relevant for kdata_huge */
969 set_pte_at(&init_mm, addr, ptep, pfn_pte(pfn, PAGE_KERNEL_RO));
972 #endif
974 #ifdef CONFIG_DEBUG_PAGEALLOC
975 static long __write_once initfree;
976 #else
977 static long __write_once initfree = 1;
978 #endif
980 /* Select whether to free (1) or mark unusable (0) the __init pages. */
981 static int __init set_initfree(char *str)
983 long val;
984 if (strict_strtol(str, 0, &val) == 0) {
985 initfree = val;
986 pr_info("initfree: %s free init pages\n",
987 initfree ? "will" : "won't");
989 return 1;
991 __setup("initfree=", set_initfree);
993 static void free_init_pages(char *what, unsigned long begin, unsigned long end)
995 unsigned long addr = (unsigned long) begin;
997 if (kdata_huge && !initfree) {
998 pr_warning("Warning: ignoring initfree=0:"
999 " incompatible with kdata=huge\n");
1000 initfree = 1;
1002 end = (end + PAGE_SIZE - 1) & PAGE_MASK;
1003 local_flush_tlb_pages(NULL, begin, PAGE_SIZE, end - begin);
1004 for (addr = begin; addr < end; addr += PAGE_SIZE) {
1006 * Note we just reset the home here directly in the
1007 * page table. We know this is safe because our caller
1008 * just flushed the caches on all the other cpus,
1009 * and they won't be touching any of these pages.
1011 int pfn = kaddr_to_pfn((void *)addr);
1012 struct page *page = pfn_to_page(pfn);
1013 pte_t *ptep = virt_to_pte(NULL, addr);
1014 if (!initfree) {
1016 * If debugging page accesses then do not free
1017 * this memory but mark them not present - any
1018 * buggy init-section access will create a
1019 * kernel page fault:
1021 pte_clear(&init_mm, addr, ptep);
1022 continue;
1024 __ClearPageReserved(page);
1025 init_page_count(page);
1026 if (pte_huge(*ptep))
1027 BUG_ON(!kdata_huge);
1028 else
1029 set_pte_at(&init_mm, addr, ptep,
1030 pfn_pte(pfn, PAGE_KERNEL));
1031 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
1032 free_page(addr);
1033 totalram_pages++;
1035 pr_info("Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
1038 void free_initmem(void)
1040 const unsigned long text_delta = MEM_SV_INTRPT - PAGE_OFFSET;
1043 * Evict the dirty initdata on the boot cpu, evict the w1data
1044 * wherever it's homed, and evict all the init code everywhere.
1045 * We are guaranteed that no one will touch the init pages any
1046 * more, and although other cpus may be touching the w1data,
1047 * we only actually change the caching on tile64, which won't
1048 * be keeping local copies in the other tiles' caches anyway.
1050 homecache_evict(&cpu_cacheable_map);
1052 /* Free the data pages that we won't use again after init. */
1053 free_init_pages("unused kernel data",
1054 (unsigned long)_sinitdata,
1055 (unsigned long)_einitdata);
1058 * Free the pages mapped from 0xc0000000 that correspond to code
1059 * pages from MEM_SV_INTRPT that we won't use again after init.
1061 free_init_pages("unused kernel text",
1062 (unsigned long)_sinittext - text_delta,
1063 (unsigned long)_einittext - text_delta);
1065 #if !CHIP_HAS_COHERENT_LOCAL_CACHE()
1067 * Upgrade the .w1data section to globally cached.
1068 * We don't do this on tilepro, since the cache architecture
1069 * pretty much makes it irrelevant, and in any case we end
1070 * up having racing issues with other tiles that may touch
1071 * the data after we flush the cache but before we update
1072 * the PTEs and flush the TLBs, causing sharer shootdowns
1073 * later. Even though this is to clean data, it seems like
1074 * an unnecessary complication.
1076 mark_w1data_ro();
1077 #endif
1079 /* Do a global TLB flush so everyone sees the changes. */
1080 flush_tlb_all();