vfpmodule.c 16 KB

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