vfpmodule.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/vfp/vfpmodule.c
  4. *
  5. * Copyright (C) 2004 ARM Limited.
  6. * Written by Deep Blue Solutions Limited.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/cpu.h>
  10. #include <linux/cpu_pm.h>
  11. #include <linux/hardirq.h>
  12. #include <linux/kernel.h>
  13. #include <linux/notifier.h>
  14. #include <linux/signal.h>
  15. #include <linux/sched/signal.h>
  16. #include <linux/smp.h>
  17. #include <linux/init.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/user.h>
  20. #include <linux/export.h>
  21. #include <linux/perf_event.h>
  22. #include <asm/cp15.h>
  23. #include <asm/cputype.h>
  24. #include <asm/system_info.h>
  25. #include <asm/thread_notify.h>
  26. #include <asm/traps.h>
  27. #include <asm/vfp.h>
  28. #include <asm/neon.h>
  29. #include "vfpinstr.h"
  30. #include "vfp.h"
  31. static bool have_vfp __ro_after_init;
  32. /*
  33. * Dual-use variable.
  34. * Used in startup: set to non-zero if VFP checks fail
  35. * After startup, holds VFP architecture
  36. */
  37. static unsigned int VFP_arch;
  38. #ifdef CONFIG_CPU_FEROCEON
  39. extern unsigned int VFP_arch_feroceon __alias(VFP_arch);
  40. #endif
  41. /*
  42. * The pointer to the vfpstate structure of the thread which currently
  43. * owns the context held in the VFP hardware, or NULL if the hardware
  44. * context is invalid.
  45. *
  46. * For UP, this is sufficient to tell which thread owns the VFP context.
  47. * However, for SMP, we also need to check the CPU number stored in the
  48. * saved state too to catch migrations.
  49. */
  50. union vfp_state *vfp_current_hw_state[NR_CPUS];
  51. /*
  52. * Is 'thread's most up to date state stored in this CPUs hardware?
  53. * Must be called from non-preemptible context.
  54. */
  55. static bool vfp_state_in_hw(unsigned int cpu, struct thread_info *thread)
  56. {
  57. #ifdef CONFIG_SMP
  58. if (thread->vfpstate.hard.cpu != cpu)
  59. return false;
  60. #endif
  61. return vfp_current_hw_state[cpu] == &thread->vfpstate;
  62. }
  63. /*
  64. * Force a reload of the VFP context from the thread structure. We do
  65. * this by ensuring that access to the VFP hardware is disabled, and
  66. * clear vfp_current_hw_state. Must be called from non-preemptible context.
  67. */
  68. static void vfp_force_reload(unsigned int cpu, struct thread_info *thread)
  69. {
  70. if (vfp_state_in_hw(cpu, thread)) {
  71. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  72. vfp_current_hw_state[cpu] = NULL;
  73. }
  74. #ifdef CONFIG_SMP
  75. thread->vfpstate.hard.cpu = NR_CPUS;
  76. #endif
  77. }
  78. /*
  79. * Per-thread VFP initialization.
  80. */
  81. static void vfp_thread_flush(struct thread_info *thread)
  82. {
  83. union vfp_state *vfp = &thread->vfpstate;
  84. unsigned int cpu;
  85. /*
  86. * Disable VFP to ensure we initialize it first. We must ensure
  87. * that the modification of vfp_current_hw_state[] and hardware
  88. * disable are done for the same CPU and without preemption.
  89. *
  90. * Do this first to ensure that preemption won't overwrite our
  91. * state saving should access to the VFP be enabled at this point.
  92. */
  93. cpu = get_cpu();
  94. if (vfp_current_hw_state[cpu] == vfp)
  95. vfp_current_hw_state[cpu] = NULL;
  96. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  97. put_cpu();
  98. memset(vfp, 0, sizeof(union vfp_state));
  99. vfp->hard.fpexc = FPEXC_EN;
  100. vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
  101. #ifdef CONFIG_SMP
  102. vfp->hard.cpu = NR_CPUS;
  103. #endif
  104. }
  105. static void vfp_thread_exit(struct thread_info *thread)
  106. {
  107. /* release case: Per-thread VFP cleanup. */
  108. union vfp_state *vfp = &thread->vfpstate;
  109. unsigned int cpu = get_cpu();
  110. if (vfp_current_hw_state[cpu] == vfp)
  111. vfp_current_hw_state[cpu] = NULL;
  112. put_cpu();
  113. }
  114. static void vfp_thread_copy(struct thread_info *thread)
  115. {
  116. struct thread_info *parent = current_thread_info();
  117. vfp_sync_hwstate(parent);
  118. thread->vfpstate = parent->vfpstate;
  119. #ifdef CONFIG_SMP
  120. thread->vfpstate.hard.cpu = NR_CPUS;
  121. #endif
  122. }
  123. /*
  124. * When this function is called with the following 'cmd's, the following
  125. * is true while this function is being run:
  126. * THREAD_NOFTIFY_SWTICH:
  127. * - the previously running thread will not be scheduled onto another CPU.
  128. * - the next thread to be run (v) will not be running on another CPU.
  129. * - thread->cpu is the local CPU number
  130. * - not preemptible as we're called in the middle of a thread switch
  131. * THREAD_NOTIFY_FLUSH:
  132. * - the thread (v) will be running on the local CPU, so
  133. * v === current_thread_info()
  134. * - thread->cpu is the local CPU number at the time it is accessed,
  135. * but may change at any time.
  136. * - we could be preempted if tree preempt rcu is enabled, so
  137. * it is unsafe to use thread->cpu.
  138. * THREAD_NOTIFY_EXIT
  139. * - we could be preempted if tree preempt rcu is enabled, so
  140. * it is unsafe to use thread->cpu.
  141. */
  142. static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  143. {
  144. struct thread_info *thread = v;
  145. u32 fpexc;
  146. #ifdef CONFIG_SMP
  147. unsigned int cpu;
  148. #endif
  149. switch (cmd) {
  150. case THREAD_NOTIFY_SWITCH:
  151. fpexc = fmrx(FPEXC);
  152. #ifdef CONFIG_SMP
  153. cpu = thread->cpu;
  154. /*
  155. * On SMP, if VFP is enabled, save the old state in
  156. * case the thread migrates to a different CPU. The
  157. * restoring is done lazily.
  158. */
  159. if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu])
  160. vfp_save_state(vfp_current_hw_state[cpu], fpexc);
  161. #endif
  162. /*
  163. * Always disable VFP so we can lazily save/restore the
  164. * old state.
  165. */
  166. fmxr(FPEXC, fpexc & ~FPEXC_EN);
  167. break;
  168. case THREAD_NOTIFY_FLUSH:
  169. vfp_thread_flush(thread);
  170. break;
  171. case THREAD_NOTIFY_EXIT:
  172. vfp_thread_exit(thread);
  173. break;
  174. case THREAD_NOTIFY_COPY:
  175. vfp_thread_copy(thread);
  176. break;
  177. }
  178. return NOTIFY_DONE;
  179. }
  180. static struct notifier_block vfp_notifier_block = {
  181. .notifier_call = vfp_notifier,
  182. };
  183. /*
  184. * Raise a SIGFPE for the current process.
  185. * sicode describes the signal being raised.
  186. */
  187. static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
  188. {
  189. /*
  190. * This is the same as NWFPE, because it's not clear what
  191. * this is used for
  192. */
  193. current->thread.error_code = 0;
  194. current->thread.trap_no = 6;
  195. send_sig_fault(SIGFPE, sicode,
  196. (void __user *)(instruction_pointer(regs) - 4),
  197. current);
  198. }
  199. static void vfp_panic(char *reason, u32 inst)
  200. {
  201. int i;
  202. pr_err("VFP: Error: %s\n", reason);
  203. pr_err("VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
  204. fmrx(FPEXC), fmrx(FPSCR), inst);
  205. for (i = 0; i < 32; i += 2)
  206. pr_err("VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
  207. i, vfp_get_float(i), i+1, vfp_get_float(i+1));
  208. }
  209. /*
  210. * Process bitmask of exception conditions.
  211. */
  212. static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
  213. {
  214. int si_code = 0;
  215. pr_debug("VFP: raising exceptions %08x\n", exceptions);
  216. if (exceptions == VFP_EXCEPTION_ERROR) {
  217. vfp_panic("unhandled bounce", inst);
  218. vfp_raise_sigfpe(FPE_FLTINV, regs);
  219. return;
  220. }
  221. /*
  222. * If any of the status flags are set, update the FPSCR.
  223. * Comparison instructions always return at least one of
  224. * these flags set.
  225. */
  226. if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  227. fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
  228. fpscr |= exceptions;
  229. fmxr(FPSCR, fpscr);
  230. #define RAISE(stat,en,sig) \
  231. if (exceptions & stat && fpscr & en) \
  232. si_code = sig;
  233. /*
  234. * These are arranged in priority order, least to highest.
  235. */
  236. RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
  237. RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
  238. RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
  239. RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
  240. RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
  241. if (si_code)
  242. vfp_raise_sigfpe(si_code, regs);
  243. }
  244. /*
  245. * Emulate a VFP instruction.
  246. */
  247. static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
  248. {
  249. u32 exceptions = VFP_EXCEPTION_ERROR;
  250. pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
  251. if (INST_CPRTDO(inst)) {
  252. if (!INST_CPRT(inst)) {
  253. /*
  254. * CPDO
  255. */
  256. if (vfp_single(inst)) {
  257. exceptions = vfp_single_cpdo(inst, fpscr);
  258. } else {
  259. exceptions = vfp_double_cpdo(inst, fpscr);
  260. }
  261. } else {
  262. /*
  263. * A CPRT instruction can not appear in FPINST2, nor
  264. * can it cause an exception. Therefore, we do not
  265. * have to emulate it.
  266. */
  267. }
  268. } else {
  269. /*
  270. * A CPDT instruction can not appear in FPINST2, nor can
  271. * it cause an exception. Therefore, we do not have to
  272. * emulate it.
  273. */
  274. }
  275. perf_sw_event(PERF_COUNT_SW_EMULATION_FAULTS, 1, regs, regs->ARM_pc);
  276. return exceptions & ~VFP_NAN_FLAG;
  277. }
  278. /*
  279. * Package up a bounce condition.
  280. */
  281. static void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
  282. {
  283. u32 fpscr, orig_fpscr, fpsid, exceptions;
  284. pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
  285. /*
  286. * At this point, FPEXC can have the following configuration:
  287. *
  288. * EX DEX IXE
  289. * 0 1 x - synchronous exception
  290. * 1 x 0 - asynchronous exception
  291. * 1 x 1 - sychronous on VFP subarch 1 and asynchronous on later
  292. * 0 0 1 - synchronous on VFP9 (non-standard subarch 1
  293. * implementation), undefined otherwise
  294. *
  295. * Clear various bits and enable access to the VFP so we can
  296. * handle the bounce.
  297. */
  298. fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
  299. fpsid = fmrx(FPSID);
  300. orig_fpscr = fpscr = fmrx(FPSCR);
  301. /*
  302. * Check for the special VFP subarch 1 and FPSCR.IXE bit case
  303. */
  304. if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
  305. && (fpscr & FPSCR_IXE)) {
  306. /*
  307. * Synchronous exception, emulate the trigger instruction
  308. */
  309. goto emulate;
  310. }
  311. if (fpexc & FPEXC_EX) {
  312. /*
  313. * Asynchronous exception. The instruction is read from FPINST
  314. * and the interrupted instruction has to be restarted.
  315. */
  316. trigger = fmrx(FPINST);
  317. regs->ARM_pc -= 4;
  318. } else if (!(fpexc & FPEXC_DEX)) {
  319. /*
  320. * Illegal combination of bits. It can be caused by an
  321. * unallocated VFP instruction but with FPSCR.IXE set and not
  322. * on VFP subarch 1.
  323. */
  324. vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
  325. return;
  326. }
  327. /*
  328. * Modify fpscr to indicate the number of iterations remaining.
  329. * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
  330. * whether FPEXC.VECITR or FPSCR.LEN is used.
  331. */
  332. if (fpexc & (FPEXC_EX | FPEXC_VV)) {
  333. u32 len;
  334. len = fpexc + (1 << FPEXC_LENGTH_BIT);
  335. fpscr &= ~FPSCR_LENGTH_MASK;
  336. fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
  337. }
  338. /*
  339. * Handle the first FP instruction. We used to take note of the
  340. * FPEXC bounce reason, but this appears to be unreliable.
  341. * Emulate the bounced instruction instead.
  342. */
  343. exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
  344. if (exceptions)
  345. vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
  346. /*
  347. * If there isn't a second FP instruction, exit now. Note that
  348. * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
  349. */
  350. if ((fpexc & (FPEXC_EX | FPEXC_FP2V)) != (FPEXC_EX | FPEXC_FP2V))
  351. return;
  352. /*
  353. * The barrier() here prevents fpinst2 being read
  354. * before the condition above.
  355. */
  356. barrier();
  357. trigger = fmrx(FPINST2);
  358. emulate:
  359. exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
  360. if (exceptions)
  361. vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
  362. }
  363. static void vfp_enable(void *unused)
  364. {
  365. u32 access;
  366. BUG_ON(preemptible());
  367. access = get_copro_access();
  368. /*
  369. * Enable full access to VFP (cp10 and cp11)
  370. */
  371. set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
  372. }
  373. /* Called by platforms on which we want to disable VFP because it may not be
  374. * present on all CPUs within a SMP complex. Needs to be called prior to
  375. * vfp_init().
  376. */
  377. void __init vfp_disable(void)
  378. {
  379. if (VFP_arch) {
  380. pr_debug("%s: should be called prior to vfp_init\n", __func__);
  381. return;
  382. }
  383. VFP_arch = 1;
  384. }
  385. #ifdef CONFIG_CPU_PM
  386. static int vfp_pm_suspend(void)
  387. {
  388. struct thread_info *ti = current_thread_info();
  389. u32 fpexc = fmrx(FPEXC);
  390. /* if vfp is on, then save state for resumption */
  391. if (fpexc & FPEXC_EN) {
  392. pr_debug("%s: saving vfp state\n", __func__);
  393. vfp_save_state(&ti->vfpstate, fpexc);
  394. /* disable, just in case */
  395. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  396. } else if (vfp_current_hw_state[ti->cpu]) {
  397. #ifndef CONFIG_SMP
  398. fmxr(FPEXC, fpexc | FPEXC_EN);
  399. vfp_save_state(vfp_current_hw_state[ti->cpu], fpexc);
  400. fmxr(FPEXC, fpexc);
  401. #endif
  402. }
  403. /* clear any information we had about last context state */
  404. vfp_current_hw_state[ti->cpu] = NULL;
  405. return 0;
  406. }
  407. static void vfp_pm_resume(void)
  408. {
  409. /* ensure we have access to the vfp */
  410. vfp_enable(NULL);
  411. /* and disable it to ensure the next usage restores the state */
  412. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  413. }
  414. static int vfp_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
  415. void *v)
  416. {
  417. switch (cmd) {
  418. case CPU_PM_ENTER:
  419. vfp_pm_suspend();
  420. break;
  421. case CPU_PM_ENTER_FAILED:
  422. case CPU_PM_EXIT:
  423. vfp_pm_resume();
  424. break;
  425. }
  426. return NOTIFY_OK;
  427. }
  428. static struct notifier_block vfp_cpu_pm_notifier_block = {
  429. .notifier_call = vfp_cpu_pm_notifier,
  430. };
  431. static void vfp_pm_init(void)
  432. {
  433. cpu_pm_register_notifier(&vfp_cpu_pm_notifier_block);
  434. }
  435. #else
  436. static inline void vfp_pm_init(void) { }
  437. #endif /* CONFIG_CPU_PM */
  438. /*
  439. * Ensure that the VFP state stored in 'thread->vfpstate' is up to date
  440. * with the hardware state.
  441. */
  442. void vfp_sync_hwstate(struct thread_info *thread)
  443. {
  444. unsigned int cpu = get_cpu();
  445. local_bh_disable();
  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. local_bh_enable();
  456. put_cpu();
  457. }
  458. /* Ensure that the thread reloads the hardware VFP state on the next use. */
  459. void vfp_flush_hwstate(struct thread_info *thread)
  460. {
  461. unsigned int cpu = get_cpu();
  462. vfp_force_reload(cpu, thread);
  463. put_cpu();
  464. }
  465. /*
  466. * Save the current VFP state into the provided structures and prepare
  467. * for entry into a new function (signal handler).
  468. */
  469. int vfp_preserve_user_clear_hwstate(struct user_vfp *ufp,
  470. struct user_vfp_exc *ufp_exc)
  471. {
  472. struct thread_info *thread = current_thread_info();
  473. struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
  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. memcpy(&ufp->fpregs, &hwstate->fpregs, sizeof(hwstate->fpregs));
  481. /*
  482. * Copy the status and control register.
  483. */
  484. ufp->fpscr = hwstate->fpscr;
  485. /*
  486. * Copy the exception registers.
  487. */
  488. ufp_exc->fpexc = hwstate->fpexc;
  489. ufp_exc->fpinst = hwstate->fpinst;
  490. ufp_exc->fpinst2 = hwstate->fpinst2;
  491. /* Ensure that VFP is disabled. */
  492. vfp_flush_hwstate(thread);
  493. /*
  494. * As per the PCS, clear the length and stride bits for function
  495. * entry.
  496. */
  497. hwstate->fpscr &= ~(FPSCR_LENGTH_MASK | FPSCR_STRIDE_MASK);
  498. return 0;
  499. }
  500. /* Sanitise and restore the current VFP state from the provided structures. */
  501. int vfp_restore_user_hwstate(struct user_vfp *ufp, struct user_vfp_exc *ufp_exc)
  502. {
  503. struct thread_info *thread = current_thread_info();
  504. struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
  505. unsigned long fpexc;
  506. /* Disable VFP to avoid corrupting the new thread state. */
  507. vfp_flush_hwstate(thread);
  508. /*
  509. * Copy the floating point registers. There can be unused
  510. * registers see asm/hwcap.h for details.
  511. */
  512. memcpy(&hwstate->fpregs, &ufp->fpregs, sizeof(hwstate->fpregs));
  513. /*
  514. * Copy the status and control register.
  515. */
  516. hwstate->fpscr = ufp->fpscr;
  517. /*
  518. * Sanitise and restore the exception registers.
  519. */
  520. fpexc = ufp_exc->fpexc;
  521. /* Ensure the VFP is enabled. */
  522. fpexc |= FPEXC_EN;
  523. /* Ensure FPINST2 is invalid and the exception flag is cleared. */
  524. fpexc &= ~(FPEXC_EX | FPEXC_FP2V);
  525. hwstate->fpexc = fpexc;
  526. hwstate->fpinst = ufp_exc->fpinst;
  527. hwstate->fpinst2 = ufp_exc->fpinst2;
  528. return 0;
  529. }
  530. /*
  531. * VFP hardware can lose all context when a CPU goes offline.
  532. * As we will be running in SMP mode with CPU hotplug, we will save the
  533. * hardware state at every thread switch. We clear our held state when
  534. * a CPU has been killed, indicating that the VFP hardware doesn't contain
  535. * a threads VFP state. When a CPU starts up, we re-enable access to the
  536. * VFP hardware. The callbacks below are called on the CPU which
  537. * is being offlined/onlined.
  538. */
  539. static int vfp_dying_cpu(unsigned int cpu)
  540. {
  541. vfp_current_hw_state[cpu] = NULL;
  542. return 0;
  543. }
  544. static int vfp_starting_cpu(unsigned int unused)
  545. {
  546. vfp_enable(NULL);
  547. return 0;
  548. }
  549. static int vfp_kmode_exception(struct pt_regs *regs, unsigned int instr)
  550. {
  551. /*
  552. * If we reach this point, a floating point exception has been raised
  553. * while running in kernel mode. If the NEON/VFP unit was enabled at the
  554. * time, it means a VFP instruction has been issued that requires
  555. * software assistance to complete, something which is not currently
  556. * supported in kernel mode.
  557. * If the NEON/VFP unit was disabled, and the location pointed to below
  558. * is properly preceded by a call to kernel_neon_begin(), something has
  559. * caused the task to be scheduled out and back in again. In this case,
  560. * rebuilding and running with CONFIG_DEBUG_ATOMIC_SLEEP enabled should
  561. * be helpful in localizing the problem.
  562. */
  563. if (fmrx(FPEXC) & FPEXC_EN)
  564. pr_crit("BUG: unsupported FP instruction in kernel mode\n");
  565. else
  566. pr_crit("BUG: FP instruction issued in kernel mode with FP unit disabled\n");
  567. pr_crit("FPEXC == 0x%08x\n", fmrx(FPEXC));
  568. return 1;
  569. }
  570. /*
  571. * vfp_support_entry - Handle VFP exception
  572. *
  573. * @regs: pt_regs structure holding the register state at exception entry
  574. * @trigger: The opcode of the instruction that triggered the exception
  575. *
  576. * Returns 0 if the exception was handled, or an error code otherwise.
  577. */
  578. static int vfp_support_entry(struct pt_regs *regs, u32 trigger)
  579. {
  580. struct thread_info *ti = current_thread_info();
  581. u32 fpexc;
  582. if (unlikely(!have_vfp))
  583. return -ENODEV;
  584. if (!user_mode(regs))
  585. return vfp_kmode_exception(regs, trigger);
  586. local_bh_disable();
  587. fpexc = fmrx(FPEXC);
  588. /*
  589. * If the VFP unit was not enabled yet, we have to check whether the
  590. * VFP state in the CPU's registers is the most recent VFP state
  591. * associated with the process. On UP systems, we don't save the VFP
  592. * state eagerly on a context switch, so we may need to save the
  593. * VFP state to memory first, as it may belong to another process.
  594. */
  595. if (!(fpexc & FPEXC_EN)) {
  596. /*
  597. * Enable the VFP unit but mask the FP exception flag for the
  598. * time being, so we can access all the registers.
  599. */
  600. fpexc |= FPEXC_EN;
  601. fmxr(FPEXC, fpexc & ~FPEXC_EX);
  602. /*
  603. * Check whether or not the VFP state in the CPU's registers is
  604. * the most recent VFP state associated with this task. On SMP,
  605. * migration may result in multiple CPUs holding VFP states
  606. * that belong to the same task, but only the most recent one
  607. * is valid.
  608. */
  609. if (!vfp_state_in_hw(ti->cpu, ti)) {
  610. if (!IS_ENABLED(CONFIG_SMP) &&
  611. vfp_current_hw_state[ti->cpu] != NULL) {
  612. /*
  613. * This CPU is currently holding the most
  614. * recent VFP state associated with another
  615. * task, and we must save that to memory first.
  616. */
  617. vfp_save_state(vfp_current_hw_state[ti->cpu],
  618. fpexc);
  619. }
  620. /*
  621. * We can now proceed with loading the task's VFP state
  622. * from memory into the CPU registers.
  623. */
  624. fpexc = vfp_load_state(&ti->vfpstate);
  625. vfp_current_hw_state[ti->cpu] = &ti->vfpstate;
  626. #ifdef CONFIG_SMP
  627. /*
  628. * Record that this CPU is now the one holding the most
  629. * recent VFP state of the task.
  630. */
  631. ti->vfpstate.hard.cpu = ti->cpu;
  632. #endif
  633. }
  634. if (fpexc & FPEXC_EX)
  635. /*
  636. * Might as well handle the pending exception before
  637. * retrying branch out before setting an FPEXC that
  638. * stops us reading stuff.
  639. */
  640. goto bounce;
  641. /*
  642. * No FP exception is pending: just enable the VFP and
  643. * replay the instruction that trapped.
  644. */
  645. fmxr(FPEXC, fpexc);
  646. } else {
  647. /* Check for synchronous or asynchronous exceptions */
  648. if (!(fpexc & (FPEXC_EX | FPEXC_DEX))) {
  649. u32 fpscr = fmrx(FPSCR);
  650. /*
  651. * On some implementations of the VFP subarch 1,
  652. * setting FPSCR.IXE causes all the CDP instructions to
  653. * be bounced synchronously without setting the
  654. * FPEXC.EX bit
  655. */
  656. if (!(fpscr & FPSCR_IXE)) {
  657. if (!(fpscr & FPSCR_LENGTH_MASK)) {
  658. pr_debug("not VFP\n");
  659. local_bh_enable();
  660. return -ENOEXEC;
  661. }
  662. fpexc |= FPEXC_DEX;
  663. }
  664. }
  665. bounce: regs->ARM_pc += 4;
  666. VFP_bounce(trigger, fpexc, regs);
  667. }
  668. local_bh_enable();
  669. return 0;
  670. }
  671. static struct undef_hook neon_support_hook[] = {{
  672. .instr_mask = 0xfe000000,
  673. .instr_val = 0xf2000000,
  674. .cpsr_mask = PSR_T_BIT,
  675. .cpsr_val = 0,
  676. .fn = vfp_support_entry,
  677. }, {
  678. .instr_mask = 0xff100000,
  679. .instr_val = 0xf4000000,
  680. .cpsr_mask = PSR_T_BIT,
  681. .cpsr_val = 0,
  682. .fn = vfp_support_entry,
  683. }, {
  684. .instr_mask = 0xef000000,
  685. .instr_val = 0xef000000,
  686. .cpsr_mask = PSR_T_BIT,
  687. .cpsr_val = PSR_T_BIT,
  688. .fn = vfp_support_entry,
  689. }, {
  690. .instr_mask = 0xff100000,
  691. .instr_val = 0xf9000000,
  692. .cpsr_mask = PSR_T_BIT,
  693. .cpsr_val = PSR_T_BIT,
  694. .fn = vfp_support_entry,
  695. }, {
  696. .instr_mask = 0xff000800,
  697. .instr_val = 0xfc000800,
  698. .cpsr_mask = 0,
  699. .cpsr_val = 0,
  700. .fn = vfp_support_entry,
  701. }, {
  702. .instr_mask = 0xff000800,
  703. .instr_val = 0xfd000800,
  704. .cpsr_mask = 0,
  705. .cpsr_val = 0,
  706. .fn = vfp_support_entry,
  707. }, {
  708. .instr_mask = 0xff000800,
  709. .instr_val = 0xfe000800,
  710. .cpsr_mask = 0,
  711. .cpsr_val = 0,
  712. .fn = vfp_support_entry,
  713. }};
  714. static struct undef_hook vfp_support_hook = {
  715. .instr_mask = 0x0c000e00,
  716. .instr_val = 0x0c000a00,
  717. .fn = vfp_support_entry,
  718. };
  719. #ifdef CONFIG_KERNEL_MODE_NEON
  720. /*
  721. * Kernel-side NEON support functions
  722. */
  723. void kernel_neon_begin(void)
  724. {
  725. struct thread_info *thread = current_thread_info();
  726. unsigned int cpu;
  727. u32 fpexc;
  728. local_bh_disable();
  729. /*
  730. * Kernel mode NEON is only allowed outside of hardirq context with
  731. * preemption and softirq processing disabled. This will make sure that
  732. * the kernel mode NEON register contents never need to be preserved.
  733. */
  734. BUG_ON(in_hardirq());
  735. cpu = __smp_processor_id();
  736. fpexc = fmrx(FPEXC) | FPEXC_EN;
  737. fmxr(FPEXC, fpexc);
  738. /*
  739. * Save the userland NEON/VFP state. Under UP,
  740. * the owner could be a task other than 'current'
  741. */
  742. if (vfp_state_in_hw(cpu, thread))
  743. vfp_save_state(&thread->vfpstate, fpexc);
  744. #ifndef CONFIG_SMP
  745. else if (vfp_current_hw_state[cpu] != NULL)
  746. vfp_save_state(vfp_current_hw_state[cpu], fpexc);
  747. #endif
  748. vfp_current_hw_state[cpu] = NULL;
  749. }
  750. EXPORT_SYMBOL(kernel_neon_begin);
  751. void kernel_neon_end(void)
  752. {
  753. /* Disable the NEON/VFP unit. */
  754. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  755. local_bh_enable();
  756. }
  757. EXPORT_SYMBOL(kernel_neon_end);
  758. #endif /* CONFIG_KERNEL_MODE_NEON */
  759. static int __init vfp_detect(struct pt_regs *regs, unsigned int instr)
  760. {
  761. VFP_arch = UINT_MAX; /* mark as not present */
  762. regs->ARM_pc += 4;
  763. return 0;
  764. }
  765. static struct undef_hook vfp_detect_hook __initdata = {
  766. .instr_mask = 0x0c000e00,
  767. .instr_val = 0x0c000a00,
  768. .cpsr_mask = MODE_MASK,
  769. .cpsr_val = SVC_MODE,
  770. .fn = vfp_detect,
  771. };
  772. /*
  773. * VFP support code initialisation.
  774. */
  775. static int __init vfp_init(void)
  776. {
  777. unsigned int vfpsid;
  778. unsigned int cpu_arch = cpu_architecture();
  779. unsigned int isar6;
  780. /*
  781. * Enable the access to the VFP on all online CPUs so the
  782. * following test on FPSID will succeed.
  783. */
  784. if (cpu_arch >= CPU_ARCH_ARMv6)
  785. on_each_cpu(vfp_enable, NULL, 1);
  786. /*
  787. * First check that there is a VFP that we can use.
  788. * The handler is already setup to just log calls, so
  789. * we just need to read the VFPSID register.
  790. */
  791. register_undef_hook(&vfp_detect_hook);
  792. barrier();
  793. vfpsid = fmrx(FPSID);
  794. barrier();
  795. unregister_undef_hook(&vfp_detect_hook);
  796. pr_info("VFP support v0.3: ");
  797. if (VFP_arch) {
  798. pr_cont("not present\n");
  799. return 0;
  800. /* Extract the architecture on CPUID scheme */
  801. } else if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) {
  802. VFP_arch = vfpsid & FPSID_CPUID_ARCH_MASK;
  803. VFP_arch >>= FPSID_ARCH_BIT;
  804. /*
  805. * Check for the presence of the Advanced SIMD
  806. * load/store instructions, integer and single
  807. * precision floating point operations. Only check
  808. * for NEON if the hardware has the MVFR registers.
  809. */
  810. if (IS_ENABLED(CONFIG_NEON) &&
  811. (fmrx(MVFR1) & 0x000fff00) == 0x00011100) {
  812. elf_hwcap |= HWCAP_NEON;
  813. for (int i = 0; i < ARRAY_SIZE(neon_support_hook); i++)
  814. register_undef_hook(&neon_support_hook[i]);
  815. }
  816. if (IS_ENABLED(CONFIG_VFPv3)) {
  817. u32 mvfr0 = fmrx(MVFR0);
  818. if (((mvfr0 & MVFR0_DP_MASK) >> MVFR0_DP_BIT) == 0x2 ||
  819. ((mvfr0 & MVFR0_SP_MASK) >> MVFR0_SP_BIT) == 0x2) {
  820. elf_hwcap |= HWCAP_VFPv3;
  821. /*
  822. * Check for VFPv3 D16 and VFPv4 D16. CPUs in
  823. * this configuration only have 16 x 64bit
  824. * registers.
  825. */
  826. if ((mvfr0 & MVFR0_A_SIMD_MASK) == 1)
  827. /* also v4-D16 */
  828. elf_hwcap |= HWCAP_VFPv3D16;
  829. else
  830. elf_hwcap |= HWCAP_VFPD32;
  831. }
  832. if ((fmrx(MVFR1) & 0xf0000000) == 0x10000000)
  833. elf_hwcap |= HWCAP_VFPv4;
  834. if (((fmrx(MVFR1) & MVFR1_ASIMDHP_MASK) >> MVFR1_ASIMDHP_BIT) == 0x2)
  835. elf_hwcap |= HWCAP_ASIMDHP;
  836. if (((fmrx(MVFR1) & MVFR1_FPHP_MASK) >> MVFR1_FPHP_BIT) == 0x3)
  837. elf_hwcap |= HWCAP_FPHP;
  838. }
  839. /*
  840. * Check for the presence of Advanced SIMD Dot Product
  841. * instructions.
  842. */
  843. isar6 = read_cpuid_ext(CPUID_EXT_ISAR6);
  844. if (cpuid_feature_extract_field(isar6, 4) == 0x1)
  845. elf_hwcap |= HWCAP_ASIMDDP;
  846. /*
  847. * Check for the presence of Advanced SIMD Floating point
  848. * half-precision multiplication instructions.
  849. */
  850. if (cpuid_feature_extract_field(isar6, 8) == 0x1)
  851. elf_hwcap |= HWCAP_ASIMDFHM;
  852. /*
  853. * Check for the presence of Advanced SIMD Bfloat16
  854. * floating point instructions.
  855. */
  856. if (cpuid_feature_extract_field(isar6, 20) == 0x1)
  857. elf_hwcap |= HWCAP_ASIMDBF16;
  858. /*
  859. * Check for the presence of Advanced SIMD and floating point
  860. * Int8 matrix multiplication instructions instructions.
  861. */
  862. if (cpuid_feature_extract_field(isar6, 24) == 0x1)
  863. elf_hwcap |= HWCAP_I8MM;
  864. /* Extract the architecture version on pre-cpuid scheme */
  865. } else {
  866. if (vfpsid & FPSID_NODOUBLE) {
  867. pr_cont("no double precision support\n");
  868. return 0;
  869. }
  870. VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT;
  871. }
  872. cpuhp_setup_state_nocalls(CPUHP_AP_ARM_VFP_STARTING,
  873. "arm/vfp:starting", vfp_starting_cpu,
  874. vfp_dying_cpu);
  875. have_vfp = true;
  876. register_undef_hook(&vfp_support_hook);
  877. thread_register_notifier(&vfp_notifier_block);
  878. vfp_pm_init();
  879. /*
  880. * We detected VFP, and the support code is
  881. * in place; report VFP support to userspace.
  882. */
  883. elf_hwcap |= HWCAP_VFP;
  884. pr_cont("implementor %02x architecture %d part %02x variant %x rev %x\n",
  885. (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
  886. VFP_arch,
  887. (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
  888. (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
  889. (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
  890. return 0;
  891. }
  892. core_initcall(vfp_init);