vfpmodule.c 15 KB

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