numa.c 38 KB

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