numa.c 28 KB

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