numa.c 37 KB

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