Commit | Line | Data |
---|---|---|
867e359b CM |
1 | /* |
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> | |
0707ad30 | 23 | #include <linux/irq.h> |
867e359b CM |
24 | #include <linux/kexec.h> |
25 | #include <linux/pci.h> | |
41bb38fc | 26 | #include <linux/swiotlb.h> |
867e359b CM |
27 | #include <linux/initrd.h> |
28 | #include <linux/io.h> | |
29 | #include <linux/highmem.h> | |
30 | #include <linux/smp.h> | |
31 | #include <linux/timex.h> | |
621b1955 | 32 | #include <linux/hugetlb.h> |
2ded5c24 | 33 | #include <linux/start_kernel.h> |
a05d3f9f | 34 | #include <linux/screen_info.h> |
867e359b CM |
35 | #include <asm/setup.h> |
36 | #include <asm/sections.h> | |
867e359b CM |
37 | #include <asm/cacheflush.h> |
38 | #include <asm/pgalloc.h> | |
39 | #include <asm/mmu_context.h> | |
40 | #include <hv/hypervisor.h> | |
41 | #include <arch/interrupts.h> | |
42 | ||
43 | /* <linux/smp.h> doesn't provide this definition. */ | |
44 | #ifndef CONFIG_SMP | |
45 | #define setup_max_cpus 1 | |
46 | #endif | |
47 | ||
48 | static inline int ABS(int x) { return x >= 0 ? x : -x; } | |
49 | ||
50 | /* Chip information */ | |
51 | char chip_model[64] __write_once; | |
52 | ||
a05d3f9f CM |
53 | #ifdef CONFIG_VT |
54 | struct screen_info screen_info; | |
55 | #endif | |
56 | ||
867e359b CM |
57 | struct pglist_data node_data[MAX_NUMNODES] __read_mostly; |
58 | EXPORT_SYMBOL(node_data); | |
59 | ||
867e359b | 60 | /* Information on the NUMA nodes that we compute early */ |
18f894c1 PG |
61 | unsigned long node_start_pfn[MAX_NUMNODES]; |
62 | unsigned long node_end_pfn[MAX_NUMNODES]; | |
867e359b CM |
63 | unsigned long __initdata node_memmap_pfn[MAX_NUMNODES]; |
64 | unsigned long __initdata node_percpu_pfn[MAX_NUMNODES]; | |
65 | unsigned long __initdata node_free_pfn[MAX_NUMNODES]; | |
66 | ||
76c567fb CM |
67 | static unsigned long __initdata node_percpu[MAX_NUMNODES]; |
68 | ||
293ef7b8 TG |
69 | /* |
70 | * per-CPU stack and boot info. | |
71 | */ | |
72 | DEFINE_PER_CPU(unsigned long, boot_sp) = | |
73 | (unsigned long)init_stack + THREAD_SIZE; | |
74 | ||
75 | #ifdef CONFIG_SMP | |
76 | DEFINE_PER_CPU(unsigned long, boot_pc) = (unsigned long)start_kernel; | |
77 | #else | |
78 | /* | |
79 | * The variable must be __initdata since it references __init code. | |
80 | * With CONFIG_SMP it is per-cpu data, which is exempt from validation. | |
81 | */ | |
82 | unsigned long __initdata boot_pc = (unsigned long)start_kernel; | |
83 | #endif | |
84 | ||
867e359b CM |
85 | #ifdef CONFIG_HIGHMEM |
86 | /* Page frame index of end of lowmem on each controller. */ | |
18f894c1 | 87 | unsigned long node_lowmem_end_pfn[MAX_NUMNODES]; |
867e359b CM |
88 | |
89 | /* Number of pages that can be mapped into lowmem. */ | |
90 | static unsigned long __initdata mappable_physpages; | |
91 | #endif | |
92 | ||
93 | /* Data on which physical memory controller corresponds to which NUMA node */ | |
94 | int node_controller[MAX_NUMNODES] = { [0 ... MAX_NUMNODES-1] = -1 }; | |
95 | ||
96 | #ifdef CONFIG_HIGHMEM | |
97 | /* Map information from VAs to PAs */ | |
98 | unsigned long pbase_map[1 << (32 - HPAGE_SHIFT)] | |
99 | __write_once __attribute__((aligned(L2_CACHE_BYTES))); | |
100 | EXPORT_SYMBOL(pbase_map); | |
101 | ||
102 | /* Map information from PAs to VAs */ | |
103 | void *vbase_map[NR_PA_HIGHBIT_VALUES] | |
104 | __write_once __attribute__((aligned(L2_CACHE_BYTES))); | |
105 | EXPORT_SYMBOL(vbase_map); | |
106 | #endif | |
107 | ||
108 | /* Node number as a function of the high PA bits */ | |
109 | int highbits_to_node[NR_PA_HIGHBIT_VALUES] __write_once; | |
110 | EXPORT_SYMBOL(highbits_to_node); | |
111 | ||
112 | static unsigned int __initdata maxmem_pfn = -1U; | |
113 | static unsigned int __initdata maxnodemem_pfn[MAX_NUMNODES] = { | |
114 | [0 ... MAX_NUMNODES-1] = -1U | |
115 | }; | |
116 | static nodemask_t __initdata isolnodes; | |
117 | ||
41bb38fc | 118 | #if defined(CONFIG_PCI) && !defined(__tilegx__) |
867e359b CM |
119 | enum { DEFAULT_PCI_RESERVE_MB = 64 }; |
120 | static unsigned int __initdata pci_reserve_mb = DEFAULT_PCI_RESERVE_MB; | |
121 | unsigned long __initdata pci_reserve_start_pfn = -1U; | |
122 | unsigned long __initdata pci_reserve_end_pfn = -1U; | |
123 | #endif | |
124 | ||
125 | static int __init setup_maxmem(char *str) | |
126 | { | |
bfffe79b CM |
127 | unsigned long long maxmem; |
128 | if (str == NULL || (maxmem = memparse(str, NULL)) == 0) | |
867e359b CM |
129 | return -EINVAL; |
130 | ||
bfffe79b | 131 | maxmem_pfn = (maxmem >> HPAGE_SHIFT) << (HPAGE_SHIFT - PAGE_SHIFT); |
0707ad30 | 132 | pr_info("Forcing RAM used to no more than %dMB\n", |
f4743673 | 133 | maxmem_pfn >> (20 - PAGE_SHIFT)); |
867e359b CM |
134 | return 0; |
135 | } | |
136 | early_param("maxmem", setup_maxmem); | |
137 | ||
138 | static int __init setup_maxnodemem(char *str) | |
139 | { | |
140 | char *endp; | |
bfffe79b CM |
141 | unsigned long long maxnodemem; |
142 | long node; | |
867e359b CM |
143 | |
144 | node = str ? simple_strtoul(str, &endp, 0) : INT_MAX; | |
bfffe79b | 145 | if (node >= MAX_NUMNODES || *endp != ':') |
867e359b CM |
146 | return -EINVAL; |
147 | ||
bfffe79b CM |
148 | maxnodemem = memparse(endp+1, NULL); |
149 | maxnodemem_pfn[node] = (maxnodemem >> HPAGE_SHIFT) << | |
867e359b | 150 | (HPAGE_SHIFT - PAGE_SHIFT); |
0707ad30 | 151 | pr_info("Forcing RAM used on node %ld to no more than %dMB\n", |
f4743673 | 152 | node, maxnodemem_pfn[node] >> (20 - PAGE_SHIFT)); |
867e359b CM |
153 | return 0; |
154 | } | |
155 | early_param("maxnodemem", setup_maxnodemem); | |
156 | ||
77f8c740 CM |
157 | struct memmap_entry { |
158 | u64 addr; /* start of memory segment */ | |
159 | u64 size; /* size of memory segment */ | |
160 | }; | |
161 | static struct memmap_entry memmap_map[64]; | |
162 | static int memmap_nr; | |
163 | ||
164 | static void add_memmap_region(u64 addr, u64 size) | |
165 | { | |
166 | if (memmap_nr >= ARRAY_SIZE(memmap_map)) { | |
167 | pr_err("Ooops! Too many entries in the memory map!\n"); | |
168 | return; | |
169 | } | |
170 | memmap_map[memmap_nr].addr = addr; | |
171 | memmap_map[memmap_nr].size = size; | |
172 | memmap_nr++; | |
173 | } | |
174 | ||
175 | static int __init setup_memmap(char *p) | |
176 | { | |
177 | char *oldp; | |
178 | u64 start_at, mem_size; | |
179 | ||
180 | if (!p) | |
181 | return -EINVAL; | |
182 | ||
183 | if (!strncmp(p, "exactmap", 8)) { | |
184 | pr_err("\"memmap=exactmap\" not valid on tile\n"); | |
185 | return 0; | |
186 | } | |
187 | ||
188 | oldp = p; | |
189 | mem_size = memparse(p, &p); | |
190 | if (p == oldp) | |
191 | return -EINVAL; | |
192 | ||
193 | if (*p == '@') { | |
194 | pr_err("\"memmap=nn@ss\" (force RAM) invalid on tile\n"); | |
195 | } else if (*p == '#') { | |
196 | pr_err("\"memmap=nn#ss\" (force ACPI data) invalid on tile\n"); | |
197 | } else if (*p == '$') { | |
198 | start_at = memparse(p+1, &p); | |
199 | add_memmap_region(start_at, mem_size); | |
200 | } else { | |
201 | if (mem_size == 0) | |
202 | return -EINVAL; | |
203 | maxmem_pfn = (mem_size >> HPAGE_SHIFT) << | |
204 | (HPAGE_SHIFT - PAGE_SHIFT); | |
205 | } | |
206 | return *p == '\0' ? 0 : -EINVAL; | |
207 | } | |
208 | early_param("memmap", setup_memmap); | |
209 | ||
210 | static int __init setup_mem(char *str) | |
211 | { | |
212 | return setup_maxmem(str); | |
213 | } | |
214 | early_param("mem", setup_mem); /* compatibility with x86 */ | |
215 | ||
867e359b CM |
216 | static int __init setup_isolnodes(char *str) |
217 | { | |
218 | char buf[MAX_NUMNODES * 5]; | |
219 | if (str == NULL || nodelist_parse(str, isolnodes) != 0) | |
220 | return -EINVAL; | |
221 | ||
222 | nodelist_scnprintf(buf, sizeof(buf), isolnodes); | |
0707ad30 | 223 | pr_info("Set isolnodes value to '%s'\n", buf); |
867e359b CM |
224 | return 0; |
225 | } | |
226 | early_param("isolnodes", setup_isolnodes); | |
227 | ||
41bb38fc | 228 | #if defined(CONFIG_PCI) && !defined(__tilegx__) |
867e359b CM |
229 | static int __init setup_pci_reserve(char* str) |
230 | { | |
b2dfa048 DW |
231 | if (str == NULL || kstrtouint(str, 0, &pci_reserve_mb) != 0 || |
232 | pci_reserve_mb > 3 * 1024) | |
867e359b CM |
233 | return -EINVAL; |
234 | ||
0707ad30 | 235 | pr_info("Reserving %dMB for PCIE root complex mappings\n", |
41bb38fc | 236 | pci_reserve_mb); |
867e359b CM |
237 | return 0; |
238 | } | |
239 | early_param("pci_reserve", setup_pci_reserve); | |
240 | #endif | |
241 | ||
242 | #ifndef __tilegx__ | |
243 | /* | |
244 | * vmalloc=size forces the vmalloc area to be exactly 'size' bytes. | |
245 | * This can be used to increase (or decrease) the vmalloc area. | |
246 | */ | |
247 | static int __init parse_vmalloc(char *arg) | |
248 | { | |
249 | if (!arg) | |
250 | return -EINVAL; | |
251 | ||
252 | VMALLOC_RESERVE = (memparse(arg, &arg) + PGDIR_SIZE - 1) & PGDIR_MASK; | |
253 | ||
254 | /* See validate_va() for more on this test. */ | |
255 | if ((long)_VMALLOC_START >= 0) | |
256 | early_panic("\"vmalloc=%#lx\" value too large: maximum %#lx\n", | |
257 | VMALLOC_RESERVE, _VMALLOC_END - 0x80000000UL); | |
258 | ||
259 | return 0; | |
260 | } | |
261 | early_param("vmalloc", parse_vmalloc); | |
262 | #endif | |
263 | ||
264 | #ifdef CONFIG_HIGHMEM | |
265 | /* | |
a78c942d CM |
266 | * Determine for each controller where its lowmem is mapped and how much of |
267 | * it is mapped there. On controller zero, the first few megabytes are | |
acbde1db | 268 | * already mapped in as code at MEM_SV_START, so in principle we could |
a78c942d CM |
269 | * start our data mappings higher up, but for now we don't bother, to avoid |
270 | * additional confusion. | |
867e359b CM |
271 | * |
272 | * One question is whether, on systems with more than 768 Mb and | |
273 | * controllers of different sizes, to map in a proportionate amount of | |
274 | * each one, or to try to map the same amount from each controller. | |
275 | * (E.g. if we have three controllers with 256MB, 1GB, and 256MB | |
276 | * respectively, do we map 256MB from each, or do we map 128 MB, 512 | |
277 | * MB, and 128 MB respectively?) For now we use a proportionate | |
278 | * solution like the latter. | |
279 | * | |
280 | * The VA/PA mapping demands that we align our decisions at 16 MB | |
281 | * boundaries so that we can rapidly convert VA to PA. | |
282 | */ | |
283 | static void *__init setup_pa_va_mapping(void) | |
284 | { | |
285 | unsigned long curr_pages = 0; | |
286 | unsigned long vaddr = PAGE_OFFSET; | |
287 | nodemask_t highonlynodes = isolnodes; | |
288 | int i, j; | |
289 | ||
290 | memset(pbase_map, -1, sizeof(pbase_map)); | |
291 | memset(vbase_map, -1, sizeof(vbase_map)); | |
292 | ||
293 | /* Node zero cannot be isolated for LOWMEM purposes. */ | |
294 | node_clear(0, highonlynodes); | |
295 | ||
296 | /* Count up the number of pages on non-highonlynodes controllers. */ | |
297 | mappable_physpages = 0; | |
298 | for_each_online_node(i) { | |
299 | if (!node_isset(i, highonlynodes)) | |
300 | mappable_physpages += | |
301 | node_end_pfn[i] - node_start_pfn[i]; | |
302 | } | |
303 | ||
304 | for_each_online_node(i) { | |
305 | unsigned long start = node_start_pfn[i]; | |
306 | unsigned long end = node_end_pfn[i]; | |
307 | unsigned long size = end - start; | |
308 | unsigned long vaddr_end; | |
309 | ||
310 | if (node_isset(i, highonlynodes)) { | |
311 | /* Mark this controller as having no lowmem. */ | |
312 | node_lowmem_end_pfn[i] = start; | |
313 | continue; | |
314 | } | |
315 | ||
316 | curr_pages += size; | |
317 | if (mappable_physpages > MAXMEM_PFN) { | |
318 | vaddr_end = PAGE_OFFSET + | |
319 | (((u64)curr_pages * MAXMEM_PFN / | |
320 | mappable_physpages) | |
321 | << PAGE_SHIFT); | |
322 | } else { | |
323 | vaddr_end = PAGE_OFFSET + (curr_pages << PAGE_SHIFT); | |
324 | } | |
325 | for (j = 0; vaddr < vaddr_end; vaddr += HPAGE_SIZE, ++j) { | |
326 | unsigned long this_pfn = | |
327 | start + (j << HUGETLB_PAGE_ORDER); | |
328 | pbase_map[vaddr >> HPAGE_SHIFT] = this_pfn; | |
329 | if (vbase_map[__pfn_to_highbits(this_pfn)] == | |
330 | (void *)-1) | |
331 | vbase_map[__pfn_to_highbits(this_pfn)] = | |
332 | (void *)(vaddr & HPAGE_MASK); | |
333 | } | |
334 | node_lowmem_end_pfn[i] = start + (j << HUGETLB_PAGE_ORDER); | |
335 | BUG_ON(node_lowmem_end_pfn[i] > end); | |
336 | } | |
337 | ||
338 | /* Return highest address of any mapped memory. */ | |
339 | return (void *)vaddr; | |
340 | } | |
341 | #endif /* CONFIG_HIGHMEM */ | |
342 | ||
343 | /* | |
344 | * Register our most important memory mappings with the debug stub. | |
345 | * | |
346 | * This is up to 4 mappings for lowmem, one mapping per memory | |
347 | * controller, plus one for our text segment. | |
348 | */ | |
18f894c1 | 349 | static void store_permanent_mappings(void) |
867e359b CM |
350 | { |
351 | int i; | |
352 | ||
353 | for_each_online_node(i) { | |
354 | HV_PhysAddr pa = ((HV_PhysAddr)node_start_pfn[i]) << PAGE_SHIFT; | |
355 | #ifdef CONFIG_HIGHMEM | |
356 | HV_PhysAddr high_mapped_pa = node_lowmem_end_pfn[i]; | |
357 | #else | |
358 | HV_PhysAddr high_mapped_pa = node_end_pfn[i]; | |
359 | #endif | |
360 | ||
361 | unsigned long pages = high_mapped_pa - node_start_pfn[i]; | |
362 | HV_VirtAddr addr = (HV_VirtAddr) __va(pa); | |
363 | hv_store_mapping(addr, pages << PAGE_SHIFT, pa); | |
364 | } | |
365 | ||
40a3b8df JL |
366 | hv_store_mapping((HV_VirtAddr)_text, |
367 | (uint32_t)(_einittext - _text), 0); | |
867e359b CM |
368 | } |
369 | ||
370 | /* | |
371 | * Use hv_inquire_physical() to populate node_{start,end}_pfn[] | |
372 | * and node_online_map, doing suitable sanity-checking. | |
373 | * Also set min_low_pfn, max_low_pfn, and max_pfn. | |
374 | */ | |
375 | static void __init setup_memory(void) | |
376 | { | |
377 | int i, j; | |
378 | int highbits_seen[NR_PA_HIGHBIT_VALUES] = { 0 }; | |
379 | #ifdef CONFIG_HIGHMEM | |
380 | long highmem_pages; | |
381 | #endif | |
382 | #ifndef __tilegx__ | |
383 | int cap; | |
384 | #endif | |
385 | #if defined(CONFIG_HIGHMEM) || defined(__tilegx__) | |
386 | long lowmem_pages; | |
387 | #endif | |
3f29c331 | 388 | unsigned long physpages = 0; |
867e359b CM |
389 | |
390 | /* We are using a char to hold the cpu_2_node[] mapping */ | |
e18105c1 | 391 | BUILD_BUG_ON(MAX_NUMNODES > 127); |
867e359b CM |
392 | |
393 | /* Discover the ranges of memory available to us */ | |
394 | for (i = 0; ; ++i) { | |
395 | unsigned long start, size, end, highbits; | |
396 | HV_PhysAddrRange range = hv_inquire_physical(i); | |
397 | if (range.size == 0) | |
398 | break; | |
399 | #ifdef CONFIG_FLATMEM | |
400 | if (i > 0) { | |
0707ad30 | 401 | pr_err("Can't use discontiguous PAs: %#llx..%#llx\n", |
867e359b CM |
402 | range.size, range.start + range.size); |
403 | continue; | |
404 | } | |
405 | #endif | |
406 | #ifndef __tilegx__ | |
407 | if ((unsigned long)range.start) { | |
0707ad30 | 408 | pr_err("Range not at 4GB multiple: %#llx..%#llx\n", |
867e359b CM |
409 | range.start, range.start + range.size); |
410 | continue; | |
411 | } | |
412 | #endif | |
413 | if ((range.start & (HPAGE_SIZE-1)) != 0 || | |
414 | (range.size & (HPAGE_SIZE-1)) != 0) { | |
415 | unsigned long long start_pa = range.start; | |
0707ad30 | 416 | unsigned long long orig_size = range.size; |
867e359b CM |
417 | range.start = (start_pa + HPAGE_SIZE - 1) & HPAGE_MASK; |
418 | range.size -= (range.start - start_pa); | |
419 | range.size &= HPAGE_MASK; | |
f4743673 | 420 | pr_err("Range not hugepage-aligned: %#llx..%#llx: now %#llx-%#llx\n", |
0707ad30 | 421 | start_pa, start_pa + orig_size, |
867e359b CM |
422 | range.start, range.start + range.size); |
423 | } | |
424 | highbits = __pa_to_highbits(range.start); | |
425 | if (highbits >= NR_PA_HIGHBIT_VALUES) { | |
0707ad30 | 426 | pr_err("PA high bits too high: %#llx..%#llx\n", |
867e359b CM |
427 | range.start, range.start + range.size); |
428 | continue; | |
429 | } | |
430 | if (highbits_seen[highbits]) { | |
0707ad30 | 431 | pr_err("Range overlaps in high bits: %#llx..%#llx\n", |
867e359b CM |
432 | range.start, range.start + range.size); |
433 | continue; | |
434 | } | |
435 | highbits_seen[highbits] = 1; | |
436 | if (PFN_DOWN(range.size) > maxnodemem_pfn[i]) { | |
0707ad30 CM |
437 | int max_size = maxnodemem_pfn[i]; |
438 | if (max_size > 0) { | |
f4743673 JP |
439 | pr_err("Maxnodemem reduced node %d to %d pages\n", |
440 | i, max_size); | |
0707ad30 | 441 | range.size = PFN_PHYS(max_size); |
867e359b | 442 | } else { |
0707ad30 | 443 | pr_err("Maxnodemem disabled node %d\n", i); |
867e359b CM |
444 | continue; |
445 | } | |
446 | } | |
3f29c331 JL |
447 | if (physpages + PFN_DOWN(range.size) > maxmem_pfn) { |
448 | int max_size = maxmem_pfn - physpages; | |
0707ad30 CM |
449 | if (max_size > 0) { |
450 | pr_err("Maxmem reduced node %d to %d pages\n", | |
451 | i, max_size); | |
452 | range.size = PFN_PHYS(max_size); | |
867e359b | 453 | } else { |
0707ad30 | 454 | pr_err("Maxmem disabled node %d\n", i); |
867e359b CM |
455 | continue; |
456 | } | |
457 | } | |
458 | if (i >= MAX_NUMNODES) { | |
0707ad30 | 459 | pr_err("Too many PA nodes (#%d): %#llx...%#llx\n", |
867e359b CM |
460 | i, range.size, range.size + range.start); |
461 | continue; | |
462 | } | |
463 | ||
464 | start = range.start >> PAGE_SHIFT; | |
465 | size = range.size >> PAGE_SHIFT; | |
466 | end = start + size; | |
467 | ||
468 | #ifndef __tilegx__ | |
469 | if (((HV_PhysAddr)end << PAGE_SHIFT) != | |
470 | (range.start + range.size)) { | |
0707ad30 | 471 | pr_err("PAs too high to represent: %#llx..%#llx\n", |
867e359b CM |
472 | range.start, range.start + range.size); |
473 | continue; | |
474 | } | |
475 | #endif | |
41bb38fc | 476 | #if defined(CONFIG_PCI) && !defined(__tilegx__) |
867e359b CM |
477 | /* |
478 | * Blocks that overlap the pci reserved region must | |
479 | * have enough space to hold the maximum percpu data | |
480 | * region at the top of the range. If there isn't | |
481 | * enough space above the reserved region, just | |
482 | * truncate the node. | |
483 | */ | |
484 | if (start <= pci_reserve_start_pfn && | |
485 | end > pci_reserve_start_pfn) { | |
486 | unsigned int per_cpu_size = | |
487 | __per_cpu_end - __per_cpu_start; | |
488 | unsigned int percpu_pages = | |
489 | NR_CPUS * (PFN_UP(per_cpu_size) >> PAGE_SHIFT); | |
490 | if (end < pci_reserve_end_pfn + percpu_pages) { | |
491 | end = pci_reserve_start_pfn; | |
f4743673 JP |
492 | pr_err("PCI mapping region reduced node %d to %ld pages\n", |
493 | i, end - start); | |
867e359b CM |
494 | } |
495 | } | |
496 | #endif | |
497 | ||
498 | for (j = __pfn_to_highbits(start); | |
499 | j <= __pfn_to_highbits(end - 1); j++) | |
500 | highbits_to_node[j] = i; | |
501 | ||
502 | node_start_pfn[i] = start; | |
503 | node_end_pfn[i] = end; | |
504 | node_controller[i] = range.controller; | |
3f29c331 | 505 | physpages += size; |
867e359b CM |
506 | max_pfn = end; |
507 | ||
508 | /* Mark node as online */ | |
509 | node_set(i, node_online_map); | |
510 | node_set(i, node_possible_map); | |
511 | } | |
512 | ||
513 | #ifndef __tilegx__ | |
514 | /* | |
515 | * For 4KB pages, mem_map "struct page" data is 1% of the size | |
516 | * of the physical memory, so can be quite big (640 MB for | |
517 | * four 16G zones). These structures must be mapped in | |
518 | * lowmem, and since we currently cap out at about 768 MB, | |
519 | * it's impractical to try to use this much address space. | |
520 | * For now, arbitrarily cap the amount of physical memory | |
521 | * we're willing to use at 8 million pages (32GB of 4KB pages). | |
522 | */ | |
523 | cap = 8 * 1024 * 1024; /* 8 million pages */ | |
3f29c331 | 524 | if (physpages > cap) { |
867e359b CM |
525 | int num_nodes = num_online_nodes(); |
526 | int cap_each = cap / num_nodes; | |
527 | unsigned long dropped_pages = 0; | |
528 | for (i = 0; i < num_nodes; ++i) { | |
529 | int size = node_end_pfn[i] - node_start_pfn[i]; | |
530 | if (size > cap_each) { | |
531 | dropped_pages += (size - cap_each); | |
532 | node_end_pfn[i] = node_start_pfn[i] + cap_each; | |
533 | } | |
534 | } | |
3f29c331 | 535 | physpages -= dropped_pages; |
0707ad30 | 536 | pr_warning("Only using %ldMB memory;" |
867e359b | 537 | " ignoring %ldMB.\n", |
3f29c331 | 538 | physpages >> (20 - PAGE_SHIFT), |
867e359b | 539 | dropped_pages >> (20 - PAGE_SHIFT)); |
0707ad30 | 540 | pr_warning("Consider using a larger page size.\n"); |
867e359b CM |
541 | } |
542 | #endif | |
543 | ||
544 | /* Heap starts just above the last loaded address. */ | |
545 | min_low_pfn = PFN_UP((unsigned long)_end - PAGE_OFFSET); | |
546 | ||
547 | #ifdef CONFIG_HIGHMEM | |
548 | /* Find where we map lowmem from each controller. */ | |
549 | high_memory = setup_pa_va_mapping(); | |
550 | ||
551 | /* Set max_low_pfn based on what node 0 can directly address. */ | |
552 | max_low_pfn = node_lowmem_end_pfn[0]; | |
553 | ||
554 | lowmem_pages = (mappable_physpages > MAXMEM_PFN) ? | |
555 | MAXMEM_PFN : mappable_physpages; | |
3f29c331 | 556 | highmem_pages = (long) (physpages - lowmem_pages); |
867e359b | 557 | |
f4743673 JP |
558 | pr_notice("%ldMB HIGHMEM available\n", |
559 | pages_to_mb(highmem_pages > 0 ? highmem_pages : 0)); | |
560 | pr_notice("%ldMB LOWMEM available\n", pages_to_mb(lowmem_pages)); | |
867e359b CM |
561 | #else |
562 | /* Set max_low_pfn based on what node 0 can directly address. */ | |
563 | max_low_pfn = node_end_pfn[0]; | |
564 | ||
565 | #ifndef __tilegx__ | |
566 | if (node_end_pfn[0] > MAXMEM_PFN) { | |
0707ad30 | 567 | pr_warning("Only using %ldMB LOWMEM.\n", |
867e359b | 568 | MAXMEM>>20); |
0707ad30 | 569 | pr_warning("Use a HIGHMEM enabled kernel.\n"); |
867e359b CM |
570 | max_low_pfn = MAXMEM_PFN; |
571 | max_pfn = MAXMEM_PFN; | |
867e359b CM |
572 | node_end_pfn[0] = MAXMEM_PFN; |
573 | } else { | |
f4743673 JP |
574 | pr_notice("%ldMB memory available\n", |
575 | pages_to_mb(node_end_pfn[0])); | |
867e359b CM |
576 | } |
577 | for (i = 1; i < MAX_NUMNODES; ++i) { | |
578 | node_start_pfn[i] = 0; | |
579 | node_end_pfn[i] = 0; | |
580 | } | |
581 | high_memory = __va(node_end_pfn[0]); | |
582 | #else | |
583 | lowmem_pages = 0; | |
584 | for (i = 0; i < MAX_NUMNODES; ++i) { | |
585 | int pages = node_end_pfn[i] - node_start_pfn[i]; | |
586 | lowmem_pages += pages; | |
587 | if (pages) | |
588 | high_memory = pfn_to_kaddr(node_end_pfn[i]); | |
589 | } | |
f4743673 | 590 | pr_notice("%ldMB memory available\n", pages_to_mb(lowmem_pages)); |
867e359b CM |
591 | #endif |
592 | #endif | |
593 | } | |
594 | ||
621b1955 CM |
595 | /* |
596 | * On 32-bit machines, we only put bootmem on the low controller, | |
597 | * since PAs > 4GB can't be used in bootmem. In principle one could | |
598 | * imagine, e.g., multiple 1 GB controllers all of which could support | |
599 | * bootmem, but in practice using controllers this small isn't a | |
600 | * particularly interesting scenario, so we just keep it simple and | |
601 | * use only the first controller for bootmem on 32-bit machines. | |
602 | */ | |
603 | static inline int node_has_bootmem(int nid) | |
867e359b | 604 | { |
621b1955 CM |
605 | #ifdef CONFIG_64BIT |
606 | return 1; | |
607 | #else | |
608 | return nid == 0; | |
609 | #endif | |
610 | } | |
867e359b | 611 | |
621b1955 CM |
612 | static inline unsigned long alloc_bootmem_pfn(int nid, |
613 | unsigned long size, | |
614 | unsigned long goal) | |
615 | { | |
616 | void *kva = __alloc_bootmem_node(NODE_DATA(nid), size, | |
617 | PAGE_SIZE, goal); | |
618 | unsigned long pfn = kaddr_to_pfn(kva); | |
619 | BUG_ON(goal && PFN_PHYS(pfn) != goal); | |
620 | return pfn; | |
621 | } | |
867e359b | 622 | |
621b1955 CM |
623 | static void __init setup_bootmem_allocator_node(int i) |
624 | { | |
625 | unsigned long start, end, mapsize, mapstart; | |
626 | ||
627 | if (node_has_bootmem(i)) { | |
628 | NODE_DATA(i)->bdata = &bootmem_node_data[i]; | |
629 | } else { | |
630 | /* Share controller zero's bdata for now. */ | |
631 | NODE_DATA(i)->bdata = &bootmem_node_data[0]; | |
632 | return; | |
633 | } | |
634 | ||
635 | /* Skip up to after the bss in node 0. */ | |
636 | start = (i == 0) ? min_low_pfn : node_start_pfn[i]; | |
637 | ||
638 | /* Only lowmem, if we're a HIGHMEM build. */ | |
639 | #ifdef CONFIG_HIGHMEM | |
640 | end = node_lowmem_end_pfn[i]; | |
867e359b | 641 | #else |
621b1955 | 642 | end = node_end_pfn[i]; |
867e359b CM |
643 | #endif |
644 | ||
621b1955 CM |
645 | /* No memory here. */ |
646 | if (end == start) | |
647 | return; | |
648 | ||
649 | /* Figure out where the bootmem bitmap is located. */ | |
650 | mapsize = bootmem_bootmap_pages(end - start); | |
651 | if (i == 0) { | |
652 | /* Use some space right before the heap on node 0. */ | |
653 | mapstart = start; | |
654 | start += mapsize; | |
655 | } else { | |
656 | /* Allocate bitmap on node 0 to avoid page table issues. */ | |
657 | mapstart = alloc_bootmem_pfn(0, PFN_PHYS(mapsize), 0); | |
658 | } | |
867e359b | 659 | |
621b1955 CM |
660 | /* Initialize a node. */ |
661 | init_bootmem_node(NODE_DATA(i), mapstart, start, end); | |
867e359b | 662 | |
621b1955 CM |
663 | /* Free all the space back into the allocator. */ |
664 | free_bootmem(PFN_PHYS(start), PFN_PHYS(end - start)); | |
665 | ||
41bb38fc | 666 | #if defined(CONFIG_PCI) && !defined(__tilegx__) |
867e359b | 667 | /* |
41bb38fc | 668 | * Throw away any memory aliased by the PCI region. |
867e359b | 669 | */ |
523c178e CM |
670 | if (pci_reserve_start_pfn < end && pci_reserve_end_pfn > start) { |
671 | start = max(pci_reserve_start_pfn, start); | |
672 | end = min(pci_reserve_end_pfn, end); | |
673 | reserve_bootmem(PFN_PHYS(start), PFN_PHYS(end - start), | |
621b1955 | 674 | BOOTMEM_EXCLUSIVE); |
523c178e | 675 | } |
621b1955 CM |
676 | #endif |
677 | } | |
867e359b | 678 | |
621b1955 CM |
679 | static void __init setup_bootmem_allocator(void) |
680 | { | |
681 | int i; | |
682 | for (i = 0; i < MAX_NUMNODES; ++i) | |
683 | setup_bootmem_allocator_node(i); | |
867e359b | 684 | |
77f8c740 CM |
685 | /* Reserve any memory excluded by "memmap" arguments. */ |
686 | for (i = 0; i < memmap_nr; ++i) { | |
687 | struct memmap_entry *m = &memmap_map[i]; | |
3af1ea5a | 688 | reserve_bootmem(m->addr, m->size, BOOTMEM_DEFAULT); |
77f8c740 CM |
689 | } |
690 | ||
6f0142d5 CM |
691 | #ifdef CONFIG_BLK_DEV_INITRD |
692 | if (initrd_start) { | |
693 | /* Make sure the initrd memory region is not modified. */ | |
694 | if (reserve_bootmem(initrd_start, initrd_end - initrd_start, | |
695 | BOOTMEM_EXCLUSIVE)) { | |
696 | pr_crit("The initrd memory region has been polluted. Disabling it.\n"); | |
697 | initrd_start = 0; | |
698 | initrd_end = 0; | |
699 | } else { | |
700 | /* | |
701 | * Translate initrd_start & initrd_end from PA to VA for | |
702 | * future access. | |
703 | */ | |
704 | initrd_start += PAGE_OFFSET; | |
705 | initrd_end += PAGE_OFFSET; | |
706 | } | |
707 | } | |
708 | #endif | |
709 | ||
867e359b CM |
710 | #ifdef CONFIG_KEXEC |
711 | if (crashk_res.start != crashk_res.end) | |
3af1ea5a WSH |
712 | reserve_bootmem(crashk_res.start, resource_size(&crashk_res), |
713 | BOOTMEM_DEFAULT); | |
867e359b | 714 | #endif |
867e359b CM |
715 | } |
716 | ||
717 | void *__init alloc_remap(int nid, unsigned long size) | |
718 | { | |
719 | int pages = node_end_pfn[nid] - node_start_pfn[nid]; | |
720 | void *map = pfn_to_kaddr(node_memmap_pfn[nid]); | |
721 | BUG_ON(size != pages * sizeof(struct page)); | |
722 | memset(map, 0, size); | |
723 | return map; | |
724 | } | |
725 | ||
726 | static int __init percpu_size(void) | |
727 | { | |
76c567fb CM |
728 | int size = __per_cpu_end - __per_cpu_start; |
729 | size += PERCPU_MODULE_RESERVE; | |
730 | size += PERCPU_DYNAMIC_EARLY_SIZE; | |
731 | if (size < PCPU_MIN_UNIT_SIZE) | |
732 | size = PCPU_MIN_UNIT_SIZE; | |
733 | size = roundup(size, PAGE_SIZE); | |
734 | ||
867e359b CM |
735 | /* In several places we assume the per-cpu data fits on a huge page. */ |
736 | BUG_ON(kdata_huge && size > HPAGE_SIZE); | |
737 | return size; | |
738 | } | |
739 | ||
867e359b CM |
740 | static void __init zone_sizes_init(void) |
741 | { | |
742 | unsigned long zones_size[MAX_NR_ZONES] = { 0 }; | |
867e359b CM |
743 | int size = percpu_size(); |
744 | int num_cpus = smp_height * smp_width; | |
eef015c8 CM |
745 | const unsigned long dma_end = (1UL << (32 - PAGE_SHIFT)); |
746 | ||
867e359b CM |
747 | int i; |
748 | ||
749 | for (i = 0; i < num_cpus; ++i) | |
750 | node_percpu[cpu_to_node(i)] += size; | |
751 | ||
752 | for_each_online_node(i) { | |
753 | unsigned long start = node_start_pfn[i]; | |
754 | unsigned long end = node_end_pfn[i]; | |
755 | #ifdef CONFIG_HIGHMEM | |
756 | unsigned long lowmem_end = node_lowmem_end_pfn[i]; | |
757 | #else | |
758 | unsigned long lowmem_end = end; | |
759 | #endif | |
760 | int memmap_size = (end - start) * sizeof(struct page); | |
761 | node_free_pfn[i] = start; | |
762 | ||
763 | /* | |
764 | * Set aside pages for per-cpu data and the mem_map array. | |
765 | * | |
766 | * Since the per-cpu data requires special homecaching, | |
767 | * if we are in kdata_huge mode, we put it at the end of | |
768 | * the lowmem region. If we're not in kdata_huge mode, | |
769 | * we take the per-cpu pages from the bottom of the | |
770 | * controller, since that avoids fragmenting a huge page | |
771 | * that users might want. We always take the memmap | |
772 | * from the bottom of the controller, since with | |
773 | * kdata_huge that lets it be under a huge TLB entry. | |
774 | * | |
775 | * If the user has requested isolnodes for a controller, | |
776 | * though, there'll be no lowmem, so we just alloc_bootmem | |
777 | * the memmap. There will be no percpu memory either. | |
778 | */ | |
621b1955 CM |
779 | if (i != 0 && cpu_isset(i, isolnodes)) { |
780 | node_memmap_pfn[i] = | |
781 | alloc_bootmem_pfn(0, memmap_size, 0); | |
782 | BUG_ON(node_percpu[i] != 0); | |
783 | } else if (node_has_bootmem(start)) { | |
867e359b CM |
784 | unsigned long goal = 0; |
785 | node_memmap_pfn[i] = | |
621b1955 | 786 | alloc_bootmem_pfn(i, memmap_size, 0); |
867e359b CM |
787 | if (kdata_huge) |
788 | goal = PFN_PHYS(lowmem_end) - node_percpu[i]; | |
789 | if (node_percpu[i]) | |
790 | node_percpu_pfn[i] = | |
621b1955 CM |
791 | alloc_bootmem_pfn(i, node_percpu[i], |
792 | goal); | |
867e359b | 793 | } else { |
621b1955 | 794 | /* In non-bootmem zones, just reserve some pages. */ |
867e359b CM |
795 | node_memmap_pfn[i] = node_free_pfn[i]; |
796 | node_free_pfn[i] += PFN_UP(memmap_size); | |
797 | if (!kdata_huge) { | |
798 | node_percpu_pfn[i] = node_free_pfn[i]; | |
799 | node_free_pfn[i] += PFN_UP(node_percpu[i]); | |
800 | } else { | |
801 | node_percpu_pfn[i] = | |
802 | lowmem_end - PFN_UP(node_percpu[i]); | |
803 | } | |
804 | } | |
805 | ||
806 | #ifdef CONFIG_HIGHMEM | |
807 | if (start > lowmem_end) { | |
808 | zones_size[ZONE_NORMAL] = 0; | |
809 | zones_size[ZONE_HIGHMEM] = end - start; | |
810 | } else { | |
811 | zones_size[ZONE_NORMAL] = lowmem_end - start; | |
812 | zones_size[ZONE_HIGHMEM] = end - lowmem_end; | |
813 | } | |
814 | #else | |
815 | zones_size[ZONE_NORMAL] = end - start; | |
816 | #endif | |
817 | ||
eef015c8 CM |
818 | if (start < dma_end) { |
819 | zones_size[ZONE_DMA] = min(zones_size[ZONE_NORMAL], | |
820 | dma_end - start); | |
821 | zones_size[ZONE_NORMAL] -= zones_size[ZONE_DMA]; | |
822 | } else { | |
823 | zones_size[ZONE_DMA] = 0; | |
824 | } | |
825 | ||
621b1955 CM |
826 | /* Take zone metadata from controller 0 if we're isolnode. */ |
827 | if (node_isset(i, isolnodes)) | |
828 | NODE_DATA(i)->bdata = &bootmem_node_data[0]; | |
867e359b CM |
829 | |
830 | free_area_init_node(i, zones_size, start, NULL); | |
76c567fb | 831 | printk(KERN_DEBUG " Normal zone: %ld per-cpu pages\n", |
867e359b CM |
832 | PFN_UP(node_percpu[i])); |
833 | ||
834 | /* Track the type of memory on each node */ | |
eef015c8 | 835 | if (zones_size[ZONE_NORMAL] || zones_size[ZONE_DMA]) |
867e359b CM |
836 | node_set_state(i, N_NORMAL_MEMORY); |
837 | #ifdef CONFIG_HIGHMEM | |
838 | if (end != start) | |
839 | node_set_state(i, N_HIGH_MEMORY); | |
840 | #endif | |
841 | ||
842 | node_set_online(i); | |
843 | } | |
844 | } | |
845 | ||
846 | #ifdef CONFIG_NUMA | |
847 | ||
848 | /* which logical CPUs are on which nodes */ | |
849 | struct cpumask node_2_cpu_mask[MAX_NUMNODES] __write_once; | |
850 | EXPORT_SYMBOL(node_2_cpu_mask); | |
851 | ||
852 | /* which node each logical CPU is on */ | |
853 | char cpu_2_node[NR_CPUS] __write_once __attribute__((aligned(L2_CACHE_BYTES))); | |
854 | EXPORT_SYMBOL(cpu_2_node); | |
855 | ||
856 | /* Return cpu_to_node() except for cpus not yet assigned, which return -1 */ | |
857 | static int __init cpu_to_bound_node(int cpu, struct cpumask* unbound_cpus) | |
858 | { | |
859 | if (!cpu_possible(cpu) || cpumask_test_cpu(cpu, unbound_cpus)) | |
860 | return -1; | |
861 | else | |
862 | return cpu_to_node(cpu); | |
863 | } | |
864 | ||
865 | /* Return number of immediately-adjacent tiles sharing the same NUMA node. */ | |
866 | static int __init node_neighbors(int node, int cpu, | |
867 | struct cpumask *unbound_cpus) | |
868 | { | |
869 | int neighbors = 0; | |
870 | int w = smp_width; | |
871 | int h = smp_height; | |
872 | int x = cpu % w; | |
873 | int y = cpu / w; | |
874 | if (x > 0 && cpu_to_bound_node(cpu-1, unbound_cpus) == node) | |
875 | ++neighbors; | |
876 | if (x < w-1 && cpu_to_bound_node(cpu+1, unbound_cpus) == node) | |
877 | ++neighbors; | |
878 | if (y > 0 && cpu_to_bound_node(cpu-w, unbound_cpus) == node) | |
879 | ++neighbors; | |
880 | if (y < h-1 && cpu_to_bound_node(cpu+w, unbound_cpus) == node) | |
881 | ++neighbors; | |
882 | return neighbors; | |
883 | } | |
884 | ||
885 | static void __init setup_numa_mapping(void) | |
886 | { | |
887 | int distance[MAX_NUMNODES][NR_CPUS]; | |
888 | HV_Coord coord; | |
889 | int cpu, node, cpus, i, x, y; | |
890 | int num_nodes = num_online_nodes(); | |
891 | struct cpumask unbound_cpus; | |
892 | nodemask_t default_nodes; | |
893 | ||
894 | cpumask_clear(&unbound_cpus); | |
895 | ||
896 | /* Get set of nodes we will use for defaults */ | |
897 | nodes_andnot(default_nodes, node_online_map, isolnodes); | |
898 | if (nodes_empty(default_nodes)) { | |
899 | BUG_ON(!node_isset(0, node_online_map)); | |
0707ad30 | 900 | pr_err("Forcing NUMA node zero available as a default node\n"); |
867e359b CM |
901 | node_set(0, default_nodes); |
902 | } | |
903 | ||
904 | /* Populate the distance[] array */ | |
905 | memset(distance, -1, sizeof(distance)); | |
906 | cpu = 0; | |
907 | for (coord.y = 0; coord.y < smp_height; ++coord.y) { | |
908 | for (coord.x = 0; coord.x < smp_width; | |
909 | ++coord.x, ++cpu) { | |
910 | BUG_ON(cpu >= nr_cpu_ids); | |
911 | if (!cpu_possible(cpu)) { | |
912 | cpu_2_node[cpu] = -1; | |
913 | continue; | |
914 | } | |
915 | for_each_node_mask(node, default_nodes) { | |
916 | HV_MemoryControllerInfo info = | |
917 | hv_inquire_memory_controller( | |
918 | coord, node_controller[node]); | |
919 | distance[node][cpu] = | |
920 | ABS(info.coord.x) + ABS(info.coord.y); | |
921 | } | |
922 | cpumask_set_cpu(cpu, &unbound_cpus); | |
923 | } | |
924 | } | |
925 | cpus = cpu; | |
926 | ||
927 | /* | |
928 | * Round-robin through the NUMA nodes until all the cpus are | |
929 | * assigned. We could be more clever here (e.g. create four | |
930 | * sorted linked lists on the same set of cpu nodes, and pull | |
931 | * off them in round-robin sequence, removing from all four | |
932 | * lists each time) but given the relatively small numbers | |
933 | * involved, O(n^2) seem OK for a one-time cost. | |
934 | */ | |
935 | node = first_node(default_nodes); | |
936 | while (!cpumask_empty(&unbound_cpus)) { | |
937 | int best_cpu = -1; | |
938 | int best_distance = INT_MAX; | |
939 | for (cpu = 0; cpu < cpus; ++cpu) { | |
940 | if (cpumask_test_cpu(cpu, &unbound_cpus)) { | |
941 | /* | |
942 | * Compute metric, which is how much | |
943 | * closer the cpu is to this memory | |
944 | * controller than the others, shifted | |
945 | * up, and then the number of | |
946 | * neighbors already in the node as an | |
947 | * epsilon adjustment to try to keep | |
948 | * the nodes compact. | |
949 | */ | |
950 | int d = distance[node][cpu] * num_nodes; | |
951 | for_each_node_mask(i, default_nodes) { | |
952 | if (i != node) | |
953 | d -= distance[i][cpu]; | |
954 | } | |
955 | d *= 8; /* allow space for epsilon */ | |
956 | d -= node_neighbors(node, cpu, &unbound_cpus); | |
957 | if (d < best_distance) { | |
958 | best_cpu = cpu; | |
959 | best_distance = d; | |
960 | } | |
961 | } | |
962 | } | |
963 | BUG_ON(best_cpu < 0); | |
964 | cpumask_set_cpu(best_cpu, &node_2_cpu_mask[node]); | |
965 | cpu_2_node[best_cpu] = node; | |
966 | cpumask_clear_cpu(best_cpu, &unbound_cpus); | |
967 | node = next_node(node, default_nodes); | |
968 | if (node == MAX_NUMNODES) | |
969 | node = first_node(default_nodes); | |
970 | } | |
971 | ||
972 | /* Print out node assignments and set defaults for disabled cpus */ | |
973 | cpu = 0; | |
974 | for (y = 0; y < smp_height; ++y) { | |
975 | printk(KERN_DEBUG "NUMA cpu-to-node row %d:", y); | |
976 | for (x = 0; x < smp_width; ++x, ++cpu) { | |
977 | if (cpu_to_node(cpu) < 0) { | |
0707ad30 | 978 | pr_cont(" -"); |
867e359b CM |
979 | cpu_2_node[cpu] = first_node(default_nodes); |
980 | } else { | |
0707ad30 | 981 | pr_cont(" %d", cpu_to_node(cpu)); |
867e359b CM |
982 | } |
983 | } | |
0707ad30 | 984 | pr_cont("\n"); |
867e359b CM |
985 | } |
986 | } | |
987 | ||
988 | static struct cpu cpu_devices[NR_CPUS]; | |
989 | ||
990 | static int __init topology_init(void) | |
991 | { | |
992 | int i; | |
993 | ||
994 | for_each_online_node(i) | |
995 | register_one_node(i); | |
996 | ||
4d658d13 | 997 | for (i = 0; i < smp_height * smp_width; ++i) |
867e359b CM |
998 | register_cpu(&cpu_devices[i], i); |
999 | ||
1000 | return 0; | |
1001 | } | |
1002 | ||
1003 | subsys_initcall(topology_init); | |
1004 | ||
1005 | #else /* !CONFIG_NUMA */ | |
1006 | ||
1007 | #define setup_numa_mapping() do { } while (0) | |
1008 | ||
1009 | #endif /* CONFIG_NUMA */ | |
1010 | ||
621b1955 CM |
1011 | /* |
1012 | * Initialize hugepage support on this cpu. We do this on all cores | |
1013 | * early in boot: before argument parsing for the boot cpu, and after | |
1014 | * argument parsing but before the init functions run on the secondaries. | |
1015 | * So the values we set up here in the hypervisor may be overridden on | |
1016 | * the boot cpu as arguments are parsed. | |
1017 | */ | |
18f894c1 | 1018 | static void init_super_pages(void) |
621b1955 CM |
1019 | { |
1020 | #ifdef CONFIG_HUGETLB_SUPER_PAGES | |
1021 | int i; | |
1022 | for (i = 0; i < HUGE_SHIFT_ENTRIES; ++i) | |
1023 | hv_set_pte_super_shift(i, huge_shift[i]); | |
1024 | #endif | |
1025 | } | |
1026 | ||
867e359b | 1027 | /** |
0707ad30 CM |
1028 | * setup_cpu() - Do all necessary per-cpu, tile-specific initialization. |
1029 | * @boot: Is this the boot cpu? | |
867e359b | 1030 | * |
0707ad30 | 1031 | * Called from setup_arch() on the boot cpu, or online_secondary(). |
867e359b | 1032 | */ |
18f894c1 | 1033 | void setup_cpu(int boot) |
867e359b | 1034 | { |
0707ad30 CM |
1035 | /* The boot cpu sets up its permanent mappings much earlier. */ |
1036 | if (!boot) | |
1037 | store_permanent_mappings(); | |
1038 | ||
867e359b CM |
1039 | /* Allow asynchronous TLB interrupts. */ |
1040 | #if CHIP_HAS_TILE_DMA() | |
5d966115 CM |
1041 | arch_local_irq_unmask(INT_DMATLB_MISS); |
1042 | arch_local_irq_unmask(INT_DMATLB_ACCESS); | |
867e359b | 1043 | #endif |
a78c942d | 1044 | #ifdef __tilegx__ |
5d966115 | 1045 | arch_local_irq_unmask(INT_SINGLE_STEP_K); |
a78c942d | 1046 | #endif |
867e359b CM |
1047 | |
1048 | /* | |
1049 | * Allow user access to many generic SPRs, like the cycle | |
1050 | * counter, PASS/FAIL/DONE, INTERRUPT_CRITICAL_SECTION, etc. | |
1051 | */ | |
1052 | __insn_mtspr(SPR_MPL_WORLD_ACCESS_SET_0, 1); | |
1053 | ||
1054 | #if CHIP_HAS_SN() | |
1055 | /* Static network is not restricted. */ | |
1056 | __insn_mtspr(SPR_MPL_SN_ACCESS_SET_0, 1); | |
1057 | #endif | |
867e359b CM |
1058 | |
1059 | /* | |
a78c942d CM |
1060 | * Set the MPL for interrupt control 0 & 1 to the corresponding |
1061 | * values. This includes access to the SYSTEM_SAVE and EX_CONTEXT | |
1062 | * SPRs, as well as the interrupt mask. | |
867e359b CM |
1063 | */ |
1064 | __insn_mtspr(SPR_MPL_INTCTRL_0_SET_0, 1); | |
a78c942d | 1065 | __insn_mtspr(SPR_MPL_INTCTRL_1_SET_1, 1); |
0707ad30 CM |
1066 | |
1067 | /* Initialize IRQ support for this cpu. */ | |
1068 | setup_irq_regs(); | |
1069 | ||
1070 | #ifdef CONFIG_HARDWALL | |
1071 | /* Reset the network state on this cpu. */ | |
1072 | reset_network_state(); | |
1073 | #endif | |
621b1955 CM |
1074 | |
1075 | init_super_pages(); | |
867e359b CM |
1076 | } |
1077 | ||
43d9ebba CM |
1078 | #ifdef CONFIG_BLK_DEV_INITRD |
1079 | ||
867e359b | 1080 | static int __initdata set_initramfs_file; |
ff7f3efb | 1081 | static char __initdata initramfs_file[128] = "initramfs"; |
867e359b CM |
1082 | |
1083 | static int __init setup_initramfs_file(char *str) | |
1084 | { | |
1085 | if (str == NULL) | |
1086 | return -EINVAL; | |
1087 | strncpy(initramfs_file, str, sizeof(initramfs_file) - 1); | |
1088 | set_initramfs_file = 1; | |
1089 | ||
1090 | return 0; | |
1091 | } | |
1092 | early_param("initramfs_file", setup_initramfs_file); | |
1093 | ||
1094 | /* | |
ff7f3efb CM |
1095 | * We look for a file called "initramfs" in the hvfs. If there is one, we |
1096 | * allocate some memory for it and it will be unpacked to the initramfs. | |
1097 | * If it's compressed, the initd code will uncompress it first. | |
867e359b CM |
1098 | */ |
1099 | static void __init load_hv_initrd(void) | |
1100 | { | |
1101 | HV_FS_StatInfo stat; | |
1102 | int fd, rc; | |
1103 | void *initrd; | |
1104 | ||
6f0142d5 CM |
1105 | /* If initrd has already been set, skip initramfs file in hvfs. */ |
1106 | if (initrd_start) | |
1107 | return; | |
1108 | ||
867e359b CM |
1109 | fd = hv_fs_findfile((HV_VirtAddr) initramfs_file); |
1110 | if (fd == HV_ENOENT) { | |
ff7f3efb | 1111 | if (set_initramfs_file) { |
0707ad30 CM |
1112 | pr_warning("No such hvfs initramfs file '%s'\n", |
1113 | initramfs_file); | |
ff7f3efb CM |
1114 | return; |
1115 | } else { | |
1116 | /* Try old backwards-compatible name. */ | |
1117 | fd = hv_fs_findfile((HV_VirtAddr)"initramfs.cpio.gz"); | |
1118 | if (fd == HV_ENOENT) | |
1119 | return; | |
1120 | } | |
867e359b CM |
1121 | } |
1122 | BUG_ON(fd < 0); | |
1123 | stat = hv_fs_fstat(fd); | |
1124 | BUG_ON(stat.size < 0); | |
1125 | if (stat.flags & HV_FS_ISDIR) { | |
0707ad30 CM |
1126 | pr_warning("Ignoring hvfs file '%s': it's a directory.\n", |
1127 | initramfs_file); | |
867e359b CM |
1128 | return; |
1129 | } | |
1130 | initrd = alloc_bootmem_pages(stat.size); | |
1131 | rc = hv_fs_pread(fd, (HV_VirtAddr) initrd, stat.size, 0); | |
1132 | if (rc != stat.size) { | |
0707ad30 | 1133 | pr_err("Error reading %d bytes from hvfs file '%s': %d\n", |
867e359b | 1134 | stat.size, initramfs_file, rc); |
bc63de7c | 1135 | free_initrd_mem((unsigned long) initrd, stat.size); |
867e359b CM |
1136 | return; |
1137 | } | |
1138 | initrd_start = (unsigned long) initrd; | |
1139 | initrd_end = initrd_start + stat.size; | |
1140 | } | |
1141 | ||
1142 | void __init free_initrd_mem(unsigned long begin, unsigned long end) | |
1143 | { | |
bc63de7c | 1144 | free_bootmem(__pa(begin), end - begin); |
867e359b CM |
1145 | } |
1146 | ||
6f0142d5 CM |
1147 | static int __init setup_initrd(char *str) |
1148 | { | |
1149 | char *endp; | |
1150 | unsigned long initrd_size; | |
1151 | ||
1152 | initrd_size = str ? simple_strtoul(str, &endp, 0) : 0; | |
1153 | if (initrd_size == 0 || *endp != '@') | |
1154 | return -EINVAL; | |
1155 | ||
1156 | initrd_start = simple_strtoul(endp+1, &endp, 0); | |
1157 | if (initrd_start == 0) | |
1158 | return -EINVAL; | |
1159 | ||
1160 | initrd_end = initrd_start + initrd_size; | |
1161 | ||
1162 | return 0; | |
1163 | } | |
1164 | early_param("initrd", setup_initrd); | |
1165 | ||
43d9ebba CM |
1166 | #else |
1167 | static inline void load_hv_initrd(void) {} | |
1168 | #endif /* CONFIG_BLK_DEV_INITRD */ | |
1169 | ||
867e359b CM |
1170 | static void __init validate_hv(void) |
1171 | { | |
1172 | /* | |
1173 | * It may already be too late, but let's check our built-in | |
1174 | * configuration against what the hypervisor is providing. | |
1175 | */ | |
1176 | unsigned long glue_size = hv_sysconf(HV_SYSCONF_GLUE_SIZE); | |
1177 | int hv_page_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_SMALL); | |
1178 | int hv_hpage_size = hv_sysconf(HV_SYSCONF_PAGE_SIZE_LARGE); | |
1179 | HV_ASIDRange asid_range; | |
1180 | ||
1181 | #ifndef CONFIG_SMP | |
1182 | HV_Topology topology = hv_inquire_topology(); | |
1183 | BUG_ON(topology.coord.x != 0 || topology.coord.y != 0); | |
1184 | if (topology.width != 1 || topology.height != 1) { | |
0707ad30 CM |
1185 | pr_warning("Warning: booting UP kernel on %dx%d grid;" |
1186 | " will ignore all but first tile.\n", | |
1187 | topology.width, topology.height); | |
867e359b CM |
1188 | } |
1189 | #endif | |
1190 | ||
1191 | if (PAGE_OFFSET + HV_GLUE_START_CPA + glue_size > (unsigned long)_text) | |
1192 | early_panic("Hypervisor glue size %ld is too big!\n", | |
1193 | glue_size); | |
1194 | if (hv_page_size != PAGE_SIZE) | |
1195 | early_panic("Hypervisor page size %#x != our %#lx\n", | |
1196 | hv_page_size, PAGE_SIZE); | |
1197 | if (hv_hpage_size != HPAGE_SIZE) | |
1198 | early_panic("Hypervisor huge page size %#x != our %#lx\n", | |
1199 | hv_hpage_size, HPAGE_SIZE); | |
1200 | ||
1201 | #ifdef CONFIG_SMP | |
1202 | /* | |
1203 | * Some hypervisor APIs take a pointer to a bitmap array | |
1204 | * whose size is at least the number of cpus on the chip. | |
1205 | * We use a struct cpumask for this, so it must be big enough. | |
1206 | */ | |
1207 | if ((smp_height * smp_width) > nr_cpu_ids) | |
1208 | early_panic("Hypervisor %d x %d grid too big for Linux" | |
1209 | " NR_CPUS %d\n", smp_height, smp_width, | |
1210 | nr_cpu_ids); | |
1211 | #endif | |
1212 | ||
1213 | /* | |
1214 | * Check that we're using allowed ASIDs, and initialize the | |
1215 | * various asid variables to their appropriate initial states. | |
1216 | */ | |
1217 | asid_range = hv_inquire_asid(0); | |
b4f50191 CL |
1218 | min_asid = asid_range.start; |
1219 | __this_cpu_write(current_asid, min_asid); | |
867e359b CM |
1220 | max_asid = asid_range.start + asid_range.size - 1; |
1221 | ||
1222 | if (hv_confstr(HV_CONFSTR_CHIP_MODEL, (HV_VirtAddr)chip_model, | |
1223 | sizeof(chip_model)) < 0) { | |
0707ad30 | 1224 | pr_err("Warning: HV_CONFSTR_CHIP_MODEL not available\n"); |
867e359b CM |
1225 | strlcpy(chip_model, "unknown", sizeof(chip_model)); |
1226 | } | |
1227 | } | |
1228 | ||
1229 | static void __init validate_va(void) | |
1230 | { | |
1231 | #ifndef __tilegx__ /* FIXME: GX: probably some validation relevant here */ | |
1232 | /* | |
1233 | * Similarly, make sure we're only using allowed VAs. | |
acbde1db | 1234 | * We assume we can contiguously use MEM_USER_INTRPT .. MEM_HV_START, |
867e359b CM |
1235 | * and 0 .. KERNEL_HIGH_VADDR. |
1236 | * In addition, make sure we CAN'T use the end of memory, since | |
1237 | * we use the last chunk of each pgd for the pgd_list. | |
1238 | */ | |
a78c942d | 1239 | int i, user_kernel_ok = 0; |
867e359b CM |
1240 | unsigned long max_va = 0; |
1241 | unsigned long list_va = | |
1242 | ((PGD_LIST_OFFSET / sizeof(pgd_t)) << PGDIR_SHIFT); | |
1243 | ||
1244 | for (i = 0; ; ++i) { | |
1245 | HV_VirtAddrRange range = hv_inquire_virtual(i); | |
1246 | if (range.size == 0) | |
1247 | break; | |
1248 | if (range.start <= MEM_USER_INTRPT && | |
acbde1db | 1249 | range.start + range.size >= MEM_HV_START) |
a78c942d | 1250 | user_kernel_ok = 1; |
867e359b CM |
1251 | if (range.start == 0) |
1252 | max_va = range.size; | |
1253 | BUG_ON(range.start + range.size > list_va); | |
1254 | } | |
a78c942d CM |
1255 | if (!user_kernel_ok) |
1256 | early_panic("Hypervisor not configured for user/kernel VAs\n"); | |
867e359b CM |
1257 | if (max_va == 0) |
1258 | early_panic("Hypervisor not configured for low VAs\n"); | |
1259 | if (max_va < KERNEL_HIGH_VADDR) | |
1260 | early_panic("Hypervisor max VA %#lx smaller than %#lx\n", | |
1261 | max_va, KERNEL_HIGH_VADDR); | |
1262 | ||
1263 | /* Kernel PCs must have their high bit set; see intvec.S. */ | |
1264 | if ((long)VMALLOC_START >= 0) | |
1265 | early_panic( | |
1266 | "Linux VMALLOC region below the 2GB line (%#lx)!\n" | |
4b12909f | 1267 | "Reconfigure the kernel with smaller VMALLOC_RESERVE.\n", |
867e359b CM |
1268 | VMALLOC_START); |
1269 | #endif | |
1270 | } | |
1271 | ||
1272 | /* | |
1273 | * cpu_lotar_map lists all the cpus that are valid for the supervisor | |
1274 | * to cache data on at a page level, i.e. what cpus can be placed in | |
1275 | * the LOTAR field of a PTE. It is equivalent to the set of possible | |
1276 | * cpus plus any other cpus that are willing to share their cache. | |
1277 | * It is set by hv_inquire_tiles(HV_INQ_TILES_LOTAR). | |
1278 | */ | |
1279 | struct cpumask __write_once cpu_lotar_map; | |
1280 | EXPORT_SYMBOL(cpu_lotar_map); | |
1281 | ||
867e359b CM |
1282 | /* |
1283 | * hash_for_home_map lists all the tiles that hash-for-home data | |
1284 | * will be cached on. Note that this may includes tiles that are not | |
1285 | * valid for this supervisor to use otherwise (e.g. if a hypervisor | |
1286 | * device is being shared between multiple supervisors). | |
1287 | * It is set by hv_inquire_tiles(HV_INQ_TILES_HFH_CACHE). | |
1288 | */ | |
1289 | struct cpumask hash_for_home_map; | |
1290 | EXPORT_SYMBOL(hash_for_home_map); | |
867e359b CM |
1291 | |
1292 | /* | |
1293 | * cpu_cacheable_map lists all the cpus whose caches the hypervisor can | |
5f054e31 | 1294 | * flush on our behalf. It is set to cpu_possible_mask OR'ed with |
867e359b CM |
1295 | * hash_for_home_map, and it is what should be passed to |
1296 | * hv_flush_remote() to flush all caches. Note that if there are | |
1297 | * dedicated hypervisor driver tiles that have authorized use of their | |
1298 | * cache, those tiles will only appear in cpu_lotar_map, NOT in | |
1299 | * cpu_cacheable_map, as they are a special case. | |
1300 | */ | |
1301 | struct cpumask __write_once cpu_cacheable_map; | |
1302 | EXPORT_SYMBOL(cpu_cacheable_map); | |
1303 | ||
1304 | static __initdata struct cpumask disabled_map; | |
1305 | ||
1306 | static int __init disabled_cpus(char *str) | |
1307 | { | |
1308 | int boot_cpu = smp_processor_id(); | |
1309 | ||
1310 | if (str == NULL || cpulist_parse_crop(str, &disabled_map) != 0) | |
1311 | return -EINVAL; | |
1312 | if (cpumask_test_cpu(boot_cpu, &disabled_map)) { | |
0707ad30 | 1313 | pr_err("disabled_cpus: can't disable boot cpu %d\n", boot_cpu); |
867e359b CM |
1314 | cpumask_clear_cpu(boot_cpu, &disabled_map); |
1315 | } | |
1316 | return 0; | |
1317 | } | |
1318 | ||
1319 | early_param("disabled_cpus", disabled_cpus); | |
1320 | ||
0707ad30 | 1321 | void __init print_disabled_cpus(void) |
867e359b CM |
1322 | { |
1323 | if (!cpumask_empty(&disabled_map)) { | |
1324 | char buf[100]; | |
1325 | cpulist_scnprintf(buf, sizeof(buf), &disabled_map); | |
0707ad30 | 1326 | pr_info("CPUs not available for Linux: %s\n", buf); |
867e359b CM |
1327 | } |
1328 | } | |
1329 | ||
1330 | static void __init setup_cpu_maps(void) | |
1331 | { | |
1332 | struct cpumask hv_disabled_map, cpu_possible_init; | |
1333 | int boot_cpu = smp_processor_id(); | |
1334 | int cpus, i, rc; | |
1335 | ||
1336 | /* Learn which cpus are allowed by the hypervisor. */ | |
1337 | rc = hv_inquire_tiles(HV_INQ_TILES_AVAIL, | |
1338 | (HV_VirtAddr) cpumask_bits(&cpu_possible_init), | |
1339 | sizeof(cpu_cacheable_map)); | |
1340 | if (rc < 0) | |
1341 | early_panic("hv_inquire_tiles(AVAIL) failed: rc %d\n", rc); | |
1342 | if (!cpumask_test_cpu(boot_cpu, &cpu_possible_init)) | |
1343 | early_panic("Boot CPU %d disabled by hypervisor!\n", boot_cpu); | |
1344 | ||
1345 | /* Compute the cpus disabled by the hvconfig file. */ | |
1346 | cpumask_complement(&hv_disabled_map, &cpu_possible_init); | |
1347 | ||
1348 | /* Include them with the cpus disabled by "disabled_cpus". */ | |
1349 | cpumask_or(&disabled_map, &disabled_map, &hv_disabled_map); | |
1350 | ||
1351 | /* | |
1352 | * Disable every cpu after "setup_max_cpus". But don't mark | |
1353 | * as disabled the cpus that are outside of our initial rectangle, | |
1354 | * since that turns out to be confusing. | |
1355 | */ | |
1356 | cpus = 1; /* this cpu */ | |
1357 | cpumask_set_cpu(boot_cpu, &disabled_map); /* ignore this cpu */ | |
1358 | for (i = 0; cpus < setup_max_cpus; ++i) | |
1359 | if (!cpumask_test_cpu(i, &disabled_map)) | |
1360 | ++cpus; | |
1361 | for (; i < smp_height * smp_width; ++i) | |
1362 | cpumask_set_cpu(i, &disabled_map); | |
1363 | cpumask_clear_cpu(boot_cpu, &disabled_map); /* reset this cpu */ | |
1364 | for (i = smp_height * smp_width; i < NR_CPUS; ++i) | |
1365 | cpumask_clear_cpu(i, &disabled_map); | |
1366 | ||
1367 | /* | |
1368 | * Setup cpu_possible map as every cpu allocated to us, minus | |
1369 | * the results of any "disabled_cpus" settings. | |
1370 | */ | |
1371 | cpumask_andnot(&cpu_possible_init, &cpu_possible_init, &disabled_map); | |
1372 | init_cpu_possible(&cpu_possible_init); | |
1373 | ||
1374 | /* Learn which cpus are valid for LOTAR caching. */ | |
1375 | rc = hv_inquire_tiles(HV_INQ_TILES_LOTAR, | |
1376 | (HV_VirtAddr) cpumask_bits(&cpu_lotar_map), | |
1377 | sizeof(cpu_lotar_map)); | |
1378 | if (rc < 0) { | |
0707ad30 | 1379 | pr_err("warning: no HV_INQ_TILES_LOTAR; using AVAIL\n"); |
0b5f9c00 | 1380 | cpu_lotar_map = *cpu_possible_mask; |
867e359b CM |
1381 | } |
1382 | ||
867e359b CM |
1383 | /* Retrieve set of CPUs used for hash-for-home caching */ |
1384 | rc = hv_inquire_tiles(HV_INQ_TILES_HFH_CACHE, | |
1385 | (HV_VirtAddr) hash_for_home_map.bits, | |
1386 | sizeof(hash_for_home_map)); | |
1387 | if (rc < 0) | |
1388 | early_panic("hv_inquire_tiles(HFH_CACHE) failed: rc %d\n", rc); | |
0b5f9c00 | 1389 | cpumask_or(&cpu_cacheable_map, cpu_possible_mask, &hash_for_home_map); |
867e359b CM |
1390 | } |
1391 | ||
1392 | ||
1393 | static int __init dataplane(char *str) | |
1394 | { | |
0707ad30 | 1395 | pr_warning("WARNING: dataplane support disabled in this kernel\n"); |
867e359b CM |
1396 | return 0; |
1397 | } | |
1398 | ||
1399 | early_param("dataplane", dataplane); | |
1400 | ||
1401 | #ifdef CONFIG_CMDLINE_BOOL | |
1402 | static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE; | |
1403 | #endif | |
1404 | ||
1405 | void __init setup_arch(char **cmdline_p) | |
1406 | { | |
1407 | int len; | |
1408 | ||
1409 | #if defined(CONFIG_CMDLINE_BOOL) && defined(CONFIG_CMDLINE_OVERRIDE) | |
1410 | len = hv_get_command_line((HV_VirtAddr) boot_command_line, | |
1411 | COMMAND_LINE_SIZE); | |
1412 | if (boot_command_line[0]) | |
0707ad30 CM |
1413 | pr_warning("WARNING: ignoring dynamic command line \"%s\"\n", |
1414 | boot_command_line); | |
867e359b CM |
1415 | strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE); |
1416 | #else | |
1417 | char *hv_cmdline; | |
1418 | #if defined(CONFIG_CMDLINE_BOOL) | |
1419 | if (builtin_cmdline[0]) { | |
1420 | int builtin_len = strlcpy(boot_command_line, builtin_cmdline, | |
1421 | COMMAND_LINE_SIZE); | |
1422 | if (builtin_len < COMMAND_LINE_SIZE-1) | |
1423 | boot_command_line[builtin_len++] = ' '; | |
1424 | hv_cmdline = &boot_command_line[builtin_len]; | |
1425 | len = COMMAND_LINE_SIZE - builtin_len; | |
1426 | } else | |
1427 | #endif | |
1428 | { | |
1429 | hv_cmdline = boot_command_line; | |
1430 | len = COMMAND_LINE_SIZE; | |
1431 | } | |
1432 | len = hv_get_command_line((HV_VirtAddr) hv_cmdline, len); | |
1433 | if (len < 0 || len > COMMAND_LINE_SIZE) | |
1434 | early_panic("hv_get_command_line failed: %d\n", len); | |
1435 | #endif | |
1436 | ||
1437 | *cmdline_p = boot_command_line; | |
1438 | ||
1439 | /* Set disabled_map and setup_max_cpus very early */ | |
1440 | parse_early_param(); | |
1441 | ||
1442 | /* Make sure the kernel is compatible with the hypervisor. */ | |
1443 | validate_hv(); | |
1444 | validate_va(); | |
1445 | ||
1446 | setup_cpu_maps(); | |
1447 | ||
1448 | ||
41bb38fc | 1449 | #if defined(CONFIG_PCI) && !defined(__tilegx__) |
867e359b CM |
1450 | /* |
1451 | * Initialize the PCI structures. This is done before memory | |
1452 | * setup so that we know whether or not a pci_reserve region | |
1453 | * is necessary. | |
1454 | */ | |
1455 | if (tile_pci_init() == 0) | |
1456 | pci_reserve_mb = 0; | |
1457 | ||
1458 | /* PCI systems reserve a region just below 4GB for mapping iomem. */ | |
1459 | pci_reserve_end_pfn = (1 << (32 - PAGE_SHIFT)); | |
1460 | pci_reserve_start_pfn = pci_reserve_end_pfn - | |
1461 | (pci_reserve_mb << (20 - PAGE_SHIFT)); | |
1462 | #endif | |
1463 | ||
1464 | init_mm.start_code = (unsigned long) _text; | |
1465 | init_mm.end_code = (unsigned long) _etext; | |
1466 | init_mm.end_data = (unsigned long) _edata; | |
1467 | init_mm.brk = (unsigned long) _end; | |
1468 | ||
1469 | setup_memory(); | |
1470 | store_permanent_mappings(); | |
1471 | setup_bootmem_allocator(); | |
1472 | ||
1473 | /* | |
1474 | * NOTE: before this point _nobody_ is allowed to allocate | |
1475 | * any memory using the bootmem allocator. | |
1476 | */ | |
1477 | ||
41bb38fc CM |
1478 | #ifdef CONFIG_SWIOTLB |
1479 | swiotlb_init(0); | |
1480 | #endif | |
1481 | ||
867e359b CM |
1482 | paging_init(); |
1483 | setup_numa_mapping(); | |
1484 | zone_sizes_init(); | |
1485 | set_page_homes(); | |
0707ad30 | 1486 | setup_cpu(1); |
867e359b CM |
1487 | setup_clock(); |
1488 | load_hv_initrd(); | |
1489 | } | |
1490 | ||
1491 | ||
1492 | /* | |
1493 | * Set up per-cpu memory. | |
1494 | */ | |
1495 | ||
1496 | unsigned long __per_cpu_offset[NR_CPUS] __write_once; | |
1497 | EXPORT_SYMBOL(__per_cpu_offset); | |
1498 | ||
1499 | static size_t __initdata pfn_offset[MAX_NUMNODES] = { 0 }; | |
1500 | static unsigned long __initdata percpu_pfn[NR_CPUS] = { 0 }; | |
1501 | ||
1502 | /* | |
1503 | * As the percpu code allocates pages, we return the pages from the | |
1504 | * end of the node for the specified cpu. | |
1505 | */ | |
1506 | static void *__init pcpu_fc_alloc(unsigned int cpu, size_t size, size_t align) | |
1507 | { | |
1508 | int nid = cpu_to_node(cpu); | |
1509 | unsigned long pfn = node_percpu_pfn[nid] + pfn_offset[nid]; | |
1510 | ||
1511 | BUG_ON(size % PAGE_SIZE != 0); | |
1512 | pfn_offset[nid] += size / PAGE_SIZE; | |
76c567fb CM |
1513 | BUG_ON(node_percpu[nid] < size); |
1514 | node_percpu[nid] -= size; | |
867e359b CM |
1515 | if (percpu_pfn[cpu] == 0) |
1516 | percpu_pfn[cpu] = pfn; | |
1517 | return pfn_to_kaddr(pfn); | |
1518 | } | |
1519 | ||
1520 | /* | |
1521 | * Pages reserved for percpu memory are not freeable, and in any case we are | |
1522 | * on a short path to panic() in setup_per_cpu_area() at this point anyway. | |
1523 | */ | |
1524 | static void __init pcpu_fc_free(void *ptr, size_t size) | |
1525 | { | |
1526 | } | |
1527 | ||
1528 | /* | |
1529 | * Set up vmalloc page tables using bootmem for the percpu code. | |
1530 | */ | |
1531 | static void __init pcpu_fc_populate_pte(unsigned long addr) | |
1532 | { | |
1533 | pgd_t *pgd; | |
1534 | pud_t *pud; | |
1535 | pmd_t *pmd; | |
1536 | pte_t *pte; | |
1537 | ||
1538 | BUG_ON(pgd_addr_invalid(addr)); | |
77d23303 | 1539 | if (addr < VMALLOC_START || addr >= VMALLOC_END) |
f4743673 | 1540 | panic("PCPU addr %#lx outside vmalloc range %#lx..%#lx; try increasing CONFIG_VMALLOC_RESERVE\n", |
77d23303 | 1541 | addr, VMALLOC_START, VMALLOC_END); |
867e359b CM |
1542 | |
1543 | pgd = swapper_pg_dir + pgd_index(addr); | |
1544 | pud = pud_offset(pgd, addr); | |
1545 | BUG_ON(!pud_present(*pud)); | |
1546 | pmd = pmd_offset(pud, addr); | |
1547 | if (pmd_present(*pmd)) { | |
1548 | BUG_ON(pmd_huge_page(*pmd)); | |
1549 | } else { | |
1550 | pte = __alloc_bootmem(L2_KERNEL_PGTABLE_SIZE, | |
1551 | HV_PAGE_TABLE_ALIGN, 0); | |
1552 | pmd_populate_kernel(&init_mm, pmd, pte); | |
1553 | } | |
1554 | } | |
1555 | ||
1556 | void __init setup_per_cpu_areas(void) | |
1557 | { | |
1558 | struct page *pg; | |
1559 | unsigned long delta, pfn, lowmem_va; | |
1560 | unsigned long size = percpu_size(); | |
1561 | char *ptr; | |
1562 | int rc, cpu, i; | |
1563 | ||
1564 | rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE, pcpu_fc_alloc, | |
1565 | pcpu_fc_free, pcpu_fc_populate_pte); | |
1566 | if (rc < 0) | |
1567 | panic("Cannot initialize percpu area (err=%d)", rc); | |
1568 | ||
1569 | delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start; | |
1570 | for_each_possible_cpu(cpu) { | |
1571 | __per_cpu_offset[cpu] = delta + pcpu_unit_offsets[cpu]; | |
1572 | ||
1573 | /* finv the copy out of cache so we can change homecache */ | |
1574 | ptr = pcpu_base_addr + pcpu_unit_offsets[cpu]; | |
1575 | __finv_buffer(ptr, size); | |
1576 | pfn = percpu_pfn[cpu]; | |
1577 | ||
1578 | /* Rewrite the page tables to cache on that cpu */ | |
1579 | pg = pfn_to_page(pfn); | |
1580 | for (i = 0; i < size; i += PAGE_SIZE, ++pfn, ++pg) { | |
1581 | ||
1582 | /* Update the vmalloc mapping and page home. */ | |
d5d14ed6 | 1583 | unsigned long addr = (unsigned long)ptr + i; |
640710a3 | 1584 | pte_t *ptep = virt_to_kpte(addr); |
867e359b CM |
1585 | pte_t pte = *ptep; |
1586 | BUG_ON(pfn != pte_pfn(pte)); | |
1587 | pte = hv_pte_set_mode(pte, HV_PTE_MODE_CACHE_TILE_L3); | |
1588 | pte = set_remote_cache_cpu(pte, cpu); | |
d5d14ed6 | 1589 | set_pte_at(&init_mm, addr, ptep, pte); |
867e359b CM |
1590 | |
1591 | /* Update the lowmem mapping for consistency. */ | |
1592 | lowmem_va = (unsigned long)pfn_to_kaddr(pfn); | |
640710a3 | 1593 | ptep = virt_to_kpte(lowmem_va); |
867e359b | 1594 | if (pte_huge(*ptep)) { |
f4743673 JP |
1595 | printk(KERN_DEBUG "early shatter of huge page at %#lx\n", |
1596 | lowmem_va); | |
867e359b | 1597 | shatter_pmd((pmd_t *)ptep); |
640710a3 | 1598 | ptep = virt_to_kpte(lowmem_va); |
867e359b CM |
1599 | BUG_ON(pte_huge(*ptep)); |
1600 | } | |
1601 | BUG_ON(pfn != pte_pfn(*ptep)); | |
d5d14ed6 | 1602 | set_pte_at(&init_mm, lowmem_va, ptep, pte); |
867e359b CM |
1603 | } |
1604 | } | |
1605 | ||
1606 | /* Set our thread pointer appropriately. */ | |
1607 | set_my_cpu_offset(__per_cpu_offset[smp_processor_id()]); | |
1608 | ||
1609 | /* Make sure the finv's have completed. */ | |
1610 | mb_incoherent(); | |
1611 | ||
1612 | /* Flush the TLB so we reference it properly from here on out. */ | |
1613 | local_flush_tlb_all(); | |
1614 | } | |
1615 | ||
1616 | static struct resource data_resource = { | |
1617 | .name = "Kernel data", | |
1618 | .start = 0, | |
1619 | .end = 0, | |
1620 | .flags = IORESOURCE_BUSY | IORESOURCE_MEM | |
1621 | }; | |
1622 | ||
1623 | static struct resource code_resource = { | |
1624 | .name = "Kernel code", | |
1625 | .start = 0, | |
1626 | .end = 0, | |
1627 | .flags = IORESOURCE_BUSY | IORESOURCE_MEM | |
1628 | }; | |
1629 | ||
1630 | /* | |
41bb38fc | 1631 | * On Pro, we reserve all resources above 4GB so that PCI won't try to put |
f6d2ce00 | 1632 | * mappings above 4GB. |
867e359b | 1633 | */ |
41bb38fc | 1634 | #if defined(CONFIG_PCI) && !defined(__tilegx__) |
867e359b CM |
1635 | static struct resource* __init |
1636 | insert_non_bus_resource(void) | |
1637 | { | |
1638 | struct resource *res = | |
1639 | kzalloc(sizeof(struct resource), GFP_ATOMIC); | |
6d715790 WSH |
1640 | if (!res) |
1641 | return NULL; | |
867e359b CM |
1642 | res->name = "Non-Bus Physical Address Space"; |
1643 | res->start = (1ULL << 32); | |
1644 | res->end = -1LL; | |
1645 | res->flags = IORESOURCE_BUSY | IORESOURCE_MEM; | |
1646 | if (insert_resource(&iomem_resource, res)) { | |
1647 | kfree(res); | |
1648 | return NULL; | |
1649 | } | |
1650 | return res; | |
1651 | } | |
1652 | #endif | |
1653 | ||
1654 | static struct resource* __init | |
77f8c740 | 1655 | insert_ram_resource(u64 start_pfn, u64 end_pfn, bool reserved) |
867e359b CM |
1656 | { |
1657 | struct resource *res = | |
1658 | kzalloc(sizeof(struct resource), GFP_ATOMIC); | |
6d715790 WSH |
1659 | if (!res) |
1660 | return NULL; | |
77f8c740 | 1661 | res->name = reserved ? "Reserved" : "System RAM"; |
867e359b CM |
1662 | res->start = start_pfn << PAGE_SHIFT; |
1663 | res->end = (end_pfn << PAGE_SHIFT) - 1; | |
1664 | res->flags = IORESOURCE_BUSY | IORESOURCE_MEM; | |
1665 | if (insert_resource(&iomem_resource, res)) { | |
1666 | kfree(res); | |
1667 | return NULL; | |
1668 | } | |
1669 | return res; | |
1670 | } | |
1671 | ||
1672 | /* | |
1673 | * Request address space for all standard resources | |
1674 | * | |
1675 | * If the system includes PCI root complex drivers, we need to create | |
1676 | * a window just below 4GB where PCI BARs can be mapped. | |
1677 | */ | |
1678 | static int __init request_standard_resources(void) | |
1679 | { | |
1680 | int i; | |
acbde1db | 1681 | enum { CODE_DELTA = MEM_SV_START - PAGE_OFFSET }; |
867e359b | 1682 | |
41bb38fc | 1683 | #if defined(CONFIG_PCI) && !defined(__tilegx__) |
867e359b CM |
1684 | insert_non_bus_resource(); |
1685 | #endif | |
1686 | ||
1687 | for_each_online_node(i) { | |
1688 | u64 start_pfn = node_start_pfn[i]; | |
1689 | u64 end_pfn = node_end_pfn[i]; | |
1690 | ||
41bb38fc | 1691 | #if defined(CONFIG_PCI) && !defined(__tilegx__) |
867e359b CM |
1692 | if (start_pfn <= pci_reserve_start_pfn && |
1693 | end_pfn > pci_reserve_start_pfn) { | |
1694 | if (end_pfn > pci_reserve_end_pfn) | |
1695 | insert_ram_resource(pci_reserve_end_pfn, | |
77f8c740 | 1696 | end_pfn, 0); |
867e359b CM |
1697 | end_pfn = pci_reserve_start_pfn; |
1698 | } | |
1699 | #endif | |
77f8c740 | 1700 | insert_ram_resource(start_pfn, end_pfn, 0); |
867e359b CM |
1701 | } |
1702 | ||
1703 | code_resource.start = __pa(_text - CODE_DELTA); | |
1704 | code_resource.end = __pa(_etext - CODE_DELTA)-1; | |
1705 | data_resource.start = __pa(_sdata); | |
1706 | data_resource.end = __pa(_end)-1; | |
1707 | ||
1708 | insert_resource(&iomem_resource, &code_resource); | |
1709 | insert_resource(&iomem_resource, &data_resource); | |
1710 | ||
77f8c740 CM |
1711 | /* Mark any "memmap" regions busy for the resource manager. */ |
1712 | for (i = 0; i < memmap_nr; ++i) { | |
1713 | struct memmap_entry *m = &memmap_map[i]; | |
1714 | insert_ram_resource(PFN_DOWN(m->addr), | |
1715 | PFN_UP(m->addr + m->size - 1), 1); | |
1716 | } | |
1717 | ||
867e359b CM |
1718 | #ifdef CONFIG_KEXEC |
1719 | insert_resource(&iomem_resource, &crashk_res); | |
1720 | #endif | |
1721 | ||
1722 | return 0; | |
1723 | } | |
1724 | ||
1725 | subsys_initcall(request_standard_resources); |