numa_64.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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, 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. /* Finally register nodes. */
  435. for_each_node_mask(nid, node_possible_map) {
  436. u64 start = (u64)max_pfn << PAGE_SHIFT;
  437. u64 end = 0;
  438. for (i = 0; i < mi->nr_blks; i++) {
  439. if (nid != mi->blk[i].nid)
  440. continue;
  441. start = min(mi->blk[i].start, start);
  442. end = max(mi->blk[i].end, end);
  443. }
  444. if (start < end)
  445. setup_node_bootmem(nid, start, end);
  446. }
  447. return 0;
  448. }
  449. #ifdef CONFIG_NUMA_EMU
  450. /* Numa emulation */
  451. static int emu_nid_to_phys[MAX_NUMNODES] __cpuinitdata;
  452. static char *emu_cmdline __initdata;
  453. void __init numa_emu_cmdline(char *str)
  454. {
  455. emu_cmdline = str;
  456. }
  457. static int __init emu_find_memblk_by_nid(int nid, const struct numa_meminfo *mi)
  458. {
  459. int i;
  460. for (i = 0; i < mi->nr_blks; i++)
  461. if (mi->blk[i].nid == nid)
  462. return i;
  463. return -ENOENT;
  464. }
  465. /*
  466. * Sets up nid to range from @start to @end. The return value is -errno if
  467. * something went wrong, 0 otherwise.
  468. */
  469. static int __init emu_setup_memblk(struct numa_meminfo *ei,
  470. struct numa_meminfo *pi,
  471. int nid, int phys_blk, u64 size)
  472. {
  473. struct numa_memblk *eb = &ei->blk[ei->nr_blks];
  474. struct numa_memblk *pb = &pi->blk[phys_blk];
  475. if (ei->nr_blks >= NR_NODE_MEMBLKS) {
  476. pr_err("NUMA: Too many emulated memblks, failing emulation\n");
  477. return -EINVAL;
  478. }
  479. ei->nr_blks++;
  480. eb->start = pb->start;
  481. eb->end = pb->start + size;
  482. eb->nid = nid;
  483. if (emu_nid_to_phys[nid] == NUMA_NO_NODE)
  484. emu_nid_to_phys[nid] = pb->nid;
  485. pb->start += size;
  486. if (pb->start >= pb->end) {
  487. WARN_ON_ONCE(pb->start > pb->end);
  488. numa_remove_memblk_from(phys_blk, pi);
  489. }
  490. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
  491. eb->start, eb->end, (eb->end - eb->start) >> 20);
  492. return 0;
  493. }
  494. /*
  495. * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
  496. * to max_addr. The return value is the number of nodes allocated.
  497. */
  498. static int __init split_nodes_interleave(struct numa_meminfo *ei,
  499. struct numa_meminfo *pi,
  500. u64 addr, u64 max_addr, int nr_nodes)
  501. {
  502. nodemask_t physnode_mask = NODE_MASK_NONE;
  503. u64 size;
  504. int big;
  505. int nid = 0;
  506. int i, ret;
  507. if (nr_nodes <= 0)
  508. return -1;
  509. if (nr_nodes > MAX_NUMNODES) {
  510. pr_info("numa=fake=%d too large, reducing to %d\n",
  511. nr_nodes, MAX_NUMNODES);
  512. nr_nodes = MAX_NUMNODES;
  513. }
  514. size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) / nr_nodes;
  515. /*
  516. * Calculate the number of big nodes that can be allocated as a result
  517. * of consolidating the remainder.
  518. */
  519. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * nr_nodes) /
  520. FAKE_NODE_MIN_SIZE;
  521. size &= FAKE_NODE_MIN_HASH_MASK;
  522. if (!size) {
  523. pr_err("Not enough memory for each node. "
  524. "NUMA emulation disabled.\n");
  525. return -1;
  526. }
  527. for (i = 0; i < pi->nr_blks; i++)
  528. node_set(pi->blk[i].nid, physnode_mask);
  529. /*
  530. * Continue to fill physical nodes with fake nodes until there is no
  531. * memory left on any of them.
  532. */
  533. while (nodes_weight(physnode_mask)) {
  534. for_each_node_mask(i, physnode_mask) {
  535. u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
  536. u64 start, limit, end;
  537. int phys_blk;
  538. phys_blk = emu_find_memblk_by_nid(i, pi);
  539. if (phys_blk < 0) {
  540. node_clear(i, physnode_mask);
  541. continue;
  542. }
  543. start = pi->blk[phys_blk].start;
  544. limit = pi->blk[phys_blk].end;
  545. end = start + size;
  546. if (nid < big)
  547. end += FAKE_NODE_MIN_SIZE;
  548. /*
  549. * Continue to add memory to this fake node if its
  550. * non-reserved memory is less than the per-node size.
  551. */
  552. while (end - start -
  553. memblock_x86_hole_size(start, end) < size) {
  554. end += FAKE_NODE_MIN_SIZE;
  555. if (end > limit) {
  556. end = limit;
  557. break;
  558. }
  559. }
  560. /*
  561. * If there won't be at least FAKE_NODE_MIN_SIZE of
  562. * non-reserved memory in ZONE_DMA32 for the next node,
  563. * this one must extend to the boundary.
  564. */
  565. if (end < dma32_end && dma32_end - end -
  566. memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  567. end = dma32_end;
  568. /*
  569. * If there won't be enough non-reserved memory for the
  570. * next node, this one must extend to the end of the
  571. * physical node.
  572. */
  573. if (limit - end -
  574. memblock_x86_hole_size(end, limit) < size)
  575. end = limit;
  576. ret = emu_setup_memblk(ei, pi, nid++ % nr_nodes,
  577. phys_blk,
  578. min(end, limit) - start);
  579. if (ret < 0)
  580. return ret;
  581. }
  582. }
  583. return 0;
  584. }
  585. /*
  586. * Returns the end address of a node so that there is at least `size' amount of
  587. * non-reserved memory or `max_addr' is reached.
  588. */
  589. static u64 __init find_end_of_node(u64 start, u64 max_addr, u64 size)
  590. {
  591. u64 end = start + size;
  592. while (end - start - memblock_x86_hole_size(start, end) < size) {
  593. end += FAKE_NODE_MIN_SIZE;
  594. if (end > max_addr) {
  595. end = max_addr;
  596. break;
  597. }
  598. }
  599. return end;
  600. }
  601. /*
  602. * Sets up fake nodes of `size' interleaved over physical nodes ranging from
  603. * `addr' to `max_addr'. The return value is the number of nodes allocated.
  604. */
  605. static int __init split_nodes_size_interleave(struct numa_meminfo *ei,
  606. struct numa_meminfo *pi,
  607. u64 addr, u64 max_addr, u64 size)
  608. {
  609. nodemask_t physnode_mask = NODE_MASK_NONE;
  610. u64 min_size;
  611. int nid = 0;
  612. int i, ret;
  613. if (!size)
  614. return -1;
  615. /*
  616. * The limit on emulated nodes is MAX_NUMNODES, so the size per node is
  617. * increased accordingly if the requested size is too small. This
  618. * creates a uniform distribution of node sizes across the entire
  619. * machine (but not necessarily over physical nodes).
  620. */
  621. min_size = (max_addr - addr - memblock_x86_hole_size(addr, max_addr)) /
  622. MAX_NUMNODES;
  623. min_size = max(min_size, FAKE_NODE_MIN_SIZE);
  624. if ((min_size & FAKE_NODE_MIN_HASH_MASK) < min_size)
  625. min_size = (min_size + FAKE_NODE_MIN_SIZE) &
  626. FAKE_NODE_MIN_HASH_MASK;
  627. if (size < min_size) {
  628. pr_err("Fake node size %LuMB too small, increasing to %LuMB\n",
  629. size >> 20, min_size >> 20);
  630. size = min_size;
  631. }
  632. size &= FAKE_NODE_MIN_HASH_MASK;
  633. for (i = 0; i < pi->nr_blks; i++)
  634. node_set(pi->blk[i].nid, physnode_mask);
  635. /*
  636. * Fill physical nodes with fake nodes of size until there is no memory
  637. * left on any of them.
  638. */
  639. while (nodes_weight(physnode_mask)) {
  640. for_each_node_mask(i, physnode_mask) {
  641. u64 dma32_end = MAX_DMA32_PFN << PAGE_SHIFT;
  642. u64 start, limit, end;
  643. int phys_blk;
  644. phys_blk = emu_find_memblk_by_nid(i, pi);
  645. if (phys_blk < 0) {
  646. node_clear(i, physnode_mask);
  647. continue;
  648. }
  649. start = pi->blk[phys_blk].start;
  650. limit = pi->blk[phys_blk].end;
  651. end = find_end_of_node(start, limit, size);
  652. /*
  653. * If there won't be at least FAKE_NODE_MIN_SIZE of
  654. * non-reserved memory in ZONE_DMA32 for the next node,
  655. * this one must extend to the boundary.
  656. */
  657. if (end < dma32_end && dma32_end - end -
  658. memblock_x86_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  659. end = dma32_end;
  660. /*
  661. * If there won't be enough non-reserved memory for the
  662. * next node, this one must extend to the end of the
  663. * physical node.
  664. */
  665. if (limit - end -
  666. memblock_x86_hole_size(end, limit) < size)
  667. end = limit;
  668. ret = emu_setup_memblk(ei, pi, nid++ % MAX_NUMNODES,
  669. phys_blk,
  670. min(end, limit) - start);
  671. if (ret < 0)
  672. return ret;
  673. }
  674. }
  675. return 0;
  676. }
  677. /*
  678. * Sets up the system RAM area from start_pfn to last_pfn according to the
  679. * numa=fake command-line option.
  680. */
  681. static bool __init numa_emulation(void)
  682. {
  683. static struct numa_meminfo ei __initdata;
  684. static struct numa_meminfo pi __initdata;
  685. const u64 max_addr = max_pfn << PAGE_SHIFT;
  686. int phys_dist_cnt = numa_distance_cnt;
  687. u8 *phys_dist = NULL;
  688. int i, j, ret;
  689. memset(&ei, 0, sizeof(ei));
  690. pi = numa_meminfo;
  691. for (i = 0; i < MAX_NUMNODES; i++)
  692. emu_nid_to_phys[i] = NUMA_NO_NODE;
  693. /*
  694. * If the numa=fake command-line contains a 'M' or 'G', it represents
  695. * the fixed node size. Otherwise, if it is just a single number N,
  696. * split the system RAM into N fake nodes.
  697. */
  698. if (strchr(emu_cmdline, 'M') || strchr(emu_cmdline, 'G')) {
  699. u64 size;
  700. size = memparse(emu_cmdline, &emu_cmdline);
  701. ret = split_nodes_size_interleave(&ei, &pi, 0, max_addr, size);
  702. } else {
  703. unsigned long n;
  704. n = simple_strtoul(emu_cmdline, NULL, 0);
  705. ret = split_nodes_interleave(&ei, &pi, 0, max_addr, n);
  706. }
  707. if (ret < 0)
  708. return false;
  709. if (numa_cleanup_meminfo(&ei) < 0) {
  710. pr_warning("NUMA: Warning: constructed meminfo invalid, disabling emulation\n");
  711. return false;
  712. }
  713. /*
  714. * Copy the original distance table. It's temporary so no need to
  715. * reserve it.
  716. */
  717. if (phys_dist_cnt) {
  718. size_t size = phys_dist_cnt * sizeof(numa_distance[0]);
  719. u64 phys;
  720. phys = memblock_find_in_range(0,
  721. (u64)max_pfn_mapped << PAGE_SHIFT,
  722. size, PAGE_SIZE);
  723. if (phys == MEMBLOCK_ERROR) {
  724. pr_warning("NUMA: Warning: can't allocate copy of distance table, disabling emulation\n");
  725. return false;
  726. }
  727. phys_dist = __va(phys);
  728. memcpy(phys_dist, numa_distance, size);
  729. }
  730. /* commit */
  731. numa_meminfo = ei;
  732. /*
  733. * Transform __apicid_to_node table to use emulated nids by
  734. * reverse-mapping phys_nid. The maps should always exist but fall
  735. * back to zero just in case.
  736. */
  737. for (i = 0; i < ARRAY_SIZE(__apicid_to_node); i++) {
  738. if (__apicid_to_node[i] == NUMA_NO_NODE)
  739. continue;
  740. for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
  741. if (__apicid_to_node[i] == emu_nid_to_phys[j])
  742. break;
  743. __apicid_to_node[i] = j < ARRAY_SIZE(emu_nid_to_phys) ? j : 0;
  744. }
  745. /* make sure all emulated nodes are mapped to a physical node */
  746. for (i = 0; i < ARRAY_SIZE(emu_nid_to_phys); i++)
  747. if (emu_nid_to_phys[i] == NUMA_NO_NODE)
  748. emu_nid_to_phys[i] = 0;
  749. /* transform distance table */
  750. numa_reset_distance();
  751. for (i = 0; i < MAX_NUMNODES; i++) {
  752. for (j = 0; j < MAX_NUMNODES; j++) {
  753. int physi = emu_nid_to_phys[i];
  754. int physj = emu_nid_to_phys[j];
  755. int dist;
  756. if (physi >= phys_dist_cnt || physj >= phys_dist_cnt)
  757. dist = physi == physj ?
  758. LOCAL_DISTANCE : REMOTE_DISTANCE;
  759. else
  760. dist = phys_dist[physi * phys_dist_cnt + physj];
  761. numa_set_distance(i, j, dist);
  762. }
  763. }
  764. return true;
  765. }
  766. #endif /* CONFIG_NUMA_EMU */
  767. static int __init dummy_numa_init(void)
  768. {
  769. printk(KERN_INFO "%s\n",
  770. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  771. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  772. 0LU, max_pfn << PAGE_SHIFT);
  773. node_set(0, numa_nodes_parsed);
  774. numa_add_memblk(0, 0, (u64)max_pfn << PAGE_SHIFT);
  775. return 0;
  776. }
  777. void __init initmem_init(void)
  778. {
  779. int (*numa_init[])(void) = { [2] = dummy_numa_init };
  780. int i, j;
  781. if (!numa_off) {
  782. #ifdef CONFIG_ACPI_NUMA
  783. numa_init[0] = x86_acpi_numa_init;
  784. #endif
  785. #ifdef CONFIG_AMD_NUMA
  786. numa_init[1] = amd_numa_init;
  787. #endif
  788. }
  789. for (i = 0; i < ARRAY_SIZE(numa_init); i++) {
  790. if (!numa_init[i])
  791. continue;
  792. for (j = 0; j < MAX_LOCAL_APIC; j++)
  793. set_apicid_to_node(j, NUMA_NO_NODE);
  794. nodes_clear(numa_nodes_parsed);
  795. nodes_clear(node_possible_map);
  796. nodes_clear(node_online_map);
  797. memset(&numa_meminfo, 0, sizeof(numa_meminfo));
  798. remove_all_active_ranges();
  799. numa_reset_distance();
  800. if (numa_init[i]() < 0)
  801. continue;
  802. if (numa_cleanup_meminfo(&numa_meminfo) < 0)
  803. continue;
  804. #ifdef CONFIG_NUMA_EMU
  805. /*
  806. * If requested, try emulation. If emulation is not used,
  807. * build identity emu_nid_to_phys[] for numa_add_cpu()
  808. */
  809. if (!emu_cmdline || !numa_emulation())
  810. for (j = 0; j < ARRAY_SIZE(emu_nid_to_phys); j++)
  811. emu_nid_to_phys[j] = j;
  812. #endif
  813. if (numa_register_memblks(&numa_meminfo) < 0)
  814. continue;
  815. for (j = 0; j < nr_cpu_ids; j++) {
  816. int nid = early_cpu_to_node(j);
  817. if (nid == NUMA_NO_NODE)
  818. continue;
  819. if (!node_online(nid))
  820. numa_clear_node(j);
  821. }
  822. numa_init_array();
  823. return;
  824. }
  825. BUG();
  826. }
  827. unsigned long __init numa_free_all_bootmem(void)
  828. {
  829. unsigned long pages = 0;
  830. int i;
  831. for_each_online_node(i)
  832. pages += free_all_bootmem_node(NODE_DATA(i));
  833. pages += free_all_memory_core_early(MAX_NUMNODES);
  834. return pages;
  835. }
  836. int __cpuinit numa_cpu_node(int cpu)
  837. {
  838. int apicid = early_per_cpu(x86_cpu_to_apicid, cpu);
  839. if (apicid != BAD_APICID)
  840. return __apicid_to_node[apicid];
  841. return NUMA_NO_NODE;
  842. }
  843. /*
  844. * UGLINESS AHEAD: Currently, CONFIG_NUMA_EMU is 64bit only and makes use
  845. * of 64bit specific data structures. The distinction is artificial and
  846. * should be removed. numa_{add|remove}_cpu() are implemented in numa.c
  847. * for both 32 and 64bit when CONFIG_NUMA_EMU is disabled but here when
  848. * enabled.
  849. *
  850. * NUMA emulation is planned to be made generic and the following and other
  851. * related code should be moved to numa.c.
  852. */
  853. #ifdef CONFIG_NUMA_EMU
  854. # ifndef CONFIG_DEBUG_PER_CPU_MAPS
  855. void __cpuinit numa_add_cpu(int cpu)
  856. {
  857. int physnid, nid;
  858. nid = numa_cpu_node(cpu);
  859. if (nid == NUMA_NO_NODE)
  860. nid = early_cpu_to_node(cpu);
  861. BUG_ON(nid == NUMA_NO_NODE || !node_online(nid));
  862. physnid = emu_nid_to_phys[nid];
  863. /*
  864. * Map the cpu to each emulated node that is allocated on the physical
  865. * node of the cpu's apic id.
  866. */
  867. for_each_online_node(nid)
  868. if (emu_nid_to_phys[nid] == physnid)
  869. cpumask_set_cpu(cpu, node_to_cpumask_map[nid]);
  870. }
  871. void __cpuinit numa_remove_cpu(int cpu)
  872. {
  873. int i;
  874. for_each_online_node(i)
  875. cpumask_clear_cpu(cpu, node_to_cpumask_map[i]);
  876. }
  877. # else /* !CONFIG_DEBUG_PER_CPU_MAPS */
  878. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  879. {
  880. struct cpumask *mask;
  881. int nid, physnid, i;
  882. nid = early_cpu_to_node(cpu);
  883. if (nid == NUMA_NO_NODE) {
  884. /* early_cpu_to_node() already emits a warning and trace */
  885. return;
  886. }
  887. physnid = emu_nid_to_phys[nid];
  888. for_each_online_node(i) {
  889. if (emu_nid_to_phys[nid] != physnid)
  890. continue;
  891. mask = debug_cpumask_set_cpu(cpu, enable);
  892. if (!mask)
  893. return;
  894. if (enable)
  895. cpumask_set_cpu(cpu, mask);
  896. else
  897. cpumask_clear_cpu(cpu, mask);
  898. }
  899. }
  900. void __cpuinit numa_add_cpu(int cpu)
  901. {
  902. numa_set_cpumask(cpu, 1);
  903. }
  904. void __cpuinit numa_remove_cpu(int cpu)
  905. {
  906. numa_set_cpumask(cpu, 0);
  907. }
  908. # endif /* !CONFIG_DEBUG_PER_CPU_MAPS */
  909. #endif /* CONFIG_NUMA_EMU */