numa.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Generic VM initialization for x86-64 NUMA setups.
  3. * Copyright 2002,2003 Andi Kleen, SuSE Labs.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <linux/string.h>
  8. #include <linux/init.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/mmzone.h>
  11. #include <linux/ctype.h>
  12. #include <linux/module.h>
  13. #include <linux/nodemask.h>
  14. #include <asm/e820.h>
  15. #include <asm/proto.h>
  16. #include <asm/dma.h>
  17. #include <asm/numa.h>
  18. #include <asm/acpi.h>
  19. #ifndef Dprintk
  20. #define Dprintk(x...)
  21. #endif
  22. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  23. bootmem_data_t plat_node_bdata[MAX_NUMNODES];
  24. struct memnode memnode;
  25. unsigned char cpu_to_node[NR_CPUS] __read_mostly = {
  26. [0 ... NR_CPUS-1] = NUMA_NO_NODE
  27. };
  28. unsigned char apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
  29. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  30. };
  31. cpumask_t node_to_cpumask[MAX_NUMNODES] __read_mostly;
  32. int numa_off __initdata;
  33. unsigned long __initdata nodemap_addr;
  34. unsigned long __initdata nodemap_size;
  35. /*
  36. * Given a shift value, try to populate memnodemap[]
  37. * Returns :
  38. * 1 if OK
  39. * 0 if memnodmap[] too small (of shift too small)
  40. * -1 if node overlap or lost ram (shift too big)
  41. */
  42. static int __init
  43. populate_memnodemap(const struct bootnode *nodes, int numnodes, int shift)
  44. {
  45. int i;
  46. int res = -1;
  47. unsigned long addr, end;
  48. memset(memnodemap, 0xff, memnodemapsize);
  49. for (i = 0; i < numnodes; i++) {
  50. addr = nodes[i].start;
  51. end = nodes[i].end;
  52. if (addr >= end)
  53. continue;
  54. if ((end >> shift) >= memnodemapsize)
  55. return 0;
  56. do {
  57. if (memnodemap[addr >> shift] != 0xff)
  58. return -1;
  59. memnodemap[addr >> shift] = i;
  60. addr += (1UL << shift);
  61. } while (addr < end);
  62. res = 1;
  63. }
  64. return res;
  65. }
  66. static int __init allocate_cachealigned_memnodemap(void)
  67. {
  68. unsigned long pad, pad_addr;
  69. memnodemap = memnode.embedded_map;
  70. if (memnodemapsize <= 48) {
  71. printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
  72. nodemap_addr, nodemap_addr + nodemap_size);
  73. return 0;
  74. }
  75. pad = L1_CACHE_BYTES - 1;
  76. pad_addr = 0x8000;
  77. nodemap_size = pad + memnodemapsize;
  78. nodemap_addr = find_e820_area(pad_addr, end_pfn<<PAGE_SHIFT,
  79. nodemap_size);
  80. if (nodemap_addr == -1UL) {
  81. printk(KERN_ERR
  82. "NUMA: Unable to allocate Memory to Node hash map\n");
  83. nodemap_addr = nodemap_size = 0;
  84. return -1;
  85. }
  86. pad_addr = (nodemap_addr + pad) & ~pad;
  87. memnodemap = phys_to_virt(pad_addr);
  88. printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
  89. nodemap_addr, nodemap_addr + nodemap_size);
  90. return 0;
  91. }
  92. /*
  93. * The LSB of all start and end addresses in the node map is the value of the
  94. * maximum possible shift.
  95. */
  96. static int __init
  97. extract_lsb_from_nodes (const struct bootnode *nodes, int numnodes)
  98. {
  99. int i;
  100. unsigned long start, end;
  101. unsigned long bitfield = 0, memtop = 0;
  102. for (i = 0; i < numnodes; i++) {
  103. start = nodes[i].start;
  104. end = nodes[i].end;
  105. if (start >= end)
  106. continue;
  107. bitfield |= start | end;
  108. if (end > memtop)
  109. memtop = end;
  110. }
  111. i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
  112. memnodemapsize = (memtop >> i)+1;
  113. return i;
  114. }
  115. int __init compute_hash_shift(struct bootnode *nodes, int numnodes)
  116. {
  117. int shift;
  118. shift = extract_lsb_from_nodes(nodes, numnodes);
  119. if (allocate_cachealigned_memnodemap())
  120. return -1;
  121. printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
  122. shift);
  123. if (populate_memnodemap(nodes, numnodes, shift) != 1) {
  124. printk(KERN_INFO
  125. "Your memory is not aligned you need to rebuild your kernel "
  126. "with a bigger NODEMAPSIZE shift=%d\n",
  127. shift);
  128. return -1;
  129. }
  130. return shift;
  131. }
  132. #ifdef CONFIG_SPARSEMEM
  133. int early_pfn_to_nid(unsigned long pfn)
  134. {
  135. return phys_to_nid(pfn << PAGE_SHIFT);
  136. }
  137. #endif
  138. static void * __init
  139. early_node_mem(int nodeid, unsigned long start, unsigned long end,
  140. unsigned long size)
  141. {
  142. unsigned long mem = find_e820_area(start, end, size);
  143. void *ptr;
  144. if (mem != -1L)
  145. return __va(mem);
  146. ptr = __alloc_bootmem_nopanic(size,
  147. SMP_CACHE_BYTES, __pa(MAX_DMA_ADDRESS));
  148. if (ptr == 0) {
  149. printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
  150. size, nodeid);
  151. return NULL;
  152. }
  153. return ptr;
  154. }
  155. /* Initialize bootmem allocator for a node */
  156. void __init setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
  157. {
  158. unsigned long start_pfn, end_pfn, bootmap_pages, bootmap_size, bootmap_start;
  159. unsigned long nodedata_phys;
  160. void *bootmap;
  161. const int pgdat_size = round_up(sizeof(pg_data_t), PAGE_SIZE);
  162. start = round_up(start, ZONE_ALIGN);
  163. printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid, start, end);
  164. start_pfn = start >> PAGE_SHIFT;
  165. end_pfn = end >> PAGE_SHIFT;
  166. node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size);
  167. if (node_data[nodeid] == NULL)
  168. return;
  169. nodedata_phys = __pa(node_data[nodeid]);
  170. memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
  171. NODE_DATA(nodeid)->bdata = &plat_node_bdata[nodeid];
  172. NODE_DATA(nodeid)->node_start_pfn = start_pfn;
  173. NODE_DATA(nodeid)->node_spanned_pages = end_pfn - start_pfn;
  174. /* Find a place for the bootmem map */
  175. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  176. bootmap_start = round_up(nodedata_phys + pgdat_size, PAGE_SIZE);
  177. bootmap = early_node_mem(nodeid, bootmap_start, end,
  178. bootmap_pages<<PAGE_SHIFT);
  179. if (bootmap == NULL) {
  180. if (nodedata_phys < start || nodedata_phys >= end)
  181. free_bootmem((unsigned long)node_data[nodeid],pgdat_size);
  182. node_data[nodeid] = NULL;
  183. return;
  184. }
  185. bootmap_start = __pa(bootmap);
  186. Dprintk("bootmap start %lu pages %lu\n", bootmap_start, bootmap_pages);
  187. bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
  188. bootmap_start >> PAGE_SHIFT,
  189. start_pfn, end_pfn);
  190. free_bootmem_with_active_regions(nodeid, end);
  191. reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys, pgdat_size);
  192. reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start, bootmap_pages<<PAGE_SHIFT);
  193. #ifdef CONFIG_ACPI_NUMA
  194. srat_reserve_add_area(nodeid);
  195. #endif
  196. node_set_online(nodeid);
  197. }
  198. /* Initialize final allocator for a zone */
  199. void __init setup_node_zones(int nodeid)
  200. {
  201. unsigned long start_pfn, end_pfn, memmapsize, limit;
  202. start_pfn = node_start_pfn(nodeid);
  203. end_pfn = node_end_pfn(nodeid);
  204. Dprintk(KERN_INFO "Setting up memmap for node %d %lx-%lx\n",
  205. nodeid, start_pfn, end_pfn);
  206. /* Try to allocate mem_map at end to not fill up precious <4GB
  207. memory. */
  208. memmapsize = sizeof(struct page) * (end_pfn-start_pfn);
  209. limit = end_pfn << PAGE_SHIFT;
  210. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  211. NODE_DATA(nodeid)->node_mem_map =
  212. __alloc_bootmem_core(NODE_DATA(nodeid)->bdata,
  213. memmapsize, SMP_CACHE_BYTES,
  214. round_down(limit - memmapsize, PAGE_SIZE),
  215. limit);
  216. #endif
  217. }
  218. void __init numa_init_array(void)
  219. {
  220. int rr, i;
  221. /* There are unfortunately some poorly designed mainboards around
  222. that only connect memory to a single CPU. This breaks the 1:1 cpu->node
  223. mapping. To avoid this fill in the mapping for all possible
  224. CPUs, as the number of CPUs is not known yet.
  225. We round robin the existing nodes. */
  226. rr = first_node(node_online_map);
  227. for (i = 0; i < NR_CPUS; i++) {
  228. if (cpu_to_node[i] != NUMA_NO_NODE)
  229. continue;
  230. numa_set_node(i, rr);
  231. rr = next_node(rr, node_online_map);
  232. if (rr == MAX_NUMNODES)
  233. rr = first_node(node_online_map);
  234. }
  235. }
  236. #ifdef CONFIG_NUMA_EMU
  237. int numa_fake __initdata = 0;
  238. /* Numa emulation */
  239. static int __init numa_emulation(unsigned long start_pfn, unsigned long end_pfn)
  240. {
  241. int i;
  242. struct bootnode nodes[MAX_NUMNODES];
  243. unsigned long sz = ((end_pfn - start_pfn)<<PAGE_SHIFT) / numa_fake;
  244. /* Kludge needed for the hash function */
  245. if (hweight64(sz) > 1) {
  246. unsigned long x = 1;
  247. while ((x << 1) < sz)
  248. x <<= 1;
  249. if (x < sz/2)
  250. printk(KERN_ERR "Numa emulation unbalanced. Complain to maintainer\n");
  251. sz = x;
  252. }
  253. memset(&nodes,0,sizeof(nodes));
  254. for (i = 0; i < numa_fake; i++) {
  255. nodes[i].start = (start_pfn<<PAGE_SHIFT) + i*sz;
  256. if (i == numa_fake-1)
  257. sz = (end_pfn<<PAGE_SHIFT) - nodes[i].start;
  258. nodes[i].end = nodes[i].start + sz;
  259. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n",
  260. i,
  261. nodes[i].start, nodes[i].end,
  262. (nodes[i].end - nodes[i].start) >> 20);
  263. node_set_online(i);
  264. }
  265. memnode_shift = compute_hash_shift(nodes, numa_fake);
  266. if (memnode_shift < 0) {
  267. memnode_shift = 0;
  268. printk(KERN_ERR "No NUMA hash function found. Emulation disabled.\n");
  269. return -1;
  270. }
  271. for_each_online_node(i) {
  272. e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  273. nodes[i].end >> PAGE_SHIFT);
  274. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  275. }
  276. numa_init_array();
  277. return 0;
  278. }
  279. #endif
  280. void __init numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  281. {
  282. int i;
  283. #ifdef CONFIG_NUMA_EMU
  284. if (numa_fake && !numa_emulation(start_pfn, end_pfn))
  285. return;
  286. #endif
  287. #ifdef CONFIG_ACPI_NUMA
  288. if (!numa_off && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
  289. end_pfn << PAGE_SHIFT))
  290. return;
  291. #endif
  292. #ifdef CONFIG_K8_NUMA
  293. if (!numa_off && !k8_scan_nodes(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT))
  294. return;
  295. #endif
  296. printk(KERN_INFO "%s\n",
  297. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  298. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  299. start_pfn << PAGE_SHIFT,
  300. end_pfn << PAGE_SHIFT);
  301. /* setup dummy node covering all memory */
  302. memnode_shift = 63;
  303. memnodemap = memnode.embedded_map;
  304. memnodemap[0] = 0;
  305. nodes_clear(node_online_map);
  306. node_set_online(0);
  307. for (i = 0; i < NR_CPUS; i++)
  308. numa_set_node(i, 0);
  309. node_to_cpumask[0] = cpumask_of_cpu(0);
  310. e820_register_active_regions(0, start_pfn, end_pfn);
  311. setup_node_bootmem(0, start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
  312. }
  313. __cpuinit void numa_add_cpu(int cpu)
  314. {
  315. set_bit(cpu, &node_to_cpumask[cpu_to_node(cpu)]);
  316. }
  317. void __cpuinit numa_set_node(int cpu, int node)
  318. {
  319. cpu_pda(cpu)->nodenumber = node;
  320. cpu_to_node[cpu] = node;
  321. }
  322. unsigned long __init numa_free_all_bootmem(void)
  323. {
  324. int i;
  325. unsigned long pages = 0;
  326. for_each_online_node(i) {
  327. pages += free_all_bootmem_node(NODE_DATA(i));
  328. }
  329. return pages;
  330. }
  331. #ifdef CONFIG_SPARSEMEM
  332. static void __init arch_sparse_init(void)
  333. {
  334. int i;
  335. for_each_online_node(i)
  336. memory_present(i, node_start_pfn(i), node_end_pfn(i));
  337. sparse_init();
  338. }
  339. #else
  340. #define arch_sparse_init() do {} while (0)
  341. #endif
  342. void __init paging_init(void)
  343. {
  344. int i;
  345. unsigned long max_zone_pfns[MAX_NR_ZONES];
  346. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  347. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  348. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  349. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  350. arch_sparse_init();
  351. for_each_online_node(i) {
  352. setup_node_zones(i);
  353. }
  354. free_area_init_nodes(max_zone_pfns);
  355. }
  356. static __init int numa_setup(char *opt)
  357. {
  358. if (!opt)
  359. return -EINVAL;
  360. if (!strncmp(opt,"off",3))
  361. numa_off = 1;
  362. #ifdef CONFIG_NUMA_EMU
  363. if(!strncmp(opt, "fake=", 5)) {
  364. numa_fake = simple_strtoul(opt+5,NULL,0); ;
  365. if (numa_fake >= MAX_NUMNODES)
  366. numa_fake = MAX_NUMNODES;
  367. }
  368. #endif
  369. #ifdef CONFIG_ACPI_NUMA
  370. if (!strncmp(opt,"noacpi",6))
  371. acpi_numa = -1;
  372. if (!strncmp(opt,"hotadd=", 7))
  373. hotadd_percent = simple_strtoul(opt+7, NULL, 10);
  374. #endif
  375. return 0;
  376. }
  377. early_param("numa", numa_setup);
  378. /*
  379. * Setup early cpu_to_node.
  380. *
  381. * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
  382. * and apicid_to_node[] tables have valid entries for a CPU.
  383. * This means we skip cpu_to_node[] initialisation for NUMA
  384. * emulation and faking node case (when running a kernel compiled
  385. * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
  386. * is already initialized in a round robin manner at numa_init_array,
  387. * prior to this call, and this initialization is good enough
  388. * for the fake NUMA cases.
  389. */
  390. void __init init_cpu_to_node(void)
  391. {
  392. int i;
  393. for (i = 0; i < NR_CPUS; i++) {
  394. u8 apicid = x86_cpu_to_apicid[i];
  395. if (apicid == BAD_APICID)
  396. continue;
  397. if (apicid_to_node[apicid] == NUMA_NO_NODE)
  398. continue;
  399. numa_set_node(i,apicid_to_node[apicid]);
  400. }
  401. }
  402. EXPORT_SYMBOL(cpu_to_node);
  403. EXPORT_SYMBOL(node_to_cpumask);
  404. EXPORT_SYMBOL(memnode);
  405. EXPORT_SYMBOL(node_data);
  406. #ifdef CONFIG_DISCONTIGMEM
  407. /*
  408. * Functions to convert PFNs from/to per node page addresses.
  409. * These are out of line because they are quite big.
  410. * They could be all tuned by pre caching more state.
  411. * Should do that.
  412. */
  413. int pfn_valid(unsigned long pfn)
  414. {
  415. unsigned nid;
  416. if (pfn >= num_physpages)
  417. return 0;
  418. nid = pfn_to_nid(pfn);
  419. if (nid == 0xff)
  420. return 0;
  421. return pfn >= node_start_pfn(nid) && (pfn) < node_end_pfn(nid);
  422. }
  423. EXPORT_SYMBOL(pfn_valid);
  424. #endif