fpsimd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * FP/SIMD context switching and fault handling
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Author: Catalin Marinas <catalin.marinas@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/cpu.h>
  20. #include <linux/cpu_pm.h>
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/sched.h>
  24. #include <linux/signal.h>
  25. #include <linux/hardirq.h>
  26. #include <asm/fpsimd.h>
  27. #include <asm/cputype.h>
  28. #define FPEXC_IOF (1 << 0)
  29. #define FPEXC_DZF (1 << 1)
  30. #define FPEXC_OFF (1 << 2)
  31. #define FPEXC_UFF (1 << 3)
  32. #define FPEXC_IXF (1 << 4)
  33. #define FPEXC_IDF (1 << 7)
  34. /*
  35. * In order to reduce the number of times the FPSIMD state is needlessly saved
  36. * and restored, we need to keep track of two things:
  37. * (a) for each task, we need to remember which CPU was the last one to have
  38. * the task's FPSIMD state loaded into its FPSIMD registers;
  39. * (b) for each CPU, we need to remember which task's userland FPSIMD state has
  40. * been loaded into its FPSIMD registers most recently, or whether it has
  41. * been used to perform kernel mode NEON in the meantime.
  42. *
  43. * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to
  44. * the id of the current CPU everytime the state is loaded onto a CPU. For (b),
  45. * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
  46. * address of the userland FPSIMD state of the task that was loaded onto the CPU
  47. * the most recently, or NULL if kernel mode NEON has been performed after that.
  48. *
  49. * With this in place, we no longer have to restore the next FPSIMD state right
  50. * when switching between tasks. Instead, we can defer this check to userland
  51. * resume, at which time we verify whether the CPU's fpsimd_last_state and the
  52. * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we
  53. * can omit the FPSIMD restore.
  54. *
  55. * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
  56. * indicate whether or not the userland FPSIMD state of the current task is
  57. * present in the registers. The flag is set unless the FPSIMD registers of this
  58. * CPU currently contain the most recent userland FPSIMD state of the current
  59. * task.
  60. *
  61. * For a certain task, the sequence may look something like this:
  62. * - the task gets scheduled in; if both the task's fpsimd_state.cpu field
  63. * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
  64. * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
  65. * cleared, otherwise it is set;
  66. *
  67. * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
  68. * userland FPSIMD state is copied from memory to the registers, the task's
  69. * fpsimd_state.cpu field is set to the id of the current CPU, the current
  70. * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
  71. * TIF_FOREIGN_FPSTATE flag is cleared;
  72. *
  73. * - the task executes an ordinary syscall; upon return to userland, the
  74. * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
  75. * restored;
  76. *
  77. * - the task executes a syscall which executes some NEON instructions; this is
  78. * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
  79. * register contents to memory, clears the fpsimd_last_state per-cpu variable
  80. * and sets the TIF_FOREIGN_FPSTATE flag;
  81. *
  82. * - the task gets preempted after kernel_neon_end() is called; as we have not
  83. * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
  84. * whatever is in the FPSIMD registers is not saved to memory, but discarded.
  85. */
  86. static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
  87. /*
  88. * Trapped FP/ASIMD access.
  89. */
  90. void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
  91. {
  92. /* TODO: implement lazy context saving/restoring */
  93. WARN_ON(1);
  94. }
  95. /*
  96. * Raise a SIGFPE for the current process.
  97. */
  98. void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
  99. {
  100. siginfo_t info;
  101. unsigned int si_code = 0;
  102. if (esr & FPEXC_IOF)
  103. si_code = FPE_FLTINV;
  104. else if (esr & FPEXC_DZF)
  105. si_code = FPE_FLTDIV;
  106. else if (esr & FPEXC_OFF)
  107. si_code = FPE_FLTOVF;
  108. else if (esr & FPEXC_UFF)
  109. si_code = FPE_FLTUND;
  110. else if (esr & FPEXC_IXF)
  111. si_code = FPE_FLTRES;
  112. memset(&info, 0, sizeof(info));
  113. info.si_signo = SIGFPE;
  114. info.si_code = si_code;
  115. info.si_addr = (void __user *)instruction_pointer(regs);
  116. send_sig_info(SIGFPE, &info, current);
  117. }
  118. void fpsimd_thread_switch(struct task_struct *next)
  119. {
  120. /*
  121. * Save the current FPSIMD state to memory, but only if whatever is in
  122. * the registers is in fact the most recent userland FPSIMD state of
  123. * 'current'.
  124. */
  125. if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
  126. fpsimd_save_state(&current->thread.fpsimd_state);
  127. if (next->mm) {
  128. /*
  129. * If we are switching to a task whose most recent userland
  130. * FPSIMD state is already in the registers of *this* cpu,
  131. * we can skip loading the state from memory. Otherwise, set
  132. * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
  133. * upon the next return to userland.
  134. */
  135. struct fpsimd_state *st = &next->thread.fpsimd_state;
  136. if (__this_cpu_read(fpsimd_last_state) == st
  137. && st->cpu == smp_processor_id())
  138. clear_ti_thread_flag(task_thread_info(next),
  139. TIF_FOREIGN_FPSTATE);
  140. else
  141. set_ti_thread_flag(task_thread_info(next),
  142. TIF_FOREIGN_FPSTATE);
  143. }
  144. }
  145. void fpsimd_flush_thread(void)
  146. {
  147. preempt_disable();
  148. memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
  149. fpsimd_flush_task_state(current);
  150. set_thread_flag(TIF_FOREIGN_FPSTATE);
  151. preempt_enable();
  152. }
  153. /*
  154. * Save the userland FPSIMD state of 'current' to memory, but only if the state
  155. * currently held in the registers does in fact belong to 'current'
  156. */
  157. void fpsimd_preserve_current_state(void)
  158. {
  159. preempt_disable();
  160. if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
  161. fpsimd_save_state(&current->thread.fpsimd_state);
  162. preempt_enable();
  163. }
  164. /*
  165. * Load the userland FPSIMD state of 'current' from memory, but only if the
  166. * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
  167. * state of 'current'
  168. */
  169. void fpsimd_restore_current_state(void)
  170. {
  171. preempt_disable();
  172. if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
  173. struct fpsimd_state *st = &current->thread.fpsimd_state;
  174. fpsimd_load_state(st);
  175. this_cpu_write(fpsimd_last_state, st);
  176. st->cpu = smp_processor_id();
  177. }
  178. preempt_enable();
  179. }
  180. /*
  181. * Load an updated userland FPSIMD state for 'current' from memory and set the
  182. * flag that indicates that the FPSIMD register contents are the most recent
  183. * FPSIMD state of 'current'
  184. */
  185. void fpsimd_update_current_state(struct fpsimd_state *state)
  186. {
  187. preempt_disable();
  188. fpsimd_load_state(state);
  189. if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
  190. struct fpsimd_state *st = &current->thread.fpsimd_state;
  191. this_cpu_write(fpsimd_last_state, st);
  192. st->cpu = smp_processor_id();
  193. }
  194. preempt_enable();
  195. }
  196. /*
  197. * Invalidate live CPU copies of task t's FPSIMD state
  198. */
  199. void fpsimd_flush_task_state(struct task_struct *t)
  200. {
  201. t->thread.fpsimd_state.cpu = NR_CPUS;
  202. }
  203. #ifdef CONFIG_KERNEL_MODE_NEON
  204. static DEFINE_PER_CPU(struct fpsimd_partial_state, hardirq_fpsimdstate);
  205. static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate);
  206. /*
  207. * Kernel-side NEON support functions
  208. */
  209. void kernel_neon_begin_partial(u32 num_regs)
  210. {
  211. if (in_interrupt()) {
  212. struct fpsimd_partial_state *s = this_cpu_ptr(
  213. in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
  214. BUG_ON(num_regs > 32);
  215. fpsimd_save_partial_state(s, roundup(num_regs, 2));
  216. } else {
  217. /*
  218. * Save the userland FPSIMD state if we have one and if we
  219. * haven't done so already. Clear fpsimd_last_state to indicate
  220. * that there is no longer userland FPSIMD state in the
  221. * registers.
  222. */
  223. preempt_disable();
  224. if (current->mm &&
  225. !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
  226. fpsimd_save_state(&current->thread.fpsimd_state);
  227. this_cpu_write(fpsimd_last_state, NULL);
  228. }
  229. }
  230. EXPORT_SYMBOL(kernel_neon_begin_partial);
  231. void kernel_neon_end(void)
  232. {
  233. if (in_interrupt()) {
  234. struct fpsimd_partial_state *s = this_cpu_ptr(
  235. in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
  236. fpsimd_load_partial_state(s);
  237. } else {
  238. preempt_enable();
  239. }
  240. }
  241. EXPORT_SYMBOL(kernel_neon_end);
  242. #endif /* CONFIG_KERNEL_MODE_NEON */
  243. #ifdef CONFIG_CPU_PM
  244. static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
  245. unsigned long cmd, void *v)
  246. {
  247. switch (cmd) {
  248. case CPU_PM_ENTER:
  249. if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
  250. fpsimd_save_state(&current->thread.fpsimd_state);
  251. this_cpu_write(fpsimd_last_state, NULL);
  252. break;
  253. case CPU_PM_EXIT:
  254. if (current->mm)
  255. set_thread_flag(TIF_FOREIGN_FPSTATE);
  256. break;
  257. case CPU_PM_ENTER_FAILED:
  258. default:
  259. return NOTIFY_DONE;
  260. }
  261. return NOTIFY_OK;
  262. }
  263. static struct notifier_block fpsimd_cpu_pm_notifier_block = {
  264. .notifier_call = fpsimd_cpu_pm_notifier,
  265. };
  266. static void fpsimd_pm_init(void)
  267. {
  268. cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
  269. }
  270. #else
  271. static inline void fpsimd_pm_init(void) { }
  272. #endif /* CONFIG_CPU_PM */
  273. #ifdef CONFIG_HOTPLUG_CPU
  274. static int fpsimd_cpu_hotplug_notifier(struct notifier_block *nfb,
  275. unsigned long action,
  276. void *hcpu)
  277. {
  278. unsigned int cpu = (long)hcpu;
  279. switch (action) {
  280. case CPU_DEAD:
  281. case CPU_DEAD_FROZEN:
  282. per_cpu(fpsimd_last_state, cpu) = NULL;
  283. break;
  284. }
  285. return NOTIFY_OK;
  286. }
  287. static struct notifier_block fpsimd_cpu_hotplug_notifier_block = {
  288. .notifier_call = fpsimd_cpu_hotplug_notifier,
  289. };
  290. static inline void fpsimd_hotplug_init(void)
  291. {
  292. register_cpu_notifier(&fpsimd_cpu_hotplug_notifier_block);
  293. }
  294. #else
  295. static inline void fpsimd_hotplug_init(void) { }
  296. #endif
  297. /*
  298. * FP/SIMD support code initialisation.
  299. */
  300. static int __init fpsimd_init(void)
  301. {
  302. if (elf_hwcap & HWCAP_FP) {
  303. fpsimd_pm_init();
  304. fpsimd_hotplug_init();
  305. } else {
  306. pr_notice("Floating-point is not implemented\n");
  307. }
  308. if (!(elf_hwcap & HWCAP_ASIMD))
  309. pr_notice("Advanced SIMD is not implemented\n");
  310. return 0;
  311. }
  312. late_initcall(fpsimd_init);