numa.c 39 KB

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