vfpmodule.c 13 KB

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