numa_64.c 17 KB

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