numa_64.c 28 KB

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