numa.c 41 KB

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