numa_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. #ifndef Dprintk
  22. #define Dprintk(x...)
  23. #endif
  24. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  25. EXPORT_SYMBOL(node_data);
  26. bootmem_data_t plat_node_bdata[MAX_NUMNODES];
  27. struct memnode memnode;
  28. int x86_cpu_to_node_map_init[NR_CPUS] = {
  29. [0 ... NR_CPUS-1] = NUMA_NO_NODE
  30. };
  31. void *x86_cpu_to_node_map_early_ptr;
  32. DEFINE_PER_CPU(int, x86_cpu_to_node_map) = NUMA_NO_NODE;
  33. EXPORT_PER_CPU_SYMBOL(x86_cpu_to_node_map);
  34. EXPORT_SYMBOL(x86_cpu_to_node_map_early_ptr);
  35. s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
  36. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  37. };
  38. cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly;
  39. EXPORT_SYMBOL(node_to_cpumask_map);
  40. int numa_off __initdata;
  41. unsigned long __initdata nodemap_addr;
  42. unsigned long __initdata nodemap_size;
  43. /*
  44. * Given a shift value, try to populate memnodemap[]
  45. * Returns :
  46. * 1 if OK
  47. * 0 if memnodmap[] too small (of shift too small)
  48. * -1 if node overlap or lost ram (shift too big)
  49. */
  50. static int __init populate_memnodemap(const struct bootnode *nodes,
  51. int numnodes, int shift)
  52. {
  53. unsigned long addr, end;
  54. int i, res = -1;
  55. memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
  56. for (i = 0; i < numnodes; i++) {
  57. addr = nodes[i].start;
  58. end = nodes[i].end;
  59. if (addr >= end)
  60. continue;
  61. if ((end >> shift) >= memnodemapsize)
  62. return 0;
  63. do {
  64. if (memnodemap[addr >> shift] != NUMA_NO_NODE)
  65. return -1;
  66. memnodemap[addr >> shift] = i;
  67. addr += (1UL << shift);
  68. } while (addr < end);
  69. res = 1;
  70. }
  71. return res;
  72. }
  73. static int __init allocate_cachealigned_memnodemap(void)
  74. {
  75. unsigned long addr;
  76. memnodemap = memnode.embedded_map;
  77. if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
  78. return 0;
  79. addr = 0x8000;
  80. nodemap_size = round_up(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
  81. nodemap_addr = find_e820_area(addr, end_pfn<<PAGE_SHIFT,
  82. nodemap_size, L1_CACHE_BYTES);
  83. if (nodemap_addr == -1UL) {
  84. printk(KERN_ERR
  85. "NUMA: Unable to allocate Memory to Node hash map\n");
  86. nodemap_addr = nodemap_size = 0;
  87. return -1;
  88. }
  89. memnodemap = phys_to_virt(nodemap_addr);
  90. reserve_early(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
  91. printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
  92. nodemap_addr, nodemap_addr + nodemap_size);
  93. return 0;
  94. }
  95. /*
  96. * The LSB of all start and end addresses in the node map is the value of the
  97. * maximum possible shift.
  98. */
  99. static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
  100. int numnodes)
  101. {
  102. int i, nodes_used = 0;
  103. unsigned long start, end;
  104. unsigned long bitfield = 0, memtop = 0;
  105. for (i = 0; i < numnodes; i++) {
  106. start = nodes[i].start;
  107. end = nodes[i].end;
  108. if (start >= end)
  109. continue;
  110. bitfield |= start;
  111. nodes_used++;
  112. if (end > memtop)
  113. memtop = end;
  114. }
  115. if (nodes_used <= 1)
  116. i = 63;
  117. else
  118. i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
  119. memnodemapsize = (memtop >> i)+1;
  120. return i;
  121. }
  122. int __init compute_hash_shift(struct bootnode *nodes, int numnodes)
  123. {
  124. int shift;
  125. shift = extract_lsb_from_nodes(nodes, numnodes);
  126. if (allocate_cachealigned_memnodemap())
  127. return -1;
  128. printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
  129. shift);
  130. if (populate_memnodemap(nodes, numnodes, shift) != 1) {
  131. printk(KERN_INFO "Your memory is not aligned you need to "
  132. "rebuild your kernel with a bigger NODEMAPSIZE "
  133. "shift=%d\n", shift);
  134. return -1;
  135. }
  136. return shift;
  137. }
  138. int early_pfn_to_nid(unsigned long pfn)
  139. {
  140. return phys_to_nid(pfn << PAGE_SHIFT);
  141. }
  142. static void * __init early_node_mem(int nodeid, unsigned long start,
  143. unsigned long end, unsigned long size,
  144. unsigned long align)
  145. {
  146. unsigned long mem = find_e820_area(start, end, size, align);
  147. void *ptr;
  148. if (mem != -1L)
  149. return __va(mem);
  150. ptr = __alloc_bootmem_nopanic(size, align, __pa(MAX_DMA_ADDRESS));
  151. if (ptr == NULL) {
  152. printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
  153. size, nodeid);
  154. return NULL;
  155. }
  156. return ptr;
  157. }
  158. /* Initialize bootmem allocator for a node */
  159. void __init setup_node_bootmem(int nodeid, unsigned long start,
  160. unsigned long end)
  161. {
  162. unsigned long start_pfn, end_pfn, bootmap_pages, bootmap_size;
  163. unsigned long bootmap_start, nodedata_phys;
  164. void *bootmap;
  165. const int pgdat_size = round_up(sizeof(pg_data_t), PAGE_SIZE);
  166. start = round_up(start, ZONE_ALIGN);
  167. printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid,
  168. start, end);
  169. start_pfn = start >> PAGE_SHIFT;
  170. end_pfn = end >> PAGE_SHIFT;
  171. node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
  172. SMP_CACHE_BYTES);
  173. if (node_data[nodeid] == NULL)
  174. return;
  175. nodedata_phys = __pa(node_data[nodeid]);
  176. printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
  177. nodedata_phys + pgdat_size - 1);
  178. memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
  179. NODE_DATA(nodeid)->bdata = &plat_node_bdata[nodeid];
  180. NODE_DATA(nodeid)->node_start_pfn = start_pfn;
  181. NODE_DATA(nodeid)->node_spanned_pages = end_pfn - start_pfn;
  182. /* Find a place for the bootmem map */
  183. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  184. bootmap_start = round_up(nodedata_phys + pgdat_size, PAGE_SIZE);
  185. /*
  186. * SMP_CAHCE_BYTES could be enough, but init_bootmem_node like
  187. * to use that to align to PAGE_SIZE
  188. */
  189. bootmap = early_node_mem(nodeid, bootmap_start, end,
  190. bootmap_pages<<PAGE_SHIFT, PAGE_SIZE);
  191. if (bootmap == NULL) {
  192. if (nodedata_phys < start || nodedata_phys >= end)
  193. free_bootmem((unsigned long)node_data[nodeid],
  194. pgdat_size);
  195. node_data[nodeid] = NULL;
  196. return;
  197. }
  198. bootmap_start = __pa(bootmap);
  199. bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
  200. bootmap_start >> PAGE_SHIFT,
  201. start_pfn, end_pfn);
  202. printk(KERN_INFO " bootmap [%016lx - %016lx] pages %lx\n",
  203. bootmap_start, bootmap_start + bootmap_size - 1,
  204. bootmap_pages);
  205. free_bootmem_with_active_regions(nodeid, end);
  206. reserve_bootmem_node(NODE_DATA(nodeid), nodedata_phys, pgdat_size,
  207. BOOTMEM_DEFAULT);
  208. reserve_bootmem_node(NODE_DATA(nodeid), bootmap_start,
  209. bootmap_pages<<PAGE_SHIFT, BOOTMEM_DEFAULT);
  210. #ifdef CONFIG_ACPI_NUMA
  211. srat_reserve_add_area(nodeid);
  212. #endif
  213. node_set_online(nodeid);
  214. }
  215. /*
  216. * There are unfortunately some poorly designed mainboards around that
  217. * only connect memory to a single CPU. This breaks the 1:1 cpu->node
  218. * mapping. To avoid this fill in the mapping for all possible CPUs,
  219. * as the number of CPUs is not known yet. We round robin the existing
  220. * nodes.
  221. */
  222. void __init numa_init_array(void)
  223. {
  224. int rr, i;
  225. rr = first_node(node_online_map);
  226. for (i = 0; i < NR_CPUS; i++) {
  227. if (early_cpu_to_node(i) != NUMA_NO_NODE)
  228. continue;
  229. numa_set_node(i, rr);
  230. rr = next_node(rr, node_online_map);
  231. if (rr == MAX_NUMNODES)
  232. rr = first_node(node_online_map);
  233. }
  234. }
  235. #ifdef CONFIG_NUMA_EMU
  236. /* Numa emulation */
  237. char *cmdline __initdata;
  238. /*
  239. * Setups up nid to range from addr to addr + size. If the end
  240. * boundary is greater than max_addr, then max_addr is used instead.
  241. * The return value is 0 if there is additional memory left for
  242. * allocation past addr and -1 otherwise. addr is adjusted to be at
  243. * the end of the node.
  244. */
  245. static int __init setup_node_range(int nid, struct bootnode *nodes, u64 *addr,
  246. u64 size, u64 max_addr)
  247. {
  248. int ret = 0;
  249. nodes[nid].start = *addr;
  250. *addr += size;
  251. if (*addr >= max_addr) {
  252. *addr = max_addr;
  253. ret = -1;
  254. }
  255. nodes[nid].end = *addr;
  256. node_set(nid, node_possible_map);
  257. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
  258. nodes[nid].start, nodes[nid].end,
  259. (nodes[nid].end - nodes[nid].start) >> 20);
  260. return ret;
  261. }
  262. /*
  263. * Splits num_nodes nodes up equally starting at node_start. The return value
  264. * is the number of nodes split up and addr is adjusted to be at the end of the
  265. * last node allocated.
  266. */
  267. static int __init split_nodes_equally(struct bootnode *nodes, u64 *addr,
  268. u64 max_addr, int node_start,
  269. int num_nodes)
  270. {
  271. unsigned int big;
  272. u64 size;
  273. int i;
  274. if (num_nodes <= 0)
  275. return -1;
  276. if (num_nodes > MAX_NUMNODES)
  277. num_nodes = MAX_NUMNODES;
  278. size = (max_addr - *addr - e820_hole_size(*addr, max_addr)) /
  279. num_nodes;
  280. /*
  281. * Calculate the number of big nodes that can be allocated as a result
  282. * of consolidating the leftovers.
  283. */
  284. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * num_nodes) /
  285. FAKE_NODE_MIN_SIZE;
  286. /* Round down to nearest FAKE_NODE_MIN_SIZE. */
  287. size &= FAKE_NODE_MIN_HASH_MASK;
  288. if (!size) {
  289. printk(KERN_ERR "Not enough memory for each node. "
  290. "NUMA emulation disabled.\n");
  291. return -1;
  292. }
  293. for (i = node_start; i < num_nodes + node_start; i++) {
  294. u64 end = *addr + size;
  295. if (i < big)
  296. end += FAKE_NODE_MIN_SIZE;
  297. /*
  298. * The final node can have the remaining system RAM. Other
  299. * nodes receive roughly the same amount of available pages.
  300. */
  301. if (i == num_nodes + node_start - 1)
  302. end = max_addr;
  303. else
  304. while (end - *addr - e820_hole_size(*addr, end) <
  305. size) {
  306. end += FAKE_NODE_MIN_SIZE;
  307. if (end > max_addr) {
  308. end = max_addr;
  309. break;
  310. }
  311. }
  312. if (setup_node_range(i, nodes, addr, end - *addr, max_addr) < 0)
  313. break;
  314. }
  315. return i - node_start + 1;
  316. }
  317. /*
  318. * Splits the remaining system RAM into chunks of size. The remaining memory is
  319. * always assigned to a final node and can be asymmetric. Returns the number of
  320. * nodes split.
  321. */
  322. static int __init split_nodes_by_size(struct bootnode *nodes, u64 *addr,
  323. u64 max_addr, int node_start, u64 size)
  324. {
  325. int i = node_start;
  326. size = (size << 20) & FAKE_NODE_MIN_HASH_MASK;
  327. while (!setup_node_range(i++, nodes, addr, size, max_addr))
  328. ;
  329. return i - node_start;
  330. }
  331. /*
  332. * Sets up the system RAM area from start_pfn to end_pfn according to the
  333. * numa=fake command-line option.
  334. */
  335. static int __init numa_emulation(unsigned long start_pfn, unsigned long end_pfn)
  336. {
  337. struct bootnode nodes[MAX_NUMNODES];
  338. u64 size, addr = start_pfn << PAGE_SHIFT;
  339. u64 max_addr = end_pfn << PAGE_SHIFT;
  340. int num_nodes = 0, num = 0, coeff_flag, coeff = -1, i;
  341. memset(&nodes, 0, sizeof(nodes));
  342. /*
  343. * If the numa=fake command-line is just a single number N, split the
  344. * system RAM into N fake nodes.
  345. */
  346. if (!strchr(cmdline, '*') && !strchr(cmdline, ',')) {
  347. long n = simple_strtol(cmdline, NULL, 0);
  348. num_nodes = split_nodes_equally(nodes, &addr, max_addr, 0, n);
  349. if (num_nodes < 0)
  350. return num_nodes;
  351. goto out;
  352. }
  353. /* Parse the command line. */
  354. for (coeff_flag = 0; ; cmdline++) {
  355. if (*cmdline && isdigit(*cmdline)) {
  356. num = num * 10 + *cmdline - '0';
  357. continue;
  358. }
  359. if (*cmdline == '*') {
  360. if (num > 0)
  361. coeff = num;
  362. coeff_flag = 1;
  363. }
  364. if (!*cmdline || *cmdline == ',') {
  365. if (!coeff_flag)
  366. coeff = 1;
  367. /*
  368. * Round down to the nearest FAKE_NODE_MIN_SIZE.
  369. * Command-line coefficients are in megabytes.
  370. */
  371. size = ((u64)num << 20) & FAKE_NODE_MIN_HASH_MASK;
  372. if (size)
  373. for (i = 0; i < coeff; i++, num_nodes++)
  374. if (setup_node_range(num_nodes, nodes,
  375. &addr, size, max_addr) < 0)
  376. goto done;
  377. if (!*cmdline)
  378. break;
  379. coeff_flag = 0;
  380. coeff = -1;
  381. }
  382. num = 0;
  383. }
  384. done:
  385. if (!num_nodes)
  386. return -1;
  387. /* Fill remainder of system RAM, if appropriate. */
  388. if (addr < max_addr) {
  389. if (coeff_flag && coeff < 0) {
  390. /* Split remaining nodes into num-sized chunks */
  391. num_nodes += split_nodes_by_size(nodes, &addr, max_addr,
  392. num_nodes, num);
  393. goto out;
  394. }
  395. switch (*(cmdline - 1)) {
  396. case '*':
  397. /* Split remaining nodes into coeff chunks */
  398. if (coeff <= 0)
  399. break;
  400. num_nodes += split_nodes_equally(nodes, &addr, max_addr,
  401. num_nodes, coeff);
  402. break;
  403. case ',':
  404. /* Do not allocate remaining system RAM */
  405. break;
  406. default:
  407. /* Give one final node */
  408. setup_node_range(num_nodes, nodes, &addr,
  409. max_addr - addr, max_addr);
  410. num_nodes++;
  411. }
  412. }
  413. out:
  414. memnode_shift = compute_hash_shift(nodes, num_nodes);
  415. if (memnode_shift < 0) {
  416. memnode_shift = 0;
  417. printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
  418. "disabled.\n");
  419. return -1;
  420. }
  421. /*
  422. * We need to vacate all active ranges that may have been registered by
  423. * SRAT and set acpi_numa to -1 so that srat_disabled() always returns
  424. * true. NUMA emulation has succeeded so we will not scan ACPI nodes.
  425. */
  426. remove_all_active_ranges();
  427. #ifdef CONFIG_ACPI_NUMA
  428. acpi_numa = -1;
  429. #endif
  430. for_each_node_mask(i, node_possible_map) {
  431. e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  432. nodes[i].end >> PAGE_SHIFT);
  433. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  434. }
  435. acpi_fake_nodes(nodes, num_nodes);
  436. numa_init_array();
  437. return 0;
  438. }
  439. #endif /* CONFIG_NUMA_EMU */
  440. void __init numa_initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  441. {
  442. int i;
  443. nodes_clear(node_possible_map);
  444. nodes_clear(node_online_map);
  445. #ifdef CONFIG_NUMA_EMU
  446. if (cmdline && !numa_emulation(start_pfn, end_pfn))
  447. return;
  448. nodes_clear(node_possible_map);
  449. nodes_clear(node_online_map);
  450. #endif
  451. #ifdef CONFIG_ACPI_NUMA
  452. if (!numa_off && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
  453. end_pfn << PAGE_SHIFT))
  454. return;
  455. nodes_clear(node_possible_map);
  456. nodes_clear(node_online_map);
  457. #endif
  458. #ifdef CONFIG_K8_NUMA
  459. if (!numa_off && !k8_scan_nodes(start_pfn<<PAGE_SHIFT,
  460. end_pfn<<PAGE_SHIFT))
  461. return;
  462. nodes_clear(node_possible_map);
  463. nodes_clear(node_online_map);
  464. #endif
  465. printk(KERN_INFO "%s\n",
  466. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  467. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  468. start_pfn << PAGE_SHIFT,
  469. end_pfn << PAGE_SHIFT);
  470. /* setup dummy node covering all memory */
  471. memnode_shift = 63;
  472. memnodemap = memnode.embedded_map;
  473. memnodemap[0] = 0;
  474. node_set_online(0);
  475. node_set(0, node_possible_map);
  476. for (i = 0; i < NR_CPUS; i++)
  477. numa_set_node(i, 0);
  478. /* cpumask_of_cpu() may not be available during early startup */
  479. memset(&node_to_cpumask_map[0], 0, sizeof(node_to_cpumask_map[0]));
  480. cpu_set(0, node_to_cpumask_map[0]);
  481. e820_register_active_regions(0, start_pfn, end_pfn);
  482. setup_node_bootmem(0, start_pfn << PAGE_SHIFT, end_pfn << PAGE_SHIFT);
  483. }
  484. __cpuinit void numa_add_cpu(int cpu)
  485. {
  486. set_bit(cpu,
  487. (unsigned long *)&node_to_cpumask_map[early_cpu_to_node(cpu)]);
  488. }
  489. void __cpuinit numa_set_node(int cpu, int node)
  490. {
  491. int *cpu_to_node_map = x86_cpu_to_node_map_early_ptr;
  492. cpu_pda(cpu)->nodenumber = node;
  493. if(cpu_to_node_map)
  494. cpu_to_node_map[cpu] = node;
  495. else if(per_cpu_offset(cpu))
  496. per_cpu(x86_cpu_to_node_map, cpu) = node;
  497. else
  498. Dprintk(KERN_INFO "Setting node for non-present cpu %d\n", cpu);
  499. }
  500. unsigned long __init numa_free_all_bootmem(void)
  501. {
  502. unsigned long pages = 0;
  503. int i;
  504. for_each_online_node(i)
  505. pages += free_all_bootmem_node(NODE_DATA(i));
  506. return pages;
  507. }
  508. void __init paging_init(void)
  509. {
  510. unsigned long max_zone_pfns[MAX_NR_ZONES];
  511. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  512. max_zone_pfns[ZONE_DMA] = MAX_DMA_PFN;
  513. max_zone_pfns[ZONE_DMA32] = MAX_DMA32_PFN;
  514. max_zone_pfns[ZONE_NORMAL] = end_pfn;
  515. sparse_memory_present_with_active_regions(MAX_NUMNODES);
  516. sparse_init();
  517. free_area_init_nodes(max_zone_pfns);
  518. }
  519. static __init int numa_setup(char *opt)
  520. {
  521. if (!opt)
  522. return -EINVAL;
  523. if (!strncmp(opt, "off", 3))
  524. numa_off = 1;
  525. #ifdef CONFIG_NUMA_EMU
  526. if (!strncmp(opt, "fake=", 5))
  527. cmdline = opt + 5;
  528. #endif
  529. #ifdef CONFIG_ACPI_NUMA
  530. if (!strncmp(opt, "noacpi", 6))
  531. acpi_numa = -1;
  532. if (!strncmp(opt, "hotadd=", 7))
  533. hotadd_percent = simple_strtoul(opt+7, NULL, 10);
  534. #endif
  535. return 0;
  536. }
  537. early_param("numa", numa_setup);
  538. /*
  539. * Setup early cpu_to_node.
  540. *
  541. * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
  542. * and apicid_to_node[] tables have valid entries for a CPU.
  543. * This means we skip cpu_to_node[] initialisation for NUMA
  544. * emulation and faking node case (when running a kernel compiled
  545. * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
  546. * is already initialized in a round robin manner at numa_init_array,
  547. * prior to this call, and this initialization is good enough
  548. * for the fake NUMA cases.
  549. */
  550. void __init init_cpu_to_node(void)
  551. {
  552. int i;
  553. for (i = 0; i < NR_CPUS; i++) {
  554. u16 apicid = x86_cpu_to_apicid_init[i];
  555. if (apicid == BAD_APICID)
  556. continue;
  557. if (apicid_to_node[apicid] == NUMA_NO_NODE)
  558. continue;
  559. numa_set_node(i, apicid_to_node[apicid]);
  560. }
  561. }