numa_64.c 19 KB

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