vfpmodule.c 16 KB

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