numa.c 37 KB

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