numa_64.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 <linux/sched.h>
  15. #include <asm/e820.h>
  16. #include <asm/proto.h>
  17. #include <asm/dma.h>
  18. #include <asm/numa.h>
  19. #include <asm/acpi.h>
  20. #include <asm/k8.h>
  21. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  22. # define DBG(x...) printk(KERN_DEBUG x)
  23. #else
  24. # define DBG(x...)
  25. #endif
  26. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  27. EXPORT_SYMBOL(node_data);
  28. struct memnode memnode;
  29. s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
  30. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  31. };
  32. int numa_off __initdata;
  33. static unsigned long __initdata nodemap_addr;
  34. static unsigned long __initdata nodemap_size;
  35. DEFINE_PER_CPU(int, node_number) = 0;
  36. EXPORT_PER_CPU_SYMBOL(node_number);
  37. /*
  38. * Map cpu index to node index
  39. */
  40. DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
  41. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
  42. /*
  43. * Which logical CPUs are on which nodes
  44. */
  45. cpumask_t *node_to_cpumask_map;
  46. EXPORT_SYMBOL(node_to_cpumask_map);
  47. /*
  48. * Given a shift value, try to populate memnodemap[]
  49. * Returns :
  50. * 1 if OK
  51. * 0 if memnodmap[] too small (of shift too small)
  52. * -1 if node overlap or lost ram (shift too big)
  53. */
  54. static int __init populate_memnodemap(const struct bootnode *nodes,
  55. int numnodes, int shift, int *nodeids)
  56. {
  57. unsigned long addr, end;
  58. int i, res = -1;
  59. memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
  60. for (i = 0; i < numnodes; i++) {
  61. addr = nodes[i].start;
  62. end = nodes[i].end;
  63. if (addr >= end)
  64. continue;
  65. if ((end >> shift) >= memnodemapsize)
  66. return 0;
  67. do {
  68. if (memnodemap[addr >> shift] != NUMA_NO_NODE)
  69. return -1;
  70. if (!nodeids)
  71. memnodemap[addr >> shift] = i;
  72. else
  73. memnodemap[addr >> shift] = nodeids[i];
  74. addr += (1UL << shift);
  75. } while (addr < end);
  76. res = 1;
  77. }
  78. return res;
  79. }
  80. static int __init allocate_cachealigned_memnodemap(void)
  81. {
  82. unsigned long addr;
  83. memnodemap = memnode.embedded_map;
  84. if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
  85. return 0;
  86. addr = 0x8000;
  87. nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
  88. nodemap_addr = find_e820_area(addr, max_pfn<<PAGE_SHIFT,
  89. nodemap_size, L1_CACHE_BYTES);
  90. if (nodemap_addr == -1UL) {
  91. printk(KERN_ERR
  92. "NUMA: Unable to allocate Memory to Node hash map\n");
  93. nodemap_addr = nodemap_size = 0;
  94. return -1;
  95. }
  96. memnodemap = phys_to_virt(nodemap_addr);
  97. reserve_early(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
  98. printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
  99. nodemap_addr, nodemap_addr + nodemap_size);
  100. return 0;
  101. }
  102. /*
  103. * The LSB of all start and end addresses in the node map is the value of the
  104. * maximum possible shift.
  105. */
  106. static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
  107. int numnodes)
  108. {
  109. int i, nodes_used = 0;
  110. unsigned long start, end;
  111. unsigned long bitfield = 0, memtop = 0;
  112. for (i = 0; i < numnodes; i++) {
  113. start = nodes[i].start;
  114. end = nodes[i].end;
  115. if (start >= end)
  116. continue;
  117. bitfield |= start;
  118. nodes_used++;
  119. if (end > memtop)
  120. memtop = end;
  121. }
  122. if (nodes_used <= 1)
  123. i = 63;
  124. else
  125. i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
  126. memnodemapsize = (memtop >> i)+1;
  127. return i;
  128. }
  129. int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
  130. int *nodeids)
  131. {
  132. int shift;
  133. shift = extract_lsb_from_nodes(nodes, numnodes);
  134. if (allocate_cachealigned_memnodemap())
  135. return -1;
  136. printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
  137. shift);
  138. if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
  139. printk(KERN_INFO "Your memory is not aligned you need to "
  140. "rebuild your kernel with a bigger NODEMAPSIZE "
  141. "shift=%d\n", shift);
  142. return -1;
  143. }
  144. return shift;
  145. }
  146. int __meminit __early_pfn_to_nid(unsigned long pfn)
  147. {
  148. return phys_to_nid(pfn << PAGE_SHIFT);
  149. }
  150. static void * __init early_node_mem(int nodeid, unsigned long start,
  151. unsigned long end, unsigned long size,
  152. unsigned long align)
  153. {
  154. unsigned long mem = find_e820_area(start, end, size, align);
  155. void *ptr;
  156. if (mem != -1L)
  157. return __va(mem);
  158. ptr = __alloc_bootmem_nopanic(size, align, __pa(MAX_DMA_ADDRESS));
  159. if (ptr == NULL) {
  160. printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
  161. size, nodeid);
  162. return NULL;
  163. }
  164. return ptr;
  165. }
  166. /* Initialize bootmem allocator for a node */
  167. void __init setup_node_bootmem(int nodeid, unsigned long start,
  168. unsigned long end)
  169. {
  170. unsigned long start_pfn, last_pfn, bootmap_pages, bootmap_size;
  171. unsigned long bootmap_start, nodedata_phys;
  172. void *bootmap;
  173. const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
  174. int nid;
  175. start = roundup(start, ZONE_ALIGN);
  176. printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid,
  177. start, end);
  178. start_pfn = start >> PAGE_SHIFT;
  179. last_pfn = end >> PAGE_SHIFT;
  180. node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
  181. SMP_CACHE_BYTES);
  182. if (node_data[nodeid] == NULL)
  183. return;
  184. nodedata_phys = __pa(node_data[nodeid]);
  185. printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
  186. nodedata_phys + pgdat_size - 1);
  187. memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
  188. NODE_DATA(nodeid)->bdata = &bootmem_node_data[nodeid];
  189. NODE_DATA(nodeid)->node_start_pfn = start_pfn;
  190. NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
  191. /*
  192. * Find a place for the bootmem map
  193. * nodedata_phys could be on other nodes by alloc_bootmem,
  194. * so need to sure bootmap_start not to be small, otherwise
  195. * early_node_mem will get that with find_e820_area instead
  196. * of alloc_bootmem, that could clash with reserved range
  197. */
  198. bootmap_pages = bootmem_bootmap_pages(last_pfn - start_pfn);
  199. nid = phys_to_nid(nodedata_phys);
  200. if (nid == nodeid)
  201. bootmap_start = roundup(nodedata_phys + pgdat_size, PAGE_SIZE);
  202. else
  203. bootmap_start = roundup(start, PAGE_SIZE);
  204. /*
  205. * SMP_CACHE_BYTES could be enough, but init_bootmem_node like
  206. * to use that to align to PAGE_SIZE
  207. */
  208. bootmap = early_node_mem(nodeid, bootmap_start, end,
  209. bootmap_pages<<PAGE_SHIFT, PAGE_SIZE);
  210. if (bootmap == NULL) {
  211. if (nodedata_phys < start || nodedata_phys >= end)
  212. free_bootmem(nodedata_phys, pgdat_size);
  213. node_data[nodeid] = NULL;
  214. return;
  215. }
  216. bootmap_start = __pa(bootmap);
  217. bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
  218. bootmap_start >> PAGE_SHIFT,
  219. start_pfn, last_pfn);
  220. printk(KERN_INFO " bootmap [%016lx - %016lx] pages %lx\n",
  221. bootmap_start, bootmap_start + bootmap_size - 1,
  222. bootmap_pages);
  223. free_bootmem_with_active_regions(nodeid, end);
  224. /*
  225. * convert early reserve to bootmem reserve earlier
  226. * otherwise early_node_mem could use early reserved mem
  227. * on previous node
  228. */
  229. early_res_to_bootmem(start, end);
  230. /*
  231. * in some case early_node_mem could use alloc_bootmem
  232. * to get range on other node, don't reserve that again
  233. */
  234. if (nid != nodeid)
  235. printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
  236. else
  237. reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys,
  238. pgdat_size, BOOTMEM_DEFAULT);
  239. nid = phys_to_nid(bootmap_start);
  240. if (nid != nodeid)
  241. printk(KERN_INFO " bootmap(%d) on node %d\n", nodeid, nid);
  242. else
  243. reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start,
  244. bootmap_pages<<PAGE_SHIFT, BOOTMEM_DEFAULT);
  245. #ifdef CONFIG_ACPI_NUMA
  246. srat_reserve_add_area(nodeid);
  247. #endif
  248. node_set_online(nodeid);
  249. }
  250. /*
  251. * There are unfortunately some poorly designed mainboards around that
  252. * only connect memory to a single CPU. This breaks the 1:1 cpu->node
  253. * mapping. To avoid this fill in the mapping for all possible CPUs,
  254. * as the number of CPUs is not known yet. We round robin the existing
  255. * nodes.
  256. */
  257. void __init numa_init_array(void)
  258. {
  259. int rr, i;
  260. rr = first_node(node_online_map);
  261. for (i = 0; i < nr_cpu_ids; i++) {
  262. if (early_cpu_to_node(i) != NUMA_NO_NODE)
  263. continue;
  264. numa_set_node(i, rr);
  265. rr = next_node(rr, node_online_map);
  266. if (rr == MAX_NUMNODES)
  267. rr = first_node(node_online_map);
  268. }
  269. }
  270. #ifdef CONFIG_NUMA_EMU
  271. /* Numa emulation */
  272. static char *cmdline __initdata;
  273. /*
  274. * Setups up nid to range from addr to addr + size. If the end
  275. * boundary is greater than max_addr, then max_addr is used instead.
  276. * The return value is 0 if there is additional memory left for
  277. * allocation past addr and -1 otherwise. addr is adjusted to be at
  278. * the end of the node.
  279. */
  280. static int __init setup_node_range(int nid, struct bootnode *nodes, u64 *addr,
  281. u64 size, u64 max_addr)
  282. {
  283. int ret = 0;
  284. nodes[nid].start = *addr;
  285. *addr += size;
  286. if (*addr >= max_addr) {
  287. *addr = max_addr;
  288. ret = -1;
  289. }
  290. nodes[nid].end = *addr;
  291. node_set(nid, node_possible_map);
  292. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
  293. nodes[nid].start, nodes[nid].end,
  294. (nodes[nid].end - nodes[nid].start) >> 20);
  295. return ret;
  296. }
  297. /*
  298. * Splits num_nodes nodes up equally starting at node_start. The return value
  299. * is the number of nodes split up and addr is adjusted to be at the end of the
  300. * last node allocated.
  301. */
  302. static int __init split_nodes_equally(struct bootnode *nodes, u64 *addr,
  303. u64 max_addr, int node_start,
  304. int num_nodes)
  305. {
  306. unsigned int big;
  307. u64 size;
  308. int i;
  309. if (num_nodes <= 0)
  310. return -1;
  311. if (num_nodes > MAX_NUMNODES)
  312. num_nodes = MAX_NUMNODES;
  313. size = (max_addr - *addr - e820_hole_size(*addr, max_addr)) /
  314. num_nodes;
  315. /*
  316. * Calculate the number of big nodes that can be allocated as a result
  317. * of consolidating the leftovers.
  318. */
  319. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * num_nodes) /
  320. FAKE_NODE_MIN_SIZE;
  321. /* Round down to nearest FAKE_NODE_MIN_SIZE. */
  322. size &= FAKE_NODE_MIN_HASH_MASK;
  323. if (!size) {
  324. printk(KERN_ERR "Not enough memory for each node. "
  325. "NUMA emulation disabled.\n");
  326. return -1;
  327. }
  328. for (i = node_start; i < num_nodes + node_start; i++) {
  329. u64 end = *addr + size;
  330. if (i < big)
  331. end += FAKE_NODE_MIN_SIZE;
  332. /*
  333. * The final node can have the remaining system RAM. Other
  334. * nodes receive roughly the same amount of available pages.
  335. */
  336. if (i == num_nodes + node_start - 1)
  337. end = max_addr;
  338. else
  339. while (end - *addr - e820_hole_size(*addr, end) <
  340. size) {
  341. end += FAKE_NODE_MIN_SIZE;
  342. if (end > max_addr) {
  343. end = max_addr;
  344. break;
  345. }
  346. }
  347. if (setup_node_range(i, nodes, addr, end - *addr, max_addr) < 0)
  348. break;
  349. }
  350. return i - node_start + 1;
  351. }
  352. /*
  353. * Splits the remaining system RAM into chunks of size. The remaining memory is
  354. * always assigned to a final node and can be asymmetric. Returns the number of
  355. * nodes split.
  356. */
  357. static int __init split_nodes_by_size(struct bootnode *nodes, u64 *addr,
  358. u64 max_addr, int node_start, u64 size)
  359. {
  360. int i = node_start;
  361. size = (size << 20) & FAKE_NODE_MIN_HASH_MASK;
  362. while (!setup_node_range(i++, nodes, addr, size, max_addr))
  363. ;
  364. return i - node_start;
  365. }
  366. /*
  367. * Sets up the system RAM area from start_pfn to last_pfn according to the
  368. * numa=fake command-line option.
  369. */
  370. static struct bootnode nodes[MAX_NUMNODES] __initdata;
  371. static int __init numa_emulation(unsigned long start_pfn, unsigned long last_pfn)
  372. {
  373. u64 size, addr = start_pfn << PAGE_SHIFT;
  374. u64 max_addr = last_pfn << PAGE_SHIFT;
  375. int num_nodes = 0, num = 0, coeff_flag, coeff = -1, i;
  376. memset(&nodes, 0, sizeof(nodes));
  377. /*
  378. * If the numa=fake command-line is just a single number N, split the
  379. * system RAM into N fake nodes.
  380. */
  381. if (!strchr(cmdline, '*') && !strchr(cmdline, ',')) {
  382. long n = simple_strtol(cmdline, NULL, 0);
  383. num_nodes = split_nodes_equally(nodes, &addr, max_addr, 0, n);
  384. if (num_nodes < 0)
  385. return num_nodes;
  386. goto out;
  387. }
  388. /* Parse the command line. */
  389. for (coeff_flag = 0; ; cmdline++) {
  390. if (*cmdline && isdigit(*cmdline)) {
  391. num = num * 10 + *cmdline - '0';
  392. continue;
  393. }
  394. if (*cmdline == '*') {
  395. if (num > 0)
  396. coeff = num;
  397. coeff_flag = 1;
  398. }
  399. if (!*cmdline || *cmdline == ',') {
  400. if (!coeff_flag)
  401. coeff = 1;
  402. /*
  403. * Round down to the nearest FAKE_NODE_MIN_SIZE.
  404. * Command-line coefficients are in megabytes.
  405. */
  406. size = ((u64)num << 20) & FAKE_NODE_MIN_HASH_MASK;
  407. if (size)
  408. for (i = 0; i < coeff; i++, num_nodes++)
  409. if (setup_node_range(num_nodes, nodes,
  410. &addr, size, max_addr) < 0)
  411. goto done;
  412. if (!*cmdline)
  413. break;
  414. coeff_flag = 0;
  415. coeff = -1;
  416. }
  417. num = 0;
  418. }
  419. done:
  420. if (!num_nodes)
  421. return -1;
  422. /* Fill remainder of system RAM, if appropriate. */
  423. if (addr < max_addr) {
  424. if (coeff_flag && coeff < 0) {
  425. /* Split remaining nodes into num-sized chunks */
  426. num_nodes += split_nodes_by_size(nodes, &addr, max_addr,
  427. num_nodes, num);
  428. goto out;
  429. }
  430. switch (*(cmdline - 1)) {
  431. case '*':
  432. /* Split remaining nodes into coeff chunks */
  433. if (coeff <= 0)
  434. break;
  435. num_nodes += split_nodes_equally(nodes, &addr, max_addr,
  436. num_nodes, coeff);
  437. break;
  438. case ',':
  439. /* Do not allocate remaining system RAM */
  440. break;
  441. default:
  442. /* Give one final node */
  443. setup_node_range(num_nodes, nodes, &addr,
  444. max_addr - addr, max_addr);
  445. num_nodes++;
  446. }
  447. }
  448. out:
  449. memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
  450. if (memnode_shift < 0) {
  451. memnode_shift = 0;
  452. printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
  453. "disabled.\n");
  454. return -1;
  455. }
  456. /*
  457. * We need to vacate all active ranges that may have been registered by
  458. * SRAT and set acpi_numa to -1 so that srat_disabled() always returns
  459. * true. NUMA emulation has succeeded so we will not scan ACPI nodes.
  460. */
  461. remove_all_active_ranges();
  462. #ifdef CONFIG_ACPI_NUMA
  463. acpi_numa = -1;
  464. #endif
  465. for_each_node_mask(i, node_possible_map) {
  466. e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  467. nodes[i].end >> PAGE_SHIFT);
  468. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  469. }
  470. acpi_fake_nodes(nodes, num_nodes);
  471. numa_init_array();
  472. return 0;
  473. }
  474. #endif /* CONFIG_NUMA_EMU */
  475. void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn)
  476. {
  477. int i;
  478. nodes_clear(node_possible_map);
  479. nodes_clear(node_online_map);
  480. #ifdef CONFIG_NUMA_EMU
  481. if (cmdline && !numa_emulation(start_pfn, last_pfn))
  482. return;
  483. nodes_clear(node_possible_map);
  484. nodes_clear(node_online_map);
  485. #endif
  486. #ifdef CONFIG_ACPI_NUMA
  487. if (!numa_off && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
  488. last_pfn << PAGE_SHIFT))
  489. return;
  490. nodes_clear(node_possible_map);
  491. nodes_clear(node_online_map);
  492. #endif
  493. #ifdef CONFIG_K8_NUMA
  494. if (!numa_off && !k8_scan_nodes(start_pfn<<PAGE_SHIFT,
  495. last_pfn<<PAGE_SHIFT))
  496. return;
  497. nodes_clear(node_possible_map);
  498. nodes_clear(node_online_map);
  499. #endif
  500. printk(KERN_INFO "%s\n",
  501. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  502. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  503. start_pfn << PAGE_SHIFT,
  504. last_pfn << PAGE_SHIFT);
  505. /* setup dummy node covering all memory */
  506. memnode_shift = 63;
  507. memnodemap = memnode.embedded_map;
  508. memnodemap[0] = 0;
  509. node_set_online(0);
  510. node_set(0, node_possible_map);
  511. for (i = 0; i < nr_cpu_ids; i++)
  512. numa_set_node(i, 0);
  513. e820_register_active_regions(0, start_pfn, last_pfn);
  514. setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
  515. }
  516. unsigned long __init numa_free_all_bootmem(void)
  517. {
  518. unsigned long pages = 0;
  519. int i;
  520. for_each_online_node(i)
  521. pages += free_all_bootmem_node(NODE_DATA(i));
  522. return pages;
  523. }
  524. void __init paging_init(void)
  525. {
  526. unsigned long max_zone_pfns[MAX_NR_ZONES];
  527. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  528. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  529. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  530. max_zone_pfns[ZONE_NORMAL] = max_pfn;
  531. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  532. sparse_init();
  533. free_area_init_nodes(max_zone_pfns);
  534. }
  535. static __init int numa_setup(char *opt)
  536. {
  537. if (!opt)
  538. return -EINVAL;
  539. if (!strncmp(opt, "off", 3))
  540. numa_off = 1;
  541. #ifdef CONFIG_NUMA_EMU
  542. if (!strncmp(opt, "fake=", 5))
  543. cmdline = opt + 5;
  544. #endif
  545. #ifdef CONFIG_ACPI_NUMA
  546. if (!strncmp(opt, "noacpi", 6))
  547. acpi_numa = -1;
  548. if (!strncmp(opt, "hotadd=", 7))
  549. hotadd_percent = simple_strtoul(opt+7, NULL, 10);
  550. #endif
  551. return 0;
  552. }
  553. early_param("numa", numa_setup);
  554. #ifdef CONFIG_NUMA
  555. /*
  556. * Setup early cpu_to_node.
  557. *
  558. * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
  559. * and apicid_to_node[] tables have valid entries for a CPU.
  560. * This means we skip cpu_to_node[] initialisation for NUMA
  561. * emulation and faking node case (when running a kernel compiled
  562. * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
  563. * is already initialized in a round robin manner at numa_init_array,
  564. * prior to this call, and this initialization is good enough
  565. * for the fake NUMA cases.
  566. *
  567. * Called before the per_cpu areas are setup.
  568. */
  569. void __init init_cpu_to_node(void)
  570. {
  571. int cpu;
  572. u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
  573. BUG_ON(cpu_to_apicid == NULL);
  574. for_each_possible_cpu(cpu) {
  575. int node;
  576. u16 apicid = cpu_to_apicid[cpu];
  577. if (apicid == BAD_APICID)
  578. continue;
  579. node = apicid_to_node[apicid];
  580. if (node == NUMA_NO_NODE)
  581. continue;
  582. if (!node_online(node))
  583. continue;
  584. numa_set_node(cpu, node);
  585. }
  586. }
  587. #endif
  588. /*
  589. * Allocate node_to_cpumask_map based on number of available nodes
  590. * Requires node_possible_map to be valid.
  591. *
  592. * Note: node_to_cpumask() is not valid until after this is done.
  593. * (Use CONFIG_DEBUG_PER_CPU_MAPS to check this.)
  594. */
  595. void __init setup_node_to_cpumask_map(void)
  596. {
  597. unsigned int node, num = 0;
  598. cpumask_t *map;
  599. /* setup nr_node_ids if not done yet */
  600. if (nr_node_ids == MAX_NUMNODES) {
  601. for_each_node_mask(node, node_possible_map)
  602. num = node;
  603. nr_node_ids = num + 1;
  604. }
  605. /* allocate the map */
  606. map = alloc_bootmem_low(nr_node_ids * sizeof(cpumask_t));
  607. DBG("node_to_cpumask_map at %p for %d nodes\n", map, nr_node_ids);
  608. pr_debug("Node to cpumask map at %p for %d nodes\n",
  609. map, nr_node_ids);
  610. /* node_to_cpumask() will now work */
  611. node_to_cpumask_map = map;
  612. }
  613. void __cpuinit numa_set_node(int cpu, int node)
  614. {
  615. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  616. /* early setting, no percpu area yet */
  617. if (cpu_to_node_map) {
  618. cpu_to_node_map[cpu] = node;
  619. return;
  620. }
  621. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  622. if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
  623. printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
  624. dump_stack();
  625. return;
  626. }
  627. #endif
  628. per_cpu(x86_cpu_to_node_map, cpu) = node;
  629. if (node != NUMA_NO_NODE)
  630. per_cpu(node_number, cpu) = node;
  631. }
  632. void __cpuinit numa_clear_node(int cpu)
  633. {
  634. numa_set_node(cpu, NUMA_NO_NODE);
  635. }
  636. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  637. void __cpuinit numa_add_cpu(int cpu)
  638. {
  639. cpu_set(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  640. }
  641. void __cpuinit numa_remove_cpu(int cpu)
  642. {
  643. cpu_clear(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  644. }
  645. #else /* CONFIG_DEBUG_PER_CPU_MAPS */
  646. /*
  647. * --------- debug versions of the numa functions ---------
  648. */
  649. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  650. {
  651. int node = early_cpu_to_node(cpu);
  652. cpumask_t *mask;
  653. char buf[64];
  654. if (node_to_cpumask_map == NULL) {
  655. printk(KERN_ERR "node_to_cpumask_map NULL\n");
  656. dump_stack();
  657. return;
  658. }
  659. mask = &node_to_cpumask_map[node];
  660. if (enable)
  661. cpu_set(cpu, *mask);
  662. else
  663. cpu_clear(cpu, *mask);
  664. cpulist_scnprintf(buf, sizeof(buf), mask);
  665. printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
  666. enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
  667. }
  668. void __cpuinit numa_add_cpu(int cpu)
  669. {
  670. numa_set_cpumask(cpu, 1);
  671. }
  672. void __cpuinit numa_remove_cpu(int cpu)
  673. {
  674. numa_set_cpumask(cpu, 0);
  675. }
  676. int cpu_to_node(int cpu)
  677. {
  678. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  679. printk(KERN_WARNING
  680. "cpu_to_node(%d): usage too early!\n", cpu);
  681. dump_stack();
  682. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  683. }
  684. return per_cpu(x86_cpu_to_node_map, cpu);
  685. }
  686. EXPORT_SYMBOL(cpu_to_node);
  687. /*
  688. * Same function as cpu_to_node() but used if called before the
  689. * per_cpu areas are setup.
  690. */
  691. int early_cpu_to_node(int cpu)
  692. {
  693. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  694. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  695. if (!cpu_possible(cpu)) {
  696. printk(KERN_WARNING
  697. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  698. dump_stack();
  699. return NUMA_NO_NODE;
  700. }
  701. return per_cpu(x86_cpu_to_node_map, cpu);
  702. }
  703. /* empty cpumask */
  704. static const cpumask_t cpu_mask_none;
  705. /*
  706. * Returns a pointer to the bitmask of CPUs on Node 'node'.
  707. */
  708. const cpumask_t *cpumask_of_node(int node)
  709. {
  710. if (node_to_cpumask_map == NULL) {
  711. printk(KERN_WARNING
  712. "cpumask_of_node(%d): no node_to_cpumask_map!\n",
  713. node);
  714. dump_stack();
  715. return (const cpumask_t *)&cpu_online_map;
  716. }
  717. if (node >= nr_node_ids) {
  718. printk(KERN_WARNING
  719. "cpumask_of_node(%d): node > nr_node_ids(%d)\n",
  720. node, nr_node_ids);
  721. dump_stack();
  722. return &cpu_mask_none;
  723. }
  724. return &node_to_cpumask_map[node];
  725. }
  726. EXPORT_SYMBOL(cpumask_of_node);
  727. /*
  728. * Returns a bitmask of CPUs on Node 'node'.
  729. *
  730. * Side note: this function creates the returned cpumask on the stack
  731. * so with a high NR_CPUS count, excessive stack space is used. The
  732. * node_to_cpumask_ptr function should be used whenever possible.
  733. */
  734. cpumask_t node_to_cpumask(int node)
  735. {
  736. if (node_to_cpumask_map == NULL) {
  737. printk(KERN_WARNING
  738. "node_to_cpumask(%d): no node_to_cpumask_map!\n", node);
  739. dump_stack();
  740. return cpu_online_map;
  741. }
  742. if (node >= nr_node_ids) {
  743. printk(KERN_WARNING
  744. "node_to_cpumask(%d): node > nr_node_ids(%d)\n",
  745. node, nr_node_ids);
  746. dump_stack();
  747. return cpu_mask_none;
  748. }
  749. return node_to_cpumask_map[node];
  750. }
  751. EXPORT_SYMBOL(node_to_cpumask);
  752. /*
  753. * --------- end of debug versions of the numa functions ---------
  754. */
  755. #endif /* CONFIG_DEBUG_PER_CPU_MAPS */