numa.c 19 KB

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