blob: be240cc4978db1840861a5fed2329c468bb629f1 [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{
Chris Metcalf867e3592010-05-28 23:09:12 -0400109 if (hash_default)
110 return PAGE_HOME_HASH;
Chris Metcalf867e3592010-05-28 23:09:12 -0400111 return smp_processor_id();
112}
113
114/*
115 * Place a pointer to an L2 page table in a middle page
116 * directory entry.
117 */
118static void __init assign_pte(pmd_t *pmd, pte_t *page_table)
119{
120 phys_addr_t pa = __pa(page_table);
121 unsigned long l2_ptfn = pa >> HV_LOG2_PAGE_TABLE_ALIGN;
122 pte_t pteval = hv_pte_set_ptfn(__pgprot(_PAGE_TABLE), l2_ptfn);
123 BUG_ON((pa & (HV_PAGE_TABLE_ALIGN-1)) != 0);
124 pteval = pte_set_home(pteval, initial_heap_home());
125 *(pte_t *)pmd = pteval;
126 if (page_table != (pte_t *)pmd_page_vaddr(*pmd))
127 BUG();
128}
129
130#ifdef __tilegx__
131
Chris Metcalf867e3592010-05-28 23:09:12 -0400132static inline pmd_t *alloc_pmd(void)
133{
Chris Metcalfd5d14ed2012-03-29 13:58:43 -0400134 return __alloc_bootmem(L1_KERNEL_PGTABLE_SIZE, HV_PAGE_TABLE_ALIGN, 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400135}
136
137static inline void assign_pmd(pud_t *pud, pmd_t *pmd)
138{
139 assign_pte((pmd_t *)pud, (pte_t *)pmd);
140}
141
142#endif /* __tilegx__ */
143
144/* Replace the given pmd with a full PTE table. */
145void __init shatter_pmd(pmd_t *pmd)
146{
147 pte_t *pte = get_prealloc_pte(pte_pfn(*(pte_t *)pmd));
148 assign_pte(pmd, pte);
149}
150
Chris Metcalfbbaa22c2012-06-13 14:46:40 -0400151#ifdef __tilegx__
152static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
153{
154 pud_t *pud = pud_offset(&pgtables[pgd_index(va)], va);
155 if (pud_none(*pud))
156 assign_pmd(pud, alloc_pmd());
157 return pmd_offset(pud, va);
158}
159#else
160static pmd_t *__init get_pmd(pgd_t pgtables[], unsigned long va)
161{
162 return pmd_offset(pud_offset(&pgtables[pgd_index(va)], va), va);
163}
164#endif
165
Chris Metcalf867e3592010-05-28 23:09:12 -0400166/*
167 * This function initializes a certain range of kernel virtual memory
168 * with new bootmem page tables, everywhere page tables are missing in
169 * the given range.
170 */
171
172/*
173 * NOTE: The pagetables are allocated contiguous on the physical space
174 * so we can cache the place of the first one and move around without
175 * checking the pgd every time.
176 */
177static void __init page_table_range_init(unsigned long start,
Chris Metcalfbbaa22c2012-06-13 14:46:40 -0400178 unsigned long end, pgd_t *pgd)
Chris Metcalf867e3592010-05-28 23:09:12 -0400179{
Chris Metcalf867e3592010-05-28 23:09:12 -0400180 unsigned long vaddr;
Chris Metcalfbbaa22c2012-06-13 14:46:40 -0400181 start = round_down(start, PMD_SIZE);
182 end = round_up(end, PMD_SIZE);
183 for (vaddr = start; vaddr < end; vaddr += PMD_SIZE) {
184 pmd_t *pmd = get_pmd(pgd, vaddr);
Chris Metcalf867e3592010-05-28 23:09:12 -0400185 if (pmd_none(*pmd))
186 assign_pte(pmd, alloc_pte());
Chris Metcalf867e3592010-05-28 23:09:12 -0400187 }
188}
Chris Metcalf867e3592010-05-28 23:09:12 -0400189
190
Chris Metcalf867e3592010-05-28 23:09:12 -0400191static int __initdata ktext_hash = 1; /* .text pages */
192static int __initdata kdata_hash = 1; /* .data and .bss pages */
193int __write_once hash_default = 1; /* kernel allocator pages */
194EXPORT_SYMBOL(hash_default);
195int __write_once kstack_hash = 1; /* if no homecaching, use h4h */
Chris Metcalf867e3592010-05-28 23:09:12 -0400196
197/*
198 * CPUs to use to for striping the pages of kernel data. If hash-for-home
199 * is available, this is only relevant if kcache_hash sets up the
200 * .data and .bss to be page-homed, and we don't want the default mode
201 * of using the full set of kernel cpus for the striping.
202 */
203static __initdata struct cpumask kdata_mask;
204static __initdata int kdata_arg_seen;
205
206int __write_once kdata_huge; /* if no homecaching, small pages */
207
208
209/* Combine a generic pgprot_t with cache home to get a cache-aware pgprot. */
210static pgprot_t __init construct_pgprot(pgprot_t prot, int home)
211{
212 prot = pte_set_home(prot, home);
Chris Metcalf867e3592010-05-28 23:09:12 -0400213 if (home == PAGE_HOME_IMMUTABLE) {
214 if (ktext_hash)
215 prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_HASH_L3);
216 else
217 prot = hv_pte_set_mode(prot, HV_PTE_MODE_CACHE_NO_L3);
218 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400219 return prot;
220}
221
222/*
223 * For a given kernel data VA, how should it be cached?
224 * We return the complete pgprot_t with caching bits set.
225 */
226static pgprot_t __init init_pgprot(ulong address)
227{
228 int cpu;
229 unsigned long page;
Chris Metcalfacbde1d2013-09-03 14:41:36 -0400230 enum { CODE_DELTA = MEM_SV_START - PAGE_OFFSET };
Chris Metcalf867e3592010-05-28 23:09:12 -0400231
Chris Metcalf867e3592010-05-28 23:09:12 -0400232 /* For kdata=huge, everything is just hash-for-home. */
233 if (kdata_huge)
234 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
Chris Metcalf867e3592010-05-28 23:09:12 -0400235
236 /* We map the aliased pages of permanent text inaccessible. */
237 if (address < (ulong) _sinittext - CODE_DELTA)
238 return PAGE_NONE;
239
Chris Metcalfd7c96612013-08-15 16:23:24 -0400240 /* We map read-only data non-coherent for performance. */
Chris Metcalf867e3592010-05-28 23:09:12 -0400241 if ((address >= (ulong) __start_rodata &&
242 address < (ulong) __end_rodata) ||
243 address == (ulong) empty_zero_page) {
244 return construct_pgprot(PAGE_KERNEL_RO, PAGE_HOME_IMMUTABLE);
245 }
246
Chris Metcalf867e3592010-05-28 23:09:12 -0400247#ifndef __tilegx__
Chris Metcalf867e3592010-05-28 23:09:12 -0400248 /* Force the atomic_locks[] array page to be hash-for-home. */
249 if (address == (ulong) atomic_locks)
250 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
251#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400252
253 /*
254 * Everything else that isn't data or bss is heap, so mark it
255 * with the initial heap home (hash-for-home, or this cpu). This
Chris Metcalf0707ad32010-06-25 17:04:17 -0400256 * includes any addresses after the loaded image and any address before
Geert Uytterhoeven454ac3e2013-11-12 20:42:20 +0100257 * __init_end, since we already captured the case of text before
258 * _sinittext, and __pa(einittext) is approximately __pa(__init_begin).
Chris Metcalf867e3592010-05-28 23:09:12 -0400259 *
260 * All the LOWMEM pages that we mark this way will get their
261 * struct page homecache properly marked later, in set_page_homes().
262 * The HIGHMEM pages we leave with a default zero for their
263 * homes, but with a zero free_time we don't have to actually
264 * do a flush action the first time we use them, either.
265 */
Geert Uytterhoeven454ac3e2013-11-12 20:42:20 +0100266 if (address >= (ulong) _end || address < (ulong) __init_end)
Chris Metcalf867e3592010-05-28 23:09:12 -0400267 return construct_pgprot(PAGE_KERNEL, initial_heap_home());
268
Chris Metcalf867e3592010-05-28 23:09:12 -0400269 /* Use hash-for-home if requested for data/bss. */
270 if (kdata_hash)
271 return construct_pgprot(PAGE_KERNEL, PAGE_HOME_HASH);
Chris Metcalf867e3592010-05-28 23:09:12 -0400272
273 /*
274 * Otherwise we just hand out consecutive cpus. To avoid
275 * requiring this function to hold state, we just walk forward from
Wang Sheng-Huie540e832014-05-07 15:28:12 +0800276 * __end_rodata by PAGE_SIZE, skipping the readonly and init data, to
277 * reach the requested address, while walking cpu home around
278 * kdata_mask. This is typically no more than a dozen or so iterations.
Chris Metcalf867e3592010-05-28 23:09:12 -0400279 */
Chris Metcalfce61cdc2013-08-15 16:29:02 -0400280 page = (((ulong)__end_rodata) + PAGE_SIZE - 1) & PAGE_MASK;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400281 BUG_ON(address < page || address >= (ulong)_end);
282 cpu = cpumask_first(&kdata_mask);
283 for (; page < address; page += PAGE_SIZE) {
284 if (page >= (ulong)&init_thread_union &&
285 page < (ulong)&init_thread_union + THREAD_SIZE)
286 continue;
Chris Metcalf867e3592010-05-28 23:09:12 -0400287 if (page == (ulong)empty_zero_page)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400288 continue;
Chris Metcalf867e3592010-05-28 23:09:12 -0400289#ifndef __tilegx__
Chris Metcalf867e3592010-05-28 23:09:12 -0400290 if (page == (ulong)atomic_locks)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400291 continue;
Chris Metcalf867e3592010-05-28 23:09:12 -0400292#endif
Chris Metcalf0707ad32010-06-25 17:04:17 -0400293 cpu = cpumask_next(cpu, &kdata_mask);
294 if (cpu == NR_CPUS)
295 cpu = cpumask_first(&kdata_mask);
Chris Metcalf867e3592010-05-28 23:09:12 -0400296 }
297 return construct_pgprot(PAGE_KERNEL, cpu);
298}
299
300/*
301 * This function sets up how we cache the kernel text. If we have
302 * hash-for-home support, normally that is used instead (see the
303 * kcache_hash boot flag for more information). But if we end up
304 * using a page-based caching technique, this option sets up the
305 * details of that. In addition, the "ktext=nocache" option may
306 * always be used to disable local caching of text pages, if desired.
307 */
308
309static int __initdata ktext_arg_seen;
310static int __initdata ktext_small;
311static int __initdata ktext_local;
312static int __initdata ktext_all;
313static int __initdata ktext_nondataplane;
314static int __initdata ktext_nocache;
315static struct cpumask __initdata ktext_mask;
316
317static int __init setup_ktext(char *str)
318{
319 if (str == NULL)
320 return -EINVAL;
321
322 /* If you have a leading "nocache", turn off ktext caching */
323 if (strncmp(str, "nocache", 7) == 0) {
324 ktext_nocache = 1;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400325 pr_info("ktext: disabling local caching of kernel text\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400326 str += 7;
327 if (*str == ',')
328 ++str;
329 if (*str == '\0')
330 return 0;
331 }
332
333 ktext_arg_seen = 1;
334
Chris Metcalfd7c96612013-08-15 16:23:24 -0400335 /* Default setting: use a huge page */
Chris Metcalf867e3592010-05-28 23:09:12 -0400336 if (strcmp(str, "huge") == 0)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400337 pr_info("ktext: using one huge locally cached page\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400338
339 /* Pay TLB cost but get no cache benefit: cache small pages locally */
340 else if (strcmp(str, "local") == 0) {
341 ktext_small = 1;
342 ktext_local = 1;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400343 pr_info("ktext: using small pages with local caching\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400344 }
345
346 /* Neighborhood cache ktext pages on all cpus. */
347 else if (strcmp(str, "all") == 0) {
348 ktext_small = 1;
349 ktext_all = 1;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400350 pr_info("ktext: using maximal caching neighborhood\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400351 }
352
353
354 /* Neighborhood ktext pages on specified mask */
355 else if (cpulist_parse(str, &ktext_mask) == 0) {
356 char buf[NR_CPUS * 5];
357 cpulist_scnprintf(buf, sizeof(buf), &ktext_mask);
358 if (cpumask_weight(&ktext_mask) > 1) {
359 ktext_small = 1;
Joe Perchesf4743672014-10-31 10:50:46 -0700360 pr_info("ktext: using caching neighborhood %s with small pages\n",
361 buf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400362 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400363 pr_info("ktext: caching on cpu %s with one huge page\n",
Joe Perchesf4743672014-10-31 10:50:46 -0700364 buf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400365 }
366 }
367
368 else if (*str)
369 return -EINVAL;
370
371 return 0;
372}
373
374early_param("ktext", setup_ktext);
375
376
377static inline pgprot_t ktext_set_nocache(pgprot_t prot)
378{
379 if (!ktext_nocache)
380 prot = hv_pte_set_nc(prot);
Chris Metcalf867e3592010-05-28 23:09:12 -0400381 else
382 prot = hv_pte_set_no_alloc_l2(prot);
Chris Metcalf867e3592010-05-28 23:09:12 -0400383 return prot;
384}
385
Chris Metcalf867e3592010-05-28 23:09:12 -0400386/* Temporary page table we use for staging. */
387static pgd_t pgtables[PTRS_PER_PGD]
Chris Metcalf2cb82402011-02-27 18:52:24 -0500388 __attribute__((aligned(HV_PAGE_TABLE_ALIGN)));
Chris Metcalf867e3592010-05-28 23:09:12 -0400389
390/*
391 * This maps the physical memory to kernel virtual address space, a total
392 * of max_low_pfn pages, by creating page tables starting from address
393 * PAGE_OFFSET.
394 *
395 * This routine transitions us from using a set of compiled-in large
396 * pages to using some more precise caching, including removing access
397 * to code pages mapped at PAGE_OFFSET (executed only at MEM_SV_START)
398 * marking read-only data as locally cacheable, striping the remaining
399 * .data and .bss across all the available tiles, and removing access
400 * to pages above the top of RAM (thus ensuring a page fault from a bad
401 * virtual address rather than a hypervisor shoot down for accessing
402 * memory outside the assigned limits).
403 */
404static void __init kernel_physical_mapping_init(pgd_t *pgd_base)
405{
Chris Metcalf51007002012-03-27 15:40:20 -0400406 unsigned long long irqmask;
Chris Metcalf867e3592010-05-28 23:09:12 -0400407 unsigned long address, pfn;
408 pmd_t *pmd;
409 pte_t *pte;
410 int pte_ofs;
411 const struct cpumask *my_cpu_mask = cpumask_of(smp_processor_id());
412 struct cpumask kstripe_mask;
413 int rc, i;
414
Chris Metcalf867e3592010-05-28 23:09:12 -0400415 if (ktext_arg_seen && ktext_hash) {
Joe Perchesf4743672014-10-31 10:50:46 -0700416 pr_warn("warning: \"ktext\" boot argument ignored if \"kcache_hash\" sets up text hash-for-home\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400417 ktext_small = 0;
418 }
419
420 if (kdata_arg_seen && kdata_hash) {
Joe Perchesf4743672014-10-31 10:50:46 -0700421 pr_warn("warning: \"kdata\" boot argument ignored if \"kcache_hash\" sets up data hash-for-home\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400422 }
423
424 if (kdata_huge && !hash_default) {
Joe Perchesf4743672014-10-31 10:50:46 -0700425 pr_warn("warning: disabling \"kdata=huge\"; requires kcache_hash=all or =allbutstack\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400426 kdata_huge = 0;
427 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400428
429 /*
430 * Set up a mask for cpus to use for kernel striping.
431 * This is normally all cpus, but minus dataplane cpus if any.
432 * If the dataplane covers the whole chip, we stripe over
433 * the whole chip too.
434 */
435 cpumask_copy(&kstripe_mask, cpu_possible_mask);
436 if (!kdata_arg_seen)
437 kdata_mask = kstripe_mask;
438
439 /* Allocate and fill in L2 page tables */
440 for (i = 0; i < MAX_NUMNODES; ++i) {
441#ifdef CONFIG_HIGHMEM
442 unsigned long end_pfn = node_lowmem_end_pfn[i];
443#else
444 unsigned long end_pfn = node_end_pfn[i];
445#endif
446 unsigned long end_huge_pfn = 0;
447
448 /* Pre-shatter the last huge page to allow per-cpu pages. */
449 if (kdata_huge)
450 end_huge_pfn = end_pfn - (HPAGE_SIZE >> PAGE_SHIFT);
451
452 pfn = node_start_pfn[i];
453
454 /* Allocate enough memory to hold L2 page tables for node. */
455 init_prealloc_ptes(i, end_pfn - pfn);
456
457 address = (unsigned long) pfn_to_kaddr(pfn);
458 while (pfn < end_pfn) {
459 BUG_ON(address & (HPAGE_SIZE-1));
460 pmd = get_pmd(pgtables, address);
461 pte = get_prealloc_pte(pfn);
462 if (pfn < end_huge_pfn) {
463 pgprot_t prot = init_pgprot(address);
464 *(pte_t *)pmd = pte_mkhuge(pfn_pte(pfn, prot));
465 for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE;
466 pfn++, pte_ofs++, address += PAGE_SIZE)
467 pte[pte_ofs] = pfn_pte(pfn, prot);
468 } else {
469 if (kdata_huge)
Joe Perchesf4743672014-10-31 10:50:46 -0700470 printk(KERN_DEBUG "pre-shattered huge page at %#lx\n",
471 address);
Chris Metcalf867e3592010-05-28 23:09:12 -0400472 for (pte_ofs = 0; pte_ofs < PTRS_PER_PTE;
473 pfn++, pte_ofs++, address += PAGE_SIZE) {
474 pgprot_t prot = init_pgprot(address);
475 pte[pte_ofs] = pfn_pte(pfn, prot);
476 }
477 assign_pte(pmd, pte);
478 }
479 }
480 }
481
482 /*
483 * Set or check ktext_map now that we have cpu_possible_mask
484 * and kstripe_mask to work with.
485 */
486 if (ktext_all)
487 cpumask_copy(&ktext_mask, cpu_possible_mask);
488 else if (ktext_nondataplane)
489 ktext_mask = kstripe_mask;
490 else if (!cpumask_empty(&ktext_mask)) {
491 /* Sanity-check any mask that was requested */
492 struct cpumask bad;
493 cpumask_andnot(&bad, &ktext_mask, cpu_possible_mask);
494 cpumask_and(&ktext_mask, &ktext_mask, cpu_possible_mask);
495 if (!cpumask_empty(&bad)) {
496 char buf[NR_CPUS * 5];
497 cpulist_scnprintf(buf, sizeof(buf), &bad);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400498 pr_info("ktext: not using unavailable cpus %s\n", buf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400499 }
500 if (cpumask_empty(&ktext_mask)) {
Joe Perchesf4743672014-10-31 10:50:46 -0700501 pr_warn("ktext: no valid cpus; caching on %d\n",
502 smp_processor_id());
Chris Metcalf867e3592010-05-28 23:09:12 -0400503 cpumask_copy(&ktext_mask,
504 cpumask_of(smp_processor_id()));
505 }
506 }
507
Chris Metcalfacbde1d2013-09-03 14:41:36 -0400508 address = MEM_SV_START;
Chris Metcalf867e3592010-05-28 23:09:12 -0400509 pmd = get_pmd(pgtables, address);
Chris Metcalf7a7039e2012-03-29 15:42:27 -0400510 pfn = 0; /* code starts at PA 0 */
Chris Metcalf867e3592010-05-28 23:09:12 -0400511 if (ktext_small) {
512 /* Allocate an L2 PTE for the kernel text */
513 int cpu = 0;
514 pgprot_t prot = construct_pgprot(PAGE_KERNEL_EXEC,
515 PAGE_HOME_IMMUTABLE);
516
517 if (ktext_local) {
518 if (ktext_nocache)
519 prot = hv_pte_set_mode(prot,
520 HV_PTE_MODE_UNCACHED);
521 else
522 prot = hv_pte_set_mode(prot,
523 HV_PTE_MODE_CACHE_NO_L3);
524 } else {
525 prot = hv_pte_set_mode(prot,
526 HV_PTE_MODE_CACHE_TILE_L3);
527 cpu = cpumask_first(&ktext_mask);
528
529 prot = ktext_set_nocache(prot);
530 }
531
Jiang Liu40a3b8d2013-07-03 15:03:39 -0700532 BUG_ON(address != (unsigned long)_text);
Chris Metcalf7a7039e2012-03-29 15:42:27 -0400533 pte = NULL;
534 for (; address < (unsigned long)_einittext;
535 pfn++, address += PAGE_SIZE) {
536 pte_ofs = pte_index(address);
537 if (pte_ofs == 0) {
538 if (pte)
539 assign_pte(pmd++, pte);
540 pte = alloc_pte();
541 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400542 if (!ktext_local) {
543 prot = set_remote_cache_cpu(prot, cpu);
544 cpu = cpumask_next(cpu, &ktext_mask);
545 if (cpu == NR_CPUS)
546 cpu = cpumask_first(&ktext_mask);
547 }
548 pte[pte_ofs] = pfn_pte(pfn, prot);
549 }
Chris Metcalf7a7039e2012-03-29 15:42:27 -0400550 if (pte)
551 assign_pte(pmd, pte);
Chris Metcalf867e3592010-05-28 23:09:12 -0400552 } else {
553 pte_t pteval = pfn_pte(0, PAGE_KERNEL_EXEC);
554 pteval = pte_mkhuge(pteval);
Chris Metcalf867e3592010-05-28 23:09:12 -0400555 if (ktext_hash) {
556 pteval = hv_pte_set_mode(pteval,
557 HV_PTE_MODE_CACHE_HASH_L3);
558 pteval = ktext_set_nocache(pteval);
559 } else
Chris Metcalf867e3592010-05-28 23:09:12 -0400560 if (cpumask_weight(&ktext_mask) == 1) {
561 pteval = set_remote_cache_cpu(pteval,
562 cpumask_first(&ktext_mask));
563 pteval = hv_pte_set_mode(pteval,
564 HV_PTE_MODE_CACHE_TILE_L3);
565 pteval = ktext_set_nocache(pteval);
566 } else if (ktext_nocache)
567 pteval = hv_pte_set_mode(pteval,
568 HV_PTE_MODE_UNCACHED);
569 else
570 pteval = hv_pte_set_mode(pteval,
571 HV_PTE_MODE_CACHE_NO_L3);
Chris Metcalf7a7039e2012-03-29 15:42:27 -0400572 for (; address < (unsigned long)_einittext;
573 pfn += PFN_DOWN(HPAGE_SIZE), address += HPAGE_SIZE)
574 *(pte_t *)(pmd++) = pfn_pte(pfn, pteval);
Chris Metcalf867e3592010-05-28 23:09:12 -0400575 }
576
577 /* Set swapper_pgprot here so it is flushed to memory right away. */
578 swapper_pgprot = init_pgprot((unsigned long)swapper_pg_dir);
579
580 /*
581 * Since we may be changing the caching of the stack and page
582 * table itself, we invoke an assembly helper to do the
583 * following steps:
584 *
585 * - flush the cache so we start with an empty slate
586 * - install pgtables[] as the real page table
587 * - flush the TLB so the new page table takes effect
588 */
Chris Metcalf51007002012-03-27 15:40:20 -0400589 irqmask = interrupt_mask_save_mask();
590 interrupt_mask_set_mask(-1ULL);
Chris Metcalf867e3592010-05-28 23:09:12 -0400591 rc = flush_and_install_context(__pa(pgtables),
592 init_pgprot((unsigned long)pgtables),
Christoph Lameterb4f50192014-08-17 12:30:50 -0500593 __this_cpu_read(current_asid),
Chris Metcalf867e3592010-05-28 23:09:12 -0400594 cpumask_bits(my_cpu_mask));
Chris Metcalf51007002012-03-27 15:40:20 -0400595 interrupt_mask_restore_mask(irqmask);
Chris Metcalf867e3592010-05-28 23:09:12 -0400596 BUG_ON(rc != 0);
597
598 /* Copy the page table back to the normal swapper_pg_dir. */
599 memcpy(pgd_base, pgtables, sizeof(pgtables));
Christoph Lameterb4f50192014-08-17 12:30:50 -0500600 __install_page_table(pgd_base, __this_cpu_read(current_asid),
Chris Metcalf867e3592010-05-28 23:09:12 -0400601 swapper_pgprot);
Chris Metcalf401586e2011-02-28 15:01:53 -0500602
603 /*
604 * We just read swapper_pgprot and thus brought it into the cache,
605 * with its new home & caching mode. When we start the other CPUs,
606 * they're going to reference swapper_pgprot via their initial fake
607 * VA-is-PA mappings, which cache everything locally. At that
608 * time, if it's in our cache with a conflicting home, the
609 * simulator's coherence checker will complain. So, flush it out
610 * of our cache; we're not going to ever use it again anyway.
611 */
612 __insn_finv(&swapper_pgprot);
Chris Metcalf867e3592010-05-28 23:09:12 -0400613}
614
615/*
616 * devmem_is_allowed() checks to see if /dev/mem access to a certain address
617 * is valid. The argument is a physical page number.
618 *
619 * On Tile, the only valid things for which we can just hand out unchecked
620 * PTEs are the kernel code and data. Anything else might change its
621 * homing with time, and we wouldn't know to adjust the /dev/mem PTEs.
622 * Note that init_thread_union is released to heap soon after boot,
623 * so we include it in the init data.
624 *
625 * For TILE-Gx, we might want to consider allowing access to PA
626 * regions corresponding to PCI space, etc.
627 */
628int devmem_is_allowed(unsigned long pagenr)
629{
630 return pagenr < kaddr_to_pfn(_end) &&
631 !(pagenr >= kaddr_to_pfn(&init_thread_union) ||
Geert Uytterhoeven454ac3e2013-11-12 20:42:20 +0100632 pagenr < kaddr_to_pfn(__init_end)) &&
Chris Metcalf867e3592010-05-28 23:09:12 -0400633 !(pagenr >= kaddr_to_pfn(_sinittext) ||
634 pagenr <= kaddr_to_pfn(_einittext-1));
635}
636
637#ifdef CONFIG_HIGHMEM
638static void __init permanent_kmaps_init(pgd_t *pgd_base)
639{
640 pgd_t *pgd;
641 pud_t *pud;
642 pmd_t *pmd;
643 pte_t *pte;
644 unsigned long vaddr;
645
646 vaddr = PKMAP_BASE;
647 page_table_range_init(vaddr, vaddr + PAGE_SIZE*LAST_PKMAP, pgd_base);
648
649 pgd = swapper_pg_dir + pgd_index(vaddr);
650 pud = pud_offset(pgd, vaddr);
651 pmd = pmd_offset(pud, vaddr);
652 pte = pte_offset_kernel(pmd, vaddr);
653 pkmap_page_table = pte;
654}
655#endif /* CONFIG_HIGHMEM */
656
657
Chris Metcalf621b1952012-04-01 14:04:21 -0400658#ifndef CONFIG_64BIT
Chris Metcalf867e3592010-05-28 23:09:12 -0400659static void __init init_free_pfn_range(unsigned long start, unsigned long end)
660{
661 unsigned long pfn;
662 struct page *page = pfn_to_page(start);
663
664 for (pfn = start; pfn < end; ) {
665 /* Optimize by freeing pages in large batches */
666 int order = __ffs(pfn);
667 int count, i;
668 struct page *p;
669
670 if (order >= MAX_ORDER)
671 order = MAX_ORDER-1;
672 count = 1 << order;
673 while (pfn + count > end) {
674 count >>= 1;
675 --order;
676 }
677 for (p = page, i = 0; i < count; ++i, ++p) {
678 __ClearPageReserved(p);
679 /*
680 * Hacky direct set to avoid unnecessary
681 * lock take/release for EVERY page here.
682 */
683 p->_count.counter = 0;
684 p->_mapcount.counter = -1;
685 }
686 init_page_count(page);
687 __free_pages(page, order);
Jiang Liuabd1b6d2013-07-03 15:03:01 -0700688 adjust_managed_page_count(page, count);
Chris Metcalf867e3592010-05-28 23:09:12 -0400689
690 page += count;
691 pfn += count;
692 }
693}
694
695static void __init set_non_bootmem_pages_init(void)
696{
697 struct zone *z;
698 for_each_zone(z) {
699 unsigned long start, end;
700 int nid = z->zone_pgdat->node_id;
Chris Metcalfeef015c2012-05-09 12:26:30 -0400701#ifdef CONFIG_HIGHMEM
Chris Metcalf0707ad32010-06-25 17:04:17 -0400702 int idx = zone_idx(z);
Chris Metcalfeef015c2012-05-09 12:26:30 -0400703#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400704
705 start = z->zone_start_pfn;
Chris Metcalf867e3592010-05-28 23:09:12 -0400706 end = start + z->spanned_pages;
Chris Metcalfeef015c2012-05-09 12:26:30 -0400707 start = max(start, node_free_pfn[nid]);
708 start = max(start, max_low_pfn);
709
Chris Metcalf867e3592010-05-28 23:09:12 -0400710#ifdef CONFIG_HIGHMEM
Chris Metcalf0707ad32010-06-25 17:04:17 -0400711 if (idx == ZONE_HIGHMEM)
Chris Metcalf867e3592010-05-28 23:09:12 -0400712 totalhigh_pages += z->spanned_pages;
713#endif
714 if (kdata_huge) {
715 unsigned long percpu_pfn = node_percpu_pfn[nid];
716 if (start < percpu_pfn && end > percpu_pfn)
717 end = percpu_pfn;
718 }
719#ifdef CONFIG_PCI
720 if (start <= pci_reserve_start_pfn &&
721 end > pci_reserve_start_pfn) {
722 if (end > pci_reserve_end_pfn)
723 init_free_pfn_range(pci_reserve_end_pfn, end);
724 end = pci_reserve_start_pfn;
725 }
726#endif
727 init_free_pfn_range(start, end);
728 }
729}
Chris Metcalf621b1952012-04-01 14:04:21 -0400730#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400731
732/*
733 * paging_init() sets up the page tables - note that all of lowmem is
734 * already mapped by head.S.
735 */
736void __init paging_init(void)
737{
Chris Metcalf867e3592010-05-28 23:09:12 -0400738#ifdef __tilegx__
739 pud_t *pud;
740#endif
741 pgd_t *pgd_base = swapper_pg_dir;
742
743 kernel_physical_mapping_init(pgd_base);
744
Chris Metcalf084fe6a2013-08-09 16:17:03 -0400745 /* Fixed mappings, only the page table structure has to be created. */
Chris Metcalfbbaa22c2012-06-13 14:46:40 -0400746 page_table_range_init(fix_to_virt(__end_of_fixed_addresses - 1),
747 FIXADDR_TOP, pgd_base);
748
749#ifdef CONFIG_HIGHMEM
Chris Metcalf867e3592010-05-28 23:09:12 -0400750 permanent_kmaps_init(pgd_base);
751#endif
752
753#ifdef __tilegx__
754 /*
755 * Since GX allocates just one pmd_t array worth of vmalloc space,
756 * we go ahead and allocate it statically here, then share it
757 * globally. As a result we don't have to worry about any task
758 * changing init_mm once we get up and running, and there's no
759 * need for e.g. vmalloc_sync_all().
760 */
Chris Metcalfd5d14ed2012-03-29 13:58:43 -0400761 BUILD_BUG_ON(pgd_index(VMALLOC_START) != pgd_index(VMALLOC_END - 1));
Chris Metcalf867e3592010-05-28 23:09:12 -0400762 pud = pud_offset(pgd_base + pgd_index(VMALLOC_START), VMALLOC_START);
763 assign_pmd(pud, alloc_pmd());
764#endif
765}
766
767
768/*
769 * Walk the kernel page tables and derive the page_home() from
770 * the PTEs, so that set_pte() can properly validate the caching
771 * of all PTEs it sees.
772 */
773void __init set_page_homes(void)
774{
775}
776
777static void __init set_max_mapnr_init(void)
778{
779#ifdef CONFIG_FLATMEM
780 max_mapnr = max_low_pfn;
781#endif
782}
783
784void __init mem_init(void)
785{
Chris Metcalf867e3592010-05-28 23:09:12 -0400786 int i;
787#ifndef __tilegx__
788 void *last;
789#endif
790
791#ifdef CONFIG_FLATMEM
Julia Lawalld1afa652011-08-02 12:35:04 +0200792 BUG_ON(!mem_map);
Chris Metcalf867e3592010-05-28 23:09:12 -0400793#endif
794
795#ifdef CONFIG_HIGHMEM
796 /* check that fixmap and pkmap do not overlap */
797 if (PKMAP_ADDR(LAST_PKMAP-1) >= FIXADDR_START) {
Joe Perchesf4743672014-10-31 10:50:46 -0700798 pr_err("fixmap and kmap areas overlap - this will crash\n");
Chris Metcalf0707ad32010-06-25 17:04:17 -0400799 pr_err("pkstart: %lxh pkend: %lxh fixstart %lxh\n",
Joe Perchesf4743672014-10-31 10:50:46 -0700800 PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP-1), FIXADDR_START);
Chris Metcalf867e3592010-05-28 23:09:12 -0400801 BUG();
802 }
803#endif
804
805 set_max_mapnr_init();
806
807 /* this will put all bootmem onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -0700808 free_all_bootmem();
Chris Metcalf867e3592010-05-28 23:09:12 -0400809
Chris Metcalf621b1952012-04-01 14:04:21 -0400810#ifndef CONFIG_64BIT
Chris Metcalf867e3592010-05-28 23:09:12 -0400811 /* count all remaining LOWMEM and give all HIGHMEM to page allocator */
812 set_non_bootmem_pages_init();
Chris Metcalf621b1952012-04-01 14:04:21 -0400813#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400814
Jiang Liu3f29c332013-07-03 15:04:15 -0700815 mem_init_print_info(NULL);
Chris Metcalf867e3592010-05-28 23:09:12 -0400816
817 /*
818 * In debug mode, dump some interesting memory mappings.
819 */
820#ifdef CONFIG_HIGHMEM
821 printk(KERN_DEBUG " KMAP %#lx - %#lx\n",
822 FIXADDR_START, FIXADDR_TOP + PAGE_SIZE - 1);
823 printk(KERN_DEBUG " PKMAP %#lx - %#lx\n",
824 PKMAP_BASE, PKMAP_ADDR(LAST_PKMAP) - 1);
825#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400826 printk(KERN_DEBUG " VMALLOC %#lx - %#lx\n",
827 _VMALLOC_START, _VMALLOC_END - 1);
828#ifdef __tilegx__
829 for (i = MAX_NUMNODES-1; i >= 0; --i) {
830 struct pglist_data *node = &node_data[i];
831 if (node->node_present_pages) {
832 unsigned long start = (unsigned long)
833 pfn_to_kaddr(node->node_start_pfn);
834 unsigned long end = start +
835 (node->node_present_pages << PAGE_SHIFT);
836 printk(KERN_DEBUG " MEM%d %#lx - %#lx\n",
837 i, start, end - 1);
838 }
839 }
840#else
841 last = high_memory;
842 for (i = MAX_NUMNODES-1; i >= 0; --i) {
843 if ((unsigned long)vbase_map[i] != -1UL) {
844 printk(KERN_DEBUG " LOWMEM%d %#lx - %#lx\n",
845 i, (unsigned long) (vbase_map[i]),
846 (unsigned long) (last-1));
847 last = vbase_map[i];
848 }
849 }
850#endif
851
852#ifndef __tilegx__
853 /*
854 * Convert from using one lock for all atomic operations to
855 * one per cpu.
856 */
857 __init_atomic_per_cpu();
858#endif
859}
860
861/*
862 * this is for the non-NUMA, single node SMP system case.
863 * Specifically, in the case of x86, we will always add
864 * memory to the highmem for now.
865 */
866#ifndef CONFIG_NEED_MULTIPLE_NODES
867int arch_add_memory(u64 start, u64 size)
868{
869 struct pglist_data *pgdata = &contig_page_data;
870 struct zone *zone = pgdata->node_zones + MAX_NR_ZONES-1;
871 unsigned long start_pfn = start >> PAGE_SHIFT;
872 unsigned long nr_pages = size >> PAGE_SHIFT;
873
874 return __add_pages(zone, start_pfn, nr_pages);
875}
876
877int remove_memory(u64 start, u64 size)
878{
879 return -EINVAL;
880}
Wen Congyang24d335c2013-02-22 16:32:58 -0800881
882#ifdef CONFIG_MEMORY_HOTREMOVE
883int arch_remove_memory(u64 start, u64 size)
884{
885 /* TODO */
886 return -EBUSY;
887}
888#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400889#endif
890
891struct kmem_cache *pgd_cache;
892
893void __init pgtable_cache_init(void)
894{
Chris Metcalf76c567f2011-02-28 16:37:34 -0500895 pgd_cache = kmem_cache_create("pgd", SIZEOF_PGD, SIZEOF_PGD, 0, NULL);
Chris Metcalf867e3592010-05-28 23:09:12 -0400896 if (!pgd_cache)
897 panic("pgtable_cache_init(): Cannot create pgd cache");
898}
899
Chris Metcalf867e3592010-05-28 23:09:12 -0400900#ifdef CONFIG_DEBUG_PAGEALLOC
901static long __write_once initfree;
902#else
903static long __write_once initfree = 1;
904#endif
905
906/* Select whether to free (1) or mark unusable (0) the __init pages. */
907static int __init set_initfree(char *str)
908{
Chris Metcalfd59e6092010-11-01 15:25:16 -0400909 long val;
Daniel Walterb2dfa042014-05-26 22:59:32 +0100910 if (kstrtol(str, 0, &val) == 0) {
Chris Metcalfd59e6092010-11-01 15:25:16 -0400911 initfree = val;
912 pr_info("initfree: %s free init pages\n",
913 initfree ? "will" : "won't");
914 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400915 return 1;
916}
917__setup("initfree=", set_initfree);
918
919static void free_init_pages(char *what, unsigned long begin, unsigned long end)
920{
921 unsigned long addr = (unsigned long) begin;
922
923 if (kdata_huge && !initfree) {
Joe Perchesf4743672014-10-31 10:50:46 -0700924 pr_warn("Warning: ignoring initfree=0: incompatible with kdata=huge\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400925 initfree = 1;
926 }
927 end = (end + PAGE_SIZE - 1) & PAGE_MASK;
928 local_flush_tlb_pages(NULL, begin, PAGE_SIZE, end - begin);
929 for (addr = begin; addr < end; addr += PAGE_SIZE) {
930 /*
931 * Note we just reset the home here directly in the
932 * page table. We know this is safe because our caller
933 * just flushed the caches on all the other cpus,
934 * and they won't be touching any of these pages.
935 */
936 int pfn = kaddr_to_pfn((void *)addr);
937 struct page *page = pfn_to_page(pfn);
Chris Metcalf640710a2013-08-12 15:08:09 -0400938 pte_t *ptep = virt_to_kpte(addr);
Chris Metcalf867e3592010-05-28 23:09:12 -0400939 if (!initfree) {
940 /*
941 * If debugging page accesses then do not free
942 * this memory but mark them not present - any
943 * buggy init-section access will create a
944 * kernel page fault:
945 */
946 pte_clear(&init_mm, addr, ptep);
947 continue;
948 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400949 if (pte_huge(*ptep))
950 BUG_ON(!kdata_huge);
951 else
952 set_pte_at(&init_mm, addr, ptep,
953 pfn_pte(pfn, PAGE_KERNEL));
954 memset((void *)addr, POISON_FREE_INITMEM, PAGE_SIZE);
Jiang Liuabd1b6d2013-07-03 15:03:01 -0700955 free_reserved_page(page);
Chris Metcalf867e3592010-05-28 23:09:12 -0400956 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400957 pr_info("Freeing %s: %ldk freed\n", what, (end - begin) >> 10);
Chris Metcalf867e3592010-05-28 23:09:12 -0400958}
959
960void free_initmem(void)
961{
Chris Metcalfacbde1d2013-09-03 14:41:36 -0400962 const unsigned long text_delta = MEM_SV_START - PAGE_OFFSET;
Chris Metcalf867e3592010-05-28 23:09:12 -0400963
964 /*
Chris Metcalfce61cdc2013-08-15 16:29:02 -0400965 * Evict the cache on all cores to avoid incoherence.
Chris Metcalfd7c96612013-08-15 16:23:24 -0400966 * We are guaranteed that no one will touch the init pages any more.
Chris Metcalf867e3592010-05-28 23:09:12 -0400967 */
968 homecache_evict(&cpu_cacheable_map);
969
970 /* Free the data pages that we won't use again after init. */
971 free_init_pages("unused kernel data",
Geert Uytterhoeven454ac3e2013-11-12 20:42:20 +0100972 (unsigned long)__init_begin,
973 (unsigned long)__init_end);
Chris Metcalf867e3592010-05-28 23:09:12 -0400974
975 /*
976 * Free the pages mapped from 0xc0000000 that correspond to code
Chris Metcalfacbde1d2013-09-03 14:41:36 -0400977 * pages from MEM_SV_START that we won't use again after init.
Chris Metcalf867e3592010-05-28 23:09:12 -0400978 */
979 free_init_pages("unused kernel text",
980 (unsigned long)_sinittext - text_delta,
981 (unsigned long)_einittext - text_delta);
Chris Metcalf867e3592010-05-28 23:09:12 -0400982 /* Do a global TLB flush so everyone sees the changes. */
983 flush_tlb_all();
984}