numa_64.c 25 KB

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