subcore.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright 2013, Michael (Ellerman|Neuling), IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #define pr_fmt(fmt) "powernv: " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/cpu.h>
  12. #include <linux/cpumask.h>
  13. #include <linux/device.h>
  14. #include <linux/gfp.h>
  15. #include <linux/smp.h>
  16. #include <linux/stop_machine.h>
  17. #include <asm/cputhreads.h>
  18. #include <asm/kvm_ppc.h>
  19. #include <asm/machdep.h>
  20. #include <asm/opal.h>
  21. #include <asm/smp.h>
  22. #include "subcore.h"
  23. #include "powernv.h"
  24. /*
  25. * Split/unsplit procedure:
  26. *
  27. * A core can be in one of three states, unsplit, 2-way split, and 4-way split.
  28. *
  29. * The mapping to subcores_per_core is simple:
  30. *
  31. * State | subcores_per_core
  32. * ------------|------------------
  33. * Unsplit | 1
  34. * 2-way split | 2
  35. * 4-way split | 4
  36. *
  37. * The core is split along thread boundaries, the mapping between subcores and
  38. * threads is as follows:
  39. *
  40. * Unsplit:
  41. * ----------------------------
  42. * Subcore | 0 |
  43. * ----------------------------
  44. * Thread | 0 1 2 3 4 5 6 7 |
  45. * ----------------------------
  46. *
  47. * 2-way split:
  48. * -------------------------------------
  49. * Subcore | 0 | 1 |
  50. * -------------------------------------
  51. * Thread | 0 1 2 3 | 4 5 6 7 |
  52. * -------------------------------------
  53. *
  54. * 4-way split:
  55. * -----------------------------------------
  56. * Subcore | 0 | 1 | 2 | 3 |
  57. * -----------------------------------------
  58. * Thread | 0 1 | 2 3 | 4 5 | 6 7 |
  59. * -----------------------------------------
  60. *
  61. *
  62. * Transitions
  63. * -----------
  64. *
  65. * It is not possible to transition between either of the split states, the
  66. * core must first be unsplit. The legal transitions are:
  67. *
  68. * ----------- ---------------
  69. * | | <----> | 2-way split |
  70. * | | ---------------
  71. * | Unsplit |
  72. * | | ---------------
  73. * | | <----> | 4-way split |
  74. * ----------- ---------------
  75. *
  76. * Unsplitting
  77. * -----------
  78. *
  79. * Unsplitting is the simpler procedure. It requires thread 0 to request the
  80. * unsplit while all other threads NAP.
  81. *
  82. * Thread 0 clears HID0_POWER8_DYNLPARDIS (Dynamic LPAR Disable). This tells
  83. * the hardware that if all threads except 0 are napping, the hardware should
  84. * unsplit the core.
  85. *
  86. * Non-zero threads are sent to a NAP loop, they don't exit the loop until they
  87. * see the core unsplit.
  88. *
  89. * Core 0 spins waiting for the hardware to see all the other threads napping
  90. * and perform the unsplit.
  91. *
  92. * Once thread 0 sees the unsplit, it IPIs the secondary threads to wake them
  93. * out of NAP. They will then see the core unsplit and exit the NAP loop.
  94. *
  95. * Splitting
  96. * ---------
  97. *
  98. * The basic splitting procedure is fairly straight forward. However it is
  99. * complicated by the fact that after the split occurs, the newly created
  100. * subcores are not in a fully initialised state.
  101. *
  102. * Most notably the subcores do not have the correct value for SDR1, which
  103. * means they must not be running in virtual mode when the split occurs. The
  104. * subcores have separate timebases SPRs but these are pre-synchronised by
  105. * opal.
  106. *
  107. * To begin with secondary threads are sent to an assembly routine. There they
  108. * switch to real mode, so they are immune to the uninitialised SDR1 value.
  109. * Once in real mode they indicate that they are in real mode, and spin waiting
  110. * to see the core split.
  111. *
  112. * Thread 0 waits to see that all secondaries are in real mode, and then begins
  113. * the splitting procedure. It firstly sets HID0_POWER8_DYNLPARDIS, which
  114. * prevents the hardware from unsplitting. Then it sets the appropriate HID bit
  115. * to request the split, and spins waiting to see that the split has happened.
  116. *
  117. * Concurrently the secondaries will notice the split. When they do they set up
  118. * their SPRs, notably SDR1, and then they can return to virtual mode and exit
  119. * the procedure.
  120. */
  121. /* Initialised at boot by subcore_init() */
  122. static int subcores_per_core;
  123. /*
  124. * Used to communicate to offline cpus that we want them to pop out of the
  125. * offline loop and do a split or unsplit.
  126. *
  127. * 0 - no split happening
  128. * 1 - unsplit in progress
  129. * 2 - split to 2 in progress
  130. * 4 - split to 4 in progress
  131. */
  132. static int new_split_mode;
  133. static cpumask_var_t cpu_offline_mask;
  134. struct split_state {
  135. u8 step;
  136. u8 master;
  137. };
  138. static DEFINE_PER_CPU(struct split_state, split_state);
  139. static void wait_for_sync_step(int step)
  140. {
  141. int i, cpu = smp_processor_id();
  142. for (i = cpu + 1; i < cpu + threads_per_core; i++)
  143. while(per_cpu(split_state, i).step < step)
  144. barrier();
  145. /* Order the wait loop vs any subsequent loads/stores. */
  146. mb();
  147. }
  148. static void update_hid_in_slw(u64 hid0)
  149. {
  150. u64 idle_states = pnv_get_supported_cpuidle_states();
  151. if (idle_states & OPAL_PM_WINKLE_ENABLED) {
  152. /* OPAL call to patch slw with the new HID0 value */
  153. u64 cpu_pir = hard_smp_processor_id();
  154. opal_slw_set_reg(cpu_pir, SPRN_HID0, hid0);
  155. }
  156. }
  157. static void unsplit_core(void)
  158. {
  159. u64 hid0, mask;
  160. int i, cpu;
  161. mask = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
  162. cpu = smp_processor_id();
  163. if (cpu_thread_in_core(cpu) != 0) {
  164. while (mfspr(SPRN_HID0) & mask)
  165. power7_nap(0);
  166. per_cpu(split_state, cpu).step = SYNC_STEP_UNSPLIT;
  167. return;
  168. }
  169. hid0 = mfspr(SPRN_HID0);
  170. hid0 &= ~HID0_POWER8_DYNLPARDIS;
  171. update_power8_hid0(hid0);
  172. update_hid_in_slw(hid0);
  173. while (mfspr(SPRN_HID0) & mask)
  174. cpu_relax();
  175. /* Wake secondaries out of NAP */
  176. for (i = cpu + 1; i < cpu + threads_per_core; i++)
  177. smp_send_reschedule(i);
  178. wait_for_sync_step(SYNC_STEP_UNSPLIT);
  179. }
  180. static void split_core(int new_mode)
  181. {
  182. struct { u64 value; u64 mask; } split_parms[2] = {
  183. { HID0_POWER8_1TO2LPAR, HID0_POWER8_2LPARMODE },
  184. { HID0_POWER8_1TO4LPAR, HID0_POWER8_4LPARMODE }
  185. };
  186. int i, cpu;
  187. u64 hid0;
  188. /* Convert new_mode (2 or 4) into an index into our parms array */
  189. i = (new_mode >> 1) - 1;
  190. BUG_ON(i < 0 || i > 1);
  191. cpu = smp_processor_id();
  192. if (cpu_thread_in_core(cpu) != 0) {
  193. split_core_secondary_loop(&per_cpu(split_state, cpu).step);
  194. return;
  195. }
  196. wait_for_sync_step(SYNC_STEP_REAL_MODE);
  197. /* Write new mode */
  198. hid0 = mfspr(SPRN_HID0);
  199. hid0 |= HID0_POWER8_DYNLPARDIS | split_parms[i].value;
  200. update_power8_hid0(hid0);
  201. update_hid_in_slw(hid0);
  202. /* Wait for it to happen */
  203. while (!(mfspr(SPRN_HID0) & split_parms[i].mask))
  204. cpu_relax();
  205. }
  206. static void cpu_do_split(int new_mode)
  207. {
  208. /*
  209. * At boot subcores_per_core will be 0, so we will always unsplit at
  210. * boot. In the usual case where the core is already unsplit it's a
  211. * nop, and this just ensures the kernel's notion of the mode is
  212. * consistent with the hardware.
  213. */
  214. if (subcores_per_core != 1)
  215. unsplit_core();
  216. if (new_mode != 1)
  217. split_core(new_mode);
  218. mb();
  219. per_cpu(split_state, smp_processor_id()).step = SYNC_STEP_FINISHED;
  220. }
  221. bool cpu_core_split_required(void)
  222. {
  223. smp_rmb();
  224. if (!new_split_mode)
  225. return false;
  226. cpu_do_split(new_split_mode);
  227. return true;
  228. }
  229. void update_subcore_sibling_mask(void)
  230. {
  231. int cpu;
  232. /*
  233. * sibling mask for the first cpu. Left shift this by required bits
  234. * to get sibling mask for the rest of the cpus.
  235. */
  236. int sibling_mask_first_cpu = (1 << threads_per_subcore) - 1;
  237. for_each_possible_cpu(cpu) {
  238. int tid = cpu_thread_in_core(cpu);
  239. int offset = (tid / threads_per_subcore) * threads_per_subcore;
  240. int mask = sibling_mask_first_cpu << offset;
  241. paca[cpu].subcore_sibling_mask = mask;
  242. }
  243. }
  244. static int cpu_update_split_mode(void *data)
  245. {
  246. int cpu, new_mode = *(int *)data;
  247. if (this_cpu_ptr(&split_state)->master) {
  248. new_split_mode = new_mode;
  249. smp_wmb();
  250. cpumask_andnot(cpu_offline_mask, cpu_present_mask,
  251. cpu_online_mask);
  252. /* This should work even though the cpu is offline */
  253. for_each_cpu(cpu, cpu_offline_mask)
  254. smp_send_reschedule(cpu);
  255. }
  256. cpu_do_split(new_mode);
  257. if (this_cpu_ptr(&split_state)->master) {
  258. /* Wait for all cpus to finish before we touch subcores_per_core */
  259. for_each_present_cpu(cpu) {
  260. if (cpu >= setup_max_cpus)
  261. break;
  262. while(per_cpu(split_state, cpu).step < SYNC_STEP_FINISHED)
  263. barrier();
  264. }
  265. new_split_mode = 0;
  266. /* Make the new mode public */
  267. subcores_per_core = new_mode;
  268. threads_per_subcore = threads_per_core / subcores_per_core;
  269. update_subcore_sibling_mask();
  270. /* Make sure the new mode is written before we exit */
  271. mb();
  272. }
  273. return 0;
  274. }
  275. static int set_subcores_per_core(int new_mode)
  276. {
  277. struct split_state *state;
  278. int cpu;
  279. if (kvm_hv_mode_active()) {
  280. pr_err("Unable to change split core mode while KVM active.\n");
  281. return -EBUSY;
  282. }
  283. /*
  284. * We are only called at boot, or from the sysfs write. If that ever
  285. * changes we'll need a lock here.
  286. */
  287. BUG_ON(new_mode < 1 || new_mode > 4 || new_mode == 3);
  288. for_each_present_cpu(cpu) {
  289. state = &per_cpu(split_state, cpu);
  290. state->step = SYNC_STEP_INITIAL;
  291. state->master = 0;
  292. }
  293. get_online_cpus();
  294. /* This cpu will update the globals before exiting stop machine */
  295. this_cpu_ptr(&split_state)->master = 1;
  296. /* Ensure state is consistent before we call the other cpus */
  297. mb();
  298. stop_machine(cpu_update_split_mode, &new_mode, cpu_online_mask);
  299. put_online_cpus();
  300. return 0;
  301. }
  302. static ssize_t __used store_subcores_per_core(struct device *dev,
  303. struct device_attribute *attr, const char *buf,
  304. size_t count)
  305. {
  306. unsigned long val;
  307. int rc;
  308. /* We are serialised by the attribute lock */
  309. rc = sscanf(buf, "%lx", &val);
  310. if (rc != 1)
  311. return -EINVAL;
  312. switch (val) {
  313. case 1:
  314. case 2:
  315. case 4:
  316. if (subcores_per_core == val)
  317. /* Nothing to do */
  318. goto out;
  319. break;
  320. default:
  321. return -EINVAL;
  322. }
  323. rc = set_subcores_per_core(val);
  324. if (rc)
  325. return rc;
  326. out:
  327. return count;
  328. }
  329. static ssize_t show_subcores_per_core(struct device *dev,
  330. struct device_attribute *attr, char *buf)
  331. {
  332. return sprintf(buf, "%x\n", subcores_per_core);
  333. }
  334. static DEVICE_ATTR(subcores_per_core, 0644,
  335. show_subcores_per_core, store_subcores_per_core);
  336. static int subcore_init(void)
  337. {
  338. if (!cpu_has_feature(CPU_FTR_SUBCORE))
  339. return 0;
  340. /*
  341. * We need all threads in a core to be present to split/unsplit so
  342. * continue only if max_cpus are aligned to threads_per_core.
  343. */
  344. if (setup_max_cpus % threads_per_core)
  345. return 0;
  346. BUG_ON(!alloc_cpumask_var(&cpu_offline_mask, GFP_KERNEL));
  347. set_subcores_per_core(1);
  348. return device_create_file(cpu_subsys.dev_root,
  349. &dev_attr_subcores_per_core);
  350. }
  351. machine_device_initcall(powernv, subcore_init);