vfpmodule.c 6.6 KB

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