numa.c 39 KB

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