setup.c 46 KB

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