fpsimd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/kernel.h>
  20. #include <linux/init.h>
  21. #include <linux/sched.h>
  22. #include <linux/signal.h>
  23. #include <linux/hardirq.h>
  24. #include <asm/fpsimd.h>
  25. #include <asm/cputype.h>
  26. #define FPEXC_IOF (1 << 0)
  27. #define FPEXC_DZF (1 << 1)
  28. #define FPEXC_OFF (1 << 2)
  29. #define FPEXC_UFF (1 << 3)
  30. #define FPEXC_IXF (1 << 4)
  31. #define FPEXC_IDF (1 << 7)
  32. /*
  33. * Trapped FP/ASIMD access.
  34. */
  35. void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
  36. {
  37. /* TODO: implement lazy context saving/restoring */
  38. WARN_ON(1);
  39. }
  40. /*
  41. * Raise a SIGFPE for the current process.
  42. */
  43. void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
  44. {
  45. siginfo_t info;
  46. unsigned int si_code = 0;
  47. if (esr & FPEXC_IOF)
  48. si_code = FPE_FLTINV;
  49. else if (esr & FPEXC_DZF)
  50. si_code = FPE_FLTDIV;
  51. else if (esr & FPEXC_OFF)
  52. si_code = FPE_FLTOVF;
  53. else if (esr & FPEXC_UFF)
  54. si_code = FPE_FLTUND;
  55. else if (esr & FPEXC_IXF)
  56. si_code = FPE_FLTRES;
  57. memset(&info, 0, sizeof(info));
  58. info.si_signo = SIGFPE;
  59. info.si_code = si_code;
  60. info.si_addr = (void __user *)instruction_pointer(regs);
  61. send_sig_info(SIGFPE, &info, current);
  62. }
  63. void fpsimd_thread_switch(struct task_struct *next)
  64. {
  65. /* check if not kernel threads */
  66. if (current->mm)
  67. fpsimd_save_state(&current->thread.fpsimd_state);
  68. if (next->mm)
  69. fpsimd_load_state(&next->thread.fpsimd_state);
  70. }
  71. void fpsimd_flush_thread(void)
  72. {
  73. memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
  74. fpsimd_load_state(&current->thread.fpsimd_state);
  75. }
  76. #ifdef CONFIG_KERNEL_MODE_NEON
  77. /*
  78. * Kernel-side NEON support functions
  79. */
  80. void kernel_neon_begin(void)
  81. {
  82. /* Avoid using the NEON in interrupt context */
  83. BUG_ON(in_interrupt());
  84. preempt_disable();
  85. if (current->mm)
  86. fpsimd_save_state(&current->thread.fpsimd_state);
  87. }
  88. EXPORT_SYMBOL(kernel_neon_begin);
  89. void kernel_neon_end(void)
  90. {
  91. if (current->mm)
  92. fpsimd_load_state(&current->thread.fpsimd_state);
  93. preempt_enable();
  94. }
  95. EXPORT_SYMBOL(kernel_neon_end);
  96. #endif /* CONFIG_KERNEL_MODE_NEON */
  97. /*
  98. * FP/SIMD support code initialisation.
  99. */
  100. static int __init fpsimd_init(void)
  101. {
  102. u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
  103. if (pfr & (0xf << 16)) {
  104. pr_notice("Floating-point is not implemented\n");
  105. return 0;
  106. }
  107. elf_hwcap |= HWCAP_FP;
  108. if (pfr & (0xf << 20))
  109. pr_notice("Advanced SIMD is not implemented\n");
  110. else
  111. elf_hwcap |= HWCAP_ASIMD;
  112. return 0;
  113. }
  114. late_initcall(fpsimd_init);