numa.c 24 KB

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