numa_64.c 27 KB

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