setup.c 44 KB

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