vfpmodule.c 21 KB

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