numa.c 36 KB

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