numa.c 37 KB

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