subcore.c 11 KB

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