numa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. return 0;
  72. pad = L1_CACHE_BYTES - 1;
  73. pad_addr = 0x8000;
  74. nodemap_size = pad + memnodemapsize;
  75. nodemap_addr = find_e820_area(pad_addr, end_pfn<<PAGE_SHIFT,
  76. nodemap_size);
  77. if (nodemap_addr == -1UL) {
  78. printk(KERN_ERR
  79. "NUMA: Unable to allocate Memory to Node hash map\n");
  80. nodemap_addr = nodemap_size = 0;
  81. return -1;
  82. }
  83. pad_addr = (nodemap_addr + pad) & ~pad;
  84. memnodemap = phys_to_virt(pad_addr);
  85. printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
  86. nodemap_addr, nodemap_addr + nodemap_size);
  87. return 0;
  88. }
  89. /*
  90. * The LSB of all start and end addresses in the node map is the value of the
  91. * maximum possible shift.
  92. */
  93. static int __init
  94. extract_lsb_from_nodes (const struct bootnode *nodes, int numnodes)
  95. {
  96. int i, nodes_used = 0;
  97. unsigned long start, end;
  98. unsigned long bitfield = 0, memtop = 0;
  99. for (i = 0; i < numnodes; i++) {
  100. start = nodes[i].start;
  101. end = nodes[i].end;
  102. if (start >= end)
  103. continue;
  104. bitfield |= start;
  105. nodes_used++;
  106. if (end > memtop)
  107. memtop = end;
  108. }
  109. if (nodes_used <= 1)
  110. i = 63;
  111. else
  112. i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
  113. memnodemapsize = (memtop >> i)+1;
  114. return i;
  115. }
  116. int __init compute_hash_shift(struct bootnode *nodes, int numnodes)
  117. {
  118. int shift;
  119. shift = extract_lsb_from_nodes(nodes, numnodes);
  120. if (allocate_cachealigned_memnodemap())
  121. return -1;
  122. printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
  123. shift);
  124. if (populate_memnodemap(nodes, numnodes, shift) != 1) {
  125. printk(KERN_INFO
  126. "Your memory is not aligned you need to rebuild your kernel "
  127. "with a bigger NODEMAPSIZE shift=%d\n",
  128. shift);
  129. return -1;
  130. }
  131. return shift;
  132. }
  133. #ifdef CONFIG_SPARSEMEM
  134. int early_pfn_to_nid(unsigned long pfn)
  135. {
  136. return phys_to_nid(pfn << PAGE_SHIFT);
  137. }
  138. #endif
  139. static void * __init
  140. early_node_mem(int nodeid, unsigned long start, unsigned long end,
  141. unsigned long size)
  142. {
  143. unsigned long mem = find_e820_area(start, end, size);
  144. void *ptr;
  145. if (mem != -1L)
  146. return __va(mem);
  147. ptr = __alloc_bootmem_nopanic(size,
  148. SMP_CACHE_BYTES, __pa(MAX_DMA_ADDRESS));
  149. if (ptr == 0) {
  150. printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
  151. size, nodeid);
  152. return NULL;
  153. }
  154. return ptr;
  155. }
  156. /* Initialize bootmem allocator for a node */
  157. void __init setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
  158. {
  159. unsigned long start_pfn, end_pfn, bootmap_pages, bootmap_size, bootmap_start;
  160. unsigned long nodedata_phys;
  161. void *bootmap;
  162. const int pgdat_size = round_up(sizeof(pg_data_t), PAGE_SIZE);
  163. start = round_up(start, ZONE_ALIGN);
  164. printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid, start, end);
  165. start_pfn = start >> PAGE_SHIFT;
  166. end_pfn = end >> PAGE_SHIFT;
  167. node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size);
  168. if (node_data[nodeid] == NULL)
  169. return;
  170. nodedata_phys = __pa(node_data[nodeid]);
  171. memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
  172. NODE_DATA(nodeid)->bdata = &plat_node_bdata[nodeid];
  173. NODE_DATA(nodeid)->node_start_pfn = start_pfn;
  174. NODE_DATA(nodeid)->node_spanned_pages = end_pfn - start_pfn;
  175. /* Find a place for the bootmem map */
  176. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  177. bootmap_start = round_up(nodedata_phys + pgdat_size, PAGE_SIZE);
  178. bootmap = early_node_mem(nodeid, bootmap_start, end,
  179. bootmap_pages<<PAGE_SHIFT);
  180. if (bootmap == NULL) {
  181. if (nodedata_phys < start || nodedata_phys >= end)
  182. free_bootmem((unsigned long)node_data[nodeid],pgdat_size);
  183. node_data[nodeid] = NULL;
  184. return;
  185. }
  186. bootmap_start = __pa(bootmap);
  187. Dprintk("bootmap start %lu pages %lu\n", bootmap_start, bootmap_pages);
  188. bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
  189. bootmap_start >> PAGE_SHIFT,
  190. start_pfn, end_pfn);
  191. free_bootmem_with_active_regions(nodeid, end);
  192. reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys, pgdat_size);
  193. reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start, bootmap_pages<<PAGE_SHIFT);
  194. #ifdef CONFIG_ACPI_NUMA
  195. srat_reserve_add_area(nodeid);
  196. #endif
  197. node_set_online(nodeid);
  198. }
  199. /* Initialize final allocator for a zone */
  200. void __init setup_node_zones(int nodeid)
  201. {
  202. unsigned long start_pfn, end_pfn, memmapsize, limit;
  203. start_pfn = node_start_pfn(nodeid);
  204. end_pfn = node_end_pfn(nodeid);
  205. Dprintk(KERN_INFO "Setting up memmap for node %d %lx-%lx\n",
  206. nodeid, start_pfn, end_pfn);
  207. /* Try to allocate mem_map at end to not fill up precious <4GB
  208. memory. */
  209. memmapsize = sizeof(struct page) * (end_pfn-start_pfn);
  210. limit = end_pfn << PAGE_SHIFT;
  211. #ifdef CONFIG_FLAT_NODE_MEM_MAP
  212. NODE_DATA(nodeid)->node_mem_map =
  213. __alloc_bootmem_core(NODE_DATA(nodeid)->bdata,
  214. memmapsize, SMP_CACHE_BYTES,
  215. round_down(limit - memmapsize, PAGE_SIZE),
  216. limit);
  217. #endif
  218. }
  219. void __init numa_init_array(void)
  220. {
  221. int rr, i;
  222. /* There are unfortunately some poorly designed mainboards around
  223. that only connect memory to a single CPU. This breaks the 1:1 cpu->node
  224. mapping. To avoid this fill in the mapping for all possible
  225. CPUs, as the number of CPUs is not known yet.
  226. We round robin the existing nodes. */
  227. rr = first_node(node_online_map);
  228. for (i = 0; i < NR_CPUS; i++) {
  229. if (cpu_to_node[i] != NUMA_NO_NODE)
  230. continue;
  231. numa_set_node(i, rr);
  232. rr = next_node(rr, node_online_map);
  233. if (rr == MAX_NUMNODES)
  234. rr = first_node(node_online_map);
  235. }
  236. }
  237. #ifdef CONFIG_NUMA_EMU
  238. /* Numa emulation */
  239. int numa_fake __initdata = 0;
  240. /*
  241. * This function is used to find out if the start and end correspond to
  242. * different zones.
  243. */
  244. int zone_cross_over(unsigned long start, unsigned long end)
  245. {
  246. if ((start < (MAX_DMA32_PFN << PAGE_SHIFT)) &&
  247. (end >= (MAX_DMA32_PFN << PAGE_SHIFT)))
  248. return 1;
  249. return 0;
  250. }
  251. static int __init numa_emulation(unsigned long start_pfn, unsigned long end_pfn)
  252. {
  253. int i, big;
  254. struct bootnode nodes[MAX_NUMNODES];
  255. unsigned long sz, old_sz;
  256. unsigned long hole_size;
  257. unsigned long start, end;
  258. unsigned long max_addr = (end_pfn << PAGE_SHIFT);
  259. start = (start_pfn << PAGE_SHIFT);
  260. hole_size = e820_hole_size(start, max_addr);
  261. sz = (max_addr - start - hole_size) / numa_fake;
  262. /* Kludge needed for the hash function */
  263. old_sz = sz;
  264. /*
  265. * Round down to the nearest FAKE_NODE_MIN_SIZE.
  266. */
  267. sz &= FAKE_NODE_MIN_HASH_MASK;
  268. /*
  269. * We ensure that each node is at least 64MB big. Smaller than this
  270. * size can cause VM hiccups.
  271. */
  272. if (sz == 0) {
  273. printk(KERN_INFO "Not enough memory for %d nodes. Reducing "
  274. "the number of nodes\n", numa_fake);
  275. numa_fake = (max_addr - start - hole_size) / FAKE_NODE_MIN_SIZE;
  276. printk(KERN_INFO "Number of fake nodes will be = %d\n",
  277. numa_fake);
  278. sz = FAKE_NODE_MIN_SIZE;
  279. }
  280. /*
  281. * Find out how many nodes can get an extra NODE_MIN_SIZE granule.
  282. * This logic ensures the extra memory gets distributed among as many
  283. * nodes as possible (as compared to one single node getting all that
  284. * extra memory.
  285. */
  286. big = ((old_sz - sz) * numa_fake) / FAKE_NODE_MIN_SIZE;
  287. printk(KERN_INFO "Fake node Size: %luMB hole_size: %luMB big nodes: "
  288. "%d\n",
  289. (sz >> 20), (hole_size >> 20), big);
  290. memset(&nodes,0,sizeof(nodes));
  291. end = start;
  292. for (i = 0; i < numa_fake; i++) {
  293. /*
  294. * In case we are not able to allocate enough memory for all
  295. * the nodes, we reduce the number of fake nodes.
  296. */
  297. if (end >= max_addr) {
  298. numa_fake = i - 1;
  299. break;
  300. }
  301. start = nodes[i].start = end;
  302. /*
  303. * Final node can have all the remaining memory.
  304. */
  305. if (i == numa_fake-1)
  306. sz = max_addr - start;
  307. end = nodes[i].start + sz;
  308. /*
  309. * Fir "big" number of nodes get extra granule.
  310. */
  311. if (i < big)
  312. end += FAKE_NODE_MIN_SIZE;
  313. /*
  314. * Iterate over the range to ensure that this node gets at
  315. * least sz amount of RAM (excluding holes)
  316. */
  317. while ((end - start - e820_hole_size(start, end)) < sz) {
  318. end += FAKE_NODE_MIN_SIZE;
  319. if (end >= max_addr)
  320. break;
  321. }
  322. /*
  323. * Look at the next node to make sure there is some real memory
  324. * to map. Bad things happen when the only memory present
  325. * in a zone on a fake node is IO hole.
  326. */
  327. while (e820_hole_size(end, end + FAKE_NODE_MIN_SIZE) > 0) {
  328. if (zone_cross_over(start, end + sz)) {
  329. end = (MAX_DMA32_PFN << PAGE_SHIFT);
  330. break;
  331. }
  332. if (end >= max_addr)
  333. break;
  334. end += FAKE_NODE_MIN_SIZE;
  335. }
  336. if (end > max_addr)
  337. end = max_addr;
  338. nodes[i].end = end;
  339. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n",
  340. i,
  341. nodes[i].start, nodes[i].end,
  342. (nodes[i].end - nodes[i].start) >> 20);
  343. node_set_online(i);
  344. }
  345. memnode_shift = compute_hash_shift(nodes, numa_fake);
  346. if (memnode_shift < 0) {
  347. memnode_shift = 0;
  348. printk(KERN_ERR "No NUMA hash function found. Emulation disabled.\n");
  349. return -1;
  350. }
  351. for_each_online_node(i) {
  352. e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  353. nodes[i].end >> PAGE_SHIFT);
  354. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  355. }
  356. numa_init_array();
  357. return 0;
  358. }
  359. #endif
  360. void __init numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  361. {
  362. int i;
  363. #ifdef CONFIG_NUMA_EMU
  364. if (numa_fake && !numa_emulation(start_pfn, end_pfn))
  365. return;
  366. #endif
  367. #ifdef CONFIG_ACPI_NUMA
  368. if (!numa_off && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
  369. end_pfn << PAGE_SHIFT))
  370. return;
  371. #endif
  372. #ifdef CONFIG_K8_NUMA
  373. if (!numa_off && !k8_scan_nodes(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT))
  374. return;
  375. #endif
  376. printk(KERN_INFO "%s\n",
  377. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  378. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  379. start_pfn << PAGE_SHIFT,
  380. end_pfn << PAGE_SHIFT);
  381. /* setup dummy node covering all memory */
  382. memnode_shift = 63;
  383. memnodemap = memnode.embedded_map;
  384. memnodemap[0] = 0;
  385. nodes_clear(node_online_map);
  386. node_set_online(0);
  387. for (i = 0; i < NR_CPUS; i++)
  388. numa_set_node(i, 0);
  389. node_to_cpumask[0] = cpumask_of_cpu(0);
  390. e820_register_active_regions(0, start_pfn, end_pfn);
  391. setup_node_bootmem(0, start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
  392. }
  393. __cpuinit void numa_add_cpu(int cpu)
  394. {
  395. set_bit(cpu, &node_to_cpumask[cpu_to_node(cpu)]);
  396. }
  397. void __cpuinit numa_set_node(int cpu, int node)
  398. {
  399. cpu_pda(cpu)->nodenumber = node;
  400. cpu_to_node[cpu] = node;
  401. }
  402. unsigned long __init numa_free_all_bootmem(void)
  403. {
  404. int i;
  405. unsigned long pages = 0;
  406. for_each_online_node(i) {
  407. pages += free_all_bootmem_node(NODE_DATA(i));
  408. }
  409. return pages;
  410. }
  411. void __init paging_init(void)
  412. {
  413. int i;
  414. unsigned long max_zone_pfns[MAX_NR_ZONES];
  415. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  416. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  417. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  418. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  419. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  420. sparse_init();
  421. for_each_online_node(i) {
  422. setup_node_zones(i);
  423. }
  424. free_area_init_nodes(max_zone_pfns);
  425. }
  426. static __init int numa_setup(char *opt)
  427. {
  428. if (!opt)
  429. return -EINVAL;
  430. if (!strncmp(opt,"off",3))
  431. numa_off = 1;
  432. #ifdef CONFIG_NUMA_EMU
  433. if(!strncmp(opt, "fake=", 5)) {
  434. numa_fake = simple_strtoul(opt+5,NULL,0); ;
  435. if (numa_fake >= MAX_NUMNODES)
  436. numa_fake = MAX_NUMNODES;
  437. }
  438. #endif
  439. #ifdef CONFIG_ACPI_NUMA
  440. if (!strncmp(opt,"noacpi",6))
  441. acpi_numa = -1;
  442. if (!strncmp(opt,"hotadd=", 7))
  443. hotadd_percent = simple_strtoul(opt+7, NULL, 10);
  444. #endif
  445. return 0;
  446. }
  447. early_param("numa", numa_setup);
  448. /*
  449. * Setup early cpu_to_node.
  450. *
  451. * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
  452. * and apicid_to_node[] tables have valid entries for a CPU.
  453. * This means we skip cpu_to_node[] initialisation for NUMA
  454. * emulation and faking node case (when running a kernel compiled
  455. * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
  456. * is already initialized in a round robin manner at numa_init_array,
  457. * prior to this call, and this initialization is good enough
  458. * for the fake NUMA cases.
  459. */
  460. void __init init_cpu_to_node(void)
  461. {
  462. int i;
  463. for (i = 0; i < NR_CPUS; i++) {
  464. u8 apicid = x86_cpu_to_apicid[i];
  465. if (apicid == BAD_APICID)
  466. continue;
  467. if (apicid_to_node[apicid] == NUMA_NO_NODE)
  468. continue;
  469. numa_set_node(i,apicid_to_node[apicid]);
  470. }
  471. }
  472. EXPORT_SYMBOL(cpu_to_node);
  473. EXPORT_SYMBOL(node_to_cpumask);
  474. EXPORT_SYMBOL(memnode);
  475. EXPORT_SYMBOL(node_data);
  476. #ifdef CONFIG_DISCONTIGMEM
  477. /*
  478. * Functions to convert PFNs from/to per node page addresses.
  479. * These are out of line because they are quite big.
  480. * They could be all tuned by pre caching more state.
  481. * Should do that.
  482. */
  483. int pfn_valid(unsigned long pfn)
  484. {
  485. unsigned nid;
  486. if (pfn >= num_physpages)
  487. return 0;
  488. nid = pfn_to_nid(pfn);
  489. if (nid == 0xff)
  490. return 0;
  491. return pfn >= node_start_pfn(nid) && (pfn) < node_end_pfn(nid);
  492. }
  493. EXPORT_SYMBOL(pfn_valid);
  494. #endif