numa.c 28 KB

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