numa.c 36 KB

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