fpsimd.c 9.6 KB

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