numa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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/lmb.h>
  21. #include <asm/machdep.h>
  22. #include <asm/abs_addr.h>
  23. #include <asm/system.h>
  24. static int numa_enabled = 1;
  25. static int numa_debug;
  26. #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
  27. #ifdef DEBUG_NUMA
  28. #define ARRAY_INITIALISER -1
  29. #else
  30. #define ARRAY_INITIALISER 0
  31. #endif
  32. int numa_cpu_lookup_table[NR_CPUS] = { [ 0 ... (NR_CPUS - 1)] =
  33. ARRAY_INITIALISER};
  34. char *numa_memory_lookup_table;
  35. cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
  36. int nr_cpus_in_node[MAX_NUMNODES] = { [0 ... (MAX_NUMNODES -1)] = 0};
  37. struct pglist_data *node_data[MAX_NUMNODES];
  38. bootmem_data_t __initdata plat_node_bdata[MAX_NUMNODES];
  39. static int min_common_depth;
  40. /*
  41. * We need somewhere to store start/span for each node until we have
  42. * allocated the real node_data structures.
  43. */
  44. static struct {
  45. unsigned long node_start_pfn;
  46. unsigned long node_end_pfn;
  47. unsigned long node_present_pages;
  48. } init_node_data[MAX_NUMNODES] __initdata;
  49. EXPORT_SYMBOL(node_data);
  50. EXPORT_SYMBOL(numa_cpu_lookup_table);
  51. EXPORT_SYMBOL(numa_memory_lookup_table);
  52. EXPORT_SYMBOL(numa_cpumask_lookup_table);
  53. EXPORT_SYMBOL(nr_cpus_in_node);
  54. static inline void map_cpu_to_node(int cpu, int node)
  55. {
  56. numa_cpu_lookup_table[cpu] = node;
  57. if (!(cpu_isset(cpu, numa_cpumask_lookup_table[node]))) {
  58. cpu_set(cpu, numa_cpumask_lookup_table[node]);
  59. nr_cpus_in_node[node]++;
  60. }
  61. }
  62. #ifdef CONFIG_HOTPLUG_CPU
  63. static void unmap_cpu_from_node(unsigned long cpu)
  64. {
  65. int node = numa_cpu_lookup_table[cpu];
  66. dbg("removing cpu %lu from node %d\n", cpu, node);
  67. if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
  68. cpu_clear(cpu, numa_cpumask_lookup_table[node]);
  69. nr_cpus_in_node[node]--;
  70. } else {
  71. printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
  72. cpu, node);
  73. }
  74. }
  75. #endif /* CONFIG_HOTPLUG_CPU */
  76. static struct device_node * __devinit find_cpu_node(unsigned int cpu)
  77. {
  78. unsigned int hw_cpuid = get_hard_smp_processor_id(cpu);
  79. struct device_node *cpu_node = NULL;
  80. unsigned int *interrupt_server, *reg;
  81. int len;
  82. while ((cpu_node = of_find_node_by_type(cpu_node, "cpu")) != NULL) {
  83. /* Try interrupt server first */
  84. interrupt_server = (unsigned int *)get_property(cpu_node,
  85. "ibm,ppc-interrupt-server#s", &len);
  86. len = len / sizeof(u32);
  87. if (interrupt_server && (len > 0)) {
  88. while (len--) {
  89. if (interrupt_server[len] == hw_cpuid)
  90. return cpu_node;
  91. }
  92. } else {
  93. reg = (unsigned int *)get_property(cpu_node,
  94. "reg", &len);
  95. if (reg && (len > 0) && (reg[0] == hw_cpuid))
  96. return cpu_node;
  97. }
  98. }
  99. return NULL;
  100. }
  101. /* must hold reference to node during call */
  102. static int *of_get_associativity(struct device_node *dev)
  103. {
  104. return (unsigned int *)get_property(dev, "ibm,associativity", NULL);
  105. }
  106. static int of_node_numa_domain(struct device_node *device)
  107. {
  108. int numa_domain;
  109. unsigned int *tmp;
  110. if (min_common_depth == -1)
  111. return 0;
  112. tmp = of_get_associativity(device);
  113. if (tmp && (tmp[0] >= min_common_depth)) {
  114. numa_domain = tmp[min_common_depth];
  115. } else {
  116. dbg("WARNING: no NUMA information for %s\n",
  117. device->full_name);
  118. numa_domain = 0;
  119. }
  120. return numa_domain;
  121. }
  122. /*
  123. * In theory, the "ibm,associativity" property may contain multiple
  124. * associativity lists because a resource may be multiply connected
  125. * into the machine. This resource then has different associativity
  126. * characteristics relative to its multiple connections. We ignore
  127. * this for now. We also assume that all cpu and memory sets have
  128. * their distances represented at a common level. This won't be
  129. * true for heirarchical NUMA.
  130. *
  131. * In any case the ibm,associativity-reference-points should give
  132. * the correct depth for a normal NUMA system.
  133. *
  134. * - Dave Hansen <haveblue@us.ibm.com>
  135. */
  136. static int __init find_min_common_depth(void)
  137. {
  138. int depth;
  139. unsigned int *ref_points;
  140. struct device_node *rtas_root;
  141. unsigned int len;
  142. rtas_root = of_find_node_by_path("/rtas");
  143. if (!rtas_root)
  144. return -1;
  145. /*
  146. * this property is 2 32-bit integers, each representing a level of
  147. * depth in the associativity nodes. The first is for an SMP
  148. * configuration (should be all 0's) and the second is for a normal
  149. * NUMA configuration.
  150. */
  151. ref_points = (unsigned int *)get_property(rtas_root,
  152. "ibm,associativity-reference-points", &len);
  153. if ((len >= 1) && ref_points) {
  154. depth = ref_points[1];
  155. } else {
  156. dbg("WARNING: could not find NUMA "
  157. "associativity reference point\n");
  158. depth = -1;
  159. }
  160. of_node_put(rtas_root);
  161. return depth;
  162. }
  163. static int __init get_mem_addr_cells(void)
  164. {
  165. struct device_node *memory = NULL;
  166. int rc;
  167. memory = of_find_node_by_type(memory, "memory");
  168. if (!memory)
  169. return 0; /* it won't matter */
  170. rc = prom_n_addr_cells(memory);
  171. return rc;
  172. }
  173. static int __init get_mem_size_cells(void)
  174. {
  175. struct device_node *memory = NULL;
  176. int rc;
  177. memory = of_find_node_by_type(memory, "memory");
  178. if (!memory)
  179. return 0; /* it won't matter */
  180. rc = prom_n_size_cells(memory);
  181. return rc;
  182. }
  183. static unsigned long read_n_cells(int n, unsigned int **buf)
  184. {
  185. unsigned long result = 0;
  186. while (n--) {
  187. result = (result << 32) | **buf;
  188. (*buf)++;
  189. }
  190. return result;
  191. }
  192. /*
  193. * Figure out to which domain a cpu belongs and stick it there.
  194. * Return the id of the domain used.
  195. */
  196. static int numa_setup_cpu(unsigned long lcpu)
  197. {
  198. int numa_domain = 0;
  199. struct device_node *cpu = find_cpu_node(lcpu);
  200. if (!cpu) {
  201. WARN_ON(1);
  202. goto out;
  203. }
  204. numa_domain = of_node_numa_domain(cpu);
  205. if (numa_domain >= num_online_nodes()) {
  206. /*
  207. * POWER4 LPAR uses 0xffff as invalid node,
  208. * dont warn in this case.
  209. */
  210. if (numa_domain != 0xffff)
  211. printk(KERN_ERR "WARNING: cpu %ld "
  212. "maps to invalid NUMA node %d\n",
  213. lcpu, numa_domain);
  214. numa_domain = 0;
  215. }
  216. out:
  217. node_set_online(numa_domain);
  218. map_cpu_to_node(lcpu, numa_domain);
  219. of_node_put(cpu);
  220. return numa_domain;
  221. }
  222. static int cpu_numa_callback(struct notifier_block *nfb,
  223. unsigned long action,
  224. void *hcpu)
  225. {
  226. unsigned long lcpu = (unsigned long)hcpu;
  227. int ret = NOTIFY_DONE;
  228. switch (action) {
  229. case CPU_UP_PREPARE:
  230. if (min_common_depth == -1 || !numa_enabled)
  231. map_cpu_to_node(lcpu, 0);
  232. else
  233. numa_setup_cpu(lcpu);
  234. ret = NOTIFY_OK;
  235. break;
  236. #ifdef CONFIG_HOTPLUG_CPU
  237. case CPU_DEAD:
  238. case CPU_UP_CANCELED:
  239. unmap_cpu_from_node(lcpu);
  240. break;
  241. ret = NOTIFY_OK;
  242. #endif
  243. }
  244. return ret;
  245. }
  246. /*
  247. * Check and possibly modify a memory region to enforce the memory limit.
  248. *
  249. * Returns the size the region should have to enforce the memory limit.
  250. * This will either be the original value of size, a truncated value,
  251. * or zero. If the returned value of size is 0 the region should be
  252. * discarded as it lies wholy above the memory limit.
  253. */
  254. static unsigned long __init numa_enforce_memory_limit(unsigned long start, unsigned long size)
  255. {
  256. /*
  257. * We use lmb_end_of_DRAM() in here instead of memory_limit because
  258. * we've already adjusted it for the limit and it takes care of
  259. * having memory holes below the limit.
  260. */
  261. if (! memory_limit)
  262. return size;
  263. if (start + size <= lmb_end_of_DRAM())
  264. return size;
  265. if (start >= lmb_end_of_DRAM())
  266. return 0;
  267. return lmb_end_of_DRAM() - start;
  268. }
  269. static int __init parse_numa_properties(void)
  270. {
  271. struct device_node *cpu = NULL;
  272. struct device_node *memory = NULL;
  273. int addr_cells, size_cells;
  274. int max_domain = 0;
  275. long entries = lmb_end_of_DRAM() >> MEMORY_INCREMENT_SHIFT;
  276. unsigned long i;
  277. if (numa_enabled == 0) {
  278. printk(KERN_WARNING "NUMA disabled by user\n");
  279. return -1;
  280. }
  281. numa_memory_lookup_table =
  282. (char *)abs_to_virt(lmb_alloc(entries * sizeof(char), 1));
  283. memset(numa_memory_lookup_table, 0, entries * sizeof(char));
  284. for (i = 0; i < entries ; i++)
  285. numa_memory_lookup_table[i] = ARRAY_INITIALISER;
  286. min_common_depth = find_min_common_depth();
  287. dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
  288. if (min_common_depth < 0)
  289. return min_common_depth;
  290. max_domain = numa_setup_cpu(boot_cpuid);
  291. /*
  292. * Even though we connect cpus to numa domains later in SMP init,
  293. * we need to know the maximum node id now. This is because each
  294. * node id must have NODE_DATA etc backing it.
  295. * As a result of hotplug we could still have cpus appear later on
  296. * with larger node ids. In that case we force the cpu into node 0.
  297. */
  298. for_each_cpu(i) {
  299. int numa_domain;
  300. cpu = find_cpu_node(i);
  301. if (cpu) {
  302. numa_domain = of_node_numa_domain(cpu);
  303. of_node_put(cpu);
  304. if (numa_domain < MAX_NUMNODES &&
  305. max_domain < numa_domain)
  306. max_domain = numa_domain;
  307. }
  308. }
  309. addr_cells = get_mem_addr_cells();
  310. size_cells = get_mem_size_cells();
  311. memory = NULL;
  312. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  313. unsigned long start;
  314. unsigned long size;
  315. int numa_domain;
  316. int ranges;
  317. unsigned int *memcell_buf;
  318. unsigned int len;
  319. memcell_buf = (unsigned int *)get_property(memory, "reg", &len);
  320. if (!memcell_buf || len <= 0)
  321. continue;
  322. ranges = memory->n_addrs;
  323. new_range:
  324. /* these are order-sensitive, and modify the buffer pointer */
  325. start = read_n_cells(addr_cells, &memcell_buf);
  326. size = read_n_cells(size_cells, &memcell_buf);
  327. start = _ALIGN_DOWN(start, MEMORY_INCREMENT);
  328. size = _ALIGN_UP(size, MEMORY_INCREMENT);
  329. numa_domain = of_node_numa_domain(memory);
  330. if (numa_domain >= MAX_NUMNODES) {
  331. if (numa_domain != 0xffff)
  332. printk(KERN_ERR "WARNING: memory at %lx maps "
  333. "to invalid NUMA node %d\n", start,
  334. numa_domain);
  335. numa_domain = 0;
  336. }
  337. if (max_domain < numa_domain)
  338. max_domain = numa_domain;
  339. if (! (size = numa_enforce_memory_limit(start, size))) {
  340. if (--ranges)
  341. goto new_range;
  342. else
  343. continue;
  344. }
  345. /*
  346. * Initialize new node struct, or add to an existing one.
  347. */
  348. if (init_node_data[numa_domain].node_end_pfn) {
  349. if ((start / PAGE_SIZE) <
  350. init_node_data[numa_domain].node_start_pfn)
  351. init_node_data[numa_domain].node_start_pfn =
  352. start / PAGE_SIZE;
  353. if (((start / PAGE_SIZE) + (size / PAGE_SIZE)) >
  354. init_node_data[numa_domain].node_end_pfn)
  355. init_node_data[numa_domain].node_end_pfn =
  356. (start / PAGE_SIZE) +
  357. (size / PAGE_SIZE);
  358. init_node_data[numa_domain].node_present_pages +=
  359. size / PAGE_SIZE;
  360. } else {
  361. node_set_online(numa_domain);
  362. init_node_data[numa_domain].node_start_pfn =
  363. start / PAGE_SIZE;
  364. init_node_data[numa_domain].node_end_pfn =
  365. init_node_data[numa_domain].node_start_pfn +
  366. size / PAGE_SIZE;
  367. init_node_data[numa_domain].node_present_pages =
  368. size / PAGE_SIZE;
  369. }
  370. for (i = start ; i < (start+size); i += MEMORY_INCREMENT)
  371. numa_memory_lookup_table[i >> MEMORY_INCREMENT_SHIFT] =
  372. numa_domain;
  373. if (--ranges)
  374. goto new_range;
  375. }
  376. for (i = 0; i <= max_domain; i++)
  377. node_set_online(i);
  378. return 0;
  379. }
  380. static void __init setup_nonnuma(void)
  381. {
  382. unsigned long top_of_ram = lmb_end_of_DRAM();
  383. unsigned long total_ram = lmb_phys_mem_size();
  384. unsigned long i;
  385. printk(KERN_INFO "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  386. top_of_ram, total_ram);
  387. printk(KERN_INFO "Memory hole size: %ldMB\n",
  388. (top_of_ram - total_ram) >> 20);
  389. if (!numa_memory_lookup_table) {
  390. long entries = top_of_ram >> MEMORY_INCREMENT_SHIFT;
  391. numa_memory_lookup_table =
  392. (char *)abs_to_virt(lmb_alloc(entries * sizeof(char), 1));
  393. memset(numa_memory_lookup_table, 0, entries * sizeof(char));
  394. for (i = 0; i < entries ; i++)
  395. numa_memory_lookup_table[i] = ARRAY_INITIALISER;
  396. }
  397. map_cpu_to_node(boot_cpuid, 0);
  398. node_set_online(0);
  399. init_node_data[0].node_start_pfn = 0;
  400. init_node_data[0].node_end_pfn = lmb_end_of_DRAM() / PAGE_SIZE;
  401. init_node_data[0].node_present_pages = total_ram / PAGE_SIZE;
  402. for (i = 0 ; i < top_of_ram; i += MEMORY_INCREMENT)
  403. numa_memory_lookup_table[i >> MEMORY_INCREMENT_SHIFT] = 0;
  404. }
  405. static void __init dump_numa_topology(void)
  406. {
  407. unsigned int node;
  408. unsigned int count;
  409. if (min_common_depth == -1 || !numa_enabled)
  410. return;
  411. for_each_online_node(node) {
  412. unsigned long i;
  413. printk(KERN_INFO "Node %d Memory:", node);
  414. count = 0;
  415. for (i = 0; i < lmb_end_of_DRAM(); i += MEMORY_INCREMENT) {
  416. if (numa_memory_lookup_table[i >> MEMORY_INCREMENT_SHIFT] == node) {
  417. if (count == 0)
  418. printk(" 0x%lx", i);
  419. ++count;
  420. } else {
  421. if (count > 0)
  422. printk("-0x%lx", i);
  423. count = 0;
  424. }
  425. }
  426. if (count > 0)
  427. printk("-0x%lx", i);
  428. printk("\n");
  429. }
  430. return;
  431. }
  432. /*
  433. * Allocate some memory, satisfying the lmb or bootmem allocator where
  434. * required. nid is the preferred node and end is the physical address of
  435. * the highest address in the node.
  436. *
  437. * Returns the physical address of the memory.
  438. */
  439. static unsigned long careful_allocation(int nid, unsigned long size,
  440. unsigned long align, unsigned long end)
  441. {
  442. unsigned long ret = lmb_alloc_base(size, align, end);
  443. /* retry over all memory */
  444. if (!ret)
  445. ret = lmb_alloc_base(size, align, lmb_end_of_DRAM());
  446. if (!ret)
  447. panic("numa.c: cannot allocate %lu bytes on node %d",
  448. size, nid);
  449. /*
  450. * If the memory came from a previously allocated node, we must
  451. * retry with the bootmem allocator.
  452. */
  453. if (pa_to_nid(ret) < nid) {
  454. nid = pa_to_nid(ret);
  455. ret = (unsigned long)__alloc_bootmem_node(NODE_DATA(nid),
  456. size, align, 0);
  457. if (!ret)
  458. panic("numa.c: cannot allocate %lu bytes on node %d",
  459. size, nid);
  460. ret = virt_to_abs(ret);
  461. dbg("alloc_bootmem %lx %lx\n", ret, size);
  462. }
  463. return ret;
  464. }
  465. void __init do_init_bootmem(void)
  466. {
  467. int nid;
  468. int addr_cells, size_cells;
  469. struct device_node *memory = NULL;
  470. static struct notifier_block ppc64_numa_nb = {
  471. .notifier_call = cpu_numa_callback,
  472. .priority = 1 /* Must run before sched domains notifier. */
  473. };
  474. min_low_pfn = 0;
  475. max_low_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
  476. max_pfn = max_low_pfn;
  477. if (parse_numa_properties())
  478. setup_nonnuma();
  479. else
  480. dump_numa_topology();
  481. register_cpu_notifier(&ppc64_numa_nb);
  482. for_each_online_node(nid) {
  483. unsigned long start_paddr, end_paddr;
  484. int i;
  485. unsigned long bootmem_paddr;
  486. unsigned long bootmap_pages;
  487. start_paddr = init_node_data[nid].node_start_pfn * PAGE_SIZE;
  488. end_paddr = init_node_data[nid].node_end_pfn * PAGE_SIZE;
  489. /* Allocate the node structure node local if possible */
  490. NODE_DATA(nid) = (struct pglist_data *)careful_allocation(nid,
  491. sizeof(struct pglist_data),
  492. SMP_CACHE_BYTES, end_paddr);
  493. NODE_DATA(nid) = abs_to_virt(NODE_DATA(nid));
  494. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  495. dbg("node %d\n", nid);
  496. dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
  497. NODE_DATA(nid)->bdata = &plat_node_bdata[nid];
  498. NODE_DATA(nid)->node_start_pfn =
  499. init_node_data[nid].node_start_pfn;
  500. NODE_DATA(nid)->node_spanned_pages =
  501. end_paddr - start_paddr;
  502. if (NODE_DATA(nid)->node_spanned_pages == 0)
  503. continue;
  504. dbg("start_paddr = %lx\n", start_paddr);
  505. dbg("end_paddr = %lx\n", end_paddr);
  506. bootmap_pages = bootmem_bootmap_pages((end_paddr - start_paddr) >> PAGE_SHIFT);
  507. bootmem_paddr = careful_allocation(nid,
  508. bootmap_pages << PAGE_SHIFT,
  509. PAGE_SIZE, end_paddr);
  510. memset(abs_to_virt(bootmem_paddr), 0,
  511. bootmap_pages << PAGE_SHIFT);
  512. dbg("bootmap_paddr = %lx\n", bootmem_paddr);
  513. init_bootmem_node(NODE_DATA(nid), bootmem_paddr >> PAGE_SHIFT,
  514. start_paddr >> PAGE_SHIFT,
  515. end_paddr >> PAGE_SHIFT);
  516. /*
  517. * We need to do another scan of all memory sections to
  518. * associate memory with the correct node.
  519. */
  520. addr_cells = get_mem_addr_cells();
  521. size_cells = get_mem_size_cells();
  522. memory = NULL;
  523. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  524. unsigned long mem_start, mem_size;
  525. int numa_domain, ranges;
  526. unsigned int *memcell_buf;
  527. unsigned int len;
  528. memcell_buf = (unsigned int *)get_property(memory, "reg", &len);
  529. if (!memcell_buf || len <= 0)
  530. continue;
  531. ranges = memory->n_addrs; /* ranges in cell */
  532. new_range:
  533. mem_start = read_n_cells(addr_cells, &memcell_buf);
  534. mem_size = read_n_cells(size_cells, &memcell_buf);
  535. if (numa_enabled) {
  536. numa_domain = of_node_numa_domain(memory);
  537. if (numa_domain >= MAX_NUMNODES)
  538. numa_domain = 0;
  539. } else
  540. numa_domain = 0;
  541. if (numa_domain != nid)
  542. continue;
  543. mem_size = numa_enforce_memory_limit(mem_start, mem_size);
  544. if (mem_size) {
  545. dbg("free_bootmem %lx %lx\n", mem_start, mem_size);
  546. free_bootmem_node(NODE_DATA(nid), mem_start, mem_size);
  547. }
  548. if (--ranges) /* process all ranges in cell */
  549. goto new_range;
  550. }
  551. /*
  552. * Mark reserved regions on this node
  553. */
  554. for (i = 0; i < lmb.reserved.cnt; i++) {
  555. unsigned long physbase = lmb.reserved.region[i].base;
  556. unsigned long size = lmb.reserved.region[i].size;
  557. if (pa_to_nid(physbase) != nid &&
  558. pa_to_nid(physbase+size-1) != nid)
  559. continue;
  560. if (physbase < end_paddr &&
  561. (physbase+size) > start_paddr) {
  562. /* overlaps */
  563. if (physbase < start_paddr) {
  564. size -= start_paddr - physbase;
  565. physbase = start_paddr;
  566. }
  567. if (size > end_paddr - physbase)
  568. size = end_paddr - physbase;
  569. dbg("reserve_bootmem %lx %lx\n", physbase,
  570. size);
  571. reserve_bootmem_node(NODE_DATA(nid), physbase,
  572. size);
  573. }
  574. }
  575. /*
  576. * This loop may look famaliar, but we have to do it again
  577. * after marking our reserved memory to mark memory present
  578. * for sparsemem.
  579. */
  580. addr_cells = get_mem_addr_cells();
  581. size_cells = get_mem_size_cells();
  582. memory = NULL;
  583. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  584. unsigned long mem_start, mem_size;
  585. int numa_domain, ranges;
  586. unsigned int *memcell_buf;
  587. unsigned int len;
  588. memcell_buf = (unsigned int *)get_property(memory, "reg", &len);
  589. if (!memcell_buf || len <= 0)
  590. continue;
  591. ranges = memory->n_addrs; /* ranges in cell */
  592. new_range2:
  593. mem_start = read_n_cells(addr_cells, &memcell_buf);
  594. mem_size = read_n_cells(size_cells, &memcell_buf);
  595. if (numa_enabled) {
  596. numa_domain = of_node_numa_domain(memory);
  597. if (numa_domain >= MAX_NUMNODES)
  598. numa_domain = 0;
  599. } else
  600. numa_domain = 0;
  601. if (numa_domain != nid)
  602. continue;
  603. mem_size = numa_enforce_memory_limit(mem_start, mem_size);
  604. memory_present(numa_domain, mem_start >> PAGE_SHIFT,
  605. (mem_start + mem_size) >> PAGE_SHIFT);
  606. if (--ranges) /* process all ranges in cell */
  607. goto new_range2;
  608. }
  609. }
  610. }
  611. void __init paging_init(void)
  612. {
  613. unsigned long zones_size[MAX_NR_ZONES];
  614. unsigned long zholes_size[MAX_NR_ZONES];
  615. int nid;
  616. memset(zones_size, 0, sizeof(zones_size));
  617. memset(zholes_size, 0, sizeof(zholes_size));
  618. for_each_online_node(nid) {
  619. unsigned long start_pfn;
  620. unsigned long end_pfn;
  621. start_pfn = init_node_data[nid].node_start_pfn;
  622. end_pfn = init_node_data[nid].node_end_pfn;
  623. zones_size[ZONE_DMA] = end_pfn - start_pfn;
  624. zholes_size[ZONE_DMA] = zones_size[ZONE_DMA] -
  625. init_node_data[nid].node_present_pages;
  626. dbg("free_area_init node %d %lx %lx (hole: %lx)\n", nid,
  627. zones_size[ZONE_DMA], start_pfn, zholes_size[ZONE_DMA]);
  628. free_area_init_node(nid, NODE_DATA(nid), zones_size,
  629. start_pfn, zholes_size);
  630. }
  631. }
  632. static int __init early_numa(char *p)
  633. {
  634. if (!p)
  635. return 0;
  636. if (strstr(p, "off"))
  637. numa_enabled = 0;
  638. if (strstr(p, "debug"))
  639. numa_debug = 1;
  640. return 0;
  641. }
  642. early_param("numa", early_numa);