numa.c 44 KB

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