setup.c 46 KB

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