numa.c 28 KB

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