numa.c 30 KB

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