correlationCalculationWaterTankSpray.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 <asm/cp15.h>
  24. #include <asm/cputype.h>
  25. #include <asm/system_info.h>
  26. #include <asm/thread_notify.h>
  27. #include <asm/vfp.h>
  28. #include "vfpinstr.h"
  29. #include "vfp.h"
  30. /*
  31. * Our undef handlers (in entry.S)
  32. */
  33. void vfp_testing_entry(void);
  34. void vfp_support_entry(void);
  35. void vfp_null_entry(void);
  36. void (*vfp_vector)(void) = vfp_null_entry;
  37. /*
  38. * Dual-use variable.
  39. * Used in startup: set to non-zero if VFP checks fail
  40. * After startup, holds VFP architecture
  41. */
  42. unsigned int VFP_arch;
  43. /*
  44. * The pointer to the vfpstate structure of the thread which currently
  45. * owns the context held in the VFP hardware, or NULL if the hardware
  46. * context is invalid.
  47. *
  48. * For UP, this is sufficient to tell which thread owns the VFP context.
  49. * However, for SMP, we also need to check the CPU number stored in the
  50. * saved state too to catch migrations.
  51. */
  52. union vfp_state *vfp_current_hw_state[NR_CPUS];
  53. /*
  54. * Is 'thread's most up to date state stored in this CPUs hardware?
  55. * Must be called from non-preemptible context.
  56. */
  57. static bool vfp_state_in_hw(unsigned int cpu, struct thread_info *thread)
  58. {
  59. #ifdef CONFIG_SMP
  60. if (thread->vfpstate.hard.cpu != cpu)
  61. return false;
  62. #endif
  63. return vfp_current_hw_state[cpu] == &thread->vfpstate;
  64. }
  65. /*
  66. * Force a reload of the VFP context from the thread structure. We do
  67. * this by ensuring that access to the VFP hardware is disabled, and
  68. * clear vfp_current_hw_state. Must be called from non-preemptible context.
  69. */
  70. static void vfp_force_reload(unsigned int cpu, struct thread_info *thread)
  71. {
  72. if (vfp_state_in_hw(cpu, thread)) {
  73. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  74. vfp_current_hw_state[cpu] = NULL;
  75. }
  76. #ifdef CONFIG_SMP
  77. thread->vfpstate.hard.cpu = NR_CPUS;
  78. #endif
  79. }
  80. /*
  81. * Per-thread VFP initialization.
  82. */
  83. static void vfp_thread_flush(struct thread_info *thread)
  84. {
  85. union vfp_state *vfp = &thread->vfpstate;
  86. unsigned int cpu;
  87. /*
  88. * Disable VFP to ensure we initialize it first. We must ensure
  89. * that the modification of vfp_current_hw_state[] and hardware
  90. * disable are done for the same CPU and without preemption.
  91. *
  92. * Do this first to ensure that preemption won't overwrite our
  93. * state saving should access to the VFP be enabled at this point.
  94. */
  95. cpu = get_cpu();
  96. if (vfp_current_hw_state[cpu] == vfp)
  97. vfp_current_hw_state[cpu] = NULL;
  98. fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
  99. put_cpu();
  100. memset(vfp, 0, sizeof(union vfp_state));
  101. vfp->hard.fpexc = FPEXC_EN;
  102. vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
  103. #ifdef CONFIG_SMP
  104. vfp->hard.cpu = NR_CPUS;
  105. #endif
  106. }
  107. static void vfp_thread_exit(struct thread_info *thread)
  108. {
  109. /* release case: Per-thread VFP cleanup. */
  110. union vfp_state *vfp = &thread->vfpstate;
  111. unsigned int cpu = get_cpu();
  112. if (vfp_current_hw_state[cpu] == vfp)
  113. vfp_current_hw_state[cpu] = NULL;
  114. put_cpu();
  115. }
  116. static void vfp_thread_copy(struct thread_info *thread)
  117. {
  118. struct thread_info *parent = current_thread_info();
  119. vfp_sync_hwstate(parent);
  120. thread->vfpstate = parent->vfpstate;
  121. #ifdef CONFIG_SMP
  122. thread->vfpstate.hard.cpu = NR_CPUS;
  123. #endif
  124. }
  125. /*
  126. * When this function is called with the following 'cmd's, the following
  127. * is true while this function is being run:
  128. * THREAD_NOFTIFY_SWTICH:
  129. * - the previously running thread will not be scheduled onto another CPU.
  130. * - the next thread to be run (v) will not be running on another CPU.
  131. * - thread->cpu is the local CPU number
  132. * - not preemptible as we're called in the middle of a thread switch
  133. * THREAD_NOTIFY_FLUSH:
  134. * - the thread (v) will be running on the local CPU, so
  135. * v === current_thread_info()
  136. * - thread->cpu is the local CPU number at the time it is accessed,
  137. * but may change at any time.
  138. * - we could be preempted if tree preempt rcu is enabled, so
  139. * it is unsafe to use thread->cpu.
  140. * THREAD_NOTIFY_EXIT
  141. * - the thread (v) will be running on the local CPU, so
  142. * v === current_thread_info()
  143. * - thread->cpu is the local CPU number at the time it is accessed,
  144. * but may change at any time.
  145. * - we could be preempted if tree preempt rcu is enabled, so
  146. * it is unsafe to use thread->cpu.
  147. */
  148. static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  149. {
  150. struct thread_info *thread = v;
  151. u32 fpexc;
  152. #ifdef CONFIG_SMP
  153. unsigned int cpu;
  154. #endif
  155. switch (cmd) {
  156. case THREAD_NOTIFY_SWITCH:
  157. fpexc = fmrx(FPEXC);
  158. #ifdef CONFIG_SMP
  159. cpu = thread->cpu;
  160. /*
  161. * On SMP, if VFP is enabled, save the old state in
  162. * case the thread migrates to a different CPU. The
  163. * restoring is done lazily.
  164. */
  165. if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu])
  166. vfp_save_state(vfp_current_hw_state[cpu], fpexc);
  167. #endif
  168. /*
  169. * Always disable VFP so we can lazily save/restore the
  170. * old state.
  171. */
  172. fmxr(FPEXC, fpexc & ~FPEXC_EN);
  173. break;
  174. case THREAD_NOTIFY_FLUSH:
  175. vfp_thread_flush(thread);
  176. break;
  177. case THREAD_NOTIFY_EXIT:
  178. vfp_thread_exit(thread);
  179. break;
  180. case THREAD_NOTIFY_COPY:
  181. vfp_thread_copy(thread);
  182. break;
  183. }
  184. return NOTIFY_DONE;
  185. }
  186. static struct notifier_block vfp_notifier_block = {
  187. .notifier_call = vfp_notifier,
  188. };
  189. /*
  190. * Raise a SIGFPE for the current process.
  191. * sicode describes the signal being raised.
  192. */
  193. static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
  194. {
  195. siginfo_t info;
  196. memset(&info, 0, sizeof(info));
  197. info.si_signo = SIGFPE;
  198. info.si_code = sicode;
  199. info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
  200. /*
  201. * This is the same as NWFPE, because it's not clear what
  202. * this is used for
  203. */
  204. current->thread.error_code = 0;
  205. current->thread.trap_no = 6;
  206. send_sig_info(SIGFPE, &info, current);
  207. }
  208. static void vfp_panic(char *reason, u32 inst)
  209. {
  210. int i;
  211. pr_err("VFP: Error: %s\n", reason);
  212. pr_err("VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
  213. fmrx(FPEXC), fmrx(FPSCR), inst);
  214. for (i = 0; i < 32; i += 2)
  215. pr_err("VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
  216. i, vfp_get_float(i), i+1, vfp_get_float(i+1));
  217. }
  218. /*
  219. * Process bitmask of exception conditions.
  220. */
  221. static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
  222. {
  223. int si_code = 0;
  224. pr_debug("VFP: raising exceptions %08x\n", exceptions);
  225. if (exceptions == VFP_EXCEPTION_ERROR) {
  226. vfp_panic("unhandled bounce", inst);
  227. vfp_raise_sigfpe(0, regs);
  228. return;
  229. }
  230. /*
  231. * If any of the status flags are set, update the FPSCR.
  232. * Comparison instructions always return at least one of
  233. * these flags set.
  234. */
  235. if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
  236. fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
  237. fpscr |= exceptions;
  238. fmxr(FPSCR, fpscr);
  239. #define RAISE(stat,en,sig) \
  240. if (exceptions & stat && fpscr & en) \
  241. si_code = sig;
  242. /*
  243. * These are arranged in priority order, least to highest.
  244. */
  245. RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
  246. RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
  247. RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
  248. RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
  249. RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
  250. if (si_code)
  251. vfp_raise_sigfpe(si_code, regs);
  252. }
  253. /*
  254. * Emulate a VFP instruction.
  255. */
  256. static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
  257. {
  258. u32 exceptions = VFP_EXCEPTION_ERROR;
  259. pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
  260. if (INST_CPRTDO(inst)) {
  261. if (!INST_CPRT(inst)) {
  262. /*
  263. * CPDO
  264. */
  265. if (vfp_single(inst)) {
  266. exceptions = vfp_single_cpdo(inst, fpscr);
  267. } else {
  268. exceptions = vfp_double_cpdo(inst, fpscr);
  269. }
  270. } else {
  271. /*
  272. * A CPRT instruction can not appear in FPINST2, nor
  273. * can it cause an exception. Therefore, we do not