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