setup.c 44 KB

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