subcore.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 unsplit_core(void)
  149. {
  150. u64 hid0, mask;
  151. int i, cpu;
  152. mask = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
  153. cpu = smp_processor_id();
  154. if (cpu_thread_in_core(cpu) != 0) {
  155. while (mfspr(SPRN_HID0) & mask)
  156. power7_nap(0);
  157. per_cpu(split_state, cpu).step = SYNC_STEP_UNSPLIT;
  158. return;
  159. }
  160. hid0 = mfspr(SPRN_HID0);
  161. hid0 &= ~HID0_POWER8_DYNLPARDIS;
  162. mtspr(SPRN_HID0, hid0);
  163. while (mfspr(SPRN_HID0) & mask)
  164. cpu_relax();
  165. /* Wake secondaries out of NAP */
  166. for (i = cpu + 1; i < cpu + threads_per_core; i++)
  167. smp_send_reschedule(i);
  168. wait_for_sync_step(SYNC_STEP_UNSPLIT);
  169. }
  170. static void split_core(int new_mode)
  171. {
  172. struct { u64 value; u64 mask; } split_parms[2] = {
  173. { HID0_POWER8_1TO2LPAR, HID0_POWER8_2LPARMODE },
  174. { HID0_POWER8_1TO4LPAR, HID0_POWER8_4LPARMODE }
  175. };
  176. int i, cpu;
  177. u64 hid0;
  178. /* Convert new_mode (2 or 4) into an index into our parms array */
  179. i = (new_mode >> 1) - 1;
  180. BUG_ON(i < 0 || i > 1);
  181. cpu = smp_processor_id();
  182. if (cpu_thread_in_core(cpu) != 0) {
  183. split_core_secondary_loop(&per_cpu(split_state, cpu).step);
  184. return;
  185. }
  186. wait_for_sync_step(SYNC_STEP_REAL_MODE);
  187. /* Write new mode */
  188. hid0 = mfspr(SPRN_HID0);
  189. hid0 |= HID0_POWER8_DYNLPARDIS | split_parms[i].value;
  190. mtspr(SPRN_HID0, hid0);
  191. /* Wait for it to happen */
  192. while (!(mfspr(SPRN_HID0) & split_parms[i].mask))
  193. cpu_relax();
  194. }
  195. static void cpu_do_split(int new_mode)
  196. {
  197. /*
  198. * At boot subcores_per_core will be 0, so we will always unsplit at
  199. * boot. In the usual case where the core is already unsplit it's a
  200. * nop, and this just ensures the kernel's notion of the mode is
  201. * consistent with the hardware.
  202. */
  203. if (subcores_per_core != 1)
  204. unsplit_core();
  205. if (new_mode != 1)
  206. split_core(new_mode);
  207. mb();
  208. per_cpu(split_state, smp_processor_id()).step = SYNC_STEP_FINISHED;
  209. }
  210. bool cpu_core_split_required(void)
  211. {
  212. smp_rmb();
  213. if (!new_split_mode)
  214. return false;
  215. cpu_do_split(new_split_mode);
  216. return true;
  217. }
  218. static int cpu_update_split_mode(void *data)
  219. {
  220. int cpu, new_mode = *(int *)data;
  221. if (this_cpu_ptr(&split_state)->master) {
  222. new_split_mode = new_mode;
  223. smp_wmb();
  224. cpumask_andnot(cpu_offline_mask, cpu_present_mask,
  225. cpu_online_mask);
  226. /* This should work even though the cpu is offline */
  227. for_each_cpu(cpu, cpu_offline_mask)
  228. smp_send_reschedule(cpu);
  229. }
  230. cpu_do_split(new_mode);
  231. if (this_cpu_ptr(&split_state)->master) {
  232. /* Wait for all cpus to finish before we touch subcores_per_core */
  233. for_each_present_cpu(cpu) {
  234. if (cpu >= setup_max_cpus)
  235. break;
  236. while(per_cpu(split_state, cpu).step < SYNC_STEP_FINISHED)
  237. barrier();
  238. }
  239. new_split_mode = 0;
  240. /* Make the new mode public */
  241. subcores_per_core = new_mode;
  242. threads_per_subcore = threads_per_core / subcores_per_core;
  243. /* Make sure the new mode is written before we exit */
  244. mb();
  245. }
  246. return 0;
  247. }
  248. static int set_subcores_per_core(int new_mode)
  249. {
  250. struct split_state *state;
  251. int cpu;
  252. if (kvm_hv_mode_active()) {
  253. pr_err("Unable to change split core mode while KVM active.\n");
  254. return -EBUSY;
  255. }
  256. /*
  257. * We are only called at boot, or from the sysfs write. If that ever
  258. * changes we'll need a lock here.
  259. */
  260. BUG_ON(new_mode < 1 || new_mode > 4 || new_mode == 3);
  261. for_each_present_cpu(cpu) {
  262. state = &per_cpu(split_state, cpu);
  263. state->step = SYNC_STEP_INITIAL;
  264. state->master = 0;
  265. }
  266. get_online_cpus();
  267. /* This cpu will update the globals before exiting stop machine */
  268. this_cpu_ptr(&split_state)->master = 1;
  269. /* Ensure state is consistent before we call the other cpus */
  270. mb();
  271. stop_machine(cpu_update_split_mode, &new_mode, cpu_online_mask);
  272. put_online_cpus();
  273. return 0;
  274. }
  275. static ssize_t __used store_subcores_per_core(struct device *dev,
  276. struct device_attribute *attr, const char *buf,
  277. size_t count)
  278. {
  279. unsigned long val;
  280. int rc;
  281. /* We are serialised by the attribute lock */
  282. rc = sscanf(buf, "%lx", &val);
  283. if (rc != 1)
  284. return -EINVAL;
  285. switch (val) {
  286. case 1:
  287. case 2:
  288. case 4:
  289. if (subcores_per_core == val)
  290. /* Nothing to do */
  291. goto out;
  292. break;
  293. default:
  294. return -EINVAL;
  295. }
  296. rc = set_subcores_per_core(val);
  297. if (rc)
  298. return rc;
  299. out:
  300. return count;
  301. }
  302. static ssize_t show_subcores_per_core(struct device *dev,
  303. struct device_attribute *attr, char *buf)
  304. {
  305. return sprintf(buf, "%x\n", subcores_per_core);
  306. }
  307. static DEVICE_ATTR(subcores_per_core, 0644,
  308. show_subcores_per_core, store_subcores_per_core);
  309. static int subcore_init(void)
  310. {
  311. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  312. return 0;
  313. /*
  314. * We need all threads in a core to be present to split/unsplit so
  315. * continue only if max_cpus are aligned to threads_per_core.
  316. */
  317. if (setup_max_cpus % threads_per_core)
  318. return 0;
  319. BUG_ON(!alloc_cpumask_var(&cpu_offline_mask, GFP_KERNEL));
  320. set_subcores_per_core(1);
  321. return device_create_file(cpu_subsys.dev_root,
  322. &dev_attr_subcores_per_core);
  323. }
  324. machine_device_initcall(powernv, subcore_init);