numa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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. const 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 = 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 = get_property(cpu_node, "reg", &len);
  147. if (reg && (len > 0) && (reg[0] == hw_cpuid))
  148. return cpu_node;
  149. }
  150. }
  151. return NULL;
  152. }
  153. /* must hold reference to node during call */
  154. static const int *of_get_associativity(struct device_node *dev)
  155. {
  156. return get_property(dev, "ibm,associativity", NULL);
  157. }
  158. /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
  159. * info is found.
  160. */
  161. static int of_node_to_nid_single(struct device_node *device)
  162. {
  163. int nid = -1;
  164. const unsigned int *tmp;
  165. if (min_common_depth == -1)
  166. goto out;
  167. tmp = of_get_associativity(device);
  168. if (!tmp)
  169. goto out;
  170. if (tmp[0] >= min_common_depth)
  171. nid = tmp[min_common_depth];
  172. /* POWER4 LPAR uses 0xffff as invalid node */
  173. if (nid == 0xffff || nid >= MAX_NUMNODES)
  174. nid = -1;
  175. out:
  176. return nid;
  177. }
  178. /* Walk the device tree upwards, looking for an associativity id */
  179. int of_node_to_nid(struct device_node *device)
  180. {
  181. struct device_node *tmp;
  182. int nid = -1;
  183. of_node_get(device);
  184. while (device) {
  185. nid = of_node_to_nid_single(device);
  186. if (nid != -1)
  187. break;
  188. tmp = device;
  189. device = of_get_parent(tmp);
  190. of_node_put(tmp);
  191. }
  192. of_node_put(device);
  193. return nid;
  194. }
  195. EXPORT_SYMBOL_GPL(of_node_to_nid);
  196. /*
  197. * In theory, the "ibm,associativity" property may contain multiple
  198. * associativity lists because a resource may be multiply connected
  199. * into the machine. This resource then has different associativity
  200. * characteristics relative to its multiple connections. We ignore
  201. * this for now. We also assume that all cpu and memory sets have
  202. * their distances represented at a common level. This won't be
  203. * true for heirarchical NUMA.
  204. *
  205. * In any case the ibm,associativity-reference-points should give
  206. * the correct depth for a normal NUMA system.
  207. *
  208. * - Dave Hansen <haveblue@us.ibm.com>
  209. */
  210. static int __init find_min_common_depth(void)
  211. {
  212. int depth;
  213. const unsigned int *ref_points;
  214. struct device_node *rtas_root;
  215. unsigned int len;
  216. rtas_root = of_find_node_by_path("/rtas");
  217. if (!rtas_root)
  218. return -1;
  219. /*
  220. * this property is 2 32-bit integers, each representing a level of
  221. * depth in the associativity nodes. The first is for an SMP
  222. * configuration (should be all 0's) and the second is for a normal
  223. * NUMA configuration.
  224. */
  225. ref_points = get_property(rtas_root,
  226. "ibm,associativity-reference-points", &len);
  227. if ((len >= 1) && ref_points) {
  228. depth = ref_points[1];
  229. } else {
  230. dbg("NUMA: ibm,associativity-reference-points not found.\n");
  231. depth = -1;
  232. }
  233. of_node_put(rtas_root);
  234. return depth;
  235. }
  236. static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
  237. {
  238. struct device_node *memory = NULL;
  239. memory = of_find_node_by_type(memory, "memory");
  240. if (!memory)
  241. panic("numa.c: No memory nodes found!");
  242. *n_addr_cells = prom_n_addr_cells(memory);
  243. *n_size_cells = prom_n_size_cells(memory);
  244. of_node_put(memory);
  245. }
  246. static unsigned long __devinit read_n_cells(int n, const unsigned int **buf)
  247. {
  248. unsigned long result = 0;
  249. while (n--) {
  250. result = (result << 32) | **buf;
  251. (*buf)++;
  252. }
  253. return result;
  254. }
  255. /*
  256. * Figure out to which domain a cpu belongs and stick it there.
  257. * Return the id of the domain used.
  258. */
  259. static int __cpuinit numa_setup_cpu(unsigned long lcpu)
  260. {
  261. int nid = 0;
  262. struct device_node *cpu = find_cpu_node(lcpu);
  263. if (!cpu) {
  264. WARN_ON(1);
  265. goto out;
  266. }
  267. nid = of_node_to_nid_single(cpu);
  268. if (nid < 0 || !node_online(nid))
  269. nid = any_online_node(NODE_MASK_ALL);
  270. out:
  271. map_cpu_to_node(lcpu, nid);
  272. of_node_put(cpu);
  273. return nid;
  274. }
  275. static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
  276. unsigned long action,
  277. void *hcpu)
  278. {
  279. unsigned long lcpu = (unsigned long)hcpu;
  280. int ret = NOTIFY_DONE;
  281. switch (action) {
  282. case CPU_UP_PREPARE:
  283. numa_setup_cpu(lcpu);
  284. ret = NOTIFY_OK;
  285. break;
  286. #ifdef CONFIG_HOTPLUG_CPU
  287. case CPU_DEAD:
  288. case CPU_UP_CANCELED:
  289. unmap_cpu_from_node(lcpu);
  290. break;
  291. ret = NOTIFY_OK;
  292. #endif
  293. }
  294. return ret;
  295. }
  296. /*
  297. * Check and possibly modify a memory region to enforce the memory limit.
  298. *
  299. * Returns the size the region should have to enforce the memory limit.
  300. * This will either be the original value of size, a truncated value,
  301. * or zero. If the returned value of size is 0 the region should be
  302. * discarded as it lies wholy above the memory limit.
  303. */
  304. static unsigned long __init numa_enforce_memory_limit(unsigned long start,
  305. unsigned long size)
  306. {
  307. /*
  308. * We use lmb_end_of_DRAM() in here instead of memory_limit because
  309. * we've already adjusted it for the limit and it takes care of
  310. * having memory holes below the limit.
  311. */
  312. if (! memory_limit)
  313. return size;
  314. if (start + size <= lmb_end_of_DRAM())
  315. return size;
  316. if (start >= lmb_end_of_DRAM())
  317. return 0;
  318. return lmb_end_of_DRAM() - start;
  319. }
  320. static int __init parse_numa_properties(void)
  321. {
  322. struct device_node *cpu = NULL;
  323. struct device_node *memory = NULL;
  324. int default_nid = 0;
  325. unsigned long i;
  326. if (numa_enabled == 0) {
  327. printk(KERN_WARNING "NUMA disabled by user\n");
  328. return -1;
  329. }
  330. min_common_depth = find_min_common_depth();
  331. if (min_common_depth < 0)
  332. return min_common_depth;
  333. dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
  334. /*
  335. * Even though we connect cpus to numa domains later in SMP
  336. * init, we need to know the node ids now. This is because
  337. * each node to be onlined must have NODE_DATA etc backing it.
  338. */
  339. for_each_present_cpu(i) {
  340. int nid;
  341. cpu = find_cpu_node(i);
  342. BUG_ON(!cpu);
  343. nid = of_node_to_nid_single(cpu);
  344. of_node_put(cpu);
  345. /*
  346. * Don't fall back to default_nid yet -- we will plug
  347. * cpus into nodes once the memory scan has discovered
  348. * the topology.
  349. */
  350. if (nid < 0)
  351. continue;
  352. node_set_online(nid);
  353. }
  354. get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
  355. memory = NULL;
  356. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  357. unsigned long start;
  358. unsigned long size;
  359. int nid;
  360. int ranges;
  361. const unsigned int *memcell_buf;
  362. unsigned int len;
  363. memcell_buf = get_property(memory,
  364. "linux,usable-memory", &len);
  365. if (!memcell_buf || len <= 0)
  366. memcell_buf = get_property(memory, "reg", &len);
  367. if (!memcell_buf || len <= 0)
  368. continue;
  369. /* ranges in cell */
  370. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  371. new_range:
  372. /* these are order-sensitive, and modify the buffer pointer */
  373. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  374. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  375. /*
  376. * Assumption: either all memory nodes or none will
  377. * have associativity properties. If none, then
  378. * everything goes to default_nid.
  379. */
  380. nid = of_node_to_nid_single(memory);
  381. if (nid < 0)
  382. nid = default_nid;
  383. node_set_online(nid);
  384. if (!(size = numa_enforce_memory_limit(start, size))) {
  385. if (--ranges)
  386. goto new_range;
  387. else
  388. continue;
  389. }
  390. add_region(nid, start >> PAGE_SHIFT,
  391. size >> PAGE_SHIFT);
  392. if (--ranges)
  393. goto new_range;
  394. }
  395. return 0;
  396. }
  397. static void __init setup_nonnuma(void)
  398. {
  399. unsigned long top_of_ram = lmb_end_of_DRAM();
  400. unsigned long total_ram = lmb_phys_mem_size();
  401. unsigned int i;
  402. printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  403. top_of_ram, total_ram);
  404. printk(KERN_DEBUG "Memory hole size: %ldMB\n",
  405. (top_of_ram - total_ram) >> 20);
  406. for (i = 0; i < lmb.memory.cnt; ++i)
  407. add_region(0, lmb.memory.region[i].base >> PAGE_SHIFT,
  408. lmb_size_pages(&lmb.memory, i));
  409. node_set_online(0);
  410. }
  411. void __init dump_numa_cpu_topology(void)
  412. {
  413. unsigned int node;
  414. unsigned int cpu, count;
  415. if (min_common_depth == -1 || !numa_enabled)
  416. return;
  417. for_each_online_node(node) {
  418. printk(KERN_DEBUG "Node %d CPUs:", node);
  419. count = 0;
  420. /*
  421. * If we used a CPU iterator here we would miss printing
  422. * the holes in the cpumap.
  423. */
  424. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  425. if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
  426. if (count == 0)
  427. printk(" %u", cpu);
  428. ++count;
  429. } else {
  430. if (count > 1)
  431. printk("-%u", cpu - 1);
  432. count = 0;
  433. }
  434. }
  435. if (count > 1)
  436. printk("-%u", NR_CPUS - 1);
  437. printk("\n");
  438. }
  439. }
  440. static void __init dump_numa_memory_topology(void)
  441. {
  442. unsigned int node;
  443. unsigned int count;
  444. if (min_common_depth == -1 || !numa_enabled)
  445. return;
  446. for_each_online_node(node) {
  447. unsigned long i;
  448. printk(KERN_DEBUG "Node %d Memory:", node);
  449. count = 0;
  450. for (i = 0; i < lmb_end_of_DRAM();
  451. i += (1 << SECTION_SIZE_BITS)) {
  452. if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
  453. if (count == 0)
  454. printk(" 0x%lx", i);
  455. ++count;
  456. } else {
  457. if (count > 0)
  458. printk("-0x%lx", i);
  459. count = 0;
  460. }
  461. }
  462. if (count > 0)
  463. printk("-0x%lx", i);
  464. printk("\n");
  465. }
  466. }
  467. /*
  468. * Allocate some memory, satisfying the lmb or bootmem allocator where
  469. * required. nid is the preferred node and end is the physical address of
  470. * the highest address in the node.
  471. *
  472. * Returns the physical address of the memory.
  473. */
  474. static void __init *careful_allocation(int nid, unsigned long size,
  475. unsigned long align,
  476. unsigned long end_pfn)
  477. {
  478. int new_nid;
  479. unsigned long ret = __lmb_alloc_base(size, align, end_pfn << PAGE_SHIFT);
  480. /* retry over all memory */
  481. if (!ret)
  482. ret = __lmb_alloc_base(size, align, lmb_end_of_DRAM());
  483. if (!ret)
  484. panic("numa.c: cannot allocate %lu bytes on node %d",
  485. size, nid);
  486. /*
  487. * If the memory came from a previously allocated node, we must
  488. * retry with the bootmem allocator.
  489. */
  490. new_nid = early_pfn_to_nid(ret >> PAGE_SHIFT);
  491. if (new_nid < nid) {
  492. ret = (unsigned long)__alloc_bootmem_node(NODE_DATA(new_nid),
  493. size, align, 0);
  494. if (!ret)
  495. panic("numa.c: cannot allocate %lu bytes on node %d",
  496. size, new_nid);
  497. ret = __pa(ret);
  498. dbg("alloc_bootmem %lx %lx\n", ret, size);
  499. }
  500. return (void *)ret;
  501. }
  502. static struct notifier_block __cpuinitdata ppc64_numa_nb = {
  503. .notifier_call = cpu_numa_callback,
  504. .priority = 1 /* Must run before sched domains notifier. */
  505. };
  506. void __init do_init_bootmem(void)
  507. {
  508. int nid;
  509. unsigned int i;
  510. min_low_pfn = 0;
  511. max_low_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
  512. max_pfn = max_low_pfn;
  513. if (parse_numa_properties())
  514. setup_nonnuma();
  515. else
  516. dump_numa_memory_topology();
  517. register_cpu_notifier(&ppc64_numa_nb);
  518. cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
  519. (void *)(unsigned long)boot_cpuid);
  520. for_each_online_node(nid) {
  521. unsigned long start_pfn, end_pfn, pages_present;
  522. unsigned long bootmem_paddr;
  523. unsigned long bootmap_pages;
  524. get_region(nid, &start_pfn, &end_pfn, &pages_present);
  525. /* Allocate the node structure node local if possible */
  526. NODE_DATA(nid) = careful_allocation(nid,
  527. sizeof(struct pglist_data),
  528. SMP_CACHE_BYTES, end_pfn);
  529. NODE_DATA(nid) = __va(NODE_DATA(nid));
  530. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  531. dbg("node %d\n", nid);
  532. dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
  533. NODE_DATA(nid)->bdata = &plat_node_bdata[nid];
  534. NODE_DATA(nid)->node_start_pfn = start_pfn;
  535. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  536. if (NODE_DATA(nid)->node_spanned_pages == 0)
  537. continue;
  538. dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
  539. dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
  540. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  541. bootmem_paddr = (unsigned long)careful_allocation(nid,
  542. bootmap_pages << PAGE_SHIFT,
  543. PAGE_SIZE, end_pfn);
  544. memset(__va(bootmem_paddr), 0, bootmap_pages << PAGE_SHIFT);
  545. dbg("bootmap_paddr = %lx\n", bootmem_paddr);
  546. init_bootmem_node(NODE_DATA(nid), bootmem_paddr >> PAGE_SHIFT,
  547. start_pfn, end_pfn);
  548. /* Add free regions on this node */
  549. for (i = 0; init_node_data[i].end_pfn; i++) {
  550. unsigned long start, end;
  551. if (init_node_data[i].nid != nid)
  552. continue;
  553. start = init_node_data[i].start_pfn << PAGE_SHIFT;
  554. end = init_node_data[i].end_pfn << PAGE_SHIFT;
  555. dbg("free_bootmem %lx %lx\n", start, end - start);
  556. free_bootmem_node(NODE_DATA(nid), start, end - start);
  557. }
  558. /* Mark reserved regions on this node */
  559. for (i = 0; i < lmb.reserved.cnt; i++) {
  560. unsigned long physbase = lmb.reserved.region[i].base;
  561. unsigned long size = lmb.reserved.region[i].size;
  562. unsigned long start_paddr = start_pfn << PAGE_SHIFT;
  563. unsigned long end_paddr = end_pfn << PAGE_SHIFT;
  564. if (early_pfn_to_nid(physbase >> PAGE_SHIFT) != nid &&
  565. early_pfn_to_nid((physbase+size-1) >> PAGE_SHIFT) != nid)
  566. continue;
  567. if (physbase < end_paddr &&
  568. (physbase+size) > start_paddr) {
  569. /* overlaps */
  570. if (physbase < start_paddr) {
  571. size -= start_paddr - physbase;
  572. physbase = start_paddr;
  573. }
  574. if (size > end_paddr - physbase)
  575. size = end_paddr - physbase;
  576. dbg("reserve_bootmem %lx %lx\n", physbase,
  577. size);
  578. reserve_bootmem_node(NODE_DATA(nid), physbase,
  579. size);
  580. }
  581. }
  582. /* Add regions into sparsemem */
  583. for (i = 0; init_node_data[i].end_pfn; i++) {
  584. unsigned long start, end;
  585. if (init_node_data[i].nid != nid)
  586. continue;
  587. start = init_node_data[i].start_pfn;
  588. end = init_node_data[i].end_pfn;
  589. memory_present(nid, start, end);
  590. }
  591. }
  592. }
  593. void __init paging_init(void)
  594. {
  595. unsigned long zones_size[MAX_NR_ZONES];
  596. unsigned long zholes_size[MAX_NR_ZONES];
  597. int nid;
  598. memset(zones_size, 0, sizeof(zones_size));
  599. memset(zholes_size, 0, sizeof(zholes_size));
  600. for_each_online_node(nid) {
  601. unsigned long start_pfn, end_pfn, pages_present;
  602. get_region(nid, &start_pfn, &end_pfn, &pages_present);
  603. zones_size[ZONE_DMA] = end_pfn - start_pfn;
  604. zholes_size[ZONE_DMA] = zones_size[ZONE_DMA] - pages_present;
  605. dbg("free_area_init node %d %lx %lx (hole: %lx)\n", nid,
  606. zones_size[ZONE_DMA], start_pfn, zholes_size[ZONE_DMA]);
  607. free_area_init_node(nid, NODE_DATA(nid), zones_size, start_pfn,
  608. zholes_size);
  609. }
  610. }
  611. static int __init early_numa(char *p)
  612. {
  613. if (!p)
  614. return 0;
  615. if (strstr(p, "off"))
  616. numa_enabled = 0;
  617. if (strstr(p, "debug"))
  618. numa_debug = 1;
  619. return 0;
  620. }
  621. early_param("numa", early_numa);
  622. #ifdef CONFIG_MEMORY_HOTPLUG
  623. /*
  624. * Find the node associated with a hot added memory section. Section
  625. * corresponds to a SPARSEMEM section, not an LMB. It is assumed that
  626. * sections are fully contained within a single LMB.
  627. */
  628. int hot_add_scn_to_nid(unsigned long scn_addr)
  629. {
  630. struct device_node *memory = NULL;
  631. nodemask_t nodes;
  632. int default_nid = any_online_node(NODE_MASK_ALL);
  633. int nid;
  634. if (!numa_enabled || (min_common_depth < 0))
  635. return default_nid;
  636. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  637. unsigned long start, size;
  638. int ranges;
  639. const unsigned int *memcell_buf;
  640. unsigned int len;
  641. memcell_buf = get_property(memory, "reg", &len);
  642. if (!memcell_buf || len <= 0)
  643. continue;
  644. /* ranges in cell */
  645. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  646. ha_new_range:
  647. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  648. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  649. nid = of_node_to_nid_single(memory);
  650. /* Domains not present at boot default to 0 */
  651. if (nid < 0 || !node_online(nid))
  652. nid = default_nid;
  653. if ((scn_addr >= start) && (scn_addr < (start + size))) {
  654. of_node_put(memory);
  655. goto got_nid;
  656. }
  657. if (--ranges) /* process all ranges in cell */
  658. goto ha_new_range;
  659. }
  660. BUG(); /* section address should be found above */
  661. return 0;
  662. /* Temporary code to ensure that returned node is not empty */
  663. got_nid:
  664. nodes_setall(nodes);
  665. while (NODE_DATA(nid)->node_spanned_pages == 0) {
  666. node_clear(nid, nodes);
  667. nid = any_online_node(nodes);
  668. }
  669. return nid;
  670. }
  671. #endif /* CONFIG_MEMORY_HOTPLUG */