numa.c 25 KB

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