numa.c 29 KB

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