vfpmodule.c 20 KB

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