blob: 445c220eae514ae1f3777579a8baea21aff6d576 [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 Metcalf867e3592010-05-28 23:09:12 -040032#include <asm/setup.h>
33#include <asm/sections.h>
Chris Metcalf867e3592010-05-28 23:09:12 -040034#include <asm/cacheflush.h>
35#include <asm/pgalloc.h>
36#include <asm/mmu_context.h>
37#include <hv/hypervisor.h>
38#include <arch/interrupts.h>
39
40/* <linux/smp.h> doesn't provide this definition. */
41#ifndef CONFIG_SMP
42#define setup_max_cpus 1
43#endif
44
45static inline int ABS(int x) { return x >= 0 ? x : -x; }
46
47/* Chip information */
48char chip_model[64] __write_once;
49
50struct pglist_data node_data[MAX_NUMNODES] __read_mostly;
51EXPORT_SYMBOL(node_data);
52
Chris Metcalf867e3592010-05-28 23:09:12 -040053/* Information on the NUMA nodes that we compute early */
54unsigned long __cpuinitdata node_start_pfn[MAX_NUMNODES];
55unsigned long __cpuinitdata node_end_pfn[MAX_NUMNODES];
56unsigned long __initdata node_memmap_pfn[MAX_NUMNODES];
57unsigned long __initdata node_percpu_pfn[MAX_NUMNODES];
58unsigned long __initdata node_free_pfn[MAX_NUMNODES];
59
Chris Metcalf76c567f2011-02-28 16:37:34 -050060static unsigned long __initdata node_percpu[MAX_NUMNODES];
61
Chris Metcalf867e3592010-05-28 23:09:12 -040062#ifdef CONFIG_HIGHMEM
63/* Page frame index of end of lowmem on each controller. */
64unsigned long __cpuinitdata node_lowmem_end_pfn[MAX_NUMNODES];
65
66/* Number of pages that can be mapped into lowmem. */
67static unsigned long __initdata mappable_physpages;
68#endif
69
70/* Data on which physical memory controller corresponds to which NUMA node */
71int node_controller[MAX_NUMNODES] = { [0 ... MAX_NUMNODES-1] = -1 };
72
73#ifdef CONFIG_HIGHMEM
74/* Map information from VAs to PAs */
75unsigned long pbase_map[1 << (32 - HPAGE_SHIFT)]
76 __write_once __attribute__((aligned(L2_CACHE_BYTES)));
77EXPORT_SYMBOL(pbase_map);
78
79/* Map information from PAs to VAs */
80void *vbase_map[NR_PA_HIGHBIT_VALUES]
81 __write_once __attribute__((aligned(L2_CACHE_BYTES)));
82EXPORT_SYMBOL(vbase_map);
83#endif
84
85/* Node number as a function of the high PA bits */
86int highbits_to_node[NR_PA_HIGHBIT_VALUES] __write_once;
87EXPORT_SYMBOL(highbits_to_node);
88
89static unsigned int __initdata maxmem_pfn = -1U;
90static unsigned int __initdata maxnodemem_pfn[MAX_NUMNODES] = {
91 [0 ... MAX_NUMNODES-1] = -1U
92};
93static nodemask_t __initdata isolnodes;
94
95#ifdef CONFIG_PCI
96enum { DEFAULT_PCI_RESERVE_MB = 64 };
97static unsigned int __initdata pci_reserve_mb = DEFAULT_PCI_RESERVE_MB;
98unsigned long __initdata pci_reserve_start_pfn = -1U;
99unsigned long __initdata pci_reserve_end_pfn = -1U;
100#endif
101
102static int __init setup_maxmem(char *str)
103{
Chris Metcalfbfffe792012-03-29 15:56:18 -0400104 unsigned long long maxmem;
105 if (str == NULL || (maxmem = memparse(str, NULL)) == 0)
Chris Metcalf867e3592010-05-28 23:09:12 -0400106 return -EINVAL;
107
Chris Metcalfbfffe792012-03-29 15:56:18 -0400108 maxmem_pfn = (maxmem >> HPAGE_SHIFT) << (HPAGE_SHIFT - PAGE_SHIFT);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400109 pr_info("Forcing RAM used to no more than %dMB\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400110 maxmem_pfn >> (20 - PAGE_SHIFT));
111 return 0;
112}
113early_param("maxmem", setup_maxmem);
114
115static int __init setup_maxnodemem(char *str)
116{
117 char *endp;
Chris Metcalfbfffe792012-03-29 15:56:18 -0400118 unsigned long long maxnodemem;
119 long node;
Chris Metcalf867e3592010-05-28 23:09:12 -0400120
121 node = str ? simple_strtoul(str, &endp, 0) : INT_MAX;
Chris Metcalfbfffe792012-03-29 15:56:18 -0400122 if (node >= MAX_NUMNODES || *endp != ':')
Chris Metcalf867e3592010-05-28 23:09:12 -0400123 return -EINVAL;
124
Chris Metcalfbfffe792012-03-29 15:56:18 -0400125 maxnodemem = memparse(endp+1, NULL);
126 maxnodemem_pfn[node] = (maxnodemem >> HPAGE_SHIFT) <<
Chris Metcalf867e3592010-05-28 23:09:12 -0400127 (HPAGE_SHIFT - PAGE_SHIFT);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400128 pr_info("Forcing RAM used on node %ld to no more than %dMB\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400129 node, maxnodemem_pfn[node] >> (20 - PAGE_SHIFT));
130 return 0;
131}
132early_param("maxnodemem", setup_maxnodemem);
133
134static int __init setup_isolnodes(char *str)
135{
136 char buf[MAX_NUMNODES * 5];
137 if (str == NULL || nodelist_parse(str, isolnodes) != 0)
138 return -EINVAL;
139
140 nodelist_scnprintf(buf, sizeof(buf), isolnodes);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400141 pr_info("Set isolnodes value to '%s'\n", buf);
Chris Metcalf867e3592010-05-28 23:09:12 -0400142 return 0;
143}
144early_param("isolnodes", setup_isolnodes);
145
146#ifdef CONFIG_PCI
147static int __init setup_pci_reserve(char* str)
148{
149 unsigned long mb;
150
151 if (str == NULL || strict_strtoul(str, 0, &mb) != 0 ||
152 mb > 3 * 1024)
153 return -EINVAL;
154
155 pci_reserve_mb = mb;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400156 pr_info("Reserving %dMB for PCIE root complex mappings\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400157 pci_reserve_mb);
158 return 0;
159}
160early_param("pci_reserve", setup_pci_reserve);
161#endif
162
163#ifndef __tilegx__
164/*
165 * vmalloc=size forces the vmalloc area to be exactly 'size' bytes.
166 * This can be used to increase (or decrease) the vmalloc area.
167 */
168static int __init parse_vmalloc(char *arg)
169{
170 if (!arg)
171 return -EINVAL;
172
173 VMALLOC_RESERVE = (memparse(arg, &arg) + PGDIR_SIZE - 1) & PGDIR_MASK;
174
175 /* See validate_va() for more on this test. */
176 if ((long)_VMALLOC_START >= 0)
177 early_panic("\"vmalloc=%#lx\" value too large: maximum %#lx\n",
178 VMALLOC_RESERVE, _VMALLOC_END - 0x80000000UL);
179
180 return 0;
181}
182early_param("vmalloc", parse_vmalloc);
183#endif
184
185#ifdef CONFIG_HIGHMEM
186/*
Chris Metcalfa78c9422010-10-14 16:23:03 -0400187 * Determine for each controller where its lowmem is mapped and how much of
188 * it is mapped there. On controller zero, the first few megabytes are
189 * already mapped in as code at MEM_SV_INTRPT, so in principle we could
190 * start our data mappings higher up, but for now we don't bother, to avoid
191 * additional confusion.
Chris Metcalf867e3592010-05-28 23:09:12 -0400192 *
193 * One question is whether, on systems with more than 768 Mb and
194 * controllers of different sizes, to map in a proportionate amount of
195 * each one, or to try to map the same amount from each controller.
196 * (E.g. if we have three controllers with 256MB, 1GB, and 256MB
197 * respectively, do we map 256MB from each, or do we map 128 MB, 512
198 * MB, and 128 MB respectively?) For now we use a proportionate
199 * solution like the latter.
200 *
201 * The VA/PA mapping demands that we align our decisions at 16 MB
202 * boundaries so that we can rapidly convert VA to PA.
203 */
204static void *__init setup_pa_va_mapping(void)
205{
206 unsigned long curr_pages = 0;
207 unsigned long vaddr = PAGE_OFFSET;
208 nodemask_t highonlynodes = isolnodes;
209 int i, j;
210
211 memset(pbase_map, -1, sizeof(pbase_map));
212 memset(vbase_map, -1, sizeof(vbase_map));
213
214 /* Node zero cannot be isolated for LOWMEM purposes. */
215 node_clear(0, highonlynodes);
216
217 /* Count up the number of pages on non-highonlynodes controllers. */
218 mappable_physpages = 0;
219 for_each_online_node(i) {
220 if (!node_isset(i, highonlynodes))
221 mappable_physpages +=
222 node_end_pfn[i] - node_start_pfn[i];
223 }
224
225 for_each_online_node(i) {
226 unsigned long start = node_start_pfn[i];
227 unsigned long end = node_end_pfn[i];
228 unsigned long size = end - start;
229 unsigned long vaddr_end;
230
231 if (node_isset(i, highonlynodes)) {
232 /* Mark this controller as having no lowmem. */
233 node_lowmem_end_pfn[i] = start;
234 continue;
235 }
236
237 curr_pages += size;
238 if (mappable_physpages > MAXMEM_PFN) {
239 vaddr_end = PAGE_OFFSET +
240 (((u64)curr_pages * MAXMEM_PFN /
241 mappable_physpages)
242 << PAGE_SHIFT);
243 } else {
244 vaddr_end = PAGE_OFFSET + (curr_pages << PAGE_SHIFT);
245 }
246 for (j = 0; vaddr < vaddr_end; vaddr += HPAGE_SIZE, ++j) {
247 unsigned long this_pfn =
248 start + (j << HUGETLB_PAGE_ORDER);
249 pbase_map[vaddr >> HPAGE_SHIFT] = this_pfn;
250 if (vbase_map[__pfn_to_highbits(this_pfn)] ==
251 (void *)-1)
252 vbase_map[__pfn_to_highbits(this_pfn)] =
253 (void *)(vaddr & HPAGE_MASK);
254 }
255 node_lowmem_end_pfn[i] = start + (j << HUGETLB_PAGE_ORDER);
256 BUG_ON(node_lowmem_end_pfn[i] > end);
257 }
258
259 /* Return highest address of any mapped memory. */
260 return (void *)vaddr;
261}
262#endif /* CONFIG_HIGHMEM */
263
264/*
265 * Register our most important memory mappings with the debug stub.
266 *
267 * This is up to 4 mappings for lowmem, one mapping per memory
268 * controller, plus one for our text segment.
269 */
Chris Metcalf0707ad32010-06-25 17:04:17 -0400270static void __cpuinit store_permanent_mappings(void)
Chris Metcalf867e3592010-05-28 23:09:12 -0400271{
272 int i;
273
274 for_each_online_node(i) {
275 HV_PhysAddr pa = ((HV_PhysAddr)node_start_pfn[i]) << PAGE_SHIFT;
276#ifdef CONFIG_HIGHMEM
277 HV_PhysAddr high_mapped_pa = node_lowmem_end_pfn[i];
278#else
279 HV_PhysAddr high_mapped_pa = node_end_pfn[i];
280#endif
281
282 unsigned long pages = high_mapped_pa - node_start_pfn[i];
283 HV_VirtAddr addr = (HV_VirtAddr) __va(pa);
284 hv_store_mapping(addr, pages << PAGE_SHIFT, pa);
285 }
286
287 hv_store_mapping((HV_VirtAddr)_stext,
288 (uint32_t)(_einittext - _stext), 0);
289}
290
291/*
292 * Use hv_inquire_physical() to populate node_{start,end}_pfn[]
293 * and node_online_map, doing suitable sanity-checking.
294 * Also set min_low_pfn, max_low_pfn, and max_pfn.
295 */
296static void __init setup_memory(void)
297{
298 int i, j;
299 int highbits_seen[NR_PA_HIGHBIT_VALUES] = { 0 };
300#ifdef CONFIG_HIGHMEM
301 long highmem_pages;
302#endif
303#ifndef __tilegx__
304 int cap;
305#endif
306#if defined(CONFIG_HIGHMEM) || defined(__tilegx__)
307 long lowmem_pages;
308#endif
309
310 /* We are using a char to hold the cpu_2_node[] mapping */
Chris Metcalfe18105c2010-10-14 16:50:26 -0400311 BUILD_BUG_ON(MAX_NUMNODES > 127);
Chris Metcalf867e3592010-05-28 23:09:12 -0400312
313 /* Discover the ranges of memory available to us */
314 for (i = 0; ; ++i) {
315 unsigned long start, size, end, highbits;
316 HV_PhysAddrRange range = hv_inquire_physical(i);
317 if (range.size == 0)
318 break;
319#ifdef CONFIG_FLATMEM
320 if (i > 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400321 pr_err("Can't use discontiguous PAs: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400322 range.size, range.start + range.size);
323 continue;
324 }
325#endif
326#ifndef __tilegx__
327 if ((unsigned long)range.start) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400328 pr_err("Range not at 4GB multiple: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400329 range.start, range.start + range.size);
330 continue;
331 }
332#endif
333 if ((range.start & (HPAGE_SIZE-1)) != 0 ||
334 (range.size & (HPAGE_SIZE-1)) != 0) {
335 unsigned long long start_pa = range.start;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400336 unsigned long long orig_size = range.size;
Chris Metcalf867e3592010-05-28 23:09:12 -0400337 range.start = (start_pa + HPAGE_SIZE - 1) & HPAGE_MASK;
338 range.size -= (range.start - start_pa);
339 range.size &= HPAGE_MASK;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400340 pr_err("Range not hugepage-aligned: %#llx..%#llx:"
Chris Metcalf867e3592010-05-28 23:09:12 -0400341 " now %#llx-%#llx\n",
Chris Metcalf0707ad32010-06-25 17:04:17 -0400342 start_pa, start_pa + orig_size,
Chris Metcalf867e3592010-05-28 23:09:12 -0400343 range.start, range.start + range.size);
344 }
345 highbits = __pa_to_highbits(range.start);
346 if (highbits >= NR_PA_HIGHBIT_VALUES) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400347 pr_err("PA high bits too high: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400348 range.start, range.start + range.size);
349 continue;
350 }
351 if (highbits_seen[highbits]) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400352 pr_err("Range overlaps in high bits: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400353 range.start, range.start + range.size);
354 continue;
355 }
356 highbits_seen[highbits] = 1;
357 if (PFN_DOWN(range.size) > maxnodemem_pfn[i]) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400358 int max_size = maxnodemem_pfn[i];
359 if (max_size > 0) {
360 pr_err("Maxnodemem reduced node %d to"
361 " %d pages\n", i, max_size);
362 range.size = PFN_PHYS(max_size);
Chris Metcalf867e3592010-05-28 23:09:12 -0400363 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400364 pr_err("Maxnodemem disabled node %d\n", i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400365 continue;
366 }
367 }
368 if (num_physpages + PFN_DOWN(range.size) > maxmem_pfn) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400369 int max_size = maxmem_pfn - num_physpages;
370 if (max_size > 0) {
371 pr_err("Maxmem reduced node %d to %d pages\n",
372 i, max_size);
373 range.size = PFN_PHYS(max_size);
Chris Metcalf867e3592010-05-28 23:09:12 -0400374 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400375 pr_err("Maxmem disabled node %d\n", i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400376 continue;
377 }
378 }
379 if (i >= MAX_NUMNODES) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400380 pr_err("Too many PA nodes (#%d): %#llx...%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400381 i, range.size, range.size + range.start);
382 continue;
383 }
384
385 start = range.start >> PAGE_SHIFT;
386 size = range.size >> PAGE_SHIFT;
387 end = start + size;
388
389#ifndef __tilegx__
390 if (((HV_PhysAddr)end << PAGE_SHIFT) !=
391 (range.start + range.size)) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400392 pr_err("PAs too high to represent: %#llx..%#llx\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400393 range.start, range.start + range.size);
394 continue;
395 }
396#endif
397#ifdef CONFIG_PCI
398 /*
399 * Blocks that overlap the pci reserved region must
400 * have enough space to hold the maximum percpu data
401 * region at the top of the range. If there isn't
402 * enough space above the reserved region, just
403 * truncate the node.
404 */
405 if (start <= pci_reserve_start_pfn &&
406 end > pci_reserve_start_pfn) {
407 unsigned int per_cpu_size =
408 __per_cpu_end - __per_cpu_start;
409 unsigned int percpu_pages =
410 NR_CPUS * (PFN_UP(per_cpu_size) >> PAGE_SHIFT);
411 if (end < pci_reserve_end_pfn + percpu_pages) {
412 end = pci_reserve_start_pfn;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400413 pr_err("PCI mapping region reduced node %d to"
Chris Metcalf867e3592010-05-28 23:09:12 -0400414 " %ld pages\n", i, end - start);
415 }
416 }
417#endif
418
419 for (j = __pfn_to_highbits(start);
420 j <= __pfn_to_highbits(end - 1); j++)
421 highbits_to_node[j] = i;
422
423 node_start_pfn[i] = start;
424 node_end_pfn[i] = end;
425 node_controller[i] = range.controller;
426 num_physpages += size;
427 max_pfn = end;
428
429 /* Mark node as online */
430 node_set(i, node_online_map);
431 node_set(i, node_possible_map);
432 }
433
434#ifndef __tilegx__
435 /*
436 * For 4KB pages, mem_map "struct page" data is 1% of the size
437 * of the physical memory, so can be quite big (640 MB for
438 * four 16G zones). These structures must be mapped in
439 * lowmem, and since we currently cap out at about 768 MB,
440 * it's impractical to try to use this much address space.
441 * For now, arbitrarily cap the amount of physical memory
442 * we're willing to use at 8 million pages (32GB of 4KB pages).
443 */
444 cap = 8 * 1024 * 1024; /* 8 million pages */
445 if (num_physpages > cap) {
446 int num_nodes = num_online_nodes();
447 int cap_each = cap / num_nodes;
448 unsigned long dropped_pages = 0;
449 for (i = 0; i < num_nodes; ++i) {
450 int size = node_end_pfn[i] - node_start_pfn[i];
451 if (size > cap_each) {
452 dropped_pages += (size - cap_each);
453 node_end_pfn[i] = node_start_pfn[i] + cap_each;
454 }
455 }
456 num_physpages -= dropped_pages;
Chris Metcalf0707ad32010-06-25 17:04:17 -0400457 pr_warning("Only using %ldMB memory;"
Chris Metcalf867e3592010-05-28 23:09:12 -0400458 " ignoring %ldMB.\n",
459 num_physpages >> (20 - PAGE_SHIFT),
460 dropped_pages >> (20 - PAGE_SHIFT));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400461 pr_warning("Consider using a larger page size.\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400462 }
463#endif
464
465 /* Heap starts just above the last loaded address. */
466 min_low_pfn = PFN_UP((unsigned long)_end - PAGE_OFFSET);
467
468#ifdef CONFIG_HIGHMEM
469 /* Find where we map lowmem from each controller. */
470 high_memory = setup_pa_va_mapping();
471
472 /* Set max_low_pfn based on what node 0 can directly address. */
473 max_low_pfn = node_lowmem_end_pfn[0];
474
475 lowmem_pages = (mappable_physpages > MAXMEM_PFN) ?
476 MAXMEM_PFN : mappable_physpages;
477 highmem_pages = (long) (num_physpages - lowmem_pages);
478
Chris Metcalf0707ad32010-06-25 17:04:17 -0400479 pr_notice("%ldMB HIGHMEM available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400480 pages_to_mb(highmem_pages > 0 ? highmem_pages : 0));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400481 pr_notice("%ldMB LOWMEM available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400482 pages_to_mb(lowmem_pages));
483#else
484 /* Set max_low_pfn based on what node 0 can directly address. */
485 max_low_pfn = node_end_pfn[0];
486
487#ifndef __tilegx__
488 if (node_end_pfn[0] > MAXMEM_PFN) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400489 pr_warning("Only using %ldMB LOWMEM.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400490 MAXMEM>>20);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400491 pr_warning("Use a HIGHMEM enabled kernel.\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400492 max_low_pfn = MAXMEM_PFN;
493 max_pfn = MAXMEM_PFN;
494 num_physpages = MAXMEM_PFN;
495 node_end_pfn[0] = MAXMEM_PFN;
496 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400497 pr_notice("%ldMB memory available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400498 pages_to_mb(node_end_pfn[0]));
499 }
500 for (i = 1; i < MAX_NUMNODES; ++i) {
501 node_start_pfn[i] = 0;
502 node_end_pfn[i] = 0;
503 }
504 high_memory = __va(node_end_pfn[0]);
505#else
506 lowmem_pages = 0;
507 for (i = 0; i < MAX_NUMNODES; ++i) {
508 int pages = node_end_pfn[i] - node_start_pfn[i];
509 lowmem_pages += pages;
510 if (pages)
511 high_memory = pfn_to_kaddr(node_end_pfn[i]);
512 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400513 pr_notice("%ldMB memory available.\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400514 pages_to_mb(lowmem_pages));
515#endif
516#endif
517}
518
Chris Metcalf621b1952012-04-01 14:04:21 -0400519/*
520 * On 32-bit machines, we only put bootmem on the low controller,
521 * since PAs > 4GB can't be used in bootmem. In principle one could
522 * imagine, e.g., multiple 1 GB controllers all of which could support
523 * bootmem, but in practice using controllers this small isn't a
524 * particularly interesting scenario, so we just keep it simple and
525 * use only the first controller for bootmem on 32-bit machines.
526 */
527static inline int node_has_bootmem(int nid)
Chris Metcalf867e3592010-05-28 23:09:12 -0400528{
Chris Metcalf621b1952012-04-01 14:04:21 -0400529#ifdef CONFIG_64BIT
530 return 1;
Chris Metcalf867e3592010-05-28 23:09:12 -0400531#else
Chris Metcalf621b1952012-04-01 14:04:21 -0400532 return nid == 0;
533#endif
534}
535
536static inline unsigned long alloc_bootmem_pfn(int nid,
537 unsigned long size,
538 unsigned long goal)
539{
540 void *kva = __alloc_bootmem_node(NODE_DATA(nid), size,
541 PAGE_SIZE, goal);
542 unsigned long pfn = kaddr_to_pfn(kva);
543 BUG_ON(goal && PFN_PHYS(pfn) != goal);
544 return pfn;
545}
546
547static void __init setup_bootmem_allocator_node(int i)
548{
549 unsigned long start, end, mapsize, mapstart;
550
551 if (node_has_bootmem(i)) {
552 NODE_DATA(i)->bdata = &bootmem_node_data[i];
553 } else {
554 /* Share controller zero's bdata for now. */
555 NODE_DATA(i)->bdata = &bootmem_node_data[0];
556 return;
557 }
558
559 /* Skip up to after the bss in node 0. */
560 start = (i == 0) ? min_low_pfn : node_start_pfn[i];
561
562 /* Only lowmem, if we're a HIGHMEM build. */
563#ifdef CONFIG_HIGHMEM
564 end = node_lowmem_end_pfn[i];
565#else
566 end = node_end_pfn[i];
Chris Metcalf867e3592010-05-28 23:09:12 -0400567#endif
568
Chris Metcalf621b1952012-04-01 14:04:21 -0400569 /* No memory here. */
570 if (end == start)
571 return;
Chris Metcalf867e3592010-05-28 23:09:12 -0400572
Chris Metcalf621b1952012-04-01 14:04:21 -0400573 /* Figure out where the bootmem bitmap is located. */
574 mapsize = bootmem_bootmap_pages(end - start);
575 if (i == 0) {
576 /* Use some space right before the heap on node 0. */
577 mapstart = start;
578 start += mapsize;
579 } else {
580 /* Allocate bitmap on node 0 to avoid page table issues. */
581 mapstart = alloc_bootmem_pfn(0, PFN_PHYS(mapsize), 0);
582 }
Chris Metcalf867e3592010-05-28 23:09:12 -0400583
Chris Metcalf621b1952012-04-01 14:04:21 -0400584 /* Initialize a node. */
585 init_bootmem_node(NODE_DATA(i), mapstart, start, end);
586
587 /* Free all the space back into the allocator. */
588 free_bootmem(PFN_PHYS(start), PFN_PHYS(end - start));
589
590#if defined(CONFIG_PCI)
591 /*
592 * Throw away any memory aliased by the PCI region. FIXME: this
593 * is a temporary hack to work around bug 10502, and needs to be
594 * fixed properly.
595 */
596 if (pci_reserve_start_pfn < end && pci_reserve_end_pfn > start)
597 reserve_bootmem(PFN_PHYS(pci_reserve_start_pfn),
598 PFN_PHYS(pci_reserve_end_pfn -
599 pci_reserve_start_pfn),
600 BOOTMEM_EXCLUSIVE);
601#endif
602}
603
604static void __init setup_bootmem_allocator(void)
605{
606 int i;
607 for (i = 0; i < MAX_NUMNODES; ++i)
608 setup_bootmem_allocator_node(i);
Chris Metcalf867e3592010-05-28 23:09:12 -0400609
610#ifdef CONFIG_KEXEC
611 if (crashk_res.start != crashk_res.end)
Joe Perches28f65c112011-06-09 09:13:32 -0700612 reserve_bootmem(crashk_res.start, resource_size(&crashk_res), 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400613#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400614}
615
616void *__init alloc_remap(int nid, unsigned long size)
617{
618 int pages = node_end_pfn[nid] - node_start_pfn[nid];
619 void *map = pfn_to_kaddr(node_memmap_pfn[nid]);
620 BUG_ON(size != pages * sizeof(struct page));
621 memset(map, 0, size);
622 return map;
623}
624
625static int __init percpu_size(void)
626{
Chris Metcalf76c567f2011-02-28 16:37:34 -0500627 int size = __per_cpu_end - __per_cpu_start;
628 size += PERCPU_MODULE_RESERVE;
629 size += PERCPU_DYNAMIC_EARLY_SIZE;
630 if (size < PCPU_MIN_UNIT_SIZE)
631 size = PCPU_MIN_UNIT_SIZE;
632 size = roundup(size, PAGE_SIZE);
633
Chris Metcalf867e3592010-05-28 23:09:12 -0400634 /* In several places we assume the per-cpu data fits on a huge page. */
635 BUG_ON(kdata_huge && size > HPAGE_SIZE);
636 return size;
637}
638
Chris Metcalf867e3592010-05-28 23:09:12 -0400639static void __init zone_sizes_init(void)
640{
641 unsigned long zones_size[MAX_NR_ZONES] = { 0 };
Chris Metcalf867e3592010-05-28 23:09:12 -0400642 int size = percpu_size();
643 int num_cpus = smp_height * smp_width;
644 int i;
645
646 for (i = 0; i < num_cpus; ++i)
647 node_percpu[cpu_to_node(i)] += size;
648
649 for_each_online_node(i) {
650 unsigned long start = node_start_pfn[i];
651 unsigned long end = node_end_pfn[i];
652#ifdef CONFIG_HIGHMEM
653 unsigned long lowmem_end = node_lowmem_end_pfn[i];
654#else
655 unsigned long lowmem_end = end;
656#endif
657 int memmap_size = (end - start) * sizeof(struct page);
658 node_free_pfn[i] = start;
659
660 /*
661 * Set aside pages for per-cpu data and the mem_map array.
662 *
663 * Since the per-cpu data requires special homecaching,
664 * if we are in kdata_huge mode, we put it at the end of
665 * the lowmem region. If we're not in kdata_huge mode,
666 * we take the per-cpu pages from the bottom of the
667 * controller, since that avoids fragmenting a huge page
668 * that users might want. We always take the memmap
669 * from the bottom of the controller, since with
670 * kdata_huge that lets it be under a huge TLB entry.
671 *
672 * If the user has requested isolnodes for a controller,
673 * though, there'll be no lowmem, so we just alloc_bootmem
674 * the memmap. There will be no percpu memory either.
675 */
Chris Metcalf621b1952012-04-01 14:04:21 -0400676 if (i != 0 && cpu_isset(i, isolnodes)) {
677 node_memmap_pfn[i] =
678 alloc_bootmem_pfn(0, memmap_size, 0);
679 BUG_ON(node_percpu[i] != 0);
680 } else if (node_has_bootmem(start)) {
Chris Metcalf867e3592010-05-28 23:09:12 -0400681 unsigned long goal = 0;
682 node_memmap_pfn[i] =
Chris Metcalf621b1952012-04-01 14:04:21 -0400683 alloc_bootmem_pfn(i, memmap_size, 0);
Chris Metcalf867e3592010-05-28 23:09:12 -0400684 if (kdata_huge)
685 goal = PFN_PHYS(lowmem_end) - node_percpu[i];
686 if (node_percpu[i])
687 node_percpu_pfn[i] =
Chris Metcalf621b1952012-04-01 14:04:21 -0400688 alloc_bootmem_pfn(i, node_percpu[i],
689 goal);
Chris Metcalf867e3592010-05-28 23:09:12 -0400690 } else {
Chris Metcalf621b1952012-04-01 14:04:21 -0400691 /* In non-bootmem zones, just reserve some pages. */
Chris Metcalf867e3592010-05-28 23:09:12 -0400692 node_memmap_pfn[i] = node_free_pfn[i];
693 node_free_pfn[i] += PFN_UP(memmap_size);
694 if (!kdata_huge) {
695 node_percpu_pfn[i] = node_free_pfn[i];
696 node_free_pfn[i] += PFN_UP(node_percpu[i]);
697 } else {
698 node_percpu_pfn[i] =
699 lowmem_end - PFN_UP(node_percpu[i]);
700 }
701 }
702
703#ifdef CONFIG_HIGHMEM
704 if (start > lowmem_end) {
705 zones_size[ZONE_NORMAL] = 0;
706 zones_size[ZONE_HIGHMEM] = end - start;
707 } else {
708 zones_size[ZONE_NORMAL] = lowmem_end - start;
709 zones_size[ZONE_HIGHMEM] = end - lowmem_end;
710 }
711#else
712 zones_size[ZONE_NORMAL] = end - start;
713#endif
714
Chris Metcalf621b1952012-04-01 14:04:21 -0400715 /* Take zone metadata from controller 0 if we're isolnode. */
716 if (node_isset(i, isolnodes))
717 NODE_DATA(i)->bdata = &bootmem_node_data[0];
Chris Metcalf867e3592010-05-28 23:09:12 -0400718
719 free_area_init_node(i, zones_size, start, NULL);
Chris Metcalf76c567f2011-02-28 16:37:34 -0500720 printk(KERN_DEBUG " Normal zone: %ld per-cpu pages\n",
Chris Metcalf867e3592010-05-28 23:09:12 -0400721 PFN_UP(node_percpu[i]));
722
723 /* Track the type of memory on each node */
724 if (zones_size[ZONE_NORMAL])
725 node_set_state(i, N_NORMAL_MEMORY);
726#ifdef CONFIG_HIGHMEM
727 if (end != start)
728 node_set_state(i, N_HIGH_MEMORY);
729#endif
730
731 node_set_online(i);
732 }
733}
734
735#ifdef CONFIG_NUMA
736
737/* which logical CPUs are on which nodes */
738struct cpumask node_2_cpu_mask[MAX_NUMNODES] __write_once;
739EXPORT_SYMBOL(node_2_cpu_mask);
740
741/* which node each logical CPU is on */
742char cpu_2_node[NR_CPUS] __write_once __attribute__((aligned(L2_CACHE_BYTES)));
743EXPORT_SYMBOL(cpu_2_node);
744
745/* Return cpu_to_node() except for cpus not yet assigned, which return -1 */
746static int __init cpu_to_bound_node(int cpu, struct cpumask* unbound_cpus)
747{
748 if (!cpu_possible(cpu) || cpumask_test_cpu(cpu, unbound_cpus))
749 return -1;
750 else
751 return cpu_to_node(cpu);
752}
753
754/* Return number of immediately-adjacent tiles sharing the same NUMA node. */
755static int __init node_neighbors(int node, int cpu,
756 struct cpumask *unbound_cpus)
757{
758 int neighbors = 0;
759 int w = smp_width;
760 int h = smp_height;
761 int x = cpu % w;
762 int y = cpu / w;
763 if (x > 0 && cpu_to_bound_node(cpu-1, unbound_cpus) == node)
764 ++neighbors;
765 if (x < w-1 && cpu_to_bound_node(cpu+1, unbound_cpus) == node)
766 ++neighbors;
767 if (y > 0 && cpu_to_bound_node(cpu-w, unbound_cpus) == node)
768 ++neighbors;
769 if (y < h-1 && cpu_to_bound_node(cpu+w, unbound_cpus) == node)
770 ++neighbors;
771 return neighbors;
772}
773
774static void __init setup_numa_mapping(void)
775{
776 int distance[MAX_NUMNODES][NR_CPUS];
777 HV_Coord coord;
778 int cpu, node, cpus, i, x, y;
779 int num_nodes = num_online_nodes();
780 struct cpumask unbound_cpus;
781 nodemask_t default_nodes;
782
783 cpumask_clear(&unbound_cpus);
784
785 /* Get set of nodes we will use for defaults */
786 nodes_andnot(default_nodes, node_online_map, isolnodes);
787 if (nodes_empty(default_nodes)) {
788 BUG_ON(!node_isset(0, node_online_map));
Chris Metcalf0707ad32010-06-25 17:04:17 -0400789 pr_err("Forcing NUMA node zero available as a default node\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400790 node_set(0, default_nodes);
791 }
792
793 /* Populate the distance[] array */
794 memset(distance, -1, sizeof(distance));
795 cpu = 0;
796 for (coord.y = 0; coord.y < smp_height; ++coord.y) {
797 for (coord.x = 0; coord.x < smp_width;
798 ++coord.x, ++cpu) {
799 BUG_ON(cpu >= nr_cpu_ids);
800 if (!cpu_possible(cpu)) {
801 cpu_2_node[cpu] = -1;
802 continue;
803 }
804 for_each_node_mask(node, default_nodes) {
805 HV_MemoryControllerInfo info =
806 hv_inquire_memory_controller(
807 coord, node_controller[node]);
808 distance[node][cpu] =
809 ABS(info.coord.x) + ABS(info.coord.y);
810 }
811 cpumask_set_cpu(cpu, &unbound_cpus);
812 }
813 }
814 cpus = cpu;
815
816 /*
817 * Round-robin through the NUMA nodes until all the cpus are
818 * assigned. We could be more clever here (e.g. create four
819 * sorted linked lists on the same set of cpu nodes, and pull
820 * off them in round-robin sequence, removing from all four
821 * lists each time) but given the relatively small numbers
822 * involved, O(n^2) seem OK for a one-time cost.
823 */
824 node = first_node(default_nodes);
825 while (!cpumask_empty(&unbound_cpus)) {
826 int best_cpu = -1;
827 int best_distance = INT_MAX;
828 for (cpu = 0; cpu < cpus; ++cpu) {
829 if (cpumask_test_cpu(cpu, &unbound_cpus)) {
830 /*
831 * Compute metric, which is how much
832 * closer the cpu is to this memory
833 * controller than the others, shifted
834 * up, and then the number of
835 * neighbors already in the node as an
836 * epsilon adjustment to try to keep
837 * the nodes compact.
838 */
839 int d = distance[node][cpu] * num_nodes;
840 for_each_node_mask(i, default_nodes) {
841 if (i != node)
842 d -= distance[i][cpu];
843 }
844 d *= 8; /* allow space for epsilon */
845 d -= node_neighbors(node, cpu, &unbound_cpus);
846 if (d < best_distance) {
847 best_cpu = cpu;
848 best_distance = d;
849 }
850 }
851 }
852 BUG_ON(best_cpu < 0);
853 cpumask_set_cpu(best_cpu, &node_2_cpu_mask[node]);
854 cpu_2_node[best_cpu] = node;
855 cpumask_clear_cpu(best_cpu, &unbound_cpus);
856 node = next_node(node, default_nodes);
857 if (node == MAX_NUMNODES)
858 node = first_node(default_nodes);
859 }
860
861 /* Print out node assignments and set defaults for disabled cpus */
862 cpu = 0;
863 for (y = 0; y < smp_height; ++y) {
864 printk(KERN_DEBUG "NUMA cpu-to-node row %d:", y);
865 for (x = 0; x < smp_width; ++x, ++cpu) {
866 if (cpu_to_node(cpu) < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400867 pr_cont(" -");
Chris Metcalf867e3592010-05-28 23:09:12 -0400868 cpu_2_node[cpu] = first_node(default_nodes);
869 } else {
Chris Metcalf0707ad32010-06-25 17:04:17 -0400870 pr_cont(" %d", cpu_to_node(cpu));
Chris Metcalf867e3592010-05-28 23:09:12 -0400871 }
872 }
Chris Metcalf0707ad32010-06-25 17:04:17 -0400873 pr_cont("\n");
Chris Metcalf867e3592010-05-28 23:09:12 -0400874 }
875}
876
877static struct cpu cpu_devices[NR_CPUS];
878
879static int __init topology_init(void)
880{
881 int i;
882
883 for_each_online_node(i)
884 register_one_node(i);
885
Chris Metcalf4d658d12010-11-24 13:42:15 -0500886 for (i = 0; i < smp_height * smp_width; ++i)
Chris Metcalf867e3592010-05-28 23:09:12 -0400887 register_cpu(&cpu_devices[i], i);
888
889 return 0;
890}
891
892subsys_initcall(topology_init);
893
894#else /* !CONFIG_NUMA */
895
896#define setup_numa_mapping() do { } while (0)
897
898#endif /* CONFIG_NUMA */
899
Chris Metcalf621b1952012-04-01 14:04:21 -0400900/*
901 * Initialize hugepage support on this cpu. We do this on all cores
902 * early in boot: before argument parsing for the boot cpu, and after
903 * argument parsing but before the init functions run on the secondaries.
904 * So the values we set up here in the hypervisor may be overridden on
905 * the boot cpu as arguments are parsed.
906 */
907static __cpuinit void init_super_pages(void)
908{
909#ifdef CONFIG_HUGETLB_SUPER_PAGES
910 int i;
911 for (i = 0; i < HUGE_SHIFT_ENTRIES; ++i)
912 hv_set_pte_super_shift(i, huge_shift[i]);
913#endif
914}
915
Chris Metcalf867e3592010-05-28 23:09:12 -0400916/**
Chris Metcalf0707ad32010-06-25 17:04:17 -0400917 * setup_cpu() - Do all necessary per-cpu, tile-specific initialization.
918 * @boot: Is this the boot cpu?
Chris Metcalf867e3592010-05-28 23:09:12 -0400919 *
Chris Metcalf0707ad32010-06-25 17:04:17 -0400920 * Called from setup_arch() on the boot cpu, or online_secondary().
Chris Metcalf867e3592010-05-28 23:09:12 -0400921 */
Chris Metcalf0707ad32010-06-25 17:04:17 -0400922void __cpuinit setup_cpu(int boot)
Chris Metcalf867e3592010-05-28 23:09:12 -0400923{
Chris Metcalf0707ad32010-06-25 17:04:17 -0400924 /* The boot cpu sets up its permanent mappings much earlier. */
925 if (!boot)
926 store_permanent_mappings();
927
Chris Metcalf867e3592010-05-28 23:09:12 -0400928 /* Allow asynchronous TLB interrupts. */
929#if CHIP_HAS_TILE_DMA()
Chris Metcalf5d966112010-11-01 15:24:29 -0400930 arch_local_irq_unmask(INT_DMATLB_MISS);
931 arch_local_irq_unmask(INT_DMATLB_ACCESS);
Chris Metcalf867e3592010-05-28 23:09:12 -0400932#endif
933#if CHIP_HAS_SN_PROC()
Chris Metcalf5d966112010-11-01 15:24:29 -0400934 arch_local_irq_unmask(INT_SNITLB_MISS);
Chris Metcalf867e3592010-05-28 23:09:12 -0400935#endif
Chris Metcalfa78c9422010-10-14 16:23:03 -0400936#ifdef __tilegx__
Chris Metcalf5d966112010-11-01 15:24:29 -0400937 arch_local_irq_unmask(INT_SINGLE_STEP_K);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400938#endif
Chris Metcalf867e3592010-05-28 23:09:12 -0400939
940 /*
941 * Allow user access to many generic SPRs, like the cycle
942 * counter, PASS/FAIL/DONE, INTERRUPT_CRITICAL_SECTION, etc.
943 */
944 __insn_mtspr(SPR_MPL_WORLD_ACCESS_SET_0, 1);
945
946#if CHIP_HAS_SN()
947 /* Static network is not restricted. */
948 __insn_mtspr(SPR_MPL_SN_ACCESS_SET_0, 1);
949#endif
950#if CHIP_HAS_SN_PROC()
951 __insn_mtspr(SPR_MPL_SN_NOTIFY_SET_0, 1);
952 __insn_mtspr(SPR_MPL_SN_CPL_SET_0, 1);
953#endif
954
955 /*
Chris Metcalfa78c9422010-10-14 16:23:03 -0400956 * Set the MPL for interrupt control 0 & 1 to the corresponding
957 * values. This includes access to the SYSTEM_SAVE and EX_CONTEXT
958 * SPRs, as well as the interrupt mask.
Chris Metcalf867e3592010-05-28 23:09:12 -0400959 */
960 __insn_mtspr(SPR_MPL_INTCTRL_0_SET_0, 1);
Chris Metcalfa78c9422010-10-14 16:23:03 -0400961 __insn_mtspr(SPR_MPL_INTCTRL_1_SET_1, 1);
Chris Metcalf0707ad32010-06-25 17:04:17 -0400962
963 /* Initialize IRQ support for this cpu. */
964 setup_irq_regs();
965
966#ifdef CONFIG_HARDWALL
967 /* Reset the network state on this cpu. */
968 reset_network_state();
969#endif
Chris Metcalf621b1952012-04-01 14:04:21 -0400970
971 init_super_pages();
Chris Metcalf867e3592010-05-28 23:09:12 -0400972}
973
Chris Metcalf43d9ebb2011-05-02 15:51:32 -0400974#ifdef CONFIG_BLK_DEV_INITRD
975
Chris Metcalf51bcdf82012-03-29 15:29:28 -0400976/*
977 * Note that the kernel can potentially support other compression
978 * techniques than gz, though we don't do so by default. If we ever
979 * decide to do so we can either look for other filename extensions,
980 * or just allow a file with this name to be compressed with an
981 * arbitrary compressor (somewhat counterintuitively).
982 */
Chris Metcalf867e3592010-05-28 23:09:12 -0400983static int __initdata set_initramfs_file;
984static char __initdata initramfs_file[128] = "initramfs.cpio.gz";
985
986static int __init setup_initramfs_file(char *str)
987{
988 if (str == NULL)
989 return -EINVAL;
990 strncpy(initramfs_file, str, sizeof(initramfs_file) - 1);
991 set_initramfs_file = 1;
992
993 return 0;
994}
995early_param("initramfs_file", setup_initramfs_file);
996
997/*
Chris Metcalf51bcdf82012-03-29 15:29:28 -0400998 * We look for an "initramfs.cpio.gz" file in the hvfs.
Chris Metcalf867e3592010-05-28 23:09:12 -0400999 * If there is one, we allocate some memory for it and it will be
Chris Metcalf51bcdf82012-03-29 15:29:28 -04001000 * unpacked to the initramfs.
Chris Metcalf867e3592010-05-28 23:09:12 -04001001 */
1002static void __init load_hv_initrd(void)
1003{
1004 HV_FS_StatInfo stat;
1005 int fd, rc;
1006 void *initrd;
1007
1008 fd = hv_fs_findfile((HV_VirtAddr) initramfs_file);
1009 if (fd == HV_ENOENT) {
1010 if (set_initramfs_file)
Chris Metcalf0707ad32010-06-25 17:04:17 -04001011 pr_warning("No such hvfs initramfs file '%s'\n",
1012 initramfs_file);
Chris Metcalf867e3592010-05-28 23:09:12 -04001013 return;
1014 }
1015 BUG_ON(fd < 0);
1016 stat = hv_fs_fstat(fd);
1017 BUG_ON(stat.size < 0);
1018 if (stat.flags & HV_FS_ISDIR) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001019 pr_warning("Ignoring hvfs file '%s': it's a directory.\n",
1020 initramfs_file);
Chris Metcalf867e3592010-05-28 23:09:12 -04001021 return;
1022 }
1023 initrd = alloc_bootmem_pages(stat.size);
1024 rc = hv_fs_pread(fd, (HV_VirtAddr) initrd, stat.size, 0);
1025 if (rc != stat.size) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001026 pr_err("Error reading %d bytes from hvfs file '%s': %d\n",
Chris Metcalf867e3592010-05-28 23:09:12 -04001027 stat.size, initramfs_file, rc);
Chris Metcalfbc63de72010-08-13 08:23:07 -04001028 free_initrd_mem((unsigned long) initrd, stat.size);
Chris Metcalf867e3592010-05-28 23:09:12 -04001029 return;
1030 }
1031 initrd_start = (unsigned long) initrd;
1032 initrd_end = initrd_start + stat.size;
1033}
1034
1035void __init free_initrd_mem(unsigned long begin, unsigned long end)
1036{
Chris Metcalfbc63de72010-08-13 08:23:07 -04001037 free_bootmem(__pa(begin), end - begin);
Chris Metcalf867e3592010-05-28 23:09:12 -04001038}
1039
Chris Metcalf43d9ebb2011-05-02 15:51:32 -04001040#else
1041static inline void load_hv_initrd(void) {}
1042#endif /* CONFIG_BLK_DEV_INITRD */
1043
Chris Metcalf867e3592010-05-28 23:09:12 -04001044static void __init validate_hv(void)
1045{
1046 /*
1047 * It may already be too late, but let's check our built-in
1048 * configuration against what the hypervisor is providing.
1049 */
1050 unsigned long glue_size = hv_sysconf(HV_SYSCONF_GLUE_SIZE);
1051 int hv_page_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_SMALL);
1052 int hv_hpage_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_LARGE);
1053 HV_ASIDRange asid_range;
1054
1055#ifndef CONFIG_SMP
1056 HV_Topology topology = hv_inquire_topology();
1057 BUG_ON(topology.coord.x != 0 || topology.coord.y != 0);
1058 if (topology.width != 1 || topology.height != 1) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001059 pr_warning("Warning: booting UP kernel on %dx%d grid;"
1060 " will ignore all but first tile.\n",
1061 topology.width, topology.height);
Chris Metcalf867e3592010-05-28 23:09:12 -04001062 }
1063#endif
1064
1065 if (PAGE_OFFSET + HV_GLUE_START_CPA + glue_size > (unsigned long)_text)
1066 early_panic("Hypervisor glue size %ld is too big!\n",
1067 glue_size);
1068 if (hv_page_size != PAGE_SIZE)
1069 early_panic("Hypervisor page size %#x != our %#lx\n",
1070 hv_page_size, PAGE_SIZE);
1071 if (hv_hpage_size != HPAGE_SIZE)
1072 early_panic("Hypervisor huge page size %#x != our %#lx\n",
1073 hv_hpage_size, HPAGE_SIZE);
1074
1075#ifdef CONFIG_SMP
1076 /*
1077 * Some hypervisor APIs take a pointer to a bitmap array
1078 * whose size is at least the number of cpus on the chip.
1079 * We use a struct cpumask for this, so it must be big enough.
1080 */
1081 if ((smp_height * smp_width) > nr_cpu_ids)
1082 early_panic("Hypervisor %d x %d grid too big for Linux"
1083 " NR_CPUS %d\n", smp_height, smp_width,
1084 nr_cpu_ids);
1085#endif
1086
1087 /*
1088 * Check that we're using allowed ASIDs, and initialize the
1089 * various asid variables to their appropriate initial states.
1090 */
1091 asid_range = hv_inquire_asid(0);
1092 __get_cpu_var(current_asid) = min_asid = asid_range.start;
1093 max_asid = asid_range.start + asid_range.size - 1;
1094
1095 if (hv_confstr(HV_CONFSTR_CHIP_MODEL, (HV_VirtAddr)chip_model,
1096 sizeof(chip_model)) < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001097 pr_err("Warning: HV_CONFSTR_CHIP_MODEL not available\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001098 strlcpy(chip_model, "unknown", sizeof(chip_model));
1099 }
1100}
1101
1102static void __init validate_va(void)
1103{
1104#ifndef __tilegx__ /* FIXME: GX: probably some validation relevant here */
1105 /*
1106 * Similarly, make sure we're only using allowed VAs.
1107 * We assume we can contiguously use MEM_USER_INTRPT .. MEM_HV_INTRPT,
1108 * and 0 .. KERNEL_HIGH_VADDR.
1109 * In addition, make sure we CAN'T use the end of memory, since
1110 * we use the last chunk of each pgd for the pgd_list.
1111 */
Chris Metcalfa78c9422010-10-14 16:23:03 -04001112 int i, user_kernel_ok = 0;
Chris Metcalf867e3592010-05-28 23:09:12 -04001113 unsigned long max_va = 0;
1114 unsigned long list_va =
1115 ((PGD_LIST_OFFSET / sizeof(pgd_t)) << PGDIR_SHIFT);
1116
1117 for (i = 0; ; ++i) {
1118 HV_VirtAddrRange range = hv_inquire_virtual(i);
1119 if (range.size == 0)
1120 break;
1121 if (range.start <= MEM_USER_INTRPT &&
1122 range.start + range.size >= MEM_HV_INTRPT)
Chris Metcalfa78c9422010-10-14 16:23:03 -04001123 user_kernel_ok = 1;
Chris Metcalf867e3592010-05-28 23:09:12 -04001124 if (range.start == 0)
1125 max_va = range.size;
1126 BUG_ON(range.start + range.size > list_va);
1127 }
Chris Metcalfa78c9422010-10-14 16:23:03 -04001128 if (!user_kernel_ok)
1129 early_panic("Hypervisor not configured for user/kernel VAs\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001130 if (max_va == 0)
1131 early_panic("Hypervisor not configured for low VAs\n");
1132 if (max_va < KERNEL_HIGH_VADDR)
1133 early_panic("Hypervisor max VA %#lx smaller than %#lx\n",
1134 max_va, KERNEL_HIGH_VADDR);
1135
1136 /* Kernel PCs must have their high bit set; see intvec.S. */
1137 if ((long)VMALLOC_START >= 0)
1138 early_panic(
1139 "Linux VMALLOC region below the 2GB line (%#lx)!\n"
1140 "Reconfigure the kernel with fewer NR_HUGE_VMAPS\n"
1141 "or smaller VMALLOC_RESERVE.\n",
1142 VMALLOC_START);
1143#endif
1144}
1145
1146/*
1147 * cpu_lotar_map lists all the cpus that are valid for the supervisor
1148 * to cache data on at a page level, i.e. what cpus can be placed in
1149 * the LOTAR field of a PTE. It is equivalent to the set of possible
1150 * cpus plus any other cpus that are willing to share their cache.
1151 * It is set by hv_inquire_tiles(HV_INQ_TILES_LOTAR).
1152 */
1153struct cpumask __write_once cpu_lotar_map;
1154EXPORT_SYMBOL(cpu_lotar_map);
1155
1156#if CHIP_HAS_CBOX_HOME_MAP()
1157/*
1158 * hash_for_home_map lists all the tiles that hash-for-home data
1159 * will be cached on. Note that this may includes tiles that are not
1160 * valid for this supervisor to use otherwise (e.g. if a hypervisor
1161 * device is being shared between multiple supervisors).
1162 * It is set by hv_inquire_tiles(HV_INQ_TILES_HFH_CACHE).
1163 */
1164struct cpumask hash_for_home_map;
1165EXPORT_SYMBOL(hash_for_home_map);
1166#endif
1167
1168/*
1169 * cpu_cacheable_map lists all the cpus whose caches the hypervisor can
Rusty Russell5f054e32012-03-29 15:38:31 +10301170 * flush on our behalf. It is set to cpu_possible_mask OR'ed with
Chris Metcalf867e3592010-05-28 23:09:12 -04001171 * hash_for_home_map, and it is what should be passed to
1172 * hv_flush_remote() to flush all caches. Note that if there are
1173 * dedicated hypervisor driver tiles that have authorized use of their
1174 * cache, those tiles will only appear in cpu_lotar_map, NOT in
1175 * cpu_cacheable_map, as they are a special case.
1176 */
1177struct cpumask __write_once cpu_cacheable_map;
1178EXPORT_SYMBOL(cpu_cacheable_map);
1179
1180static __initdata struct cpumask disabled_map;
1181
1182static int __init disabled_cpus(char *str)
1183{
1184 int boot_cpu = smp_processor_id();
1185
1186 if (str == NULL || cpulist_parse_crop(str, &disabled_map) != 0)
1187 return -EINVAL;
1188 if (cpumask_test_cpu(boot_cpu, &disabled_map)) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001189 pr_err("disabled_cpus: can't disable boot cpu %d\n", boot_cpu);
Chris Metcalf867e3592010-05-28 23:09:12 -04001190 cpumask_clear_cpu(boot_cpu, &disabled_map);
1191 }
1192 return 0;
1193}
1194
1195early_param("disabled_cpus", disabled_cpus);
1196
Chris Metcalf0707ad32010-06-25 17:04:17 -04001197void __init print_disabled_cpus(void)
Chris Metcalf867e3592010-05-28 23:09:12 -04001198{
1199 if (!cpumask_empty(&disabled_map)) {
1200 char buf[100];
1201 cpulist_scnprintf(buf, sizeof(buf), &disabled_map);
Chris Metcalf0707ad32010-06-25 17:04:17 -04001202 pr_info("CPUs not available for Linux: %s\n", buf);
Chris Metcalf867e3592010-05-28 23:09:12 -04001203 }
1204}
1205
1206static void __init setup_cpu_maps(void)
1207{
1208 struct cpumask hv_disabled_map, cpu_possible_init;
1209 int boot_cpu = smp_processor_id();
1210 int cpus, i, rc;
1211
1212 /* Learn which cpus are allowed by the hypervisor. */
1213 rc = hv_inquire_tiles(HV_INQ_TILES_AVAIL,
1214 (HV_VirtAddr) cpumask_bits(&cpu_possible_init),
1215 sizeof(cpu_cacheable_map));
1216 if (rc < 0)
1217 early_panic("hv_inquire_tiles(AVAIL) failed: rc %d\n", rc);
1218 if (!cpumask_test_cpu(boot_cpu, &cpu_possible_init))
1219 early_panic("Boot CPU %d disabled by hypervisor!\n", boot_cpu);
1220
1221 /* Compute the cpus disabled by the hvconfig file. */
1222 cpumask_complement(&hv_disabled_map, &cpu_possible_init);
1223
1224 /* Include them with the cpus disabled by "disabled_cpus". */
1225 cpumask_or(&disabled_map, &disabled_map, &hv_disabled_map);
1226
1227 /*
1228 * Disable every cpu after "setup_max_cpus". But don't mark
1229 * as disabled the cpus that are outside of our initial rectangle,
1230 * since that turns out to be confusing.
1231 */
1232 cpus = 1; /* this cpu */
1233 cpumask_set_cpu(boot_cpu, &disabled_map); /* ignore this cpu */
1234 for (i = 0; cpus < setup_max_cpus; ++i)
1235 if (!cpumask_test_cpu(i, &disabled_map))
1236 ++cpus;
1237 for (; i < smp_height * smp_width; ++i)
1238 cpumask_set_cpu(i, &disabled_map);
1239 cpumask_clear_cpu(boot_cpu, &disabled_map); /* reset this cpu */
1240 for (i = smp_height * smp_width; i < NR_CPUS; ++i)
1241 cpumask_clear_cpu(i, &disabled_map);
1242
1243 /*
1244 * Setup cpu_possible map as every cpu allocated to us, minus
1245 * the results of any "disabled_cpus" settings.
1246 */
1247 cpumask_andnot(&cpu_possible_init, &cpu_possible_init, &disabled_map);
1248 init_cpu_possible(&cpu_possible_init);
1249
1250 /* Learn which cpus are valid for LOTAR caching. */
1251 rc = hv_inquire_tiles(HV_INQ_TILES_LOTAR,
1252 (HV_VirtAddr) cpumask_bits(&cpu_lotar_map),
1253 sizeof(cpu_lotar_map));
1254 if (rc < 0) {
Chris Metcalf0707ad32010-06-25 17:04:17 -04001255 pr_err("warning: no HV_INQ_TILES_LOTAR; using AVAIL\n");
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301256 cpu_lotar_map = *cpu_possible_mask;
Chris Metcalf867e3592010-05-28 23:09:12 -04001257 }
1258
1259#if CHIP_HAS_CBOX_HOME_MAP()
1260 /* Retrieve set of CPUs used for hash-for-home caching */
1261 rc = hv_inquire_tiles(HV_INQ_TILES_HFH_CACHE,
1262 (HV_VirtAddr) hash_for_home_map.bits,
1263 sizeof(hash_for_home_map));
1264 if (rc < 0)
1265 early_panic("hv_inquire_tiles(HFH_CACHE) failed: rc %d\n", rc);
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301266 cpumask_or(&cpu_cacheable_map, cpu_possible_mask, &hash_for_home_map);
Chris Metcalf867e3592010-05-28 23:09:12 -04001267#else
Rusty Russell0b5f9c02012-03-29 15:38:30 +10301268 cpu_cacheable_map = *cpu_possible_mask;
Chris Metcalf867e3592010-05-28 23:09:12 -04001269#endif
1270}
1271
1272
1273static int __init dataplane(char *str)
1274{
Chris Metcalf0707ad32010-06-25 17:04:17 -04001275 pr_warning("WARNING: dataplane support disabled in this kernel\n");
Chris Metcalf867e3592010-05-28 23:09:12 -04001276 return 0;
1277}
1278
1279early_param("dataplane", dataplane);
1280
1281#ifdef CONFIG_CMDLINE_BOOL
1282static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
1283#endif
1284
1285void __init setup_arch(char **cmdline_p)
1286{
1287 int len;
1288
1289#if defined(CONFIG_CMDLINE_BOOL) && defined(CONFIG_CMDLINE_OVERRIDE)
1290 len = hv_get_command_line((HV_VirtAddr) boot_command_line,
1291 COMMAND_LINE_SIZE);
1292 if (boot_command_line[0])
Chris Metcalf0707ad32010-06-25 17:04:17 -04001293 pr_warning("WARNING: ignoring dynamic command line \"%s\"\n",
1294 boot_command_line);
Chris Metcalf867e3592010-05-28 23:09:12 -04001295 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
1296#else
1297 char *hv_cmdline;
1298#if defined(CONFIG_CMDLINE_BOOL)
1299 if (builtin_cmdline[0]) {
1300 int builtin_len = strlcpy(boot_command_line, builtin_cmdline,
1301 COMMAND_LINE_SIZE);
1302 if (builtin_len < COMMAND_LINE_SIZE-1)
1303 boot_command_line[builtin_len++] = ' ';
1304 hv_cmdline = &boot_command_line[builtin_len];
1305 len = COMMAND_LINE_SIZE - builtin_len;
1306 } else
1307#endif
1308 {
1309 hv_cmdline = boot_command_line;
1310 len = COMMAND_LINE_SIZE;
1311 }
1312 len = hv_get_command_line((HV_VirtAddr) hv_cmdline, len);
1313 if (len < 0 || len > COMMAND_LINE_SIZE)
1314 early_panic("hv_get_command_line failed: %d\n", len);
1315#endif
1316
1317 *cmdline_p = boot_command_line;
1318
1319 /* Set disabled_map and setup_max_cpus very early */
1320 parse_early_param();
1321
1322 /* Make sure the kernel is compatible with the hypervisor. */
1323 validate_hv();
1324 validate_va();
1325
1326 setup_cpu_maps();
1327
1328
1329#ifdef CONFIG_PCI
1330 /*
1331 * Initialize the PCI structures. This is done before memory
1332 * setup so that we know whether or not a pci_reserve region
1333 * is necessary.
1334 */
1335 if (tile_pci_init() == 0)
1336 pci_reserve_mb = 0;
1337
1338 /* PCI systems reserve a region just below 4GB for mapping iomem. */
1339 pci_reserve_end_pfn = (1 << (32 - PAGE_SHIFT));
1340 pci_reserve_start_pfn = pci_reserve_end_pfn -
1341 (pci_reserve_mb << (20 - PAGE_SHIFT));
1342#endif
1343
1344 init_mm.start_code = (unsigned long) _text;
1345 init_mm.end_code = (unsigned long) _etext;
1346 init_mm.end_data = (unsigned long) _edata;
1347 init_mm.brk = (unsigned long) _end;
1348
1349 setup_memory();
1350 store_permanent_mappings();
1351 setup_bootmem_allocator();
1352
1353 /*
1354 * NOTE: before this point _nobody_ is allowed to allocate
1355 * any memory using the bootmem allocator.
1356 */
1357
1358 paging_init();
1359 setup_numa_mapping();
1360 zone_sizes_init();
1361 set_page_homes();
Chris Metcalf0707ad32010-06-25 17:04:17 -04001362 setup_cpu(1);
Chris Metcalf867e3592010-05-28 23:09:12 -04001363 setup_clock();
1364 load_hv_initrd();
1365}
1366
1367
1368/*
1369 * Set up per-cpu memory.
1370 */
1371
1372unsigned long __per_cpu_offset[NR_CPUS] __write_once;
1373EXPORT_SYMBOL(__per_cpu_offset);
1374
1375static size_t __initdata pfn_offset[MAX_NUMNODES] = { 0 };
1376static unsigned long __initdata percpu_pfn[NR_CPUS] = { 0 };
1377
1378/*
1379 * As the percpu code allocates pages, we return the pages from the
1380 * end of the node for the specified cpu.
1381 */
1382static void *__init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align)
1383{
1384 int nid = cpu_to_node(cpu);
1385 unsigned long pfn = node_percpu_pfn[nid] + pfn_offset[nid];
1386
1387 BUG_ON(size % PAGE_SIZE != 0);
1388 pfn_offset[nid] += size / PAGE_SIZE;
Chris Metcalf76c567f2011-02-28 16:37:34 -05001389 BUG_ON(node_percpu[nid] < size);
1390 node_percpu[nid] -= size;
Chris Metcalf867e3592010-05-28 23:09:12 -04001391 if (percpu_pfn[cpu] == 0)
1392 percpu_pfn[cpu] = pfn;
1393 return pfn_to_kaddr(pfn);
1394}
1395
1396/*
1397 * Pages reserved for percpu memory are not freeable, and in any case we are
1398 * on a short path to panic() in setup_per_cpu_area() at this point anyway.
1399 */
1400static void __init pcpu_fc_free(void *ptr, size_t size)
1401{
1402}
1403
1404/*
1405 * Set up vmalloc page tables using bootmem for the percpu code.
1406 */
1407static void __init pcpu_fc_populate_pte(unsigned long addr)
1408{
1409 pgd_t *pgd;
1410 pud_t *pud;
1411 pmd_t *pmd;
1412 pte_t *pte;
1413
1414 BUG_ON(pgd_addr_invalid(addr));
Chris Metcalf77d23302010-10-14 14:47:35 -04001415 if (addr < VMALLOC_START || addr >= VMALLOC_END)
1416 panic("PCPU addr %#lx outside vmalloc range %#lx..%#lx;"
1417 " try increasing CONFIG_VMALLOC_RESERVE\n",
1418 addr, VMALLOC_START, VMALLOC_END);
Chris Metcalf867e3592010-05-28 23:09:12 -04001419
1420 pgd = swapper_pg_dir + pgd_index(addr);
1421 pud = pud_offset(pgd, addr);
1422 BUG_ON(!pud_present(*pud));
1423 pmd = pmd_offset(pud, addr);
1424 if (pmd_present(*pmd)) {
1425 BUG_ON(pmd_huge_page(*pmd));
1426 } else {
1427 pte = __alloc_bootmem(L2_KERNEL_PGTABLE_SIZE,
1428 HV_PAGE_TABLE_ALIGN, 0);
1429 pmd_populate_kernel(&init_mm, pmd, pte);
1430 }
1431}
1432
1433void __init setup_per_cpu_areas(void)
1434{
1435 struct page *pg;
1436 unsigned long delta, pfn, lowmem_va;
1437 unsigned long size = percpu_size();
1438 char *ptr;
1439 int rc, cpu, i;
1440
1441 rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, pcpu_fc_alloc,
1442 pcpu_fc_free, pcpu_fc_populate_pte);
1443 if (rc < 0)
1444 panic("Cannot initialize percpu area (err=%d)", rc);
1445
1446 delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
1447 for_each_possible_cpu(cpu) {
1448 __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu];
1449
1450 /* finv the copy out of cache so we can change homecache */
1451 ptr = pcpu_base_addr + pcpu_unit_offsets[cpu];
1452 __finv_buffer(ptr, size);
1453 pfn = percpu_pfn[cpu];
1454
1455 /* Rewrite the page tables to cache on that cpu */
1456 pg = pfn_to_page(pfn);
1457 for (i = 0; i < size; i += PAGE_SIZE, ++pfn, ++pg) {
1458
1459 /* Update the vmalloc mapping and page home. */
Chris Metcalfd5d14ed2012-03-29 13:58:43 -04001460 unsigned long addr = (unsigned long)ptr + i;
1461 pte_t *ptep = virt_to_pte(NULL, addr);
Chris Metcalf867e3592010-05-28 23:09:12 -04001462 pte_t pte = *ptep;
1463 BUG_ON(pfn != pte_pfn(pte));
1464 pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_TILE_L3);
1465 pte = set_remote_cache_cpu(pte, cpu);
Chris Metcalfd5d14ed2012-03-29 13:58:43 -04001466 set_pte_at(&init_mm, addr, ptep, pte);
Chris Metcalf867e3592010-05-28 23:09:12 -04001467
1468 /* Update the lowmem mapping for consistency. */
1469 lowmem_va = (unsigned long)pfn_to_kaddr(pfn);
1470 ptep = virt_to_pte(NULL, lowmem_va);
1471 if (pte_huge(*ptep)) {
1472 printk(KERN_DEBUG "early shatter of huge page"
1473 " at %#lx\n", lowmem_va);
1474 shatter_pmd((pmd_t *)ptep);
1475 ptep = virt_to_pte(NULL, lowmem_va);
1476 BUG_ON(pte_huge(*ptep));
1477 }
1478 BUG_ON(pfn != pte_pfn(*ptep));
Chris Metcalfd5d14ed2012-03-29 13:58:43 -04001479 set_pte_at(&init_mm, lowmem_va, ptep, pte);
Chris Metcalf867e3592010-05-28 23:09:12 -04001480 }
1481 }
1482
1483 /* Set our thread pointer appropriately. */
1484 set_my_cpu_offset(__per_cpu_offset[smp_processor_id()]);
1485
1486 /* Make sure the finv's have completed. */
1487 mb_incoherent();
1488
1489 /* Flush the TLB so we reference it properly from here on out. */
1490 local_flush_tlb_all();
1491}
1492
1493static struct resource data_resource = {
1494 .name = "Kernel data",
1495 .start = 0,
1496 .end = 0,
1497 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
1498};
1499
1500static struct resource code_resource = {
1501 .name = "Kernel code",
1502 .start = 0,
1503 .end = 0,
1504 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
1505};
1506
1507/*
1508 * We reserve all resources above 4GB so that PCI won't try to put
1509 * mappings above 4GB; the standard allows that for some devices but
1510 * the probing code trunates values to 32 bits.
1511 */
1512#ifdef CONFIG_PCI
1513static struct resource* __init
1514insert_non_bus_resource(void)
1515{
1516 struct resource *res =
1517 kzalloc(sizeof(struct resource), GFP_ATOMIC);
1518 res->name = "Non-Bus Physical Address Space";
1519 res->start = (1ULL << 32);
1520 res->end = -1LL;
1521 res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1522 if (insert_resource(&iomem_resource, res)) {
1523 kfree(res);
1524 return NULL;
1525 }
1526 return res;
1527}
1528#endif
1529
1530static struct resource* __init
1531insert_ram_resource(u64 start_pfn, u64 end_pfn)
1532{
1533 struct resource *res =
1534 kzalloc(sizeof(struct resource), GFP_ATOMIC);
1535 res->name = "System RAM";
1536 res->start = start_pfn << PAGE_SHIFT;
1537 res->end = (end_pfn << PAGE_SHIFT) - 1;
1538 res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
1539 if (insert_resource(&iomem_resource, res)) {
1540 kfree(res);
1541 return NULL;
1542 }
1543 return res;
1544}
1545
1546/*
1547 * Request address space for all standard resources
1548 *
1549 * If the system includes PCI root complex drivers, we need to create
1550 * a window just below 4GB where PCI BARs can be mapped.
1551 */
1552static int __init request_standard_resources(void)
1553{
1554 int i;
1555 enum { CODE_DELTA = MEM_SV_INTRPT - PAGE_OFFSET };
1556
1557 iomem_resource.end = -1LL;
1558#ifdef CONFIG_PCI
1559 insert_non_bus_resource();
1560#endif
1561
1562 for_each_online_node(i) {
1563 u64 start_pfn = node_start_pfn[i];
1564 u64 end_pfn = node_end_pfn[i];
1565
1566#ifdef CONFIG_PCI
1567 if (start_pfn <= pci_reserve_start_pfn &&
1568 end_pfn > pci_reserve_start_pfn) {
1569 if (end_pfn > pci_reserve_end_pfn)
1570 insert_ram_resource(pci_reserve_end_pfn,
1571 end_pfn);
1572 end_pfn = pci_reserve_start_pfn;
1573 }
1574#endif
1575 insert_ram_resource(start_pfn, end_pfn);
1576 }
1577
1578 code_resource.start = __pa(_text - CODE_DELTA);
1579 code_resource.end = __pa(_etext - CODE_DELTA)-1;
1580 data_resource.start = __pa(_sdata);
1581 data_resource.end = __pa(_end)-1;
1582
1583 insert_resource(&iomem_resource, &code_resource);
1584 insert_resource(&iomem_resource, &data_resource);
1585
1586#ifdef CONFIG_KEXEC
1587 insert_resource(&iomem_resource, &crashk_res);
1588#endif
1589
1590 return 0;
1591}
1592
1593subsys_initcall(request_standard_resources);