numa_64.c 23 KB

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