numa_64.c 23 KB

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