vfpmodule.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * linux/arch/arm/vfp/vfpmodule.c
  3. *
  4. * Copyright (C) 2004 ARM Limited.
  5. * Written by Deep Blue Solutions Limited.
  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. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/signal.h>
  15. #include <linux/sched.h>
  16. #include <linux/init.h>
  17. #include <asm/thread_notify.h>
  18. #include <asm/vfp.h>
  19. #include "vfpinstr.h"
  20. #include "vfp.h"
  21. /*
  22. * Our undef handlers (in entry.S)
  23. */
  24. void vfp_testing_entry(void);
  25. void vfp_support_entry(void);
  26. void (*vfp_vector)(void) = vfp_testing_entry;
  27. union vfp_state *last_VFP_context;
  28. /*
  29. * Dual-use variable.
  30. * Used in startup: set to non-zero if VFP checks fail
  31. * After startup, holds VFP architecture
  32. */
  33. unsigned int VFP_arch;
  34. static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  35. {
  36. struct thread_info *thread = v;
  37. union vfp_state *vfp;
  38. if (likely(cmd == THREAD_NOTIFY_SWITCH)) {
  39. /*
  40. * Always disable VFP so we can lazily save/restore the
  41. * old state.
  42. */
  43. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_ENABLE);
  44. return NOTIFY_DONE;
  45. }
  46. vfp = &thread->vfpstate;
  47. if (cmd == THREAD_NOTIFY_FLUSH) {
  48. /*
  49. * Per-thread VFP initialisation.
  50. */
  51. memset(vfp, 0, sizeof(union vfp_state));
  52. vfp->hard.fpexc = FPEXC_ENABLE;
  53. vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
  54. /*
  55. * Disable VFP to ensure we initialise it first.
  56. */
  57. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_ENABLE);
  58. }
  59. /* flush and release case: Per-thread VFP cleanup. */
  60. if (last_VFP_context == vfp)
  61. last_VFP_context = NULL;
  62. return NOTIFY_DONE;
  63. }
  64. static struct notifier_block vfp_notifier_block = {
  65. .notifier_call = vfp_notifier,
  66. };
  67. /*
  68. * Raise a SIGFPE for the current process.
  69. * sicode describes the signal being raised.
  70. */
  71. void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
  72. {
  73. siginfo_t info;
  74. memset(&info, 0, sizeof(info));
  75. info.si_signo = SIGFPE;
  76. info.si_code = sicode;
  77. info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
  78. /*
  79. * This is the same as NWFPE, because it's not clear what
  80. * this is used for
  81. */
  82. current->thread.error_code = 0;
  83. current->thread.trap_no = 6;
  84. send_sig_info(SIGFPE, &info, current);
  85. }
  86. static void vfp_panic(char *reason)
  87. {
  88. int i;
  89. printk(KERN_ERR "VFP: Error: %s\n", reason);
  90. printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
  91. fmrx(FPEXC), fmrx(FPSCR), fmrx(FPINST));
  92. for (i = 0; i < 32; i += 2)
  93. printk(KERN_ERR "VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
  94. i, vfp_get_float(i), i+1, vfp_get_float(i+1));
  95. }
  96. /*
  97. * Process bitmask of exception conditions.
  98. */
  99. static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
  100. {
  101. int si_code = 0;
  102. pr_debug("VFP: raising exceptions %08x\n", exceptions);
  103. if (exceptions == VFP_EXCEPTION_ERROR) {
  104. vfp_panic("unhandled bounce");
  105. vfp_raise_sigfpe(0, regs);
  106. return;
  107. }
  108. /*
  109. * If any of the status flags are set, update the FPSCR.
  110. * Comparison instructions always return at least one of
  111. * these flags set.
  112. */
  113. if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  114. fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
  115. fpscr |= exceptions;
  116. fmxr(FPSCR, fpscr);
  117. #define RAISE(stat,en,sig) \
  118. if (exceptions & stat && fpscr & en) \
  119. si_code = sig;
  120. /*
  121. * These are arranged in priority order, least to highest.
  122. */
  123. RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
  124. RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
  125. RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
  126. RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
  127. RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
  128. if (si_code)
  129. vfp_raise_sigfpe(si_code, regs);
  130. }
  131. /*
  132. * Emulate a VFP instruction.
  133. */
  134. static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
  135. {
  136. u32 exceptions = VFP_EXCEPTION_ERROR;
  137. pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
  138. if (INST_CPRTDO(inst)) {
  139. if (!INST_CPRT(inst)) {
  140. /*
  141. * CPDO
  142. */
  143. if (vfp_single(inst)) {
  144. exceptions = vfp_single_cpdo(inst, fpscr);
  145. } else {
  146. exceptions = vfp_double_cpdo(inst, fpscr);
  147. }
  148. } else {
  149. /*
  150. * A CPRT instruction can not appear in FPINST2, nor
  151. * can it cause an exception. Therefore, we do not
  152. * have to emulate it.
  153. */
  154. }
  155. } else {
  156. /*
  157. * A CPDT instruction can not appear in FPINST2, nor can
  158. * it cause an exception. Therefore, we do not have to
  159. * emulate it.
  160. */
  161. }
  162. return exceptions & ~VFP_NAN_FLAG;
  163. }
  164. /*
  165. * Package up a bounce condition.
  166. */
  167. void VFP9_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
  168. {
  169. u32 fpscr, orig_fpscr, exceptions, inst;
  170. pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
  171. /*
  172. * Enable access to the VFP so we can handle the bounce.
  173. */
  174. fmxr(FPEXC, fpexc & ~(FPEXC_EXCEPTION|FPEXC_INV|FPEXC_UFC|FPEXC_IOC));
  175. orig_fpscr = fpscr = fmrx(FPSCR);
  176. /*
  177. * If we are running with inexact exceptions enabled, we need to
  178. * emulate the trigger instruction. Note that as we're emulating
  179. * the trigger instruction, we need to increment PC.
  180. */
  181. if (fpscr & FPSCR_IXE) {
  182. regs->ARM_pc += 4;
  183. goto emulate;
  184. }
  185. barrier();
  186. /*
  187. * Modify fpscr to indicate the number of iterations remaining
  188. */
  189. if (fpexc & FPEXC_EXCEPTION) {
  190. u32 len;
  191. len = fpexc + (1 << FPEXC_LENGTH_BIT);
  192. fpscr &= ~FPSCR_LENGTH_MASK;
  193. fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
  194. }
  195. /*
  196. * Handle the first FP instruction. We used to take note of the
  197. * FPEXC bounce reason, but this appears to be unreliable.
  198. * Emulate the bounced instruction instead.
  199. */
  200. inst = fmrx(FPINST);
  201. exceptions = vfp_emulate_instruction(inst, fpscr, regs);
  202. if (exceptions)
  203. vfp_raise_exceptions(exceptions, inst, orig_fpscr, regs);
  204. /*
  205. * If there isn't a second FP instruction, exit now.
  206. */
  207. if (!(fpexc & FPEXC_FPV2))
  208. return;
  209. /*
  210. * The barrier() here prevents fpinst2 being read
  211. * before the condition above.
  212. */
  213. barrier();
  214. trigger = fmrx(FPINST2);
  215. orig_fpscr = fpscr = fmrx(FPSCR);
  216. emulate:
  217. exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
  218. if (exceptions)
  219. vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
  220. }
  221. /*
  222. * VFP support code initialisation.
  223. */
  224. static int __init vfp_init(void)
  225. {
  226. unsigned int vfpsid;
  227. /*
  228. * First check that there is a VFP that we can use.
  229. * The handler is already setup to just log calls, so
  230. * we just need to read the VFPSID register.
  231. */
  232. vfpsid = fmrx(FPSID);
  233. printk(KERN_INFO "VFP support v0.3: ");
  234. if (VFP_arch) {
  235. printk("not present\n");
  236. } else if (vfpsid & FPSID_NODOUBLE) {
  237. printk("no double precision support\n");
  238. } else {
  239. VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT; /* Extract the architecture version */
  240. printk("implementor %02x architecture %d part %02x variant %x rev %x\n",
  241. (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
  242. (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT,
  243. (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
  244. (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
  245. (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
  246. vfp_vector = vfp_support_entry;
  247. thread_register_notifier(&vfp_notifier_block);
  248. }
  249. return 0;
  250. }
  251. late_initcall(vfp_init);