blob: 98d80eb49ddbd912a0957b0dce0ad4d121be665a [file] [log] [blame]
Chris Metcalf867e3592010-05-28 23:09:12 -04001/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 */
14
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/mmzone.h>
18#include <linux/bootmem.h>
19#include <linux/module.h>
20#include <linux/node.h>
21#include <linux/cpu.h>
22#include <linux/ioport.h>
Chris Metcalf0707ad32010-06-25 17:04:17 -040023#include <linux/irq.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040024#include <linux/kexec.h>
25#include <linux/pci.h>
26#include <linux/initrd.h>
27#include <linux/io.h>
28#include <linux/highmem.h>
29#include <linux/smp.h>
30#include <linux/timex.h>
31#include <asm/setup.h>
32#include <asm/sections.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040033#include <asm/cacheflush.h>
34#include <asm/pgalloc.h>
35#include <asm/mmu_context.h>
36#include <hv/hypervisor.h>
37#include <arch/interrupts.h>
38
39/* <linux/smp.h> doesn't provide this definition. */
40#ifndef CONFIG_SMP
41#define setup_max_cpus 1
42#endif
43
44static inline int ABS(int x) { return x >= 0 ? x : -x; }
45
46/* Chip information */
47char chip_model[64] __write_once;
48
49struct pglist_data node_data[MAX_NUMNODES] __read_mostly;
50EXPORT_SYMBOL(node_data);
51
52/* We only create bootmem data on node 0. */
53static bootmem_data_t __initdata node0_bdata;
54
55/* Information on the NUMA nodes that we compute early */
56unsigned long __cpuinitdata node_start_pfn[MAX_NUMNODES];
57unsigned long __cpuinitdata node_end_pfn[MAX_NUMNODES];
58unsigned long __initdata node_memmap_pfn[MAX_NUMNODES];
59unsigned long __initdata node_percpu_pfn[MAX_NUMNODES];
60unsigned long __initdata node_free_pfn[MAX_NUMNODES];
61
Chris Metcalf76c567f2011-02-28 16:37:34 -050062static unsigned long __initdata node_percpu[MAX_NUMNODES];
63
Thomas Gleixner293ef7b2012-05-03 09:02:59 +000064/*
65 * per-CPU stack and boot info.
66 */
67DEFINE_PER_CPU(unsigned long, boot_sp) =
68 (unsigned long)init_stack + THREAD_SIZE;
69
70#ifdef CONFIG_SMP
71DEFINE_PER_CPU(unsigned long, boot_pc) = (unsigned long)start_kernel;
72#else
73/*
74 * The variable must be __initdata since it references __init code.
75 * With CONFIG_SMP it is per-cpu data, which is exempt from validation.
76 */
77unsigned long __initdata boot_pc = (unsigned long)start_kernel;
78#endif
79
Chris Metcalf867e3592010-05-28 23:09:12 -040080#ifdef CONFIG_HIGHMEM
81/* Page frame index of end of lowmem on each controller. */
82unsigned long __cpuinitdata node_lowmem_end_pfn[MAX_NUMNODES];
83
84/* Number of pages that can be mapped into lowmem. */
85static unsigned long __initdata mappable_physpages;
86#endif
87
88/* Data on which physical memory controller corresponds to which NUMA node */
89int node_controller[MAX_NUMNODES] = { [0 ... MAX_NUMNODES-1] = -1 };
90
91#ifdef CONFIG_HIGHMEM
92/* Map information from VAs to PAs */
93unsigned long pbase_map[1 << (32 - HPAGE_SHIFT)]
94 __write_once __attribute__((aligned(L2_CACHE_BYTES)));
95EXPORT_SYMBOL(pbase_map);
96
97/* Map information from PAs to VAs */
98void *vbase_map[NR_PA_HIGHBIT_VALUES]
99 __write_once __attribute__((aligned(L2_CACHE_BYTES)));
100EXPORT_SYMBOL(vbase_map);
101#endif
102
103/* Node number as a function of the high PA bits */
104int highbits_to_node[NR_PA_HIGHBIT_VALUES] __write_once;
105EXPORT_SYMBOL(highbits_to_node);
106
107static unsigned int __initdata maxmem_pfn = -1U;
108static unsigned int __initdata maxnodemem_pfn[MAX_NUMNODES] = {
109 [0 ... MAX_NUMNODES-1] = -1U
110};
111static nodemask_t __initdata isolnodes;
112
113#ifdef CONFIG_PCI
114enum { DEFAULT_PCI_RESERVE_MB = 64 };
115static unsigned int __initdata pci_reserve_mb = DEFAULT_PCI_RESERVE_MB;
116unsigned long __initdata pci_reserve_start_pfn = -1U;
117unsigned long __initdata pci_reserve_end_pfn = -1U;
118#endif
119
120static int __init setup_maxmem(char *str)
121{
Chris Metcalfbfffe792012-03-29 15:56:18 -0400122 unsigned long long maxmem;
123 if (str == NULL || (maxmem = memparse(str, NULL)) == 0)
Chris Metcalf867e3592010-05-28 23:09:12 -0400124 return -EINVAL;
125
Chris Metcalfbfffe792012-03-29 15:56:18 -0400126 maxmem_pfn = (maxmem >> HPAGE_SHIFT) << (HPAGE_SHIFT - PAGE_SHIFT);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400127 pr_info("Forcing RAM used to no more than %dMB\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400128 maxmem_pfn >> (20 - PAGE_SHIFT));
129 return 0;
130}
131early_param("maxmem", setup_maxmem);
132
133static int __init setup_maxnodemem(char *str)
134{
135 char *endp;
Chris Metcalfbfffe792012-03-29 15:56:18 -0400136 unsigned long long maxnodemem;
137 long node;
Chris Metcalf867e3592010-05-28 23:09:12 -0400138
139 node = str ? simple_strtoul(str, &endp, 0) : INT_MAX;
Chris Metcalfbfffe792012-03-29 15:56:18 -0400140 if (node >= MAX_NUMNODES || *endp != ':')
Chris Metcalf867e3592010-05-28 23:09:12 -0400141 return -EINVAL;
142
Chris Metcalfbfffe792012-03-29 15:56:18 -0400143 maxnodemem = memparse(endp+1, NULL);
144 maxnodemem_pfn[node] = (maxnodemem >> HPAGE_SHIFT) <<
Chris Metcalf867e3592010-05-28 23:09:12 -0400145 (HPAGE_SHIFT - PAGE_SHIFT);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400146 pr_info("Forcing RAM used on node %ld to no more than %dMB\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400147 node, maxnodemem_pfn[node] >> (20 - PAGE_SHIFT));
148 return 0;
149}
150early_param("maxnodemem", setup_maxnodemem);
151
152static int __init setup_isolnodes(char *str)
153{
154 char buf[MAX_NUMNODES * 5];
155 if (str == NULL || nodelist_parse(str, isolnodes) != 0)
156 return -EINVAL;
157
158 nodelist_scnprintf(buf, sizeof(buf), isolnodes);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400159 pr_info("Set isolnodes value to '%s'\n", buf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400160 return 0;
161}
162early_param("isolnodes", setup_isolnodes);
163
164#ifdef CONFIG_PCI
165static int __init setup_pci_reserve(char* str)
166{
167 unsigned long mb;
168
169 if (str == NULL || strict_strtoul(str, 0, &mb) != 0 ||
170 mb > 3 * 1024)
171 return -EINVAL;
172
173 pci_reserve_mb = mb;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400174 pr_info("Reserving %dMB for PCIE root complex mappings\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400175 pci_reserve_mb);
176 return 0;
177}
178early_param("pci_reserve", setup_pci_reserve);
179#endif
180
181#ifndef __tilegx__
182/*
183 * vmalloc=size forces the vmalloc area to be exactly 'size' bytes.
184 * This can be used to increase (or decrease) the vmalloc area.
185 */
186static int __init parse_vmalloc(char *arg)
187{
188 if (!arg)
189 return -EINVAL;
190
191 VMALLOC_RESERVE = (memparse(arg, &arg) + PGDIR_SIZE - 1) & PGDIR_MASK;
192
193 /* See validate_va() for more on this test. */
194 if ((long)_VMALLOC_START >= 0)
195 early_panic("\"vmalloc=%#lx\" value too large: maximum %#lx\n",
196 VMALLOC_RESERVE, _VMALLOC_END - 0x80000000UL);
197
198 return 0;
199}
200early_param("vmalloc", parse_vmalloc);
201#endif
202
203#ifdef CONFIG_HIGHMEM
204/*
Chris Metcalfa78c9422010-10-14 16:23:03 -0400205 * Determine for each controller where its lowmem is mapped and how much of
206 * it is mapped there. On controller zero, the first few megabytes are
207 * already mapped in as code at MEM_SV_INTRPT, so in principle we could
208 * start our data mappings higher up, but for now we don't bother, to avoid
209 * additional confusion.
Chris Metcalf867e3592010-05-28 23:09:12 -0400210 *
211 * One question is whether, on systems with more than 768 Mb and
212 * controllers of different sizes, to map in a proportionate amount of
213 * each one, or to try to map the same amount from each controller.
214 * (E.g. if we have three controllers with 256MB, 1GB, and 256MB
215 * respectively, do we map 256MB from each, or do we map 128 MB, 512
216 * MB, and 128 MB respectively?) For now we use a proportionate
217 * solution like the latter.
218 *
219 * The VA/PA mapping demands that we align our decisions at 16 MB
220 * boundaries so that we can rapidly convert VA to PA.
221 */
222static void *__init setup_pa_va_mapping(void)
223{
224 unsigned long curr_pages = 0;
225 unsigned long vaddr = PAGE_OFFSET;
226 nodemask_t highonlynodes = isolnodes;
227 int i, j;
228
229 memset(pbase_map, -1, sizeof(pbase_map));
230 memset(vbase_map, -1, sizeof(vbase_map));
231
232 /* Node zero cannot be isolated for LOWMEM purposes. */
233 node_clear(0, highonlynodes);
234
235 /* Count up the number of pages on non-highonlynodes controllers. */
236 mappable_physpages = 0;
237 for_each_online_node(i) {
238 if (!node_isset(i, highonlynodes))
239 mappable_physpages +=
240 node_end_pfn[i] - node_start_pfn[i];
241 }
242
243 for_each_online_node(i) {
244 unsigned long start = node_start_pfn[i];
245 unsigned long end = node_end_pfn[i];
246 unsigned long size = end - start;
247 unsigned long vaddr_end;
248
249 if (node_isset(i, highonlynodes)) {
250 /* Mark this controller as having no lowmem. */
251 node_lowmem_end_pfn[i] = start;
252 continue;
253 }
254
255 curr_pages += size;
256 if (mappable_physpages > MAXMEM_PFN) {
257 vaddr_end = PAGE_OFFSET +
258 (((u64)curr_pages * MAXMEM_PFN /
259 mappable_physpages)
260 << PAGE_SHIFT);
261 } else {
262 vaddr_end = PAGE_OFFSET + (curr_pages << PAGE_SHIFT);
263 }
264 for (j = 0; vaddr < vaddr_end; vaddr += HPAGE_SIZE, ++j) {
265 unsigned long this_pfn =
266 start + (j << HUGETLB_PAGE_ORDER);
267 pbase_map[vaddr >> HPAGE_SHIFT] = this_pfn;
268 if (vbase_map[__pfn_to_highbits(this_pfn)] ==
269 (void *)-1)
270 vbase_map[__pfn_to_highbits(this_pfn)] =
271 (void *)(vaddr & HPAGE_MASK);
272 }
273 node_lowmem_end_pfn[i] = start + (j << HUGETLB_PAGE_ORDER);
274 BUG_ON(node_lowmem_end_pfn[i] > end);
275 }
276
277 /* Return highest address of any mapped memory. */
278 return (void *)vaddr;
279}
280#endif /* CONFIG_HIGHMEM */
281
282/*
283 * Register our most important memory mappings with the debug stub.
284 *
285 * This is up to 4 mappings for lowmem, one mapping per memory
286 * controller, plus one for our text segment.
287 */
Chris Metcalf0707ad32010-06-25 17:04:17 -0400288static void __cpuinit store_permanent_mappings(void)
Chris Metcalf867e3592010-05-28 23:09:12 -0400289{
290 int i;
291
292 for_each_online_node(i) {
293 HV_PhysAddr pa = ((HV_PhysAddr)node_start_pfn[i]) << PAGE_SHIFT;
294#ifdef CONFIG_HIGHMEM
295 HV_PhysAddr high_mapped_pa = node_lowmem_end_pfn[i];
296#else
297 HV_PhysAddr high_mapped_pa = node_end_pfn[i];
298#endif
299
300 unsigned long pages = high_mapped_pa - node_start_pfn[i];
301 HV_VirtAddr addr = (HV_VirtAddr) __va(pa);
302 hv_store_mapping(addr, pages << PAGE_SHIFT, pa);
303 }
304
305 hv_store_mapping((HV_VirtAddr)_stext,
306 (uint32_t)(_einittext - _stext), 0);
307}
308
309/*
310 * Use hv_inquire_physical() to populate node_{start,end}_pfn[]
311 * and node_online_map, doing suitable sanity-checking.
312 * Also set min_low_pfn, max_low_pfn, and max_pfn.
313 */
314static void __init setup_memory(void)
315{
316 int i, j;
317 int highbits_seen[NR_PA_HIGHBIT_VALUES] = { 0 };
318#ifdef CONFIG_HIGHMEM
319 long highmem_pages;
320#endif
321#ifndef __tilegx__
322 int cap;
323#endif
324#if defined(CONFIG_HIGHMEM) || defined(__tilegx__)
325 long lowmem_pages;
326#endif
327
328 /* We are using a char to hold the cpu_2_node[] mapping */
Chris Metcalfe18105c2010-10-14 16:50:26 -0400329 BUILD_BUG_ON(MAX_NUMNODES > 127);
Chris Metcalf867e3592010-05-28 23:09:12 -0400330
331 /* Discover the ranges of memory available to us */
332 for (i = 0; ; ++i) {
333 unsigned long start, size, end, highbits;
334 HV_PhysAddrRange range = hv_inquire_physical(i);
335 if (range.size == 0)
336 break;
337#ifdef CONFIG_FLATMEM
338 if (i > 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400339 pr_err("Can't use discontiguous PAs: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400340 range.size, range.start + range.size);
341 continue;
342 }
343#endif
344#ifndef __tilegx__
345 if ((unsigned long)range.start) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400346 pr_err("Range not at 4GB multiple: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400347 range.start, range.start + range.size);
348 continue;
349 }
350#endif
351 if ((range.start & (HPAGE_SIZE-1)) != 0 ||
352 (range.size & (HPAGE_SIZE-1)) != 0) {
353 unsigned long long start_pa = range.start;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400354 unsigned long long orig_size = range.size;
Chris Metcalf867e3592010-05-28 23:09:12 -0400355 range.start = (start_pa + HPAGE_SIZE - 1) & HPAGE_MASK;
356 range.size -= (range.start - start_pa);
357 range.size &= HPAGE_MASK;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400358 pr_err("Range not hugepage-aligned: %#llx..%#llx:"
Chris Metcalf867e3592010-05-28 23:09:12 -0400359 " now %#llx-%#llx\n",
Chris Metcalf0707ad32010-06-25 17:04:17 -0400360 start_pa, start_pa + orig_size,
Chris Metcalf867e3592010-05-28 23:09:12 -0400361 range.start, range.start + range.size);
362 }
363 highbits = __pa_to_highbits(range.start);
364 if (highbits >= NR_PA_HIGHBIT_VALUES) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400365 pr_err("PA high bits too high: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400366 range.start, range.start + range.size);
367 continue;
368 }
369 if (highbits_seen[highbits]) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400370 pr_err("Range overlaps in high bits: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400371 range.start, range.start + range.size);
372 continue;
373 }
374 highbits_seen[highbits] = 1;
375 if (PFN_DOWN(range.size) > maxnodemem_pfn[i]) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400376 int max_size = maxnodemem_pfn[i];
377 if (max_size > 0) {
378 pr_err("Maxnodemem reduced node %d to"
379 " %d pages\n", i, max_size);
380 range.size = PFN_PHYS(max_size);
Chris Metcalf867e3592010-05-28 23:09:12 -0400381 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400382 pr_err("Maxnodemem disabled node %d\n", i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400383 continue;
384 }
385 }
386 if (num_physpages + PFN_DOWN(range.size) > maxmem_pfn) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400387 int max_size = maxmem_pfn - num_physpages;
388 if (max_size > 0) {
389 pr_err("Maxmem reduced node %d to %d pages\n",
390 i, max_size);
391 range.size = PFN_PHYS(max_size);
Chris Metcalf867e3592010-05-28 23:09:12 -0400392 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400393 pr_err("Maxmem disabled node %d\n", i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400394 continue;
395 }
396 }
397 if (i >= MAX_NUMNODES) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400398 pr_err("Too many PA nodes (#%d): %#llx...%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400399 i, range.size, range.size + range.start);
400 continue;
401 }
402
403 start = range.start >> PAGE_SHIFT;
404 size = range.size >> PAGE_SHIFT;
405 end = start + size;
406
407#ifndef __tilegx__
408 if (((HV_PhysAddr)end << PAGE_SHIFT) !=
409 (range.start + range.size)) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400410 pr_err("PAs too high to represent: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400411 range.start, range.start + range.size);
412 continue;
413 }
414#endif
415#ifdef CONFIG_PCI
416 /*
417 * Blocks that overlap the pci reserved region must
418 * have enough space to hold the maximum percpu data
419 * region at the top of the range. If there isn't
420 * enough space above the reserved region, just
421 * truncate the node.
422 */
423 if (start <= pci_reserve_start_pfn &&
424 end > pci_reserve_start_pfn) {
425 unsigned int per_cpu_size =
426 __per_cpu_end - __per_cpu_start;
427 unsigned int percpu_pages =
428 NR_CPUS * (PFN_UP(per_cpu_size) >> PAGE_SHIFT);
429 if (end < pci_reserve_end_pfn + percpu_pages) {
430 end = pci_reserve_start_pfn;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400431 pr_err("PCI mapping region reduced node %d to"
Chris Metcalf867e3592010-05-28 23:09:12 -0400432 " %ld pages\n", i, end - start);
433 }
434 }
435#endif
436
437 for (j = __pfn_to_highbits(start);
438 j <= __pfn_to_highbits(end - 1); j++)
439 highbits_to_node[j] = i;
440
441 node_start_pfn[i] = start;
442 node_end_pfn[i] = end;
443 node_controller[i] = range.controller;
444 num_physpages += size;
445 max_pfn = end;
446
447 /* Mark node as online */
448 node_set(i, node_online_map);
449 node_set(i, node_possible_map);
450 }
451
452#ifndef __tilegx__
453 /*
454 * For 4KB pages, mem_map "struct page" data is 1% of the size
455 * of the physical memory, so can be quite big (640 MB for
456 * four 16G zones). These structures must be mapped in
457 * lowmem, and since we currently cap out at about 768 MB,
458 * it's impractical to try to use this much address space.
459 * For now, arbitrarily cap the amount of physical memory
460 * we're willing to use at 8 million pages (32GB of 4KB pages).
461 */
462 cap = 8 * 1024 * 1024; /* 8 million pages */
463 if (num_physpages > cap) {
464 int num_nodes = num_online_nodes();
465 int cap_each = cap / num_nodes;
466 unsigned long dropped_pages = 0;
467 for (i = 0; i < num_nodes; ++i) {
468 int size = node_end_pfn[i] - node_start_pfn[i];
469 if (size > cap_each) {
470 dropped_pages += (size - cap_each);
471 node_end_pfn[i] = node_start_pfn[i] + cap_each;
472 }
473 }
474 num_physpages -= dropped_pages;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400475 pr_warning("Only using %ldMB memory;"
Chris Metcalf867e3592010-05-28 23:09:12 -0400476 " ignoring %ldMB.\n",
477 num_physpages >> (20 - PAGE_SHIFT),
478 dropped_pages >> (20 - PAGE_SHIFT));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400479 pr_warning("Consider using a larger page size.\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400480 }
481#endif
482
483 /* Heap starts just above the last loaded address. */
484 min_low_pfn = PFN_UP((unsigned long)_end - PAGE_OFFSET);
485
486#ifdef CONFIG_HIGHMEM
487 /* Find where we map lowmem from each controller. */
488 high_memory = setup_pa_va_mapping();
489
490 /* Set max_low_pfn based on what node 0 can directly address. */
491 max_low_pfn = node_lowmem_end_pfn[0];
492
493 lowmem_pages = (mappable_physpages > MAXMEM_PFN) ?
494 MAXMEM_PFN : mappable_physpages;
495 highmem_pages = (long) (num_physpages - lowmem_pages);
496
Chris Metcalf0707ad32010-06-25 17:04:17 -0400497 pr_notice("%ldMB HIGHMEM available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400498 pages_to_mb(highmem_pages > 0 ? highmem_pages : 0));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400499 pr_notice("%ldMB LOWMEM available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400500 pages_to_mb(lowmem_pages));
501#else
502 /* Set max_low_pfn based on what node 0 can directly address. */
503 max_low_pfn = node_end_pfn[0];
504
505#ifndef __tilegx__
506 if (node_end_pfn[0] > MAXMEM_PFN) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400507 pr_warning("Only using %ldMB LOWMEM.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400508 MAXMEM>>20);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400509 pr_warning("Use a HIGHMEM enabled kernel.\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400510 max_low_pfn = MAXMEM_PFN;
511 max_pfn = MAXMEM_PFN;
512 num_physpages = MAXMEM_PFN;
513 node_end_pfn[0] = MAXMEM_PFN;
514 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400515 pr_notice("%ldMB memory available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400516 pages_to_mb(node_end_pfn[0]));
517 }
518 for (i = 1; i < MAX_NUMNODES; ++i) {
519 node_start_pfn[i] = 0;
520 node_end_pfn[i] = 0;
521 }
522 high_memory = __va(node_end_pfn[0]);
523#else
524 lowmem_pages = 0;
525 for (i = 0; i < MAX_NUMNODES; ++i) {
526 int pages = node_end_pfn[i] - node_start_pfn[i];
527 lowmem_pages += pages;
528 if (pages)
529 high_memory = pfn_to_kaddr(node_end_pfn[i]);
530 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400531 pr_notice("%ldMB memory available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400532 pages_to_mb(lowmem_pages));
533#endif
534#endif
535}
536
537static void __init setup_bootmem_allocator(void)
538{
539 unsigned long bootmap_size, first_alloc_pfn, last_alloc_pfn;
540
541 /* Provide a node 0 bdata. */
542 NODE_DATA(0)->bdata = &node0_bdata;
543
544#ifdef CONFIG_PCI
545 /* Don't let boot memory alias the PCI region. */
546 last_alloc_pfn = min(max_low_pfn, pci_reserve_start_pfn);
547#else
548 last_alloc_pfn = max_low_pfn;
549#endif
550
551 /*
552 * Initialize the boot-time allocator (with low memory only):
553 * The first argument says where to put the bitmap, and the
554 * second says where the end of allocatable memory is.
555 */
556 bootmap_size = init_bootmem(min_low_pfn, last_alloc_pfn);
557
558 /*
559 * Let the bootmem allocator use all the space we've given it
560 * except for its own bitmap.
561 */
562 first_alloc_pfn = min_low_pfn + PFN_UP(bootmap_size);
563 if (first_alloc_pfn >= last_alloc_pfn)
564 early_panic("Not enough memory on controller 0 for bootmem\n");
565
566 free_bootmem(PFN_PHYS(first_alloc_pfn),
567 PFN_PHYS(last_alloc_pfn - first_alloc_pfn));
568
569#ifdef CONFIG_KEXEC
570 if (crashk_res.start != crashk_res.end)
Joe Perches28f65c112011-06-09 09:13:32 -0700571 reserve_bootmem(crashk_res.start, resource_size(&crashk_res), 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400572#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400573}
574
575void *__init alloc_remap(int nid, unsigned long size)
576{
577 int pages = node_end_pfn[nid] - node_start_pfn[nid];
578 void *map = pfn_to_kaddr(node_memmap_pfn[nid]);
579 BUG_ON(size != pages * sizeof(struct page));
580 memset(map, 0, size);
581 return map;
582}
583
584static int __init percpu_size(void)
585{
Chris Metcalf76c567f2011-02-28 16:37:34 -0500586 int size = __per_cpu_end - __per_cpu_start;
587 size += PERCPU_MODULE_RESERVE;
588 size += PERCPU_DYNAMIC_EARLY_SIZE;
589 if (size < PCPU_MIN_UNIT_SIZE)
590 size = PCPU_MIN_UNIT_SIZE;
591 size = roundup(size, PAGE_SIZE);
592
Chris Metcalf867e3592010-05-28 23:09:12 -0400593 /* In several places we assume the per-cpu data fits on a huge page. */
594 BUG_ON(kdata_huge && size > HPAGE_SIZE);
595 return size;
596}
597
598static inline unsigned long alloc_bootmem_pfn(int size, unsigned long goal)
599{
600 void *kva = __alloc_bootmem(size, PAGE_SIZE, goal);
601 unsigned long pfn = kaddr_to_pfn(kva);
602 BUG_ON(goal && PFN_PHYS(pfn) != goal);
603 return pfn;
604}
605
606static void __init zone_sizes_init(void)
607{
608 unsigned long zones_size[MAX_NR_ZONES] = { 0 };
Chris Metcalf867e3592010-05-28 23:09:12 -0400609 int size = percpu_size();
610 int num_cpus = smp_height * smp_width;
611 int i;
612
613 for (i = 0; i < num_cpus; ++i)
614 node_percpu[cpu_to_node(i)] += size;
615
616 for_each_online_node(i) {
617 unsigned long start = node_start_pfn[i];
618 unsigned long end = node_end_pfn[i];
619#ifdef CONFIG_HIGHMEM
620 unsigned long lowmem_end = node_lowmem_end_pfn[i];
621#else
622 unsigned long lowmem_end = end;
623#endif
624 int memmap_size = (end - start) * sizeof(struct page);
625 node_free_pfn[i] = start;
626
627 /*
628 * Set aside pages for per-cpu data and the mem_map array.
629 *
630 * Since the per-cpu data requires special homecaching,
631 * if we are in kdata_huge mode, we put it at the end of
632 * the lowmem region. If we're not in kdata_huge mode,
633 * we take the per-cpu pages from the bottom of the
634 * controller, since that avoids fragmenting a huge page
635 * that users might want. We always take the memmap
636 * from the bottom of the controller, since with
637 * kdata_huge that lets it be under a huge TLB entry.
638 *
639 * If the user has requested isolnodes for a controller,
640 * though, there'll be no lowmem, so we just alloc_bootmem
641 * the memmap. There will be no percpu memory either.
642 */
643 if (__pfn_to_highbits(start) == 0) {
644 /* In low PAs, allocate via bootmem. */
645 unsigned long goal = 0;
646 node_memmap_pfn[i] =
647 alloc_bootmem_pfn(memmap_size, goal);
648 if (kdata_huge)
649 goal = PFN_PHYS(lowmem_end) - node_percpu[i];
650 if (node_percpu[i])
651 node_percpu_pfn[i] =
652 alloc_bootmem_pfn(node_percpu[i], goal);
653 } else if (cpu_isset(i, isolnodes)) {
654 node_memmap_pfn[i] = alloc_bootmem_pfn(memmap_size, 0);
655 BUG_ON(node_percpu[i] != 0);
656 } else {
657 /* In high PAs, just reserve some pages. */
658 node_memmap_pfn[i] = node_free_pfn[i];
659 node_free_pfn[i] += PFN_UP(memmap_size);
660 if (!kdata_huge) {
661 node_percpu_pfn[i] = node_free_pfn[i];
662 node_free_pfn[i] += PFN_UP(node_percpu[i]);
663 } else {
664 node_percpu_pfn[i] =
665 lowmem_end - PFN_UP(node_percpu[i]);
666 }
667 }
668
669#ifdef CONFIG_HIGHMEM
670 if (start > lowmem_end) {
671 zones_size[ZONE_NORMAL] = 0;
672 zones_size[ZONE_HIGHMEM] = end - start;
673 } else {
674 zones_size[ZONE_NORMAL] = lowmem_end - start;
675 zones_size[ZONE_HIGHMEM] = end - lowmem_end;
676 }
677#else
678 zones_size[ZONE_NORMAL] = end - start;
679#endif
680
681 /*
682 * Everyone shares node 0's bootmem allocator, but
683 * we use alloc_remap(), above, to put the actual
684 * struct page array on the individual controllers,
685 * which is most of the data that we actually care about.
686 * We can't place bootmem allocators on the other
687 * controllers since the bootmem allocator can only
688 * operate on 32-bit physical addresses.
689 */
690 NODE_DATA(i)->bdata = NODE_DATA(0)->bdata;
691
692 free_area_init_node(i, zones_size, start, NULL);
Chris Metcalf76c567f2011-02-28 16:37:34 -0500693 printk(KERN_DEBUG " Normal zone: %ld per-cpu pages\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400694 PFN_UP(node_percpu[i]));
695
696 /* Track the type of memory on each node */
697 if (zones_size[ZONE_NORMAL])
698 node_set_state(i, N_NORMAL_MEMORY);
699#ifdef CONFIG_HIGHMEM
700 if (end != start)
701 node_set_state(i, N_HIGH_MEMORY);
702#endif
703
704 node_set_online(i);
705 }
706}
707
708#ifdef CONFIG_NUMA
709
710/* which logical CPUs are on which nodes */
711struct cpumask node_2_cpu_mask[MAX_NUMNODES] __write_once;
712EXPORT_SYMBOL(node_2_cpu_mask);
713
714/* which node each logical CPU is on */
715char cpu_2_node[NR_CPUS] __write_once __attribute__((aligned(L2_CACHE_BYTES)));
716EXPORT_SYMBOL(cpu_2_node);
717
718/* Return cpu_to_node() except for cpus not yet assigned, which return -1 */
719static int __init cpu_to_bound_node(int cpu, struct cpumask* unbound_cpus)
720{
721 if (!cpu_possible(cpu) || cpumask_test_cpu(cpu, unbound_cpus))
722 return -1;
723 else
724 return cpu_to_node(cpu);
725}
726
727/* Return number of immediately-adjacent tiles sharing the same NUMA node. */
728static int __init node_neighbors(int node, int cpu,
729 struct cpumask *unbound_cpus)
730{
731 int neighbors = 0;
732 int w = smp_width;
733 int h = smp_height;
734 int x = cpu % w;
735 int y = cpu / w;
736 if (x > 0 && cpu_to_bound_node(cpu-1, unbound_cpus) == node)
737 ++neighbors;
738 if (x < w-1 && cpu_to_bound_node(cpu+1, unbound_cpus) == node)
739 ++neighbors;
740 if (y > 0 && cpu_to_bound_node(cpu-w, unbound_cpus) == node)
741 ++neighbors;
742 if (y < h-1 && cpu_to_bound_node(cpu+w, unbound_cpus) == node)
743 ++neighbors;
744 return neighbors;
745}
746
747static void __init setup_numa_mapping(void)
748{
749 int distance[MAX_NUMNODES][NR_CPUS];
750 HV_Coord coord;
751 int cpu, node, cpus, i, x, y;
752 int num_nodes = num_online_nodes();
753 struct cpumask unbound_cpus;
754 nodemask_t default_nodes;
755
756 cpumask_clear(&unbound_cpus);
757
758 /* Get set of nodes we will use for defaults */
759 nodes_andnot(default_nodes, node_online_map, isolnodes);
760 if (nodes_empty(default_nodes)) {
761 BUG_ON(!node_isset(0, node_online_map));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400762 pr_err("Forcing NUMA node zero available as a default node\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400763 node_set(0, default_nodes);
764 }
765
766 /* Populate the distance[] array */
767 memset(distance, -1, sizeof(distance));
768 cpu = 0;
769 for (coord.y = 0; coord.y < smp_height; ++coord.y) {
770 for (coord.x = 0; coord.x < smp_width;
771 ++coord.x, ++cpu) {
772 BUG_ON(cpu >= nr_cpu_ids);
773 if (!cpu_possible(cpu)) {
774 cpu_2_node[cpu] = -1;
775 continue;
776 }
777 for_each_node_mask(node, default_nodes) {
778 HV_MemoryControllerInfo info =
779 hv_inquire_memory_controller(
780 coord, node_controller[node]);
781 distance[node][cpu] =
782 ABS(info.coord.x) + ABS(info.coord.y);
783 }
784 cpumask_set_cpu(cpu, &unbound_cpus);
785 }
786 }
787 cpus = cpu;
788
789 /*
790 * Round-robin through the NUMA nodes until all the cpus are
791 * assigned. We could be more clever here (e.g. create four
792 * sorted linked lists on the same set of cpu nodes, and pull
793 * off them in round-robin sequence, removing from all four
794 * lists each time) but given the relatively small numbers
795 * involved, O(n^2) seem OK for a one-time cost.
796 */
797 node = first_node(default_nodes);
798 while (!cpumask_empty(&unbound_cpus)) {
799 int best_cpu = -1;
800 int best_distance = INT_MAX;
801 for (cpu = 0; cpu < cpus; ++cpu) {
802 if (cpumask_test_cpu(cpu, &unbound_cpus)) {
803 /*
804 * Compute metric, which is how much
805 * closer the cpu is to this memory
806 * controller than the others, shifted
807 * up, and then the number of
808 * neighbors already in the node as an
809 * epsilon adjustment to try to keep
810 * the nodes compact.
811 */
812 int d = distance[node][cpu] * num_nodes;
813 for_each_node_mask(i, default_nodes) {
814 if (i != node)
815 d -= distance[i][cpu];
816 }
817 d *= 8; /* allow space for epsilon */
818 d -= node_neighbors(node, cpu, &unbound_cpus);
819 if (d < best_distance) {
820 best_cpu = cpu;
821 best_distance = d;
822 }
823 }
824 }
825 BUG_ON(best_cpu < 0);
826 cpumask_set_cpu(best_cpu, &node_2_cpu_mask[node]);
827 cpu_2_node[best_cpu] = node;
828 cpumask_clear_cpu(best_cpu, &unbound_cpus);
829 node = next_node(node, default_nodes);
830 if (node == MAX_NUMNODES)
831 node = first_node(default_nodes);
832 }
833
834 /* Print out node assignments and set defaults for disabled cpus */
835 cpu = 0;
836 for (y = 0; y < smp_height; ++y) {
837 printk(KERN_DEBUG "NUMA cpu-to-node row %d:", y);
838 for (x = 0; x < smp_width; ++x, ++cpu) {
839 if (cpu_to_node(cpu) < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400840 pr_cont(" -");
Chris Metcalf867e3592010-05-28 23:09:12 -0400841 cpu_2_node[cpu] = first_node(default_nodes);
842 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400843 pr_cont(" %d", cpu_to_node(cpu));
Chris Metcalf867e3592010-05-28 23:09:12 -0400844 }
845 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400846 pr_cont("\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400847 }
848}
849
850static struct cpu cpu_devices[NR_CPUS];
851
852static int __init topology_init(void)
853{
854 int i;
855
856 for_each_online_node(i)
857 register_one_node(i);
858
Chris Metcalf4d658d12010-11-24 13:42:15 -0500859 for (i = 0; i < smp_height * smp_width; ++i)
Chris Metcalf867e3592010-05-28 23:09:12 -0400860 register_cpu(&cpu_devices[i], i);
861
862 return 0;
863}
864
865subsys_initcall(topology_init);
866
867#else /* !CONFIG_NUMA */
868
869#define setup_numa_mapping() do { } while (0)
870
871#endif /* CONFIG_NUMA */
872
873/**
Chris Metcalf0707ad32010-06-25 17:04:17 -0400874 * setup_cpu() - Do all necessary per-cpu, tile-specific initialization.
875 * @boot: Is this the boot cpu?
Chris Metcalf867e3592010-05-28 23:09:12 -0400876 *
Chris Metcalf0707ad32010-06-25 17:04:17 -0400877 * Called from setup_arch() on the boot cpu, or online_secondary().
Chris Metcalf867e3592010-05-28 23:09:12 -0400878 */
Chris Metcalf0707ad32010-06-25 17:04:17 -0400879void __cpuinit setup_cpu(int boot)
Chris Metcalf867e3592010-05-28 23:09:12 -0400880{
Chris Metcalf0707ad32010-06-25 17:04:17 -0400881 /* The boot cpu sets up its permanent mappings much earlier. */
882 if (!boot)
883 store_permanent_mappings();
884
Chris Metcalf867e3592010-05-28 23:09:12 -0400885 /* Allow asynchronous TLB interrupts. */
886#if CHIP_HAS_TILE_DMA()
Chris Metcalf5d966112010-11-01 15:24:29 -0400887 arch_local_irq_unmask(INT_DMATLB_MISS);
888 arch_local_irq_unmask(INT_DMATLB_ACCESS);
Chris Metcalf867e3592010-05-28 23:09:12 -0400889#endif
890#if CHIP_HAS_SN_PROC()
Chris Metcalf5d966112010-11-01 15:24:29 -0400891 arch_local_irq_unmask(INT_SNITLB_MISS);
Chris Metcalf867e3592010-05-28 23:09:12 -0400892#endif
Chris Metcalfa78c9422010-10-14 16:23:03 -0400893#ifdef __tilegx__
Chris Metcalf5d966112010-11-01 15:24:29 -0400894 arch_local_irq_unmask(INT_SINGLE_STEP_K);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400895#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400896
897 /*
898 * Allow user access to many generic SPRs, like the cycle
899 * counter, PASS/FAIL/DONE, INTERRUPT_CRITICAL_SECTION, etc.
900 */
901 __insn_mtspr(SPR_MPL_WORLD_ACCESS_SET_0, 1);
902
903#if CHIP_HAS_SN()
904 /* Static network is not restricted. */
905 __insn_mtspr(SPR_MPL_SN_ACCESS_SET_0, 1);
906#endif
907#if CHIP_HAS_SN_PROC()
908 __insn_mtspr(SPR_MPL_SN_NOTIFY_SET_0, 1);
909 __insn_mtspr(SPR_MPL_SN_CPL_SET_0, 1);
910#endif
911
912 /*
Chris Metcalfa78c9422010-10-14 16:23:03 -0400913 * Set the MPL for interrupt control 0 & 1 to the corresponding
914 * values. This includes access to the SYSTEM_SAVE and EX_CONTEXT
915 * SPRs, as well as the interrupt mask.
Chris Metcalf867e3592010-05-28 23:09:12 -0400916 */
917 __insn_mtspr(SPR_MPL_INTCTRL_0_SET_0, 1);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400918 __insn_mtspr(SPR_MPL_INTCTRL_1_SET_1, 1);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400919
920 /* Initialize IRQ support for this cpu. */
921 setup_irq_regs();
922
923#ifdef CONFIG_HARDWALL
924 /* Reset the network state on this cpu. */
925 reset_network_state();
926#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400927}
928
Chris Metcalf43d9ebb2011-05-02 15:51:32 -0400929#ifdef CONFIG_BLK_DEV_INITRD
930
Chris Metcalf51bcdf82012-03-29 15:29:28 -0400931/*
932 * Note that the kernel can potentially support other compression
933 * techniques than gz, though we don't do so by default. If we ever
934 * decide to do so we can either look for other filename extensions,
935 * or just allow a file with this name to be compressed with an
936 * arbitrary compressor (somewhat counterintuitively).
937 */
Chris Metcalf867e3592010-05-28 23:09:12 -0400938static int __initdata set_initramfs_file;
939static char __initdata initramfs_file[128] = "initramfs.cpio.gz";
940
941static int __init setup_initramfs_file(char *str)
942{
943 if (str == NULL)
944 return -EINVAL;
945 strncpy(initramfs_file, str, sizeof(initramfs_file) - 1);
946 set_initramfs_file = 1;
947
948 return 0;
949}
950early_param("initramfs_file", setup_initramfs_file);
951
952/*
Chris Metcalf51bcdf82012-03-29 15:29:28 -0400953 * We look for an "initramfs.cpio.gz" file in the hvfs.
Chris Metcalf867e3592010-05-28 23:09:12 -0400954 * If there is one, we allocate some memory for it and it will be
Chris Metcalf51bcdf82012-03-29 15:29:28 -0400955 * unpacked to the initramfs.
Chris Metcalf867e3592010-05-28 23:09:12 -0400956 */
957static void __init load_hv_initrd(void)
958{
959 HV_FS_StatInfo stat;
960 int fd, rc;
961 void *initrd;
962
963 fd = hv_fs_findfile((HV_VirtAddr) initramfs_file);
964 if (fd == HV_ENOENT) {
965 if (set_initramfs_file)
Chris Metcalf0707ad32010-06-25 17:04:17 -0400966 pr_warning("No such hvfs initramfs file '%s'\n",
967 initramfs_file);
Chris Metcalf867e3592010-05-28 23:09:12 -0400968 return;
969 }
970 BUG_ON(fd < 0);
971 stat = hv_fs_fstat(fd);
972 BUG_ON(stat.size < 0);
973 if (stat.flags & HV_FS_ISDIR) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400974 pr_warning("Ignoring hvfs file '%s': it's a directory.\n",
975 initramfs_file);
Chris Metcalf867e3592010-05-28 23:09:12 -0400976 return;
977 }
978 initrd = alloc_bootmem_pages(stat.size);
979 rc = hv_fs_pread(fd, (HV_VirtAddr) initrd, stat.size, 0);
980 if (rc != stat.size) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400981 pr_err("Error reading %d bytes from hvfs file '%s': %d\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400982 stat.size, initramfs_file, rc);
Chris Metcalfbc63de72010-08-13 08:23:07 -0400983 free_initrd_mem((unsigned long) initrd, stat.size);
Chris Metcalf867e3592010-05-28 23:09:12 -0400984 return;
985 }
986 initrd_start = (unsigned long) initrd;
987 initrd_end = initrd_start + stat.size;
988}
989
990void __init free_initrd_mem(unsigned long begin, unsigned long end)
991{
Chris Metcalfbc63de72010-08-13 08:23:07 -0400992 free_bootmem(__pa(begin), end - begin);
Chris Metcalf867e3592010-05-28 23:09:12 -0400993}
994
Chris Metcalf43d9ebb2011-05-02 15:51:32 -0400995#else
996static inline void load_hv_initrd(void) {}
997#endif /* CONFIG_BLK_DEV_INITRD */
998
Chris Metcalf867e3592010-05-28 23:09:12 -0400999static void __init validate_hv(void)
1000{
1001 /*
1002 * It may already be too late, but let's check our built-in
1003 * configuration against what the hypervisor is providing.
1004 */
1005 unsigned long glue_size = hv_sysconf(HV_SYSCONF_GLUE_SIZE);
1006 int hv_page_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_SMALL);
1007 int hv_hpage_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_LARGE);
1008 HV_ASIDRange asid_range;
1009
1010#ifndef CONFIG_SMP
1011 HV_Topology topology = hv_inquire_topology();
1012 BUG_ON(topology.coord.x != 0 || topology.coord.y != 0);
1013 if (topology.width != 1 || topology.height != 1) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001014 pr_warning("Warning: booting UP kernel on %dx%d grid;"
1015 " will ignore all but first tile.\n",
1016 topology.width, topology.height);
Chris Metcalf867e3592010-05-28 23:09:12 -04001017 }
1018#endif
1019
1020 if (PAGE_OFFSET + HV_GLUE_START_CPA + glue_size > (unsigned long)_text)
1021 early_panic("Hypervisor glue size %ld is too big!\n",
1022 glue_size);
1023 if (hv_page_size != PAGE_SIZE)
1024 early_panic("Hypervisor page size %#x != our %#lx\n",
1025 hv_page_size, PAGE_SIZE);
1026 if (hv_hpage_size != HPAGE_SIZE)
1027 early_panic("Hypervisor huge page size %#x != our %#lx\n",
1028 hv_hpage_size, HPAGE_SIZE);
1029
1030#ifdef CONFIG_SMP
1031 /*
1032 * Some hypervisor APIs take a pointer to a bitmap array
1033 * whose size is at least the number of cpus on the chip.
1034 * We use a struct cpumask for this, so it must be big enough.
1035 */
1036 if ((smp_height * smp_width) > nr_cpu_ids)
1037 early_panic("Hypervisor %d x %d grid too big for Linux"
1038 " NR_CPUS %d\n", smp_height, smp_width,
1039 nr_cpu_ids);
1040#endif
1041
1042 /*
1043 * Check that we're using allowed ASIDs, and initialize the
1044 * various asid variables to their appropriate initial states.
1045 */
1046 asid_range = hv_inquire_asid(0);
1047 __get_cpu_var(current_asid) = min_asid = asid_range.start;
1048 max_asid = asid_range.start + asid_range.size - 1;
1049
1050 if (hv_confstr(HV_CONFSTR_CHIP_MODEL, (HV_VirtAddr)chip_model,
1051 sizeof(chip_model)) < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001052 pr_err("Warning: HV_CONFSTR_CHIP_MODEL not available\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001053 strlcpy(chip_model, "unknown", sizeof(chip_model));
1054 }
1055}
1056
1057static void __init validate_va(void)
1058{
1059#ifndef __tilegx__ /* FIXME: GX: probably some validation relevant here */
1060 /*
1061 * Similarly, make sure we're only using allowed VAs.
1062 * We assume we can contiguously use MEM_USER_INTRPT .. MEM_HV_INTRPT,
1063 * and 0 .. KERNEL_HIGH_VADDR.
1064 * In addition, make sure we CAN'T use the end of memory, since
1065 * we use the last chunk of each pgd for the pgd_list.
1066 */
Chris Metcalfa78c9422010-10-14 16:23:03 -04001067 int i, user_kernel_ok = 0;
Chris Metcalf867e3592010-05-28 23:09:12 -04001068 unsigned long max_va = 0;
1069 unsigned long list_va =
1070 ((PGD_LIST_OFFSET / sizeof(pgd_t)) << PGDIR_SHIFT);
1071
1072 for (i = 0; ; ++i) {
1073 HV_VirtAddrRange range = hv_inquire_virtual(i);
1074 if (range.size == 0)
1075 break;
1076 if (range.start <= MEM_USER_INTRPT &&
1077 range.start + range.size >= MEM_HV_INTRPT)
Chris Metcalfa78c9422010-10-14 16:23:03 -04001078 user_kernel_ok = 1;
Chris Metcalf867e3592010-05-28 23:09:12 -04001079 if (range.start == 0)
1080 max_va = range.size;
1081 BUG_ON(range.start + range.size > list_va);
1082 }
Chris Metcalfa78c9422010-10-14 16:23:03 -04001083 if (!user_kernel_ok)
1084 early_panic("Hypervisor not configured for user/kernel VAs\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001085 if (max_va == 0)
1086 early_panic("Hypervisor not configured for low VAs\n");
1087 if (max_va < KERNEL_HIGH_VADDR)
1088 early_panic("Hypervisor max VA %#lx smaller than %#lx\n",
1089 max_va, KERNEL_HIGH_VADDR);
1090
1091 /* Kernel PCs must have their high bit set; see intvec.S. */
1092 if ((long)VMALLOC_START >= 0)
1093 early_panic(
1094 "Linux VMALLOC region below the 2GB line (%#lx)!\n"
1095 "Reconfigure the kernel with fewer NR_HUGE_VMAPS\n"
1096 "or smaller VMALLOC_RESERVE.\n",
1097 VMALLOC_START);
1098#endif
1099}
1100
1101/*
1102 * cpu_lotar_map lists all the cpus that are valid for the supervisor
1103 * to cache data on at a page level, i.e. what cpus can be placed in
1104 * the LOTAR field of a PTE. It is equivalent to the set of possible
1105 * cpus plus any other cpus that are willing to share their cache.
1106 * It is set by hv_inquire_tiles(HV_INQ_TILES_LOTAR).
1107 */
1108struct cpumask __write_once cpu_lotar_map;
1109EXPORT_SYMBOL(cpu_lotar_map);
1110
1111#if CHIP_HAS_CBOX_HOME_MAP()
1112/*
1113 * hash_for_home_map lists all the tiles that hash-for-home data
1114 * will be cached on. Note that this may includes tiles that are not
1115 * valid for this supervisor to use otherwise (e.g. if a hypervisor
1116 * device is being shared between multiple supervisors).
1117 * It is set by hv_inquire_tiles(HV_INQ_TILES_HFH_CACHE).
1118 */
1119struct cpumask hash_for_home_map;
1120EXPORT_SYMBOL(hash_for_home_map);
1121#endif
1122
1123/*
1124 * cpu_cacheable_map lists all the cpus whose caches the hypervisor can
Rusty Russell5f054e32012-03-29 15:38:31 +10301125 * flush on our behalf. It is set to cpu_possible_mask OR'ed with
Chris Metcalf867e3592010-05-28 23:09:12 -04001126 * hash_for_home_map, and it is what should be passed to
1127 * hv_flush_remote() to flush all caches. Note that if there are
1128 * dedicated hypervisor driver tiles that have authorized use of their
1129 * cache, those tiles will only appear in cpu_lotar_map, NOT in
1130 * cpu_cacheable_map, as they are a special case.
1131 */
1132struct cpumask __write_once cpu_cacheable_map;
1133EXPORT_SYMBOL(cpu_cacheable_map);
1134
1135static __initdata struct cpumask disabled_map;
1136
1137static int __init disabled_cpus(char *str)
1138{
1139 int boot_cpu = smp_processor_id();
1140
1141 if (str == NULL || cpulist_parse_crop(str, &disabled_map) != 0)
1142 return -EINVAL;
1143 if (cpumask_test_cpu(boot_cpu, &disabled_map)) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001144 pr_err("disabled_cpus: can't disable boot cpu %d\n", boot_cpu);
Chris Metcalf867e3592010-05-28 23:09:12 -04001145 cpumask_clear_cpu(boot_cpu, &disabled_map);
1146 }
1147 return 0;
1148}
1149
1150early_param("disabled_cpus", disabled_cpus);
1151
Chris Metcalf0707ad32010-06-25 17:04:17 -04001152void __init print_disabled_cpus(void)
Chris Metcalf867e3592010-05-28 23:09:12 -04001153{
1154 if (!cpumask_empty(&disabled_map)) {
1155 char buf[100];
1156 cpulist_scnprintf(buf, sizeof(buf), &disabled_map);
Chris Metcalf0707ad32010-06-25 17:04:17 -04001157 pr_info("CPUs not available for Linux: %s\n", buf);
Chris Metcalf867e3592010-05-28 23:09:12 -04001158 }
1159}
1160
1161static void __init setup_cpu_maps(void)
1162{
1163 struct cpumask hv_disabled_map, cpu_possible_init;
1164 int boot_cpu = smp_processor_id();
1165 int cpus, i, rc;
1166
1167 /* Learn which cpus are allowed by the hypervisor. */
1168 rc = hv_inquire_tiles(HV_INQ_TILES_AVAIL,
1169 (HV_VirtAddr) cpumask_bits(&cpu_possible_init),
1170 sizeof(cpu_cacheable_map));
1171 if (rc < 0)
1172 early_panic("hv_inquire_tiles(AVAIL) failed: rc %d\n", rc);
1173 if (!cpumask_test_cpu(boot_cpu, &cpu_possible_init))
1174 early_panic("Boot CPU %d disabled by hypervisor!\n", boot_cpu);
1175
1176 /* Compute the cpus disabled by the hvconfig file. */
1177 cpumask_complement(&hv_disabled_map, &cpu_possible_init);
1178
1179 /* Include them with the cpus disabled by "disabled_cpus". */
1180 cpumask_or(&disabled_map, &disabled_map, &hv_disabled_map);
1181
1182 /*
1183 * Disable every cpu after "setup_max_cpus". But don't mark
1184 * as disabled the cpus that are outside of our initial rectangle,
1185 * since that turns out to be confusing.
1186 */
1187 cpus = 1; /* this cpu */
1188 cpumask_set_cpu(boot_cpu, &disabled_map); /* ignore this cpu */
1189 for (i = 0; cpus < setup_max_cpus; ++i)
1190 if (!cpumask_test_cpu(i, &disabled_map))
1191 ++cpus;
1192 for (; i < smp_height * smp_width; ++i)
1193 cpumask_set_cpu(i, &disabled_map);
1194 cpumask_clear_cpu(boot_cpu, &disabled_map); /* reset this cpu */
1195 for (i = smp_height * smp_width; i < NR_CPUS; ++i)
1196 cpumask_clear_cpu(i, &disabled_map);
1197
1198 /*
1199 * Setup cpu_possible map as every cpu allocated to us, minus
1200 * the results of any "disabled_cpus" settings.
1201 */
1202 cpumask_andnot(&cpu_possible_init, &cpu_possible_init, &disabled_map);
1203 init_cpu_possible(&cpu_possible_init);
1204
1205 /* Learn which cpus are valid for LOTAR caching. */
1206 rc = hv_inquire_tiles(HV_INQ_TILES_LOTAR,
1207 (HV_VirtAddr) cpumask_bits(&cpu_lotar_map),
1208 sizeof(cpu_lotar_map));
1209 if (rc < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001210 pr_err("warning: no HV_INQ_TILES_LOTAR; using AVAIL\n");
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301211 cpu_lotar_map = *cpu_possible_mask;
Chris Metcalf867e3592010-05-28 23:09:12 -04001212 }
1213
1214#if CHIP_HAS_CBOX_HOME_MAP()
1215 /* Retrieve set of CPUs used for hash-for-home caching */
1216 rc = hv_inquire_tiles(HV_INQ_TILES_HFH_CACHE,
1217 (HV_VirtAddr) hash_for_home_map.bits,
1218 sizeof(hash_for_home_map));
1219 if (rc < 0)
1220 early_panic("hv_inquire_tiles(HFH_CACHE) failed: rc %d\n", rc);
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301221 cpumask_or(&cpu_cacheable_map, cpu_possible_mask, &hash_for_home_map);
Chris Metcalf867e3592010-05-28 23:09:12 -04001222#else
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301223 cpu_cacheable_map = *cpu_possible_mask;
Chris Metcalf867e3592010-05-28 23:09:12 -04001224#endif
1225}
1226
1227
1228static int __init dataplane(char *str)
1229{
Chris Metcalf0707ad32010-06-25 17:04:17 -04001230 pr_warning("WARNING: dataplane support disabled in this kernel\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001231 return 0;
1232}
1233
1234early_param("dataplane", dataplane);
1235
1236#ifdef CONFIG_CMDLINE_BOOL
1237static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
1238#endif
1239
1240void __init setup_arch(char **cmdline_p)
1241{
1242 int len;
1243
1244#if defined(CONFIG_CMDLINE_BOOL) && defined(CONFIG_CMDLINE_OVERRIDE)
1245 len = hv_get_command_line((HV_VirtAddr) boot_command_line,
1246 COMMAND_LINE_SIZE);
1247 if (boot_command_line[0])
Chris Metcalf0707ad32010-06-25 17:04:17 -04001248 pr_warning("WARNING: ignoring dynamic command line \"%s\"\n",
1249 boot_command_line);
Chris Metcalf867e3592010-05-28 23:09:12 -04001250 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
1251#else
1252 char *hv_cmdline;
1253#if defined(CONFIG_CMDLINE_BOOL)
1254 if (builtin_cmdline[0]) {
1255 int builtin_len = strlcpy(boot_command_line, builtin_cmdline,
1256 COMMAND_LINE_SIZE);
1257 if (builtin_len < COMMAND_LINE_SIZE-1)
1258 boot_command_line[builtin_len++] = ' ';
1259 hv_cmdline = &boot_command_line[builtin_len];
1260 len = COMMAND_LINE_SIZE - builtin_len;
1261 } else
1262#endif
1263 {
1264 hv_cmdline = boot_command_line;
1265 len = COMMAND_LINE_SIZE;
1266 }
1267 len = hv_get_command_line((HV_VirtAddr) hv_cmdline, len);
1268 if (len < 0 || len > COMMAND_LINE_SIZE)
1269 early_panic("hv_get_command_line failed: %d\n", len);
1270#endif
1271
1272 *cmdline_p = boot_command_line;
1273
1274 /* Set disabled_map and setup_max_cpus very early */
1275 parse_early_param();
1276
1277 /* Make sure the kernel is compatible with the hypervisor. */
1278 validate_hv();
1279 validate_va();
1280
1281 setup_cpu_maps();
1282
1283
1284#ifdef CONFIG_PCI
1285 /*
1286 * Initialize the PCI structures. This is done before memory
1287 * setup so that we know whether or not a pci_reserve region
1288 * is necessary.
1289 */
1290 if (tile_pci_init() == 0)
1291 pci_reserve_mb = 0;
1292
1293 /* PCI systems reserve a region just below 4GB for mapping iomem. */
1294 pci_reserve_end_pfn = (1 << (32 - PAGE_SHIFT));
1295 pci_reserve_start_pfn = pci_reserve_end_pfn -
1296 (pci_reserve_mb << (20 - PAGE_SHIFT));
1297#endif
1298
1299 init_mm.start_code = (unsigned long) _text;
1300 init_mm.end_code = (unsigned long) _etext;
1301 init_mm.end_data = (unsigned long) _edata;
1302 init_mm.brk = (unsigned long) _end;
1303
1304 setup_memory();
1305 store_permanent_mappings();
1306 setup_bootmem_allocator();
1307
1308 /*
1309 * NOTE: before this point _nobody_ is allowed to allocate
1310 * any memory using the bootmem allocator.
1311 */
1312
1313 paging_init();
1314 setup_numa_mapping();
1315 zone_sizes_init();
1316 set_page_homes();
Chris Metcalf0707ad32010-06-25 17:04:17 -04001317 setup_cpu(1);
Chris Metcalf867e3592010-05-28 23:09:12 -04001318 setup_clock();
1319 load_hv_initrd();
1320}
1321
1322
1323/*
1324 * Set up per-cpu memory.
1325 */
1326
1327unsigned long __per_cpu_offset[NR_CPUS] __write_once;
1328EXPORT_SYMBOL(__per_cpu_offset);
1329
1330static size_t __initdata pfn_offset[MAX_NUMNODES] = { 0 };
1331static unsigned long __initdata percpu_pfn[NR_CPUS] = { 0 };
1332
1333/*
1334 * As the percpu code allocates pages, we return the pages from the
1335 * end of the node for the specified cpu.
1336 */
1337static void *__init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
1338{
1339 int nid = cpu_to_node(cpu);
1340 unsigned long pfn = node_percpu_pfn[nid] + pfn_offset[nid];
1341
1342 BUG_ON(size % PAGE_SIZE != 0);
1343 pfn_offset[nid] += size / PAGE_SIZE;
Chris Metcalf76c567f2011-02-28 16:37:34 -05001344 BUG_ON(node_percpu[nid] < size);
1345 node_percpu[nid] -= size;
Chris Metcalf867e3592010-05-28 23:09:12 -04001346 if (percpu_pfn[cpu] == 0)
1347 percpu_pfn[cpu] = pfn;
1348 return pfn_to_kaddr(pfn);
1349}
1350
1351/*
1352 * Pages reserved for percpu memory are not freeable, and in any case we are
1353 * on a short path to panic() in setup_per_cpu_area() at this point anyway.
1354 */
1355static void __init pcpu_fc_free(void *ptr, size_t size)
1356{
1357}
1358
1359/*
1360 * Set up vmalloc page tables using bootmem for the percpu code.
1361 */
1362static void __init pcpu_fc_populate_pte(unsigned long addr)
1363{
1364 pgd_t *pgd;
1365 pud_t *pud;
1366 pmd_t *pmd;
1367 pte_t *pte;
1368
1369 BUG_ON(pgd_addr_invalid(addr));
Chris Metcalf77d23302010-10-14 14:47:35 -04001370 if (addr < VMALLOC_START || addr >= VMALLOC_END)
1371 panic("PCPU addr %#lx outside vmalloc range %#lx..%#lx;"
1372 " try increasing CONFIG_VMALLOC_RESERVE\n",
1373 addr, VMALLOC_START, VMALLOC_END);
Chris Metcalf867e3592010-05-28 23:09:12 -04001374
1375 pgd = swapper_pg_dir + pgd_index(addr);
1376 pud = pud_offset(pgd, addr);
1377 BUG_ON(!pud_present(*pud));
1378 pmd = pmd_offset(pud, addr);
1379 if (pmd_present(*pmd)) {
1380 BUG_ON(pmd_huge_page(*pmd));
1381 } else {
1382 pte = __alloc_bootmem(L2_KERNEL_PGTABLE_SIZE,
1383 HV_PAGE_TABLE_ALIGN, 0);
1384 pmd_populate_kernel(&init_mm, pmd, pte);
1385 }
1386}
1387
1388void __init setup_per_cpu_areas(void)
1389{
1390 struct page *pg;
1391 unsigned long delta, pfn, lowmem_va;
1392 unsigned long size = percpu_size();
1393 char *ptr;
1394 int rc, cpu, i;
1395
1396 rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, pcpu_fc_alloc,
1397 pcpu_fc_free, pcpu_fc_populate_pte);
1398 if (rc < 0)
1399 panic("Cannot initialize percpu area (err=%d)", rc);
1400
1401 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
1402 for_each_possible_cpu(cpu) {
1403 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
1404
1405 /* finv the copy out of cache so we can change homecache */
1406 ptr = pcpu_base_addr + pcpu_unit_offsets[cpu];
1407 __finv_buffer(ptr, size);
1408 pfn = percpu_pfn[cpu];
1409
1410 /* Rewrite the page tables to cache on that cpu */
1411 pg = pfn_to_page(pfn);
1412 for (i = 0; i < size; i += PAGE_SIZE, ++pfn, ++pg) {
1413
1414 /* Update the vmalloc mapping and page home. */
1415 pte_t *ptep =
1416 virt_to_pte(NULL, (unsigned long)ptr + i);
1417 pte_t pte = *ptep;
1418 BUG_ON(pfn != pte_pfn(pte));
1419 pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_TILE_L3);
1420 pte = set_remote_cache_cpu(pte, cpu);
1421 set_pte(ptep, pte);
1422
1423 /* Update the lowmem mapping for consistency. */
1424 lowmem_va = (unsigned long)pfn_to_kaddr(pfn);
1425 ptep = virt_to_pte(NULL, lowmem_va);
1426 if (pte_huge(*ptep)) {
1427 printk(KERN_DEBUG "early shatter of huge page"
1428 " at %#lx\n", lowmem_va);
1429 shatter_pmd((pmd_t *)ptep);
1430 ptep = virt_to_pte(NULL, lowmem_va);
1431 BUG_ON(pte_huge(*ptep));
1432 }
1433 BUG_ON(pfn != pte_pfn(*ptep));
1434 set_pte(ptep, pte);
1435 }
1436 }
1437
1438 /* Set our thread pointer appropriately. */
1439 set_my_cpu_offset(__per_cpu_offset[smp_processor_id()]);
1440
1441 /* Make sure the finv's have completed. */
1442 mb_incoherent();
1443
1444 /* Flush the TLB so we reference it properly from here on out. */
1445 local_flush_tlb_all();
1446}
1447
1448static struct resource data_resource = {
1449 .name = "Kernel data",
1450 .start = 0,
1451 .end = 0,
1452 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
1453};
1454
1455static struct resource code_resource = {
1456 .name = "Kernel code",
1457 .start = 0,
1458 .end = 0,
1459 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
1460};
1461
1462/*
1463 * We reserve all resources above 4GB so that PCI won't try to put
1464 * mappings above 4GB; the standard allows that for some devices but
1465 * the probing code trunates values to 32 bits.
1466 */
1467#ifdef CONFIG_PCI
1468static struct resource* __init
1469insert_non_bus_resource(void)
1470{
1471 struct resource *res =
1472 kzalloc(sizeof(struct resource), GFP_ATOMIC);
1473 res->name = "Non-Bus Physical Address Space";
1474 res->start = (1ULL << 32);
1475 res->end = -1LL;
1476 res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1477 if (insert_resource(&iomem_resource, res)) {
1478 kfree(res);
1479 return NULL;
1480 }
1481 return res;
1482}
1483#endif
1484
1485static struct resource* __init
1486insert_ram_resource(u64 start_pfn, u64 end_pfn)
1487{
1488 struct resource *res =
1489 kzalloc(sizeof(struct resource), GFP_ATOMIC);
1490 res->name = "System RAM";
1491 res->start = start_pfn << PAGE_SHIFT;
1492 res->end = (end_pfn << PAGE_SHIFT) - 1;
1493 res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1494 if (insert_resource(&iomem_resource, res)) {
1495 kfree(res);
1496 return NULL;
1497 }
1498 return res;
1499}
1500
1501/*
1502 * Request address space for all standard resources
1503 *
1504 * If the system includes PCI root complex drivers, we need to create
1505 * a window just below 4GB where PCI BARs can be mapped.
1506 */
1507static int __init request_standard_resources(void)
1508{
1509 int i;
1510 enum { CODE_DELTA = MEM_SV_INTRPT - PAGE_OFFSET };
1511
1512 iomem_resource.end = -1LL;
1513#ifdef CONFIG_PCI
1514 insert_non_bus_resource();
1515#endif
1516
1517 for_each_online_node(i) {
1518 u64 start_pfn = node_start_pfn[i];
1519 u64 end_pfn = node_end_pfn[i];
1520
1521#ifdef CONFIG_PCI
1522 if (start_pfn <= pci_reserve_start_pfn &&
1523 end_pfn > pci_reserve_start_pfn) {
1524 if (end_pfn > pci_reserve_end_pfn)
1525 insert_ram_resource(pci_reserve_end_pfn,
1526 end_pfn);
1527 end_pfn = pci_reserve_start_pfn;
1528 }
1529#endif
1530 insert_ram_resource(start_pfn, end_pfn);
1531 }
1532
1533 code_resource.start = __pa(_text - CODE_DELTA);
1534 code_resource.end = __pa(_etext - CODE_DELTA)-1;
1535 data_resource.start = __pa(_sdata);
1536 data_resource.end = __pa(_end)-1;
1537
1538 insert_resource(&iomem_resource, &code_resource);
1539 insert_resource(&iomem_resource, &data_resource);
1540
1541#ifdef CONFIG_KEXEC
1542 insert_resource(&iomem_resource, &crashk_res);
1543#endif
1544
1545 return 0;
1546}
1547
1548subsys_initcall(request_standard_resources);