numa.c 40 KB

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