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;
  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. nid = 0;
  465. goto out;
  466. }
  467. nid = of_node_to_nid_single(cpu);
  468. if (nid < 0 || !node_online(nid))
  469. nid = first_online_node;
  470. out:
  471. map_cpu_to_node(lcpu, nid);
  472. of_node_put(cpu);
  473. return nid;
  474. }
  475. static void verify_cpu_node_mapping(int cpu, int node)
  476. {
  477. int base, sibling, i;
  478. /* Verify that all the threads in the core belong to the same node */
  479. base = cpu_first_thread_sibling(cpu);
  480. for (i = 0; i < threads_per_core; i++) {
  481. sibling = base + i;
  482. if (sibling == cpu || cpu_is_offline(sibling))
  483. continue;
  484. if (cpu_to_node(sibling) != node) {
  485. WARN(1, "CPU thread siblings %d and %d don't belong"
  486. " to the same node!\n", cpu, sibling);
  487. break;
  488. }
  489. }
  490. }
  491. static int cpu_numa_callback(struct notifier_block *nfb, unsigned long action,
  492. void *hcpu)
  493. {
  494. unsigned long lcpu = (unsigned long)hcpu;
  495. int ret = NOTIFY_DONE, nid;
  496. switch (action) {
  497. case CPU_UP_PREPARE:
  498. case CPU_UP_PREPARE_FROZEN:
  499. nid = numa_setup_cpu(lcpu);
  500. verify_cpu_node_mapping((int)lcpu, nid);
  501. ret = NOTIFY_OK;
  502. break;
  503. #ifdef CONFIG_HOTPLUG_CPU
  504. case CPU_DEAD:
  505. case CPU_DEAD_FROZEN:
  506. case CPU_UP_CANCELED:
  507. case CPU_UP_CANCELED_FROZEN:
  508. unmap_cpu_from_node(lcpu);
  509. break;
  510. ret = NOTIFY_OK;
  511. #endif
  512. }
  513. return ret;
  514. }
  515. /*
  516. * Check and possibly modify a memory region to enforce the memory limit.
  517. *
  518. * Returns the size the region should have to enforce the memory limit.
  519. * This will either be the original value of size, a truncated value,
  520. * or zero. If the returned value of size is 0 the region should be
  521. * discarded as it lies wholly above the memory limit.
  522. */
  523. static unsigned long __init numa_enforce_memory_limit(unsigned long start,
  524. unsigned long size)
  525. {
  526. /*
  527. * We use memblock_end_of_DRAM() in here instead of memory_limit because
  528. * we've already adjusted it for the limit and it takes care of
  529. * having memory holes below the limit. Also, in the case of
  530. * iommu_is_off, memory_limit is not set but is implicitly enforced.
  531. */
  532. if (start + size <= memblock_end_of_DRAM())
  533. return size;
  534. if (start >= memblock_end_of_DRAM())
  535. return 0;
  536. return memblock_end_of_DRAM() - start;
  537. }
  538. /*
  539. * Reads the counter for a given entry in
  540. * linux,drconf-usable-memory property
  541. */
  542. static inline int __init read_usm_ranges(const __be32 **usm)
  543. {
  544. /*
  545. * For each lmb in ibm,dynamic-memory a corresponding
  546. * entry in linux,drconf-usable-memory property contains
  547. * a counter followed by that many (base, size) duple.
  548. * read the counter from linux,drconf-usable-memory
  549. */
  550. return read_n_cells(n_mem_size_cells, usm);
  551. }
  552. /*
  553. * Extract NUMA information from the ibm,dynamic-reconfiguration-memory
  554. * node. This assumes n_mem_{addr,size}_cells have been set.
  555. */
  556. static void __init parse_drconf_memory(struct device_node *memory)
  557. {
  558. const __be32 *uninitialized_var(dm), *usm;
  559. unsigned int n, rc, ranges, is_kexec_kdump = 0;
  560. unsigned long lmb_size, base, size, sz;
  561. int nid;
  562. struct assoc_arrays aa = { .arrays = NULL };
  563. n = of_get_drconf_memory(memory, &dm);
  564. if (!n)
  565. return;
  566. lmb_size = of_get_lmb_size(memory);
  567. if (!lmb_size)
  568. return;
  569. rc = of_get_assoc_arrays(memory, &aa);
  570. if (rc)
  571. return;
  572. /* check if this is a kexec/kdump kernel */
  573. usm = of_get_usable_memory(memory);
  574. if (usm != NULL)
  575. is_kexec_kdump = 1;
  576. for (; n != 0; --n) {
  577. struct of_drconf_cell drmem;
  578. read_drconf_cell(&drmem, &dm);
  579. /* skip this block if the reserved bit is set in flags (0x80)
  580. or if the block is not assigned to this partition (0x8) */
  581. if ((drmem.flags & DRCONF_MEM_RESERVED)
  582. || !(drmem.flags & DRCONF_MEM_ASSIGNED))
  583. continue;
  584. base = drmem.base_addr;
  585. size = lmb_size;
  586. ranges = 1;
  587. if (is_kexec_kdump) {
  588. ranges = read_usm_ranges(&usm);
  589. if (!ranges) /* there are no (base, size) duple */
  590. continue;
  591. }
  592. do {
  593. if (is_kexec_kdump) {
  594. base = read_n_cells(n_mem_addr_cells, &usm);
  595. size = read_n_cells(n_mem_size_cells, &usm);
  596. }
  597. nid = of_drconf_to_nid_single(&drmem, &aa);
  598. fake_numa_create_new_node(
  599. ((base + size) >> PAGE_SHIFT),
  600. &nid);
  601. node_set_online(nid);
  602. sz = numa_enforce_memory_limit(base, size);
  603. if (sz)
  604. memblock_set_node(base, sz,
  605. &memblock.memory, nid);
  606. } while (--ranges);
  607. }
  608. }
  609. static int __init parse_numa_properties(void)
  610. {
  611. struct device_node *memory;
  612. int default_nid = 0;
  613. unsigned long i;
  614. if (numa_enabled == 0) {
  615. printk(KERN_WARNING "NUMA disabled by user\n");
  616. return -1;
  617. }
  618. min_common_depth = find_min_common_depth();
  619. if (min_common_depth < 0)
  620. return min_common_depth;
  621. dbg("NUMA associativity depth for CPU/Memory: %d\n", min_common_depth);
  622. /*
  623. * Even though we connect cpus to numa domains later in SMP
  624. * init, we need to know the node ids now. This is because
  625. * each node to be onlined must have NODE_DATA etc backing it.
  626. */
  627. for_each_present_cpu(i) {
  628. struct device_node *cpu;
  629. int nid;
  630. cpu = of_get_cpu_node(i, NULL);
  631. BUG_ON(!cpu);
  632. nid = of_node_to_nid_single(cpu);
  633. of_node_put(cpu);
  634. /*
  635. * Don't fall back to default_nid yet -- we will plug
  636. * cpus into nodes once the memory scan has discovered
  637. * the topology.
  638. */
  639. if (nid < 0)
  640. continue;
  641. node_set_online(nid);
  642. }
  643. get_n_mem_cells(&n_mem_addr_cells, &n_mem_size_cells);
  644. for_each_node_by_type(memory, "memory") {
  645. unsigned long start;
  646. unsigned long size;
  647. int nid;
  648. int ranges;
  649. const __be32 *memcell_buf;
  650. unsigned int len;
  651. memcell_buf = of_get_property(memory,
  652. "linux,usable-memory", &len);
  653. if (!memcell_buf || len <= 0)
  654. memcell_buf = of_get_property(memory, "reg", &len);
  655. if (!memcell_buf || len <= 0)
  656. continue;
  657. /* ranges in cell */
  658. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  659. new_range:
  660. /* these are order-sensitive, and modify the buffer pointer */
  661. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  662. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  663. /*
  664. * Assumption: either all memory nodes or none will
  665. * have associativity properties. If none, then
  666. * everything goes to default_nid.
  667. */
  668. nid = of_node_to_nid_single(memory);
  669. if (nid < 0)
  670. nid = default_nid;
  671. fake_numa_create_new_node(((start + size) >> PAGE_SHIFT), &nid);
  672. node_set_online(nid);
  673. if (!(size = numa_enforce_memory_limit(start, size))) {
  674. if (--ranges)
  675. goto new_range;
  676. else
  677. continue;
  678. }
  679. memblock_set_node(start, size, &memblock.memory, nid);
  680. if (--ranges)
  681. goto new_range;
  682. }
  683. /*
  684. * Now do the same thing for each MEMBLOCK listed in the
  685. * ibm,dynamic-memory property in the
  686. * ibm,dynamic-reconfiguration-memory node.
  687. */
  688. memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  689. if (memory)
  690. parse_drconf_memory(memory);
  691. return 0;
  692. }
  693. static void __init setup_nonnuma(void)
  694. {
  695. unsigned long top_of_ram = memblock_end_of_DRAM();
  696. unsigned long total_ram = memblock_phys_mem_size();
  697. unsigned long start_pfn, end_pfn;
  698. unsigned int nid = 0;
  699. struct memblock_region *reg;
  700. printk(KERN_DEBUG "Top of RAM: 0x%lx, Total RAM: 0x%lx\n",
  701. top_of_ram, total_ram);
  702. printk(KERN_DEBUG "Memory hole size: %ldMB\n",
  703. (top_of_ram - total_ram) >> 20);
  704. for_each_memblock(memory, reg) {
  705. start_pfn = memblock_region_memory_base_pfn(reg);
  706. end_pfn = memblock_region_memory_end_pfn(reg);
  707. fake_numa_create_new_node(end_pfn, &nid);
  708. memblock_set_node(PFN_PHYS(start_pfn),
  709. PFN_PHYS(end_pfn - start_pfn),
  710. &memblock.memory, nid);
  711. node_set_online(nid);
  712. }
  713. }
  714. void __init dump_numa_cpu_topology(void)
  715. {
  716. unsigned int node;
  717. unsigned int cpu, count;
  718. if (min_common_depth == -1 || !numa_enabled)
  719. return;
  720. for_each_online_node(node) {
  721. printk(KERN_DEBUG "Node %d CPUs:", node);
  722. count = 0;
  723. /*
  724. * If we used a CPU iterator here we would miss printing
  725. * the holes in the cpumap.
  726. */
  727. for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
  728. if (cpumask_test_cpu(cpu,
  729. node_to_cpumask_map[node])) {
  730. if (count == 0)
  731. printk(" %u", cpu);
  732. ++count;
  733. } else {
  734. if (count > 1)
  735. printk("-%u", cpu - 1);
  736. count = 0;
  737. }
  738. }
  739. if (count > 1)
  740. printk("-%u", nr_cpu_ids - 1);
  741. printk("\n");
  742. }
  743. }
  744. static void __init dump_numa_memory_topology(void)
  745. {
  746. unsigned int node;
  747. unsigned int count;
  748. if (min_common_depth == -1 || !numa_enabled)
  749. return;
  750. for_each_online_node(node) {
  751. unsigned long i;
  752. printk(KERN_DEBUG "Node %d Memory:", node);
  753. count = 0;
  754. for (i = 0; i < memblock_end_of_DRAM();
  755. i += (1 << SECTION_SIZE_BITS)) {
  756. if (early_pfn_to_nid(i >> PAGE_SHIFT) == node) {
  757. if (count == 0)
  758. printk(" 0x%lx", i);
  759. ++count;
  760. } else {
  761. if (count > 0)
  762. printk("-0x%lx", i);
  763. count = 0;
  764. }
  765. }
  766. if (count > 0)
  767. printk("-0x%lx", i);
  768. printk("\n");
  769. }
  770. }
  771. /*
  772. * Allocate some memory, satisfying the memblock or bootmem allocator where
  773. * required. nid is the preferred node and end is the physical address of
  774. * the highest address in the node.
  775. *
  776. * Returns the virtual address of the memory.
  777. */
  778. static void __init *careful_zallocation(int nid, unsigned long size,
  779. unsigned long align,
  780. unsigned long end_pfn)
  781. {
  782. void *ret;
  783. int new_nid;
  784. unsigned long ret_paddr;
  785. ret_paddr = __memblock_alloc_base(size, align, end_pfn << PAGE_SHIFT);
  786. /* retry over all memory */
  787. if (!ret_paddr)
  788. ret_paddr = __memblock_alloc_base(size, align, memblock_end_of_DRAM());
  789. if (!ret_paddr)
  790. panic("numa.c: cannot allocate %lu bytes for node %d",
  791. size, nid);
  792. ret = __va(ret_paddr);
  793. /*
  794. * We initialize the nodes in numeric order: 0, 1, 2...
  795. * and hand over control from the MEMBLOCK allocator to the
  796. * bootmem allocator. If this function is called for
  797. * node 5, then we know that all nodes <5 are using the
  798. * bootmem allocator instead of the MEMBLOCK allocator.
  799. *
  800. * So, check the nid from which this allocation came
  801. * and double check to see if we need to use bootmem
  802. * instead of the MEMBLOCK. We don't free the MEMBLOCK memory
  803. * since it would be useless.
  804. */
  805. new_nid = early_pfn_to_nid(ret_paddr >> PAGE_SHIFT);
  806. if (new_nid < nid) {
  807. ret = __alloc_bootmem_node(NODE_DATA(new_nid),
  808. size, align, 0);
  809. dbg("alloc_bootmem %p %lx\n", ret, size);
  810. }
  811. memset(ret, 0, size);
  812. return ret;
  813. }
  814. static struct notifier_block ppc64_numa_nb = {
  815. .notifier_call = cpu_numa_callback,
  816. .priority = 1 /* Must run before sched domains notifier. */
  817. };
  818. static void __init mark_reserved_regions_for_nid(int nid)
  819. {
  820. struct pglist_data *node = NODE_DATA(nid);
  821. struct memblock_region *reg;
  822. for_each_memblock(reserved, reg) {
  823. unsigned long physbase = reg->base;
  824. unsigned long size = reg->size;
  825. unsigned long start_pfn = physbase >> PAGE_SHIFT;
  826. unsigned long end_pfn = PFN_UP(physbase + size);
  827. struct node_active_region node_ar;
  828. unsigned long node_end_pfn = pgdat_end_pfn(node);
  829. /*
  830. * Check to make sure that this memblock.reserved area is
  831. * within the bounds of the node that we care about.
  832. * Checking the nid of the start and end points is not
  833. * sufficient because the reserved area could span the
  834. * entire node.
  835. */
  836. if (end_pfn <= node->node_start_pfn ||
  837. start_pfn >= node_end_pfn)
  838. continue;
  839. get_node_active_region(start_pfn, &node_ar);
  840. while (start_pfn < end_pfn &&
  841. node_ar.start_pfn < node_ar.end_pfn) {
  842. unsigned long reserve_size = size;
  843. /*
  844. * if reserved region extends past active region
  845. * then trim size to active region
  846. */
  847. if (end_pfn > node_ar.end_pfn)
  848. reserve_size = (node_ar.end_pfn << PAGE_SHIFT)
  849. - physbase;
  850. /*
  851. * Only worry about *this* node, others may not
  852. * yet have valid NODE_DATA().
  853. */
  854. if (node_ar.nid == nid) {
  855. dbg("reserve_bootmem %lx %lx nid=%d\n",
  856. physbase, reserve_size, node_ar.nid);
  857. reserve_bootmem_node(NODE_DATA(node_ar.nid),
  858. physbase, reserve_size,
  859. BOOTMEM_DEFAULT);
  860. }
  861. /*
  862. * if reserved region is contained in the active region
  863. * then done.
  864. */
  865. if (end_pfn <= node_ar.end_pfn)
  866. break;
  867. /*
  868. * reserved region extends past the active region
  869. * get next active region that contains this
  870. * reserved region
  871. */
  872. start_pfn = node_ar.end_pfn;
  873. physbase = start_pfn << PAGE_SHIFT;
  874. size = size - reserve_size;
  875. get_node_active_region(start_pfn, &node_ar);
  876. }
  877. }
  878. }
  879. void __init do_init_bootmem(void)
  880. {
  881. int nid;
  882. min_low_pfn = 0;
  883. max_low_pfn = memblock_end_of_DRAM() >> PAGE_SHIFT;
  884. max_pfn = max_low_pfn;
  885. if (parse_numa_properties())
  886. setup_nonnuma();
  887. else
  888. dump_numa_memory_topology();
  889. for_each_online_node(nid) {
  890. unsigned long start_pfn, end_pfn;
  891. void *bootmem_vaddr;
  892. unsigned long bootmap_pages;
  893. get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
  894. /*
  895. * Allocate the node structure node local if possible
  896. *
  897. * Be careful moving this around, as it relies on all
  898. * previous nodes' bootmem to be initialized and have
  899. * all reserved areas marked.
  900. */
  901. NODE_DATA(nid) = careful_zallocation(nid,
  902. sizeof(struct pglist_data),
  903. SMP_CACHE_BYTES, end_pfn);
  904. dbg("node %d\n", nid);
  905. dbg("NODE_DATA() = %p\n", NODE_DATA(nid));
  906. NODE_DATA(nid)->bdata = &bootmem_node_data[nid];
  907. NODE_DATA(nid)->node_start_pfn = start_pfn;
  908. NODE_DATA(nid)->node_spanned_pages = end_pfn - start_pfn;
  909. if (NODE_DATA(nid)->node_spanned_pages == 0)
  910. continue;
  911. dbg("start_paddr = %lx\n", start_pfn << PAGE_SHIFT);
  912. dbg("end_paddr = %lx\n", end_pfn << PAGE_SHIFT);
  913. bootmap_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
  914. bootmem_vaddr = careful_zallocation(nid,
  915. bootmap_pages << PAGE_SHIFT,
  916. PAGE_SIZE, end_pfn);
  917. dbg("bootmap_vaddr = %p\n", bootmem_vaddr);
  918. init_bootmem_node(NODE_DATA(nid),
  919. __pa(bootmem_vaddr) >> PAGE_SHIFT,
  920. start_pfn, end_pfn);
  921. free_bootmem_with_active_regions(nid, end_pfn);
  922. /*
  923. * Be very careful about moving this around. Future
  924. * calls to careful_zallocation() depend on this getting
  925. * done correctly.
  926. */
  927. mark_reserved_regions_for_nid(nid);
  928. sparse_memory_present_with_active_regions(nid);
  929. }
  930. init_bootmem_done = 1;
  931. /*
  932. * Now bootmem is initialised we can create the node to cpumask
  933. * lookup tables and setup the cpu callback to populate them.
  934. */
  935. setup_node_to_cpumask_map();
  936. reset_numa_cpu_lookup_table();
  937. register_cpu_notifier(&ppc64_numa_nb);
  938. cpu_numa_callback(&ppc64_numa_nb, CPU_UP_PREPARE,
  939. (void *)(unsigned long)boot_cpuid);
  940. }
  941. void __init paging_init(void)
  942. {
  943. unsigned long max_zone_pfns[MAX_NR_ZONES];
  944. memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
  945. max_zone_pfns[ZONE_DMA] = memblock_end_of_DRAM() >> PAGE_SHIFT;
  946. free_area_init_nodes(max_zone_pfns);
  947. }
  948. static int __init early_numa(char *p)
  949. {
  950. if (!p)
  951. return 0;
  952. if (strstr(p, "off"))
  953. numa_enabled = 0;
  954. if (strstr(p, "debug"))
  955. numa_debug = 1;
  956. p = strstr(p, "fake=");
  957. if (p)
  958. cmdline = p + strlen("fake=");
  959. return 0;
  960. }
  961. early_param("numa", early_numa);
  962. #ifdef CONFIG_MEMORY_HOTPLUG
  963. /*
  964. * Find the node associated with a hot added memory section for
  965. * memory represented in the device tree by the property
  966. * ibm,dynamic-reconfiguration-memory/ibm,dynamic-memory.
  967. */
  968. static int hot_add_drconf_scn_to_nid(struct device_node *memory,
  969. unsigned long scn_addr)
  970. {
  971. const __be32 *dm;
  972. unsigned int drconf_cell_cnt, rc;
  973. unsigned long lmb_size;
  974. struct assoc_arrays aa;
  975. int nid = -1;
  976. drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
  977. if (!drconf_cell_cnt)
  978. return -1;
  979. lmb_size = of_get_lmb_size(memory);
  980. if (!lmb_size)
  981. return -1;
  982. rc = of_get_assoc_arrays(memory, &aa);
  983. if (rc)
  984. return -1;
  985. for (; drconf_cell_cnt != 0; --drconf_cell_cnt) {
  986. struct of_drconf_cell drmem;
  987. read_drconf_cell(&drmem, &dm);
  988. /* skip this block if it is reserved or not assigned to
  989. * this partition */
  990. if ((drmem.flags & DRCONF_MEM_RESERVED)
  991. || !(drmem.flags & DRCONF_MEM_ASSIGNED))
  992. continue;
  993. if ((scn_addr < drmem.base_addr)
  994. || (scn_addr >= (drmem.base_addr + lmb_size)))
  995. continue;
  996. nid = of_drconf_to_nid_single(&drmem, &aa);
  997. break;
  998. }
  999. return nid;
  1000. }
  1001. /*
  1002. * Find the node associated with a hot added memory section for memory
  1003. * represented in the device tree as a node (i.e. memory@XXXX) for
  1004. * each memblock.
  1005. */
  1006. static int hot_add_node_scn_to_nid(unsigned long scn_addr)
  1007. {
  1008. struct device_node *memory;
  1009. int nid = -1;
  1010. for_each_node_by_type(memory, "memory") {
  1011. unsigned long start, size;
  1012. int ranges;
  1013. const __be32 *memcell_buf;
  1014. unsigned int len;
  1015. memcell_buf = of_get_property(memory, "reg", &len);
  1016. if (!memcell_buf || len <= 0)
  1017. continue;
  1018. /* ranges in cell */
  1019. ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
  1020. while (ranges--) {
  1021. start = read_n_cells(n_mem_addr_cells, &memcell_buf);
  1022. size = read_n_cells(n_mem_size_cells, &memcell_buf);
  1023. if ((scn_addr < start) || (scn_addr >= (start + size)))
  1024. continue;
  1025. nid = of_node_to_nid_single(memory);
  1026. break;
  1027. }
  1028. if (nid >= 0)
  1029. break;
  1030. }
  1031. of_node_put(memory);
  1032. return nid;
  1033. }
  1034. /*
  1035. * Find the node associated with a hot added memory section. Section
  1036. * corresponds to a SPARSEMEM section, not an MEMBLOCK. It is assumed that
  1037. * sections are fully contained within a single MEMBLOCK.
  1038. */
  1039. int hot_add_scn_to_nid(unsigned long scn_addr)
  1040. {
  1041. struct device_node *memory = NULL;
  1042. int nid, found = 0;
  1043. if (!numa_enabled || (min_common_depth < 0))
  1044. return first_online_node;
  1045. memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  1046. if (memory) {
  1047. nid = hot_add_drconf_scn_to_nid(memory, scn_addr);
  1048. of_node_put(memory);
  1049. } else {
  1050. nid = hot_add_node_scn_to_nid(scn_addr);
  1051. }
  1052. if (nid < 0 || !node_online(nid))
  1053. nid = first_online_node;
  1054. if (NODE_DATA(nid)->node_spanned_pages)
  1055. return nid;
  1056. for_each_online_node(nid) {
  1057. if (NODE_DATA(nid)->node_spanned_pages) {
  1058. found = 1;
  1059. break;
  1060. }
  1061. }
  1062. BUG_ON(!found);
  1063. return nid;
  1064. }
  1065. static u64 hot_add_drconf_memory_max(void)
  1066. {
  1067. struct device_node *memory = NULL;
  1068. unsigned int drconf_cell_cnt = 0;
  1069. u64 lmb_size = 0;
  1070. const __be32 *dm = NULL;
  1071. memory = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
  1072. if (memory) {
  1073. drconf_cell_cnt = of_get_drconf_memory(memory, &dm);
  1074. lmb_size = of_get_lmb_size(memory);
  1075. of_node_put(memory);
  1076. }
  1077. return lmb_size * drconf_cell_cnt;
  1078. }
  1079. /*
  1080. * memory_hotplug_max - return max address of memory that may be added
  1081. *
  1082. * This is currently only used on systems that support drconfig memory
  1083. * hotplug.
  1084. */
  1085. u64 memory_hotplug_max(void)
  1086. {
  1087. return max(hot_add_drconf_memory_max(), memblock_end_of_DRAM());
  1088. }
  1089. #endif /* CONFIG_MEMORY_HOTPLUG */
  1090. /* Virtual Processor Home Node (VPHN) support */
  1091. #ifdef CONFIG_PPC_SPLPAR
  1092. struct topology_update_data {
  1093. struct topology_update_data *next;
  1094. unsigned int cpu;
  1095. int old_nid;
  1096. int new_nid;
  1097. };
  1098. static u8 vphn_cpu_change_counts[NR_CPUS][MAX_DISTANCE_REF_POINTS];
  1099. static cpumask_t cpu_associativity_changes_mask;
  1100. static int vphn_enabled;
  1101. static int prrn_enabled;
  1102. static void reset_topology_timer(void);
  1103. /*
  1104. * Store the current values of the associativity change counters in the
  1105. * hypervisor.
  1106. */
  1107. static void setup_cpu_associativity_change_counters(void)
  1108. {
  1109. int cpu;
  1110. /* The VPHN feature supports a maximum of 8 reference points */
  1111. BUILD_BUG_ON(MAX_DISTANCE_REF_POINTS > 8);
  1112. for_each_possible_cpu(cpu) {
  1113. int i;
  1114. u8 *counts = vphn_cpu_change_counts[cpu];
  1115. volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
  1116. for (i = 0; i < distance_ref_points_depth; i++)
  1117. counts[i] = hypervisor_counts[i];
  1118. }
  1119. }
  1120. /*
  1121. * The hypervisor maintains a set of 8 associativity change counters in
  1122. * the VPA of each cpu that correspond to the associativity levels in the
  1123. * ibm,associativity-reference-points property. When an associativity
  1124. * level changes, the corresponding counter is incremented.
  1125. *
  1126. * Set a bit in cpu_associativity_changes_mask for each cpu whose home
  1127. * node associativity levels have changed.
  1128. *
  1129. * Returns the number of cpus with unhandled associativity changes.
  1130. */
  1131. static int update_cpu_associativity_changes_mask(void)
  1132. {
  1133. int cpu;
  1134. cpumask_t *changes = &cpu_associativity_changes_mask;
  1135. for_each_possible_cpu(cpu) {
  1136. int i, changed = 0;
  1137. u8 *counts = vphn_cpu_change_counts[cpu];
  1138. volatile u8 *hypervisor_counts = lppaca[cpu].vphn_assoc_counts;
  1139. for (i = 0; i < distance_ref_points_depth; i++) {
  1140. if (hypervisor_counts[i] != counts[i]) {
  1141. counts[i] = hypervisor_counts[i];
  1142. changed = 1;
  1143. }
  1144. }
  1145. if (changed) {
  1146. cpumask_or(changes, changes, cpu_sibling_mask(cpu));
  1147. cpu = cpu_last_thread_sibling(cpu);
  1148. }
  1149. }
  1150. return cpumask_weight(changes);
  1151. }
  1152. /*
  1153. * 6 64-bit registers unpacked into 12 32-bit associativity values. To form
  1154. * the complete property we have to add the length in the first cell.
  1155. */
  1156. #define VPHN_ASSOC_BUFSIZE (6*sizeof(u64)/sizeof(u32) + 1)
  1157. /*
  1158. * Convert the associativity domain numbers returned from the hypervisor
  1159. * to the sequence they would appear in the ibm,associativity property.
  1160. */
  1161. static int vphn_unpack_associativity(const long *packed, __be32 *unpacked)
  1162. {
  1163. int i, nr_assoc_doms = 0;
  1164. const __be16 *field = (const __be16 *) packed;
  1165. #define VPHN_FIELD_UNUSED (0xffff)
  1166. #define VPHN_FIELD_MSB (0x8000)
  1167. #define VPHN_FIELD_MASK (~VPHN_FIELD_MSB)
  1168. for (i = 1; i < VPHN_ASSOC_BUFSIZE; i++) {
  1169. if (be16_to_cpup(field) == VPHN_FIELD_UNUSED) {
  1170. /* All significant fields processed, and remaining
  1171. * fields contain the reserved value of all 1's.
  1172. * Just store them.
  1173. */
  1174. unpacked[i] = *((__be32 *)field);
  1175. field += 2;
  1176. } else if (be16_to_cpup(field) & VPHN_FIELD_MSB) {
  1177. /* Data is in the lower 15 bits of this field */
  1178. unpacked[i] = cpu_to_be32(
  1179. be16_to_cpup(field) & VPHN_FIELD_MASK);
  1180. field++;
  1181. nr_assoc_doms++;
  1182. } else {
  1183. /* Data is in the lower 15 bits of this field
  1184. * concatenated with the next 16 bit field
  1185. */
  1186. unpacked[i] = *((__be32 *)field);
  1187. field += 2;
  1188. nr_assoc_doms++;
  1189. }
  1190. }
  1191. /* The first cell contains the length of the property */
  1192. unpacked[0] = cpu_to_be32(nr_assoc_doms);
  1193. return nr_assoc_doms;
  1194. }
  1195. /*
  1196. * Retrieve the new associativity information for a virtual processor's
  1197. * home node.
  1198. */
  1199. static long hcall_vphn(unsigned long cpu, __be32 *associativity)
  1200. {
  1201. long rc;
  1202. long retbuf[PLPAR_HCALL9_BUFSIZE] = {0};
  1203. u64 flags = 1;
  1204. int hwcpu = get_hard_smp_processor_id(cpu);
  1205. rc = plpar_hcall9(H_HOME_NODE_ASSOCIATIVITY, retbuf, flags, hwcpu);
  1206. vphn_unpack_associativity(retbuf, associativity);
  1207. return rc;
  1208. }
  1209. static long vphn_get_associativity(unsigned long cpu,
  1210. __be32 *associativity)
  1211. {
  1212. long rc;
  1213. rc = hcall_vphn(cpu, associativity);
  1214. switch (rc) {
  1215. case H_FUNCTION:
  1216. printk(KERN_INFO
  1217. "VPHN is not supported. Disabling polling...\n");
  1218. stop_topology_update();
  1219. break;
  1220. case H_HARDWARE:
  1221. printk(KERN_ERR
  1222. "hcall_vphn() experienced a hardware fault "
  1223. "preventing VPHN. Disabling polling...\n");
  1224. stop_topology_update();
  1225. }
  1226. return rc;
  1227. }
  1228. /*
  1229. * Update the CPU maps and sysfs entries for a single CPU when its NUMA
  1230. * characteristics change. This function doesn't perform any locking and is
  1231. * only safe to call from stop_machine().
  1232. */
  1233. static int update_cpu_topology(void *data)
  1234. {
  1235. struct topology_update_data *update;
  1236. unsigned long cpu;
  1237. if (!data)
  1238. return -EINVAL;
  1239. cpu = smp_processor_id();
  1240. for (update = data; update; update = update->next) {
  1241. if (cpu != update->cpu)
  1242. continue;
  1243. unmap_cpu_from_node(update->cpu);
  1244. map_cpu_to_node(update->cpu, update->new_nid);
  1245. vdso_getcpu_init();
  1246. }
  1247. return 0;
  1248. }
  1249. static int update_lookup_table(void *data)
  1250. {
  1251. struct topology_update_data *update;
  1252. if (!data)
  1253. return -EINVAL;
  1254. /*
  1255. * Upon topology update, the numa-cpu lookup table needs to be updated
  1256. * for all threads in the core, including offline CPUs, to ensure that
  1257. * future hotplug operations respect the cpu-to-node associativity
  1258. * properly.
  1259. */
  1260. for (update = data; update; update = update->next) {
  1261. int nid, base, j;
  1262. nid = update->new_nid;
  1263. base = cpu_first_thread_sibling(update->cpu);
  1264. for (j = 0; j < threads_per_core; j++) {
  1265. update_numa_cpu_lookup_table(base + j, nid);
  1266. }
  1267. }
  1268. return 0;
  1269. }
  1270. /*
  1271. * Update the node maps and sysfs entries for each cpu whose home node
  1272. * has changed. Returns 1 when the topology has changed, and 0 otherwise.
  1273. */
  1274. int arch_update_cpu_topology(void)
  1275. {
  1276. unsigned int cpu, sibling, changed = 0;
  1277. struct topology_update_data *updates, *ud;
  1278. __be32 associativity[VPHN_ASSOC_BUFSIZE] = {0};
  1279. cpumask_t updated_cpus;
  1280. struct device *dev;
  1281. int weight, new_nid, i = 0;
  1282. weight = cpumask_weight(&cpu_associativity_changes_mask);
  1283. if (!weight)
  1284. return 0;
  1285. updates = kzalloc(weight * (sizeof(*updates)), GFP_KERNEL);
  1286. if (!updates)
  1287. return 0;
  1288. cpumask_clear(&updated_cpus);
  1289. for_each_cpu(cpu, &cpu_associativity_changes_mask) {
  1290. /*
  1291. * If siblings aren't flagged for changes, updates list
  1292. * will be too short. Skip on this update and set for next
  1293. * update.
  1294. */
  1295. if (!cpumask_subset(cpu_sibling_mask(cpu),
  1296. &cpu_associativity_changes_mask)) {
  1297. pr_info("Sibling bits not set for associativity "
  1298. "change, cpu%d\n", cpu);
  1299. cpumask_or(&cpu_associativity_changes_mask,
  1300. &cpu_associativity_changes_mask,
  1301. cpu_sibling_mask(cpu));
  1302. cpu = cpu_last_thread_sibling(cpu);
  1303. continue;
  1304. }
  1305. /* Use associativity from first thread for all siblings */
  1306. vphn_get_associativity(cpu, associativity);
  1307. new_nid = associativity_to_nid(associativity);
  1308. if (new_nid < 0 || !node_online(new_nid))
  1309. new_nid = first_online_node;
  1310. if (new_nid == numa_cpu_lookup_table[cpu]) {
  1311. cpumask_andnot(&cpu_associativity_changes_mask,
  1312. &cpu_associativity_changes_mask,
  1313. cpu_sibling_mask(cpu));
  1314. cpu = cpu_last_thread_sibling(cpu);
  1315. continue;
  1316. }
  1317. for_each_cpu(sibling, cpu_sibling_mask(cpu)) {
  1318. ud = &updates[i++];
  1319. ud->cpu = sibling;
  1320. ud->new_nid = new_nid;
  1321. ud->old_nid = numa_cpu_lookup_table[sibling];
  1322. cpumask_set_cpu(sibling, &updated_cpus);
  1323. if (i < weight)
  1324. ud->next = &updates[i];
  1325. }
  1326. cpu = cpu_last_thread_sibling(cpu);
  1327. }
  1328. /*
  1329. * In cases where we have nothing to update (because the updates list
  1330. * is too short or because the new topology is same as the old one),
  1331. * skip invoking update_cpu_topology() via stop-machine(). This is
  1332. * necessary (and not just a fast-path optimization) since stop-machine
  1333. * can end up electing a random CPU to run update_cpu_topology(), and
  1334. * thus trick us into setting up incorrect cpu-node mappings (since
  1335. * 'updates' is kzalloc()'ed).
  1336. *
  1337. * And for the similar reason, we will skip all the following updating.
  1338. */
  1339. if (!cpumask_weight(&updated_cpus))
  1340. goto out;
  1341. stop_machine(update_cpu_topology, &updates[0], &updated_cpus);
  1342. /*
  1343. * Update the numa-cpu lookup table with the new mappings, even for
  1344. * offline CPUs. It is best to perform this update from the stop-
  1345. * machine context.
  1346. */
  1347. stop_machine(update_lookup_table, &updates[0],
  1348. cpumask_of(raw_smp_processor_id()));
  1349. for (ud = &updates[0]; ud; ud = ud->next) {
  1350. unregister_cpu_under_node(ud->cpu, ud->old_nid);
  1351. register_cpu_under_node(ud->cpu, ud->new_nid);
  1352. dev = get_cpu_device(ud->cpu);
  1353. if (dev)
  1354. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  1355. cpumask_clear_cpu(ud->cpu, &cpu_associativity_changes_mask);
  1356. changed = 1;
  1357. }
  1358. out:
  1359. kfree(updates);
  1360. return changed;
  1361. }
  1362. static void topology_work_fn(struct work_struct *work)
  1363. {
  1364. rebuild_sched_domains();
  1365. }
  1366. static DECLARE_WORK(topology_work, topology_work_fn);
  1367. static void topology_schedule_update(void)
  1368. {
  1369. schedule_work(&topology_work);
  1370. }
  1371. static void topology_timer_fn(unsigned long ignored)
  1372. {
  1373. if (prrn_enabled && cpumask_weight(&cpu_associativity_changes_mask))
  1374. topology_schedule_update();
  1375. else if (vphn_enabled) {
  1376. if (update_cpu_associativity_changes_mask() > 0)
  1377. topology_schedule_update();
  1378. reset_topology_timer();
  1379. }
  1380. }
  1381. static struct timer_list topology_timer =
  1382. TIMER_INITIALIZER(topology_timer_fn, 0, 0);
  1383. static void reset_topology_timer(void)
  1384. {
  1385. topology_timer.data = 0;
  1386. topology_timer.expires = jiffies + 60 * HZ;
  1387. mod_timer(&topology_timer, topology_timer.expires);
  1388. }
  1389. #ifdef CONFIG_SMP
  1390. static void stage_topology_update(int core_id)
  1391. {
  1392. cpumask_or(&cpu_associativity_changes_mask,
  1393. &cpu_associativity_changes_mask, cpu_sibling_mask(core_id));
  1394. reset_topology_timer();
  1395. }
  1396. static int dt_update_callback(struct notifier_block *nb,
  1397. unsigned long action, void *data)
  1398. {
  1399. struct of_prop_reconfig *update;
  1400. int rc = NOTIFY_DONE;
  1401. switch (action) {
  1402. case OF_RECONFIG_UPDATE_PROPERTY:
  1403. update = (struct of_prop_reconfig *)data;
  1404. if (!of_prop_cmp(update->dn->type, "cpu") &&
  1405. !of_prop_cmp(update->prop->name, "ibm,associativity")) {
  1406. u32 core_id;
  1407. of_property_read_u32(update->dn, "reg", &core_id);
  1408. stage_topology_update(core_id);
  1409. rc = NOTIFY_OK;
  1410. }
  1411. break;
  1412. }
  1413. return rc;
  1414. }
  1415. static struct notifier_block dt_update_nb = {
  1416. .notifier_call = dt_update_callback,
  1417. };
  1418. #endif
  1419. /*
  1420. * Start polling for associativity changes.
  1421. */
  1422. int start_topology_update(void)
  1423. {
  1424. int rc = 0;
  1425. if (firmware_has_feature(FW_FEATURE_PRRN)) {
  1426. if (!prrn_enabled) {
  1427. prrn_enabled = 1;
  1428. vphn_enabled = 0;
  1429. #ifdef CONFIG_SMP
  1430. rc = of_reconfig_notifier_register(&dt_update_nb);
  1431. #endif
  1432. }
  1433. } else if (firmware_has_feature(FW_FEATURE_VPHN) &&
  1434. lppaca_shared_proc(get_lppaca())) {
  1435. if (!vphn_enabled) {
  1436. prrn_enabled = 0;
  1437. vphn_enabled = 1;
  1438. setup_cpu_associativity_change_counters();
  1439. init_timer_deferrable(&topology_timer);
  1440. reset_topology_timer();
  1441. }
  1442. }
  1443. return rc;
  1444. }
  1445. /*
  1446. * Disable polling for VPHN associativity changes.
  1447. */
  1448. int stop_topology_update(void)
  1449. {
  1450. int rc = 0;
  1451. if (prrn_enabled) {
  1452. prrn_enabled = 0;
  1453. #ifdef CONFIG_SMP
  1454. rc = of_reconfig_notifier_unregister(&dt_update_nb);
  1455. #endif
  1456. } else if (vphn_enabled) {
  1457. vphn_enabled = 0;
  1458. rc = del_timer_sync(&topology_timer);
  1459. }
  1460. return rc;
  1461. }
  1462. int prrn_is_enabled(void)
  1463. {
  1464. return prrn_enabled;
  1465. }
  1466. static int topology_read(struct seq_file *file, void *v)
  1467. {
  1468. if (vphn_enabled || prrn_enabled)
  1469. seq_puts(file, "on\n");
  1470. else
  1471. seq_puts(file, "off\n");
  1472. return 0;
  1473. }
  1474. static int topology_open(struct inode *inode, struct file *file)
  1475. {
  1476. return single_open(file, topology_read, NULL);
  1477. }
  1478. static ssize_t topology_write(struct file *file, const char __user *buf,
  1479. size_t count, loff_t *off)
  1480. {
  1481. char kbuf[4]; /* "on" or "off" plus null. */
  1482. int read_len;
  1483. read_len = count < 3 ? count : 3;
  1484. if (copy_from_user(kbuf, buf, read_len))
  1485. return -EINVAL;
  1486. kbuf[read_len] = '\0';
  1487. if (!strncmp(kbuf, "on", 2))
  1488. start_topology_update();
  1489. else if (!strncmp(kbuf, "off", 3))
  1490. stop_topology_update();
  1491. else
  1492. return -EINVAL;
  1493. return count;
  1494. }
  1495. static const struct file_operations topology_ops = {
  1496. .read = seq_read,
  1497. .write = topology_write,
  1498. .open = topology_open,
  1499. .release = single_release
  1500. };
  1501. static int topology_update_init(void)
  1502. {
  1503. start_topology_update();
  1504. proc_create("powerpc/topology_updates", 0644, NULL, &topology_ops);
  1505. return 0;
  1506. }
  1507. device_initcall(topology_update_init);
  1508. #endif /* CONFIG_PPC_SPLPAR */