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