numa.c 20 KB

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