vfpmodule.c 15 KB

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