numa_64.c 23 KB

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