numa.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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 int min_common_depth;
  37. static int n_mem_addr_cells, n_mem_size_cells;
  38. static int __cpuinit fake_numa_create_new_node(unsigned long end_pfn,
  39. unsigned int *nid)
  40. {
  41. unsigned long long mem;
  42. char *p = cmdline;
  43. static unsigned int fake_nid;
  44. static unsigned long long curr_boundary;
  45. /*
  46. * Modify node id, iff we started creating NUMA nodes
  47. * We want to continue from where we left of the last time
  48. */
  49. if (fake_nid)
  50. *nid = fake_nid;
  51. /*
  52. * In case there are no more arguments to parse, the
  53. * node_id should be the same as the last fake node id
  54. * (we've handled this above).
  55. */
  56. if (!p)
  57. return 0;
  58. mem = memparse(p, &p);
  59. if (!mem)
  60. return 0;
  61. if (mem < curr_boundary)
  62. return 0;
  63. curr_boundary = mem;
  64. if ((end_pfn << PAGE_SHIFT) > mem) {
  65. /*
  66. * Skip commas and spaces
  67. */
  68. while (*p == ',' || *p == ' ' || *p == '\t')
  69. p++;
  70. cmdline = p;
  71. fake_nid++;
  72. *nid = fake_nid;
  73. dbg("created new fake_node with id %d\n", fake_nid);
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. /*
  79. * get_active_region_work_fn - A helper function for get_node_active_region
  80. * Returns datax set to the start_pfn and end_pfn if they contain
  81. * the initial value of datax->start_pfn between them
  82. * @start_pfn: start page(inclusive) of region to check
  83. * @end_pfn: end page(exclusive) of region to check
  84. * @datax: comes in with ->start_pfn set to value to search for and
  85. * goes out with active range if it contains it
  86. * Returns 1 if search value is in range else 0
  87. */
  88. static int __init get_active_region_work_fn(unsigned long start_pfn,
  89. unsigned long end_pfn, void *datax)
  90. {
  91. struct node_active_region *data;
  92. data = (struct node_active_region *)datax;
  93. if (start_pfn <= data->start_pfn && end_pfn > data->start_pfn) {
  94. data->start_pfn = start_pfn;
  95. data->end_pfn = end_pfn;
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. /*
  101. * get_node_active_region - Return active region containing start_pfn
  102. * @start_pfn: The page to return the region for.
  103. * @node_ar: Returned set to the active region containing start_pfn
  104. */
  105. static void __init get_node_active_region(unsigned long start_pfn,
  106. struct node_active_region *node_ar)
  107. {
  108. int nid = early_pfn_to_nid(start_pfn);
  109. node_ar->nid = nid;
  110. node_ar->start_pfn = start_pfn;
  111. work_with_active_regions(nid, get_active_region_work_fn, node_ar);
  112. }
  113. static void __cpuinit map_cpu_to_node(int cpu, int node)
  114. {
  115. numa_cpu_lookup_table[cpu] = node;
  116. dbg("adding cpu %d to node %d\n", cpu, node);
  117. if (!(cpu_isset(cpu, numa_cpumask_lookup_table[node])))
  118. cpu_set(cpu, numa_cpumask_lookup_table[node]);
  119. }
  120. #ifdef CONFIG_HOTPLUG_CPU
  121. static void unmap_cpu_from_node(unsigned long cpu)
  122. {
  123. int node = numa_cpu_lookup_table[cpu];
  124. dbg("removing cpu %lu from node %d\n", cpu, node);
  125. if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
  126. cpu_clear(cpu, numa_cpumask_lookup_table[node]);
  127. } else {
  128. printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
  129. cpu, node);
  130. }
  131. }
  132. #endif /* CONFIG_HOTPLUG_CPU */
  133. static struct device_node * __cpuinit find_cpu_node(unsigned int cpu)
  134. {
  135. unsigned int hw_cpuid = get_hard_smp_processor_id(cpu);
  136. struct device_node *cpu_node = NULL;
  137. const unsigned int *interrupt_server, *reg;
  138. int len;
  139. while ((cpu_node = of_find_node_by_type(cpu_node, "cpu")) != NULL) {
  140. /* Try interrupt server first */
  141. interrupt_server = of_get_property(cpu_node,
  142. "ibm,ppc-interrupt-server#s", &len);
  143. len = len / sizeof(u32);
  144. if (interrupt_server && (len > 0)) {
  145. while (len--) {
  146. if (interrupt_server[len] == hw_cpuid)
  147. return cpu_node;
  148. }
  149. } else {
  150. reg = of_get_property(cpu_node, "reg", &len);
  151. if (reg && (len > 0) && (reg[0] == hw_cpuid))
  152. return cpu_node;
  153. }
  154. }
  155. return NULL;
  156. }
  157. /* must hold reference to node during call */
  158. static const int *of_get_associativity(struct device_node *dev)
  159. {
  160. return of_get_property(dev, "ibm,associativity", NULL);
  161. }
  162. /*
  163. * Returns the property linux,drconf-usable-memory if
  164. * it exists (the property exists only in kexec/kdump kernels,
  165. * added by kexec-tools)
  166. */
  167. static const u32 *of_get_usable_memory(struct device_node *memory)
  168. {
  169. const u32 *prop;
  170. u32 len;
  171. prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
  172. if (!prop || len < sizeof(unsigned int))
  173. return 0;
  174. return prop;
  175. }
  176. /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
  177. * info is found.
  178. */
  179. static int of_node_to_nid_single(struct device_node *device)
  180. {
  181. int nid = -1;
  182. const unsigned int *tmp;
  183. if (min_common_depth == -1)
  184. goto out;
  185. tmp = of_get_associativity(device);
  186. if (!tmp)
  187. goto out;
  188. if (tmp[0] >= min_common_depth)
  189. nid = tmp[min_common_depth];
  190. /* POWER4 LPAR uses 0xffff as invalid node */
  191. if (nid == 0xffff || nid >= MAX_NUMNODES)
  192. nid = -1;
  193. out:
  194. return nid;
  195. }
  196. /* Walk the device tree upwards, looking for an associativity id */
  197. int of_node_to_nid(struct device_node *device)
  198. {
  199. struct device_node *tmp;
  200. int nid = -1;
  201. of_node_get(device);
  202. while (device) {
  203. nid = of_node_to_nid_single(device);
  204. if (nid != -1)
  205. break;
  206. tmp = device;
  207. device = of_get_parent(tmp);
  208. of_node_put(tmp);
  209. }
  210. of_node_put(device);
  211. return nid;
  212. }
  213. EXPORT_SYMBOL_GPL(of_node_to_nid);
  214. /*
  215. * In theory, the "ibm,associativity" property may contain multiple
  216. * associativity lists because a resource may be multiply connected
  217. * into the machine. This resource then has different associativity
  218. * characteristics relative to its multiple connections. We ignore
  219. * this for now. We also assume that all cpu and memory sets have
  220. * their distances represented at a common level. This won't be
  221. * true for hierarchical NUMA.
  222. *
  223. * In any case the ibm,associativity-reference-points should give
  224. * the correct depth for a normal NUMA system.
  225. *
  226. * - Dave Hansen <haveblue@us.ibm.com>
  227. */
  228. static int __init find_min_common_depth(void)
  229. {
  230. int depth;
  231. const unsigned int *ref_points;
  232. struct device_node *rtas_root;
  233. unsigned int len;
  234. rtas_root = of_find_node_by_path("/rtas");
  235. if (!rtas_root)
  236. return -1;
  237. /*
  238. * this property is 2 32-bit integers, each representing a level of
  239. * depth in the associativity nodes. The first is for an SMP
  240. * configuration (should be all 0's) and the second is for a normal
  241. * NUMA configuration.
  242. */
  243. ref_points = of_get_property(rtas_root,
  244. "ibm,associativity-reference-points", &len);
  245. if ((len >= 1) && ref_points) {
  246. depth = ref_points[1];
  247. } else {
  248. dbg("NUMA: ibm,associativity-reference-points not found.\n");
  249. depth = -1;
  250. }
  251. of_node_put(rtas_root);
  252. return depth;
  253. }
  254. static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
  255. {
  256. struct device_node *memory = NULL;
  257. memory = of_find_node_by_type(memory, "memory");
  258. if (!memory)
  259. panic("numa.c: No memory nodes found!");
  260. *n_addr_cells = of_n_addr_cells(memory);
  261. *n_size_cells = of_n_size_cells(memory);
  262. of_node_put(memory);
  263. }
  264. static unsigned long __devinit read_n_cells(int n, const unsigned int **buf)
  265. {
  266. unsigned long result = 0;
  267. while (n--) {
  268. result = (result << 32) | **buf;
  269. (*buf)++;
  270. }
  271. return result;
  272. }
  273. struct of_drconf_cell {
  274. u64 base_addr;
  275. u32 drc_index;
  276. u32 reserved;
  277. u32 aa_index;
  278. u32 flags;
  279. };
  280. #define DRCONF_MEM_ASSIGNED 0x00000008
  281. #define DRCONF_MEM_AI_INVALID 0x00000040
  282. #define DRCONF_MEM_RESERVED 0x00000080
  283. /*
  284. * Read the next lmb list entry from the ibm,dynamic-memory property
  285. * and return the information in the provided of_drconf_cell structure.
  286. */
  287. static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
  288. {
  289. const u32 *cp;
  290. drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
  291. cp = *cellp;
  292. drmem->drc_index = cp[0];
  293. drmem->reserved = cp[1];
  294. drmem->aa_index = cp[2];
  295. drmem->flags = cp[3];
  296. *cellp = cp + 4;
  297. }
  298. /*
  299. * Retreive and validate the ibm,dynamic-memory property of the device tree.
  300. *
  301. * The layout of the ibm,dynamic-memory property is a number N of lmb
  302. * list entries followed by N lmb list entries. Each lmb list entry
  303. * contains information as layed out in the of_drconf_cell struct above.
  304. */
  305. static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
  306. {
  307. const u32 *prop;
  308. u32 len, entries;
  309. prop = of_get_property(memory, "ibm,dynamic-memory", &len);
  310. if (!prop || len < sizeof(unsigned int))
  311. return 0;
  312. entries = *prop++;
  313. /* Now that we know the number of entries, revalidate the size
  314. * of the property read in to ensure we have everything
  315. */
  316. if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
  317. return 0;
  318. *dm = prop;
  319. return entries;
  320. }
  321. /*
  322. * Retreive and validate the ibm,lmb-size property for drconf memory
  323. * from the device tree.
  324. */
  325. static u64 of_get_lmb_size(struct device_node *memory)
  326. {
  327. const u32 *prop;
  328. u32 len;
  329. prop = of_get_property(memory, "ibm,lmb-size", &len);
  330. if (!prop || len < sizeof(unsigned int))
  331. return 0;
  332. return read_n_cells(n_mem_size_cells, &prop);
  333. }
  334. struct assoc_arrays {
  335. u32 n_arrays;
  336. u32 array_sz;
  337. const u32 *arrays;
  338. };
  339. /*
  340. * Retreive and validate the list of associativity arrays for drconf
  341. * memory from the ibm,associativity-lookup-arrays property of the
  342. * device tree..
  343. *
  344. * The layout of the ibm,associativity-lookup-arrays property is a number N
  345. * indicating the number of associativity arrays, followed by a number M
  346. * indicating the size of each associativity array, followed by a list
  347. * of N associativity arrays.
  348. */
  349. static int of_get_assoc_arrays(struct device_node *memory,
  350. struct assoc_arrays *aa)
  351. {
  352. const u32 *prop;
  353. u32 len;
  354. prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
  355. if (!prop || len < 2 * sizeof(unsigned int))
  356. return -1;
  357. aa->n_arrays = *prop++;
  358. aa->array_sz = *prop++;
  359. /* Now that we know the number of arrrays and size of each array,
  360. * revalidate the size of the property read in.
  361. */
  362. if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
  363. return -1;
  364. aa->arrays = prop;
  365. return 0;
  366. }
  367. /*
  368. * This is like of_node_to_nid_single() for memory represented in the
  369. * ibm,dynamic-reconfiguration-memory node.
  370. */
  371. static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
  372. struct assoc_arrays *aa)
  373. {
  374. int default_nid = 0;
  375. int nid = default_nid;
  376. int index;
  377. if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
  378. !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
  379. drmem->aa_index < aa->n_arrays) {
  380. index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
  381. nid = aa->arrays[index];
  382. if (nid == 0xffff || nid >= MAX_NUMNODES)
  383. nid = default_nid;
  384. }
  385. return nid;
  386. }
  387. /*
  388. * Figure out to which domain a cpu belongs and stick it there.
  389. * Return the id of the domain used.
  390. */
  391. static int __cpuinit numa_setup_cpu(unsigned long lcpu)
  392. {
  393. int nid = 0;
  394. struct device_node *cpu = find_cpu_node(lcpu);
  395. if (!cpu) {
  396. WARN_ON(1);
  397. goto out;
  398. }
  399. nid = of_node_to_nid_single(cpu);
  400. if (nid < 0 || !node_online(nid))
  401. nid = any_online_node(NODE_MASK_ALL);
  402. out:
  403. map_cpu_to_node(lcpu, nid);
  404. of_node_put(cpu);
  405. return nid;
  406. }
  407. static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
  408. unsigned long action,
  409. void *hcpu)
  410. {
  411. unsigned long lcpu = (unsigned long)hcpu;
  412. int ret = NOTIFY_DONE;
  413. switch (action) {
  414. case CPU_UP_PREPARE:
  415. case CPU_UP_PREPARE_FROZEN:
  416. numa_setup_cpu(lcpu);
  417. ret = NOTIFY_OK;
  418. break;
  419. #ifdef CONFIG_HOTPLUG_CPU
  420. case CPU_DEAD:
  421. case CPU_DEAD_FROZEN:
  422. case CPU_UP_CANCELED:
  423. case CPU_UP_CANCELED_FROZEN:
  424. unmap_cpu_from_node(lcpu);
  425. break;
  426. ret = NOTIFY_OK;
  427. #endif
  428. }
  429. return ret;
  430. }
  431. /*
  432. * Check and possibly modify a memory region to enforce the memory limit.
  433. *
  434. * Returns the size the region should have to enforce the memory limit.
  435. * This will either be the original value of size, a truncated value,
  436. * or zero. If the returned value of size is 0 the region should be
  437. * discarded as it lies wholy above the memory limit.
  438. */
  439. static unsigned long __init numa_enforce_memory_limit(unsigned long start,
  440. unsigned long size)
  441. {
  442. /*
  443. * We use lmb_end_of_DRAM() in here instead of memory_limit because
  444. * we've already adjusted it for the limit and it takes care of
  445. * having memory holes below the limit.
  446. */
  447. if (! memory_limit)
  448. return size;
  449. if (start + size <= lmb_end_of_DRAM())
  450. return size;
  451. if (start >= lmb_end_of_DRAM())
  452. return 0;
  453. return lmb_end_of_DRAM() - start;
  454. }
  455. /*
  456. * Reads the counter for a given entry in
  457. * linux,drconf-usable-memory property
  458. */
  459. static inline int __init read_usm_ranges(const u32 **usm)
  460. {
  461. /*
  462. * For each lmb in ibm,dynamic-memory a corresponding
  463. * entry in linux,drconf-usable-memory property contains
  464. * a counter followed by that many (base, size) duple.
  465. * read the counter from linux,drconf-usable-memory
  466. */
  467. return read_n_cells(n_mem_size_cells, usm);
  468. }
  469. /*
  470. * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
  471. * node. This assumes n_mem_{addr,size}_cells have been set.
  472. */
  473. static void __init parse_drconf_memory(struct device_node *memory)
  474. {
  475. const u32 *dm, *usm;
  476. unsigned int n, rc, ranges, is_kexec_kdump = 0;
  477. unsigned long lmb_size, base, size, sz;
  478. int nid;
  479. struct assoc_arrays aa;
  480. n = of_get_drconf_memory(memory, &dm);
  481. if (!n)
  482. return;
  483. lmb_size = of_get_lmb_size(memory);
  484. if (!lmb_size)
  485. return;
  486. rc = of_get_assoc_arrays(memory, &aa);
  487. if (rc)
  488. return;
  489. /* check if this is a kexec/kdump kernel */
  490. usm = of_get_usable_memory(memory);
  491. if (usm != NULL)
  492. is_kexec_kdump = 1;
  493. for (; n != 0; --n) {
  494. struct of_drconf_cell drmem;
  495. read_drconf_cell(&drmem, &dm);
  496. /* skip this block if the reserved bit is set in flags (0x80)
  497. or if the block is not assigned to this partition (0x8) */
  498. if ((drmem.flags & DRCONF_MEM_RESERVED)
  499. || !(drmem.flags & DRCONF_MEM_ASSIGNED))
  500. continue;
  501. base = drmem.base_addr;
  502. size = lmb_size;
  503. ranges = 1;
  504. if (is_kexec_kdump) {
  505. ranges = read_usm_ranges(&usm);
  506. if (!ranges) /* there are no (base, size) duple */
  507. continue;
  508. }
  509. do {
  510. if (is_kexec_kdump) {
  511. base = read_n_cells(n_mem_addr_cells, &usm);
  512. size = read_n_cells(n_mem_size_cells, &usm);
  513. }
  514. nid = of_drconf_to_nid_single(&drmem, &aa);
  515. fake_numa_create_new_node(
  516. ((base + size) >> PAGE_SHIFT),
  517. &nid);
  518. node_set_online(nid);
  519. sz = numa_enforce_memory_limit(base, size);
  520. if (sz)
  521. add_active_range(nid, base >> PAGE_SHIFT,
  522. (base >> PAGE_SHIFT)
  523. + (sz >> PAGE_SHIFT));
  524. } while (--ranges);
  525. }
  526. }
  527. static int __init parse_numa_properties(void)
  528. {
  529. struct device_node *cpu = NULL;
  530. struct device_node *memory = NULL;
  531. int default_nid = 0;
  532. unsigned long i;
  533. if (numa_enabled == 0) {
  534. printk(KERN_WARNING "NUMA disabled by user\n");
  535. return -1;
  536. }
  537. min_common_depth = find_min_common_depth();
  538. if (min_common_depth < 0)
  539. return min_common_depth;
  540. dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
  541. /*
  542. * Even though we connect cpus to numa domains later in SMP
  543. * init, we need to know the node ids now. This is because
  544. * each node to be onlined must have NODE_DATA etc backing it.
  545. */
  546. for_each_present_cpu(i) {
  547. int nid;
  548. cpu = find_cpu_node(i);
  549. BUG_ON(!cpu);
  550. nid = of_node_to_nid_single(cpu);
  551. of_node_put(cpu);
  552. /*
  553. * Don't fall back to default_nid yet -- we will plug
  554. * cpus into nodes once the memory scan has discovered
  555. * the topology.
  556. */
  557. if (nid < 0)
  558. continue;
  559. node_set_online(nid);
  560. }
  561. get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
  562. memory = NULL;
  563. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  564. unsigned long start;
  565. unsigned long size;
  566. int nid;
  567. int ranges;
  568. const unsigned int *memcell_buf;
  569. unsigned int len;
  570. memcell_buf = of_get_property(memory,
  571. "linux,usable-memory", &len);
  572. if (!memcell_buf || len <= 0)
  573. memcell_buf = of_get_property(memory, "reg", &len);
  574. if (!memcell_buf || len <= 0)
  575. continue;
  576. /* ranges in cell */
  577. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  578. new_range:
  579. /* these are order-sensitive, and modify the buffer pointer */
  580. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  581. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  582. /*
  583. * Assumption: either all memory nodes or none will
  584. * have associativity properties. If none, then
  585. * everything goes to default_nid.
  586. */
  587. nid = of_node_to_nid_single(memory);
  588. if (nid < 0)
  589. nid = default_nid;
  590. fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
  591. node_set_online(nid);
  592. if (!(size = numa_enforce_memory_limit(start, size))) {
  593. if (--ranges)
  594. goto new_range;
  595. else
  596. continue;
  597. }
  598. add_active_range(nid, start >> PAGE_SHIFT,
  599. (start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
  600. if (--ranges)
  601. goto new_range;
  602. }
  603. /*
  604. * Now do the same thing for each LMB listed in the ibm,dynamic-memory
  605. * property in the ibm,dynamic-reconfiguration-memory node.
  606. */
  607. memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  608. if (memory)
  609. parse_drconf_memory(memory);
  610. return 0;
  611. }
  612. static void __init setup_nonnuma(void)
  613. {
  614. unsigned long top_of_ram = lmb_end_of_DRAM();
  615. unsigned long total_ram = lmb_phys_mem_size();
  616. unsigned long start_pfn, end_pfn;
  617. unsigned int i, nid = 0;
  618. printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  619. top_of_ram, total_ram);
  620. printk(KERN_DEBUG "Memory hole size: %ldMB\n",
  621. (top_of_ram - total_ram) >> 20);
  622. for (i = 0; i < lmb.memory.cnt; ++i) {
  623. start_pfn = lmb.memory.region[i].base >> PAGE_SHIFT;
  624. end_pfn = start_pfn + lmb_size_pages(&lmb.memory, i);
  625. fake_numa_create_new_node(end_pfn, &nid);
  626. add_active_range(nid, start_pfn, end_pfn);
  627. node_set_online(nid);
  628. }
  629. }
  630. void __init dump_numa_cpu_topology(void)
  631. {
  632. unsigned int node;
  633. unsigned int cpu, count;
  634. if (min_common_depth == -1 || !numa_enabled)
  635. return;
  636. for_each_online_node(node) {
  637. printk(KERN_DEBUG "Node %d CPUs:", node);
  638. count = 0;
  639. /*
  640. * If we used a CPU iterator here we would miss printing
  641. * the holes in the cpumap.
  642. */
  643. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  644. if (cpu_isset(cpu, numa_cpumask_lookup_table[node])) {
  645. if (count == 0)
  646. printk(" %u", cpu);
  647. ++count;
  648. } else {
  649. if (count > 1)
  650. printk("-%u", cpu - 1);
  651. count = 0;
  652. }
  653. }
  654. if (count > 1)
  655. printk("-%u", NR_CPUS - 1);
  656. printk("\n");
  657. }
  658. }
  659. static void __init dump_numa_memory_topology(void)
  660. {
  661. unsigned int node;
  662. unsigned int count;
  663. if (min_common_depth == -1 || !numa_enabled)
  664. return;
  665. for_each_online_node(node) {
  666. unsigned long i;
  667. printk(KERN_DEBUG "Node %d Memory:", node);
  668. count = 0;
  669. for (i = 0; i < lmb_end_of_DRAM();
  670. i += (1 << SECTION_SIZE_BITS)) {
  671. if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
  672. if (count == 0)
  673. printk(" 0x%lx", i);
  674. ++count;
  675. } else {
  676. if (count > 0)
  677. printk("-0x%lx", i);
  678. count = 0;
  679. }
  680. }
  681. if (count > 0)
  682. printk("-0x%lx", i);
  683. printk("\n");
  684. }
  685. }
  686. /*
  687. * Allocate some memory, satisfying the lmb or bootmem allocator where
  688. * required. nid is the preferred node and end is the physical address of
  689. * the highest address in the node.
  690. *
  691. * Returns the physical address of the memory.
  692. */
  693. static void __init *careful_allocation(int nid, unsigned long size,
  694. unsigned long align,
  695. unsigned long end_pfn)
  696. {
  697. int new_nid;
  698. unsigned long ret = __lmb_alloc_base(size, align, end_pfn << PAGE_SHIFT);
  699. /* retry over all memory */
  700. if (!ret)
  701. ret = __lmb_alloc_base(size, align, lmb_end_of_DRAM());
  702. if (!ret)
  703. panic("numa.c: cannot allocate %lu bytes on node %d",
  704. size, nid);
  705. /*
  706. * If the memory came from a previously allocated node, we must
  707. * retry with the bootmem allocator.
  708. */
  709. new_nid = early_pfn_to_nid(ret >> PAGE_SHIFT);
  710. if (new_nid < nid) {
  711. ret = (unsigned long)__alloc_bootmem_node(NODE_DATA(new_nid),
  712. size, align, 0);
  713. if (!ret)
  714. panic("numa.c: cannot allocate %lu bytes on node %d",
  715. size, new_nid);
  716. ret = __pa(ret);
  717. dbg("alloc_bootmem %lx %lx\n", ret, size);
  718. }
  719. return (void *)ret;
  720. }
  721. static struct notifier_block __cpuinitdata ppc64_numa_nb = {
  722. .notifier_call = cpu_numa_callback,
  723. .priority = 1 /* Must run before sched domains notifier. */
  724. };
  725. void __init do_init_bootmem(void)
  726. {
  727. int nid;
  728. unsigned int i;
  729. min_low_pfn = 0;
  730. max_low_pfn = lmb_end_of_DRAM() >> PAGE_SHIFT;
  731. max_pfn = max_low_pfn;
  732. if (parse_numa_properties())
  733. setup_nonnuma();
  734. else
  735. dump_numa_memory_topology();
  736. register_cpu_notifier(&ppc64_numa_nb);
  737. cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
  738. (void *)(unsigned long)boot_cpuid);
  739. for_each_online_node(nid) {
  740. unsigned long start_pfn, end_pfn;
  741. unsigned long bootmem_paddr;
  742. unsigned long bootmap_pages;
  743. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  744. /* Allocate the node structure node local if possible */
  745. NODE_DATA(nid) = careful_allocation(nid,
  746. sizeof(struct pglist_data),
  747. SMP_CACHE_BYTES, end_pfn);
  748. NODE_DATA(nid) = __va(NODE_DATA(nid));
  749. memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
  750. dbg("node %d\n", nid);
  751. dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
  752. NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
  753. NODE_DATA(nid)->node_start_pfn = start_pfn;
  754. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  755. if (NODE_DATA(nid)->node_spanned_pages == 0)
  756. continue;
  757. dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
  758. dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
  759. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  760. bootmem_paddr = (unsigned long)careful_allocation(nid,
  761. bootmap_pages << PAGE_SHIFT,
  762. PAGE_SIZE, end_pfn);
  763. memset(__va(bootmem_paddr), 0, bootmap_pages << PAGE_SHIFT);
  764. dbg("bootmap_paddr = %lx\n", bootmem_paddr);
  765. init_bootmem_node(NODE_DATA(nid), bootmem_paddr >> PAGE_SHIFT,
  766. start_pfn, end_pfn);
  767. free_bootmem_with_active_regions(nid, end_pfn);
  768. }
  769. /* Mark reserved regions */
  770. for (i = 0; i < lmb.reserved.cnt; i++) {
  771. unsigned long physbase = lmb.reserved.region[i].base;
  772. unsigned long size = lmb.reserved.region[i].size;
  773. unsigned long start_pfn = physbase >> PAGE_SHIFT;
  774. unsigned long end_pfn = ((physbase + size) >> PAGE_SHIFT);
  775. struct node_active_region node_ar;
  776. get_node_active_region(start_pfn, &node_ar);
  777. while (start_pfn < end_pfn) {
  778. /*
  779. * if reserved region extends past active region
  780. * then trim size to active region
  781. */
  782. if (end_pfn > node_ar.end_pfn)
  783. size = (node_ar.end_pfn << PAGE_SHIFT)
  784. - (start_pfn << PAGE_SHIFT);
  785. dbg("reserve_bootmem %lx %lx nid=%d\n", physbase, size,
  786. node_ar.nid);
  787. reserve_bootmem_node(NODE_DATA(node_ar.nid), physbase,
  788. size, BOOTMEM_DEFAULT);
  789. /*
  790. * if reserved region is contained in the active region
  791. * then done.
  792. */
  793. if (end_pfn <= node_ar.end_pfn)
  794. break;
  795. /*
  796. * reserved region extends past the active region
  797. * get next active region that contains this
  798. * reserved region
  799. */
  800. start_pfn = node_ar.end_pfn;
  801. physbase = start_pfn << PAGE_SHIFT;
  802. get_node_active_region(start_pfn, &node_ar);
  803. }
  804. }
  805. for_each_online_node(nid)
  806. sparse_memory_present_with_active_regions(nid);
  807. }
  808. void __init paging_init(void)
  809. {
  810. unsigned long max_zone_pfns[MAX_NR_ZONES];
  811. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  812. max_zone_pfns[ZONE_DMA] = lmb_end_of_DRAM() >> PAGE_SHIFT;
  813. free_area_init_nodes(max_zone_pfns);
  814. }
  815. static int __init early_numa(char *p)
  816. {
  817. if (!p)
  818. return 0;
  819. if (strstr(p, "off"))
  820. numa_enabled = 0;
  821. if (strstr(p, "debug"))
  822. numa_debug = 1;
  823. p = strstr(p, "fake=");
  824. if (p)
  825. cmdline = p + strlen("fake=");
  826. return 0;
  827. }
  828. early_param("numa", early_numa);
  829. #ifdef CONFIG_MEMORY_HOTPLUG
  830. /*
  831. * Validate the node associated with the memory section we are
  832. * trying to add.
  833. */
  834. int valid_hot_add_scn(int *nid, unsigned long start, u32 lmb_size,
  835. unsigned long scn_addr)
  836. {
  837. nodemask_t nodes;
  838. if (*nid < 0 || !node_online(*nid))
  839. *nid = any_online_node(NODE_MASK_ALL);
  840. if ((scn_addr >= start) && (scn_addr < (start + lmb_size))) {
  841. nodes_setall(nodes);
  842. while (NODE_DATA(*nid)->node_spanned_pages == 0) {
  843. node_clear(*nid, nodes);
  844. *nid = any_online_node(nodes);
  845. }
  846. return 1;
  847. }
  848. return 0;
  849. }
  850. /*
  851. * Find the node associated with a hot added memory section represented
  852. * by the ibm,dynamic-reconfiguration-memory node.
  853. */
  854. static int hot_add_drconf_scn_to_nid(struct device_node *memory,
  855. unsigned long scn_addr)
  856. {
  857. const u32 *dm;
  858. unsigned int n, rc;
  859. unsigned long lmb_size;
  860. int default_nid = any_online_node(NODE_MASK_ALL);
  861. int nid;
  862. struct assoc_arrays aa;
  863. n = of_get_drconf_memory(memory, &dm);
  864. if (!n)
  865. return default_nid;;
  866. lmb_size = of_get_lmb_size(memory);
  867. if (!lmb_size)
  868. return default_nid;
  869. rc = of_get_assoc_arrays(memory, &aa);
  870. if (rc)
  871. return default_nid;
  872. for (; n != 0; --n) {
  873. struct of_drconf_cell drmem;
  874. read_drconf_cell(&drmem, &dm);
  875. /* skip this block if it is reserved or not assigned to
  876. * this partition */
  877. if ((drmem.flags & DRCONF_MEM_RESERVED)
  878. || !(drmem.flags & DRCONF_MEM_ASSIGNED))
  879. continue;
  880. nid = of_drconf_to_nid_single(&drmem, &aa);
  881. if (valid_hot_add_scn(&nid, drmem.base_addr, lmb_size,
  882. scn_addr))
  883. return nid;
  884. }
  885. BUG(); /* section address should be found above */
  886. return 0;
  887. }
  888. /*
  889. * Find the node associated with a hot added memory section. Section
  890. * corresponds to a SPARSEMEM section, not an LMB. It is assumed that
  891. * sections are fully contained within a single LMB.
  892. */
  893. int hot_add_scn_to_nid(unsigned long scn_addr)
  894. {
  895. struct device_node *memory = NULL;
  896. int nid;
  897. if (!numa_enabled || (min_common_depth < 0))
  898. return any_online_node(NODE_MASK_ALL);
  899. memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  900. if (memory) {
  901. nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
  902. of_node_put(memory);
  903. return nid;
  904. }
  905. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  906. unsigned long start, size;
  907. int ranges;
  908. const unsigned int *memcell_buf;
  909. unsigned int len;
  910. memcell_buf = of_get_property(memory, "reg", &len);
  911. if (!memcell_buf || len <= 0)
  912. continue;
  913. /* ranges in cell */
  914. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  915. ha_new_range:
  916. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  917. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  918. nid = of_node_to_nid_single(memory);
  919. if (valid_hot_add_scn(&nid, start, size, scn_addr)) {
  920. of_node_put(memory);
  921. return nid;
  922. }
  923. if (--ranges) /* process all ranges in cell */
  924. goto ha_new_range;
  925. }
  926. BUG(); /* section address should be found above */
  927. return 0;
  928. }
  929. #endif /* CONFIG_MEMORY_HOTPLUG */