numa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * pSeries NUMA support
  3. *
  4. * Copyright (C) 2002 Anton Blanchard <anton@au.ibm.com>, IBM
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/threads.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/init.h>
  14. #include <linux/mm.h>
  15. #include <linux/mmzone.h>
  16. #include <linux/module.h>
  17. #include <linux/nodemask.h>
  18. #include <linux/cpu.h>
  19. #include <linux/notifier.h>
  20. #include <asm/sparsemem.h>
  21. #include <asm/lmb.h>
  22. #include <asm/system.h>
  23. #include <asm/smp.h>
  24. static int numa_enabled = 1;
  25. static int numa_debug;
  26. #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
  27. int numa_cpu_lookup_table[NR_CPUS];
  28. cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
  29. struct pglist_data *node_data[MAX_NUMNODES];
  30. EXPORT_SYMBOL(numa_cpu_lookup_table);
  31. EXPORT_SYMBOL(numa_cpumask_lookup_table);
  32. EXPORT_SYMBOL(node_data);
  33. static bootmem_data_t __initdata plat_node_bdata[MAX_NUMNODES];
  34. static int min_common_depth;
  35. static int n_mem_addr_cells, n_mem_size_cells;
  36. /*
  37. * We need somewhere to store start/end/node for each region until we have
  38. * allocated the real node_data structures.
  39. */
  40. #define MAX_REGIONS (MAX_LMB_REGIONS*2)
  41. static struct {
  42. unsigned long start_pfn;
  43. unsigned long end_pfn;
  44. int nid;
  45. } init_node_data[MAX_REGIONS] __initdata;
  46. int __init early_pfn_to_nid(unsigned long pfn)
  47. {
  48. unsigned int i;
  49. for (i = 0; init_node_data[i].end_pfn; i++) {
  50. unsigned long start_pfn = init_node_data[i].start_pfn;
  51. unsigned long end_pfn = init_node_data[i].end_pfn;
  52. if ((start_pfn <= pfn) && (pfn < end_pfn))
  53. return init_node_data[i].nid;
  54. }
  55. return -1;
  56. }
  57. void __init add_region(unsigned int nid, unsigned long start_pfn,
  58. unsigned long pages)
  59. {
  60. unsigned int i;
  61. dbg("add_region nid %d start_pfn 0x%lx pages 0x%lx\n",
  62. nid, start_pfn, pages);
  63. for (i = 0; init_node_data[i].end_pfn; i++) {
  64. if (init_node_data[i].nid != nid)
  65. continue;
  66. if (init_node_data[i].end_pfn == start_pfn) {
  67. init_node_data[i].end_pfn += pages;
  68. return;
  69. }
  70. if (init_node_data[i].start_pfn == (start_pfn + pages)) {
  71. init_node_data[i].start_pfn -= pages;
  72. return;
  73. }
  74. }
  75. /*
  76. * Leave last entry NULL so we dont iterate off the end (we use
  77. * entry.end_pfn to terminate the walk).
  78. */
  79. if (i >= (MAX_REGIONS - 1)) {
  80. printk(KERN_ERR "WARNING: too many memory regions in "
  81. "numa code, truncating\n");
  82. return;
  83. }
  84. init_node_data[i].start_pfn = start_pfn;
  85. init_node_data[i].end_pfn = start_pfn + pages;
  86. init_node_data[i].nid = nid;
  87. }
  88. /* We assume init_node_data has no overlapping regions */
  89. void __init get_region(unsigned int nid, unsigned long *start_pfn,
  90. unsigned long *end_pfn, unsigned long *pages_present)
  91. {
  92. unsigned int i;
  93. *start_pfn = -1UL;
  94. *end_pfn = *pages_present = 0;
  95. for (i = 0; init_node_data[i].end_pfn; i++) {
  96. if (init_node_data[i].nid != nid)
  97. continue;
  98. *pages_present += init_node_data[i].end_pfn -
  99. init_node_data[i].start_pfn;
  100. if (init_node_data[i].start_pfn < *start_pfn)
  101. *start_pfn = init_node_data[i].start_pfn;
  102. if (init_node_data[i].end_pfn > *end_pfn)
  103. *end_pfn = init_node_data[i].end_pfn;
  104. }
  105. /* We didnt find a matching region, return start/end as 0 */
  106. if (*start_pfn == -1UL)
  107. *start_pfn = 0;
  108. }
  109. static void __cpuinit map_cpu_to_node(int cpu, int node)
  110. {
  111. numa_cpu_lookup_table[cpu] = node;
  112. dbg("adding cpu %d to node %d\n", cpu, node);
  113. if (!(cpu_isset(cpu, numa_cpumask_lookup_table[node])))
  114. cpu_set(cpu, numa_cpumask_lookup_table[node]);
  115. }
  116. #ifdef CONFIG_HOTPLUG_CPU
  117. static void unmap_cpu_from_node(unsigned long cpu)
  118. {
  119. int node = numa_cpu_lookup_table[cpu];
  120. dbg("removing cpu %lu from node %d\n", cpu, node);
  121. if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
  122. cpu_clear(cpu, numa_cpumask_lookup_table[node]);
  123. } else {
  124. printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
  125. cpu, node);
  126. }
  127. }
  128. #endif /* CONFIG_HOTPLUG_CPU */
  129. static struct device_node * __cpuinit find_cpu_node(unsigned int cpu)
  130. {
  131. unsigned int hw_cpuid = get_hard_smp_processor_id(cpu);
  132. struct device_node *cpu_node = NULL;
  133. unsigned int *interrupt_server, *reg;
  134. int len;
  135. while ((cpu_node = of_find_node_by_type(cpu_node, "cpu")) != NULL) {
  136. /* Try interrupt server first */
  137. interrupt_server = (unsigned int *)get_property(cpu_node,
  138. "ibm,ppc-interrupt-server#s", &len);
  139. len = len / sizeof(u32);
  140. if (interrupt_server && (len > 0)) {
  141. while (len--) {
  142. if (interrupt_server[len] == hw_cpuid)
  143. return cpu_node;
  144. }
  145. } else {
  146. reg = (unsigned int *)get_property(cpu_node,
  147. "reg", &len);
  148. if (reg && (len > 0) && (reg[0] == hw_cpuid))
  149. return cpu_node;
  150. }
  151. }
  152. return NULL;
  153. }
  154. /* must hold reference to node during call */
  155. static int *of_get_associativity(struct device_node *dev)
  156. {
  157. return (unsigned int *)get_property(dev, "ibm,associativity", NULL);
  158. }
  159. /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
  160. * info is found.
  161. */
  162. static int of_node_to_nid(struct device_node *device)
  163. {
  164. int nid = -1;
  165. unsigned int *tmp;
  166. if (min_common_depth == -1)
  167. goto out;
  168. tmp = of_get_associativity(device);
  169. if (!tmp)
  170. goto out;
  171. if (tmp[0] >= min_common_depth)
  172. nid = tmp[min_common_depth];
  173. /* POWER4 LPAR uses 0xffff as invalid node */
  174. if (nid == 0xffff || nid >= MAX_NUMNODES)
  175. nid = -1;
  176. out:
  177. return nid;
  178. }
  179. /*
  180. * In theory, the "ibm,associativity" property may contain multiple
  181. * associativity lists because a resource may be multiply connected
  182. * into the machine. This resource then has different associativity
  183. * characteristics relative to its multiple connections. We ignore
  184. * this for now. We also assume that all cpu and memory sets have
  185. * their distances represented at a common level. This won't be
  186. * true for heirarchical NUMA.
  187. *
  188. * In any case the ibm,associativity-reference-points should give
  189. * the correct depth for a normal NUMA system.
  190. *
  191. * - Dave Hansen <haveblue@us.ibm.com>
  192. */
  193. static int __init find_min_common_depth(void)
  194. {
  195. int depth;
  196. unsigned int *ref_points;
  197. struct device_node *rtas_root;
  198. unsigned int len;
  199. rtas_root = of_find_node_by_path("/rtas");
  200. if (!rtas_root)
  201. return -1;
  202. /*
  203. * this property is 2 32-bit integers, each representing a level of
  204. * depth in the associativity nodes. The first is for an SMP
  205. * configuration (should be all 0's) and the second is for a normal
  206. * NUMA configuration.
  207. */
  208. ref_points = (unsigned int *)get_property(rtas_root,
  209. "ibm,associativity-reference-points", &len);
  210. if ((len >= 1) && ref_points) {
  211. depth = ref_points[1];
  212. } else {
  213. dbg("NUMA: ibm,associativity-reference-points not found.\n");
  214. depth = -1;
  215. }
  216. of_node_put(rtas_root);
  217. return depth;
  218. }
  219. static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
  220. {
  221. struct device_node *memory = NULL;
  222. memory = of_find_node_by_type(memory, "memory");
  223. if (!memory)
  224. panic("numa.c: No memory nodes found!");
  225. *n_addr_cells = prom_n_addr_cells(memory);
  226. *n_size_cells = prom_n_size_cells(memory);
  227. of_node_put(memory);
  228. }
  229. static unsigned long __devinit read_n_cells(int n, unsigned int **buf)
  230. {
  231. unsigned long result = 0;
  232. while (n--) {
  233. result = (result << 32) | **buf;
  234. (*buf)++;
  235. }
  236. return result;
  237. }
  238. /*
  239. * Figure out to which domain a cpu belongs and stick it there.
  240. * Return the id of the domain used.
  241. */
  242. static int __cpuinit numa_setup_cpu(unsigned long lcpu)
  243. {
  244. int nid = 0;
  245. struct device_node *cpu = find_cpu_node(lcpu);
  246. if (!cpu) {
  247. WARN_ON(1);
  248. goto out;
  249. }
  250. nid = of_node_to_nid(cpu);
  251. if (nid < 0 || !node_online(nid))
  252. nid = any_online_node(NODE_MASK_ALL);
  253. out:
  254. map_cpu_to_node(lcpu, nid);
  255. of_node_put(cpu);
  256. return nid;
  257. }
  258. static int cpu_numa_callback(struct notifier_block *nfb,
  259. unsigned long action,
  260. void *hcpu)
  261. {
  262. unsigned long lcpu = (unsigned long)hcpu;
  263. int ret = NOTIFY_DONE;
  264. switch (action) {
  265. case CPU_UP_PREPARE:
  266. if (min_common_depth == -1 || !numa_enabled)
  267. map_cpu_to_node(lcpu, 0);
  268. else
  269. numa_setup_cpu(lcpu);
  270. ret = NOTIFY_OK;
  271. break;
  272. #ifdef CONFIG_HOTPLUG_CPU
  273. case CPU_DEAD:
  274. case CPU_UP_CANCELED:
  275. unmap_cpu_from_node(lcpu);
  276. break;
  277. ret = NOTIFY_OK;
  278. #endif
  279. }
  280. return ret;
  281. }
  282. /*
  283. * Check and possibly modify a memory region to enforce the memory limit.
  284. *
  285. * Returns the size the region should have to enforce the memory limit.
  286. * This will either be the original value of size, a truncated value,
  287. * or zero. If the returned value of size is 0 the region should be
  288. * discarded as it lies wholy above the memory limit.
  289. */
  290. static unsigned long __init numa_enforce_memory_limit(unsigned long start,
  291. unsigned long size)
  292. {
  293. /*
  294. * We use lmb_end_of_DRAM() in here instead of memory_limit because
  295. * we've already adjusted it for the limit and it takes care of
  296. * having memory holes below the limit.
  297. */
  298. if (! memory_limit)
  299. return size;
  300. if (start + size <= lmb_end_of_DRAM())
  301. return size;
  302. if (start >= lmb_end_of_DRAM())
  303. return 0;
  304. return lmb_end_of_DRAM() - start;
  305. }
  306. static int __init parse_numa_properties(void)
  307. {
  308. struct device_node *cpu = NULL;
  309. struct device_node *memory = NULL;
  310. int default_nid = 0;
  311. unsigned long i;
  312. if (numa_enabled == 0) {
  313. printk(KERN_WARNING "NUMA disabled by user\n");
  314. return -1;
  315. }
  316. min_common_depth = find_min_common_depth();
  317. if (min_common_depth < 0)
  318. return min_common_depth;
  319. dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
  320. /*
  321. * Even though we connect cpus to numa domains later in SMP
  322. * init, we need to know the node ids now. This is because
  323. * each node to be onlined must have NODE_DATA etc backing it.
  324. */
  325. for_each_present_cpu(i) {
  326. int nid;
  327. cpu = find_cpu_node(i);
  328. BUG_ON(!cpu);
  329. nid = of_node_to_nid(cpu);
  330. of_node_put(cpu);
  331. /*
  332. * Don't fall back to default_nid yet -- we will plug
  333. * cpus into nodes once the memory scan has discovered
  334. * the topology.
  335. */
  336. if (nid < 0)
  337. continue;
  338. node_set_online(nid);
  339. }
  340. get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
  341. memory = NULL;
  342. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  343. unsigned long start;
  344. unsigned long size;
  345. int nid;
  346. int ranges;
  347. unsigned int *memcell_buf;
  348. unsigned int len;
  349. memcell_buf = (unsigned int *)get_property(memory,
  350. "linux,usable-memory", &len);
  351. if (!memcell_buf || len <= 0)
  352. memcell_buf =
  353. (unsigned int *)get_property(memory, "reg",
  354. &len);
  355. if (!memcell_buf || len <= 0)
  356. continue;
  357. /* ranges in cell */
  358. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  359. new_range:
  360. /* these are order-sensitive, and modify the buffer pointer */
  361. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  362. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  363. /*
  364. * Assumption: either all memory nodes or none will
  365. * have associativity properties. If none, then
  366. * everything goes to default_nid.
  367. */
  368. nid = of_node_to_nid(memory);
  369. if (nid < 0)
  370. nid = default_nid;
  371. node_set_online(nid);
  372. if (!(size = numa_enforce_memory_limit(start, size))) {
  373. if (--ranges)
  374. goto new_range;
  375. else
  376. continue;
  377. }
  378. add_region(nid, start >> PAGE_SHIFT,
  379. size >> PAGE_SHIFT);
  380. if (--ranges)
  381. goto new_range;
  382. }
  383. numa_setup_cpu(boot_cpuid);
  384. return 0;
  385. }
  386. static void __init setup_nonnuma(void)
  387. {
  388. unsigned long top_of_ram = lmb_end_of_DRAM();
  389. unsigned long total_ram = lmb_phys_mem_size();
  390. unsigned int i;
  391. printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  392. top_of_ram, total_ram);
  393. printk(KERN_INFO "Memory hole size: %ldMB\n",
  394. (top_of_ram - total_ram) >> 20);
  395. map_cpu_to_node(boot_cpuid, 0);
  396. for (i = 0; i < lmb.memory.cnt; ++i)
  397. add_region(0, lmb.memory.region[i].base >> PAGE_SHIFT,
  398. lmb_size_pages(&lmb.memory, i));
  399. node_set_online(0);
  400. }
  401. void __init dump_numa_cpu_topology(void)
  402. {
  403. unsigned int node;
  404. unsigned int cpu, count;
  405. if (min_common_depth == -1 || !numa_enabled)
  406. return;
  407. for_each_online_node(node) {
  408. printk(KERN_INFO "Node %d CPUs:", node);
  409. count = 0;
  410. /*
  411. * If we used a CPU iterator here we would miss printing
  412. * the holes in the cpumap.
  413. */
  414. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  415. if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
  416. if (count == 0)
  417. printk(" %u", cpu);
  418. ++count;
  419. } else {
  420. if (count > 1)
  421. printk("-%u", cpu - 1);
  422. count = 0;
  423. }
  424. }
  425. if (count > 1)
  426. printk("-%u", NR_CPUS - 1);
  427. printk("\n");
  428. }
  429. }
  430. static void __init dump_numa_memory_topology(void)
  431. {
  432. unsigned int node;
  433. unsigned int count;
  434. if (min_common_depth == -1 || !numa_enabled)
  435. return;
  436. for_each_online_node(node) {
  437. unsigned long i;
  438. printk(KERN_INFO "Node %d Memory:", node);
  439. count = 0;
  440. for (i = 0; i < lmb_end_of_DRAM();
  441. i += (1 << SECTION_SIZE_BITS)) {
  442. if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
  443. if (count == 0)
  444. printk(" 0x%lx", i);
  445. ++count;
  446. } else {
  447. if (count > 0)
  448. printk("-0x%lx", i);
  449. count = 0;
  450. }
  451. }
  452. if (count > 0)
  453. printk("-0x%lx", i);
  454. printk("\n");
  455. }
  456. }
  457. /*
  458. * Allocate some memory, satisfying the lmb or bootmem allocator where
  459. * required. nid is the preferred node and end is the physical address of
  460. * the highest address in the node.
  461. *
  462. * Returns the physical address of the memory.
  463. */
  464. static void __init *careful_allocation(int nid, unsigned long size,
  465. unsigned long align,
  466. unsigned long end_pfn)
  467. {
  468. int new_nid;
  469. unsigned long ret = __lmb_alloc_base(size, align, end_pfn << PAGE_SHIFT);
  470. /* retry over all memory */
  471. if (!ret)
  472. ret = __lmb_alloc_base(size, align, lmb_end_of_DRAM());
  473. if (!ret)
  474. panic("numa.c: cannot allocate %lu bytes on node %d",
  475. size, nid);
  476. /*
  477. * If the memory came from a previously allocated node, we must
  478. * retry with the bootmem allocator.
  479. */
  480. new_nid = early_pfn_to_nid(ret >> PAGE_SHIFT);
  481. if (new_nid < nid) {
  482. ret = (unsigned long)__alloc_bootmem_node(NODE_DATA(new_nid),
  483. size, align, 0);
  484. if (!ret)
  485. panic("numa.c: cannot allocate %lu bytes on node %d",
  486. size, new_nid);
  487. ret = __pa(ret);
  488. dbg("alloc_bootmem %lx %lx\n", ret, size);
  489. }
  490. return (void *)ret;
  491. }
  492. void __init do_init_bootmem(void)
  493. {
  494. int nid;
  495. unsigned int i;
  496. static struct notifier_block ppc64_numa_nb = {
  497. .notifier_call = cpu_numa_callback,
  498. .priority = 1 /* Must run before sched domains notifier. */
  499. };
  500. min_low_pfn = 0;
  501. max_low_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
  502. max_pfn = max_low_pfn;
  503. if (parse_numa_properties())
  504. setup_nonnuma();
  505. else
  506. dump_numa_memory_topology();
  507. register_cpu_notifier(&ppc64_numa_nb);
  508. for_each_online_node(nid) {
  509. unsigned long start_pfn, end_pfn, pages_present;
  510. unsigned long bootmem_paddr;
  511. unsigned long bootmap_pages;
  512. get_region(nid, &start_pfn, &end_pfn, &pages_present);
  513. /* Allocate the node structure node local if possible */
  514. NODE_DATA(nid) = careful_allocation(nid,
  515. sizeof(struct pglist_data),
  516. SMP_CACHE_BYTES, end_pfn);
  517. NODE_DATA(nid) = __va(NODE_DATA(nid));
  518. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  519. dbg("node %d\n", nid);
  520. dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
  521. NODE_DATA(nid)->bdata = &plat_node_bdata[nid];
  522. NODE_DATA(nid)->node_start_pfn = start_pfn;
  523. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  524. if (NODE_DATA(nid)->node_spanned_pages == 0)
  525. continue;
  526. dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
  527. dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
  528. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  529. bootmem_paddr = (unsigned long)careful_allocation(nid,
  530. bootmap_pages << PAGE_SHIFT,
  531. PAGE_SIZE, end_pfn);
  532. memset(__va(bootmem_paddr), 0, bootmap_pages << PAGE_SHIFT);
  533. dbg("bootmap_paddr = %lx\n", bootmem_paddr);
  534. init_bootmem_node(NODE_DATA(nid), bootmem_paddr >> PAGE_SHIFT,
  535. start_pfn, end_pfn);
  536. /* Add free regions on this node */
  537. for (i = 0; init_node_data[i].end_pfn; i++) {
  538. unsigned long start, end;
  539. if (init_node_data[i].nid != nid)
  540. continue;
  541. start = init_node_data[i].start_pfn << PAGE_SHIFT;
  542. end = init_node_data[i].end_pfn << PAGE_SHIFT;
  543. dbg("free_bootmem %lx %lx\n", start, end - start);
  544. free_bootmem_node(NODE_DATA(nid), start, end - start);
  545. }
  546. /* Mark reserved regions on this node */
  547. for (i = 0; i < lmb.reserved.cnt; i++) {
  548. unsigned long physbase = lmb.reserved.region[i].base;
  549. unsigned long size = lmb.reserved.region[i].size;
  550. unsigned long start_paddr = start_pfn << PAGE_SHIFT;
  551. unsigned long end_paddr = end_pfn << PAGE_SHIFT;
  552. if (early_pfn_to_nid(physbase >> PAGE_SHIFT) != nid &&
  553. early_pfn_to_nid((physbase+size-1) >> PAGE_SHIFT) != nid)
  554. continue;
  555. if (physbase < end_paddr &&
  556. (physbase+size) > start_paddr) {
  557. /* overlaps */
  558. if (physbase < start_paddr) {
  559. size -= start_paddr - physbase;
  560. physbase = start_paddr;
  561. }
  562. if (size > end_paddr - physbase)
  563. size = end_paddr - physbase;
  564. dbg("reserve_bootmem %lx %lx\n", physbase,
  565. size);
  566. reserve_bootmem_node(NODE_DATA(nid), physbase,
  567. size);
  568. }
  569. }
  570. /* Add regions into sparsemem */
  571. for (i = 0; init_node_data[i].end_pfn; i++) {
  572. unsigned long start, end;
  573. if (init_node_data[i].nid != nid)
  574. continue;
  575. start = init_node_data[i].start_pfn;
  576. end = init_node_data[i].end_pfn;
  577. memory_present(nid, start, end);
  578. }
  579. }
  580. }
  581. void __init paging_init(void)
  582. {
  583. unsigned long zones_size[MAX_NR_ZONES];
  584. unsigned long zholes_size[MAX_NR_ZONES];
  585. int nid;
  586. memset(zones_size, 0, sizeof(zones_size));
  587. memset(zholes_size, 0, sizeof(zholes_size));
  588. for_each_online_node(nid) {
  589. unsigned long start_pfn, end_pfn, pages_present;
  590. get_region(nid, &start_pfn, &end_pfn, &pages_present);
  591. zones_size[ZONE_DMA] = end_pfn - start_pfn;
  592. zholes_size[ZONE_DMA] = zones_size[ZONE_DMA] - pages_present;
  593. dbg("free_area_init node %d %lx %lx (hole: %lx)\n", nid,
  594. zones_size[ZONE_DMA], start_pfn, zholes_size[ZONE_DMA]);
  595. free_area_init_node(nid, NODE_DATA(nid), zones_size, start_pfn,
  596. zholes_size);
  597. }
  598. }
  599. static int __init early_numa(char *p)
  600. {
  601. if (!p)
  602. return 0;
  603. if (strstr(p, "off"))
  604. numa_enabled = 0;
  605. if (strstr(p, "debug"))
  606. numa_debug = 1;
  607. return 0;
  608. }
  609. early_param("numa", early_numa);
  610. #ifdef CONFIG_MEMORY_HOTPLUG
  611. /*
  612. * Find the node associated with a hot added memory section. Section
  613. * corresponds to a SPARSEMEM section, not an LMB. It is assumed that
  614. * sections are fully contained within a single LMB.
  615. */
  616. int hot_add_scn_to_nid(unsigned long scn_addr)
  617. {
  618. struct device_node *memory = NULL;
  619. nodemask_t nodes;
  620. int default_nid = any_online_node(NODE_MASK_ALL);
  621. if (!numa_enabled || (min_common_depth < 0))
  622. return default_nid;
  623. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  624. unsigned long start, size;
  625. int ranges;
  626. unsigned int *memcell_buf;
  627. unsigned int len;
  628. memcell_buf = (unsigned int *)get_property(memory, "reg", &len);
  629. if (!memcell_buf || len <= 0)
  630. continue;
  631. /* ranges in cell */
  632. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  633. ha_new_range:
  634. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  635. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  636. nid = of_node_to_nid(memory);
  637. /* Domains not present at boot default to 0 */
  638. if (nid < 0 || !node_online(nid))
  639. nid = default_nid;
  640. if ((scn_addr >= start) && (scn_addr < (start + size))) {
  641. of_node_put(memory);
  642. goto got_nid;
  643. }
  644. if (--ranges) /* process all ranges in cell */
  645. goto ha_new_range;
  646. }
  647. BUG(); /* section address should be found above */
  648. /* Temporary code to ensure that returned node is not empty */
  649. got_nid:
  650. nodes_setall(nodes);
  651. while (NODE_DATA(nid)->node_spanned_pages == 0) {
  652. node_clear(nid, nodes);
  653. nid = any_online_node(nodes);
  654. }
  655. return nid;
  656. }
  657. #endif /* CONFIG_MEMORY_HOTPLUG */