numa.c 24 KB

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