numa_64.c 20 KB

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