Merge tag 'mfd-3.11-1' of git://git.kernel.org/pub/scm/linux/kernel/git/sameo/mfd...
[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 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  */
120 static 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
134 static inline pmd_t *alloc_pmd(void)
135 {
136         return __alloc_bootmem(L1_KERNEL_PGTABLE_SIZE, HV_PAGE_TABLE_ALIGN, 0);
137 }
138
139 static 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. */
147 void __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__
154 static 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
162 static 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  */
179 static 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
195 static int __initdata ktext_hash = 1;  /* .text pages */
196 static int __initdata kdata_hash = 1;  /* .data and .bss pages */
197 int __write_once hash_default = 1;     /* kernel allocator pages */
198 EXPORT_SYMBOL(hash_default);
199 int __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  */
208 static __initdata struct cpumask kdata_mask;
209 static __initdata int kdata_arg_seen;
210
211 int __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. */
215 static 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  */
233 static 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
335 static int __initdata ktext_arg_seen;
336 static int __initdata ktext_small;
337 static int __initdata ktext_local;
338 static int __initdata ktext_all;
339 static int __initdata ktext_nondataplane;
340 static int __initdata ktext_nocache;
341 static struct cpumask __initdata ktext_mask;
342
343 static 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
400 early_param("ktext", setup_ktext);
401
402
403 static 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. */
415 static 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  */
432 static 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)_text);
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  */
663 int 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
673 static 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
694 static 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                 adjust_managed_page_count(page, count);
724
725                 page += count;
726                 pfn += count;
727         }
728 }
729
730 static 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  */
771 void __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  */
811 void __init set_page_homes(void)
812 {
813 }
814
815 static void __init set_max_mapnr_init(void)
816 {
817 #ifdef CONFIG_FLATMEM
818         max_mapnr = max_low_pfn;
819 #endif
820 }
821
822 void __init mem_init(void)
823 {
824         int i;
825 #ifndef __tilegx__
826         void *last;
827 #endif
828
829 #ifdef CONFIG_FLATMEM
830         BUG_ON(!mem_map);
831 #endif
832
833 #ifdef CONFIG_HIGHMEM
834         /* check that fixmap and pkmap do not overlap */
835         if (PKMAP_ADDR(LAST_PKMAP-1) >= FIXADDR_START) {
836                 pr_err("fixmap and kmap areas overlap"
837                        " - this will crash\n");
838                 pr_err("pkstart: %lxh pkend: %lxh fixstart %lxh\n",
839                        PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP-1),
840                        FIXADDR_START);
841                 BUG();
842         }
843 #endif
844
845         set_max_mapnr_init();
846
847         /* this will put all bootmem onto the freelists */
848         free_all_bootmem();
849
850 #ifndef CONFIG_64BIT
851         /* count all remaining LOWMEM and give all HIGHMEM to page allocator */
852         set_non_bootmem_pages_init();
853 #endif
854
855         mem_init_print_info(NULL);
856
857         /*
858          * In debug mode, dump some interesting memory mappings.
859          */
860 #ifdef CONFIG_HIGHMEM
861         printk(KERN_DEBUG "  KMAP    %#lx - %#lx\n",
862                FIXADDR_START, FIXADDR_TOP + PAGE_SIZE - 1);
863         printk(KERN_DEBUG "  PKMAP   %#lx - %#lx\n",
864                PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP) - 1);
865 #endif
866 #ifdef CONFIG_HUGEVMAP
867         printk(KERN_DEBUG "  HUGEMAP %#lx - %#lx\n",
868                HUGE_VMAP_BASE, HUGE_VMAP_END - 1);
869 #endif
870         printk(KERN_DEBUG "  VMALLOC %#lx - %#lx\n",
871                _VMALLOC_START, _VMALLOC_END - 1);
872 #ifdef __tilegx__
873         for (i = MAX_NUMNODES-1; i >= 0; --i) {
874                 struct pglist_data *node = &node_data[i];
875                 if (node->node_present_pages) {
876                         unsigned long start = (unsigned long)
877                                 pfn_to_kaddr(node->node_start_pfn);
878                         unsigned long end = start +
879                                 (node->node_present_pages << PAGE_SHIFT);
880                         printk(KERN_DEBUG "  MEM%d    %#lx - %#lx\n",
881                                i, start, end - 1);
882                 }
883         }
884 #else
885         last = high_memory;
886         for (i = MAX_NUMNODES-1; i >= 0; --i) {
887                 if ((unsigned long)vbase_map[i] != -1UL) {
888                         printk(KERN_DEBUG "  LOWMEM%d %#lx - %#lx\n",
889                                i, (unsigned long) (vbase_map[i]),
890                                (unsigned long) (last-1));
891                         last = vbase_map[i];
892                 }
893         }
894 #endif
895
896 #ifndef __tilegx__
897         /*
898          * Convert from using one lock for all atomic operations to
899          * one per cpu.
900          */
901         __init_atomic_per_cpu();
902 #endif
903 }
904
905 /*
906  * this is for the non-NUMA, single node SMP system case.
907  * Specifically, in the case of x86, we will always add
908  * memory to the highmem for now.
909  */
910 #ifndef CONFIG_NEED_MULTIPLE_NODES
911 int arch_add_memory(u64 start, u64 size)
912 {
913         struct pglist_data *pgdata = &contig_page_data;
914         struct zone *zone = pgdata->node_zones + MAX_NR_ZONES-1;
915         unsigned long start_pfn = start >> PAGE_SHIFT;
916         unsigned long nr_pages = size >> PAGE_SHIFT;
917
918         return __add_pages(zone, start_pfn, nr_pages);
919 }
920
921 int remove_memory(u64 start, u64 size)
922 {
923         return -EINVAL;
924 }
925
926 #ifdef CONFIG_MEMORY_HOTREMOVE
927 int arch_remove_memory(u64 start, u64 size)
928 {
929         /* TODO */
930         return -EBUSY;
931 }
932 #endif
933 #endif
934
935 struct kmem_cache *pgd_cache;
936
937 void __init pgtable_cache_init(void)
938 {
939         pgd_cache = kmem_cache_create("pgd", SIZEOF_PGD, SIZEOF_PGD, 0, NULL);
940         if (!pgd_cache)
941                 panic("pgtable_cache_init(): Cannot create pgd cache");
942 }
943
944 #if !CHIP_HAS_COHERENT_LOCAL_CACHE()
945 /*
946  * The __w1data area holds data that is only written during initialization,
947  * and is read-only and thus freely cacheable thereafter.  Fix the page
948  * table entries that cover that region accordingly.
949  */
950 static void mark_w1data_ro(void)
951 {
952         /* Loop over page table entries */
953         unsigned long addr = (unsigned long)__w1data_begin;
954         BUG_ON((addr & (PAGE_SIZE-1)) != 0);
955         for (; addr <= (unsigned long)__w1data_end - 1; addr += PAGE_SIZE) {
956                 unsigned long pfn = kaddr_to_pfn((void *)addr);
957                 pte_t *ptep = virt_to_pte(NULL, addr);
958                 BUG_ON(pte_huge(*ptep));   /* not relevant for kdata_huge */
959                 set_pte_at(&init_mm, addr, ptep, pfn_pte(pfn, PAGE_KERNEL_RO));
960         }
961 }
962 #endif
963
964 #ifdef CONFIG_DEBUG_PAGEALLOC
965 static long __write_once initfree;
966 #else
967 static long __write_once initfree = 1;
968 #endif
969
970 /* Select whether to free (1) or mark unusable (0) the __init pages. */
971 static int __init set_initfree(char *str)
972 {
973         long val;
974         if (strict_strtol(str, 0, &val) == 0) {
975                 initfree = val;
976                 pr_info("initfree: %s free init pages\n",
977                         initfree ? "will" : "won't");
978         }
979         return 1;
980 }
981 __setup("initfree=", set_initfree);
982
983 static void free_init_pages(char *what, unsigned long begin, unsigned long end)
984 {
985         unsigned long addr = (unsigned long) begin;
986
987         if (kdata_huge && !initfree) {
988                 pr_warning("Warning: ignoring initfree=0:"
989                            " incompatible with kdata=huge\n");
990                 initfree = 1;
991         }
992         end = (end + PAGE_SIZE - 1) & PAGE_MASK;
993         local_flush_tlb_pages(NULL, begin, PAGE_SIZE, end - begin);
994         for (addr = begin; addr < end; addr += PAGE_SIZE) {
995                 /*
996                  * Note we just reset the home here directly in the
997                  * page table.  We know this is safe because our caller
998                  * just flushed the caches on all the other cpus,
999                  * and they won't be touching any of these pages.
1000                  */
1001                 int pfn = kaddr_to_pfn((void *)addr);
1002                 struct page *page = pfn_to_page(pfn);
1003                 pte_t *ptep = virt_to_pte(NULL, addr);
1004                 if (!initfree) {
1005                         /*
1006                          * If debugging page accesses then do not free
1007                          * this memory but mark them not present - any
1008                          * buggy init-section access will create a
1009                          * kernel page fault:
1010                          */
1011                         pte_clear(&init_mm, addr, ptep);
1012                         continue;
1013                 }
1014                 if (pte_huge(*ptep))
1015                         BUG_ON(!kdata_huge);
1016                 else
1017                         set_pte_at(&init_mm, addr, ptep,
1018                                    pfn_pte(pfn, PAGE_KERNEL));
1019                 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
1020                 free_reserved_page(page);
1021         }
1022         pr_info("Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
1023 }
1024
1025 void free_initmem(void)
1026 {
1027         const unsigned long text_delta = MEM_SV_INTRPT - PAGE_OFFSET;
1028
1029         /*
1030          * Evict the dirty initdata on the boot cpu, evict the w1data
1031          * wherever it's homed, and evict all the init code everywhere.
1032          * We are guaranteed that no one will touch the init pages any
1033          * more, and although other cpus may be touching the w1data,
1034          * we only actually change the caching on tile64, which won't
1035          * be keeping local copies in the other tiles' caches anyway.
1036          */
1037         homecache_evict(&cpu_cacheable_map);
1038
1039         /* Free the data pages that we won't use again after init. */
1040         free_init_pages("unused kernel data",
1041                         (unsigned long)_sinitdata,
1042                         (unsigned long)_einitdata);
1043
1044         /*
1045          * Free the pages mapped from 0xc0000000 that correspond to code
1046          * pages from MEM_SV_INTRPT that we won't use again after init.
1047          */
1048         free_init_pages("unused kernel text",
1049                         (unsigned long)_sinittext - text_delta,
1050                         (unsigned long)_einittext - text_delta);
1051
1052 #if !CHIP_HAS_COHERENT_LOCAL_CACHE()
1053         /*
1054          * Upgrade the .w1data section to globally cached.
1055          * We don't do this on tilepro, since the cache architecture
1056          * pretty much makes it irrelevant, and in any case we end
1057          * up having racing issues with other tiles that may touch
1058          * the data after we flush the cache but before we update
1059          * the PTEs and flush the TLBs, causing sharer shootdowns
1060          * later.  Even though this is to clean data, it seems like
1061          * an unnecessary complication.
1062          */
1063         mark_w1data_ro();
1064 #endif
1065
1066         /* Do a global TLB flush so everyone sees the changes. */
1067         flush_tlb_all();
1068 }