blob: fdde3b6986e54f59b117fd61c25566dde5df0dc0 [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>
Chris Metcalf621b1952012-04-01 14:04:21 -040031#include <linux/hugetlb.h>
Chris Metcalf2ded5c22012-06-06 11:23:19 -040032#include <linux/start_kernel.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040033#include <asm/setup.h>
34#include <asm/sections.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040035#include <asm/cacheflush.h>
36#include <asm/pgalloc.h>
37#include <asm/mmu_context.h>
38#include <hv/hypervisor.h>
39#include <arch/interrupts.h>
40
41/* <linux/smp.h> doesn't provide this definition. */
42#ifndef CONFIG_SMP
43#define setup_max_cpus 1
44#endif
45
46static inline int ABS(int x) { return x >= 0 ? x : -x; }
47
48/* Chip information */
49char chip_model[64] __write_once;
50
51struct pglist_data node_data[MAX_NUMNODES] __read_mostly;
52EXPORT_SYMBOL(node_data);
53
Chris Metcalf867e3592010-05-28 23:09:12 -040054/* Information on the NUMA nodes that we compute early */
55unsigned long __cpuinitdata node_start_pfn[MAX_NUMNODES];
56unsigned long __cpuinitdata node_end_pfn[MAX_NUMNODES];
57unsigned long __initdata node_memmap_pfn[MAX_NUMNODES];
58unsigned long __initdata node_percpu_pfn[MAX_NUMNODES];
59unsigned long __initdata node_free_pfn[MAX_NUMNODES];
60
Chris Metcalf76c567f2011-02-28 16:37:34 -050061static unsigned long __initdata node_percpu[MAX_NUMNODES];
62
Thomas Gleixner293ef7b2012-05-03 09:02:59 +000063/*
64 * per-CPU stack and boot info.
65 */
66DEFINE_PER_CPU(unsigned long, boot_sp) =
67 (unsigned long)init_stack + THREAD_SIZE;
68
69#ifdef CONFIG_SMP
70DEFINE_PER_CPU(unsigned long, boot_pc) = (unsigned long)start_kernel;
71#else
72/*
73 * The variable must be __initdata since it references __init code.
74 * With CONFIG_SMP it is per-cpu data, which is exempt from validation.
75 */
76unsigned long __initdata boot_pc = (unsigned long)start_kernel;
77#endif
78
Chris Metcalf867e3592010-05-28 23:09:12 -040079#ifdef CONFIG_HIGHMEM
80/* Page frame index of end of lowmem on each controller. */
81unsigned long __cpuinitdata node_lowmem_end_pfn[MAX_NUMNODES];
82
83/* Number of pages that can be mapped into lowmem. */
84static unsigned long __initdata mappable_physpages;
85#endif
86
87/* Data on which physical memory controller corresponds to which NUMA node */
88int node_controller[MAX_NUMNODES] = { [0 ... MAX_NUMNODES-1] = -1 };
89
90#ifdef CONFIG_HIGHMEM
91/* Map information from VAs to PAs */
92unsigned long pbase_map[1 << (32 - HPAGE_SHIFT)]
93 __write_once __attribute__((aligned(L2_CACHE_BYTES)));
94EXPORT_SYMBOL(pbase_map);
95
96/* Map information from PAs to VAs */
97void *vbase_map[NR_PA_HIGHBIT_VALUES]
98 __write_once __attribute__((aligned(L2_CACHE_BYTES)));
99EXPORT_SYMBOL(vbase_map);
100#endif
101
102/* Node number as a function of the high PA bits */
103int highbits_to_node[NR_PA_HIGHBIT_VALUES] __write_once;
104EXPORT_SYMBOL(highbits_to_node);
105
106static unsigned int __initdata maxmem_pfn = -1U;
107static unsigned int __initdata maxnodemem_pfn[MAX_NUMNODES] = {
108 [0 ... MAX_NUMNODES-1] = -1U
109};
110static nodemask_t __initdata isolnodes;
111
112#ifdef CONFIG_PCI
113enum { DEFAULT_PCI_RESERVE_MB = 64 };
114static unsigned int __initdata pci_reserve_mb = DEFAULT_PCI_RESERVE_MB;
115unsigned long __initdata pci_reserve_start_pfn = -1U;
116unsigned long __initdata pci_reserve_end_pfn = -1U;
117#endif
118
119static int __init setup_maxmem(char *str)
120{
Chris Metcalfbfffe792012-03-29 15:56:18 -0400121 unsigned long long maxmem;
122 if (str == NULL || (maxmem = memparse(str, NULL)) == 0)
Chris Metcalf867e3592010-05-28 23:09:12 -0400123 return -EINVAL;
124
Chris Metcalfbfffe792012-03-29 15:56:18 -0400125 maxmem_pfn = (maxmem >> HPAGE_SHIFT) << (HPAGE_SHIFT - PAGE_SHIFT);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400126 pr_info("Forcing RAM used to no more than %dMB\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400127 maxmem_pfn >> (20 - PAGE_SHIFT));
128 return 0;
129}
130early_param("maxmem", setup_maxmem);
131
132static int __init setup_maxnodemem(char *str)
133{
134 char *endp;
Chris Metcalfbfffe792012-03-29 15:56:18 -0400135 unsigned long long maxnodemem;
136 long node;
Chris Metcalf867e3592010-05-28 23:09:12 -0400137
138 node = str ? simple_strtoul(str, &endp, 0) : INT_MAX;
Chris Metcalfbfffe792012-03-29 15:56:18 -0400139 if (node >= MAX_NUMNODES || *endp != ':')
Chris Metcalf867e3592010-05-28 23:09:12 -0400140 return -EINVAL;
141
Chris Metcalfbfffe792012-03-29 15:56:18 -0400142 maxnodemem = memparse(endp+1, NULL);
143 maxnodemem_pfn[node] = (maxnodemem >> HPAGE_SHIFT) <<
Chris Metcalf867e3592010-05-28 23:09:12 -0400144 (HPAGE_SHIFT - PAGE_SHIFT);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400145 pr_info("Forcing RAM used on node %ld to no more than %dMB\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400146 node, maxnodemem_pfn[node] >> (20 - PAGE_SHIFT));
147 return 0;
148}
149early_param("maxnodemem", setup_maxnodemem);
150
151static int __init setup_isolnodes(char *str)
152{
153 char buf[MAX_NUMNODES * 5];
154 if (str == NULL || nodelist_parse(str, isolnodes) != 0)
155 return -EINVAL;
156
157 nodelist_scnprintf(buf, sizeof(buf), isolnodes);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400158 pr_info("Set isolnodes value to '%s'\n", buf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400159 return 0;
160}
161early_param("isolnodes", setup_isolnodes);
162
163#ifdef CONFIG_PCI
164static int __init setup_pci_reserve(char* str)
165{
166 unsigned long mb;
167
168 if (str == NULL || strict_strtoul(str, 0, &mb) != 0 ||
169 mb > 3 * 1024)
170 return -EINVAL;
171
172 pci_reserve_mb = mb;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400173 pr_info("Reserving %dMB for PCIE root complex mappings\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400174 pci_reserve_mb);
175 return 0;
176}
177early_param("pci_reserve", setup_pci_reserve);
178#endif
179
180#ifndef __tilegx__
181/*
182 * vmalloc=size forces the vmalloc area to be exactly 'size' bytes.
183 * This can be used to increase (or decrease) the vmalloc area.
184 */
185static int __init parse_vmalloc(char *arg)
186{
187 if (!arg)
188 return -EINVAL;
189
190 VMALLOC_RESERVE = (memparse(arg, &arg) + PGDIR_SIZE - 1) & PGDIR_MASK;
191
192 /* See validate_va() for more on this test. */
193 if ((long)_VMALLOC_START >= 0)
194 early_panic("\"vmalloc=%#lx\" value too large: maximum %#lx\n",
195 VMALLOC_RESERVE, _VMALLOC_END - 0x80000000UL);
196
197 return 0;
198}
199early_param("vmalloc", parse_vmalloc);
200#endif
201
202#ifdef CONFIG_HIGHMEM
203/*
Chris Metcalfa78c9422010-10-14 16:23:03 -0400204 * Determine for each controller where its lowmem is mapped and how much of
205 * it is mapped there. On controller zero, the first few megabytes are
206 * already mapped in as code at MEM_SV_INTRPT, so in principle we could
207 * start our data mappings higher up, but for now we don't bother, to avoid
208 * additional confusion.
Chris Metcalf867e3592010-05-28 23:09:12 -0400209 *
210 * One question is whether, on systems with more than 768 Mb and
211 * controllers of different sizes, to map in a proportionate amount of
212 * each one, or to try to map the same amount from each controller.
213 * (E.g. if we have three controllers with 256MB, 1GB, and 256MB
214 * respectively, do we map 256MB from each, or do we map 128 MB, 512
215 * MB, and 128 MB respectively?) For now we use a proportionate
216 * solution like the latter.
217 *
218 * The VA/PA mapping demands that we align our decisions at 16 MB
219 * boundaries so that we can rapidly convert VA to PA.
220 */
221static void *__init setup_pa_va_mapping(void)
222{
223 unsigned long curr_pages = 0;
224 unsigned long vaddr = PAGE_OFFSET;
225 nodemask_t highonlynodes = isolnodes;
226 int i, j;
227
228 memset(pbase_map, -1, sizeof(pbase_map));
229 memset(vbase_map, -1, sizeof(vbase_map));
230
231 /* Node zero cannot be isolated for LOWMEM purposes. */
232 node_clear(0, highonlynodes);
233
234 /* Count up the number of pages on non-highonlynodes controllers. */
235 mappable_physpages = 0;
236 for_each_online_node(i) {
237 if (!node_isset(i, highonlynodes))
238 mappable_physpages +=
239 node_end_pfn[i] - node_start_pfn[i];
240 }
241
242 for_each_online_node(i) {
243 unsigned long start = node_start_pfn[i];
244 unsigned long end = node_end_pfn[i];
245 unsigned long size = end - start;
246 unsigned long vaddr_end;
247
248 if (node_isset(i, highonlynodes)) {
249 /* Mark this controller as having no lowmem. */
250 node_lowmem_end_pfn[i] = start;
251 continue;
252 }
253
254 curr_pages += size;
255 if (mappable_physpages > MAXMEM_PFN) {
256 vaddr_end = PAGE_OFFSET +
257 (((u64)curr_pages * MAXMEM_PFN /
258 mappable_physpages)
259 << PAGE_SHIFT);
260 } else {
261 vaddr_end = PAGE_OFFSET + (curr_pages << PAGE_SHIFT);
262 }
263 for (j = 0; vaddr < vaddr_end; vaddr += HPAGE_SIZE, ++j) {
264 unsigned long this_pfn =
265 start + (j << HUGETLB_PAGE_ORDER);
266 pbase_map[vaddr >> HPAGE_SHIFT] = this_pfn;
267 if (vbase_map[__pfn_to_highbits(this_pfn)] ==
268 (void *)-1)
269 vbase_map[__pfn_to_highbits(this_pfn)] =
270 (void *)(vaddr & HPAGE_MASK);
271 }
272 node_lowmem_end_pfn[i] = start + (j << HUGETLB_PAGE_ORDER);
273 BUG_ON(node_lowmem_end_pfn[i] > end);
274 }
275
276 /* Return highest address of any mapped memory. */
277 return (void *)vaddr;
278}
279#endif /* CONFIG_HIGHMEM */
280
281/*
282 * Register our most important memory mappings with the debug stub.
283 *
284 * This is up to 4 mappings for lowmem, one mapping per memory
285 * controller, plus one for our text segment.
286 */
Chris Metcalf0707ad32010-06-25 17:04:17 -0400287static void __cpuinit store_permanent_mappings(void)
Chris Metcalf867e3592010-05-28 23:09:12 -0400288{
289 int i;
290
291 for_each_online_node(i) {
292 HV_PhysAddr pa = ((HV_PhysAddr)node_start_pfn[i]) << PAGE_SHIFT;
293#ifdef CONFIG_HIGHMEM
294 HV_PhysAddr high_mapped_pa = node_lowmem_end_pfn[i];
295#else
296 HV_PhysAddr high_mapped_pa = node_end_pfn[i];
297#endif
298
299 unsigned long pages = high_mapped_pa - node_start_pfn[i];
300 HV_VirtAddr addr = (HV_VirtAddr) __va(pa);
301 hv_store_mapping(addr, pages << PAGE_SHIFT, pa);
302 }
303
304 hv_store_mapping((HV_VirtAddr)_stext,
305 (uint32_t)(_einittext - _stext), 0);
306}
307
308/*
309 * Use hv_inquire_physical() to populate node_{start,end}_pfn[]
310 * and node_online_map, doing suitable sanity-checking.
311 * Also set min_low_pfn, max_low_pfn, and max_pfn.
312 */
313static void __init setup_memory(void)
314{
315 int i, j;
316 int highbits_seen[NR_PA_HIGHBIT_VALUES] = { 0 };
317#ifdef CONFIG_HIGHMEM
318 long highmem_pages;
319#endif
320#ifndef __tilegx__
321 int cap;
322#endif
323#if defined(CONFIG_HIGHMEM) || defined(__tilegx__)
324 long lowmem_pages;
325#endif
326
327 /* We are using a char to hold the cpu_2_node[] mapping */
Chris Metcalfe18105c2010-10-14 16:50:26 -0400328 BUILD_BUG_ON(MAX_NUMNODES > 127);
Chris Metcalf867e3592010-05-28 23:09:12 -0400329
330 /* Discover the ranges of memory available to us */
331 for (i = 0; ; ++i) {
332 unsigned long start, size, end, highbits;
333 HV_PhysAddrRange range = hv_inquire_physical(i);
334 if (range.size == 0)
335 break;
336#ifdef CONFIG_FLATMEM
337 if (i > 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400338 pr_err("Can't use discontiguous PAs: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400339 range.size, range.start + range.size);
340 continue;
341 }
342#endif
343#ifndef __tilegx__
344 if ((unsigned long)range.start) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400345 pr_err("Range not at 4GB multiple: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400346 range.start, range.start + range.size);
347 continue;
348 }
349#endif
350 if ((range.start & (HPAGE_SIZE-1)) != 0 ||
351 (range.size & (HPAGE_SIZE-1)) != 0) {
352 unsigned long long start_pa = range.start;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400353 unsigned long long orig_size = range.size;
Chris Metcalf867e3592010-05-28 23:09:12 -0400354 range.start = (start_pa + HPAGE_SIZE - 1) & HPAGE_MASK;
355 range.size -= (range.start - start_pa);
356 range.size &= HPAGE_MASK;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400357 pr_err("Range not hugepage-aligned: %#llx..%#llx:"
Chris Metcalf867e3592010-05-28 23:09:12 -0400358 " now %#llx-%#llx\n",
Chris Metcalf0707ad32010-06-25 17:04:17 -0400359 start_pa, start_pa + orig_size,
Chris Metcalf867e3592010-05-28 23:09:12 -0400360 range.start, range.start + range.size);
361 }
362 highbits = __pa_to_highbits(range.start);
363 if (highbits >= NR_PA_HIGHBIT_VALUES) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400364 pr_err("PA high bits too high: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400365 range.start, range.start + range.size);
366 continue;
367 }
368 if (highbits_seen[highbits]) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400369 pr_err("Range overlaps in high bits: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400370 range.start, range.start + range.size);
371 continue;
372 }
373 highbits_seen[highbits] = 1;
374 if (PFN_DOWN(range.size) > maxnodemem_pfn[i]) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400375 int max_size = maxnodemem_pfn[i];
376 if (max_size > 0) {
377 pr_err("Maxnodemem reduced node %d to"
378 " %d pages\n", i, max_size);
379 range.size = PFN_PHYS(max_size);
Chris Metcalf867e3592010-05-28 23:09:12 -0400380 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400381 pr_err("Maxnodemem disabled node %d\n", i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400382 continue;
383 }
384 }
385 if (num_physpages + PFN_DOWN(range.size) > maxmem_pfn) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400386 int max_size = maxmem_pfn - num_physpages;
387 if (max_size > 0) {
388 pr_err("Maxmem reduced node %d to %d pages\n",
389 i, max_size);
390 range.size = PFN_PHYS(max_size);
Chris Metcalf867e3592010-05-28 23:09:12 -0400391 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400392 pr_err("Maxmem disabled node %d\n", i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400393 continue;
394 }
395 }
396 if (i >= MAX_NUMNODES) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400397 pr_err("Too many PA nodes (#%d): %#llx...%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400398 i, range.size, range.size + range.start);
399 continue;
400 }
401
402 start = range.start >> PAGE_SHIFT;
403 size = range.size >> PAGE_SHIFT;
404 end = start + size;
405
406#ifndef __tilegx__
407 if (((HV_PhysAddr)end << PAGE_SHIFT) !=
408 (range.start + range.size)) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400409 pr_err("PAs too high to represent: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400410 range.start, range.start + range.size);
411 continue;
412 }
413#endif
414#ifdef CONFIG_PCI
415 /*
416 * Blocks that overlap the pci reserved region must
417 * have enough space to hold the maximum percpu data
418 * region at the top of the range. If there isn't
419 * enough space above the reserved region, just
420 * truncate the node.
421 */
422 if (start <= pci_reserve_start_pfn &&
423 end > pci_reserve_start_pfn) {
424 unsigned int per_cpu_size =
425 __per_cpu_end - __per_cpu_start;
426 unsigned int percpu_pages =
427 NR_CPUS * (PFN_UP(per_cpu_size) >> PAGE_SHIFT);
428 if (end < pci_reserve_end_pfn + percpu_pages) {
429 end = pci_reserve_start_pfn;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400430 pr_err("PCI mapping region reduced node %d to"
Chris Metcalf867e3592010-05-28 23:09:12 -0400431 " %ld pages\n", i, end - start);
432 }
433 }
434#endif
435
436 for (j = __pfn_to_highbits(start);
437 j <= __pfn_to_highbits(end - 1); j++)
438 highbits_to_node[j] = i;
439
440 node_start_pfn[i] = start;
441 node_end_pfn[i] = end;
442 node_controller[i] = range.controller;
443 num_physpages += size;
444 max_pfn = end;
445
446 /* Mark node as online */
447 node_set(i, node_online_map);
448 node_set(i, node_possible_map);
449 }
450
451#ifndef __tilegx__
452 /*
453 * For 4KB pages, mem_map "struct page" data is 1% of the size
454 * of the physical memory, so can be quite big (640 MB for
455 * four 16G zones). These structures must be mapped in
456 * lowmem, and since we currently cap out at about 768 MB,
457 * it's impractical to try to use this much address space.
458 * For now, arbitrarily cap the amount of physical memory
459 * we're willing to use at 8 million pages (32GB of 4KB pages).
460 */
461 cap = 8 * 1024 * 1024; /* 8 million pages */
462 if (num_physpages > cap) {
463 int num_nodes = num_online_nodes();
464 int cap_each = cap / num_nodes;
465 unsigned long dropped_pages = 0;
466 for (i = 0; i < num_nodes; ++i) {
467 int size = node_end_pfn[i] - node_start_pfn[i];
468 if (size > cap_each) {
469 dropped_pages += (size - cap_each);
470 node_end_pfn[i] = node_start_pfn[i] + cap_each;
471 }
472 }
473 num_physpages -= dropped_pages;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400474 pr_warning("Only using %ldMB memory;"
Chris Metcalf867e3592010-05-28 23:09:12 -0400475 " ignoring %ldMB.\n",
476 num_physpages >> (20 - PAGE_SHIFT),
477 dropped_pages >> (20 - PAGE_SHIFT));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400478 pr_warning("Consider using a larger page size.\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400479 }
480#endif
481
482 /* Heap starts just above the last loaded address. */
483 min_low_pfn = PFN_UP((unsigned long)_end - PAGE_OFFSET);
484
485#ifdef CONFIG_HIGHMEM
486 /* Find where we map lowmem from each controller. */
487 high_memory = setup_pa_va_mapping();
488
489 /* Set max_low_pfn based on what node 0 can directly address. */
490 max_low_pfn = node_lowmem_end_pfn[0];
491
492 lowmem_pages = (mappable_physpages > MAXMEM_PFN) ?
493 MAXMEM_PFN : mappable_physpages;
494 highmem_pages = (long) (num_physpages - lowmem_pages);
495
Chris Metcalf0707ad32010-06-25 17:04:17 -0400496 pr_notice("%ldMB HIGHMEM available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400497 pages_to_mb(highmem_pages > 0 ? highmem_pages : 0));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400498 pr_notice("%ldMB LOWMEM available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400499 pages_to_mb(lowmem_pages));
500#else
501 /* Set max_low_pfn based on what node 0 can directly address. */
502 max_low_pfn = node_end_pfn[0];
503
504#ifndef __tilegx__
505 if (node_end_pfn[0] > MAXMEM_PFN) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400506 pr_warning("Only using %ldMB LOWMEM.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400507 MAXMEM>>20);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400508 pr_warning("Use a HIGHMEM enabled kernel.\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400509 max_low_pfn = MAXMEM_PFN;
510 max_pfn = MAXMEM_PFN;
511 num_physpages = MAXMEM_PFN;
512 node_end_pfn[0] = MAXMEM_PFN;
513 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400514 pr_notice("%ldMB memory available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400515 pages_to_mb(node_end_pfn[0]));
516 }
517 for (i = 1; i < MAX_NUMNODES; ++i) {
518 node_start_pfn[i] = 0;
519 node_end_pfn[i] = 0;
520 }
521 high_memory = __va(node_end_pfn[0]);
522#else
523 lowmem_pages = 0;
524 for (i = 0; i < MAX_NUMNODES; ++i) {
525 int pages = node_end_pfn[i] - node_start_pfn[i];
526 lowmem_pages += pages;
527 if (pages)
528 high_memory = pfn_to_kaddr(node_end_pfn[i]);
529 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400530 pr_notice("%ldMB memory available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400531 pages_to_mb(lowmem_pages));
532#endif
533#endif
534}
535
Chris Metcalf621b1952012-04-01 14:04:21 -0400536/*
537 * On 32-bit machines, we only put bootmem on the low controller,
538 * since PAs > 4GB can't be used in bootmem. In principle one could
539 * imagine, e.g., multiple 1 GB controllers all of which could support
540 * bootmem, but in practice using controllers this small isn't a
541 * particularly interesting scenario, so we just keep it simple and
542 * use only the first controller for bootmem on 32-bit machines.
543 */
544static inline int node_has_bootmem(int nid)
Chris Metcalf867e3592010-05-28 23:09:12 -0400545{
Chris Metcalf621b1952012-04-01 14:04:21 -0400546#ifdef CONFIG_64BIT
547 return 1;
Chris Metcalf867e3592010-05-28 23:09:12 -0400548#else
Chris Metcalf621b1952012-04-01 14:04:21 -0400549 return nid == 0;
550#endif
551}
552
553static inline unsigned long alloc_bootmem_pfn(int nid,
554 unsigned long size,
555 unsigned long goal)
556{
557 void *kva = __alloc_bootmem_node(NODE_DATA(nid), size,
558 PAGE_SIZE, goal);
559 unsigned long pfn = kaddr_to_pfn(kva);
560 BUG_ON(goal && PFN_PHYS(pfn) != goal);
561 return pfn;
562}
563
564static void __init setup_bootmem_allocator_node(int i)
565{
566 unsigned long start, end, mapsize, mapstart;
567
568 if (node_has_bootmem(i)) {
569 NODE_DATA(i)->bdata = &bootmem_node_data[i];
570 } else {
571 /* Share controller zero's bdata for now. */
572 NODE_DATA(i)->bdata = &bootmem_node_data[0];
573 return;
574 }
575
576 /* Skip up to after the bss in node 0. */
577 start = (i == 0) ? min_low_pfn : node_start_pfn[i];
578
579 /* Only lowmem, if we're a HIGHMEM build. */
580#ifdef CONFIG_HIGHMEM
581 end = node_lowmem_end_pfn[i];
582#else
583 end = node_end_pfn[i];
Chris Metcalf867e3592010-05-28 23:09:12 -0400584#endif
585
Chris Metcalf621b1952012-04-01 14:04:21 -0400586 /* No memory here. */
587 if (end == start)
588 return;
Chris Metcalf867e3592010-05-28 23:09:12 -0400589
Chris Metcalf621b1952012-04-01 14:04:21 -0400590 /* Figure out where the bootmem bitmap is located. */
591 mapsize = bootmem_bootmap_pages(end - start);
592 if (i == 0) {
593 /* Use some space right before the heap on node 0. */
594 mapstart = start;
595 start += mapsize;
596 } else {
597 /* Allocate bitmap on node 0 to avoid page table issues. */
598 mapstart = alloc_bootmem_pfn(0, PFN_PHYS(mapsize), 0);
599 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400600
Chris Metcalf621b1952012-04-01 14:04:21 -0400601 /* Initialize a node. */
602 init_bootmem_node(NODE_DATA(i), mapstart, start, end);
603
604 /* Free all the space back into the allocator. */
605 free_bootmem(PFN_PHYS(start), PFN_PHYS(end - start));
606
607#if defined(CONFIG_PCI)
608 /*
609 * Throw away any memory aliased by the PCI region. FIXME: this
610 * is a temporary hack to work around bug 10502, and needs to be
611 * fixed properly.
612 */
613 if (pci_reserve_start_pfn < end && pci_reserve_end_pfn > start)
614 reserve_bootmem(PFN_PHYS(pci_reserve_start_pfn),
615 PFN_PHYS(pci_reserve_end_pfn -
616 pci_reserve_start_pfn),
617 BOOTMEM_EXCLUSIVE);
618#endif
619}
620
621static void __init setup_bootmem_allocator(void)
622{
623 int i;
624 for (i = 0; i < MAX_NUMNODES; ++i)
625 setup_bootmem_allocator_node(i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400626
627#ifdef CONFIG_KEXEC
628 if (crashk_res.start != crashk_res.end)
Joe Perches28f65c112011-06-09 09:13:32 -0700629 reserve_bootmem(crashk_res.start, resource_size(&crashk_res), 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400630#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400631}
632
633void *__init alloc_remap(int nid, unsigned long size)
634{
635 int pages = node_end_pfn[nid] - node_start_pfn[nid];
636 void *map = pfn_to_kaddr(node_memmap_pfn[nid]);
637 BUG_ON(size != pages * sizeof(struct page));
638 memset(map, 0, size);
639 return map;
640}
641
642static int __init percpu_size(void)
643{
Chris Metcalf76c567f2011-02-28 16:37:34 -0500644 int size = __per_cpu_end - __per_cpu_start;
645 size += PERCPU_MODULE_RESERVE;
646 size += PERCPU_DYNAMIC_EARLY_SIZE;
647 if (size < PCPU_MIN_UNIT_SIZE)
648 size = PCPU_MIN_UNIT_SIZE;
649 size = roundup(size, PAGE_SIZE);
650
Chris Metcalf867e3592010-05-28 23:09:12 -0400651 /* In several places we assume the per-cpu data fits on a huge page. */
652 BUG_ON(kdata_huge && size > HPAGE_SIZE);
653 return size;
654}
655
Chris Metcalf867e3592010-05-28 23:09:12 -0400656static void __init zone_sizes_init(void)
657{
658 unsigned long zones_size[MAX_NR_ZONES] = { 0 };
Chris Metcalf867e3592010-05-28 23:09:12 -0400659 int size = percpu_size();
660 int num_cpus = smp_height * smp_width;
Chris Metcalfeef015c2012-05-09 12:26:30 -0400661 const unsigned long dma_end = (1UL << (32 - PAGE_SHIFT));
662
Chris Metcalf867e3592010-05-28 23:09:12 -0400663 int i;
664
665 for (i = 0; i < num_cpus; ++i)
666 node_percpu[cpu_to_node(i)] += size;
667
668 for_each_online_node(i) {
669 unsigned long start = node_start_pfn[i];
670 unsigned long end = node_end_pfn[i];
671#ifdef CONFIG_HIGHMEM
672 unsigned long lowmem_end = node_lowmem_end_pfn[i];
673#else
674 unsigned long lowmem_end = end;
675#endif
676 int memmap_size = (end - start) * sizeof(struct page);
677 node_free_pfn[i] = start;
678
679 /*
680 * Set aside pages for per-cpu data and the mem_map array.
681 *
682 * Since the per-cpu data requires special homecaching,
683 * if we are in kdata_huge mode, we put it at the end of
684 * the lowmem region. If we're not in kdata_huge mode,
685 * we take the per-cpu pages from the bottom of the
686 * controller, since that avoids fragmenting a huge page
687 * that users might want. We always take the memmap
688 * from the bottom of the controller, since with
689 * kdata_huge that lets it be under a huge TLB entry.
690 *
691 * If the user has requested isolnodes for a controller,
692 * though, there'll be no lowmem, so we just alloc_bootmem
693 * the memmap. There will be no percpu memory either.
694 */
Chris Metcalf621b1952012-04-01 14:04:21 -0400695 if (i != 0 && cpu_isset(i, isolnodes)) {
696 node_memmap_pfn[i] =
697 alloc_bootmem_pfn(0, memmap_size, 0);
698 BUG_ON(node_percpu[i] != 0);
699 } else if (node_has_bootmem(start)) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400700 unsigned long goal = 0;
701 node_memmap_pfn[i] =
Chris Metcalf621b1952012-04-01 14:04:21 -0400702 alloc_bootmem_pfn(i, memmap_size, 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400703 if (kdata_huge)
704 goal = PFN_PHYS(lowmem_end) - node_percpu[i];
705 if (node_percpu[i])
706 node_percpu_pfn[i] =
Chris Metcalf621b1952012-04-01 14:04:21 -0400707 alloc_bootmem_pfn(i, node_percpu[i],
708 goal);
Chris Metcalf867e3592010-05-28 23:09:12 -0400709 } else {
Chris Metcalf621b1952012-04-01 14:04:21 -0400710 /* In non-bootmem zones, just reserve some pages. */
Chris Metcalf867e3592010-05-28 23:09:12 -0400711 node_memmap_pfn[i] = node_free_pfn[i];
712 node_free_pfn[i] += PFN_UP(memmap_size);
713 if (!kdata_huge) {
714 node_percpu_pfn[i] = node_free_pfn[i];
715 node_free_pfn[i] += PFN_UP(node_percpu[i]);
716 } else {
717 node_percpu_pfn[i] =
718 lowmem_end - PFN_UP(node_percpu[i]);
719 }
720 }
721
722#ifdef CONFIG_HIGHMEM
723 if (start > lowmem_end) {
724 zones_size[ZONE_NORMAL] = 0;
725 zones_size[ZONE_HIGHMEM] = end - start;
726 } else {
727 zones_size[ZONE_NORMAL] = lowmem_end - start;
728 zones_size[ZONE_HIGHMEM] = end - lowmem_end;
729 }
730#else
731 zones_size[ZONE_NORMAL] = end - start;
732#endif
733
Chris Metcalfeef015c2012-05-09 12:26:30 -0400734 if (start < dma_end) {
735 zones_size[ZONE_DMA] = min(zones_size[ZONE_NORMAL],
736 dma_end - start);
737 zones_size[ZONE_NORMAL] -= zones_size[ZONE_DMA];
738 } else {
739 zones_size[ZONE_DMA] = 0;
740 }
741
Chris Metcalf621b1952012-04-01 14:04:21 -0400742 /* Take zone metadata from controller 0 if we're isolnode. */
743 if (node_isset(i, isolnodes))
744 NODE_DATA(i)->bdata = &bootmem_node_data[0];
Chris Metcalf867e3592010-05-28 23:09:12 -0400745
746 free_area_init_node(i, zones_size, start, NULL);
Chris Metcalf76c567f2011-02-28 16:37:34 -0500747 printk(KERN_DEBUG " Normal zone: %ld per-cpu pages\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400748 PFN_UP(node_percpu[i]));
749
750 /* Track the type of memory on each node */
Chris Metcalfeef015c2012-05-09 12:26:30 -0400751 if (zones_size[ZONE_NORMAL] || zones_size[ZONE_DMA])
Chris Metcalf867e3592010-05-28 23:09:12 -0400752 node_set_state(i, N_NORMAL_MEMORY);
753#ifdef CONFIG_HIGHMEM
754 if (end != start)
755 node_set_state(i, N_HIGH_MEMORY);
756#endif
757
758 node_set_online(i);
759 }
760}
761
762#ifdef CONFIG_NUMA
763
764/* which logical CPUs are on which nodes */
765struct cpumask node_2_cpu_mask[MAX_NUMNODES] __write_once;
766EXPORT_SYMBOL(node_2_cpu_mask);
767
768/* which node each logical CPU is on */
769char cpu_2_node[NR_CPUS] __write_once __attribute__((aligned(L2_CACHE_BYTES)));
770EXPORT_SYMBOL(cpu_2_node);
771
772/* Return cpu_to_node() except for cpus not yet assigned, which return -1 */
773static int __init cpu_to_bound_node(int cpu, struct cpumask* unbound_cpus)
774{
775 if (!cpu_possible(cpu) || cpumask_test_cpu(cpu, unbound_cpus))
776 return -1;
777 else
778 return cpu_to_node(cpu);
779}
780
781/* Return number of immediately-adjacent tiles sharing the same NUMA node. */
782static int __init node_neighbors(int node, int cpu,
783 struct cpumask *unbound_cpus)
784{
785 int neighbors = 0;
786 int w = smp_width;
787 int h = smp_height;
788 int x = cpu % w;
789 int y = cpu / w;
790 if (x > 0 && cpu_to_bound_node(cpu-1, unbound_cpus) == node)
791 ++neighbors;
792 if (x < w-1 && cpu_to_bound_node(cpu+1, unbound_cpus) == node)
793 ++neighbors;
794 if (y > 0 && cpu_to_bound_node(cpu-w, unbound_cpus) == node)
795 ++neighbors;
796 if (y < h-1 && cpu_to_bound_node(cpu+w, unbound_cpus) == node)
797 ++neighbors;
798 return neighbors;
799}
800
801static void __init setup_numa_mapping(void)
802{
803 int distance[MAX_NUMNODES][NR_CPUS];
804 HV_Coord coord;
805 int cpu, node, cpus, i, x, y;
806 int num_nodes = num_online_nodes();
807 struct cpumask unbound_cpus;
808 nodemask_t default_nodes;
809
810 cpumask_clear(&unbound_cpus);
811
812 /* Get set of nodes we will use for defaults */
813 nodes_andnot(default_nodes, node_online_map, isolnodes);
814 if (nodes_empty(default_nodes)) {
815 BUG_ON(!node_isset(0, node_online_map));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400816 pr_err("Forcing NUMA node zero available as a default node\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400817 node_set(0, default_nodes);
818 }
819
820 /* Populate the distance[] array */
821 memset(distance, -1, sizeof(distance));
822 cpu = 0;
823 for (coord.y = 0; coord.y < smp_height; ++coord.y) {
824 for (coord.x = 0; coord.x < smp_width;
825 ++coord.x, ++cpu) {
826 BUG_ON(cpu >= nr_cpu_ids);
827 if (!cpu_possible(cpu)) {
828 cpu_2_node[cpu] = -1;
829 continue;
830 }
831 for_each_node_mask(node, default_nodes) {
832 HV_MemoryControllerInfo info =
833 hv_inquire_memory_controller(
834 coord, node_controller[node]);
835 distance[node][cpu] =
836 ABS(info.coord.x) + ABS(info.coord.y);
837 }
838 cpumask_set_cpu(cpu, &unbound_cpus);
839 }
840 }
841 cpus = cpu;
842
843 /*
844 * Round-robin through the NUMA nodes until all the cpus are
845 * assigned. We could be more clever here (e.g. create four
846 * sorted linked lists on the same set of cpu nodes, and pull
847 * off them in round-robin sequence, removing from all four
848 * lists each time) but given the relatively small numbers
849 * involved, O(n^2) seem OK for a one-time cost.
850 */
851 node = first_node(default_nodes);
852 while (!cpumask_empty(&unbound_cpus)) {
853 int best_cpu = -1;
854 int best_distance = INT_MAX;
855 for (cpu = 0; cpu < cpus; ++cpu) {
856 if (cpumask_test_cpu(cpu, &unbound_cpus)) {
857 /*
858 * Compute metric, which is how much
859 * closer the cpu is to this memory
860 * controller than the others, shifted
861 * up, and then the number of
862 * neighbors already in the node as an
863 * epsilon adjustment to try to keep
864 * the nodes compact.
865 */
866 int d = distance[node][cpu] * num_nodes;
867 for_each_node_mask(i, default_nodes) {
868 if (i != node)
869 d -= distance[i][cpu];
870 }
871 d *= 8; /* allow space for epsilon */
872 d -= node_neighbors(node, cpu, &unbound_cpus);
873 if (d < best_distance) {
874 best_cpu = cpu;
875 best_distance = d;
876 }
877 }
878 }
879 BUG_ON(best_cpu < 0);
880 cpumask_set_cpu(best_cpu, &node_2_cpu_mask[node]);
881 cpu_2_node[best_cpu] = node;
882 cpumask_clear_cpu(best_cpu, &unbound_cpus);
883 node = next_node(node, default_nodes);
884 if (node == MAX_NUMNODES)
885 node = first_node(default_nodes);
886 }
887
888 /* Print out node assignments and set defaults for disabled cpus */
889 cpu = 0;
890 for (y = 0; y < smp_height; ++y) {
891 printk(KERN_DEBUG "NUMA cpu-to-node row %d:", y);
892 for (x = 0; x < smp_width; ++x, ++cpu) {
893 if (cpu_to_node(cpu) < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400894 pr_cont(" -");
Chris Metcalf867e3592010-05-28 23:09:12 -0400895 cpu_2_node[cpu] = first_node(default_nodes);
896 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400897 pr_cont(" %d", cpu_to_node(cpu));
Chris Metcalf867e3592010-05-28 23:09:12 -0400898 }
899 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400900 pr_cont("\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400901 }
902}
903
904static struct cpu cpu_devices[NR_CPUS];
905
906static int __init topology_init(void)
907{
908 int i;
909
910 for_each_online_node(i)
911 register_one_node(i);
912
Chris Metcalf4d658d12010-11-24 13:42:15 -0500913 for (i = 0; i < smp_height * smp_width; ++i)
Chris Metcalf867e3592010-05-28 23:09:12 -0400914 register_cpu(&cpu_devices[i], i);
915
916 return 0;
917}
918
919subsys_initcall(topology_init);
920
921#else /* !CONFIG_NUMA */
922
923#define setup_numa_mapping() do { } while (0)
924
925#endif /* CONFIG_NUMA */
926
Chris Metcalf621b1952012-04-01 14:04:21 -0400927/*
928 * Initialize hugepage support on this cpu. We do this on all cores
929 * early in boot: before argument parsing for the boot cpu, and after
930 * argument parsing but before the init functions run on the secondaries.
931 * So the values we set up here in the hypervisor may be overridden on
932 * the boot cpu as arguments are parsed.
933 */
934static __cpuinit void init_super_pages(void)
935{
936#ifdef CONFIG_HUGETLB_SUPER_PAGES
937 int i;
938 for (i = 0; i < HUGE_SHIFT_ENTRIES; ++i)
939 hv_set_pte_super_shift(i, huge_shift[i]);
940#endif
941}
942
Chris Metcalf867e3592010-05-28 23:09:12 -0400943/**
Chris Metcalf0707ad32010-06-25 17:04:17 -0400944 * setup_cpu() - Do all necessary per-cpu, tile-specific initialization.
945 * @boot: Is this the boot cpu?
Chris Metcalf867e3592010-05-28 23:09:12 -0400946 *
Chris Metcalf0707ad32010-06-25 17:04:17 -0400947 * Called from setup_arch() on the boot cpu, or online_secondary().
Chris Metcalf867e3592010-05-28 23:09:12 -0400948 */
Chris Metcalf0707ad32010-06-25 17:04:17 -0400949void __cpuinit setup_cpu(int boot)
Chris Metcalf867e3592010-05-28 23:09:12 -0400950{
Chris Metcalf0707ad32010-06-25 17:04:17 -0400951 /* The boot cpu sets up its permanent mappings much earlier. */
952 if (!boot)
953 store_permanent_mappings();
954
Chris Metcalf867e3592010-05-28 23:09:12 -0400955 /* Allow asynchronous TLB interrupts. */
956#if CHIP_HAS_TILE_DMA()
Chris Metcalf5d966112010-11-01 15:24:29 -0400957 arch_local_irq_unmask(INT_DMATLB_MISS);
958 arch_local_irq_unmask(INT_DMATLB_ACCESS);
Chris Metcalf867e3592010-05-28 23:09:12 -0400959#endif
960#if CHIP_HAS_SN_PROC()
Chris Metcalf5d966112010-11-01 15:24:29 -0400961 arch_local_irq_unmask(INT_SNITLB_MISS);
Chris Metcalf867e3592010-05-28 23:09:12 -0400962#endif
Chris Metcalfa78c9422010-10-14 16:23:03 -0400963#ifdef __tilegx__
Chris Metcalf5d966112010-11-01 15:24:29 -0400964 arch_local_irq_unmask(INT_SINGLE_STEP_K);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400965#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400966
967 /*
968 * Allow user access to many generic SPRs, like the cycle
969 * counter, PASS/FAIL/DONE, INTERRUPT_CRITICAL_SECTION, etc.
970 */
971 __insn_mtspr(SPR_MPL_WORLD_ACCESS_SET_0, 1);
972
973#if CHIP_HAS_SN()
974 /* Static network is not restricted. */
975 __insn_mtspr(SPR_MPL_SN_ACCESS_SET_0, 1);
976#endif
977#if CHIP_HAS_SN_PROC()
978 __insn_mtspr(SPR_MPL_SN_NOTIFY_SET_0, 1);
979 __insn_mtspr(SPR_MPL_SN_CPL_SET_0, 1);
980#endif
981
982 /*
Chris Metcalfa78c9422010-10-14 16:23:03 -0400983 * Set the MPL for interrupt control 0 & 1 to the corresponding
984 * values. This includes access to the SYSTEM_SAVE and EX_CONTEXT
985 * SPRs, as well as the interrupt mask.
Chris Metcalf867e3592010-05-28 23:09:12 -0400986 */
987 __insn_mtspr(SPR_MPL_INTCTRL_0_SET_0, 1);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400988 __insn_mtspr(SPR_MPL_INTCTRL_1_SET_1, 1);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400989
990 /* Initialize IRQ support for this cpu. */
991 setup_irq_regs();
992
993#ifdef CONFIG_HARDWALL
994 /* Reset the network state on this cpu. */
995 reset_network_state();
996#endif
Chris Metcalf621b1952012-04-01 14:04:21 -0400997
998 init_super_pages();
Chris Metcalf867e3592010-05-28 23:09:12 -0400999}
1000
Chris Metcalf43d9ebb2011-05-02 15:51:32 -04001001#ifdef CONFIG_BLK_DEV_INITRD
1002
Chris Metcalf51bcdf82012-03-29 15:29:28 -04001003/*
1004 * Note that the kernel can potentially support other compression
1005 * techniques than gz, though we don't do so by default. If we ever
1006 * decide to do so we can either look for other filename extensions,
1007 * or just allow a file with this name to be compressed with an
1008 * arbitrary compressor (somewhat counterintuitively).
1009 */
Chris Metcalf867e3592010-05-28 23:09:12 -04001010static int __initdata set_initramfs_file;
1011static char __initdata initramfs_file[128] = "initramfs.cpio.gz";
1012
1013static int __init setup_initramfs_file(char *str)
1014{
1015 if (str == NULL)
1016 return -EINVAL;
1017 strncpy(initramfs_file, str, sizeof(initramfs_file) - 1);
1018 set_initramfs_file = 1;
1019
1020 return 0;
1021}
1022early_param("initramfs_file", setup_initramfs_file);
1023
1024/*
Chris Metcalf51bcdf82012-03-29 15:29:28 -04001025 * We look for an "initramfs.cpio.gz" file in the hvfs.
Chris Metcalf867e3592010-05-28 23:09:12 -04001026 * If there is one, we allocate some memory for it and it will be
Chris Metcalf51bcdf82012-03-29 15:29:28 -04001027 * unpacked to the initramfs.
Chris Metcalf867e3592010-05-28 23:09:12 -04001028 */
1029static void __init load_hv_initrd(void)
1030{
1031 HV_FS_StatInfo stat;
1032 int fd, rc;
1033 void *initrd;
1034
1035 fd = hv_fs_findfile((HV_VirtAddr) initramfs_file);
1036 if (fd == HV_ENOENT) {
1037 if (set_initramfs_file)
Chris Metcalf0707ad32010-06-25 17:04:17 -04001038 pr_warning("No such hvfs initramfs file '%s'\n",
1039 initramfs_file);
Chris Metcalf867e3592010-05-28 23:09:12 -04001040 return;
1041 }
1042 BUG_ON(fd < 0);
1043 stat = hv_fs_fstat(fd);
1044 BUG_ON(stat.size < 0);
1045 if (stat.flags & HV_FS_ISDIR) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001046 pr_warning("Ignoring hvfs file '%s': it's a directory.\n",
1047 initramfs_file);
Chris Metcalf867e3592010-05-28 23:09:12 -04001048 return;
1049 }
1050 initrd = alloc_bootmem_pages(stat.size);
1051 rc = hv_fs_pread(fd, (HV_VirtAddr) initrd, stat.size, 0);
1052 if (rc != stat.size) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001053 pr_err("Error reading %d bytes from hvfs file '%s': %d\n",
Chris Metcalf867e3592010-05-28 23:09:12 -04001054 stat.size, initramfs_file, rc);
Chris Metcalfbc63de72010-08-13 08:23:07 -04001055 free_initrd_mem((unsigned long) initrd, stat.size);
Chris Metcalf867e3592010-05-28 23:09:12 -04001056 return;
1057 }
1058 initrd_start = (unsigned long) initrd;
1059 initrd_end = initrd_start + stat.size;
1060}
1061
1062void __init free_initrd_mem(unsigned long begin, unsigned long end)
1063{
Chris Metcalfbc63de72010-08-13 08:23:07 -04001064 free_bootmem(__pa(begin), end - begin);
Chris Metcalf867e3592010-05-28 23:09:12 -04001065}
1066
Chris Metcalf43d9ebb2011-05-02 15:51:32 -04001067#else
1068static inline void load_hv_initrd(void) {}
1069#endif /* CONFIG_BLK_DEV_INITRD */
1070
Chris Metcalf867e3592010-05-28 23:09:12 -04001071static void __init validate_hv(void)
1072{
1073 /*
1074 * It may already be too late, but let's check our built-in
1075 * configuration against what the hypervisor is providing.
1076 */
1077 unsigned long glue_size = hv_sysconf(HV_SYSCONF_GLUE_SIZE);
1078 int hv_page_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_SMALL);
1079 int hv_hpage_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_LARGE);
1080 HV_ASIDRange asid_range;
1081
1082#ifndef CONFIG_SMP
1083 HV_Topology topology = hv_inquire_topology();
1084 BUG_ON(topology.coord.x != 0 || topology.coord.y != 0);
1085 if (topology.width != 1 || topology.height != 1) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001086 pr_warning("Warning: booting UP kernel on %dx%d grid;"
1087 " will ignore all but first tile.\n",
1088 topology.width, topology.height);
Chris Metcalf867e3592010-05-28 23:09:12 -04001089 }
1090#endif
1091
1092 if (PAGE_OFFSET + HV_GLUE_START_CPA + glue_size > (unsigned long)_text)
1093 early_panic("Hypervisor glue size %ld is too big!\n",
1094 glue_size);
1095 if (hv_page_size != PAGE_SIZE)
1096 early_panic("Hypervisor page size %#x != our %#lx\n",
1097 hv_page_size, PAGE_SIZE);
1098 if (hv_hpage_size != HPAGE_SIZE)
1099 early_panic("Hypervisor huge page size %#x != our %#lx\n",
1100 hv_hpage_size, HPAGE_SIZE);
1101
1102#ifdef CONFIG_SMP
1103 /*
1104 * Some hypervisor APIs take a pointer to a bitmap array
1105 * whose size is at least the number of cpus on the chip.
1106 * We use a struct cpumask for this, so it must be big enough.
1107 */
1108 if ((smp_height * smp_width) > nr_cpu_ids)
1109 early_panic("Hypervisor %d x %d grid too big for Linux"
1110 " NR_CPUS %d\n", smp_height, smp_width,
1111 nr_cpu_ids);
1112#endif
1113
1114 /*
1115 * Check that we're using allowed ASIDs, and initialize the
1116 * various asid variables to their appropriate initial states.
1117 */
1118 asid_range = hv_inquire_asid(0);
1119 __get_cpu_var(current_asid) = min_asid = asid_range.start;
1120 max_asid = asid_range.start + asid_range.size - 1;
1121
1122 if (hv_confstr(HV_CONFSTR_CHIP_MODEL, (HV_VirtAddr)chip_model,
1123 sizeof(chip_model)) < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001124 pr_err("Warning: HV_CONFSTR_CHIP_MODEL not available\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001125 strlcpy(chip_model, "unknown", sizeof(chip_model));
1126 }
1127}
1128
1129static void __init validate_va(void)
1130{
1131#ifndef __tilegx__ /* FIXME: GX: probably some validation relevant here */
1132 /*
1133 * Similarly, make sure we're only using allowed VAs.
1134 * We assume we can contiguously use MEM_USER_INTRPT .. MEM_HV_INTRPT,
1135 * and 0 .. KERNEL_HIGH_VADDR.
1136 * In addition, make sure we CAN'T use the end of memory, since
1137 * we use the last chunk of each pgd for the pgd_list.
1138 */
Chris Metcalfa78c9422010-10-14 16:23:03 -04001139 int i, user_kernel_ok = 0;
Chris Metcalf867e3592010-05-28 23:09:12 -04001140 unsigned long max_va = 0;
1141 unsigned long list_va =
1142 ((PGD_LIST_OFFSET / sizeof(pgd_t)) << PGDIR_SHIFT);
1143
1144 for (i = 0; ; ++i) {
1145 HV_VirtAddrRange range = hv_inquire_virtual(i);
1146 if (range.size == 0)
1147 break;
1148 if (range.start <= MEM_USER_INTRPT &&
1149 range.start + range.size >= MEM_HV_INTRPT)
Chris Metcalfa78c9422010-10-14 16:23:03 -04001150 user_kernel_ok = 1;
Chris Metcalf867e3592010-05-28 23:09:12 -04001151 if (range.start == 0)
1152 max_va = range.size;
1153 BUG_ON(range.start + range.size > list_va);
1154 }
Chris Metcalfa78c9422010-10-14 16:23:03 -04001155 if (!user_kernel_ok)
1156 early_panic("Hypervisor not configured for user/kernel VAs\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001157 if (max_va == 0)
1158 early_panic("Hypervisor not configured for low VAs\n");
1159 if (max_va < KERNEL_HIGH_VADDR)
1160 early_panic("Hypervisor max VA %#lx smaller than %#lx\n",
1161 max_va, KERNEL_HIGH_VADDR);
1162
1163 /* Kernel PCs must have their high bit set; see intvec.S. */
1164 if ((long)VMALLOC_START >= 0)
1165 early_panic(
1166 "Linux VMALLOC region below the 2GB line (%#lx)!\n"
1167 "Reconfigure the kernel with fewer NR_HUGE_VMAPS\n"
1168 "or smaller VMALLOC_RESERVE.\n",
1169 VMALLOC_START);
1170#endif
1171}
1172
1173/*
1174 * cpu_lotar_map lists all the cpus that are valid for the supervisor
1175 * to cache data on at a page level, i.e. what cpus can be placed in
1176 * the LOTAR field of a PTE. It is equivalent to the set of possible
1177 * cpus plus any other cpus that are willing to share their cache.
1178 * It is set by hv_inquire_tiles(HV_INQ_TILES_LOTAR).
1179 */
1180struct cpumask __write_once cpu_lotar_map;
1181EXPORT_SYMBOL(cpu_lotar_map);
1182
1183#if CHIP_HAS_CBOX_HOME_MAP()
1184/*
1185 * hash_for_home_map lists all the tiles that hash-for-home data
1186 * will be cached on. Note that this may includes tiles that are not
1187 * valid for this supervisor to use otherwise (e.g. if a hypervisor
1188 * device is being shared between multiple supervisors).
1189 * It is set by hv_inquire_tiles(HV_INQ_TILES_HFH_CACHE).
1190 */
1191struct cpumask hash_for_home_map;
1192EXPORT_SYMBOL(hash_for_home_map);
1193#endif
1194
1195/*
1196 * cpu_cacheable_map lists all the cpus whose caches the hypervisor can
Rusty Russell5f054e32012-03-29 15:38:31 +10301197 * flush on our behalf. It is set to cpu_possible_mask OR'ed with
Chris Metcalf867e3592010-05-28 23:09:12 -04001198 * hash_for_home_map, and it is what should be passed to
1199 * hv_flush_remote() to flush all caches. Note that if there are
1200 * dedicated hypervisor driver tiles that have authorized use of their
1201 * cache, those tiles will only appear in cpu_lotar_map, NOT in
1202 * cpu_cacheable_map, as they are a special case.
1203 */
1204struct cpumask __write_once cpu_cacheable_map;
1205EXPORT_SYMBOL(cpu_cacheable_map);
1206
1207static __initdata struct cpumask disabled_map;
1208
1209static int __init disabled_cpus(char *str)
1210{
1211 int boot_cpu = smp_processor_id();
1212
1213 if (str == NULL || cpulist_parse_crop(str, &disabled_map) != 0)
1214 return -EINVAL;
1215 if (cpumask_test_cpu(boot_cpu, &disabled_map)) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001216 pr_err("disabled_cpus: can't disable boot cpu %d\n", boot_cpu);
Chris Metcalf867e3592010-05-28 23:09:12 -04001217 cpumask_clear_cpu(boot_cpu, &disabled_map);
1218 }
1219 return 0;
1220}
1221
1222early_param("disabled_cpus", disabled_cpus);
1223
Chris Metcalf0707ad32010-06-25 17:04:17 -04001224void __init print_disabled_cpus(void)
Chris Metcalf867e3592010-05-28 23:09:12 -04001225{
1226 if (!cpumask_empty(&disabled_map)) {
1227 char buf[100];
1228 cpulist_scnprintf(buf, sizeof(buf), &disabled_map);
Chris Metcalf0707ad32010-06-25 17:04:17 -04001229 pr_info("CPUs not available for Linux: %s\n", buf);
Chris Metcalf867e3592010-05-28 23:09:12 -04001230 }
1231}
1232
1233static void __init setup_cpu_maps(void)
1234{
1235 struct cpumask hv_disabled_map, cpu_possible_init;
1236 int boot_cpu = smp_processor_id();
1237 int cpus, i, rc;
1238
1239 /* Learn which cpus are allowed by the hypervisor. */
1240 rc = hv_inquire_tiles(HV_INQ_TILES_AVAIL,
1241 (HV_VirtAddr) cpumask_bits(&cpu_possible_init),
1242 sizeof(cpu_cacheable_map));
1243 if (rc < 0)
1244 early_panic("hv_inquire_tiles(AVAIL) failed: rc %d\n", rc);
1245 if (!cpumask_test_cpu(boot_cpu, &cpu_possible_init))
1246 early_panic("Boot CPU %d disabled by hypervisor!\n", boot_cpu);
1247
1248 /* Compute the cpus disabled by the hvconfig file. */
1249 cpumask_complement(&hv_disabled_map, &cpu_possible_init);
1250
1251 /* Include them with the cpus disabled by "disabled_cpus". */
1252 cpumask_or(&disabled_map, &disabled_map, &hv_disabled_map);
1253
1254 /*
1255 * Disable every cpu after "setup_max_cpus". But don't mark
1256 * as disabled the cpus that are outside of our initial rectangle,
1257 * since that turns out to be confusing.
1258 */
1259 cpus = 1; /* this cpu */
1260 cpumask_set_cpu(boot_cpu, &disabled_map); /* ignore this cpu */
1261 for (i = 0; cpus < setup_max_cpus; ++i)
1262 if (!cpumask_test_cpu(i, &disabled_map))
1263 ++cpus;
1264 for (; i < smp_height * smp_width; ++i)
1265 cpumask_set_cpu(i, &disabled_map);
1266 cpumask_clear_cpu(boot_cpu, &disabled_map); /* reset this cpu */
1267 for (i = smp_height * smp_width; i < NR_CPUS; ++i)
1268 cpumask_clear_cpu(i, &disabled_map);
1269
1270 /*
1271 * Setup cpu_possible map as every cpu allocated to us, minus
1272 * the results of any "disabled_cpus" settings.
1273 */
1274 cpumask_andnot(&cpu_possible_init, &cpu_possible_init, &disabled_map);
1275 init_cpu_possible(&cpu_possible_init);
1276
1277 /* Learn which cpus are valid for LOTAR caching. */
1278 rc = hv_inquire_tiles(HV_INQ_TILES_LOTAR,
1279 (HV_VirtAddr) cpumask_bits(&cpu_lotar_map),
1280 sizeof(cpu_lotar_map));
1281 if (rc < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001282 pr_err("warning: no HV_INQ_TILES_LOTAR; using AVAIL\n");
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301283 cpu_lotar_map = *cpu_possible_mask;
Chris Metcalf867e3592010-05-28 23:09:12 -04001284 }
1285
1286#if CHIP_HAS_CBOX_HOME_MAP()
1287 /* Retrieve set of CPUs used for hash-for-home caching */
1288 rc = hv_inquire_tiles(HV_INQ_TILES_HFH_CACHE,
1289 (HV_VirtAddr) hash_for_home_map.bits,
1290 sizeof(hash_for_home_map));
1291 if (rc < 0)
1292 early_panic("hv_inquire_tiles(HFH_CACHE) failed: rc %d\n", rc);
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301293 cpumask_or(&cpu_cacheable_map, cpu_possible_mask, &hash_for_home_map);
Chris Metcalf867e3592010-05-28 23:09:12 -04001294#else
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301295 cpu_cacheable_map = *cpu_possible_mask;
Chris Metcalf867e3592010-05-28 23:09:12 -04001296#endif
1297}
1298
1299
1300static int __init dataplane(char *str)
1301{
Chris Metcalf0707ad32010-06-25 17:04:17 -04001302 pr_warning("WARNING: dataplane support disabled in this kernel\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001303 return 0;
1304}
1305
1306early_param("dataplane", dataplane);
1307
1308#ifdef CONFIG_CMDLINE_BOOL
1309static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
1310#endif
1311
1312void __init setup_arch(char **cmdline_p)
1313{
1314 int len;
1315
1316#if defined(CONFIG_CMDLINE_BOOL) && defined(CONFIG_CMDLINE_OVERRIDE)
1317 len = hv_get_command_line((HV_VirtAddr) boot_command_line,
1318 COMMAND_LINE_SIZE);
1319 if (boot_command_line[0])
Chris Metcalf0707ad32010-06-25 17:04:17 -04001320 pr_warning("WARNING: ignoring dynamic command line \"%s\"\n",
1321 boot_command_line);
Chris Metcalf867e3592010-05-28 23:09:12 -04001322 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
1323#else
1324 char *hv_cmdline;
1325#if defined(CONFIG_CMDLINE_BOOL)
1326 if (builtin_cmdline[0]) {
1327 int builtin_len = strlcpy(boot_command_line, builtin_cmdline,
1328 COMMAND_LINE_SIZE);
1329 if (builtin_len < COMMAND_LINE_SIZE-1)
1330 boot_command_line[builtin_len++] = ' ';
1331 hv_cmdline = &boot_command_line[builtin_len];
1332 len = COMMAND_LINE_SIZE - builtin_len;
1333 } else
1334#endif
1335 {
1336 hv_cmdline = boot_command_line;
1337 len = COMMAND_LINE_SIZE;
1338 }
1339 len = hv_get_command_line((HV_VirtAddr) hv_cmdline, len);
1340 if (len < 0 || len > COMMAND_LINE_SIZE)
1341 early_panic("hv_get_command_line failed: %d\n", len);
1342#endif
1343
1344 *cmdline_p = boot_command_line;
1345
1346 /* Set disabled_map and setup_max_cpus very early */
1347 parse_early_param();
1348
1349 /* Make sure the kernel is compatible with the hypervisor. */
1350 validate_hv();
1351 validate_va();
1352
1353 setup_cpu_maps();
1354
1355
1356#ifdef CONFIG_PCI
Chris Metcalf12962262012-04-07 17:10:17 -04001357#if !defined (__tilegx__)
Chris Metcalf867e3592010-05-28 23:09:12 -04001358 /*
1359 * Initialize the PCI structures. This is done before memory
1360 * setup so that we know whether or not a pci_reserve region
1361 * is necessary.
1362 */
1363 if (tile_pci_init() == 0)
1364 pci_reserve_mb = 0;
Chris Metcalf12962262012-04-07 17:10:17 -04001365#endif
Chris Metcalf867e3592010-05-28 23:09:12 -04001366
1367 /* PCI systems reserve a region just below 4GB for mapping iomem. */
1368 pci_reserve_end_pfn = (1 << (32 - PAGE_SHIFT));
1369 pci_reserve_start_pfn = pci_reserve_end_pfn -
1370 (pci_reserve_mb << (20 - PAGE_SHIFT));
1371#endif
1372
1373 init_mm.start_code = (unsigned long) _text;
1374 init_mm.end_code = (unsigned long) _etext;
1375 init_mm.end_data = (unsigned long) _edata;
1376 init_mm.brk = (unsigned long) _end;
1377
1378 setup_memory();
1379 store_permanent_mappings();
1380 setup_bootmem_allocator();
1381
1382 /*
1383 * NOTE: before this point _nobody_ is allowed to allocate
1384 * any memory using the bootmem allocator.
1385 */
1386
1387 paging_init();
1388 setup_numa_mapping();
1389 zone_sizes_init();
1390 set_page_homes();
Chris Metcalf0707ad32010-06-25 17:04:17 -04001391 setup_cpu(1);
Chris Metcalf867e3592010-05-28 23:09:12 -04001392 setup_clock();
1393 load_hv_initrd();
Chris Metcalf12962262012-04-07 17:10:17 -04001394
1395#if defined(CONFIG_PCI) && defined (__tilegx__)
1396 tile_pci_init();
1397#endif
Chris Metcalf867e3592010-05-28 23:09:12 -04001398}
1399
1400
1401/*
1402 * Set up per-cpu memory.
1403 */
1404
1405unsigned long __per_cpu_offset[NR_CPUS] __write_once;
1406EXPORT_SYMBOL(__per_cpu_offset);
1407
1408static size_t __initdata pfn_offset[MAX_NUMNODES] = { 0 };
1409static unsigned long __initdata percpu_pfn[NR_CPUS] = { 0 };
1410
1411/*
1412 * As the percpu code allocates pages, we return the pages from the
1413 * end of the node for the specified cpu.
1414 */
1415static void *__init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
1416{
1417 int nid = cpu_to_node(cpu);
1418 unsigned long pfn = node_percpu_pfn[nid] + pfn_offset[nid];
1419
1420 BUG_ON(size % PAGE_SIZE != 0);
1421 pfn_offset[nid] += size / PAGE_SIZE;
Chris Metcalf76c567f2011-02-28 16:37:34 -05001422 BUG_ON(node_percpu[nid] < size);
1423 node_percpu[nid] -= size;
Chris Metcalf867e3592010-05-28 23:09:12 -04001424 if (percpu_pfn[cpu] == 0)
1425 percpu_pfn[cpu] = pfn;
1426 return pfn_to_kaddr(pfn);
1427}
1428
1429/*
1430 * Pages reserved for percpu memory are not freeable, and in any case we are
1431 * on a short path to panic() in setup_per_cpu_area() at this point anyway.
1432 */
1433static void __init pcpu_fc_free(void *ptr, size_t size)
1434{
1435}
1436
1437/*
1438 * Set up vmalloc page tables using bootmem for the percpu code.
1439 */
1440static void __init pcpu_fc_populate_pte(unsigned long addr)
1441{
1442 pgd_t *pgd;
1443 pud_t *pud;
1444 pmd_t *pmd;
1445 pte_t *pte;
1446
1447 BUG_ON(pgd_addr_invalid(addr));
Chris Metcalf77d23302010-10-14 14:47:35 -04001448 if (addr < VMALLOC_START || addr >= VMALLOC_END)
1449 panic("PCPU addr %#lx outside vmalloc range %#lx..%#lx;"
1450 " try increasing CONFIG_VMALLOC_RESERVE\n",
1451 addr, VMALLOC_START, VMALLOC_END);
Chris Metcalf867e3592010-05-28 23:09:12 -04001452
1453 pgd = swapper_pg_dir + pgd_index(addr);
1454 pud = pud_offset(pgd, addr);
1455 BUG_ON(!pud_present(*pud));
1456 pmd = pmd_offset(pud, addr);
1457 if (pmd_present(*pmd)) {
1458 BUG_ON(pmd_huge_page(*pmd));
1459 } else {
1460 pte = __alloc_bootmem(L2_KERNEL_PGTABLE_SIZE,
1461 HV_PAGE_TABLE_ALIGN, 0);
1462 pmd_populate_kernel(&init_mm, pmd, pte);
1463 }
1464}
1465
1466void __init setup_per_cpu_areas(void)
1467{
1468 struct page *pg;
1469 unsigned long delta, pfn, lowmem_va;
1470 unsigned long size = percpu_size();
1471 char *ptr;
1472 int rc, cpu, i;
1473
1474 rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, pcpu_fc_alloc,
1475 pcpu_fc_free, pcpu_fc_populate_pte);
1476 if (rc < 0)
1477 panic("Cannot initialize percpu area (err=%d)", rc);
1478
1479 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
1480 for_each_possible_cpu(cpu) {
1481 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
1482
1483 /* finv the copy out of cache so we can change homecache */
1484 ptr = pcpu_base_addr + pcpu_unit_offsets[cpu];
1485 __finv_buffer(ptr, size);
1486 pfn = percpu_pfn[cpu];
1487
1488 /* Rewrite the page tables to cache on that cpu */
1489 pg = pfn_to_page(pfn);
1490 for (i = 0; i < size; i += PAGE_SIZE, ++pfn, ++pg) {
1491
1492 /* Update the vmalloc mapping and page home. */
Chris Metcalfd5d14ed2012-03-29 13:58:43 -04001493 unsigned long addr = (unsigned long)ptr + i;
1494 pte_t *ptep = virt_to_pte(NULL, addr);
Chris Metcalf867e3592010-05-28 23:09:12 -04001495 pte_t pte = *ptep;
1496 BUG_ON(pfn != pte_pfn(pte));
1497 pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_TILE_L3);
1498 pte = set_remote_cache_cpu(pte, cpu);
Chris Metcalfd5d14ed2012-03-29 13:58:43 -04001499 set_pte_at(&init_mm, addr, ptep, pte);
Chris Metcalf867e3592010-05-28 23:09:12 -04001500
1501 /* Update the lowmem mapping for consistency. */
1502 lowmem_va = (unsigned long)pfn_to_kaddr(pfn);
1503 ptep = virt_to_pte(NULL, lowmem_va);
1504 if (pte_huge(*ptep)) {
1505 printk(KERN_DEBUG "early shatter of huge page"
1506 " at %#lx\n", lowmem_va);
1507 shatter_pmd((pmd_t *)ptep);
1508 ptep = virt_to_pte(NULL, lowmem_va);
1509 BUG_ON(pte_huge(*ptep));
1510 }
1511 BUG_ON(pfn != pte_pfn(*ptep));
Chris Metcalfd5d14ed2012-03-29 13:58:43 -04001512 set_pte_at(&init_mm, lowmem_va, ptep, pte);
Chris Metcalf867e3592010-05-28 23:09:12 -04001513 }
1514 }
1515
1516 /* Set our thread pointer appropriately. */
1517 set_my_cpu_offset(__per_cpu_offset[smp_processor_id()]);
1518
1519 /* Make sure the finv's have completed. */
1520 mb_incoherent();
1521
1522 /* Flush the TLB so we reference it properly from here on out. */
1523 local_flush_tlb_all();
1524}
1525
1526static struct resource data_resource = {
1527 .name = "Kernel data",
1528 .start = 0,
1529 .end = 0,
1530 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
1531};
1532
1533static struct resource code_resource = {
1534 .name = "Kernel code",
1535 .start = 0,
1536 .end = 0,
1537 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
1538};
1539
1540/*
1541 * We reserve all resources above 4GB so that PCI won't try to put
1542 * mappings above 4GB; the standard allows that for some devices but
1543 * the probing code trunates values to 32 bits.
1544 */
1545#ifdef CONFIG_PCI
1546static struct resource* __init
1547insert_non_bus_resource(void)
1548{
1549 struct resource *res =
1550 kzalloc(sizeof(struct resource), GFP_ATOMIC);
1551 res->name = "Non-Bus Physical Address Space";
1552 res->start = (1ULL << 32);
1553 res->end = -1LL;
1554 res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1555 if (insert_resource(&iomem_resource, res)) {
1556 kfree(res);
1557 return NULL;
1558 }
1559 return res;
1560}
1561#endif
1562
1563static struct resource* __init
1564insert_ram_resource(u64 start_pfn, u64 end_pfn)
1565{
1566 struct resource *res =
1567 kzalloc(sizeof(struct resource), GFP_ATOMIC);
1568 res->name = "System RAM";
1569 res->start = start_pfn << PAGE_SHIFT;
1570 res->end = (end_pfn << PAGE_SHIFT) - 1;
1571 res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1572 if (insert_resource(&iomem_resource, res)) {
1573 kfree(res);
1574 return NULL;
1575 }
1576 return res;
1577}
1578
1579/*
1580 * Request address space for all standard resources
1581 *
1582 * If the system includes PCI root complex drivers, we need to create
1583 * a window just below 4GB where PCI BARs can be mapped.
1584 */
1585static int __init request_standard_resources(void)
1586{
1587 int i;
1588 enum { CODE_DELTA = MEM_SV_INTRPT - PAGE_OFFSET };
1589
1590 iomem_resource.end = -1LL;
1591#ifdef CONFIG_PCI
1592 insert_non_bus_resource();
1593#endif
1594
1595 for_each_online_node(i) {
1596 u64 start_pfn = node_start_pfn[i];
1597 u64 end_pfn = node_end_pfn[i];
1598
1599#ifdef CONFIG_PCI
1600 if (start_pfn <= pci_reserve_start_pfn &&
1601 end_pfn > pci_reserve_start_pfn) {
1602 if (end_pfn > pci_reserve_end_pfn)
1603 insert_ram_resource(pci_reserve_end_pfn,
1604 end_pfn);
1605 end_pfn = pci_reserve_start_pfn;
1606 }
1607#endif
1608 insert_ram_resource(start_pfn, end_pfn);
1609 }
1610
1611 code_resource.start = __pa(_text - CODE_DELTA);
1612 code_resource.end = __pa(_etext - CODE_DELTA)-1;
1613 data_resource.start = __pa(_sdata);
1614 data_resource.end = __pa(_end)-1;
1615
1616 insert_resource(&iomem_resource, &code_resource);
1617 insert_resource(&iomem_resource, &data_resource);
1618
1619#ifdef CONFIG_KEXEC
1620 insert_resource(&iomem_resource, &crashk_res);
1621#endif
1622
1623 return 0;
1624}
1625
1626subsys_initcall(request_standard_resources);