setup.c 43 KB

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