setup.c 43 KB

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