numa.c 40 KB

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