numa.c 41 KB

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