vfpmodule.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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/hardirq.h>
  15. #include <linux/kernel.h>
  16. #include <linux/notifier.h>
  17. #include <linux/signal.h>
  18. #include <linux/sched.h>
  19. #include <linux/smp.h>
  20. #include <linux/init.h>
  21. #include <linux/uaccess.h>
  22. #include <linux/user.h>
  23. #include <linux/export.h>
  24. #include <asm/cp15.h>
  25. #include <asm/cputype.h>
  26. #include <asm/system_info.h>
  27. #include <asm/thread_notify.h>
  28. #include <asm/vfp.h>
  29. #include "vfpinstr.h"
  30. #include "vfp.h"
  31. /*
  32. * Our undef handlers (in entry.S)
  33. */
  34. void vfp_testing_entry(void);
  35. void vfp_support_entry(void);
  36. void vfp_null_entry(void);
  37. void (*vfp_vector)(void) = vfp_null_entry;
  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. * The pointer to the vfpstate structure of the thread which currently
  46. * owns the context held in the VFP hardware, or NULL if the hardware
  47. * context is invalid.
  48. *
  49. * For UP, this is sufficient to tell which thread owns the VFP context.
  50. * However, for SMP, we also need to check the CPU number stored in the
  51. * saved state too to catch migrations.
  52. */
  53. union vfp_state *vfp_current_hw_state[NR_CPUS];
  54. /*
  55. * Is 'thread's most up to date state stored in this CPUs hardware?
  56. * Must be called from non-preemptible context.
  57. */
  58. static bool vfp_state_in_hw(unsigned int cpu, struct thread_info *thread)
  59. {
  60. #ifdef CONFIG_SMP
  61. if (thread->vfpstate.hard.cpu != cpu)
  62. return false;
  63. #endif
  64. return vfp_current_hw_state[cpu] == &thread->vfpstate;
  65. }
  66. /*
  67. * Force a reload of the VFP context from the thread structure. We do
  68. * this by ensuring that access to the VFP hardware is disabled, and
  69. * clear vfp_current_hw_state. Must be called from non-preemptible context.
  70. */
  71. static void vfp_force_reload(unsigned int cpu, struct thread_info *thread)
  72. {
  73. if (vfp_state_in_hw(cpu, thread)) {
  74. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  75. vfp_current_hw_state[cpu] = NULL;
  76. }
  77. #ifdef CONFIG_SMP
  78. thread->vfpstate.hard.cpu = NR_CPUS;
  79. #endif
  80. }
  81. /*
  82. * Per-thread VFP initialization.
  83. */
  84. static void vfp_thread_flush(struct thread_info *thread)
  85. {
  86. union vfp_state *vfp = &thread->vfpstate;
  87. unsigned int cpu;
  88. /*
  89. * Disable VFP to ensure we initialize it first. We must ensure
  90. * that the modification of vfp_current_hw_state[] and hardware
  91. * disable are done for the same CPU and without preemption.
  92. *
  93. * Do this first to ensure that preemption won't overwrite our
  94. * state saving should access to the VFP be enabled at this point.
  95. */
  96. cpu = get_cpu();
  97. if (vfp_current_hw_state[cpu] == vfp)
  98. vfp_current_hw_state[cpu] = NULL;
  99. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  100. put_cpu();
  101. memset(vfp, 0, sizeof(union vfp_state));
  102. vfp->hard.fpexc = FPEXC_EN;
  103. vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
  104. #ifdef CONFIG_SMP
  105. vfp->hard.cpu = NR_CPUS;
  106. #endif
  107. }
  108. static void vfp_thread_exit(struct thread_info *thread)
  109. {
  110. /* release case: Per-thread VFP cleanup. */
  111. union vfp_state *vfp = &thread->vfpstate;
  112. unsigned int cpu = get_cpu();
  113. if (vfp_current_hw_state[cpu] == vfp)
  114. vfp_current_hw_state[cpu] = NULL;
  115. put_cpu();
  116. }
  117. static void vfp_thread_copy(struct thread_info *thread)
  118. {
  119. struct thread_info *parent = current_thread_info();
  120. vfp_sync_hwstate(parent);
  121. thread->vfpstate = parent->vfpstate;
  122. #ifdef CONFIG_SMP
  123. thread->vfpstate.hard.cpu = NR_CPUS;
  124. #endif
  125. }
  126. /*
  127. * When this function is called with the following 'cmd's, the following
  128. * is true while this function is being run:
  129. * THREAD_NOFTIFY_SWTICH:
  130. * - the previously running thread will not be scheduled onto another CPU.
  131. * - the next thread to be run (v) will not be running on another CPU.
  132. * - thread->cpu is the local CPU number
  133. * - not preemptible as we're called in the middle of a thread switch
  134. * THREAD_NOTIFY_FLUSH:
  135. * - the thread (v) will be running on the local CPU, so
  136. * v === current_thread_info()
  137. * - thread->cpu is the local CPU number at the time it is accessed,
  138. * but may change at any time.
  139. * - we could be preempted if tree preempt rcu is enabled, so
  140. * it is unsafe to use thread->cpu.
  141. * THREAD_NOTIFY_EXIT
  142. * - the thread (v) will be running on the local CPU, so
  143. * v === current_thread_info()
  144. * - thread->cpu is the local CPU number at the time it is accessed,
  145. * but may change at any time.
  146. * - we could be preempted if tree preempt rcu is enabled, so
  147. * it is unsafe to use thread->cpu.
  148. */
  149. static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  150. {
  151. struct thread_info *thread = v;
  152. u32 fpexc;
  153. #ifdef CONFIG_SMP
  154. unsigned int cpu;
  155. #endif
  156. switch (cmd) {
  157. case THREAD_NOTIFY_SWITCH:
  158. fpexc = fmrx(FPEXC);
  159. #ifdef CONFIG_SMP
  160. cpu = thread->cpu;
  161. /*
  162. * On SMP, if VFP is enabled, save the old state in
  163. * case the thread migrates to a different CPU. The
  164. * restoring is done lazily.
  165. */
  166. if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu])
  167. vfp_save_state(vfp_current_hw_state[cpu], fpexc);
  168. #endif
  169. /*
  170. * Always disable VFP so we can lazily save/restore the
  171. * old state.
  172. */
  173. fmxr(FPEXC, fpexc & ~FPEXC_EN);
  174. break;
  175. case THREAD_NOTIFY_FLUSH:
  176. vfp_thread_flush(thread);
  177. break;
  178. case THREAD_NOTIFY_EXIT:
  179. vfp_thread_exit(thread);
  180. break;
  181. case THREAD_NOTIFY_COPY:
  182. vfp_thread_copy(thread);
  183. break;
  184. }
  185. return NOTIFY_DONE;
  186. }
  187. static struct notifier_block vfp_notifier_block = {
  188. .notifier_call = vfp_notifier,
  189. };
  190. /*
  191. * Raise a SIGFPE for the current process.
  192. * sicode describes the signal being raised.
  193. */
  194. static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
  195. {
  196. siginfo_t info;
  197. memset(&info, 0, sizeof(info));
  198. info.si_signo = SIGFPE;
  199. info.si_code = sicode;
  200. info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
  201. /*
  202. * This is the same as NWFPE, because it's not clear what
  203. * this is used for
  204. */
  205. current->thread.error_code = 0;
  206. current->thread.trap_no = 6;
  207. send_sig_info(SIGFPE, &info, current);
  208. }
  209. static void vfp_panic(char *reason, u32 inst)
  210. {
  211. int i;
  212. pr_err("VFP: Error: %s\n", reason);
  213. pr_err("VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
  214. fmrx(FPEXC), fmrx(FPSCR), inst);
  215. for (i = 0; i < 32; i += 2)
  216. pr_err("VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
  217. i, vfp_get_float(i), i+1, vfp_get_float(i+1));
  218. }
  219. /*
  220. * Process bitmask of exception conditions.
  221. */
  222. static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
  223. {
  224. int si_code = 0;
  225. pr_debug("VFP: raising exceptions %08x\n", exceptions);
  226. if (exceptions == VFP_EXCEPTION_ERROR) {
  227. vfp_panic("unhandled bounce", inst);
  228. vfp_raise_sigfpe(0, regs);
  229. return;
  230. }
  231. /*
  232. * If any of the status flags are set, update the FPSCR.
  233. * Comparison instructions always return at least one of
  234. * these flags set.
  235. */
  236. if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  237. fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
  238. fpscr |= exceptions;
  239. fmxr(FPSCR, fpscr);
  240. #define RAISE(stat,en,sig) \
  241. if (exceptions & stat && fpscr & en) \
  242. si_code = sig;
  243. /*
  244. * These are arranged in priority order, least to highest.
  245. */
  246. RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
  247. RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
  248. RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
  249. RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
  250. RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
  251. if (si_code)
  252. vfp_raise_sigfpe(si_code, regs);
  253. }
  254. /*
  255. * Emulate a VFP instruction.
  256. */
  257. static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
  258. {
  259. u32 exceptions = VFP_EXCEPTION_ERROR;
  260. pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
  261. if (INST_CPRTDO(inst)) {
  262. if (!INST_CPRT(inst)) {
  263. /*
  264. * CPDO
  265. */
  266. if (vfp_single(inst)) {
  267. exceptions = vfp_single_cpdo(inst, fpscr);
  268. } else {
  269. exceptions = vfp_double_cpdo(inst, fpscr);
  270. }
  271. } else {
  272. /*
  273. * A CPRT instruction can not appear in FPINST2, nor
  274. * can it cause an exception. Therefore, we do not
  275. * have to emulate it.
  276. */
  277. }
  278. } else {
  279. /*
  280. * A CPDT instruction can not appear in FPINST2, nor can
  281. * it cause an exception. Therefore, we do not have to
  282. * emulate it.
  283. */
  284. }
  285. return exceptions & ~VFP_NAN_FLAG;
  286. }
  287. /*
  288. * Package up a bounce condition.
  289. */
  290. void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
  291. {
  292. u32 fpscr, orig_fpscr, fpsid, exceptions;
  293. pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
  294. /*
  295. * At this point, FPEXC can have the following configuration:
  296. *
  297. * EX DEX IXE
  298. * 0 1 x - synchronous exception
  299. * 1 x 0 - asynchronous exception
  300. * 1 x 1 - sychronous on VFP subarch 1 and asynchronous on later
  301. * 0 0 1 - synchronous on VFP9 (non-standard subarch 1
  302. * implementation), undefined otherwise
  303. *
  304. * Clear various bits and enable access to the VFP so we can
  305. * handle the bounce.
  306. */
  307. fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
  308. fpsid = fmrx(FPSID);
  309. orig_fpscr = fpscr = fmrx(FPSCR);
  310. /*
  311. * Check for the special VFP subarch 1 and FPSCR.IXE bit case
  312. */
  313. if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
  314. && (fpscr & FPSCR_IXE)) {
  315. /*
  316. * Synchronous exception, emulate the trigger instruction
  317. */
  318. goto emulate;
  319. }
  320. if (fpexc & FPEXC_EX) {
  321. #ifndef CONFIG_CPU_FEROCEON
  322. /*
  323. * Asynchronous exception. The instruction is read from FPINST
  324. * and the interrupted instruction has to be restarted.
  325. */
  326. trigger = fmrx(FPINST);
  327. regs->ARM_pc -= 4;
  328. #endif
  329. } else if (!(fpexc & FPEXC_DEX)) {
  330. /*
  331. * Illegal combination of bits. It can be caused by an
  332. * unallocated VFP instruction but with FPSCR.IXE set and not
  333. * on VFP subarch 1.
  334. */
  335. vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
  336. goto exit;
  337. }
  338. /*
  339. * Modify fpscr to indicate the number of iterations remaining.
  340. * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
  341. * whether FPEXC.VECITR or FPSCR.LEN is used.
  342. */
  343. if (fpexc & (FPEXC_EX | FPEXC_VV)) {
  344. u32 len;
  345. len = fpexc + (1 << FPEXC_LENGTH_BIT);
  346. fpscr &= ~FPSCR_LENGTH_MASK;
  347. fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
  348. }
  349. /*
  350. * Handle the first FP instruction. We used to take note of the
  351. * FPEXC bounce reason, but this appears to be unreliable.
  352. * Emulate the bounced instruction instead.
  353. */
  354. exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
  355. if (exceptions)
  356. vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
  357. /*
  358. * If there isn't a second FP instruction, exit now. Note that
  359. * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
  360. */
  361. if ((fpexc & (FPEXC_EX | FPEXC_FP2V)) != (FPEXC_EX | FPEXC_FP2V))
  362. goto exit;
  363. /*
  364. * The barrier() here prevents fpinst2 being read
  365. * before the condition above.
  366. */
  367. barrier();
  368. trigger = fmrx(FPINST2);
  369. emulate:
  370. exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
  371. if (exceptions)
  372. vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
  373. exit:
  374. preempt_enable();
  375. }
  376. static void vfp_enable(void *unused)
  377. {
  378. u32 access;
  379. BUG_ON(preemptible());
  380. access = get_copro_access();
  381. /*
  382. * Enable full access to VFP (cp10 and cp11)
  383. */
  384. set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
  385. }
  386. #ifdef CONFIG_CPU_PM
  387. static int vfp_pm_suspend(void)
  388. {
  389. struct thread_info *ti = current_thread_info();
  390. u32 fpexc = fmrx(FPEXC);
  391. /* if vfp is on, then save state for resumption */
  392. if (fpexc & FPEXC_EN) {
  393. pr_debug("%s: saving vfp state\n", __func__);
  394. vfp_save_state(&ti->vfpstate, fpexc);
  395. /* disable, just in case */
  396. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  397. } else if (vfp_current_hw_state[ti->cpu]) {
  398. #ifndef CONFIG_SMP
  399. fmxr(FPEXC, fpexc | FPEXC_EN);
  400. vfp_save_state(vfp_current_hw_state[ti->cpu], fpexc);
  401. fmxr(FPEXC, fpexc);
  402. #endif
  403. }
  404. /* clear any information we had about last context state */
  405. vfp_current_hw_state[ti->cpu] = NULL;
  406. return 0;
  407. }
  408. static void vfp_pm_resume(void)
  409. {
  410. /* ensure we have access to the vfp */
  411. vfp_enable(NULL);
  412. /* and disable it to ensure the next usage restores the state */
  413. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  414. }
  415. static int vfp_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
  416. void *v)
  417. {
  418. switch (cmd) {
  419. case CPU_PM_ENTER:
  420. vfp_pm_suspend();
  421. break;
  422. case CPU_PM_ENTER_FAILED:
  423. case CPU_PM_EXIT:
  424. vfp_pm_resume();
  425. break;
  426. }
  427. return NOTIFY_OK;
  428. }
  429. static struct notifier_block vfp_cpu_pm_notifier_block = {
  430. .notifier_call = vfp_cpu_pm_notifier,
  431. };
  432. static void vfp_pm_init(void)
  433. {
  434. cpu_pm_register_notifier(&vfp_cpu_pm_notifier_block);
  435. }
  436. #else
  437. static inline void vfp_pm_init(void) { }
  438. #endif /* CONFIG_CPU_PM */
  439. /*
  440. * Ensure that the VFP state stored in 'thread->vfpstate' is up to date
  441. * with the hardware state.
  442. */
  443. void vfp_sync_hwstate(struct thread_info *thread)
  444. {
  445. unsigned int cpu = get_cpu();
  446. if (vfp_state_in_hw(cpu, thread)) {
  447. u32 fpexc = fmrx(FPEXC);
  448. /*
  449. * Save the last VFP state on this CPU.
  450. */
  451. fmxr(FPEXC, fpexc | FPEXC_EN);
  452. vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN);
  453. fmxr(FPEXC, fpexc);
  454. }
  455. put_cpu();
  456. }
  457. /* Ensure that the thread reloads the hardware VFP state on the next use. */
  458. void vfp_flush_hwstate(struct thread_info *thread)
  459. {
  460. unsigned int cpu = get_cpu();
  461. vfp_force_reload(cpu, thread);
  462. put_cpu();
  463. }
  464. /*
  465. * Save the current VFP state into the provided structures and prepare
  466. * for entry into a new function (signal handler).
  467. */
  468. int vfp_preserve_user_clear_hwstate(struct user_vfp __user *ufp,
  469. struct user_vfp_exc __user *ufp_exc)
  470. {
  471. struct thread_info *thread = current_thread_info();
  472. struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
  473. int err = 0;
  474. /* Ensure that the saved hwstate is up-to-date. */
  475. vfp_sync_hwstate(thread);
  476. /*
  477. * Copy the floating point registers. There can be unused
  478. * registers see asm/hwcap.h for details.
  479. */
  480. err |= __copy_to_user(&ufp->fpregs, &hwstate->fpregs,
  481. sizeof(hwstate->fpregs));
  482. /*
  483. * Copy the status and control register.
  484. */
  485. __put_user_error(hwstate->fpscr, &ufp->fpscr, err);
  486. /*
  487. * Copy the exception registers.
  488. */
  489. __put_user_error(hwstate->fpexc, &ufp_exc->fpexc, err);
  490. __put_user_error(hwstate->fpinst, &ufp_exc->fpinst, err);
  491. __put_user_error(hwstate->fpinst2, &ufp_exc->fpinst2, err);
  492. if (err)
  493. return -EFAULT;
  494. /* Ensure that VFP is disabled. */
  495. vfp_flush_hwstate(thread);
  496. /*
  497. * As per the PCS, clear the length and stride bits for function
  498. * entry.
  499. */
  500. hwstate->fpscr &= ~(FPSCR_LENGTH_MASK | FPSCR_STRIDE_MASK);
  501. return 0;
  502. }
  503. /* Sanitise and restore the current VFP state from the provided structures. */
  504. int vfp_restore_user_hwstate(struct user_vfp __user *ufp,
  505. struct user_vfp_exc __user *ufp_exc)
  506. {
  507. struct thread_info *thread = current_thread_info();
  508. struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
  509. unsigned long fpexc;
  510. int err = 0;
  511. /* Disable VFP to avoid corrupting the new thread state. */
  512. vfp_flush_hwstate(thread);
  513. /*
  514. * Copy the floating point registers. There can be unused
  515. * registers see asm/hwcap.h for details.
  516. */
  517. err |= __copy_from_user(&hwstate->fpregs, &ufp->fpregs,
  518. sizeof(hwstate->fpregs));
  519. /*
  520. * Copy the status and control register.
  521. */
  522. __get_user_error(hwstate->fpscr, &ufp->fpscr, err);
  523. /*
  524. * Sanitise and restore the exception registers.
  525. */
  526. __get_user_error(fpexc, &ufp_exc->fpexc, err);
  527. /* Ensure the VFP is enabled. */
  528. fpexc |= FPEXC_EN;
  529. /* Ensure FPINST2 is invalid and the exception flag is cleared. */
  530. fpexc &= ~(FPEXC_EX | FPEXC_FP2V);
  531. hwstate->fpexc = fpexc;
  532. __get_user_error(hwstate->fpinst, &ufp_exc->fpinst, err);
  533. __get_user_error(hwstate->fpinst2, &ufp_exc->fpinst2, err);
  534. return err ? -EFAULT : 0;
  535. }
  536. /*
  537. * VFP hardware can lose all context when a CPU goes offline.
  538. * As we will be running in SMP mode with CPU hotplug, we will save the
  539. * hardware state at every thread switch. We clear our held state when
  540. * a CPU has been killed, indicating that the VFP hardware doesn't contain
  541. * a threads VFP state. When a CPU starts up, we re-enable access to the
  542. * VFP hardware.
  543. *
  544. * Both CPU_DYING and CPU_STARTING are called on the CPU which
  545. * is being offlined/onlined.
  546. */
  547. static int vfp_hotplug(struct notifier_block *b, unsigned long action,
  548. void *hcpu)
  549. {
  550. if (action == CPU_DYING || action == CPU_DYING_FROZEN) {
  551. vfp_force_reload((long)hcpu, current_thread_info());
  552. } else if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  553. vfp_enable(NULL);
  554. return NOTIFY_OK;
  555. }
  556. void vfp_kmode_exception(void)
  557. {
  558. /*
  559. * If we reach this point, a floating point exception has been raised
  560. * while running in kernel mode. If the NEON/VFP unit was enabled at the
  561. * time, it means a VFP instruction has been issued that requires
  562. * software assistance to complete, something which is not currently
  563. * supported in kernel mode.
  564. * If the NEON/VFP unit was disabled, and the location pointed to below
  565. * is properly preceded by a call to kernel_neon_begin(), something has
  566. * caused the task to be scheduled out and back in again. In this case,
  567. * rebuilding and running with CONFIG_DEBUG_ATOMIC_SLEEP enabled should
  568. * be helpful in localizing the problem.
  569. */
  570. if (fmrx(FPEXC) & FPEXC_EN)
  571. pr_crit("BUG: unsupported FP instruction in kernel mode\n");
  572. else
  573. pr_crit("BUG: FP instruction issued in kernel mode with FP unit disabled\n");
  574. }
  575. #ifdef CONFIG_KERNEL_MODE_NEON
  576. /*
  577. * Kernel-side NEON support functions
  578. */
  579. void kernel_neon_begin(void)
  580. {
  581. struct thread_info *thread = current_thread_info();
  582. unsigned int cpu;
  583. u32 fpexc;
  584. /*
  585. * Kernel mode NEON is only allowed outside of interrupt context
  586. * with preemption disabled. This will make sure that the kernel
  587. * mode NEON register contents never need to be preserved.
  588. */
  589. BUG_ON(in_interrupt());
  590. cpu = get_cpu();
  591. fpexc = fmrx(FPEXC) | FPEXC_EN;
  592. fmxr(FPEXC, fpexc);
  593. /*
  594. * Save the userland NEON/VFP state. Under UP,
  595. * the owner could be a task other than 'current'
  596. */
  597. if (vfp_state_in_hw(cpu, thread))
  598. vfp_save_state(&thread->vfpstate, fpexc);
  599. #ifndef CONFIG_SMP
  600. else if (vfp_current_hw_state[cpu] != NULL)
  601. vfp_save_state(vfp_current_hw_state[cpu], fpexc);
  602. #endif
  603. vfp_current_hw_state[cpu] = NULL;
  604. }
  605. EXPORT_SYMBOL(kernel_neon_begin);
  606. void kernel_neon_end(void)
  607. {
  608. /* Disable the NEON/VFP unit. */
  609. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  610. put_cpu();
  611. }
  612. EXPORT_SYMBOL(kernel_neon_end);
  613. #endif /* CONFIG_KERNEL_MODE_NEON */
  614. /*
  615. * VFP support code initialisation.
  616. */
  617. static int __init vfp_init(void)
  618. {
  619. unsigned int vfpsid;
  620. unsigned int cpu_arch = cpu_architecture();
  621. if (cpu_arch >= CPU_ARCH_ARMv6)
  622. on_each_cpu(vfp_enable, NULL, 1);
  623. /*
  624. * First check that there is a VFP that we can use.
  625. * The handler is already setup to just log calls, so
  626. * we just need to read the VFPSID register.
  627. */
  628. vfp_vector = vfp_testing_entry;
  629. barrier();
  630. vfpsid = fmrx(FPSID);
  631. barrier();
  632. vfp_vector = vfp_null_entry;
  633. pr_info("VFP support v0.3: ");
  634. if (VFP_arch)
  635. pr_cont("not present\n");
  636. else if (vfpsid & FPSID_NODOUBLE) {
  637. pr_cont("no double precision support\n");
  638. } else {
  639. hotcpu_notifier(vfp_hotplug, 0);
  640. VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT; /* Extract the architecture version */
  641. pr_cont("implementor %02x architecture %d part %02x variant %x rev %x\n",
  642. (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
  643. (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT,
  644. (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
  645. (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
  646. (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
  647. vfp_vector = vfp_support_entry;
  648. thread_register_notifier(&vfp_notifier_block);
  649. vfp_pm_init();
  650. /*
  651. * We detected VFP, and the support code is
  652. * in place; report VFP support to userspace.
  653. */
  654. elf_hwcap |= HWCAP_VFP;
  655. #ifdef CONFIG_VFPv3
  656. if (VFP_arch >= 2) {
  657. elf_hwcap |= HWCAP_VFPv3;
  658. /*
  659. * Check for VFPv3 D16 and VFPv4 D16. CPUs in
  660. * this configuration only have 16 x 64bit
  661. * registers.
  662. */
  663. if (((fmrx(MVFR0) & MVFR0_A_SIMD_MASK)) == 1)
  664. elf_hwcap |= HWCAP_VFPv3D16; /* also v4-D16 */
  665. else
  666. elf_hwcap |= HWCAP_VFPD32;
  667. }
  668. #endif
  669. /*
  670. * Check for the presence of the Advanced SIMD
  671. * load/store instructions, integer and single
  672. * precision floating point operations. Only check
  673. * for NEON if the hardware has the MVFR registers.
  674. */
  675. if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) {
  676. #ifdef CONFIG_NEON
  677. if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100)
  678. elf_hwcap |= HWCAP_NEON;
  679. #endif
  680. #ifdef CONFIG_VFPv3
  681. if ((fmrx(MVFR1) & 0xf0000000) == 0x10000000)
  682. elf_hwcap |= HWCAP_VFPv4;
  683. #endif
  684. }
  685. }
  686. return 0;
  687. }
  688. core_initcall(vfp_init);