numa.c 28 KB

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