vfpmodule.c 7.2 KB

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