numa.c 37 KB

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