setup.c 46 KB

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