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