numa.c 44 KB

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