vfpmodule.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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_null_entry(void);
  27. void (*vfp_vector)(void) = vfp_null_entry;
  28. union vfp_state *last_VFP_context[NR_CPUS];
  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;
  39. __u32 cpu = thread->cpu;
  40. if (likely(cmd == THREAD_NOTIFY_SWITCH)) {
  41. u32 fpexc = fmrx(FPEXC);
  42. #ifdef CONFIG_SMP
  43. /*
  44. * On SMP, if VFP is enabled, save the old state in
  45. * case the thread migrates to a different CPU. The
  46. * restoring is done lazily.
  47. */
  48. if ((fpexc & FPEXC_EN) && last_VFP_context[cpu]) {
  49. vfp_save_state(last_VFP_context[cpu], fpexc);
  50. last_VFP_context[cpu]->hard.cpu = cpu;
  51. }
  52. /*
  53. * Thread migration, just force the reloading of the
  54. * state on the new CPU in case the VFP registers
  55. * contain stale data.
  56. */
  57. if (thread->vfpstate.hard.cpu != cpu)
  58. last_VFP_context[cpu] = NULL;
  59. #endif
  60. /*
  61. * Always disable VFP so we can lazily save/restore the
  62. * old state.
  63. */
  64. fmxr(FPEXC, fpexc & ~FPEXC_EN);
  65. return NOTIFY_DONE;
  66. }
  67. vfp = &thread->vfpstate;
  68. if (cmd == THREAD_NOTIFY_FLUSH) {
  69. /*
  70. * Per-thread VFP initialisation.
  71. */
  72. memset(vfp, 0, sizeof(union vfp_state));
  73. vfp->hard.fpexc = FPEXC_EN;
  74. vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
  75. /*
  76. * Disable VFP to ensure we initialise it first.
  77. */
  78. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  79. }
  80. /* flush and release case: Per-thread VFP cleanup. */
  81. if (last_VFP_context[cpu] == vfp)
  82. last_VFP_context[cpu] = NULL;
  83. return NOTIFY_DONE;
  84. }
  85. static struct notifier_block vfp_notifier_block = {
  86. .notifier_call = vfp_notifier,
  87. };
  88. /*
  89. * Raise a SIGFPE for the current process.
  90. * sicode describes the signal being raised.
  91. */
  92. void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
  93. {
  94. siginfo_t info;
  95. memset(&info, 0, sizeof(info));
  96. info.si_signo = SIGFPE;
  97. info.si_code = sicode;
  98. info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
  99. /*
  100. * This is the same as NWFPE, because it's not clear what
  101. * this is used for
  102. */
  103. current->thread.error_code = 0;
  104. current->thread.trap_no = 6;
  105. send_sig_info(SIGFPE, &info, current);
  106. }
  107. static void vfp_panic(char *reason, u32 inst)
  108. {
  109. int i;
  110. printk(KERN_ERR "VFP: Error: %s\n", reason);
  111. printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
  112. fmrx(FPEXC), fmrx(FPSCR), inst);
  113. for (i = 0; i < 32; i += 2)
  114. printk(KERN_ERR "VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
  115. i, vfp_get_float(i), i+1, vfp_get_float(i+1));
  116. }
  117. /*
  118. * Process bitmask of exception conditions.
  119. */
  120. static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
  121. {
  122. int si_code = 0;
  123. pr_debug("VFP: raising exceptions %08x\n", exceptions);
  124. if (exceptions == VFP_EXCEPTION_ERROR) {
  125. vfp_panic("unhandled bounce", inst);
  126. vfp_raise_sigfpe(0, regs);
  127. return;
  128. }
  129. /*
  130. * Update the FPSCR with the additional exception flags.
  131. * Comparison instructions always return at least one of
  132. * these flags set.
  133. */
  134. fpscr |= exceptions;
  135. fmxr(FPSCR, fpscr);
  136. #define RAISE(stat,en,sig) \
  137. if (exceptions & stat && fpscr & en) \
  138. si_code = sig;
  139. /*
  140. * These are arranged in priority order, least to highest.
  141. */
  142. RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
  143. RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
  144. RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
  145. RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
  146. RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
  147. if (si_code)
  148. vfp_raise_sigfpe(si_code, regs);
  149. }
  150. /*
  151. * Emulate a VFP instruction.
  152. */
  153. static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
  154. {
  155. u32 exceptions = VFP_EXCEPTION_ERROR;
  156. pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
  157. if (INST_CPRTDO(inst)) {
  158. if (!INST_CPRT(inst)) {
  159. /*
  160. * CPDO
  161. */
  162. if (vfp_single(inst)) {
  163. exceptions = vfp_single_cpdo(inst, fpscr);
  164. } else {
  165. exceptions = vfp_double_cpdo(inst, fpscr);
  166. }
  167. } else {
  168. /*
  169. * A CPRT instruction can not appear in FPINST2, nor
  170. * can it cause an exception. Therefore, we do not
  171. * have to emulate it.
  172. */
  173. }
  174. } else {
  175. /*
  176. * A CPDT instruction can not appear in FPINST2, nor can
  177. * it cause an exception. Therefore, we do not have to
  178. * emulate it.
  179. */
  180. }
  181. return exceptions & ~VFP_NAN_FLAG;
  182. }
  183. /*
  184. * Package up a bounce condition.
  185. */
  186. void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
  187. {
  188. u32 fpscr, orig_fpscr, fpsid, exceptions;
  189. pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
  190. /*
  191. * At this point, FPEXC can have the following configuration:
  192. *
  193. * EX DEX IXE
  194. * 0 1 x - synchronous exception
  195. * 1 x 0 - asynchronous exception
  196. * 1 x 1 - sychronous on VFP subarch 1 and asynchronous on later
  197. * 0 0 1 - synchronous on VFP9 (non-standard subarch 1
  198. * implementation), undefined otherwise
  199. *
  200. * Clear various bits and enable access to the VFP so we can
  201. * handle the bounce.
  202. */
  203. fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
  204. fpsid = fmrx(FPSID);
  205. orig_fpscr = fpscr = fmrx(FPSCR);
  206. /*
  207. * Check for the special VFP subarch 1 and FPSCR.IXE bit case
  208. */
  209. if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
  210. && (fpscr & FPSCR_IXE)) {
  211. /*
  212. * Synchronous exception, emulate the trigger instruction
  213. */
  214. goto emulate;
  215. }
  216. if (fpexc & FPEXC_EX) {
  217. /*
  218. * Asynchronous exception. The instruction is read from FPINST
  219. * and the interrupted instruction has to be restarted.
  220. */
  221. trigger = fmrx(FPINST);
  222. regs->ARM_pc -= 4;
  223. } else if (!(fpexc & FPEXC_DEX)) {
  224. /*
  225. * Illegal combination of bits. It can be caused by an
  226. * unallocated VFP instruction but with FPSCR.IXE set and not
  227. * on VFP subarch 1.
  228. */
  229. vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
  230. goto exit;
  231. }
  232. /*
  233. * Modify fpscr to indicate the number of iterations remaining.
  234. * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
  235. * whether FPEXC.VECITR or FPSCR.LEN is used.
  236. */
  237. if (fpexc & (FPEXC_EX | FPEXC_VV)) {
  238. u32 len;
  239. len = fpexc + (1 << FPEXC_LENGTH_BIT);
  240. fpscr &= ~FPSCR_LENGTH_MASK;
  241. fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
  242. }
  243. /*
  244. * Handle the first FP instruction. We used to take note of the
  245. * FPEXC bounce reason, but this appears to be unreliable.
  246. * Emulate the bounced instruction instead.
  247. */
  248. exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
  249. if (exceptions)
  250. vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
  251. /*
  252. * If there isn't a second FP instruction, exit now. Note that
  253. * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
  254. */
  255. if (fpexc ^ (FPEXC_EX | FPEXC_FP2V))
  256. goto exit;
  257. /*
  258. * The barrier() here prevents fpinst2 being read
  259. * before the condition above.
  260. */
  261. barrier();
  262. trigger = fmrx(FPINST2);
  263. emulate:
  264. exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
  265. if (exceptions)
  266. vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
  267. exit:
  268. preempt_enable();
  269. }
  270. static void vfp_enable(void *unused)
  271. {
  272. u32 access = get_copro_access();
  273. /*
  274. * Enable full access to VFP (cp10 and cp11)
  275. */
  276. set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
  277. }
  278. #ifdef CONFIG_PM
  279. #include <linux/sysdev.h>
  280. static int vfp_pm_suspend(struct sys_device *dev, pm_message_t state)
  281. {
  282. struct thread_info *ti = current_thread_info();
  283. u32 fpexc = fmrx(FPEXC);
  284. /* if vfp is on, then save state for resumption */
  285. if (fpexc & FPEXC_EN) {
  286. printk(KERN_DEBUG "%s: saving vfp state\n", __func__);
  287. vfp_save_state(&ti->vfpstate, fpexc);
  288. /* disable, just in case */
  289. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  290. }
  291. /* clear any information we had about last context state */
  292. memset(last_VFP_context, 0, sizeof(last_VFP_context));
  293. return 0;
  294. }
  295. static int vfp_pm_resume(struct sys_device *dev)
  296. {
  297. /* ensure we have access to the vfp */
  298. vfp_enable(NULL);
  299. /* and disable it to ensure the next usage restores the state */
  300. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  301. return 0;
  302. }
  303. static struct sysdev_class vfp_pm_sysclass = {
  304. .name = "vfp",
  305. .suspend = vfp_pm_suspend,
  306. .resume = vfp_pm_resume,
  307. };
  308. static struct sys_device vfp_pm_sysdev = {
  309. .cls = &vfp_pm_sysclass,
  310. };
  311. static void vfp_pm_init(void)
  312. {
  313. sysdev_class_register(&vfp_pm_sysclass);
  314. sysdev_register(&vfp_pm_sysdev);
  315. }
  316. #else
  317. static inline void vfp_pm_init(void) { }
  318. #endif /* CONFIG_PM */
  319. /*
  320. * Synchronise the hardware VFP state of a thread other than current with the
  321. * saved one. This function is used by the ptrace mechanism.
  322. */
  323. #ifdef CONFIG_SMP
  324. void vfp_sync_state(struct thread_info *thread)
  325. {
  326. /*
  327. * On SMP systems, the VFP state is automatically saved at every
  328. * context switch. We mark the thread VFP state as belonging to a
  329. * non-existent CPU so that the saved one will be reloaded when
  330. * needed.
  331. */
  332. thread->vfpstate.hard.cpu = NR_CPUS;
  333. }
  334. #else
  335. void vfp_sync_state(struct thread_info *thread)
  336. {
  337. unsigned int cpu = get_cpu();
  338. u32 fpexc = fmrx(FPEXC);
  339. /*
  340. * If VFP is enabled, the previous state was already saved and
  341. * last_VFP_context updated.
  342. */
  343. if (fpexc & FPEXC_EN)
  344. goto out;
  345. if (!last_VFP_context[cpu])
  346. goto out;
  347. /*
  348. * Save the last VFP state on this CPU.
  349. */
  350. fmxr(FPEXC, fpexc | FPEXC_EN);
  351. vfp_save_state(last_VFP_context[cpu], fpexc);
  352. fmxr(FPEXC, fpexc);
  353. /*
  354. * Set the context to NULL to force a reload the next time the thread
  355. * uses the VFP.
  356. */
  357. last_VFP_context[cpu] = NULL;
  358. out:
  359. put_cpu();
  360. }
  361. #endif
  362. #include <linux/smp.h>
  363. /*
  364. * VFP support code initialisation.
  365. */
  366. static int __init vfp_init(void)
  367. {
  368. unsigned int vfpsid;
  369. unsigned int cpu_arch = cpu_architecture();
  370. if (cpu_arch >= CPU_ARCH_ARMv6)
  371. vfp_enable(NULL);
  372. /*
  373. * First check that there is a VFP that we can use.
  374. * The handler is already setup to just log calls, so
  375. * we just need to read the VFPSID register.
  376. */
  377. vfp_vector = vfp_testing_entry;
  378. barrier();
  379. vfpsid = fmrx(FPSID);
  380. barrier();
  381. vfp_vector = vfp_null_entry;
  382. printk(KERN_INFO "VFP support v0.3: ");
  383. if (VFP_arch)
  384. printk("not present\n");
  385. else if (vfpsid & FPSID_NODOUBLE) {
  386. printk("no double precision support\n");
  387. } else {
  388. smp_call_function(vfp_enable, NULL, 1);
  389. VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT; /* Extract the architecture version */
  390. printk("implementor %02x architecture %d part %02x variant %x rev %x\n",
  391. (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
  392. (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT,
  393. (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
  394. (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
  395. (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
  396. vfp_vector = vfp_support_entry;
  397. thread_register_notifier(&vfp_notifier_block);
  398. vfp_pm_init();
  399. /*
  400. * We detected VFP, and the support code is
  401. * in place; report VFP support to userspace.
  402. */
  403. elf_hwcap |= HWCAP_VFP;
  404. #ifdef CONFIG_VFPv3
  405. if (VFP_arch >= 3) {
  406. elf_hwcap |= HWCAP_VFPv3;
  407. /*
  408. * Check for VFPv3 D16. CPUs in this configuration
  409. * only have 16 x 64bit registers.
  410. */
  411. if (((fmrx(MVFR0) & MVFR0_A_SIMD_MASK)) == 1)
  412. elf_hwcap |= HWCAP_VFPv3D16;
  413. }
  414. #endif
  415. #ifdef CONFIG_NEON
  416. /*
  417. * Check for the presence of the Advanced SIMD
  418. * load/store instructions, integer and single
  419. * precision floating point operations.
  420. */
  421. if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100)
  422. elf_hwcap |= HWCAP_NEON;
  423. #endif
  424. }
  425. return 0;
  426. }
  427. late_initcall(vfp_init);