numa_64.c 23 KB

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