numa.c 39 KB

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