numa.c 38 KB

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