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