numa_64.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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/mmzone.h>
  11. #include <linux/ctype.h>
  12. #include <linux/module.h>
  13. #include <linux/nodemask.h>
  14. #include <linux/sched.h>
  15. #include <asm/e820.h>
  16. #include <asm/proto.h>
  17. #include <asm/dma.h>
  18. #include <asm/numa.h>
  19. #include <asm/acpi.h>
  20. #include <asm/k8.h>
  21. struct pglist_data *node_data[MAX_NUMNODES] __read_mostly;
  22. EXPORT_SYMBOL(node_data);
  23. struct memnode memnode;
  24. s16 apicid_to_node[MAX_LOCAL_APIC] __cpuinitdata = {
  25. [0 ... MAX_LOCAL_APIC-1] = NUMA_NO_NODE
  26. };
  27. int numa_off __initdata;
  28. static unsigned long __initdata nodemap_addr;
  29. static unsigned long __initdata nodemap_size;
  30. DEFINE_PER_CPU(int, node_number) = 0;
  31. EXPORT_PER_CPU_SYMBOL(node_number);
  32. /*
  33. * Map cpu index to node index
  34. */
  35. DEFINE_EARLY_PER_CPU(int, x86_cpu_to_node_map, NUMA_NO_NODE);
  36. EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_node_map);
  37. /*
  38. * Given a shift value, try to populate memnodemap[]
  39. * Returns :
  40. * 1 if OK
  41. * 0 if memnodmap[] too small (of shift too small)
  42. * -1 if node overlap or lost ram (shift too big)
  43. */
  44. static int __init populate_memnodemap(const struct bootnode *nodes,
  45. int numnodes, int shift, int *nodeids)
  46. {
  47. unsigned long addr, end;
  48. int i, res = -1;
  49. memset(memnodemap, 0xff, sizeof(s16)*memnodemapsize);
  50. for (i = 0; i < numnodes; i++) {
  51. addr = nodes[i].start;
  52. end = nodes[i].end;
  53. if (addr >= end)
  54. continue;
  55. if ((end >> shift) >= memnodemapsize)
  56. return 0;
  57. do {
  58. if (memnodemap[addr >> shift] != NUMA_NO_NODE)
  59. return -1;
  60. if (!nodeids)
  61. memnodemap[addr >> shift] = i;
  62. else
  63. memnodemap[addr >> shift] = nodeids[i];
  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 = find_e820_area(addr, max_pfn<<PAGE_SHIFT,
  79. nodemap_size, L1_CACHE_BYTES);
  80. if (nodemap_addr == -1UL) {
  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. reserve_early(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 bootnode *nodes,
  97. int numnodes)
  98. {
  99. int i, nodes_used = 0;
  100. unsigned long start, end;
  101. unsigned long bitfield = 0, memtop = 0;
  102. for (i = 0; i < numnodes; i++) {
  103. start = nodes[i].start;
  104. end = nodes[i].end;
  105. if (start >= end)
  106. continue;
  107. bitfield |= start;
  108. nodes_used++;
  109. if (end > memtop)
  110. memtop = end;
  111. }
  112. if (nodes_used <= 1)
  113. i = 63;
  114. else
  115. i = find_first_bit(&bitfield, sizeof(unsigned long)*8);
  116. memnodemapsize = (memtop >> i)+1;
  117. return i;
  118. }
  119. int __init compute_hash_shift(struct bootnode *nodes, int numnodes,
  120. int *nodeids)
  121. {
  122. int shift;
  123. shift = extract_lsb_from_nodes(nodes, numnodes);
  124. if (allocate_cachealigned_memnodemap())
  125. return -1;
  126. printk(KERN_DEBUG "NUMA: Using %d for the hash shift.\n",
  127. shift);
  128. if (populate_memnodemap(nodes, numnodes, shift, nodeids) != 1) {
  129. printk(KERN_INFO "Your memory is not aligned you need to "
  130. "rebuild your kernel with a bigger NODEMAPSIZE "
  131. "shift=%d\n", shift);
  132. return -1;
  133. }
  134. return shift;
  135. }
  136. int __meminit __early_pfn_to_nid(unsigned long pfn)
  137. {
  138. return phys_to_nid(pfn << PAGE_SHIFT);
  139. }
  140. static void * __init early_node_mem(int nodeid, unsigned long start,
  141. unsigned long end, unsigned long size,
  142. unsigned long align)
  143. {
  144. unsigned long mem;
  145. /*
  146. * put it on high as possible
  147. * something will go with NODE_DATA
  148. */
  149. if (start < (MAX_DMA_PFN<<PAGE_SHIFT))
  150. start = MAX_DMA_PFN<<PAGE_SHIFT;
  151. if (start < (MAX_DMA32_PFN<<PAGE_SHIFT) &&
  152. end > (MAX_DMA32_PFN<<PAGE_SHIFT))
  153. start = MAX_DMA32_PFN<<PAGE_SHIFT;
  154. mem = find_e820_area(start, end, size, align);
  155. if (mem != -1L)
  156. return __va(mem);
  157. /* extend the search scope */
  158. end = max_pfn_mapped << PAGE_SHIFT;
  159. if (end > (MAX_DMA32_PFN<<PAGE_SHIFT))
  160. start = MAX_DMA32_PFN<<PAGE_SHIFT;
  161. else
  162. start = MAX_DMA_PFN<<PAGE_SHIFT;
  163. mem = find_e820_area(start, end, size, align);
  164. if (mem != -1L)
  165. return __va(mem);
  166. printk(KERN_ERR "Cannot find %lu bytes in node %d\n",
  167. size, nodeid);
  168. return NULL;
  169. }
  170. /* Initialize bootmem allocator for a node */
  171. void __init
  172. setup_node_bootmem(int nodeid, unsigned long start, unsigned long end)
  173. {
  174. unsigned long start_pfn, last_pfn, bootmap_pages, bootmap_size;
  175. const int pgdat_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
  176. unsigned long bootmap_start, nodedata_phys;
  177. void *bootmap;
  178. int nid;
  179. if (!end)
  180. return;
  181. /*
  182. * Don't confuse VM with a node that doesn't have the
  183. * minimum amount of memory:
  184. */
  185. if (end && (end - start) < NODE_MIN_SIZE)
  186. return;
  187. start = roundup(start, ZONE_ALIGN);
  188. printk(KERN_INFO "Bootmem setup node %d %016lx-%016lx\n", nodeid,
  189. start, end);
  190. start_pfn = start >> PAGE_SHIFT;
  191. last_pfn = end >> PAGE_SHIFT;
  192. node_data[nodeid] = early_node_mem(nodeid, start, end, pgdat_size,
  193. SMP_CACHE_BYTES);
  194. if (node_data[nodeid] == NULL)
  195. return;
  196. nodedata_phys = __pa(node_data[nodeid]);
  197. reserve_early(nodedata_phys, nodedata_phys + pgdat_size, "NODE_DATA");
  198. printk(KERN_INFO " NODE_DATA [%016lx - %016lx]\n", nodedata_phys,
  199. nodedata_phys + pgdat_size - 1);
  200. nid = phys_to_nid(nodedata_phys);
  201. if (nid != nodeid)
  202. printk(KERN_INFO " NODE_DATA(%d) on node %d\n", nodeid, nid);
  203. memset(NODE_DATA(nodeid), 0, sizeof(pg_data_t));
  204. NODE_DATA(nodeid)->bdata = &bootmem_node_data[nodeid];
  205. NODE_DATA(nodeid)->node_start_pfn = start_pfn;
  206. NODE_DATA(nodeid)->node_spanned_pages = last_pfn - start_pfn;
  207. /*
  208. * Find a place for the bootmem map
  209. * nodedata_phys could be on other nodes by alloc_bootmem,
  210. * so need to sure bootmap_start not to be small, otherwise
  211. * early_node_mem will get that with find_e820_area instead
  212. * of alloc_bootmem, that could clash with reserved range
  213. */
  214. bootmap_pages = bootmem_bootmap_pages(last_pfn - start_pfn);
  215. bootmap_start = roundup(nodedata_phys + pgdat_size, PAGE_SIZE);
  216. /*
  217. * SMP_CACHE_BYTES could be enough, but init_bootmem_node like
  218. * to use that to align to PAGE_SIZE
  219. */
  220. bootmap = early_node_mem(nodeid, bootmap_start, end,
  221. bootmap_pages<<PAGE_SHIFT, PAGE_SIZE);
  222. if (bootmap == NULL) {
  223. free_early(nodedata_phys, nodedata_phys + pgdat_size);
  224. node_data[nodeid] = NULL;
  225. return;
  226. }
  227. bootmap_start = __pa(bootmap);
  228. reserve_early(bootmap_start, bootmap_start+(bootmap_pages<<PAGE_SHIFT),
  229. "BOOTMAP");
  230. bootmap_size = init_bootmem_node(NODE_DATA(nodeid),
  231. bootmap_start >> PAGE_SHIFT,
  232. start_pfn, last_pfn);
  233. printk(KERN_INFO " bootmap [%016lx - %016lx] pages %lx\n",
  234. bootmap_start, bootmap_start + bootmap_size - 1,
  235. bootmap_pages);
  236. nid = phys_to_nid(bootmap_start);
  237. if (nid != nodeid)
  238. printk(KERN_INFO " bootmap(%d) on node %d\n", nodeid, nid);
  239. free_bootmem_with_active_regions(nodeid, end);
  240. node_set_online(nodeid);
  241. }
  242. /*
  243. * There are unfortunately some poorly designed mainboards around that
  244. * only connect memory to a single CPU. This breaks the 1:1 cpu->node
  245. * mapping. To avoid this fill in the mapping for all possible CPUs,
  246. * as the number of CPUs is not known yet. We round robin the existing
  247. * nodes.
  248. */
  249. void __init numa_init_array(void)
  250. {
  251. int rr, i;
  252. rr = first_node(node_online_map);
  253. for (i = 0; i < nr_cpu_ids; i++) {
  254. if (early_cpu_to_node(i) != NUMA_NO_NODE)
  255. continue;
  256. numa_set_node(i, rr);
  257. rr = next_node(rr, node_online_map);
  258. if (rr == MAX_NUMNODES)
  259. rr = first_node(node_online_map);
  260. }
  261. }
  262. #ifdef CONFIG_NUMA_EMU
  263. /* Numa emulation */
  264. static struct bootnode nodes[MAX_NUMNODES] __initdata;
  265. static struct bootnode physnodes[MAX_NUMNODES] __initdata;
  266. static char *cmdline __initdata;
  267. static int __init setup_physnodes(unsigned long start, unsigned long end,
  268. int acpi, int k8)
  269. {
  270. int nr_nodes = 0;
  271. int ret = 0;
  272. int i;
  273. #ifdef CONFIG_ACPI_NUMA
  274. if (acpi)
  275. nr_nodes = acpi_get_nodes(physnodes);
  276. #endif
  277. #ifdef CONFIG_K8_NUMA
  278. if (k8)
  279. nr_nodes = k8_get_nodes(physnodes);
  280. #endif
  281. /*
  282. * Basic sanity checking on the physical node map: there may be errors
  283. * if the SRAT or K8 incorrectly reported the topology or the mem=
  284. * kernel parameter is used.
  285. */
  286. for (i = 0; i < nr_nodes; i++) {
  287. if (physnodes[i].start == physnodes[i].end)
  288. continue;
  289. if (physnodes[i].start > end) {
  290. physnodes[i].end = physnodes[i].start;
  291. continue;
  292. }
  293. if (physnodes[i].end < start) {
  294. physnodes[i].start = physnodes[i].end;
  295. continue;
  296. }
  297. if (physnodes[i].start < start)
  298. physnodes[i].start = start;
  299. if (physnodes[i].end > end)
  300. physnodes[i].end = end;
  301. }
  302. /*
  303. * Remove all nodes that have no memory or were truncated because of the
  304. * limited address range.
  305. */
  306. for (i = 0; i < nr_nodes; i++) {
  307. if (physnodes[i].start == physnodes[i].end)
  308. continue;
  309. physnodes[ret].start = physnodes[i].start;
  310. physnodes[ret].end = physnodes[i].end;
  311. ret++;
  312. }
  313. /*
  314. * If no physical topology was detected, a single node is faked to cover
  315. * the entire address space.
  316. */
  317. if (!ret) {
  318. physnodes[ret].start = start;
  319. physnodes[ret].end = end;
  320. ret = 1;
  321. }
  322. return ret;
  323. }
  324. /*
  325. * Setups up nid to range from addr to addr + size. If the end
  326. * boundary is greater than max_addr, then max_addr is used instead.
  327. * The return value is 0 if there is additional memory left for
  328. * allocation past addr and -1 otherwise. addr is adjusted to be at
  329. * the end of the node.
  330. */
  331. static int __init setup_node_range(int nid, u64 *addr, u64 size, u64 max_addr)
  332. {
  333. int ret = 0;
  334. nodes[nid].start = *addr;
  335. *addr += size;
  336. if (*addr >= max_addr) {
  337. *addr = max_addr;
  338. ret = -1;
  339. }
  340. nodes[nid].end = *addr;
  341. node_set(nid, node_possible_map);
  342. printk(KERN_INFO "Faking node %d at %016Lx-%016Lx (%LuMB)\n", nid,
  343. nodes[nid].start, nodes[nid].end,
  344. (nodes[nid].end - nodes[nid].start) >> 20);
  345. return ret;
  346. }
  347. /*
  348. * Sets up nr_nodes fake nodes interleaved over physical nodes ranging from addr
  349. * to max_addr. The return value is the number of nodes allocated.
  350. */
  351. static int __init split_nodes_interleave(u64 addr, u64 max_addr,
  352. int nr_phys_nodes, int nr_nodes)
  353. {
  354. nodemask_t physnode_mask = NODE_MASK_NONE;
  355. u64 size;
  356. int big;
  357. int ret = 0;
  358. int i;
  359. if (nr_nodes <= 0)
  360. return -1;
  361. if (nr_nodes > MAX_NUMNODES) {
  362. pr_info("numa=fake=%d too large, reducing to %d\n",
  363. nr_nodes, MAX_NUMNODES);
  364. nr_nodes = MAX_NUMNODES;
  365. }
  366. size = (max_addr - addr - e820_hole_size(addr, max_addr)) / nr_nodes;
  367. /*
  368. * Calculate the number of big nodes that can be allocated as a result
  369. * of consolidating the remainder.
  370. */
  371. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) & nr_nodes) /
  372. FAKE_NODE_MIN_SIZE;
  373. size &= FAKE_NODE_MIN_HASH_MASK;
  374. if (!size) {
  375. pr_err("Not enough memory for each node. "
  376. "NUMA emulation disabled.\n");
  377. return -1;
  378. }
  379. for (i = 0; i < nr_phys_nodes; i++)
  380. if (physnodes[i].start != physnodes[i].end)
  381. node_set(i, physnode_mask);
  382. /*
  383. * Continue to fill physical nodes with fake nodes until there is no
  384. * memory left on any of them.
  385. */
  386. while (nodes_weight(physnode_mask)) {
  387. for_each_node_mask(i, physnode_mask) {
  388. u64 end = physnodes[i].start + size;
  389. u64 dma32_end = PFN_PHYS(MAX_DMA32_PFN);
  390. if (ret < big)
  391. end += FAKE_NODE_MIN_SIZE;
  392. /*
  393. * Continue to add memory to this fake node if its
  394. * non-reserved memory is less than the per-node size.
  395. */
  396. while (end - physnodes[i].start -
  397. e820_hole_size(physnodes[i].start, end) < size) {
  398. end += FAKE_NODE_MIN_SIZE;
  399. if (end > physnodes[i].end) {
  400. end = physnodes[i].end;
  401. break;
  402. }
  403. }
  404. /*
  405. * If there won't be at least FAKE_NODE_MIN_SIZE of
  406. * non-reserved memory in ZONE_DMA32 for the next node,
  407. * this one must extend to the boundary.
  408. */
  409. if (end < dma32_end && dma32_end - end -
  410. e820_hole_size(end, dma32_end) < FAKE_NODE_MIN_SIZE)
  411. end = dma32_end;
  412. /*
  413. * If there won't be enough non-reserved memory for the
  414. * next node, this one must extend to the end of the
  415. * physical node.
  416. */
  417. if (physnodes[i].end - end -
  418. e820_hole_size(end, physnodes[i].end) < size)
  419. end = physnodes[i].end;
  420. /*
  421. * Avoid allocating more nodes than requested, which can
  422. * happen as a result of rounding down each node's size
  423. * to FAKE_NODE_MIN_SIZE.
  424. */
  425. if (nodes_weight(physnode_mask) + ret >= nr_nodes)
  426. end = physnodes[i].end;
  427. if (setup_node_range(ret++, &physnodes[i].start,
  428. end - physnodes[i].start,
  429. physnodes[i].end) < 0)
  430. node_clear(i, physnode_mask);
  431. }
  432. }
  433. return ret;
  434. }
  435. /*
  436. * Splits num_nodes nodes up equally starting at node_start. The return value
  437. * is the number of nodes split up and addr is adjusted to be at the end of the
  438. * last node allocated.
  439. */
  440. static int __init split_nodes_equally(u64 *addr, u64 max_addr, int node_start,
  441. int num_nodes)
  442. {
  443. unsigned int big;
  444. u64 size;
  445. int i;
  446. if (num_nodes <= 0)
  447. return -1;
  448. if (num_nodes > MAX_NUMNODES)
  449. num_nodes = MAX_NUMNODES;
  450. size = (max_addr - *addr - e820_hole_size(*addr, max_addr)) /
  451. num_nodes;
  452. /*
  453. * Calculate the number of big nodes that can be allocated as a result
  454. * of consolidating the leftovers.
  455. */
  456. big = ((size & ~FAKE_NODE_MIN_HASH_MASK) * num_nodes) /
  457. FAKE_NODE_MIN_SIZE;
  458. /* Round down to nearest FAKE_NODE_MIN_SIZE. */
  459. size &= FAKE_NODE_MIN_HASH_MASK;
  460. if (!size) {
  461. printk(KERN_ERR "Not enough memory for each node. "
  462. "NUMA emulation disabled.\n");
  463. return -1;
  464. }
  465. for (i = node_start; i < num_nodes + node_start; i++) {
  466. u64 end = *addr + size;
  467. if (i < big)
  468. end += FAKE_NODE_MIN_SIZE;
  469. /*
  470. * The final node can have the remaining system RAM. Other
  471. * nodes receive roughly the same amount of available pages.
  472. */
  473. if (i == num_nodes + node_start - 1)
  474. end = max_addr;
  475. else
  476. while (end - *addr - e820_hole_size(*addr, end) <
  477. size) {
  478. end += FAKE_NODE_MIN_SIZE;
  479. if (end > max_addr) {
  480. end = max_addr;
  481. break;
  482. }
  483. }
  484. if (setup_node_range(i, addr, end - *addr, max_addr) < 0)
  485. break;
  486. }
  487. return i - node_start + 1;
  488. }
  489. /*
  490. * Splits the remaining system RAM into chunks of size. The remaining memory is
  491. * always assigned to a final node and can be asymmetric. Returns the number of
  492. * nodes split.
  493. */
  494. static int __init split_nodes_by_size(u64 *addr, u64 max_addr, int node_start,
  495. u64 size)
  496. {
  497. int i = node_start;
  498. size = (size << 20) & FAKE_NODE_MIN_HASH_MASK;
  499. while (!setup_node_range(i++, addr, size, max_addr))
  500. ;
  501. return i - node_start;
  502. }
  503. /*
  504. * Sets up the system RAM area from start_pfn to last_pfn according to the
  505. * numa=fake command-line option.
  506. */
  507. static int __init numa_emulation(unsigned long start_pfn,
  508. unsigned long last_pfn, int acpi, int k8)
  509. {
  510. u64 size, addr = start_pfn << PAGE_SHIFT;
  511. u64 max_addr = last_pfn << PAGE_SHIFT;
  512. int num_nodes = 0, num = 0, coeff_flag, coeff = -1, i;
  513. int num_phys_nodes;
  514. num_phys_nodes = setup_physnodes(addr, max_addr, acpi, k8);
  515. /*
  516. * If the numa=fake command-line is just a single number N, split the
  517. * system RAM into N fake nodes.
  518. */
  519. if (!strchr(cmdline, '*') && !strchr(cmdline, ',')) {
  520. long n = simple_strtol(cmdline, NULL, 0);
  521. num_nodes = split_nodes_interleave(addr, max_addr,
  522. num_phys_nodes, n);
  523. if (num_nodes < 0)
  524. return num_nodes;
  525. goto out;
  526. }
  527. /* Parse the command line. */
  528. for (coeff_flag = 0; ; cmdline++) {
  529. if (*cmdline && isdigit(*cmdline)) {
  530. num = num * 10 + *cmdline - '0';
  531. continue;
  532. }
  533. if (*cmdline == '*') {
  534. if (num > 0)
  535. coeff = num;
  536. coeff_flag = 1;
  537. }
  538. if (!*cmdline || *cmdline == ',') {
  539. if (!coeff_flag)
  540. coeff = 1;
  541. /*
  542. * Round down to the nearest FAKE_NODE_MIN_SIZE.
  543. * Command-line coefficients are in megabytes.
  544. */
  545. size = ((u64)num << 20) & FAKE_NODE_MIN_HASH_MASK;
  546. if (size)
  547. for (i = 0; i < coeff; i++, num_nodes++)
  548. if (setup_node_range(num_nodes, &addr,
  549. size, max_addr) < 0)
  550. goto done;
  551. if (!*cmdline)
  552. break;
  553. coeff_flag = 0;
  554. coeff = -1;
  555. }
  556. num = 0;
  557. }
  558. done:
  559. if (!num_nodes)
  560. return -1;
  561. /* Fill remainder of system RAM, if appropriate. */
  562. if (addr < max_addr) {
  563. if (coeff_flag && coeff < 0) {
  564. /* Split remaining nodes into num-sized chunks */
  565. num_nodes += split_nodes_by_size(&addr, max_addr,
  566. num_nodes, num);
  567. goto out;
  568. }
  569. switch (*(cmdline - 1)) {
  570. case '*':
  571. /* Split remaining nodes into coeff chunks */
  572. if (coeff <= 0)
  573. break;
  574. num_nodes += split_nodes_equally(&addr, max_addr,
  575. num_nodes, coeff);
  576. break;
  577. case ',':
  578. /* Do not allocate remaining system RAM */
  579. break;
  580. default:
  581. /* Give one final node */
  582. setup_node_range(num_nodes, &addr, max_addr - addr,
  583. max_addr);
  584. num_nodes++;
  585. }
  586. }
  587. out:
  588. memnode_shift = compute_hash_shift(nodes, num_nodes, NULL);
  589. if (memnode_shift < 0) {
  590. memnode_shift = 0;
  591. printk(KERN_ERR "No NUMA hash function found. NUMA emulation "
  592. "disabled.\n");
  593. return -1;
  594. }
  595. /*
  596. * We need to vacate all active ranges that may have been registered for
  597. * the e820 memory map.
  598. */
  599. remove_all_active_ranges();
  600. for_each_node_mask(i, node_possible_map) {
  601. e820_register_active_regions(i, nodes[i].start >> PAGE_SHIFT,
  602. nodes[i].end >> PAGE_SHIFT);
  603. setup_node_bootmem(i, nodes[i].start, nodes[i].end);
  604. }
  605. acpi_fake_nodes(nodes, num_nodes);
  606. numa_init_array();
  607. return 0;
  608. }
  609. #endif /* CONFIG_NUMA_EMU */
  610. void __init initmem_init(unsigned long start_pfn, unsigned long last_pfn,
  611. int acpi, int k8)
  612. {
  613. int i;
  614. nodes_clear(node_possible_map);
  615. nodes_clear(node_online_map);
  616. #ifdef CONFIG_NUMA_EMU
  617. if (cmdline && !numa_emulation(start_pfn, last_pfn, acpi, k8))
  618. return;
  619. nodes_clear(node_possible_map);
  620. nodes_clear(node_online_map);
  621. #endif
  622. #ifdef CONFIG_ACPI_NUMA
  623. if (!numa_off && acpi && !acpi_scan_nodes(start_pfn << PAGE_SHIFT,
  624. last_pfn << PAGE_SHIFT))
  625. return;
  626. nodes_clear(node_possible_map);
  627. nodes_clear(node_online_map);
  628. #endif
  629. #ifdef CONFIG_K8_NUMA
  630. if (!numa_off && k8 && !k8_scan_nodes())
  631. return;
  632. nodes_clear(node_possible_map);
  633. nodes_clear(node_online_map);
  634. #endif
  635. printk(KERN_INFO "%s\n",
  636. numa_off ? "NUMA turned off" : "No NUMA configuration found");
  637. printk(KERN_INFO "Faking a node at %016lx-%016lx\n",
  638. start_pfn << PAGE_SHIFT,
  639. last_pfn << PAGE_SHIFT);
  640. /* setup dummy node covering all memory */
  641. memnode_shift = 63;
  642. memnodemap = memnode.embedded_map;
  643. memnodemap[0] = 0;
  644. node_set_online(0);
  645. node_set(0, node_possible_map);
  646. for (i = 0; i < nr_cpu_ids; i++)
  647. numa_set_node(i, 0);
  648. e820_register_active_regions(0, start_pfn, last_pfn);
  649. setup_node_bootmem(0, start_pfn << PAGE_SHIFT, last_pfn << PAGE_SHIFT);
  650. }
  651. unsigned long __init numa_free_all_bootmem(void)
  652. {
  653. unsigned long pages = 0;
  654. int i;
  655. for_each_online_node(i)
  656. pages += free_all_bootmem_node(NODE_DATA(i));
  657. return pages;
  658. }
  659. static __init int numa_setup(char *opt)
  660. {
  661. if (!opt)
  662. return -EINVAL;
  663. if (!strncmp(opt, "off", 3))
  664. numa_off = 1;
  665. #ifdef CONFIG_NUMA_EMU
  666. if (!strncmp(opt, "fake=", 5))
  667. cmdline = opt + 5;
  668. #endif
  669. #ifdef CONFIG_ACPI_NUMA
  670. if (!strncmp(opt, "noacpi", 6))
  671. acpi_numa = -1;
  672. #endif
  673. return 0;
  674. }
  675. early_param("numa", numa_setup);
  676. #ifdef CONFIG_NUMA
  677. static __init int find_near_online_node(int node)
  678. {
  679. int n, val;
  680. int min_val = INT_MAX;
  681. int best_node = -1;
  682. for_each_online_node(n) {
  683. val = node_distance(node, n);
  684. if (val < min_val) {
  685. min_val = val;
  686. best_node = n;
  687. }
  688. }
  689. return best_node;
  690. }
  691. /*
  692. * Setup early cpu_to_node.
  693. *
  694. * Populate cpu_to_node[] only if x86_cpu_to_apicid[],
  695. * and apicid_to_node[] tables have valid entries for a CPU.
  696. * This means we skip cpu_to_node[] initialisation for NUMA
  697. * emulation and faking node case (when running a kernel compiled
  698. * for NUMA on a non NUMA box), which is OK as cpu_to_node[]
  699. * is already initialized in a round robin manner at numa_init_array,
  700. * prior to this call, and this initialization is good enough
  701. * for the fake NUMA cases.
  702. *
  703. * Called before the per_cpu areas are setup.
  704. */
  705. void __init init_cpu_to_node(void)
  706. {
  707. int cpu;
  708. u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
  709. BUG_ON(cpu_to_apicid == NULL);
  710. for_each_possible_cpu(cpu) {
  711. int node;
  712. u16 apicid = cpu_to_apicid[cpu];
  713. if (apicid == BAD_APICID)
  714. continue;
  715. node = apicid_to_node[apicid];
  716. if (node == NUMA_NO_NODE)
  717. continue;
  718. if (!node_online(node))
  719. node = find_near_online_node(node);
  720. numa_set_node(cpu, node);
  721. }
  722. }
  723. #endif
  724. void __cpuinit numa_set_node(int cpu, int node)
  725. {
  726. int *cpu_to_node_map = early_per_cpu_ptr(x86_cpu_to_node_map);
  727. /* early setting, no percpu area yet */
  728. if (cpu_to_node_map) {
  729. cpu_to_node_map[cpu] = node;
  730. return;
  731. }
  732. #ifdef CONFIG_DEBUG_PER_CPU_MAPS
  733. if (cpu >= nr_cpu_ids || !cpu_possible(cpu)) {
  734. printk(KERN_ERR "numa_set_node: invalid cpu# (%d)\n", cpu);
  735. dump_stack();
  736. return;
  737. }
  738. #endif
  739. per_cpu(x86_cpu_to_node_map, cpu) = node;
  740. if (node != NUMA_NO_NODE)
  741. per_cpu(node_number, cpu) = node;
  742. }
  743. void __cpuinit numa_clear_node(int cpu)
  744. {
  745. numa_set_node(cpu, NUMA_NO_NODE);
  746. }
  747. #ifndef CONFIG_DEBUG_PER_CPU_MAPS
  748. void __cpuinit numa_add_cpu(int cpu)
  749. {
  750. cpumask_set_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  751. }
  752. void __cpuinit numa_remove_cpu(int cpu)
  753. {
  754. cpumask_clear_cpu(cpu, node_to_cpumask_map[early_cpu_to_node(cpu)]);
  755. }
  756. #else /* CONFIG_DEBUG_PER_CPU_MAPS */
  757. /*
  758. * --------- debug versions of the numa functions ---------
  759. */
  760. static void __cpuinit numa_set_cpumask(int cpu, int enable)
  761. {
  762. int node = early_cpu_to_node(cpu);
  763. struct cpumask *mask;
  764. char buf[64];
  765. mask = node_to_cpumask_map[node];
  766. if (mask == NULL) {
  767. printk(KERN_ERR "node_to_cpumask_map[%i] NULL\n", node);
  768. dump_stack();
  769. return;
  770. }
  771. if (enable)
  772. cpumask_set_cpu(cpu, mask);
  773. else
  774. cpumask_clear_cpu(cpu, mask);
  775. cpulist_scnprintf(buf, sizeof(buf), mask);
  776. printk(KERN_DEBUG "%s cpu %d node %d: mask now %s\n",
  777. enable ? "numa_add_cpu" : "numa_remove_cpu", cpu, node, buf);
  778. }
  779. void __cpuinit numa_add_cpu(int cpu)
  780. {
  781. numa_set_cpumask(cpu, 1);
  782. }
  783. void __cpuinit numa_remove_cpu(int cpu)
  784. {
  785. numa_set_cpumask(cpu, 0);
  786. }
  787. int cpu_to_node(int cpu)
  788. {
  789. if (early_per_cpu_ptr(x86_cpu_to_node_map)) {
  790. printk(KERN_WARNING
  791. "cpu_to_node(%d): usage too early!\n", cpu);
  792. dump_stack();
  793. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  794. }
  795. return per_cpu(x86_cpu_to_node_map, cpu);
  796. }
  797. EXPORT_SYMBOL(cpu_to_node);
  798. /*
  799. * Same function as cpu_to_node() but used if called before the
  800. * per_cpu areas are setup.
  801. */
  802. int early_cpu_to_node(int cpu)
  803. {
  804. if (early_per_cpu_ptr(x86_cpu_to_node_map))
  805. return early_per_cpu_ptr(x86_cpu_to_node_map)[cpu];
  806. if (!cpu_possible(cpu)) {
  807. printk(KERN_WARNING
  808. "early_cpu_to_node(%d): no per_cpu area!\n", cpu);
  809. dump_stack();
  810. return NUMA_NO_NODE;
  811. }
  812. return per_cpu(x86_cpu_to_node_map, cpu);
  813. }
  814. /*
  815. * --------- end of debug versions of the numa functions ---------
  816. */
  817. #endif /* CONFIG_DEBUG_PER_CPU_MAPS */