numa.c 40 KB

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