numa_64.c 28 KB

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