numa.c 20 KB

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