numa.c 20 KB

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