numa_64.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. /*
  249. * Sanity check to catch more bad NUMA configurations (they are amazingly
  250. * common). Make sure the nodes cover all memory.
  251. */
  252. static int __init nodes_cover_memory(const struct bootnode *nodes)
  253. {
  254. unsigned long numaram, e820ram;
  255. int i;
  256. numaram = 0;
  257. for_each_node_mask(i, mem_nodes_parsed) {
  258. unsigned long s = nodes[i].start >> PAGE_SHIFT;
  259. unsigned long e = nodes[i].end >> PAGE_SHIFT;
  260. numaram += e - s;
  261. numaram -= __absent_pages_in_range(i, s, e);
  262. if ((long)numaram < 0)
  263. numaram = 0;
  264. }
  265. e820ram = max_pfn -
  266. (memblock_x86_hole_size(0, max_pfn<<PAGE_SHIFT) >> PAGE_SHIFT);
  267. /* We seem to lose 3 pages somewhere. Allow 1M of slack. */
  268. if ((long)(e820ram - numaram) >= (1<<(20 - PAGE_SHIFT))) {
  269. printk(KERN_ERR "NUMA: nodes only cover %luMB of your %luMB e820 RAM. Not used.\n",
  270. (numaram << PAGE_SHIFT) >> 20,
  271. (e820ram << PAGE_SHIFT) >> 20);
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. static int __init numa_register_memblks(void)
  277. {
  278. int i;
  279. /*
  280. * Join together blocks on the same node, holes between
  281. * which don't overlap with memory on other nodes.
  282. */
  283. for (i = 0; i < num_node_memblks; ++i) {
  284. int j, k;
  285. for (j = i + 1; j < num_node_memblks; ++j) {
  286. unsigned long start, end;
  287. if (memblk_nodeid[i] != memblk_nodeid[j])
  288. continue;
  289. start = min(node_memblk_range[i].end,
  290. node_memblk_range[j].end);
  291. end = max(node_memblk_range[i].start,
  292. node_memblk_range[j].start);
  293. for (k = 0; k < num_node_memblks; ++k) {
  294. if (memblk_nodeid[i] == memblk_nodeid[k])
  295. continue;
  296. if (start < node_memblk_range[k].end &&
  297. end > node_memblk_range[k].start)
  298. break;
  299. }
  300. if (k < num_node_memblks)
  301. continue;
  302. start = min(node_memblk_range[i].start,
  303. node_memblk_range[j].start);
  304. end = max(node_memblk_range[i].end,
  305. node_memblk_range[j].end);
  306. printk(KERN_INFO "NUMA: Node %d [%Lx,%Lx) + [%Lx,%Lx) -> [%lx,%lx)\n",
  307. memblk_nodeid[i],
  308. node_memblk_range[i].start,
  309. node_memblk_range[i].end,
  310. node_memblk_range[j].start,
  311. node_memblk_range[j].end,
  312. start, end);
  313. node_memblk_range[i].start = start;
  314. node_memblk_range[i].end = end;
  315. k = --num_node_memblks - j;
  316. memmove(memblk_nodeid + j, memblk_nodeid + j+1,
  317. k * sizeof(*memblk_nodeid));
  318. memmove(node_memblk_range + j, node_memblk_range + j+1,
  319. k * sizeof(*node_memblk_range));
  320. --j;
  321. }
  322. }
  323. memnode_shift = compute_hash_shift(node_memblk_range, num_node_memblks,
  324. memblk_nodeid);
  325. if (memnode_shift < 0) {
  326. printk(KERN_ERR "NUMA: No NUMA node hash function found. Contact maintainer\n");
  327. return -EINVAL;
  328. }
  329. for (i = 0; i < num_node_memblks; i++)
  330. memblock_x86_register_active_regions(memblk_nodeid[i],
  331. node_memblk_range[i].start >> PAGE_SHIFT,
  332. node_memblk_range[i].end >> PAGE_SHIFT);
  333. /* for out of order entries */
  334. sort_node_map();
  335. if (!nodes_cover_memory(numa_nodes))
  336. return -EINVAL;
  337. init_memory_mapping_high();
  338. /* Finally register nodes. */
  339. for_each_node_mask(i, node_possible_map)
  340. setup_node_bootmem(i, numa_nodes[i].start, numa_nodes[i].end);
  341. /*
  342. * Try again in case setup_node_bootmem missed one due to missing
  343. * bootmem.
  344. */
  345. for_each_node_mask(i, node_possible_map)
  346. if (!node_online(i))
  347. setup_node_bootmem(i, numa_nodes[i].start,
  348. numa_nodes[i].end);
  349. return 0;
  350. }
  351. #ifdef CONFIG_NUMA_EMU
  352. /* Numa emulation */
  353. static struct bootnode nodes[MAX_NUMNODES] __initdata;
  354. static struct bootnode physnodes[MAX_NUMNODES] __cpuinitdata;
  355. static char *cmdline __initdata;
  356. void __init numa_emu_cmdline(char *str)
  357. {
  358. cmdline = str;
  359. }
  360. static int __init setup_physnodes(unsigned long start, unsigned long end)
  361. {
  362. int ret = 0;
  363. int i;
  364. memset(physnodes, 0, sizeof(physnodes));
  365. for_each_node_mask(i, mem_nodes_parsed) {
  366. physnodes[i].start = numa_nodes[i].start;
  367. physnodes[i].end = numa_nodes[i].end;
  368. }
  369. /*
  370. * Basic sanity checking on the physical node map: there may be errors
  371. * if the SRAT or AMD code incorrectly reported the topology or the mem=
  372. * kernel parameter is used.
  373. */
  374. for (i = 0; i < MAX_NUMNODES; i++) {
  375. if (physnodes[i].start == physnodes[i].end)
  376. continue;
  377. if (physnodes[i].start > end) {
  378. physnodes[i].end = physnodes[i].start;
  379. continue;
  380. }
  381. if (physnodes[i].end < start) {
  382. physnodes[i].start = physnodes[i].end;
  383. continue;
  384. }
  385. if (physnodes[i].start < start)
  386. physnodes[i].start = start;
  387. if (physnodes[i].end > end)
  388. physnodes[i].end = end;
  389. ret++;
  390. }
  391. /*
  392. * If no physical topology was detected, a single node is faked to cover
  393. * the entire address space.
  394. */
  395. if (!ret) {
  396. physnodes[ret].start = start;
  397. physnodes[ret].end = end;
  398. ret = 1;
  399. }
  400. return ret;
  401. }
  402. static void __init fake_physnodes(int acpi, int amd, int nr_nodes)
  403. {
  404. int i;
  405. BUG_ON(acpi && amd);
  406. #ifdef CONFIG_ACPI_NUMA
  407. if (acpi)
  408. acpi_fake_nodes(nodes, nr_nodes);
  409. #endif
  410. #ifdef CONFIG_AMD_NUMA
  411. if (amd)
  412. amd_fake_nodes(nodes, nr_nodes);
  413. #endif
  414. if (!acpi && !amd)
  415. for (i = 0; i < nr_cpu_ids; i++)
  416. numa_set_node(i, 0);
  417. }
  418. /*
  419. * Setups up nid to range from addr to addr + size. If the end
  420. * boundary is greater than max_addr, then max_addr is used instead.
  421. * The return value is 0 if there is additional memory left for
  422. * allocation past addr and -1 otherwise. addr is adjusted to be at
  423. * the end of the node.
  424. */
  425. static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
  426. {
  427. int ret = 0;
  428. nodes[nid].start = *addr;
  429. *addr += size;
  430. if (*addr >= max_addr) {
  431. *addr = max_addr;
  432. ret = -1;
  433. }
  434. nodes[nid].end = *addr;
  435. node_set(nid, node_possible_map);
  436. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
  437. nodes[nid].start, nodes[nid].end,
  438. (nodes[nid].end - nodes[nid].start) >> 20);
  439. return ret;
  440. }
  441. /*
  442. * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
  443. * to max_addr. The return value is the number of nodes allocated.
  444. */
  445. static int __init split_nodes_interleave(u64 addr, u64 max_addr, int nr_nodes)
  446. {
  447. nodemask_t physnode_mask = NODE_MASK_NONE;
  448. u64 size;
  449. int big;
  450. int ret = 0;
  451. int i;
  452. if (nr_nodes <= 0)
  453. return -1;
  454. if (nr_nodes > MAX_NUMNODES) {
  455. pr_info("numa=fake=%d too large, reducing to %d\n",
  456. nr_nodes, MAX_NUMNODES);
  457. nr_nodes = MAX_NUMNODES;
  458. }
  459. size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
  460. /*
  461. * Calculate the number of big nodes that can be allocated as a result
  462. * of consolidating the remainder.
  463. */
  464. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
  465. FAKE_NODE_MIN_SIZE;
  466. size &= FAKE_NODE_MIN_HASH_MASK;
  467. if (!size) {
  468. pr_err("Not enough memory for each node. "
  469. "NUMA emulation disabled.\n");
  470. return -1;
  471. }
  472. for (i = 0; i < MAX_NUMNODES; i++)
  473. if (physnodes[i].start != physnodes[i].end)
  474. node_set(i, physnode_mask);
  475. /*
  476. * Continue to fill physical nodes with fake nodes until there is no
  477. * memory left on any of them.
  478. */
  479. while (nodes_weight(physnode_mask)) {
  480. for_each_node_mask(i, physnode_mask) {
  481. u64 end = physnodes[i].start + size;
  482. u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
  483. if (ret < big)
  484. end += FAKE_NODE_MIN_SIZE;
  485. /*
  486. * Continue to add memory to this fake node if its
  487. * non-reserved memory is less than the per-node size.
  488. */
  489. while (end - physnodes[i].start -
  490. memblock_x86_hole_size(physnodes[i].start, end) < size) {
  491. end += FAKE_NODE_MIN_SIZE;
  492. if (end > physnodes[i].end) {
  493. end = physnodes[i].end;
  494. break;
  495. }
  496. }
  497. /*
  498. * If there won't be at least FAKE_NODE_MIN_SIZE of
  499. * non-reserved memory in ZONE_DMA32 for the next node,
  500. * this one must extend to the boundary.
  501. */
  502. if (end < dma32_end && dma32_end - end -
  503. memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  504. end = dma32_end;
  505. /*
  506. * If there won't be enough non-reserved memory for the
  507. * next node, this one must extend to the end of the
  508. * physical node.
  509. */
  510. if (physnodes[i].end - end -
  511. memblock_x86_hole_size(end, physnodes[i].end) < size)
  512. end = physnodes[i].end;
  513. /*
  514. * Avoid allocating more nodes than requested, which can
  515. * happen as a result of rounding down each node's size
  516. * to FAKE_NODE_MIN_SIZE.
  517. */
  518. if (nodes_weight(physnode_mask) + ret >= nr_nodes)
  519. end = physnodes[i].end;
  520. if (setup_node_range(ret++, &physnodes[i].start,
  521. end - physnodes[i].start,
  522. physnodes[i].end) < 0)
  523. node_clear(i, physnode_mask);
  524. }
  525. }
  526. return ret;
  527. }
  528. /*
  529. * Returns the end address of a node so that there is at least `size' amount of
  530. * non-reserved memory or `max_addr' is reached.
  531. */
  532. static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
  533. {
  534. u64 end = start + size;
  535. while (end - start - memblock_x86_hole_size(start, end) < size) {
  536. end += FAKE_NODE_MIN_SIZE;
  537. if (end > max_addr) {
  538. end = max_addr;
  539. break;
  540. }
  541. }
  542. return end;
  543. }
  544. /*
  545. * Sets up fake nodes of `size' interleaved over physical nodes ranging from
  546. * `addr' to `max_addr'. The return value is the number of nodes allocated.
  547. */
  548. static int __init split_nodes_size_interleave(u64 addr, u64 max_addr, u64 size)
  549. {
  550. nodemask_t physnode_mask = NODE_MASK_NONE;
  551. u64 min_size;
  552. int ret = 0;
  553. int i;
  554. if (!size)
  555. return -1;
  556. /*
  557. * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
  558. * increased accordingly if the requested size is too small. This
  559. * creates a uniform distribution of node sizes across the entire
  560. * machine (but not necessarily over physical nodes).
  561. */
  562. min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
  563. MAX_NUMNODES;
  564. min_size = max(min_size, FAKE_NODE_MIN_SIZE);
  565. if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
  566. min_size = (min_size + FAKE_NODE_MIN_SIZE) &
  567. FAKE_NODE_MIN_HASH_MASK;
  568. if (size < min_size) {
  569. pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
  570. size >> 20, min_size >> 20);
  571. size = min_size;
  572. }
  573. size &= FAKE_NODE_MIN_HASH_MASK;
  574. for (i = 0; i < MAX_NUMNODES; i++)
  575. if (physnodes[i].start != physnodes[i].end)
  576. node_set(i, physnode_mask);
  577. /*
  578. * Fill physical nodes with fake nodes of size until there is no memory
  579. * left on any of them.
  580. */
  581. while (nodes_weight(physnode_mask)) {
  582. for_each_node_mask(i, physnode_mask) {
  583. u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
  584. u64 end;
  585. end = find_end_of_node(physnodes[i].start,
  586. physnodes[i].end, size);
  587. /*
  588. * If there won't be at least FAKE_NODE_MIN_SIZE of
  589. * non-reserved memory in ZONE_DMA32 for the next node,
  590. * this one must extend to the boundary.
  591. */
  592. if (end < dma32_end && dma32_end - end -
  593. memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  594. end = dma32_end;
  595. /*
  596. * If there won't be enough non-reserved memory for the
  597. * next node, this one must extend to the end of the
  598. * physical node.
  599. */
  600. if (physnodes[i].end - end -
  601. memblock_x86_hole_size(end, physnodes[i].end) < size)
  602. end = physnodes[i].end;
  603. /*
  604. * Setup the fake node that will be allocated as bootmem
  605. * later. If setup_node_range() returns non-zero, there
  606. * is no more memory available on this physical node.
  607. */
  608. if (setup_node_range(ret++, &physnodes[i].start,
  609. end - physnodes[i].start,
  610. physnodes[i].end) < 0)
  611. node_clear(i, physnode_mask);
  612. }
  613. }
  614. return ret;
  615. }
  616. /*
  617. * Sets up the system RAM area from start_pfn to last_pfn according to the
  618. * numa=fake command-line option.
  619. */
  620. static int __init numa_emulation(unsigned long start_pfn,
  621. unsigned long last_pfn, int acpi, int amd)
  622. {
  623. u64 addr = start_pfn << PAGE_SHIFT;
  624. u64 max_addr = last_pfn << PAGE_SHIFT;
  625. int num_nodes;
  626. int i;
  627. /*
  628. * If the numa=fake command-line contains a 'M' or 'G', it represents
  629. * the fixed node size. Otherwise, if it is just a single number N,
  630. * split the system RAM into N fake nodes.
  631. */
  632. if (strchr(cmdline, 'M') || strchr(cmdline, 'G')) {
  633. u64 size;
  634. size = memparse(cmdline, &cmdline);
  635. num_nodes = split_nodes_size_interleave(addr, max_addr, size);
  636. } else {
  637. unsigned long n;
  638. n = simple_strtoul(cmdline, NULL, 0);
  639. num_nodes = split_nodes_interleave(addr, max_addr, n);
  640. }
  641. if (num_nodes < 0)
  642. return num_nodes;
  643. memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
  644. if (memnode_shift < 0) {
  645. memnode_shift = 0;
  646. printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
  647. "disabled.\n");
  648. return -1;
  649. }
  650. /*
  651. * We need to vacate all active ranges that may have been registered for
  652. * the e820 memory map.
  653. */
  654. remove_all_active_ranges();
  655. for_each_node_mask(i, node_possible_map)
  656. memblock_x86_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  657. nodes[i].end >> PAGE_SHIFT);
  658. init_memory_mapping_high();
  659. for_each_node_mask(i, node_possible_map)
  660. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  661. setup_physnodes(addr, max_addr);
  662. fake_physnodes(acpi, amd, num_nodes);
  663. numa_init_array();
  664. return 0;
  665. }
  666. #endif /* CONFIG_NUMA_EMU */
  667. static int dummy_numa_init(void)
  668. {
  669. printk(KERN_INFO "%s\n",
  670. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  671. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  672. 0LU, max_pfn << PAGE_SHIFT);
  673. node_set(0, cpu_nodes_parsed);
  674. node_set(0, mem_nodes_parsed);
  675. numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
  676. numa_nodes[0].start = 0;
  677. numa_nodes[0].end = (u64)max_pfn << PAGE_SHIFT;
  678. return 0;
  679. }
  680. void __init initmem_init(void)
  681. {
  682. int (*numa_init[])(void) = { [2] = dummy_numa_init };
  683. int i, j;
  684. if (!numa_off) {
  685. #ifdef CONFIG_ACPI_NUMA
  686. numa_init[0] = x86_acpi_numa_init;
  687. #endif
  688. #ifdef CONFIG_AMD_NUMA
  689. numa_init[1] = amd_numa_init;
  690. #endif
  691. }
  692. for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
  693. if (!numa_init[i])
  694. continue;
  695. for (j = 0; j < MAX_LOCAL_APIC; j++)
  696. set_apicid_to_node(j, NUMA_NO_NODE);
  697. nodes_clear(cpu_nodes_parsed);
  698. nodes_clear(mem_nodes_parsed);
  699. nodes_clear(node_possible_map);
  700. nodes_clear(node_online_map);
  701. num_node_memblks = 0;
  702. memset(node_memblk_range, 0, sizeof(node_memblk_range));
  703. memset(memblk_nodeid, 0, sizeof(memblk_nodeid));
  704. memset(numa_nodes, 0, sizeof(numa_nodes));
  705. remove_all_active_ranges();
  706. if (numa_init[i]() < 0)
  707. continue;
  708. /* clean up the node list */
  709. for (j = 0; j < MAX_NUMNODES; j++)
  710. cutoff_node(j, 0, max_pfn << PAGE_SHIFT);
  711. #ifdef CONFIG_NUMA_EMU
  712. setup_physnodes(0, max_pfn << PAGE_SHIFT);
  713. if (cmdline && !numa_emulation(0, max_pfn, i == 0, i == 1))
  714. return;
  715. setup_physnodes(0, max_pfn << PAGE_SHIFT);
  716. nodes_clear(node_possible_map);
  717. nodes_clear(node_online_map);
  718. #endif
  719. /* Account for nodes with cpus and no memory */
  720. nodes_or(node_possible_map, mem_nodes_parsed, cpu_nodes_parsed);
  721. if (WARN_ON(nodes_empty(node_possible_map)))
  722. continue;
  723. if (numa_register_memblks() < 0)
  724. continue;
  725. for (j = 0; j < nr_cpu_ids; j++) {
  726. int nid = early_cpu_to_node(j);
  727. if (nid == NUMA_NO_NODE)
  728. continue;
  729. if (!node_online(nid))
  730. numa_clear_node(j);
  731. }
  732. numa_init_array();
  733. return;
  734. }
  735. BUG();
  736. }
  737. unsigned long __init numa_free_all_bootmem(void)
  738. {
  739. unsigned long pages = 0;
  740. int i;
  741. for_each_online_node(i)
  742. pages += free_all_bootmem_node(NODE_DATA(i));
  743. pages += free_all_memory_core_early(MAX_NUMNODES);
  744. return pages;
  745. }
  746. int __cpuinit numa_cpu_node(int cpu)
  747. {
  748. int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
  749. if (apicid != BAD_APICID)
  750. return __apicid_to_node[apicid];
  751. return NUMA_NO_NODE;
  752. }
  753. /*
  754. * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
  755. * of 64bit specific data structures. The distinction is artificial and
  756. * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
  757. * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
  758. * enabled.
  759. *
  760. * NUMA emulation is planned to be made generic and the following and other
  761. * related code should be moved to numa.c.
  762. */
  763. #ifdef CONFIG_NUMA_EMU
  764. # ifndef CONFIG_DEBUG_PER_CPU_MAPS
  765. void __cpuinit numa_add_cpu(int cpu)
  766. {
  767. unsigned long addr;
  768. int physnid, nid;
  769. nid = numa_cpu_node(cpu);
  770. if (nid == NUMA_NO_NODE)
  771. nid = early_cpu_to_node(cpu);
  772. BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
  773. /*
  774. * Use the starting address of the emulated node to find which physical
  775. * node it is allocated on.
  776. */
  777. addr = node_start_pfn(nid) << PAGE_SHIFT;
  778. for (physnid = 0; physnid < MAX_NUMNODES; physnid++)
  779. if (addr >= physnodes[physnid].start &&
  780. addr < physnodes[physnid].end)
  781. break;
  782. /*
  783. * Map the cpu to each emulated node that is allocated on the physical
  784. * node of the cpu's apic id.
  785. */
  786. for_each_online_node(nid) {
  787. addr = node_start_pfn(nid) << PAGE_SHIFT;
  788. if (addr >= physnodes[physnid].start &&
  789. addr < physnodes[physnid].end)
  790. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  791. }
  792. }
  793. void __cpuinit numa_remove_cpu(int cpu)
  794. {
  795. int i;
  796. for_each_online_node(i)
  797. cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
  798. }
  799. # else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  800. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  801. {
  802. int node = early_cpu_to_node(cpu);
  803. struct cpumask *mask;
  804. int i;
  805. if (node == NUMA_NO_NODE) {
  806. /* early_cpu_to_node() already emits a warning and trace */
  807. return;
  808. }
  809. for_each_online_node(i) {
  810. unsigned long addr;
  811. addr = node_start_pfn(i) << PAGE_SHIFT;
  812. if (addr < physnodes[node].start ||
  813. addr >= physnodes[node].end)
  814. continue;
  815. mask = debug_cpumask_set_cpu(cpu, enable);
  816. if (!mask)
  817. return;
  818. if (enable)
  819. cpumask_set_cpu(cpu, mask);
  820. else
  821. cpumask_clear_cpu(cpu, mask);
  822. }
  823. }
  824. void __cpuinit numa_add_cpu(int cpu)
  825. {
  826. numa_set_cpumask(cpu, 1);
  827. }
  828. void __cpuinit numa_remove_cpu(int cpu)
  829. {
  830. numa_set_cpumask(cpu, 0);
  831. }
  832. # endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
  833. #endif /* CONFIG_NUMA_EMU */