fpsimd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
  148. set_thread_flag(TIF_FOREIGN_FPSTATE);
  149. }
  150. /*
  151. * Save the userland FPSIMD state of 'current' to memory, but only if the state
  152. * currently held in the registers does in fact belong to 'current'
  153. */
  154. void fpsimd_preserve_current_state(void)
  155. {
  156. preempt_disable();
  157. if (!test_thread_flag(TIF_FOREIGN_FPSTATE))
  158. fpsimd_save_state(&current->thread.fpsimd_state);
  159. preempt_enable();
  160. }
  161. /*
  162. * Load the userland FPSIMD state of 'current' from memory, but only if the
  163. * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
  164. * state of 'current'
  165. */
  166. void fpsimd_restore_current_state(void)
  167. {
  168. preempt_disable();
  169. if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
  170. struct fpsimd_state *st = &current->thread.fpsimd_state;
  171. fpsimd_load_state(st);
  172. this_cpu_write(fpsimd_last_state, st);
  173. st->cpu = smp_processor_id();
  174. }
  175. preempt_enable();
  176. }
  177. /*
  178. * Load an updated userland FPSIMD state for 'current' from memory and set the
  179. * flag that indicates that the FPSIMD register contents are the most recent
  180. * FPSIMD state of 'current'
  181. */
  182. void fpsimd_update_current_state(struct fpsimd_state *state)
  183. {
  184. preempt_disable();
  185. fpsimd_load_state(state);
  186. if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
  187. struct fpsimd_state *st = &current->thread.fpsimd_state;
  188. this_cpu_write(fpsimd_last_state, st);
  189. st->cpu = smp_processor_id();
  190. }
  191. preempt_enable();
  192. }
  193. /*
  194. * Invalidate live CPU copies of task t's FPSIMD state
  195. */
  196. void fpsimd_flush_task_state(struct task_struct *t)
  197. {
  198. t->thread.fpsimd_state.cpu = NR_CPUS;
  199. }
  200. #ifdef CONFIG_KERNEL_MODE_NEON
  201. static DEFINE_PER_CPU(struct fpsimd_partial_state, hardirq_fpsimdstate);
  202. static DEFINE_PER_CPU(struct fpsimd_partial_state, softirq_fpsimdstate);
  203. /*
  204. * Kernel-side NEON support functions
  205. */
  206. void kernel_neon_begin_partial(u32 num_regs)
  207. {
  208. if (in_interrupt()) {
  209. struct fpsimd_partial_state *s = this_cpu_ptr(
  210. in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
  211. BUG_ON(num_regs > 32);
  212. fpsimd_save_partial_state(s, roundup(num_regs, 2));
  213. } else {
  214. /*
  215. * Save the userland FPSIMD state if we have one and if we
  216. * haven't done so already. Clear fpsimd_last_state to indicate
  217. * that there is no longer userland FPSIMD state in the
  218. * registers.
  219. */
  220. preempt_disable();
  221. if (current->mm &&
  222. !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE))
  223. fpsimd_save_state(&current->thread.fpsimd_state);
  224. this_cpu_write(fpsimd_last_state, NULL);
  225. }
  226. }
  227. EXPORT_SYMBOL(kernel_neon_begin_partial);
  228. void kernel_neon_end(void)
  229. {
  230. if (in_interrupt()) {
  231. struct fpsimd_partial_state *s = this_cpu_ptr(
  232. in_irq() ? &hardirq_fpsimdstate : &softirq_fpsimdstate);
  233. fpsimd_load_partial_state(s);
  234. } else {
  235. preempt_enable();
  236. }
  237. }
  238. EXPORT_SYMBOL(kernel_neon_end);
  239. #endif /* CONFIG_KERNEL_MODE_NEON */
  240. #ifdef CONFIG_CPU_PM
  241. static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
  242. unsigned long cmd, void *v)
  243. {
  244. switch (cmd) {
  245. case CPU_PM_ENTER:
  246. if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE))
  247. fpsimd_save_state(&current->thread.fpsimd_state);
  248. this_cpu_write(fpsimd_last_state, NULL);
  249. break;
  250. case CPU_PM_EXIT:
  251. if (current->mm)
  252. set_thread_flag(TIF_FOREIGN_FPSTATE);
  253. break;
  254. case CPU_PM_ENTER_FAILED:
  255. default:
  256. return NOTIFY_DONE;
  257. }
  258. return NOTIFY_OK;
  259. }
  260. static struct notifier_block fpsimd_cpu_pm_notifier_block = {
  261. .notifier_call = fpsimd_cpu_pm_notifier,
  262. };
  263. static void fpsimd_pm_init(void)
  264. {
  265. cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
  266. }
  267. #else
  268. static inline void fpsimd_pm_init(void) { }
  269. #endif /* CONFIG_CPU_PM */
  270. #ifdef CONFIG_HOTPLUG_CPU
  271. static int fpsimd_cpu_hotplug_notifier(struct notifier_block *nfb,
  272. unsigned long action,
  273. void *hcpu)
  274. {
  275. unsigned int cpu = (long)hcpu;
  276. switch (action) {
  277. case CPU_DEAD:
  278. case CPU_DEAD_FROZEN:
  279. per_cpu(fpsimd_last_state, cpu) = NULL;
  280. break;
  281. }
  282. return NOTIFY_OK;
  283. }
  284. static struct notifier_block fpsimd_cpu_hotplug_notifier_block = {
  285. .notifier_call = fpsimd_cpu_hotplug_notifier,
  286. };
  287. static inline void fpsimd_hotplug_init(void)
  288. {
  289. register_cpu_notifier(&fpsimd_cpu_hotplug_notifier_block);
  290. }
  291. #else
  292. static inline void fpsimd_hotplug_init(void) { }
  293. #endif
  294. /*
  295. * FP/SIMD support code initialisation.
  296. */
  297. static int __init fpsimd_init(void)
  298. {
  299. u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
  300. if (pfr & (0xf << 16)) {
  301. pr_notice("Floating-point is not implemented\n");
  302. return 0;
  303. }
  304. elf_hwcap |= HWCAP_FP;
  305. if (pfr & (0xf << 20))
  306. pr_notice("Advanced SIMD is not implemented\n");
  307. else
  308. elf_hwcap |= HWCAP_ASIMD;
  309. fpsimd_pm_init();
  310. fpsimd_hotplug_init();
  311. return 0;
  312. }
  313. late_initcall(fpsimd_init);