numa.c 41 KB

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