numa.c 37 KB

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