Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/types.h> |
Russell King | 90b4419 | 2010-12-18 10:59:49 +0000 | [diff] [blame] | 13 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Russell King | 90b4419 | 2010-12-18 10:59:49 +0000 | [diff] [blame] | 15 | #include <linux/notifier.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/signal.h> |
| 17 | #include <linux/sched.h> |
Russell King | 90b4419 | 2010-12-18 10:59:49 +0000 | [diff] [blame] | 18 | #include <linux/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
Russell King | d6551e8 | 2006-06-21 13:31:52 +0100 | [diff] [blame] | 20 | |
Tony Lindgren | 5aaf254 | 2010-07-01 13:41:05 +0100 | [diff] [blame] | 21 | #include <asm/cputype.h> |
Russell King | d6551e8 | 2006-06-21 13:31:52 +0100 | [diff] [blame] | 22 | #include <asm/thread_notify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/vfp.h> |
| 24 | |
| 25 | #include "vfpinstr.h" |
| 26 | #include "vfp.h" |
| 27 | |
| 28 | /* |
| 29 | * Our undef handlers (in entry.S) |
| 30 | */ |
| 31 | void vfp_testing_entry(void); |
| 32 | void vfp_support_entry(void); |
Russell King | 5d4cae5 | 2007-06-10 12:22:20 +0100 | [diff] [blame] | 33 | void vfp_null_entry(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Russell King | 5d4cae5 | 2007-06-10 12:22:20 +0100 | [diff] [blame] | 35 | void (*vfp_vector)(void) = vfp_null_entry; |
Catalin Marinas | c642846 | 2007-01-24 18:47:08 +0100 | [diff] [blame] | 36 | union vfp_state *last_VFP_context[NR_CPUS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 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 | |
Russell King | 0d782dc | 2009-12-12 14:47:40 +0000 | [diff] [blame] | 45 | /* |
| 46 | * Per-thread VFP initialization. |
| 47 | */ |
| 48 | static void vfp_thread_flush(struct thread_info *thread) |
| 49 | { |
| 50 | union vfp_state *vfp = &thread->vfpstate; |
| 51 | unsigned int cpu; |
| 52 | |
| 53 | memset(vfp, 0, sizeof(union vfp_state)); |
| 54 | |
| 55 | vfp->hard.fpexc = FPEXC_EN; |
| 56 | vfp->hard.fpscr = FPSCR_ROUND_NEAREST; |
| 57 | |
| 58 | /* |
| 59 | * Disable VFP to ensure we initialize it first. We must ensure |
| 60 | * that the modification of last_VFP_context[] and hardware disable |
| 61 | * are done for the same CPU and without preemption. |
| 62 | */ |
| 63 | cpu = get_cpu(); |
| 64 | if (last_VFP_context[cpu] == vfp) |
| 65 | last_VFP_context[cpu] = NULL; |
| 66 | fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN); |
| 67 | put_cpu(); |
| 68 | } |
| 69 | |
Russell King | 797245f | 2009-12-18 14:34:43 +0000 | [diff] [blame] | 70 | static void vfp_thread_exit(struct thread_info *thread) |
Russell King | 0d782dc | 2009-12-12 14:47:40 +0000 | [diff] [blame] | 71 | { |
| 72 | /* release case: Per-thread VFP cleanup. */ |
| 73 | union vfp_state *vfp = &thread->vfpstate; |
Russell King | 797245f | 2009-12-18 14:34:43 +0000 | [diff] [blame] | 74 | unsigned int cpu = get_cpu(); |
Russell King | 0d782dc | 2009-12-12 14:47:40 +0000 | [diff] [blame] | 75 | |
| 76 | if (last_VFP_context[cpu] == vfp) |
| 77 | last_VFP_context[cpu] = NULL; |
Russell King | 797245f | 2009-12-18 14:34:43 +0000 | [diff] [blame] | 78 | put_cpu(); |
Russell King | 0d782dc | 2009-12-12 14:47:40 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
| 82 | * When this function is called with the following 'cmd's, the following |
| 83 | * is true while this function is being run: |
| 84 | * THREAD_NOFTIFY_SWTICH: |
| 85 | * - the previously running thread will not be scheduled onto another CPU. |
| 86 | * - the next thread to be run (v) will not be running on another CPU. |
| 87 | * - thread->cpu is the local CPU number |
| 88 | * - not preemptible as we're called in the middle of a thread switch |
| 89 | * THREAD_NOTIFY_FLUSH: |
| 90 | * - the thread (v) will be running on the local CPU, so |
| 91 | * v === current_thread_info() |
| 92 | * - thread->cpu is the local CPU number at the time it is accessed, |
| 93 | * but may change at any time. |
| 94 | * - we could be preempted if tree preempt rcu is enabled, so |
| 95 | * it is unsafe to use thread->cpu. |
Russell King | 797245f | 2009-12-18 14:34:43 +0000 | [diff] [blame] | 96 | * THREAD_NOTIFY_EXIT |
| 97 | * - the thread (v) will be running on the local CPU, so |
| 98 | * v === current_thread_info() |
| 99 | * - thread->cpu is the local CPU number at the time it is accessed, |
| 100 | * but may change at any time. |
| 101 | * - we could be preempted if tree preempt rcu is enabled, so |
| 102 | * it is unsafe to use thread->cpu. |
Russell King | 0d782dc | 2009-12-12 14:47:40 +0000 | [diff] [blame] | 103 | */ |
Russell King | d6551e8 | 2006-06-21 13:31:52 +0100 | [diff] [blame] | 104 | static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Russell King | d6551e8 | 2006-06-21 13:31:52 +0100 | [diff] [blame] | 106 | struct thread_info *thread = v; |
Catalin Marinas | 2e82669a | 2011-04-06 16:16:29 +0100 | [diff] [blame^] | 107 | u32 fpexc; |
| 108 | #ifdef CONFIG_SMP |
| 109 | unsigned int cpu; |
| 110 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Catalin Marinas | 2e82669a | 2011-04-06 16:16:29 +0100 | [diff] [blame^] | 112 | switch (cmd) { |
| 113 | case THREAD_NOTIFY_SWITCH: |
| 114 | fpexc = fmrx(FPEXC); |
Catalin Marinas | c642846 | 2007-01-24 18:47:08 +0100 | [diff] [blame] | 115 | |
| 116 | #ifdef CONFIG_SMP |
Catalin Marinas | 2e82669a | 2011-04-06 16:16:29 +0100 | [diff] [blame^] | 117 | cpu = thread->cpu; |
Russell King | 0d782dc | 2009-12-12 14:47:40 +0000 | [diff] [blame] | 118 | |
Catalin Marinas | c642846 | 2007-01-24 18:47:08 +0100 | [diff] [blame] | 119 | /* |
| 120 | * On SMP, if VFP is enabled, save the old state in |
| 121 | * case the thread migrates to a different CPU. The |
| 122 | * restoring is done lazily. |
| 123 | */ |
Russell King | 228adef | 2007-07-18 09:37:10 +0100 | [diff] [blame] | 124 | if ((fpexc & FPEXC_EN) && last_VFP_context[cpu]) { |
Catalin Marinas | c642846 | 2007-01-24 18:47:08 +0100 | [diff] [blame] | 125 | vfp_save_state(last_VFP_context[cpu], fpexc); |
| 126 | last_VFP_context[cpu]->hard.cpu = cpu; |
| 127 | } |
| 128 | /* |
| 129 | * Thread migration, just force the reloading of the |
| 130 | * state on the new CPU in case the VFP registers |
| 131 | * contain stale data. |
| 132 | */ |
| 133 | if (thread->vfpstate.hard.cpu != cpu) |
| 134 | last_VFP_context[cpu] = NULL; |
| 135 | #endif |
| 136 | |
Russell King | 681a499 | 2006-08-27 12:38:34 +0100 | [diff] [blame] | 137 | /* |
| 138 | * Always disable VFP so we can lazily save/restore the |
| 139 | * old state. |
| 140 | */ |
Russell King | 228adef | 2007-07-18 09:37:10 +0100 | [diff] [blame] | 141 | fmxr(FPEXC, fpexc & ~FPEXC_EN); |
Catalin Marinas | 2e82669a | 2011-04-06 16:16:29 +0100 | [diff] [blame^] | 142 | break; |
Russell King | 681a499 | 2006-08-27 12:38:34 +0100 | [diff] [blame] | 143 | |
Catalin Marinas | 2e82669a | 2011-04-06 16:16:29 +0100 | [diff] [blame^] | 144 | case THREAD_NOTIFY_FLUSH: |
Russell King | 0d782dc | 2009-12-12 14:47:40 +0000 | [diff] [blame] | 145 | vfp_thread_flush(thread); |
Catalin Marinas | 2e82669a | 2011-04-06 16:16:29 +0100 | [diff] [blame^] | 146 | break; |
| 147 | |
| 148 | case THREAD_NOTIFY_EXIT: |
Russell King | 797245f | 2009-12-18 14:34:43 +0000 | [diff] [blame] | 149 | vfp_thread_exit(thread); |
Catalin Marinas | 2e82669a | 2011-04-06 16:16:29 +0100 | [diff] [blame^] | 150 | break; |
| 151 | } |
Russell King | 681a499 | 2006-08-27 12:38:34 +0100 | [diff] [blame] | 152 | |
Russell King | d6551e8 | 2006-06-21 13:31:52 +0100 | [diff] [blame] | 153 | return NOTIFY_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Russell King | d6551e8 | 2006-06-21 13:31:52 +0100 | [diff] [blame] | 156 | static struct notifier_block vfp_notifier_block = { |
| 157 | .notifier_call = vfp_notifier, |
| 158 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * Raise a SIGFPE for the current process. |
| 162 | * sicode describes the signal being raised. |
| 163 | */ |
Russell King | 2bbd7e9 | 2011-01-08 12:05:09 +0000 | [diff] [blame] | 164 | static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | { |
| 166 | siginfo_t info; |
| 167 | |
| 168 | memset(&info, 0, sizeof(info)); |
| 169 | |
| 170 | info.si_signo = SIGFPE; |
| 171 | info.si_code = sicode; |
Al Viro | 35d59fc | 2006-10-11 17:22:44 +0100 | [diff] [blame] | 172 | info.si_addr = (void __user *)(instruction_pointer(regs) - 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * This is the same as NWFPE, because it's not clear what |
| 176 | * this is used for |
| 177 | */ |
| 178 | current->thread.error_code = 0; |
| 179 | current->thread.trap_no = 6; |
| 180 | |
Russell King | da41119 | 2005-06-29 23:02:02 +0100 | [diff] [blame] | 181 | send_sig_info(SIGFPE, &info, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 184 | static void vfp_panic(char *reason, u32 inst) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
| 186 | int i; |
| 187 | |
| 188 | printk(KERN_ERR "VFP: Error: %s\n", reason); |
| 189 | printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n", |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 190 | fmrx(FPEXC), fmrx(FPSCR), inst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | for (i = 0; i < 32; i += 2) |
| 192 | printk(KERN_ERR "VFP: s%2u: 0x%08x s%2u: 0x%08x\n", |
| 193 | i, vfp_get_float(i), i+1, vfp_get_float(i+1)); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Process bitmask of exception conditions. |
| 198 | */ |
| 199 | static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs) |
| 200 | { |
| 201 | int si_code = 0; |
| 202 | |
| 203 | pr_debug("VFP: raising exceptions %08x\n", exceptions); |
| 204 | |
Daniel Jacobowitz | 7c6f251 | 2006-08-27 12:42:08 +0100 | [diff] [blame] | 205 | if (exceptions == VFP_EXCEPTION_ERROR) { |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 206 | vfp_panic("unhandled bounce", inst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | vfp_raise_sigfpe(0, regs); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | /* |
Catalin Marinas | dbead40 | 2010-02-01 18:50:40 +0100 | [diff] [blame] | 212 | * If any of the status flags are set, update the FPSCR. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | * Comparison instructions always return at least one of |
| 214 | * these flags set. |
| 215 | */ |
Catalin Marinas | dbead40 | 2010-02-01 18:50:40 +0100 | [diff] [blame] | 216 | if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V)) |
| 217 | fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V); |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | fpscr |= exceptions; |
| 220 | |
| 221 | fmxr(FPSCR, fpscr); |
| 222 | |
| 223 | #define RAISE(stat,en,sig) \ |
| 224 | if (exceptions & stat && fpscr & en) \ |
| 225 | si_code = sig; |
| 226 | |
| 227 | /* |
| 228 | * These are arranged in priority order, least to highest. |
| 229 | */ |
Takashi Ohmasa | e0f205d | 2006-10-23 11:19:40 +0100 | [diff] [blame] | 230 | RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES); |
| 232 | RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND); |
| 233 | RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF); |
| 234 | RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV); |
| 235 | |
| 236 | if (si_code) |
| 237 | vfp_raise_sigfpe(si_code, regs); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Emulate a VFP instruction. |
| 242 | */ |
| 243 | static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs) |
| 244 | { |
Daniel Jacobowitz | 7c6f251 | 2006-08-27 12:42:08 +0100 | [diff] [blame] | 245 | u32 exceptions = VFP_EXCEPTION_ERROR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
| 247 | pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr); |
| 248 | |
| 249 | if (INST_CPRTDO(inst)) { |
| 250 | if (!INST_CPRT(inst)) { |
| 251 | /* |
| 252 | * CPDO |
| 253 | */ |
| 254 | if (vfp_single(inst)) { |
| 255 | exceptions = vfp_single_cpdo(inst, fpscr); |
| 256 | } else { |
| 257 | exceptions = vfp_double_cpdo(inst, fpscr); |
| 258 | } |
| 259 | } else { |
| 260 | /* |
| 261 | * A CPRT instruction can not appear in FPINST2, nor |
| 262 | * can it cause an exception. Therefore, we do not |
| 263 | * have to emulate it. |
| 264 | */ |
| 265 | } |
| 266 | } else { |
| 267 | /* |
| 268 | * A CPDT instruction can not appear in FPINST2, nor can |
| 269 | * it cause an exception. Therefore, we do not have to |
| 270 | * emulate it. |
| 271 | */ |
| 272 | } |
Russell King | 928bd1b | 2006-04-25 20:41:27 +0100 | [diff] [blame] | 273 | return exceptions & ~VFP_NAN_FLAG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* |
| 277 | * Package up a bounce condition. |
| 278 | */ |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 279 | void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 281 | u32 fpscr, orig_fpscr, fpsid, exceptions; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc); |
| 284 | |
| 285 | /* |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | */ |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 298 | fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 300 | fpsid = fmrx(FPSID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | orig_fpscr = fpscr = fmrx(FPSCR); |
| 302 | |
| 303 | /* |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 304 | * Check for the special VFP subarch 1 and FPSCR.IXE bit case |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | */ |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 306 | if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT) |
| 307 | && (fpscr & FPSCR_IXE)) { |
| 308 | /* |
| 309 | * Synchronous exception, emulate the trigger instruction |
| 310 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | goto emulate; |
| 312 | } |
| 313 | |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 314 | if (fpexc & FPEXC_EX) { |
Catalin Marinas | 85d6943 | 2009-05-30 14:00:18 +0100 | [diff] [blame] | 315 | #ifndef CONFIG_CPU_FEROCEON |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 316 | /* |
| 317 | * Asynchronous exception. The instruction is read from FPINST |
| 318 | * and the interrupted instruction has to be restarted. |
| 319 | */ |
| 320 | trigger = fmrx(FPINST); |
| 321 | regs->ARM_pc -= 4; |
Catalin Marinas | 85d6943 | 2009-05-30 14:00:18 +0100 | [diff] [blame] | 322 | #endif |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 323 | } else if (!(fpexc & FPEXC_DEX)) { |
| 324 | /* |
| 325 | * Illegal combination of bits. It can be caused by an |
| 326 | * unallocated VFP instruction but with FPSCR.IXE set and not |
| 327 | * on VFP subarch 1. |
| 328 | */ |
| 329 | vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs); |
George G. Davis | f2255be | 2009-04-01 20:27:18 +0100 | [diff] [blame] | 330 | goto exit; |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 331 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | /* |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 334 | * Modify fpscr to indicate the number of iterations remaining. |
| 335 | * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates |
| 336 | * whether FPEXC.VECITR or FPSCR.LEN is used. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | */ |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 338 | if (fpexc & (FPEXC_EX | FPEXC_VV)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | u32 len; |
| 340 | |
| 341 | len = fpexc + (1 << FPEXC_LENGTH_BIT); |
| 342 | |
| 343 | fpscr &= ~FPSCR_LENGTH_MASK; |
| 344 | fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT); |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Handle the first FP instruction. We used to take note of the |
| 349 | * FPEXC bounce reason, but this appears to be unreliable. |
| 350 | * Emulate the bounced instruction instead. |
| 351 | */ |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 352 | exceptions = vfp_emulate_instruction(trigger, fpscr, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (exceptions) |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 354 | vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | /* |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 357 | * If there isn't a second FP instruction, exit now. Note that |
| 358 | * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | */ |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 360 | if (fpexc ^ (FPEXC_EX | FPEXC_FP2V)) |
George G. Davis | f2255be | 2009-04-01 20:27:18 +0100 | [diff] [blame] | 361 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
| 363 | /* |
| 364 | * The barrier() here prevents fpinst2 being read |
| 365 | * before the condition above. |
| 366 | */ |
| 367 | barrier(); |
| 368 | trigger = fmrx(FPINST2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | emulate: |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 371 | exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | if (exceptions) |
| 373 | vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs); |
George G. Davis | f2255be | 2009-04-01 20:27:18 +0100 | [diff] [blame] | 374 | exit: |
| 375 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | } |
Russell King | efe90d2 | 2006-12-08 15:22:20 +0000 | [diff] [blame] | 377 | |
Russell King | 8e14036 | 2007-01-02 23:40:30 +0000 | [diff] [blame] | 378 | static void vfp_enable(void *unused) |
| 379 | { |
| 380 | u32 access = get_copro_access(); |
| 381 | |
| 382 | /* |
| 383 | * Enable full access to VFP (cp10 and cp11) |
| 384 | */ |
| 385 | set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11)); |
| 386 | } |
| 387 | |
Ben Dooks | fc0b7a2 | 2008-12-18 12:26:54 +0100 | [diff] [blame] | 388 | #ifdef CONFIG_PM |
| 389 | #include <linux/sysdev.h> |
| 390 | |
| 391 | static int vfp_pm_suspend(struct sys_device *dev, pm_message_t state) |
| 392 | { |
| 393 | struct thread_info *ti = current_thread_info(); |
| 394 | u32 fpexc = fmrx(FPEXC); |
| 395 | |
| 396 | /* if vfp is on, then save state for resumption */ |
| 397 | if (fpexc & FPEXC_EN) { |
| 398 | printk(KERN_DEBUG "%s: saving vfp state\n", __func__); |
| 399 | vfp_save_state(&ti->vfpstate, fpexc); |
| 400 | |
| 401 | /* disable, just in case */ |
| 402 | fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN); |
| 403 | } |
| 404 | |
| 405 | /* clear any information we had about last context state */ |
| 406 | memset(last_VFP_context, 0, sizeof(last_VFP_context)); |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | static int vfp_pm_resume(struct sys_device *dev) |
| 412 | { |
| 413 | /* ensure we have access to the vfp */ |
| 414 | vfp_enable(NULL); |
| 415 | |
| 416 | /* and disable it to ensure the next usage restores the state */ |
| 417 | fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN); |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static struct sysdev_class vfp_pm_sysclass = { |
| 423 | .name = "vfp", |
| 424 | .suspend = vfp_pm_suspend, |
| 425 | .resume = vfp_pm_resume, |
| 426 | }; |
| 427 | |
| 428 | static struct sys_device vfp_pm_sysdev = { |
| 429 | .cls = &vfp_pm_sysclass, |
| 430 | }; |
| 431 | |
| 432 | static void vfp_pm_init(void) |
| 433 | { |
| 434 | sysdev_class_register(&vfp_pm_sysclass); |
| 435 | sysdev_register(&vfp_pm_sysdev); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | #else |
| 440 | static inline void vfp_pm_init(void) { } |
| 441 | #endif /* CONFIG_PM */ |
| 442 | |
Russell King | ad187f9 | 2010-02-06 11:36:23 +0000 | [diff] [blame] | 443 | void vfp_sync_hwstate(struct thread_info *thread) |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 444 | { |
| 445 | unsigned int cpu = get_cpu(); |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 446 | |
| 447 | /* |
Russell King | 54cb3db | 2010-02-06 11:27:45 +0000 | [diff] [blame] | 448 | * If the thread we're interested in is the current owner of the |
| 449 | * hardware VFP state, then we need to save its state. |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 450 | */ |
Russell King | 54cb3db | 2010-02-06 11:27:45 +0000 | [diff] [blame] | 451 | if (last_VFP_context[cpu] == &thread->vfpstate) { |
| 452 | u32 fpexc = fmrx(FPEXC); |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 453 | |
Russell King | 54cb3db | 2010-02-06 11:27:45 +0000 | [diff] [blame] | 454 | /* |
| 455 | * Save the last VFP state on this CPU. |
| 456 | */ |
| 457 | fmxr(FPEXC, fpexc | FPEXC_EN); |
| 458 | vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN); |
Russell King | ad187f9 | 2010-02-06 11:36:23 +0000 | [diff] [blame] | 459 | fmxr(FPEXC, fpexc); |
| 460 | } |
| 461 | |
| 462 | put_cpu(); |
| 463 | } |
| 464 | |
| 465 | void vfp_flush_hwstate(struct thread_info *thread) |
| 466 | { |
| 467 | unsigned int cpu = get_cpu(); |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 468 | |
| 469 | /* |
Russell King | ad187f9 | 2010-02-06 11:36:23 +0000 | [diff] [blame] | 470 | * If the thread we're interested in is the current owner of the |
| 471 | * hardware VFP state, then we need to save its state. |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 472 | */ |
Russell King | ad187f9 | 2010-02-06 11:36:23 +0000 | [diff] [blame] | 473 | if (last_VFP_context[cpu] == &thread->vfpstate) { |
| 474 | u32 fpexc = fmrx(FPEXC); |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 475 | |
Russell King | 54cb3db | 2010-02-06 11:27:45 +0000 | [diff] [blame] | 476 | fmxr(FPEXC, fpexc & ~FPEXC_EN); |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 477 | |
Russell King | 54cb3db | 2010-02-06 11:27:45 +0000 | [diff] [blame] | 478 | /* |
| 479 | * Set the context to NULL to force a reload the next time |
| 480 | * the thread uses the VFP. |
| 481 | */ |
| 482 | last_VFP_context[cpu] = NULL; |
| 483 | } |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 484 | |
Imre Deak | 5c5cac6 | 2010-04-11 15:57:07 +0100 | [diff] [blame] | 485 | #ifdef CONFIG_SMP |
| 486 | /* |
| 487 | * For SMP we still have to take care of the case where the thread |
| 488 | * migrates to another CPU and then back to the original CPU on which |
| 489 | * the last VFP user is still the same thread. Mark the thread VFP |
| 490 | * state as belonging to a non-existent CPU so that the saved one will |
| 491 | * be reloaded in the above case. |
| 492 | */ |
| 493 | thread->vfpstate.hard.cpu = NR_CPUS; |
| 494 | #endif |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 495 | put_cpu(); |
| 496 | } |
Catalin Marinas | 3d1228e | 2009-02-11 13:12:56 +0100 | [diff] [blame] | 497 | |
Russell King | 90b4419 | 2010-12-18 10:59:49 +0000 | [diff] [blame] | 498 | /* |
| 499 | * VFP hardware can lose all context when a CPU goes offline. |
Russell King | 74c25be | 2011-01-31 14:43:25 +0000 | [diff] [blame] | 500 | * As we will be running in SMP mode with CPU hotplug, we will save the |
| 501 | * hardware state at every thread switch. We clear our held state when |
| 502 | * a CPU has been killed, indicating that the VFP hardware doesn't contain |
| 503 | * a threads VFP state. When a CPU starts up, we re-enable access to the |
| 504 | * VFP hardware. |
Russell King | 90b4419 | 2010-12-18 10:59:49 +0000 | [diff] [blame] | 505 | * |
| 506 | * Both CPU_DYING and CPU_STARTING are called on the CPU which |
| 507 | * is being offlined/onlined. |
| 508 | */ |
| 509 | static int vfp_hotplug(struct notifier_block *b, unsigned long action, |
| 510 | void *hcpu) |
| 511 | { |
| 512 | if (action == CPU_DYING || action == CPU_DYING_FROZEN) { |
| 513 | unsigned int cpu = (long)hcpu; |
| 514 | last_VFP_context[cpu] = NULL; |
| 515 | } else if (action == CPU_STARTING || action == CPU_STARTING_FROZEN) |
| 516 | vfp_enable(NULL); |
| 517 | return NOTIFY_OK; |
| 518 | } |
Russell King | 8e14036 | 2007-01-02 23:40:30 +0000 | [diff] [blame] | 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | /* |
| 521 | * VFP support code initialisation. |
| 522 | */ |
| 523 | static int __init vfp_init(void) |
| 524 | { |
| 525 | unsigned int vfpsid; |
Russell King | efe90d2 | 2006-12-08 15:22:20 +0000 | [diff] [blame] | 526 | unsigned int cpu_arch = cpu_architecture(); |
Russell King | efe90d2 | 2006-12-08 15:22:20 +0000 | [diff] [blame] | 527 | |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 528 | if (cpu_arch >= CPU_ARCH_ARMv6) |
| 529 | vfp_enable(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | |
| 531 | /* |
| 532 | * First check that there is a VFP that we can use. |
| 533 | * The handler is already setup to just log calls, so |
| 534 | * we just need to read the VFPSID register. |
| 535 | */ |
Russell King | 5d4cae5 | 2007-06-10 12:22:20 +0100 | [diff] [blame] | 536 | vfp_vector = vfp_testing_entry; |
Tzachi Perelstein | b9338a7 | 2007-09-09 14:24:59 +0100 | [diff] [blame] | 537 | barrier(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | vfpsid = fmrx(FPSID); |
Russell King | 8e14036 | 2007-01-02 23:40:30 +0000 | [diff] [blame] | 539 | barrier(); |
Russell King | 5d4cae5 | 2007-06-10 12:22:20 +0100 | [diff] [blame] | 540 | vfp_vector = vfp_null_entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | printk(KERN_INFO "VFP support v0.3: "); |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 543 | if (VFP_arch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | printk("not present\n"); |
Catalin Marinas | c98929c | 2007-11-22 18:32:01 +0100 | [diff] [blame] | 545 | else if (vfpsid & FPSID_NODOUBLE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | printk("no double precision support\n"); |
| 547 | } else { |
Russell King | 90b4419 | 2010-12-18 10:59:49 +0000 | [diff] [blame] | 548 | hotcpu_notifier(vfp_hotplug, 0); |
| 549 | |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 550 | smp_call_function(vfp_enable, NULL, 1); |
Russell King | 8e14036 | 2007-01-02 23:40:30 +0000 | [diff] [blame] | 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT; /* Extract the architecture version */ |
| 553 | printk("implementor %02x architecture %d part %02x variant %x rev %x\n", |
| 554 | (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT, |
| 555 | (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT, |
| 556 | (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT, |
| 557 | (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT, |
| 558 | (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT); |
Russell King | efe90d2 | 2006-12-08 15:22:20 +0000 | [diff] [blame] | 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | vfp_vector = vfp_support_entry; |
Russell King | d6551e8 | 2006-06-21 13:31:52 +0100 | [diff] [blame] | 561 | |
| 562 | thread_register_notifier(&vfp_notifier_block); |
Ben Dooks | fc0b7a2 | 2008-12-18 12:26:54 +0100 | [diff] [blame] | 563 | vfp_pm_init(); |
Russell King | efe90d2 | 2006-12-08 15:22:20 +0000 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * We detected VFP, and the support code is |
| 567 | * in place; report VFP support to userspace. |
| 568 | */ |
| 569 | elf_hwcap |= HWCAP_VFP; |
Catalin Marinas | 7279dc3 | 2009-02-11 13:13:56 +0100 | [diff] [blame] | 570 | #ifdef CONFIG_VFPv3 |
Catalin Marinas | 325ffc3 | 2010-03-26 15:44:57 +0100 | [diff] [blame] | 571 | if (VFP_arch >= 2) { |
Catalin Marinas | 7279dc3 | 2009-02-11 13:13:56 +0100 | [diff] [blame] | 572 | elf_hwcap |= HWCAP_VFPv3; |
| 573 | |
| 574 | /* |
| 575 | * Check for VFPv3 D16. CPUs in this configuration |
| 576 | * only have 16 x 64bit registers. |
| 577 | */ |
| 578 | if (((fmrx(MVFR0) & MVFR0_A_SIMD_MASK)) == 1) |
| 579 | elf_hwcap |= HWCAP_VFPv3D16; |
| 580 | } |
| 581 | #endif |
Catalin Marinas | 2bedbdf | 2008-11-06 13:23:07 +0000 | [diff] [blame] | 582 | #ifdef CONFIG_NEON |
| 583 | /* |
| 584 | * Check for the presence of the Advanced SIMD |
| 585 | * load/store instructions, integer and single |
Tony Lindgren | 5aaf254 | 2010-07-01 13:41:05 +0100 | [diff] [blame] | 586 | * precision floating point operations. Only check |
| 587 | * for NEON if the hardware has the MVFR registers. |
Catalin Marinas | 2bedbdf | 2008-11-06 13:23:07 +0000 | [diff] [blame] | 588 | */ |
Tony Lindgren | 5aaf254 | 2010-07-01 13:41:05 +0100 | [diff] [blame] | 589 | if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) { |
| 590 | if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100) |
| 591 | elf_hwcap |= HWCAP_NEON; |
| 592 | } |
Catalin Marinas | 2bedbdf | 2008-11-06 13:23:07 +0000 | [diff] [blame] | 593 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | late_initcall(vfp_init); |