numa_64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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/memblock.h>
  11. #include <linux/mmzone.h>
  12. #include <linux/ctype.h>
  13. #include <linux/module.h>
  14. #include <linux/nodemask.h>
  15. #include <linux/sched.h>
  16. #include <asm/e820.h>
  17. #include <asm/proto.h>
  18. #include <asm/dma.h>
  19. #include <asm/numa.h>
  20. #include <asm/acpi.h>
  21. #include <asm/amd_nb.h>
  22. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  23. EXPORT_SYMBOL(node_data);
  24. struct memnode memnode;
  25. static unsigned long __initdata nodemap_addr;
  26. static unsigned long __initdata nodemap_size;
  27. /*
  28. * Given a shift value, try to populate memnodemap[]
  29. * Returns :
  30. * 1 if OK
  31. * 0 if memnodmap[] too small (of shift too small)
  32. * -1 if node overlap or lost ram (shift too big)
  33. */
  34. static int __init populate_memnodemap(const struct bootnode *nodes,
  35. int numnodes, int shift, int *nodeids)
  36. {
  37. unsigned long addr, end;
  38. int i, res = -1;
  39. memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
  40. for (i = 0; i < numnodes; i++) {
  41. addr = nodes[i].start;
  42. end = nodes[i].end;
  43. if (addr >= end)
  44. continue;
  45. if ((end >> shift) >= memnodemapsize)
  46. return 0;
  47. do {
  48. if (memnodemap[addr >> shift] != NUMA_NO_NODE)
  49. return -1;
  50. if (!nodeids)
  51. memnodemap[addr >> shift] = i;
  52. else
  53. memnodemap[addr >> shift] = nodeids[i];
  54. addr += (1UL << shift);
  55. } while (addr < end);
  56. res = 1;
  57. }
  58. return res;
  59. }
  60. static int __init allocate_cachealigned_memnodemap(void)
  61. {
  62. unsigned long addr;
  63. memnodemap = memnode.embedded_map;
  64. if (memnodemapsize <= ARRAY_SIZE(memnode.embedded_map))
  65. return 0;
  66. addr = 0x8000;
  67. nodemap_size = roundup(sizeof(s16) * memnodemapsize, L1_CACHE_BYTES);
  68. nodemap_addr = memblock_find_in_range(addr, max_pfn<<PAGE_SHIFT,
  69. nodemap_size, L1_CACHE_BYTES);
  70. if (nodemap_addr == MEMBLOCK_ERROR) {
  71. printk(KERN_ERR
  72. "NUMA: Unable to allocate Memory to Node hash map\n");
  73. nodemap_addr = nodemap_size = 0;
  74. return -1;
  75. }
  76. memnodemap = phys_to_virt(nodemap_addr);
  77. memblock_x86_reserve_range(nodemap_addr, nodemap_addr + nodemap_size, "MEMNODEMAP");
  78. printk(KERN_DEBUG "NUMA: Allocated memnodemap from %lx - %lx\n",
  79. nodemap_addr, nodemap_addr + nodemap_size);
  80. return 0;
  81. }
  82. /*
  83. * The LSB of all start and end addresses in the node map is the value of the
  84. * maximum possible shift.
  85. */
  86. static int __init extract_lsb_from_nodes(const struct bootnode *nodes,
  87. int numnodes)
  88. {
  89. int i, nodes_used = 0;
  90. unsigned long start, end;
  91. unsigned long bitfield = 0, memtop = 0;
  92. for (i = 0; i < numnodes; i++) {
  93. start = nodes[i].start;
  94. end = nodes[i].end;
  95. if (start >= end)
  96. continue;
  97. bitfield |= start;
  98. nodes_used++;
  99. if (end > memtop)
  100. memtop = end;
  101. }
  102. if (nodes_used <= 1)
  103. i = 63;
  104. else
  105. i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
  106. memnodemapsize = (memtop >> i)+1;
  107. return i;
  108. }
  109. int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
  110. int *nodeids)
  111. {
  112. int shift;
  113. shift = extract_lsb_from_nodes(nodes, numnodes);
  114. if (allocate_cachealigned_memnodemap())
  115. return -1;
  116. printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
  117. shift);
  118. if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
  119. printk(KERN_INFO "Your memory is not aligned you need to "
  120. "rebuild your kernel with a bigger NODEMAPSIZE "
  121. "shift=%d\n", shift);
  122. return -1;
  123. }
  124. return shift;
  125. }
  126. int __meminit __early_pfn_to_nid(unsigned long pfn)
  127. {
  128. return phys_to_nid(pfn << PAGE_SHIFT);
  129. }
  130. static void * __init early_node_mem(int nodeid, unsigned long start,
  131. unsigned long end, unsigned long size,
  132. unsigned long align)
  133. {
  134. unsigned long mem;
  135. /*
  136. * put it on high as possible
  137. * something will go with NODE_DATA
  138. */
  139. if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
  140. start = MAX_DMA_PFN<<PAGE_SHIFT;
  141. if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
  142. end > (MAX_DMA32_PFN<<PAGE_SHIFT))
  143. start = MAX_DMA32_PFN<<PAGE_SHIFT;
  144. mem = memblock_x86_find_in_range_node(nodeid, start, end, size, align);
  145. if (mem != MEMBLOCK_ERROR)
  146. return __va(mem);
  147. /* extend the search scope */
  148. end = max_pfn_mapped << PAGE_SHIFT;
  149. start = MAX_DMA_PFN << PAGE_SHIFT;
  150. mem = memblock_find_in_range(start, end, size, align);
  151. if (mem != MEMBLOCK_ERROR)
  152. return __va(mem);
  153. printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
  154. size, nodeid);
  155. return NULL;
  156. }
  157. /* Initialize bootmem allocator for a node */
  158. void __init
  159. setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
  160. {
  161. unsigned long start_pfn, last_pfn, nodedata_phys;
  162. const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
  163. int nid;
  164. if (!end)
  165. return;
  166. /*
  167. * Don't confuse VM with a node that doesn't have the
  168. * minimum amount of memory:
  169. */
  170. if (end && (end - start) < NODE_MIN_SIZE)
  171. return;
  172. start = roundup(start, ZONE_ALIGN);
  173. printk(KERN_INFO "Initmem setup node %d %016lx-%016lx\n", nodeid,
  174. start, end);
  175. start_pfn = start >> PAGE_SHIFT;
  176. last_pfn = end >> PAGE_SHIFT;
  177. node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
  178. SMP_CACHE_BYTES);
  179. if (node_data[nodeid] == NULL)
  180. return;
  181. nodedata_phys = __pa(node_data[nodeid]);
  182. memblock_x86_reserve_range(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
  183. printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
  184. nodedata_phys + pgdat_size - 1);
  185. nid = phys_to_nid(nodedata_phys);
  186. if (nid != nodeid)
  187. printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
  188. memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
  189. NODE_DATA(nodeid)->node_id = nodeid;
  190. NODE_DATA(nodeid)->node_start_pfn = start_pfn;
  191. NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
  192. node_set_online(nodeid);
  193. }
  194. #ifdef CONFIG_NUMA_EMU
  195. /* Numa emulation */
  196. static struct bootnode nodes[MAX_NUMNODES] __initdata;
  197. static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
  198. static char *cmdline __initdata;
  199. void __init numa_emu_cmdline(char *str)
  200. {
  201. cmdline = str;
  202. }
  203. static int __init setup_physnodes(unsigned long start, unsigned long end,
  204. int acpi, int amd)
  205. {
  206. int ret = 0;
  207. int i;
  208. memset(physnodes, 0, sizeof(physnodes));
  209. #ifdef CONFIG_ACPI_NUMA
  210. if (acpi)
  211. acpi_get_nodes(physnodes, start, end);
  212. #endif
  213. #ifdef CONFIG_AMD_NUMA
  214. if (amd)
  215. amd_get_nodes(physnodes);
  216. #endif
  217. /*
  218. * Basic sanity checking on the physical node map: there may be errors
  219. * if the SRAT or AMD code incorrectly reported the topology or the mem=
  220. * kernel parameter is used.
  221. */
  222. for (i = 0; i < MAX_NUMNODES; i++) {
  223. if (physnodes[i].start == physnodes[i].end)
  224. continue;
  225. if (physnodes[i].start > end) {
  226. physnodes[i].end = physnodes[i].start;
  227. continue;
  228. }
  229. if (physnodes[i].end < start) {
  230. physnodes[i].start = physnodes[i].end;
  231. continue;
  232. }
  233. if (physnodes[i].start < start)
  234. physnodes[i].start = start;
  235. if (physnodes[i].end > end)
  236. physnodes[i].end = end;
  237. ret++;
  238. }
  239. /*
  240. * If no physical topology was detected, a single node is faked to cover
  241. * the entire address space.
  242. */
  243. if (!ret) {
  244. physnodes[ret].start = start;
  245. physnodes[ret].end = end;
  246. ret = 1;
  247. }
  248. return ret;
  249. }
  250. static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
  251. {
  252. int i;
  253. BUG_ON(acpi && amd);
  254. #ifdef CONFIG_ACPI_NUMA
  255. if (acpi)
  256. acpi_fake_nodes(nodes, nr_nodes);
  257. #endif
  258. #ifdef CONFIG_AMD_NUMA
  259. if (amd)
  260. amd_fake_nodes(nodes, nr_nodes);
  261. #endif
  262. if (!acpi && !amd)
  263. for (i = 0; i < nr_cpu_ids; i++)
  264. numa_set_node(i, 0);
  265. }
  266. /*
  267. * Setups up nid to range from addr to addr + size. If the end
  268. * boundary is greater than max_addr, then max_addr is used instead.
  269. * The return value is 0 if there is additional memory left for
  270. * allocation past addr and -1 otherwise. addr is adjusted to be at
  271. * the end of the node.
  272. */
  273. static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
  274. {
  275. int ret = 0;
  276. nodes[nid].start = *addr;
  277. *addr += size;
  278. if (*addr >= max_addr) {
  279. *addr = max_addr;
  280. ret = -1;
  281. }
  282. nodes[nid].end = *addr;
  283. node_set(nid, node_possible_map);
  284. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
  285. nodes[nid].start, nodes[nid].end,
  286. (nodes[nid].end - nodes[nid].start) >> 20);
  287. return ret;
  288. }
  289. /*
  290. * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
  291. * to max_addr. The return value is the number of nodes allocated.
  292. */
  293. static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
  294. {
  295. nodemask_t physnode_mask = NODE_MASK_NONE;
  296. u64 size;
  297. int big;
  298. int ret = 0;
  299. int i;
  300. if (nr_nodes <= 0)
  301. return -1;
  302. if (nr_nodes > MAX_NUMNODES) {
  303. pr_info("numa=fake=%d too large, reducing to %d\n",
  304. nr_nodes, MAX_NUMNODES);
  305. nr_nodes = MAX_NUMNODES;
  306. }
  307. size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
  308. /*
  309. * Calculate the number of big nodes that can be allocated as a result
  310. * of consolidating the remainder.
  311. */
  312. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
  313. FAKE_NODE_MIN_SIZE;
  314. size &= FAKE_NODE_MIN_HASH_MASK;
  315. if (!size) {
  316. pr_err("Not enough memory for each node. "
  317. "NUMA emulation disabled.\n");
  318. return -1;
  319. }
  320. for (i = 0; i < MAX_NUMNODES; i++)
  321. if (physnodes[i].start != physnodes[i].end)
  322. node_set(i, physnode_mask);
  323. /*
  324. * Continue to fill physical nodes with fake nodes until there is no
  325. * memory left on any of them.
  326. */
  327. while (nodes_weight(physnode_mask)) {
  328. for_each_node_mask(i, physnode_mask) {
  329. u64 end = physnodes[i].start + size;
  330. u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
  331. if (ret < big)
  332. end += FAKE_NODE_MIN_SIZE;
  333. /*
  334. * Continue to add memory to this fake node if its
  335. * non-reserved memory is less than the per-node size.
  336. */
  337. while (end - physnodes[i].start -
  338. memblock_x86_hole_size(physnodes[i].start, end) < size) {
  339. end += FAKE_NODE_MIN_SIZE;
  340. if (end > physnodes[i].end) {
  341. end = physnodes[i].end;
  342. break;
  343. }
  344. }
  345. /*
  346. * If there won't be at least FAKE_NODE_MIN_SIZE of
  347. * non-reserved memory in ZONE_DMA32 for the next node,
  348. * this one must extend to the boundary.
  349. */
  350. if (end < dma32_end && dma32_end - end -
  351. memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  352. end = dma32_end;
  353. /*
  354. * If there won't be enough non-reserved memory for the
  355. * next node, this one must extend to the end of the
  356. * physical node.
  357. */
  358. if (physnodes[i].end - end -
  359. memblock_x86_hole_size(end, physnodes[i].end) < size)
  360. end = physnodes[i].end;
  361. /*
  362. * Avoid allocating more nodes than requested, which can
  363. * happen as a result of rounding down each node's size
  364. * to FAKE_NODE_MIN_SIZE.
  365. */
  366. if (nodes_weight(physnode_mask) + ret >= nr_nodes)
  367. end = physnodes[i].end;
  368. if (setup_node_range(ret++, &physnodes[i].start,
  369. end - physnodes[i].start,
  370. physnodes[i].end) < 0)
  371. node_clear(i, physnode_mask);
  372. }
  373. }
  374. return ret;
  375. }
  376. /*
  377. * Returns the end address of a node so that there is at least `size' amount of
  378. * non-reserved memory or `max_addr' is reached.
  379. */
  380. static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
  381. {
  382. u64 end = start + size;
  383. while (end - start - memblock_x86_hole_size(start, end) < size) {
  384. end += FAKE_NODE_MIN_SIZE;
  385. if (end > max_addr) {
  386. end = max_addr;
  387. break;
  388. }
  389. }
  390. return end;
  391. }
  392. /*
  393. * Sets up fake nodes of `size' interleaved over physical nodes ranging from
  394. * `addr' to `max_addr'. The return value is the number of nodes allocated.
  395. */
  396. static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
  397. {
  398. nodemask_t physnode_mask = NODE_MASK_NONE;
  399. u64 min_size;
  400. int ret = 0;
  401. int i;
  402. if (!size)
  403. return -1;
  404. /*
  405. * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
  406. * increased accordingly if the requested size is too small. This
  407. * creates a uniform distribution of node sizes across the entire
  408. * machine (but not necessarily over physical nodes).
  409. */
  410. min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
  411. MAX_NUMNODES;
  412. min_size = max(min_size, FAKE_NODE_MIN_SIZE);
  413. if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
  414. min_size = (min_size + FAKE_NODE_MIN_SIZE) &
  415. FAKE_NODE_MIN_HASH_MASK;
  416. if (size < min_size) {
  417. pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
  418. size >> 20, min_size >> 20);
  419. size = min_size;
  420. }
  421. size &= FAKE_NODE_MIN_HASH_MASK;
  422. for (i = 0; i < MAX_NUMNODES; i++)
  423. if (physnodes[i].start != physnodes[i].end)
  424. node_set(i, physnode_mask);
  425. /*
  426. * Fill physical nodes with fake nodes of size until there is no memory
  427. * left on any of them.
  428. */
  429. while (nodes_weight(physnode_mask)) {
  430. for_each_node_mask(i, physnode_mask) {
  431. u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
  432. u64 end;
  433. end = find_end_of_node(physnodes[i].start,
  434. physnodes[i].end, size);
  435. /*
  436. * If there won't be at least FAKE_NODE_MIN_SIZE of
  437. * non-reserved memory in ZONE_DMA32 for the next node,
  438. * this one must extend to the boundary.
  439. */
  440. if (end < dma32_end && dma32_end - end -
  441. memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  442. end = dma32_end;
  443. /*
  444. * If there won't be enough non-reserved memory for the
  445. * next node, this one must extend to the end of the
  446. * physical node.
  447. */
  448. if (physnodes[i].end - end -
  449. memblock_x86_hole_size(end, physnodes[i].end) < size)
  450. end = physnodes[i].end;
  451. /*
  452. * Setup the fake node that will be allocated as bootmem
  453. * later. If setup_node_range() returns non-zero, there
  454. * is no more memory available on this physical node.
  455. */
  456. if (setup_node_range(ret++, &physnodes[i].start,
  457. end - physnodes[i].start,
  458. physnodes[i].end) < 0)
  459. node_clear(i, physnode_mask);
  460. }
  461. }
  462. return ret;
  463. }
  464. /*
  465. * Sets up the system RAM area from start_pfn to last_pfn according to the
  466. * numa=fake command-line option.
  467. */
  468. static int __init numa_emulation(unsigned long start_pfn,
  469. unsigned long last_pfn, int acpi, int amd)
  470. {
  471. u64 addr = start_pfn << PAGE_SHIFT;
  472. u64 max_addr = last_pfn << PAGE_SHIFT;
  473. int num_nodes;
  474. int i;
  475. /*
  476. * If the numa=fake command-line contains a 'M' or 'G', it represents
  477. * the fixed node size. Otherwise, if it is just a single number N,
  478. * split the system RAM into N fake nodes.
  479. */
  480. if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
  481. u64 size;
  482. size = memparse(cmdline, &cmdline);
  483. num_nodes = split_nodes_size_interleave(addr, max_addr, size);
  484. } else {
  485. unsigned long n;
  486. n = simple_strtoul(cmdline, NULL, 0);
  487. num_nodes = split_nodes_interleave(addr, max_addr, n);
  488. }
  489. if (num_nodes < 0)
  490. return num_nodes;
  491. memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
  492. if (memnode_shift < 0) {
  493. memnode_shift = 0;
  494. printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
  495. "disabled.\n");
  496. return -1;
  497. }
  498. /*
  499. * We need to vacate all active ranges that may have been registered for
  500. * the e820 memory map.
  501. */
  502. remove_all_active_ranges();
  503. for_each_node_mask(i, node_possible_map) {
  504. memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  505. nodes[i].end >> PAGE_SHIFT);
  506. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  507. }
  508. setup_physnodes(addr, max_addr, acpi, amd);
  509. fake_physnodes(acpi, amd, num_nodes);
  510. numa_init_array();
  511. return 0;
  512. }
  513. #endif /* CONFIG_NUMA_EMU */
  514. void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn,
  515. int acpi, int amd)
  516. {
  517. int i;
  518. nodes_clear(node_possible_map);
  519. nodes_clear(node_online_map);
  520. #ifdef CONFIG_NUMA_EMU
  521. setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
  522. acpi, amd);
  523. if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, amd))
  524. return;
  525. setup_physnodes(start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT,
  526. acpi, amd);
  527. nodes_clear(node_possible_map);
  528. nodes_clear(node_online_map);
  529. #endif
  530. #ifdef CONFIG_ACPI_NUMA
  531. if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
  532. last_pfn << PAGE_SHIFT))
  533. return;
  534. nodes_clear(node_possible_map);
  535. nodes_clear(node_online_map);
  536. #endif
  537. #ifdef CONFIG_AMD_NUMA
  538. if (!numa_off && amd && !amd_scan_nodes())
  539. return;
  540. nodes_clear(node_possible_map);
  541. nodes_clear(node_online_map);
  542. #endif
  543. printk(KERN_INFO "%s\n",
  544. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  545. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  546. start_pfn << PAGE_SHIFT,
  547. last_pfn << PAGE_SHIFT);
  548. /* setup dummy node covering all memory */
  549. memnode_shift = 63;
  550. memnodemap = memnode.embedded_map;
  551. memnodemap[0] = 0;
  552. node_set_online(0);
  553. node_set(0, node_possible_map);
  554. for (i = 0; i < nr_cpu_ids; i++)
  555. numa_set_node(i, 0);
  556. memblock_x86_register_active_regions(0, start_pfn, last_pfn);
  557. setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
  558. }
  559. unsigned long __init numa_free_all_bootmem(void)
  560. {
  561. unsigned long pages = 0;
  562. int i;
  563. for_each_online_node(i)
  564. pages += free_all_bootmem_node(NODE_DATA(i));
  565. pages += free_all_memory_core_early(MAX_NUMNODES);
  566. return pages;
  567. }
  568. int __cpuinit numa_cpu_node(int cpu)
  569. {
  570. int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
  571. if (apicid != BAD_APICID)
  572. return __apicid_to_node[apicid];
  573. return NUMA_NO_NODE;
  574. }
  575. /*
  576. * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
  577. * of 64bit specific data structures. The distinction is artificial and
  578. * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
  579. * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
  580. * enabled.
  581. *
  582. * NUMA emulation is planned to be made generic and the following and other
  583. * related code should be moved to numa.c.
  584. */
  585. #ifdef CONFIG_NUMA_EMU
  586. # ifndef CONFIG_DEBUG_PER_CPU_MAPS
  587. void __cpuinit numa_add_cpu(int cpu)
  588. {
  589. unsigned long addr;
  590. int physnid, nid;
  591. nid = numa_cpu_node(cpu);
  592. if (nid == NUMA_NO_NODE)
  593. nid = early_cpu_to_node(cpu);
  594. BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
  595. /*
  596. * Use the starting address of the emulated node to find which physical
  597. * node it is allocated on.
  598. */
  599. addr = node_start_pfn(nid) << PAGE_SHIFT;
  600. for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
  601. if (addr >= physnodes[physnid].start &&
  602. addr < physnodes[physnid].end)
  603. break;
  604. /*
  605. * Map the cpu to each emulated node that is allocated on the physical
  606. * node of the cpu's apic id.
  607. */
  608. for_each_online_node(nid) {
  609. addr = node_start_pfn(nid) << PAGE_SHIFT;
  610. if (addr >= physnodes[physnid].start &&
  611. addr < physnodes[physnid].end)
  612. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  613. }
  614. }
  615. void __cpuinit numa_remove_cpu(int cpu)
  616. {
  617. int i;
  618. for_each_online_node(i)
  619. cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
  620. }
  621. # else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  622. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  623. {
  624. int node = early_cpu_to_node(cpu);
  625. struct cpumask *mask;
  626. int i;
  627. if (node == NUMA_NO_NODE) {
  628. /* early_cpu_to_node() already emits a warning and trace */
  629. return;
  630. }
  631. for_each_online_node(i) {
  632. unsigned long addr;
  633. addr = node_start_pfn(i) << PAGE_SHIFT;
  634. if (addr < physnodes[node].start ||
  635. addr >= physnodes[node].end)
  636. continue;
  637. mask = debug_cpumask_set_cpu(cpu, enable);
  638. if (!mask)
  639. return;
  640. if (enable)
  641. cpumask_set_cpu(cpu, mask);
  642. else
  643. cpumask_clear_cpu(cpu, mask);
  644. }
  645. }
  646. void __cpuinit numa_add_cpu(int cpu)
  647. {
  648. numa_set_cpumask(cpu, 1);
  649. }
  650. void __cpuinit numa_remove_cpu(int cpu)
  651. {
  652. numa_set_cpumask(cpu, 0);
  653. }
  654. # endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
  655. #endif /* CONFIG_NUMA_EMU */