vfpmodule.c 13 KB

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