numa_64.c 23 KB

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