numa.c 45 KB

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