numa_64.c 19 KB

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