numa.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. int memnode_shift;
  25. u8 memnodemap[NODEMAPSIZE];
  26. unsigned char cpu_to_node[NR_CPUS] __read_mostly = {
  27. [0 ... NR_CPUS-1] = NUMA_NO_NODE
  28. };
  29. unsigned char apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
  30. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  31. };
  32. cpumask_t node_to_cpumask[MAX_NUMNODES] __read_mostly;
  33. int numa_off __initdata;
  34. int __init compute_hash_shift(struct node *nodes, int numnodes)
  35. {
  36. int i;
  37. int shift = 20;
  38. unsigned long addr,maxend=0;
  39. for (i = 0; i < numnodes; i++)
  40. if ((nodes[i].start != nodes[i].end) && (nodes[i].end > maxend))
  41. maxend = nodes[i].end;
  42. while ((1UL << shift) < (maxend / NODEMAPSIZE))
  43. shift++;
  44. printk (KERN_DEBUG"Using %d for the hash shift. Max adder is %lx \n",
  45. shift,maxend);
  46. memset(memnodemap,0xff,sizeof(*memnodemap) * NODEMAPSIZE);
  47. for (i = 0; i < numnodes; i++) {
  48. if (nodes[i].start == nodes[i].end)
  49. continue;
  50. for (addr = nodes[i].start;
  51. addr < nodes[i].end;
  52. addr += (1UL << shift)) {
  53. if (memnodemap[addr >> shift] != 0xff) {
  54. printk(KERN_INFO
  55. "Your memory is not aligned you need to rebuild your kernel "
  56. "with a bigger NODEMAPSIZE shift=%d adder=%lu\n",
  57. shift,addr);
  58. return -1;
  59. }
  60. memnodemap[addr >> shift] = i;
  61. }
  62. }
  63. return shift;
  64. }
  65. #ifdef CONFIG_SPARSEMEM
  66. int early_pfn_to_nid(unsigned long pfn)
  67. {
  68. return phys_to_nid(pfn << PAGE_SHIFT);
  69. }
  70. #endif
  71. /* Initialize bootmem allocator for a node */
  72. void __init setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
  73. {
  74. unsigned long start_pfn, end_pfn, bootmap_pages, bootmap_size, bootmap_start;
  75. unsigned long nodedata_phys;
  76. const int pgdat_size = round_up(sizeof(pg_data_t), PAGE_SIZE);
  77. start = round_up(start, ZONE_ALIGN);
  78. printk("Bootmem setup node %d %016lx-%016lx\n", nodeid, start, end);
  79. start_pfn = start >> PAGE_SHIFT;
  80. end_pfn = end >> PAGE_SHIFT;
  81. memory_present(nodeid, start_pfn, end_pfn);
  82. nodedata_phys = find_e820_area(start, end, pgdat_size);
  83. if (nodedata_phys == -1L)
  84. panic("Cannot find memory pgdat in node %d\n", nodeid);
  85. Dprintk("nodedata_phys %lx\n", nodedata_phys);
  86. node_data[nodeid] = phys_to_virt(nodedata_phys);
  87. memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
  88. NODE_DATA(nodeid)->bdata = &plat_node_bdata[nodeid];
  89. NODE_DATA(nodeid)->node_start_pfn = start_pfn;
  90. NODE_DATA(nodeid)->node_spanned_pages = end_pfn - start_pfn;
  91. /* Find a place for the bootmem map */
  92. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  93. bootmap_start = round_up(nodedata_phys + pgdat_size, PAGE_SIZE);
  94. bootmap_start = find_e820_area(bootmap_start, end, bootmap_pages<<PAGE_SHIFT);
  95. if (bootmap_start == -1L)
  96. panic("Not enough continuous space for bootmap on node %d", nodeid);
  97. Dprintk("bootmap start %lu pages %lu\n", bootmap_start, bootmap_pages);
  98. bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
  99. bootmap_start >> PAGE_SHIFT,
  100. start_pfn, end_pfn);
  101. e820_bootmem_free(NODE_DATA(nodeid), start, end);
  102. reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys, pgdat_size);
  103. reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start, bootmap_pages<<PAGE_SHIFT);
  104. node_set_online(nodeid);
  105. }
  106. /* Initialize final allocator for a zone */
  107. void __init setup_node_zones(int nodeid)
  108. {
  109. unsigned long start_pfn, end_pfn;
  110. unsigned long zones[MAX_NR_ZONES];
  111. unsigned long holes[MAX_NR_ZONES];
  112. start_pfn = node_start_pfn(nodeid);
  113. end_pfn = node_end_pfn(nodeid);
  114. Dprintk(KERN_INFO "setting up node %d %lx-%lx\n",
  115. nodeid, start_pfn, end_pfn);
  116. size_zones(zones, holes, start_pfn, end_pfn);
  117. free_area_init_node(nodeid, NODE_DATA(nodeid), zones,
  118. start_pfn, holes);
  119. }
  120. void __init numa_init_array(void)
  121. {
  122. int rr, i;
  123. /* There are unfortunately some poorly designed mainboards around
  124. that only connect memory to a single CPU. This breaks the 1:1 cpu->node
  125. mapping. To avoid this fill in the mapping for all possible
  126. CPUs, as the number of CPUs is not known yet.
  127. We round robin the existing nodes. */
  128. rr = first_node(node_online_map);
  129. for (i = 0; i < NR_CPUS; i++) {
  130. if (cpu_to_node[i] != NUMA_NO_NODE)
  131. continue;
  132. cpu_to_node[i] = rr;
  133. rr = next_node(rr, node_online_map);
  134. if (rr == MAX_NUMNODES)
  135. rr = first_node(node_online_map);
  136. }
  137. }
  138. #ifdef CONFIG_NUMA_EMU
  139. int numa_fake __initdata = 0;
  140. /* Numa emulation */
  141. static int numa_emulation(unsigned long start_pfn, unsigned long end_pfn)
  142. {
  143. int i;
  144. struct node nodes[MAX_NUMNODES];
  145. unsigned long sz = ((end_pfn - start_pfn)<<PAGE_SHIFT) / numa_fake;
  146. /* Kludge needed for the hash function */
  147. if (hweight64(sz) > 1) {
  148. unsigned long x = 1;
  149. while ((x << 1) < sz)
  150. x <<= 1;
  151. if (x < sz/2)
  152. printk("Numa emulation unbalanced. Complain to maintainer\n");
  153. sz = x;
  154. }
  155. memset(&nodes,0,sizeof(nodes));
  156. for (i = 0; i < numa_fake; i++) {
  157. nodes[i].start = (start_pfn<<PAGE_SHIFT) + i*sz;
  158. if (i == numa_fake-1)
  159. sz = (end_pfn<<PAGE_SHIFT) - nodes[i].start;
  160. nodes[i].end = nodes[i].start + sz;
  161. if (i != numa_fake-1)
  162. nodes[i].end--;
  163. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n",
  164. i,
  165. nodes[i].start, nodes[i].end,
  166. (nodes[i].end - nodes[i].start) >> 20);
  167. node_set_online(i);
  168. }
  169. memnode_shift = compute_hash_shift(nodes, numa_fake);
  170. if (memnode_shift < 0) {
  171. memnode_shift = 0;
  172. printk(KERN_ERR "No NUMA hash function found. Emulation disabled.\n");
  173. return -1;
  174. }
  175. for_each_online_node(i)
  176. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  177. numa_init_array();
  178. return 0;
  179. }
  180. #endif
  181. void __init numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  182. {
  183. int i;
  184. #ifdef CONFIG_NUMA_EMU
  185. if (numa_fake && !numa_emulation(start_pfn, end_pfn))
  186. return;
  187. #endif
  188. #ifdef CONFIG_ACPI_NUMA
  189. if (!numa_off && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
  190. end_pfn << PAGE_SHIFT))
  191. return;
  192. #endif
  193. #ifdef CONFIG_K8_NUMA
  194. if (!numa_off && !k8_scan_nodes(start_pfn<<PAGE_SHIFT, end_pfn<<PAGE_SHIFT))
  195. return;
  196. #endif
  197. printk(KERN_INFO "%s\n",
  198. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  199. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  200. start_pfn << PAGE_SHIFT,
  201. end_pfn << PAGE_SHIFT);
  202. /* setup dummy node covering all memory */
  203. memnode_shift = 63;
  204. memnodemap[0] = 0;
  205. nodes_clear(node_online_map);
  206. node_set_online(0);
  207. for (i = 0; i < NR_CPUS; i++)
  208. cpu_to_node[i] = 0;
  209. node_to_cpumask[0] = cpumask_of_cpu(0);
  210. setup_node_bootmem(0, start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
  211. }
  212. __cpuinit void numa_add_cpu(int cpu)
  213. {
  214. set_bit(cpu, &node_to_cpumask[cpu_to_node(cpu)]);
  215. }
  216. unsigned long __init numa_free_all_bootmem(void)
  217. {
  218. int i;
  219. unsigned long pages = 0;
  220. for_each_online_node(i) {
  221. pages += free_all_bootmem_node(NODE_DATA(i));
  222. }
  223. return pages;
  224. }
  225. void __init paging_init(void)
  226. {
  227. int i;
  228. for_each_online_node(i) {
  229. setup_node_zones(i);
  230. }
  231. }
  232. /* [numa=off] */
  233. __init int numa_setup(char *opt)
  234. {
  235. if (!strncmp(opt,"off",3))
  236. numa_off = 1;
  237. #ifdef CONFIG_NUMA_EMU
  238. if(!strncmp(opt, "fake=", 5)) {
  239. numa_fake = simple_strtoul(opt+5,NULL,0); ;
  240. if (numa_fake >= MAX_NUMNODES)
  241. numa_fake = MAX_NUMNODES;
  242. }
  243. #endif
  244. #ifdef CONFIG_ACPI_NUMA
  245. if (!strncmp(opt,"noacpi",6))
  246. acpi_numa = -1;
  247. #endif
  248. return 1;
  249. }
  250. EXPORT_SYMBOL(cpu_to_node);
  251. EXPORT_SYMBOL(node_to_cpumask);
  252. EXPORT_SYMBOL(memnode_shift);
  253. EXPORT_SYMBOL(memnodemap);
  254. EXPORT_SYMBOL(node_data);