subcore.c 9.7 KB

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