blob: 3bfa1275e3336b96b731b2ad7a671e9fe8c04fd2 [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{
Chris Metcalfd5d14ed2012-03-29 13:58:43 -040085 BUG_ON(pages & (PTRS_PER_PTE - 1));
Chris Metcalf867e3592010-05-28 23:09:12 -040086 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
Chris Metcalf867e3592010-05-28 23:09:12 -0400134static inline pmd_t *alloc_pmd(void)
135{
Chris Metcalfd5d14ed2012-03-29 13:58:43 -0400136 return __alloc_bootmem(L1_KERNEL_PGTABLE_SIZE, HV_PAGE_TABLE_ALIGN, 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400137}
138
139static inline void assign_pmd(pud_t *pud, pmd_t *pmd)
140{
141 assign_pte((pmd_t *)pud, (pte_t *)pmd);
142}
143
144#endif /* __tilegx__ */
145
146/* Replace the given pmd with a full PTE table. */
147void __init shatter_pmd(pmd_t *pmd)
148{
149 pte_t *pte = get_prealloc_pte(pte_pfn(*(pte_t *)pmd));
150 assign_pte(pmd, pte);
151}
152
Chris Metcalfbbaa22c2012-06-13 14:46:40 -0400153#ifdef __tilegx__
154static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
155{
156 pud_t *pud = pud_offset(&pgtables[pgd_index(va)], va);
157 if (pud_none(*pud))
158 assign_pmd(pud, alloc_pmd());
159 return pmd_offset(pud, va);
160}
161#else
162static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
163{
164 return pmd_offset(pud_offset(&pgtables[pgd_index(va)], va), va);
165}
166#endif
167
Chris Metcalf867e3592010-05-28 23:09:12 -0400168/*
169 * This function initializes a certain range of kernel virtual memory
170 * with new bootmem page tables, everywhere page tables are missing in
171 * the given range.
172 */
173
174/*
175 * NOTE: The pagetables are allocated contiguous on the physical space
176 * so we can cache the place of the first one and move around without
177 * checking the pgd every time.
178 */
179static void __init page_table_range_init(unsigned long start,
Chris Metcalfbbaa22c2012-06-13 14:46:40 -0400180 unsigned long end, pgd_t *pgd)
Chris Metcalf867e3592010-05-28 23:09:12 -0400181{
Chris Metcalf867e3592010-05-28 23:09:12 -0400182 unsigned long vaddr;
Chris Metcalfbbaa22c2012-06-13 14:46:40 -0400183 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);
Chris Metcalf867e3592010-05-28 23:09:12 -0400187 if (pmd_none(*pmd))
188 assign_pte(pmd, alloc_pte());
Chris Metcalf867e3592010-05-28 23:09:12 -0400189 }
190}
Chris Metcalf867e3592010-05-28 23:09:12 -0400191
192
193#if CHIP_HAS_CBOX_HOME_MAP()
194
195static int __initdata ktext_hash = 1; /* .text pages */
196static int __initdata kdata_hash = 1; /* .data and .bss pages */
197int __write_once hash_default = 1; /* kernel allocator pages */
198EXPORT_SYMBOL(hash_default);
199int __write_once kstack_hash = 1; /* if no homecaching, use h4h */
200#endif /* CHIP_HAS_CBOX_HOME_MAP */
201
202/*
203 * CPUs to use to for striping the pages of kernel data. If hash-for-home
204 * is available, this is only relevant if kcache_hash sets up the
205 * .data and .bss to be page-homed, and we don't want the default mode
206 * of using the full set of kernel cpus for the striping.
207 */
208static __initdata struct cpumask kdata_mask;
209static __initdata int kdata_arg_seen;
210
211int __write_once kdata_huge; /* if no homecaching, small pages */
212
213
214/* Combine a generic pgprot_t with cache home to get a cache-aware pgprot. */
215static pgprot_t __init construct_pgprot(pgprot_t prot, int home)
216{
217 prot = pte_set_home(prot, home);
218#if CHIP_HAS_CBOX_HOME_MAP()
219 if (home == PAGE_HOME_IMMUTABLE) {
220 if (ktext_hash)
221 prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_HASH_L3);
222 else
223 prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_NO_L3);
224 }
225#endif
226 return prot;
227}
228
229/*
230 * For a given kernel data VA, how should it be cached?
231 * We return the complete pgprot_t with caching bits set.
232 */
233static pgprot_t __init init_pgprot(ulong address)
234{
235 int cpu;
236 unsigned long page;
237 enum { CODE_DELTA = MEM_SV_INTRPT - PAGE_OFFSET };
238
239#if CHIP_HAS_CBOX_HOME_MAP()
240 /* For kdata=huge, everything is just hash-for-home. */
241 if (kdata_huge)
242 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
243#endif
244
245 /* We map the aliased pages of permanent text inaccessible. */
246 if (address < (ulong) _sinittext - CODE_DELTA)
247 return PAGE_NONE;
248
249 /*
250 * We map read-only data non-coherent for performance. We could
251 * use neighborhood caching on TILE64, but it's not clear it's a win.
252 */
253 if ((address >= (ulong) __start_rodata &&
254 address < (ulong) __end_rodata) ||
255 address == (ulong) empty_zero_page) {
256 return construct_pgprot(PAGE_KERNEL_RO, PAGE_HOME_IMMUTABLE);
257 }
258
Chris Metcalf867e3592010-05-28 23:09:12 -0400259#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
Chris Metcalf0707ad32010-06-25 17:04:17 -0400270 * 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).
Chris Metcalf867e3592010-05-28 23:09:12 -0400273 *
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 */
Chris Metcalf0707ad32010-06-25 17:04:17 -0400280 if (address >= (ulong) _end || address < (ulong) _einitdata)
Chris Metcalf867e3592010-05-28 23:09:12 -0400281 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 /*
Chris Metcalf0707ad32010-06-25 17:04:17 -0400290 * 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 /*
Chris Metcalf867e3592010-05-28 23:09:12 -0400298 * 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 */
Chris Metcalf0707ad32010-06-25 17:04:17 -0400304 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;
Chris Metcalf867e3592010-05-28 23:09:12 -0400311 if (page == (ulong)empty_zero_page)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400312 continue;
Chris Metcalf867e3592010-05-28 23:09:12 -0400313#ifndef __tilegx__
314#if !ATOMIC_LOCKS_FOUND_VIA_TABLE()
315 if (page == (ulong)atomic_locks)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400316 continue;
Chris Metcalf867e3592010-05-28 23:09:12 -0400317#endif
318#endif
Chris Metcalf0707ad32010-06-25 17:04:17 -0400319 cpu = cpumask_next(cpu, &kdata_mask);
320 if (cpu == NR_CPUS)
321 cpu = cpumask_first(&kdata_mask);
Chris Metcalf867e3592010-05-28 23:09:12 -0400322 }
323 return construct_pgprot(PAGE_KERNEL, cpu);
324}
325
326/*
327 * This function sets up how we cache the kernel text. If we have
328 * hash-for-home support, normally that is used instead (see the
329 * kcache_hash boot flag for more information). But if we end up
330 * using a page-based caching technique, this option sets up the
331 * details of that. In addition, the "ktext=nocache" option may
332 * always be used to disable local caching of text pages, if desired.
333 */
334
335static int __initdata ktext_arg_seen;
336static int __initdata ktext_small;
337static int __initdata ktext_local;
338static int __initdata ktext_all;
339static int __initdata ktext_nondataplane;
340static int __initdata ktext_nocache;
341static struct cpumask __initdata ktext_mask;
342
343static int __init setup_ktext(char *str)
344{
345 if (str == NULL)
346 return -EINVAL;
347
348 /* If you have a leading "nocache", turn off ktext caching */
349 if (strncmp(str, "nocache", 7) == 0) {
350 ktext_nocache = 1;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400351 pr_info("ktext: disabling local caching of kernel text\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400352 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)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400363 pr_info("ktext: using one huge locally cached page\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400364
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;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400369 pr_info("ktext: using small pages with local caching\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400370 }
371
372 /* Neighborhood cache ktext pages on all cpus. */
373 else if (strcmp(str, "all") == 0) {
374 ktext_small = 1;
375 ktext_all = 1;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400376 pr_info("ktext: using maximal caching neighborhood\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400377 }
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;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400386 pr_info("ktext: using caching neighborhood %s "
Chris Metcalf867e3592010-05-28 23:09:12 -0400387 "with small pages\n", buf);
388 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400389 pr_info("ktext: caching on cpu %s with one huge page\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400390 buf);
391 }
392 }
393
394 else if (*str)
395 return -EINVAL;
396
397 return 0;
398}
399
400early_param("ktext", setup_ktext);
401
402
403static inline pgprot_t ktext_set_nocache(pgprot_t prot)
404{
405 if (!ktext_nocache)
406 prot = hv_pte_set_nc(prot);
407#if CHIP_HAS_NC_AND_NOALLOC_BITS()
408 else
409 prot = hv_pte_set_no_alloc_l2(prot);
410#endif
411 return prot;
412}
413
Chris Metcalf867e3592010-05-28 23:09:12 -0400414/* Temporary page table we use for staging. */
415static pgd_t pgtables[PTRS_PER_PGD]
Chris Metcalf2cb82402011-02-27 18:52:24 -0500416 __attribute__((aligned(HV_PAGE_TABLE_ALIGN)));
Chris Metcalf867e3592010-05-28 23:09:12 -0400417
418/*
419 * This maps the physical memory to kernel virtual address space, a total
420 * of max_low_pfn pages, by creating page tables starting from address
421 * PAGE_OFFSET.
422 *
423 * This routine transitions us from using a set of compiled-in large
424 * pages to using some more precise caching, including removing access
425 * to code pages mapped at PAGE_OFFSET (executed only at MEM_SV_START)
426 * marking read-only data as locally cacheable, striping the remaining
427 * .data and .bss across all the available tiles, and removing access
428 * to pages above the top of RAM (thus ensuring a page fault from a bad
429 * virtual address rather than a hypervisor shoot down for accessing
430 * memory outside the assigned limits).
431 */
432static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
433{
Chris Metcalf51007002012-03-27 15:40:20 -0400434 unsigned long long irqmask;
Chris Metcalf867e3592010-05-28 23:09:12 -0400435 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) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400445 pr_warning("warning: \"ktext\" boot argument ignored"
446 " if \"kcache_hash\" sets up text hash-for-home\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400447 ktext_small = 0;
448 }
449
450 if (kdata_arg_seen && kdata_hash) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400451 pr_warning("warning: \"kdata\" boot argument ignored"
452 " if \"kcache_hash\" sets up data hash-for-home\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400453 }
454
455 if (kdata_huge && !hash_default) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400456 pr_warning("warning: disabling \"kdata=huge\"; requires"
457 " kcache_hash=all or =allbutstack\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400458 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);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400531 pr_info("ktext: not using unavailable cpus %s\n", buf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400532 }
533 if (cpumask_empty(&ktext_mask)) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400534 pr_warning("ktext: no valid cpus; caching on %d.\n",
535 smp_processor_id());
Chris Metcalf867e3592010-05-28 23:09:12 -0400536 cpumask_copy(&ktext_mask,
537 cpumask_of(smp_processor_id()));
538 }
539 }
540
541 address = MEM_SV_INTRPT;
542 pmd = get_pmd(pgtables, address);
Chris Metcalf7a7039e2012-03-29 15:42:27 -0400543 pfn = 0; /* code starts at PA 0 */
Chris Metcalf867e3592010-05-28 23:09:12 -0400544 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
Jiang Liu40a3b8d2013-07-03 15:03:39 -0700565 BUG_ON(address != (unsigned long)_text);
Chris Metcalf7a7039e2012-03-29 15:42:27 -0400566 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 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400575 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 }
Chris Metcalf7a7039e2012-03-29 15:42:27 -0400583 if (pte)
584 assign_pte(pmd, pte);
Chris Metcalf867e3592010-05-28 23:09:12 -0400585 } 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);
Chris Metcalf7a7039e2012-03-29 15:42:27 -0400607 for (; address < (unsigned long)_einittext;
608 pfn += PFN_DOWN(HPAGE_SIZE), address += HPAGE_SIZE)
609 *(pte_t *)(pmd++) = pfn_pte(pfn, pteval);
Chris Metcalf867e3592010-05-28 23:09:12 -0400610 }
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 */
Chris Metcalf51007002012-03-27 15:40:20 -0400624 irqmask = interrupt_mask_save_mask();
625 interrupt_mask_set_mask(-1ULL);
Chris Metcalf867e3592010-05-28 23:09:12 -0400626 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));
Chris Metcalf51007002012-03-27 15:40:20 -0400630 interrupt_mask_restore_mask(irqmask);
Chris Metcalf867e3592010-05-28 23:09:12 -0400631 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);
Chris Metcalf401586e2011-02-28 15:01:53 -0500637
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);
Chris Metcalf867e3592010-05-28 23:09:12 -0400648}
649
650/*
651 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
652 * is valid. The argument is a physical page number.
653 *
654 * On Tile, the only valid things for which we can just hand out unchecked
655 * PTEs are the kernel code and data. Anything else might change its
656 * homing with time, and we wouldn't know to adjust the /dev/mem PTEs.
657 * Note that init_thread_union is released to heap soon after boot,
658 * so we include it in the init data.
659 *
660 * For TILE-Gx, we might want to consider allowing access to PA
661 * regions corresponding to PCI space, etc.
662 */
663int devmem_is_allowed(unsigned long pagenr)
664{
665 return pagenr < kaddr_to_pfn(_end) &&
666 !(pagenr >= kaddr_to_pfn(&init_thread_union) ||
667 pagenr < kaddr_to_pfn(_einitdata)) &&
668 !(pagenr >= kaddr_to_pfn(_sinittext) ||
669 pagenr <= kaddr_to_pfn(_einittext-1));
670}
671
672#ifdef CONFIG_HIGHMEM
673static void __init permanent_kmaps_init(pgd_t *pgd_base)
674{
675 pgd_t *pgd;
676 pud_t *pud;
677 pmd_t *pmd;
678 pte_t *pte;
679 unsigned long vaddr;
680
681 vaddr = PKMAP_BASE;
682 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
683
684 pgd = swapper_pg_dir + pgd_index(vaddr);
685 pud = pud_offset(pgd, vaddr);
686 pmd = pmd_offset(pud, vaddr);
687 pte = pte_offset_kernel(pmd, vaddr);
688 pkmap_page_table = pte;
689}
690#endif /* CONFIG_HIGHMEM */
691
692
Chris Metcalf621b1952012-04-01 14:04:21 -0400693#ifndef CONFIG_64BIT
Chris Metcalf867e3592010-05-28 23:09:12 -0400694static 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);
Jiang Liuabd1b6d2013-07-03 15:03:01 -0700723 adjust_managed_page_count(page, count);
Chris Metcalf867e3592010-05-28 23:09:12 -0400724
725 page += count;
726 pfn += count;
727 }
728}
729
730static void __init set_non_bootmem_pages_init(void)
731{
732 struct zone *z;
733 for_each_zone(z) {
734 unsigned long start, end;
735 int nid = z->zone_pgdat->node_id;
Chris Metcalfeef015c2012-05-09 12:26:30 -0400736#ifdef CONFIG_HIGHMEM
Chris Metcalf0707ad32010-06-25 17:04:17 -0400737 int idx = zone_idx(z);
Chris Metcalfeef015c2012-05-09 12:26:30 -0400738#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400739
740 start = z->zone_start_pfn;
Chris Metcalf867e3592010-05-28 23:09:12 -0400741 end = start + z->spanned_pages;
Chris Metcalfeef015c2012-05-09 12:26:30 -0400742 start = max(start, node_free_pfn[nid]);
743 start = max(start, max_low_pfn);
744
Chris Metcalf867e3592010-05-28 23:09:12 -0400745#ifdef CONFIG_HIGHMEM
Chris Metcalf0707ad32010-06-25 17:04:17 -0400746 if (idx == ZONE_HIGHMEM)
Chris Metcalf867e3592010-05-28 23:09:12 -0400747 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}
Chris Metcalf621b1952012-04-01 14:04:21 -0400765#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400766
767/*
768 * paging_init() sets up the page tables - note that all of lowmem is
769 * already mapped by head.S.
770 */
771void __init paging_init(void)
772{
Chris Metcalf867e3592010-05-28 23:09:12 -0400773#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
Chris Metcalf084fe6a2013-08-09 16:17:03 -0400780 /* Fixed mappings, only the page table structure has to be created. */
Chris Metcalfbbaa22c2012-06-13 14:46:40 -0400781 page_table_range_init(fix_to_virt(__end_of_fixed_addresses - 1),
782 FIXADDR_TOP, pgd_base);
783
784#ifdef CONFIG_HIGHMEM
Chris Metcalf867e3592010-05-28 23:09:12 -0400785 permanent_kmaps_init(pgd_base);
786#endif
787
788#ifdef __tilegx__
789 /*
790 * Since GX allocates just one pmd_t array worth of vmalloc space,
791 * we go ahead and allocate it statically here, then share it
792 * globally. As a result we don't have to worry about any task
793 * changing init_mm once we get up and running, and there's no
794 * need for e.g. vmalloc_sync_all().
795 */
Chris Metcalfd5d14ed2012-03-29 13:58:43 -0400796 BUILD_BUG_ON(pgd_index(VMALLOC_START) != pgd_index(VMALLOC_END - 1));
Chris Metcalf867e3592010-05-28 23:09:12 -0400797 pud = pud_offset(pgd_base + pgd_index(VMALLOC_START), VMALLOC_START);
798 assign_pmd(pud, alloc_pmd());
799#endif
800}
801
802
803/*
804 * Walk the kernel page tables and derive the page_home() from
805 * the PTEs, so that set_pte() can properly validate the caching
806 * of all PTEs it sees.
807 */
808void __init set_page_homes(void)
809{
810}
811
812static void __init set_max_mapnr_init(void)
813{
814#ifdef CONFIG_FLATMEM
815 max_mapnr = max_low_pfn;
816#endif
817}
818
819void __init mem_init(void)
820{
Chris Metcalf867e3592010-05-28 23:09:12 -0400821 int i;
822#ifndef __tilegx__
823 void *last;
824#endif
825
826#ifdef CONFIG_FLATMEM
Julia Lawalld1afa652011-08-02 12:35:04 +0200827 BUG_ON(!mem_map);
Chris Metcalf867e3592010-05-28 23:09:12 -0400828#endif
829
830#ifdef CONFIG_HIGHMEM
831 /* check that fixmap and pkmap do not overlap */
832 if (PKMAP_ADDR(LAST_PKMAP-1) >= FIXADDR_START) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400833 pr_err("fixmap and kmap areas overlap"
Chris Metcalf867e3592010-05-28 23:09:12 -0400834 " - this will crash\n");
Chris Metcalf0707ad32010-06-25 17:04:17 -0400835 pr_err("pkstart: %lxh pkend: %lxh fixstart %lxh\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400836 PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP-1),
837 FIXADDR_START);
838 BUG();
839 }
840#endif
841
842 set_max_mapnr_init();
843
844 /* this will put all bootmem onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -0700845 free_all_bootmem();
Chris Metcalf867e3592010-05-28 23:09:12 -0400846
Chris Metcalf621b1952012-04-01 14:04:21 -0400847#ifndef CONFIG_64BIT
Chris Metcalf867e3592010-05-28 23:09:12 -0400848 /* count all remaining LOWMEM and give all HIGHMEM to page allocator */
849 set_non_bootmem_pages_init();
Chris Metcalf621b1952012-04-01 14:04:21 -0400850#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400851
Jiang Liu3f29c332013-07-03 15:04:15 -0700852 mem_init_print_info(NULL);
Chris Metcalf867e3592010-05-28 23:09:12 -0400853
854 /*
855 * In debug mode, dump some interesting memory mappings.
856 */
857#ifdef CONFIG_HIGHMEM
858 printk(KERN_DEBUG " KMAP %#lx - %#lx\n",
859 FIXADDR_START, FIXADDR_TOP + PAGE_SIZE - 1);
860 printk(KERN_DEBUG " PKMAP %#lx - %#lx\n",
861 PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP) - 1);
862#endif
863#ifdef CONFIG_HUGEVMAP
864 printk(KERN_DEBUG " HUGEMAP %#lx - %#lx\n",
865 HUGE_VMAP_BASE, HUGE_VMAP_END - 1);
866#endif
867 printk(KERN_DEBUG " VMALLOC %#lx - %#lx\n",
868 _VMALLOC_START, _VMALLOC_END - 1);
869#ifdef __tilegx__
870 for (i = MAX_NUMNODES-1; i >= 0; --i) {
871 struct pglist_data *node = &node_data[i];
872 if (node->node_present_pages) {
873 unsigned long start = (unsigned long)
874 pfn_to_kaddr(node->node_start_pfn);
875 unsigned long end = start +
876 (node->node_present_pages << PAGE_SHIFT);
877 printk(KERN_DEBUG " MEM%d %#lx - %#lx\n",
878 i, start, end - 1);
879 }
880 }
881#else
882 last = high_memory;
883 for (i = MAX_NUMNODES-1; i >= 0; --i) {
884 if ((unsigned long)vbase_map[i] != -1UL) {
885 printk(KERN_DEBUG " LOWMEM%d %#lx - %#lx\n",
886 i, (unsigned long) (vbase_map[i]),
887 (unsigned long) (last-1));
888 last = vbase_map[i];
889 }
890 }
891#endif
892
893#ifndef __tilegx__
894 /*
895 * Convert from using one lock for all atomic operations to
896 * one per cpu.
897 */
898 __init_atomic_per_cpu();
899#endif
900}
901
902/*
903 * this is for the non-NUMA, single node SMP system case.
904 * Specifically, in the case of x86, we will always add
905 * memory to the highmem for now.
906 */
907#ifndef CONFIG_NEED_MULTIPLE_NODES
908int arch_add_memory(u64 start, u64 size)
909{
910 struct pglist_data *pgdata = &contig_page_data;
911 struct zone *zone = pgdata->node_zones + MAX_NR_ZONES-1;
912 unsigned long start_pfn = start >> PAGE_SHIFT;
913 unsigned long nr_pages = size >> PAGE_SHIFT;
914
915 return __add_pages(zone, start_pfn, nr_pages);
916}
917
918int remove_memory(u64 start, u64 size)
919{
920 return -EINVAL;
921}
Wen Congyang24d335c2013-02-22 16:32:58 -0800922
923#ifdef CONFIG_MEMORY_HOTREMOVE
924int arch_remove_memory(u64 start, u64 size)
925{
926 /* TODO */
927 return -EBUSY;
928}
929#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400930#endif
931
932struct kmem_cache *pgd_cache;
933
934void __init pgtable_cache_init(void)
935{
Chris Metcalf76c567f2011-02-28 16:37:34 -0500936 pgd_cache = kmem_cache_create("pgd", SIZEOF_PGD, SIZEOF_PGD, 0, NULL);
Chris Metcalf867e3592010-05-28 23:09:12 -0400937 if (!pgd_cache)
938 panic("pgtable_cache_init(): Cannot create pgd cache");
939}
940
941#if !CHIP_HAS_COHERENT_LOCAL_CACHE()
942/*
943 * The __w1data area holds data that is only written during initialization,
944 * and is read-only and thus freely cacheable thereafter. Fix the page
945 * table entries that cover that region accordingly.
946 */
947static void mark_w1data_ro(void)
948{
949 /* Loop over page table entries */
950 unsigned long addr = (unsigned long)__w1data_begin;
951 BUG_ON((addr & (PAGE_SIZE-1)) != 0);
952 for (; addr <= (unsigned long)__w1data_end - 1; addr += PAGE_SIZE) {
953 unsigned long pfn = kaddr_to_pfn((void *)addr);
Chris Metcalf867e3592010-05-28 23:09:12 -0400954 pte_t *ptep = virt_to_pte(NULL, addr);
955 BUG_ON(pte_huge(*ptep)); /* not relevant for kdata_huge */
956 set_pte_at(&init_mm, addr, ptep, pfn_pte(pfn, PAGE_KERNEL_RO));
957 }
958}
959#endif
960
961#ifdef CONFIG_DEBUG_PAGEALLOC
962static long __write_once initfree;
963#else
964static long __write_once initfree = 1;
965#endif
966
967/* Select whether to free (1) or mark unusable (0) the __init pages. */
968static int __init set_initfree(char *str)
969{
Chris Metcalfd59e6092010-11-01 15:25:16 -0400970 long val;
Chris Metcalfed54d382011-02-28 15:14:19 -0500971 if (strict_strtol(str, 0, &val) == 0) {
Chris Metcalfd59e6092010-11-01 15:25:16 -0400972 initfree = val;
973 pr_info("initfree: %s free init pages\n",
974 initfree ? "will" : "won't");
975 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400976 return 1;
977}
978__setup("initfree=", set_initfree);
979
980static void free_init_pages(char *what, unsigned long begin, unsigned long end)
981{
982 unsigned long addr = (unsigned long) begin;
983
984 if (kdata_huge && !initfree) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400985 pr_warning("Warning: ignoring initfree=0:"
986 " incompatible with kdata=huge\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400987 initfree = 1;
988 }
989 end = (end + PAGE_SIZE - 1) & PAGE_MASK;
990 local_flush_tlb_pages(NULL, begin, PAGE_SIZE, end - begin);
991 for (addr = begin; addr < end; addr += PAGE_SIZE) {
992 /*
993 * Note we just reset the home here directly in the
994 * page table. We know this is safe because our caller
995 * just flushed the caches on all the other cpus,
996 * and they won't be touching any of these pages.
997 */
998 int pfn = kaddr_to_pfn((void *)addr);
999 struct page *page = pfn_to_page(pfn);
1000 pte_t *ptep = virt_to_pte(NULL, addr);
1001 if (!initfree) {
1002 /*
1003 * If debugging page accesses then do not free
1004 * this memory but mark them not present - any
1005 * buggy init-section access will create a
1006 * kernel page fault:
1007 */
1008 pte_clear(&init_mm, addr, ptep);
1009 continue;
1010 }
Chris Metcalf867e3592010-05-28 23:09:12 -04001011 if (pte_huge(*ptep))
1012 BUG_ON(!kdata_huge);
1013 else
1014 set_pte_at(&init_mm, addr, ptep,
1015 pfn_pte(pfn, PAGE_KERNEL));
1016 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Jiang Liuabd1b6d2013-07-03 15:03:01 -07001017 free_reserved_page(page);
Chris Metcalf867e3592010-05-28 23:09:12 -04001018 }
Chris Metcalf0707ad32010-06-25 17:04:17 -04001019 pr_info("Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
Chris Metcalf867e3592010-05-28 23:09:12 -04001020}
1021
1022void free_initmem(void)
1023{
1024 const unsigned long text_delta = MEM_SV_INTRPT - PAGE_OFFSET;
1025
1026 /*
1027 * Evict the dirty initdata on the boot cpu, evict the w1data
1028 * wherever it's homed, and evict all the init code everywhere.
1029 * We are guaranteed that no one will touch the init pages any
1030 * more, and although other cpus may be touching the w1data,
1031 * we only actually change the caching on tile64, which won't
1032 * be keeping local copies in the other tiles' caches anyway.
1033 */
1034 homecache_evict(&cpu_cacheable_map);
1035
1036 /* Free the data pages that we won't use again after init. */
1037 free_init_pages("unused kernel data",
1038 (unsigned long)_sinitdata,
1039 (unsigned long)_einitdata);
1040
1041 /*
1042 * Free the pages mapped from 0xc0000000 that correspond to code
Chris Metcalfa78c9422010-10-14 16:23:03 -04001043 * pages from MEM_SV_INTRPT that we won't use again after init.
Chris Metcalf867e3592010-05-28 23:09:12 -04001044 */
1045 free_init_pages("unused kernel text",
1046 (unsigned long)_sinittext - text_delta,
1047 (unsigned long)_einittext - text_delta);
1048
1049#if !CHIP_HAS_COHERENT_LOCAL_CACHE()
1050 /*
1051 * Upgrade the .w1data section to globally cached.
1052 * We don't do this on tilepro, since the cache architecture
1053 * pretty much makes it irrelevant, and in any case we end
1054 * up having racing issues with other tiles that may touch
1055 * the data after we flush the cache but before we update
1056 * the PTEs and flush the TLBs, causing sharer shootdowns
1057 * later. Even though this is to clean data, it seems like
1058 * an unnecessary complication.
1059 */
1060 mark_w1data_ro();
1061#endif
1062
1063 /* Do a global TLB flush so everyone sees the changes. */
1064 flush_tlb_all();
1065}