setup.c 45 KB

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