numa.c 36 KB

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