vfpmodule.c 15 KB

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