numa.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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 <linux/cpuset.h>
  24. #include <linux/node.h>
  25. #include <asm/sparsemem.h>
  26. #include <asm/prom.h>
  27. #include <asm/system.h>
  28. #include <asm/smp.h>
  29. #include <asm/firmware.h>
  30. #include <asm/paca.h>
  31. #include <asm/hvcall.h>
  32. static int numa_enabled = 1;
  33. static char *cmdline __initdata;
  34. static int numa_debug;
  35. #define dbg(args...) if (numa_debug) { printk(KERN_INFO args); }
  36. int numa_cpu_lookup_table[NR_CPUS];
  37. cpumask_var_t node_to_cpumask_map[MAX_NUMNODES];
  38. struct pglist_data *node_data[MAX_NUMNODES];
  39. EXPORT_SYMBOL(numa_cpu_lookup_table);
  40. EXPORT_SYMBOL(node_to_cpumask_map);
  41. EXPORT_SYMBOL(node_data);
  42. static int min_common_depth;
  43. static int n_mem_addr_cells, n_mem_size_cells;
  44. static int form1_affinity;
  45. #define MAX_DISTANCE_REF_POINTS 4
  46. static int distance_ref_points_depth;
  47. static const unsigned int *distance_ref_points;
  48. static int distance_lookup_table[MAX_NUMNODES][MAX_DISTANCE_REF_POINTS];
  49. /*
  50. * Allocate node_to_cpumask_map based on number of available nodes
  51. * Requires node_possible_map to be valid.
  52. *
  53. * Note: node_to_cpumask() is not valid until after this is done.
  54. */
  55. static void __init setup_node_to_cpumask_map(void)
  56. {
  57. unsigned int node, num = 0;
  58. /* setup nr_node_ids if not done yet */
  59. if (nr_node_ids == MAX_NUMNODES) {
  60. for_each_node_mask(node, node_possible_map)
  61. num = node;
  62. nr_node_ids = num + 1;
  63. }
  64. /* allocate the map */
  65. for (node = 0; node < nr_node_ids; node++)
  66. alloc_bootmem_cpumask_var(&node_to_cpumask_map[node]);
  67. /* cpumask_of_node() will now work */
  68. dbg("Node to cpumask map for %d nodes\n", nr_node_ids);
  69. }
  70. static int __cpuinit fake_numa_create_new_node(unsigned long end_pfn,
  71. unsigned int *nid)
  72. {
  73. unsigned long long mem;
  74. char *p = cmdline;
  75. static unsigned int fake_nid;
  76. static unsigned long long curr_boundary;
  77. /*
  78. * Modify node id, iff we started creating NUMA nodes
  79. * We want to continue from where we left of the last time
  80. */
  81. if (fake_nid)
  82. *nid = fake_nid;
  83. /*
  84. * In case there are no more arguments to parse, the
  85. * node_id should be the same as the last fake node id
  86. * (we've handled this above).
  87. */
  88. if (!p)
  89. return 0;
  90. mem = memparse(p, &p);
  91. if (!mem)
  92. return 0;
  93. if (mem < curr_boundary)
  94. return 0;
  95. curr_boundary = mem;
  96. if ((end_pfn << PAGE_SHIFT) > mem) {
  97. /*
  98. * Skip commas and spaces
  99. */
  100. while (*p == ',' || *p == ' ' || *p == '\t')
  101. p++;
  102. cmdline = p;
  103. fake_nid++;
  104. *nid = fake_nid;
  105. dbg("created new fake_node with id %d\n", fake_nid);
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. /*
  111. * get_node_active_region - Return active region containing pfn
  112. * Active range returned is empty if none found.
  113. * @pfn: The page to return the region for
  114. * @node_ar: Returned set to the active region containing @pfn
  115. */
  116. static void __init get_node_active_region(unsigned long pfn,
  117. struct node_active_region *node_ar)
  118. {
  119. unsigned long start_pfn, end_pfn;
  120. int i, nid;
  121. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
  122. if (pfn >= start_pfn && pfn < end_pfn) {
  123. node_ar->nid = nid;
  124. node_ar->start_pfn = start_pfn;
  125. node_ar->end_pfn = end_pfn;
  126. break;
  127. }
  128. }
  129. }
  130. static void map_cpu_to_node(int cpu, int node)
  131. {
  132. numa_cpu_lookup_table[cpu] = node;
  133. dbg("adding cpu %d to node %d\n", cpu, node);
  134. if (!(cpumask_test_cpu(cpu, node_to_cpumask_map[node])))
  135. cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
  136. }
  137. #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PPC_SPLPAR)
  138. static void unmap_cpu_from_node(unsigned long cpu)
  139. {
  140. int node = numa_cpu_lookup_table[cpu];
  141. dbg("removing cpu %lu from node %d\n", cpu, node);
  142. if (cpumask_test_cpu(cpu, node_to_cpumask_map[node])) {
  143. cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
  144. } else {
  145. printk(KERN_ERR "WARNING: cpu %lu not found in node %d\n",
  146. cpu, node);
  147. }
  148. }
  149. #endif /* CONFIG_HOTPLUG_CPU || CONFIG_PPC_SPLPAR */
  150. /* must hold reference to node during call */
  151. static const int *of_get_associativity(struct device_node *dev)
  152. {
  153. return of_get_property(dev, "ibm,associativity", NULL);
  154. }
  155. /*
  156. * Returns the property linux,drconf-usable-memory if
  157. * it exists (the property exists only in kexec/kdump kernels,
  158. * added by kexec-tools)
  159. */
  160. static const u32 *of_get_usable_memory(struct device_node *memory)
  161. {
  162. const u32 *prop;
  163. u32 len;
  164. prop = of_get_property(memory, "linux,drconf-usable-memory", &len);
  165. if (!prop || len < sizeof(unsigned int))
  166. return 0;
  167. return prop;
  168. }
  169. int __node_distance(int a, int b)
  170. {
  171. int i;
  172. int distance = LOCAL_DISTANCE;
  173. if (!form1_affinity)
  174. return distance;
  175. for (i = 0; i < distance_ref_points_depth; i++) {
  176. if (distance_lookup_table[a][i] == distance_lookup_table[b][i])
  177. break;
  178. /* Double the distance for each NUMA level */
  179. distance *= 2;
  180. }
  181. return distance;
  182. }
  183. static void initialize_distance_lookup_table(int nid,
  184. const unsigned int *associativity)
  185. {
  186. int i;
  187. if (!form1_affinity)
  188. return;
  189. for (i = 0; i < distance_ref_points_depth; i++) {
  190. distance_lookup_table[nid][i] =
  191. associativity[distance_ref_points[i]];
  192. }
  193. }
  194. /* Returns nid in the range [0..MAX_NUMNODES-1], or -1 if no useful numa
  195. * info is found.
  196. */
  197. static int associativity_to_nid(const unsigned int *associativity)
  198. {
  199. int nid = -1;
  200. if (min_common_depth == -1)
  201. goto out;
  202. if (associativity[0] >= min_common_depth)
  203. nid = associativity[min_common_depth];
  204. /* POWER4 LPAR uses 0xffff as invalid node */
  205. if (nid == 0xffff || nid >= MAX_NUMNODES)
  206. nid = -1;
  207. if (nid > 0 && associativity[0] >= distance_ref_points_depth)
  208. initialize_distance_lookup_table(nid, associativity);
  209. out:
  210. return nid;
  211. }
  212. /* Returns the nid associated with the given device tree node,
  213. * or -1 if not found.
  214. */
  215. static int of_node_to_nid_single(struct device_node *device)
  216. {
  217. int nid = -1;
  218. const unsigned int *tmp;
  219. tmp = of_get_associativity(device);
  220. if (tmp)
  221. nid = associativity_to_nid(tmp);
  222. return nid;
  223. }
  224. /* Walk the device tree upwards, looking for an associativity id */
  225. int of_node_to_nid(struct device_node *device)
  226. {
  227. struct device_node *tmp;
  228. int nid = -1;
  229. of_node_get(device);
  230. while (device) {
  231. nid = of_node_to_nid_single(device);
  232. if (nid != -1)
  233. break;
  234. tmp = device;
  235. device = of_get_parent(tmp);
  236. of_node_put(tmp);
  237. }
  238. of_node_put(device);
  239. return nid;
  240. }
  241. EXPORT_SYMBOL_GPL(of_node_to_nid);
  242. static int __init find_min_common_depth(void)
  243. {
  244. int depth;
  245. struct device_node *chosen;
  246. struct device_node *root;
  247. const char *vec5;
  248. root = of_find_node_by_path("/rtas");
  249. if (!root)
  250. root = of_find_node_by_path("/");
  251. /*
  252. * This property is a set of 32-bit integers, each representing
  253. * an index into the ibm,associativity nodes.
  254. *
  255. * With form 0 affinity the first integer is for an SMP configuration
  256. * (should be all 0's) and the second is for a normal NUMA
  257. * configuration. We have only one level of NUMA.
  258. *
  259. * With form 1 affinity the first integer is the most significant
  260. * NUMA boundary and the following are progressively less significant
  261. * boundaries. There can be more than one level of NUMA.
  262. */
  263. distance_ref_points = of_get_property(root,
  264. "ibm,associativity-reference-points",
  265. &distance_ref_points_depth);
  266. if (!distance_ref_points) {
  267. dbg("NUMA: ibm,associativity-reference-points not found.\n");
  268. goto err;
  269. }
  270. distance_ref_points_depth /= sizeof(int);
  271. #define VEC5_AFFINITY_BYTE 5
  272. #define VEC5_AFFINITY 0x80
  273. chosen = of_find_node_by_path("/chosen");
  274. if (chosen) {
  275. vec5 = of_get_property(chosen, "ibm,architecture-vec-5", NULL);
  276. if (vec5 && (vec5[VEC5_AFFINITY_BYTE] & VEC5_AFFINITY)) {
  277. dbg("Using form 1 affinity\n");
  278. form1_affinity = 1;
  279. }
  280. }
  281. if (form1_affinity) {
  282. depth = distance_ref_points[0];
  283. } else {
  284. if (distance_ref_points_depth < 2) {
  285. printk(KERN_WARNING "NUMA: "
  286. "short ibm,associativity-reference-points\n");
  287. goto err;
  288. }
  289. depth = distance_ref_points[1];
  290. }
  291. /*
  292. * Warn and cap if the hardware supports more than
  293. * MAX_DISTANCE_REF_POINTS domains.
  294. */
  295. if (distance_ref_points_depth > MAX_DISTANCE_REF_POINTS) {
  296. printk(KERN_WARNING "NUMA: distance array capped at "
  297. "%d entries\n", MAX_DISTANCE_REF_POINTS);
  298. distance_ref_points_depth = MAX_DISTANCE_REF_POINTS;
  299. }
  300. of_node_put(root);
  301. return depth;
  302. err:
  303. of_node_put(root);
  304. return -1;
  305. }
  306. static void __init get_n_mem_cells(int *n_addr_cells, int *n_size_cells)
  307. {
  308. struct device_node *memory = NULL;
  309. memory = of_find_node_by_type(memory, "memory");
  310. if (!memory)
  311. panic("numa.c: No memory nodes found!");
  312. *n_addr_cells = of_n_addr_cells(memory);
  313. *n_size_cells = of_n_size_cells(memory);
  314. of_node_put(memory);
  315. }
  316. static unsigned long __devinit read_n_cells(int n, const unsigned int **buf)
  317. {
  318. unsigned long result = 0;
  319. while (n--) {
  320. result = (result << 32) | **buf;
  321. (*buf)++;
  322. }
  323. return result;
  324. }
  325. struct of_drconf_cell {
  326. u64 base_addr;
  327. u32 drc_index;
  328. u32 reserved;
  329. u32 aa_index;
  330. u32 flags;
  331. };
  332. #define DRCONF_MEM_ASSIGNED 0x00000008
  333. #define DRCONF_MEM_AI_INVALID 0x00000040
  334. #define DRCONF_MEM_RESERVED 0x00000080
  335. /*
  336. * Read the next memblock list entry from the ibm,dynamic-memory property
  337. * and return the information in the provided of_drconf_cell structure.
  338. */
  339. static void read_drconf_cell(struct of_drconf_cell *drmem, const u32 **cellp)
  340. {
  341. const u32 *cp;
  342. drmem->base_addr = read_n_cells(n_mem_addr_cells, cellp);
  343. cp = *cellp;
  344. drmem->drc_index = cp[0];
  345. drmem->reserved = cp[1];
  346. drmem->aa_index = cp[2];
  347. drmem->flags = cp[3];
  348. *cellp = cp + 4;
  349. }
  350. /*
  351. * Retrieve and validate the ibm,dynamic-memory property of the device tree.
  352. *
  353. * The layout of the ibm,dynamic-memory property is a number N of memblock
  354. * list entries followed by N memblock list entries. Each memblock list entry
  355. * contains information as laid out in the of_drconf_cell struct above.
  356. */
  357. static int of_get_drconf_memory(struct device_node *memory, const u32 **dm)
  358. {
  359. const u32 *prop;
  360. u32 len, entries;
  361. prop = of_get_property(memory, "ibm,dynamic-memory", &len);
  362. if (!prop || len < sizeof(unsigned int))
  363. return 0;
  364. entries = *prop++;
  365. /* Now that we know the number of entries, revalidate the size
  366. * of the property read in to ensure we have everything
  367. */
  368. if (len < (entries * (n_mem_addr_cells + 4) + 1) * sizeof(unsigned int))
  369. return 0;
  370. *dm = prop;
  371. return entries;
  372. }
  373. /*
  374. * Retrieve and validate the ibm,lmb-size property for drconf memory
  375. * from the device tree.
  376. */
  377. static u64 of_get_lmb_size(struct device_node *memory)
  378. {
  379. const u32 *prop;
  380. u32 len;
  381. prop = of_get_property(memory, "ibm,lmb-size", &len);
  382. if (!prop || len < sizeof(unsigned int))
  383. return 0;
  384. return read_n_cells(n_mem_size_cells, &prop);
  385. }
  386. struct assoc_arrays {
  387. u32 n_arrays;
  388. u32 array_sz;
  389. const u32 *arrays;
  390. };
  391. /*
  392. * Retrieve and validate the list of associativity arrays for drconf
  393. * memory from the ibm,associativity-lookup-arrays property of the
  394. * device tree..
  395. *
  396. * The layout of the ibm,associativity-lookup-arrays property is a number N
  397. * indicating the number of associativity arrays, followed by a number M
  398. * indicating the size of each associativity array, followed by a list
  399. * of N associativity arrays.
  400. */
  401. static int of_get_assoc_arrays(struct device_node *memory,
  402. struct assoc_arrays *aa)
  403. {
  404. const u32 *prop;
  405. u32 len;
  406. prop = of_get_property(memory, "ibm,associativity-lookup-arrays", &len);
  407. if (!prop || len < 2 * sizeof(unsigned int))
  408. return -1;
  409. aa->n_arrays = *prop++;
  410. aa->array_sz = *prop++;
  411. /* Now that we know the number of arrrays and size of each array,
  412. * revalidate the size of the property read in.
  413. */
  414. if (len < (aa->n_arrays * aa->array_sz + 2) * sizeof(unsigned int))
  415. return -1;
  416. aa->arrays = prop;
  417. return 0;
  418. }
  419. /*
  420. * This is like of_node_to_nid_single() for memory represented in the
  421. * ibm,dynamic-reconfiguration-memory node.
  422. */
  423. static int of_drconf_to_nid_single(struct of_drconf_cell *drmem,
  424. struct assoc_arrays *aa)
  425. {
  426. int default_nid = 0;
  427. int nid = default_nid;
  428. int index;
  429. if (min_common_depth > 0 && min_common_depth <= aa->array_sz &&
  430. !(drmem->flags & DRCONF_MEM_AI_INVALID) &&
  431. drmem->aa_index < aa->n_arrays) {
  432. index = drmem->aa_index * aa->array_sz + min_common_depth - 1;
  433. nid = aa->arrays[index];
  434. if (nid == 0xffff || nid >= MAX_NUMNODES)
  435. nid = default_nid;
  436. }
  437. return nid;
  438. }
  439. /*
  440. * Figure out to which domain a cpu belongs and stick it there.
  441. * Return the id of the domain used.
  442. */
  443. static int __cpuinit numa_setup_cpu(unsigned long lcpu)
  444. {
  445. int nid = 0;
  446. struct device_node *cpu = of_get_cpu_node(lcpu, NULL);
  447. if (!cpu) {
  448. WARN_ON(1);
  449. goto out;
  450. }
  451. nid = of_node_to_nid_single(cpu);
  452. if (nid < 0 || !node_online(nid))
  453. nid = first_online_node;
  454. out:
  455. map_cpu_to_node(lcpu, nid);
  456. of_node_put(cpu);
  457. return nid;
  458. }
  459. static int __cpuinit cpu_numa_callback(struct notifier_block *nfb,
  460. unsigned long action,
  461. void *hcpu)
  462. {
  463. unsigned long lcpu = (unsigned long)hcpu;
  464. int ret = NOTIFY_DONE;
  465. switch (action) {
  466. case CPU_UP_PREPARE:
  467. case CPU_UP_PREPARE_FROZEN:
  468. numa_setup_cpu(lcpu);
  469. ret = NOTIFY_OK;
  470. break;
  471. #ifdef CONFIG_HOTPLUG_CPU
  472. case CPU_DEAD:
  473. case CPU_DEAD_FROZEN:
  474. case CPU_UP_CANCELED:
  475. case CPU_UP_CANCELED_FROZEN:
  476. unmap_cpu_from_node(lcpu);
  477. break;
  478. ret = NOTIFY_OK;
  479. #endif
  480. }
  481. return ret;
  482. }
  483. /*
  484. * Check and possibly modify a memory region to enforce the memory limit.
  485. *
  486. * Returns the size the region should have to enforce the memory limit.
  487. * This will either be the original value of size, a truncated value,
  488. * or zero. If the returned value of size is 0 the region should be
  489. * discarded as it lies wholly above the memory limit.
  490. */
  491. static unsigned long __init numa_enforce_memory_limit(unsigned long start,
  492. unsigned long size)
  493. {
  494. /*
  495. * We use memblock_end_of_DRAM() in here instead of memory_limit because
  496. * we've already adjusted it for the limit and it takes care of
  497. * having memory holes below the limit. Also, in the case of
  498. * iommu_is_off, memory_limit is not set but is implicitly enforced.
  499. */
  500. if (start + size <= memblock_end_of_DRAM())
  501. return size;
  502. if (start >= memblock_end_of_DRAM())
  503. return 0;
  504. return memblock_end_of_DRAM() - start;
  505. }
  506. /*
  507. * Reads the counter for a given entry in
  508. * linux,drconf-usable-memory property
  509. */
  510. static inline int __init read_usm_ranges(const u32 **usm)
  511. {
  512. /*
  513. * For each lmb in ibm,dynamic-memory a corresponding
  514. * entry in linux,drconf-usable-memory property contains
  515. * a counter followed by that many (base, size) duple.
  516. * read the counter from linux,drconf-usable-memory
  517. */
  518. return read_n_cells(n_mem_size_cells, usm);
  519. }
  520. /*
  521. * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
  522. * node. This assumes n_mem_{addr,size}_cells have been set.
  523. */
  524. static void __init parse_drconf_memory(struct device_node *memory)
  525. {
  526. const u32 *dm, *usm;
  527. unsigned int n, rc, ranges, is_kexec_kdump = 0;
  528. unsigned long lmb_size, base, size, sz;
  529. int nid;
  530. struct assoc_arrays aa;
  531. n = of_get_drconf_memory(memory, &dm);
  532. if (!n)
  533. return;
  534. lmb_size = of_get_lmb_size(memory);
  535. if (!lmb_size)
  536. return;
  537. rc = of_get_assoc_arrays(memory, &aa);
  538. if (rc)
  539. return;
  540. /* check if this is a kexec/kdump kernel */
  541. usm = of_get_usable_memory(memory);
  542. if (usm != NULL)
  543. is_kexec_kdump = 1;
  544. for (; n != 0; --n) {
  545. struct of_drconf_cell drmem;
  546. read_drconf_cell(&drmem, &dm);
  547. /* skip this block if the reserved bit is set in flags (0x80)
  548. or if the block is not assigned to this partition (0x8) */
  549. if ((drmem.flags & DRCONF_MEM_RESERVED)
  550. || !(drmem.flags & DRCONF_MEM_ASSIGNED))
  551. continue;
  552. base = drmem.base_addr;
  553. size = lmb_size;
  554. ranges = 1;
  555. if (is_kexec_kdump) {
  556. ranges = read_usm_ranges(&usm);
  557. if (!ranges) /* there are no (base, size) duple */
  558. continue;
  559. }
  560. do {
  561. if (is_kexec_kdump) {
  562. base = read_n_cells(n_mem_addr_cells, &usm);
  563. size = read_n_cells(n_mem_size_cells, &usm);
  564. }
  565. nid = of_drconf_to_nid_single(&drmem, &aa);
  566. fake_numa_create_new_node(
  567. ((base + size) >> PAGE_SHIFT),
  568. &nid);
  569. node_set_online(nid);
  570. sz = numa_enforce_memory_limit(base, size);
  571. if (sz)
  572. add_active_range(nid, base >> PAGE_SHIFT,
  573. (base >> PAGE_SHIFT)
  574. + (sz >> PAGE_SHIFT));
  575. } while (--ranges);
  576. }
  577. }
  578. static int __init parse_numa_properties(void)
  579. {
  580. struct device_node *cpu = NULL;
  581. struct device_node *memory = NULL;
  582. int default_nid = 0;
  583. unsigned long i;
  584. if (numa_enabled == 0) {
  585. printk(KERN_WARNING "NUMA disabled by user\n");
  586. return -1;
  587. }
  588. min_common_depth = find_min_common_depth();
  589. if (min_common_depth < 0)
  590. return min_common_depth;
  591. dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
  592. /*
  593. * Even though we connect cpus to numa domains later in SMP
  594. * init, we need to know the node ids now. This is because
  595. * each node to be onlined must have NODE_DATA etc backing it.
  596. */
  597. for_each_present_cpu(i) {
  598. int nid;
  599. cpu = of_get_cpu_node(i, NULL);
  600. BUG_ON(!cpu);
  601. nid = of_node_to_nid_single(cpu);
  602. of_node_put(cpu);
  603. /*
  604. * Don't fall back to default_nid yet -- we will plug
  605. * cpus into nodes once the memory scan has discovered
  606. * the topology.
  607. */
  608. if (nid < 0)
  609. continue;
  610. node_set_online(nid);
  611. }
  612. get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
  613. memory = NULL;
  614. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  615. unsigned long start;
  616. unsigned long size;
  617. int nid;
  618. int ranges;
  619. const unsigned int *memcell_buf;
  620. unsigned int len;
  621. memcell_buf = of_get_property(memory,
  622. "linux,usable-memory", &len);
  623. if (!memcell_buf || len <= 0)
  624. memcell_buf = of_get_property(memory, "reg", &len);
  625. if (!memcell_buf || len <= 0)
  626. continue;
  627. /* ranges in cell */
  628. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  629. new_range:
  630. /* these are order-sensitive, and modify the buffer pointer */
  631. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  632. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  633. /*
  634. * Assumption: either all memory nodes or none will
  635. * have associativity properties. If none, then
  636. * everything goes to default_nid.
  637. */
  638. nid = of_node_to_nid_single(memory);
  639. if (nid < 0)
  640. nid = default_nid;
  641. fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
  642. node_set_online(nid);
  643. if (!(size = numa_enforce_memory_limit(start, size))) {
  644. if (--ranges)
  645. goto new_range;
  646. else
  647. continue;
  648. }
  649. add_active_range(nid, start >> PAGE_SHIFT,
  650. (start >> PAGE_SHIFT) + (size >> PAGE_SHIFT));
  651. if (--ranges)
  652. goto new_range;
  653. }
  654. /*
  655. * Now do the same thing for each MEMBLOCK listed in the ibm,dynamic-memory
  656. * property in the ibm,dynamic-reconfiguration-memory node.
  657. */
  658. memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  659. if (memory)
  660. parse_drconf_memory(memory);
  661. return 0;
  662. }
  663. static void __init setup_nonnuma(void)
  664. {
  665. unsigned long top_of_ram = memblock_end_of_DRAM();
  666. unsigned long total_ram = memblock_phys_mem_size();
  667. unsigned long start_pfn, end_pfn;
  668. unsigned int nid = 0;
  669. struct memblock_region *reg;
  670. printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  671. top_of_ram, total_ram);
  672. printk(KERN_DEBUG "Memory hole size: %ldMB\n",
  673. (top_of_ram - total_ram) >> 20);
  674. for_each_memblock(memory, reg) {
  675. start_pfn = memblock_region_memory_base_pfn(reg);
  676. end_pfn = memblock_region_memory_end_pfn(reg);
  677. fake_numa_create_new_node(end_pfn, &nid);
  678. add_active_range(nid, start_pfn, end_pfn);
  679. node_set_online(nid);
  680. }
  681. }
  682. void __init dump_numa_cpu_topology(void)
  683. {
  684. unsigned int node;
  685. unsigned int cpu, count;
  686. if (min_common_depth == -1 || !numa_enabled)
  687. return;
  688. for_each_online_node(node) {
  689. printk(KERN_DEBUG "Node %d CPUs:", node);
  690. count = 0;
  691. /*
  692. * If we used a CPU iterator here we would miss printing
  693. * the holes in the cpumap.
  694. */
  695. for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
  696. if (cpumask_test_cpu(cpu,
  697. node_to_cpumask_map[node])) {
  698. if (count == 0)
  699. printk(" %u", cpu);
  700. ++count;
  701. } else {
  702. if (count > 1)
  703. printk("-%u", cpu - 1);
  704. count = 0;
  705. }
  706. }
  707. if (count > 1)
  708. printk("-%u", nr_cpu_ids - 1);
  709. printk("\n");
  710. }
  711. }
  712. static void __init dump_numa_memory_topology(void)
  713. {
  714. unsigned int node;
  715. unsigned int count;
  716. if (min_common_depth == -1 || !numa_enabled)
  717. return;
  718. for_each_online_node(node) {
  719. unsigned long i;
  720. printk(KERN_DEBUG "Node %d Memory:", node);
  721. count = 0;
  722. for (i = 0; i < memblock_end_of_DRAM();
  723. i += (1 << SECTION_SIZE_BITS)) {
  724. if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
  725. if (count == 0)
  726. printk(" 0x%lx", i);
  727. ++count;
  728. } else {
  729. if (count > 0)
  730. printk("-0x%lx", i);
  731. count = 0;
  732. }
  733. }
  734. if (count > 0)
  735. printk("-0x%lx", i);
  736. printk("\n");
  737. }
  738. }
  739. /*
  740. * Allocate some memory, satisfying the memblock or bootmem allocator where
  741. * required. nid is the preferred node and end is the physical address of
  742. * the highest address in the node.
  743. *
  744. * Returns the virtual address of the memory.
  745. */
  746. static void __init *careful_zallocation(int nid, unsigned long size,
  747. unsigned long align,
  748. unsigned long end_pfn)
  749. {
  750. void *ret;
  751. int new_nid;
  752. unsigned long ret_paddr;
  753. ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
  754. /* retry over all memory */
  755. if (!ret_paddr)
  756. ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
  757. if (!ret_paddr)
  758. panic("numa.c: cannot allocate %lu bytes for node %d",
  759. size, nid);
  760. ret = __va(ret_paddr);
  761. /*
  762. * We initialize the nodes in numeric order: 0, 1, 2...
  763. * and hand over control from the MEMBLOCK allocator to the
  764. * bootmem allocator. If this function is called for
  765. * node 5, then we know that all nodes <5 are using the
  766. * bootmem allocator instead of the MEMBLOCK allocator.
  767. *
  768. * So, check the nid from which this allocation came
  769. * and double check to see if we need to use bootmem
  770. * instead of the MEMBLOCK. We don't free the MEMBLOCK memory
  771. * since it would be useless.
  772. */
  773. new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
  774. if (new_nid < nid) {
  775. ret = __alloc_bootmem_node(NODE_DATA(new_nid),
  776. size, align, 0);
  777. dbg("alloc_bootmem %p %lx\n", ret, size);
  778. }
  779. memset(ret, 0, size);
  780. return ret;
  781. }
  782. static struct notifier_block __cpuinitdata ppc64_numa_nb = {
  783. .notifier_call = cpu_numa_callback,
  784. .priority = 1 /* Must run before sched domains notifier. */
  785. };
  786. static void mark_reserved_regions_for_nid(int nid)
  787. {
  788. struct pglist_data *node = NODE_DATA(nid);
  789. struct memblock_region *reg;
  790. for_each_memblock(reserved, reg) {
  791. unsigned long physbase = reg->base;
  792. unsigned long size = reg->size;
  793. unsigned long start_pfn = physbase >> PAGE_SHIFT;
  794. unsigned long end_pfn = PFN_UP(physbase + size);
  795. struct node_active_region node_ar;
  796. unsigned long node_end_pfn = node->node_start_pfn +
  797. node->node_spanned_pages;
  798. /*
  799. * Check to make sure that this memblock.reserved area is
  800. * within the bounds of the node that we care about.
  801. * Checking the nid of the start and end points is not
  802. * sufficient because the reserved area could span the
  803. * entire node.
  804. */
  805. if (end_pfn <= node->node_start_pfn ||
  806. start_pfn >= node_end_pfn)
  807. continue;
  808. get_node_active_region(start_pfn, &node_ar);
  809. while (start_pfn < end_pfn &&
  810. node_ar.start_pfn < node_ar.end_pfn) {
  811. unsigned long reserve_size = size;
  812. /*
  813. * if reserved region extends past active region
  814. * then trim size to active region
  815. */
  816. if (end_pfn > node_ar.end_pfn)
  817. reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
  818. - physbase;
  819. /*
  820. * Only worry about *this* node, others may not
  821. * yet have valid NODE_DATA().
  822. */
  823. if (node_ar.nid == nid) {
  824. dbg("reserve_bootmem %lx %lx nid=%d\n",
  825. physbase, reserve_size, node_ar.nid);
  826. reserve_bootmem_node(NODE_DATA(node_ar.nid),
  827. physbase, reserve_size,
  828. BOOTMEM_DEFAULT);
  829. }
  830. /*
  831. * if reserved region is contained in the active region
  832. * then done.
  833. */
  834. if (end_pfn <= node_ar.end_pfn)
  835. break;
  836. /*
  837. * reserved region extends past the active region
  838. * get next active region that contains this
  839. * reserved region
  840. */
  841. start_pfn = node_ar.end_pfn;
  842. physbase = start_pfn << PAGE_SHIFT;
  843. size = size - reserve_size;
  844. get_node_active_region(start_pfn, &node_ar);
  845. }
  846. }
  847. }
  848. void __init do_init_bootmem(void)
  849. {
  850. int nid;
  851. min_low_pfn = 0;
  852. max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  853. max_pfn = max_low_pfn;
  854. if (parse_numa_properties())
  855. setup_nonnuma();
  856. else
  857. dump_numa_memory_topology();
  858. for_each_online_node(nid) {
  859. unsigned long start_pfn, end_pfn;
  860. void *bootmem_vaddr;
  861. unsigned long bootmap_pages;
  862. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  863. /*
  864. * Allocate the node structure node local if possible
  865. *
  866. * Be careful moving this around, as it relies on all
  867. * previous nodes' bootmem to be initialized and have
  868. * all reserved areas marked.
  869. */
  870. NODE_DATA(nid) = careful_zallocation(nid,
  871. sizeof(struct pglist_data),
  872. SMP_CACHE_BYTES, end_pfn);
  873. dbg("node %d\n", nid);
  874. dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
  875. NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
  876. NODE_DATA(nid)->node_start_pfn = start_pfn;
  877. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  878. if (NODE_DATA(nid)->node_spanned_pages == 0)
  879. continue;
  880. dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
  881. dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
  882. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  883. bootmem_vaddr = careful_zallocation(nid,
  884. bootmap_pages << PAGE_SHIFT,
  885. PAGE_SIZE, end_pfn);
  886. dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
  887. init_bootmem_node(NODE_DATA(nid),
  888. __pa(bootmem_vaddr) >> PAGE_SHIFT,
  889. start_pfn, end_pfn);
  890. free_bootmem_with_active_regions(nid, end_pfn);
  891. /*
  892. * Be very careful about moving this around. Future
  893. * calls to careful_zallocation() depend on this getting
  894. * done correctly.
  895. */
  896. mark_reserved_regions_for_nid(nid);
  897. sparse_memory_present_with_active_regions(nid);
  898. }
  899. init_bootmem_done = 1;
  900. /*
  901. * Now bootmem is initialised we can create the node to cpumask
  902. * lookup tables and setup the cpu callback to populate them.
  903. */
  904. setup_node_to_cpumask_map();
  905. register_cpu_notifier(&ppc64_numa_nb);
  906. cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
  907. (void *)(unsigned long)boot_cpuid);
  908. }
  909. void __init paging_init(void)
  910. {
  911. unsigned long max_zone_pfns[MAX_NR_ZONES];
  912. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  913. max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
  914. free_area_init_nodes(max_zone_pfns);
  915. }
  916. static int __init early_numa(char *p)
  917. {
  918. if (!p)
  919. return 0;
  920. if (strstr(p, "off"))
  921. numa_enabled = 0;
  922. if (strstr(p, "debug"))
  923. numa_debug = 1;
  924. p = strstr(p, "fake=");
  925. if (p)
  926. cmdline = p + strlen("fake=");
  927. return 0;
  928. }
  929. early_param("numa", early_numa);
  930. #ifdef CONFIG_MEMORY_HOTPLUG
  931. /*
  932. * Find the node associated with a hot added memory section for
  933. * memory represented in the device tree by the property
  934. * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
  935. */
  936. static int hot_add_drconf_scn_to_nid(struct device_node *memory,
  937. unsigned long scn_addr)
  938. {
  939. const u32 *dm;
  940. unsigned int drconf_cell_cnt, rc;
  941. unsigned long lmb_size;
  942. struct assoc_arrays aa;
  943. int nid = -1;
  944. drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
  945. if (!drconf_cell_cnt)
  946. return -1;
  947. lmb_size = of_get_lmb_size(memory);
  948. if (!lmb_size)
  949. return -1;
  950. rc = of_get_assoc_arrays(memory, &aa);
  951. if (rc)
  952. return -1;
  953. for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
  954. struct of_drconf_cell drmem;
  955. read_drconf_cell(&drmem, &dm);
  956. /* skip this block if it is reserved or not assigned to
  957. * this partition */
  958. if ((drmem.flags & DRCONF_MEM_RESERVED)
  959. || !(drmem.flags & DRCONF_MEM_ASSIGNED))
  960. continue;
  961. if ((scn_addr < drmem.base_addr)
  962. || (scn_addr >= (drmem.base_addr + lmb_size)))
  963. continue;
  964. nid = of_drconf_to_nid_single(&drmem, &aa);
  965. break;
  966. }
  967. return nid;
  968. }
  969. /*
  970. * Find the node associated with a hot added memory section for memory
  971. * represented in the device tree as a node (i.e. memory@XXXX) for
  972. * each memblock.
  973. */
  974. int hot_add_node_scn_to_nid(unsigned long scn_addr)
  975. {
  976. struct device_node *memory = NULL;
  977. int nid = -1;
  978. while ((memory = of_find_node_by_type(memory, "memory")) != NULL) {
  979. unsigned long start, size;
  980. int ranges;
  981. const unsigned int *memcell_buf;
  982. unsigned int len;
  983. memcell_buf = of_get_property(memory, "reg", &len);
  984. if (!memcell_buf || len <= 0)
  985. continue;
  986. /* ranges in cell */
  987. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  988. while (ranges--) {
  989. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  990. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  991. if ((scn_addr < start) || (scn_addr >= (start + size)))
  992. continue;
  993. nid = of_node_to_nid_single(memory);
  994. break;
  995. }
  996. of_node_put(memory);
  997. if (nid >= 0)
  998. break;
  999. }
  1000. return nid;
  1001. }
  1002. /*
  1003. * Find the node associated with a hot added memory section. Section
  1004. * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
  1005. * sections are fully contained within a single MEMBLOCK.
  1006. */
  1007. int hot_add_scn_to_nid(unsigned long scn_addr)
  1008. {
  1009. struct device_node *memory = NULL;
  1010. int nid, found = 0;
  1011. if (!numa_enabled || (min_common_depth < 0))
  1012. return first_online_node;
  1013. memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  1014. if (memory) {
  1015. nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
  1016. of_node_put(memory);
  1017. } else {
  1018. nid = hot_add_node_scn_to_nid(scn_addr);
  1019. }
  1020. if (nid < 0 || !node_online(nid))
  1021. nid = first_online_node;
  1022. if (NODE_DATA(nid)->node_spanned_pages)
  1023. return nid;
  1024. for_each_online_node(nid) {
  1025. if (NODE_DATA(nid)->node_spanned_pages) {
  1026. found = 1;
  1027. break;
  1028. }
  1029. }
  1030. BUG_ON(!found);
  1031. return nid;
  1032. }
  1033. static u64 hot_add_drconf_memory_max(void)
  1034. {
  1035. struct device_node *memory = NULL;
  1036. unsigned int drconf_cell_cnt = 0;
  1037. u64 lmb_size = 0;
  1038. const u32 *dm = 0;
  1039. memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  1040. if (memory) {
  1041. drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
  1042. lmb_size = of_get_lmb_size(memory);
  1043. of_node_put(memory);
  1044. }
  1045. return lmb_size * drconf_cell_cnt;
  1046. }
  1047. /*
  1048. * memory_hotplug_max - return max address of memory that may be added
  1049. *
  1050. * This is currently only used on systems that support drconfig memory
  1051. * hotplug.
  1052. */
  1053. u64 memory_hotplug_max(void)
  1054. {
  1055. return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
  1056. }
  1057. #endif /* CONFIG_MEMORY_HOTPLUG */
  1058. /* Virtual Processor Home Node (VPHN) support */
  1059. #ifdef CONFIG_PPC_SPLPAR
  1060. static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
  1061. static cpumask_t cpu_associativity_changes_mask;
  1062. static int vphn_enabled;
  1063. static void set_topology_timer(void);
  1064. /*
  1065. * Store the current values of the associativity change counters in the
  1066. * hypervisor.
  1067. */
  1068. static void setup_cpu_associativity_change_counters(void)
  1069. {
  1070. int cpu;
  1071. /* The VPHN feature supports a maximum of 8 reference points */
  1072. BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
  1073. for_each_possible_cpu(cpu) {
  1074. int i;
  1075. u8 *counts = vphn_cpu_change_counts[cpu];
  1076. volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
  1077. for (i = 0; i < distance_ref_points_depth; i++)
  1078. counts[i] = hypervisor_counts[i];
  1079. }
  1080. }
  1081. /*
  1082. * The hypervisor maintains a set of 8 associativity change counters in
  1083. * the VPA of each cpu that correspond to the associativity levels in the
  1084. * ibm,associativity-reference-points property. When an associativity
  1085. * level changes, the corresponding counter is incremented.
  1086. *
  1087. * Set a bit in cpu_associativity_changes_mask for each cpu whose home
  1088. * node associativity levels have changed.
  1089. *
  1090. * Returns the number of cpus with unhandled associativity changes.
  1091. */
  1092. static int update_cpu_associativity_changes_mask(void)
  1093. {
  1094. int cpu, nr_cpus = 0;
  1095. cpumask_t *changes = &cpu_associativity_changes_mask;
  1096. cpumask_clear(changes);
  1097. for_each_possible_cpu(cpu) {
  1098. int i, changed = 0;
  1099. u8 *counts = vphn_cpu_change_counts[cpu];
  1100. volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
  1101. for (i = 0; i < distance_ref_points_depth; i++) {
  1102. if (hypervisor_counts[i] != counts[i]) {
  1103. counts[i] = hypervisor_counts[i];
  1104. changed = 1;
  1105. }
  1106. }
  1107. if (changed) {
  1108. cpumask_set_cpu(cpu, changes);
  1109. nr_cpus++;
  1110. }
  1111. }
  1112. return nr_cpus;
  1113. }
  1114. /*
  1115. * 6 64-bit registers unpacked into 12 32-bit associativity values. To form
  1116. * the complete property we have to add the length in the first cell.
  1117. */
  1118. #define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1)
  1119. /*
  1120. * Convert the associativity domain numbers returned from the hypervisor
  1121. * to the sequence they would appear in the ibm,associativity property.
  1122. */
  1123. static int vphn_unpack_associativity(const long *packed, unsigned int *unpacked)
  1124. {
  1125. int i, nr_assoc_doms = 0;
  1126. const u16 *field = (const u16*) packed;
  1127. #define VPHN_FIELD_UNUSED (0xffff)
  1128. #define VPHN_FIELD_MSB (0x8000)
  1129. #define VPHN_FIELD_MASK (~VPHN_FIELD_MSB)
  1130. for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
  1131. if (*field == VPHN_FIELD_UNUSED) {
  1132. /* All significant fields processed, and remaining
  1133. * fields contain the reserved value of all 1's.
  1134. * Just store them.
  1135. */
  1136. unpacked[i] = *((u32*)field);
  1137. field += 2;
  1138. } else if (*field & VPHN_FIELD_MSB) {
  1139. /* Data is in the lower 15 bits of this field */
  1140. unpacked[i] = *field & VPHN_FIELD_MASK;
  1141. field++;
  1142. nr_assoc_doms++;
  1143. } else {
  1144. /* Data is in the lower 15 bits of this field
  1145. * concatenated with the next 16 bit field
  1146. */
  1147. unpacked[i] = *((u32*)field);
  1148. field += 2;
  1149. nr_assoc_doms++;
  1150. }
  1151. }
  1152. /* The first cell contains the length of the property */
  1153. unpacked[0] = nr_assoc_doms;
  1154. return nr_assoc_doms;
  1155. }
  1156. /*
  1157. * Retrieve the new associativity information for a virtual processor's
  1158. * home node.
  1159. */
  1160. static long hcall_vphn(unsigned long cpu, unsigned int *associativity)
  1161. {
  1162. long rc;
  1163. long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
  1164. u64 flags = 1;
  1165. int hwcpu = get_hard_smp_processor_id(cpu);
  1166. rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
  1167. vphn_unpack_associativity(retbuf, associativity);
  1168. return rc;
  1169. }
  1170. static long vphn_get_associativity(unsigned long cpu,
  1171. unsigned int *associativity)
  1172. {
  1173. long rc;
  1174. rc = hcall_vphn(cpu, associativity);
  1175. switch (rc) {
  1176. case H_FUNCTION:
  1177. printk(KERN_INFO
  1178. "VPHN is not supported. Disabling polling...\n");
  1179. stop_topology_update();
  1180. break;
  1181. case H_HARDWARE:
  1182. printk(KERN_ERR
  1183. "hcall_vphn() experienced a hardware fault "
  1184. "preventing VPHN. Disabling polling...\n");
  1185. stop_topology_update();
  1186. }
  1187. return rc;
  1188. }
  1189. /*
  1190. * Update the node maps and sysfs entries for each cpu whose home node
  1191. * has changed.
  1192. */
  1193. int arch_update_cpu_topology(void)
  1194. {
  1195. int cpu, nid, old_nid;
  1196. unsigned int associativity[VPHN_ASSOC_BUFSIZE] = {0};
  1197. struct sys_device *sysdev;
  1198. for_each_cpu(cpu,&cpu_associativity_changes_mask) {
  1199. vphn_get_associativity(cpu, associativity);
  1200. nid = associativity_to_nid(associativity);
  1201. if (nid < 0 || !node_online(nid))
  1202. nid = first_online_node;
  1203. old_nid = numa_cpu_lookup_table[cpu];
  1204. /* Disable hotplug while we update the cpu
  1205. * masks and sysfs.
  1206. */
  1207. get_online_cpus();
  1208. unregister_cpu_under_node(cpu, old_nid);
  1209. unmap_cpu_from_node(cpu);
  1210. map_cpu_to_node(cpu, nid);
  1211. register_cpu_under_node(cpu, nid);
  1212. put_online_cpus();
  1213. sysdev = get_cpu_sysdev(cpu);
  1214. if (sysdev)
  1215. kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
  1216. }
  1217. return 1;
  1218. }
  1219. static void topology_work_fn(struct work_struct *work)
  1220. {
  1221. rebuild_sched_domains();
  1222. }
  1223. static DECLARE_WORK(topology_work, topology_work_fn);
  1224. void topology_schedule_update(void)
  1225. {
  1226. schedule_work(&topology_work);
  1227. }
  1228. static void topology_timer_fn(unsigned long ignored)
  1229. {
  1230. if (!vphn_enabled)
  1231. return;
  1232. if (update_cpu_associativity_changes_mask() > 0)
  1233. topology_schedule_update();
  1234. set_topology_timer();
  1235. }
  1236. static struct timer_list topology_timer =
  1237. TIMER_INITIALIZER(topology_timer_fn, 0, 0);
  1238. static void set_topology_timer(void)
  1239. {
  1240. topology_timer.data = 0;
  1241. topology_timer.expires = jiffies + 60 * HZ;
  1242. add_timer(&topology_timer);
  1243. }
  1244. /*
  1245. * Start polling for VPHN associativity changes.
  1246. */
  1247. int start_topology_update(void)
  1248. {
  1249. int rc = 0;
  1250. /* Disabled until races with load balancing are fixed */
  1251. if (0 && firmware_has_feature(FW_FEATURE_VPHN) &&
  1252. get_lppaca()->shared_proc) {
  1253. vphn_enabled = 1;
  1254. setup_cpu_associativity_change_counters();
  1255. init_timer_deferrable(&topology_timer);
  1256. set_topology_timer();
  1257. rc = 1;
  1258. }
  1259. return rc;
  1260. }
  1261. __initcall(start_topology_update);
  1262. /*
  1263. * Disable polling for VPHN associativity changes.
  1264. */
  1265. int stop_topology_update(void)
  1266. {
  1267. vphn_enabled = 0;
  1268. return del_timer_sync(&topology_timer);
  1269. }
  1270. #endif /* CONFIG_PPC_SPLPAR */