numa_64.c 23 KB

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