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. static int numa_enabled = 1;
  24. static int numa_debug;
  25. #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
  26. #ifdef DEBUG_NUMA
  27. #define ARRAY_INITIALISER -1
  28. #else
  29. #define ARRAY_INITIALISER 0
  30. #endif
  31. int numa_cpu_lookup_table[NR_CPUS] = { [ 0 ... (NR_CPUS - 1)] =
  32. ARRAY_INITIALISER};
  33. char *numa_memory_lookup_table;
  34. cpumask_t numa_cpumask_lookup_table[MAX_NUMNODES];
  35. int nr_cpus_in_node[MAX_NUMNODES] = { [0 ... (MAX_NUMNODES -1)] = 0};
  36. struct pglist_data *node_data[MAX_NUMNODES];
  37. bootmem_data_t __initdata plat_node_bdata[MAX_NUMNODES];
  38. static int min_common_depth;
  39. /*
  40. * We need somewhere to store start/span for each node until we have
  41. * allocated the real node_data structures.
  42. */
  43. static struct {
  44. unsigned long node_start_pfn;
  45. unsigned long node_end_pfn;
  46. unsigned long node_present_pages;
  47. } init_node_data[MAX_NUMNODES] __initdata;
  48. EXPORT_SYMBOL(node_data);
  49. EXPORT_SYMBOL(numa_cpu_lookup_table);
  50. EXPORT_SYMBOL(numa_memory_lookup_table);
  51. EXPORT_SYMBOL(numa_cpumask_lookup_table);
  52. EXPORT_SYMBOL(nr_cpus_in_node);
  53. static inline void map_cpu_to_node(int cpu, int node)
  54. {
  55. numa_cpu_lookup_table[cpu] = node;
  56. if (!(cpu_isset(cpu, numa_cpumask_lookup_table[node]))) {
  57. cpu_set(cpu, numa_cpumask_lookup_table[node]);
  58. nr_cpus_in_node[node]++;
  59. }
  60. }
  61. #ifdef CONFIG_HOTPLUG_CPU
  62. static void unmap_cpu_from_node(unsigned long cpu)
  63. {
  64. int node = numa_cpu_lookup_table[cpu];
  65. dbg("removing cpu %lu from node %d\n", cpu, node);
  66. if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
  67. cpu_clear(cpu, numa_cpumask_lookup_table[node]);
  68. nr_cpus_in_node[node]--;
  69. } else {
  70. printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
  71. cpu, node);
  72. }
  73. }
  74. #endif /* CONFIG_HOTPLUG_CPU */
  75. static struct device_node * __devinit find_cpu_node(unsigned int cpu)
  76. {
  77. unsigned int hw_cpuid = get_hard_smp_processor_id(cpu);
  78. struct device_node *cpu_node = NULL;
  79. unsigned int *interrupt_server, *reg;
  80. int len;
  81. while ((cpu_node = of_find_node_by_type(cpu_node, "cpu")) != NULL) {
  82. /* Try interrupt server first */
  83. interrupt_server = (unsigned int *)get_property(cpu_node,
  84. "ibm,ppc-interrupt-server#s", &len);
  85. len = len / sizeof(u32);
  86. if (interrupt_server && (len > 0)) {
  87. while (len--) {
  88. if (interrupt_server[len] == hw_cpuid)
  89. return cpu_node;
  90. }
  91. } else {
  92. reg = (unsigned int *)get_property(cpu_node,
  93. "reg", &len);
  94. if (reg && (len > 0) && (reg[0] == hw_cpuid))
  95. return cpu_node;
  96. }
  97. }
  98. return NULL;
  99. }
  100. /* must hold reference to node during call */
  101. static int *of_get_associativity(struct device_node *dev)
  102. {
  103. return (unsigned int *)get_property(dev, "ibm,associativity", NULL);
  104. }
  105. static int of_node_numa_domain(struct device_node *device)
  106. {
  107. int numa_domain;
  108. unsigned int *tmp;
  109. if (min_common_depth == -1)
  110. return 0;
  111. tmp = of_get_associativity(device);
  112. if (tmp && (tmp[0] >= min_common_depth)) {
  113. numa_domain = tmp[min_common_depth];
  114. } else {
  115. dbg("WARNING: no NUMA information for %s\n",
  116. device->full_name);
  117. numa_domain = 0;
  118. }
  119. return numa_domain;
  120. }
  121. /*
  122. * In theory, the "ibm,associativity" property may contain multiple
  123. * associativity lists because a resource may be multiply connected
  124. * into the machine. This resource then has different associativity
  125. * characteristics relative to its multiple connections. We ignore
  126. * this for now. We also assume that all cpu and memory sets have
  127. * their distances represented at a common level. This won't be
  128. * true for heirarchical NUMA.
  129. *
  130. * In any case the ibm,associativity-reference-points should give
  131. * the correct depth for a normal NUMA system.
  132. *
  133. * - Dave Hansen <haveblue@us.ibm.com>
  134. */
  135. static int __init find_min_common_depth(void)
  136. {
  137. int depth;
  138. unsigned int *ref_points;
  139. struct device_node *rtas_root;
  140. unsigned int len;
  141. rtas_root = of_find_node_by_path("/rtas");
  142. if (!rtas_root)
  143. return -1;
  144. /*
  145. * this property is 2 32-bit integers, each representing a level of
  146. * depth in the associativity nodes. The first is for an SMP
  147. * configuration (should be all 0's) and the second is for a normal
  148. * NUMA configuration.
  149. */
  150. ref_points = (unsigned int *)get_property(rtas_root,
  151. "ibm,associativity-reference-points", &len);
  152. if ((len >= 1) && ref_points) {
  153. depth = ref_points[1];
  154. } else {
  155. dbg("WARNING: could not find NUMA "
  156. "associativity reference point\n");
  157. depth = -1;
  158. }
  159. of_node_put(rtas_root);
  160. return depth;
  161. }
  162. static int __init get_mem_addr_cells(void)
  163. {
  164. struct device_node *memory = NULL;
  165. int rc;
  166. memory = of_find_node_by_type(memory, "memory");
  167. if (!memory)
  168. return 0; /* it won't matter */
  169. rc = prom_n_addr_cells(memory);
  170. return rc;
  171. }
  172. static int __init get_mem_size_cells(void)
  173. {
  174. struct device_node *memory = NULL;
  175. int rc;
  176. memory = of_find_node_by_type(memory, "memory");
  177. if (!memory)
  178. return 0; /* it won't matter */
  179. rc = prom_n_size_cells(memory);
  180. return rc;
  181. }
  182. static unsigned long read_n_cells(int n, unsigned int **buf)
  183. {
  184. unsigned long result = 0;
  185. while (n--) {
  186. result = (result << 32) | **buf;
  187. (*buf)++;
  188. }
  189. return result;
  190. }
  191. /*
  192. * Figure out to which domain a cpu belongs and stick it there.
  193. * Return the id of the domain used.
  194. */
  195. static int numa_setup_cpu(unsigned long lcpu)
  196. {
  197. int numa_domain = 0;
  198. struct device_node *cpu = find_cpu_node(lcpu);
  199. if (!cpu) {
  200. WARN_ON(1);
  201. goto out;
  202. }
  203. numa_domain = of_node_numa_domain(cpu);
  204. if (numa_domain >= num_online_nodes()) {
  205. /*
  206. * POWER4 LPAR uses 0xffff as invalid node,
  207. * dont warn in this case.
  208. */
  209. if (numa_domain != 0xffff)
  210. printk(KERN_ERR "WARNING: cpu %ld "
  211. "maps to invalid NUMA node %d\n",
  212. lcpu, numa_domain);
  213. numa_domain = 0;
  214. }
  215. out:
  216. node_set_online(numa_domain);
  217. map_cpu_to_node(lcpu, numa_domain);
  218. of_node_put(cpu);
  219. return numa_domain;
  220. }
  221. static int cpu_numa_callback(struct notifier_block *nfb,
  222. unsigned long action,
  223. void *hcpu)
  224. {
  225. unsigned long lcpu = (unsigned long)hcpu;
  226. int ret = NOTIFY_DONE;
  227. switch (action) {
  228. case CPU_UP_PREPARE:
  229. if (min_common_depth == -1 || !numa_enabled)
  230. map_cpu_to_node(lcpu, 0);
  231. else
  232. numa_setup_cpu(lcpu);
  233. ret = NOTIFY_OK;
  234. break;
  235. #ifdef CONFIG_HOTPLUG_CPU
  236. case CPU_DEAD:
  237. case CPU_UP_CANCELED:
  238. unmap_cpu_from_node(lcpu);
  239. break;
  240. ret = NOTIFY_OK;
  241. #endif
  242. }
  243. return ret;
  244. }
  245. /*
  246. * Check and possibly modify a memory region to enforce the memory limit.
  247. *
  248. * Returns the size the region should have to enforce the memory limit.
  249. * This will either be the original value of size, a truncated value,
  250. * or zero. If the returned value of size is 0 the region should be
  251. * discarded as it lies wholy above the memory limit.
  252. */
  253. static unsigned long __init numa_enforce_memory_limit(unsigned long start, unsigned long size)
  254. {
  255. /*
  256. * We use lmb_end_of_DRAM() in here instead of memory_limit because
  257. * we've already adjusted it for the limit and it takes care of
  258. * having memory holes below the limit.
  259. */
  260. extern unsigned long memory_limit;
  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);