numa_64.c 23 KB

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