mmap: avoid unnecessary anon_vma lock acquisition in vma_adjust()
[linux-2.6/mini2440.git] / arch / arm / vfp / vfpmodule.c
blob01599c4ef7266f9f3eb27b56bbe0cdd3f3931121
1 /*
2 * linux/arch/arm/vfp/vfpmodule.c
4 * Copyright (C) 2004 ARM Limited.
5 * Written by Deep Blue Solutions Limited.
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.
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/init.h>
18 #include <asm/thread_notify.h>
19 #include <asm/vfp.h>
21 #include "vfpinstr.h"
22 #include "vfp.h"
25 * Our undef handlers (in entry.S)
27 void vfp_testing_entry(void);
28 void vfp_support_entry(void);
29 void vfp_null_entry(void);
31 void (*vfp_vector)(void) = vfp_null_entry;
32 union vfp_state *last_VFP_context[NR_CPUS];
35 * Dual-use variable.
36 * Used in startup: set to non-zero if VFP checks fail
37 * After startup, holds VFP architecture
39 unsigned int VFP_arch;
41 static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
43 struct thread_info *thread = v;
44 union vfp_state *vfp;
45 __u32 cpu = thread->cpu;
47 if (likely(cmd == THREAD_NOTIFY_SWITCH)) {
48 u32 fpexc = fmrx(FPEXC);
50 #ifdef CONFIG_SMP
52 * On SMP, if VFP is enabled, save the old state in
53 * case the thread migrates to a different CPU. The
54 * restoring is done lazily.
56 if ((fpexc & FPEXC_EN) && last_VFP_context[cpu]) {
57 vfp_save_state(last_VFP_context[cpu], fpexc);
58 last_VFP_context[cpu]->hard.cpu = cpu;
61 * Thread migration, just force the reloading of the
62 * state on the new CPU in case the VFP registers
63 * contain stale data.
65 if (thread->vfpstate.hard.cpu != cpu)
66 last_VFP_context[cpu] = NULL;
67 #endif
70 * Always disable VFP so we can lazily save/restore the
71 * old state.
73 fmxr(FPEXC, fpexc & ~FPEXC_EN);
74 return NOTIFY_DONE;
77 vfp = &thread->vfpstate;
78 if (cmd == THREAD_NOTIFY_FLUSH) {
80 * Per-thread VFP initialisation.
82 memset(vfp, 0, sizeof(union vfp_state));
84 vfp->hard.fpexc = FPEXC_EN;
85 vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
88 * Disable VFP to ensure we initialise it first.
90 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
93 /* flush and release case: Per-thread VFP cleanup. */
94 if (last_VFP_context[cpu] == vfp)
95 last_VFP_context[cpu] = NULL;
97 return NOTIFY_DONE;
100 static struct notifier_block vfp_notifier_block = {
101 .notifier_call = vfp_notifier,
105 * Raise a SIGFPE for the current process.
106 * sicode describes the signal being raised.
108 void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
110 siginfo_t info;
112 memset(&info, 0, sizeof(info));
114 info.si_signo = SIGFPE;
115 info.si_code = sicode;
116 info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
119 * This is the same as NWFPE, because it's not clear what
120 * this is used for
122 current->thread.error_code = 0;
123 current->thread.trap_no = 6;
125 send_sig_info(SIGFPE, &info, current);
128 static void vfp_panic(char *reason, u32 inst)
130 int i;
132 printk(KERN_ERR "VFP: Error: %s\n", reason);
133 printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
134 fmrx(FPEXC), fmrx(FPSCR), inst);
135 for (i = 0; i < 32; i += 2)
136 printk(KERN_ERR "VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
137 i, vfp_get_float(i), i+1, vfp_get_float(i+1));
141 * Process bitmask of exception conditions.
143 static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
145 int si_code = 0;
147 pr_debug("VFP: raising exceptions %08x\n", exceptions);
149 if (exceptions == VFP_EXCEPTION_ERROR) {
150 vfp_panic("unhandled bounce", inst);
151 vfp_raise_sigfpe(0, regs);
152 return;
156 * Update the FPSCR with the additional exception flags.
157 * Comparison instructions always return at least one of
158 * these flags set.
160 fpscr |= exceptions;
162 fmxr(FPSCR, fpscr);
164 #define RAISE(stat,en,sig) \
165 if (exceptions & stat && fpscr & en) \
166 si_code = sig;
169 * These are arranged in priority order, least to highest.
171 RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
172 RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
173 RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
174 RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
175 RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
177 if (si_code)
178 vfp_raise_sigfpe(si_code, regs);
182 * Emulate a VFP instruction.
184 static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
186 u32 exceptions = VFP_EXCEPTION_ERROR;
188 pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
190 if (INST_CPRTDO(inst)) {
191 if (!INST_CPRT(inst)) {
193 * CPDO
195 if (vfp_single(inst)) {
196 exceptions = vfp_single_cpdo(inst, fpscr);
197 } else {
198 exceptions = vfp_double_cpdo(inst, fpscr);
200 } else {
202 * A CPRT instruction can not appear in FPINST2, nor
203 * can it cause an exception. Therefore, we do not
204 * have to emulate it.
207 } else {
209 * A CPDT instruction can not appear in FPINST2, nor can
210 * it cause an exception. Therefore, we do not have to
211 * emulate it.
214 return exceptions & ~VFP_NAN_FLAG;
218 * Package up a bounce condition.
220 void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
222 u32 fpscr, orig_fpscr, fpsid, exceptions;
224 pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
227 * At this point, FPEXC can have the following configuration:
229 * EX DEX IXE
230 * 0 1 x - synchronous exception
231 * 1 x 0 - asynchronous exception
232 * 1 x 1 - sychronous on VFP subarch 1 and asynchronous on later
233 * 0 0 1 - synchronous on VFP9 (non-standard subarch 1
234 * implementation), undefined otherwise
236 * Clear various bits and enable access to the VFP so we can
237 * handle the bounce.
239 fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
241 fpsid = fmrx(FPSID);
242 orig_fpscr = fpscr = fmrx(FPSCR);
245 * Check for the special VFP subarch 1 and FPSCR.IXE bit case
247 if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
248 && (fpscr & FPSCR_IXE)) {
250 * Synchronous exception, emulate the trigger instruction
252 goto emulate;
255 if (fpexc & FPEXC_EX) {
257 * Asynchronous exception. The instruction is read from FPINST
258 * and the interrupted instruction has to be restarted.
260 trigger = fmrx(FPINST);
261 regs->ARM_pc -= 4;
262 } else if (!(fpexc & FPEXC_DEX)) {
264 * Illegal combination of bits. It can be caused by an
265 * unallocated VFP instruction but with FPSCR.IXE set and not
266 * on VFP subarch 1.
268 vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
269 goto exit;
273 * Modify fpscr to indicate the number of iterations remaining.
274 * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
275 * whether FPEXC.VECITR or FPSCR.LEN is used.
277 if (fpexc & (FPEXC_EX | FPEXC_VV)) {
278 u32 len;
280 len = fpexc + (1 << FPEXC_LENGTH_BIT);
282 fpscr &= ~FPSCR_LENGTH_MASK;
283 fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
287 * Handle the first FP instruction. We used to take note of the
288 * FPEXC bounce reason, but this appears to be unreliable.
289 * Emulate the bounced instruction instead.
291 exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
292 if (exceptions)
293 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
296 * If there isn't a second FP instruction, exit now. Note that
297 * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
299 if (fpexc ^ (FPEXC_EX | FPEXC_FP2V))
300 goto exit;
303 * The barrier() here prevents fpinst2 being read
304 * before the condition above.
306 barrier();
307 trigger = fmrx(FPINST2);
309 emulate:
310 exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
311 if (exceptions)
312 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
313 exit:
314 preempt_enable();
317 static void vfp_enable(void *unused)
319 u32 access = get_copro_access();
322 * Enable full access to VFP (cp10 and cp11)
324 set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
327 #ifdef CONFIG_PM
328 #include <linux/sysdev.h>
330 static int vfp_pm_suspend(struct sys_device *dev, pm_message_t state)
332 struct thread_info *ti = current_thread_info();
333 u32 fpexc = fmrx(FPEXC);
335 /* if vfp is on, then save state for resumption */
336 if (fpexc & FPEXC_EN) {
337 printk(KERN_DEBUG "%s: saving vfp state\n", __func__);
338 vfp_save_state(&ti->vfpstate, fpexc);
340 /* disable, just in case */
341 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
344 /* clear any information we had about last context state */
345 memset(last_VFP_context, 0, sizeof(last_VFP_context));
347 return 0;
350 static int vfp_pm_resume(struct sys_device *dev)
352 /* ensure we have access to the vfp */
353 vfp_enable(NULL);
355 /* and disable it to ensure the next usage restores the state */
356 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
358 return 0;
361 static struct sysdev_class vfp_pm_sysclass = {
362 .name = "vfp",
363 .suspend = vfp_pm_suspend,
364 .resume = vfp_pm_resume,
367 static struct sys_device vfp_pm_sysdev = {
368 .cls = &vfp_pm_sysclass,
371 static void vfp_pm_init(void)
373 sysdev_class_register(&vfp_pm_sysclass);
374 sysdev_register(&vfp_pm_sysdev);
378 #else
379 static inline void vfp_pm_init(void) { }
380 #endif /* CONFIG_PM */
383 * Synchronise the hardware VFP state of a thread other than current with the
384 * saved one. This function is used by the ptrace mechanism.
386 #ifdef CONFIG_SMP
387 void vfp_sync_state(struct thread_info *thread)
390 * On SMP systems, the VFP state is automatically saved at every
391 * context switch. We mark the thread VFP state as belonging to a
392 * non-existent CPU so that the saved one will be reloaded when
393 * needed.
395 thread->vfpstate.hard.cpu = NR_CPUS;
397 #else
398 void vfp_sync_state(struct thread_info *thread)
400 unsigned int cpu = get_cpu();
401 u32 fpexc = fmrx(FPEXC);
404 * If VFP is enabled, the previous state was already saved and
405 * last_VFP_context updated.
407 if (fpexc & FPEXC_EN)
408 goto out;
410 if (!last_VFP_context[cpu])
411 goto out;
414 * Save the last VFP state on this CPU.
416 fmxr(FPEXC, fpexc | FPEXC_EN);
417 vfp_save_state(last_VFP_context[cpu], fpexc);
418 fmxr(FPEXC, fpexc);
421 * Set the context to NULL to force a reload the next time the thread
422 * uses the VFP.
424 last_VFP_context[cpu] = NULL;
426 out:
427 put_cpu();
429 #endif
431 #include <linux/smp.h>
434 * VFP support code initialisation.
436 static int __init vfp_init(void)
438 unsigned int vfpsid;
439 unsigned int cpu_arch = cpu_architecture();
441 if (cpu_arch >= CPU_ARCH_ARMv6)
442 vfp_enable(NULL);
445 * First check that there is a VFP that we can use.
446 * The handler is already setup to just log calls, so
447 * we just need to read the VFPSID register.
449 vfp_vector = vfp_testing_entry;
450 barrier();
451 vfpsid = fmrx(FPSID);
452 barrier();
453 vfp_vector = vfp_null_entry;
455 printk(KERN_INFO "VFP support v0.3: ");
456 if (VFP_arch)
457 printk("not present\n");
458 else if (vfpsid & FPSID_NODOUBLE) {
459 printk("no double precision support\n");
460 } else {
461 smp_call_function(vfp_enable, NULL, 1);
463 VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT; /* Extract the architecture version */
464 printk("implementor %02x architecture %d part %02x variant %x rev %x\n",
465 (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
466 (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT,
467 (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
468 (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
469 (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
471 vfp_vector = vfp_support_entry;
473 thread_register_notifier(&vfp_notifier_block);
474 vfp_pm_init();
477 * We detected VFP, and the support code is
478 * in place; report VFP support to userspace.
480 elf_hwcap |= HWCAP_VFP;
481 #ifdef CONFIG_VFPv3
482 if (VFP_arch >= 3) {
483 elf_hwcap |= HWCAP_VFPv3;
486 * Check for VFPv3 D16. CPUs in this configuration
487 * only have 16 x 64bit registers.
489 if (((fmrx(MVFR0) & MVFR0_A_SIMD_MASK)) == 1)
490 elf_hwcap |= HWCAP_VFPv3D16;
492 #endif
493 #ifdef CONFIG_NEON
495 * Check for the presence of the Advanced SIMD
496 * load/store instructions, integer and single
497 * precision floating point operations.
499 if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100)
500 elf_hwcap |= HWCAP_NEON;
501 #endif
503 return 0;
506 late_initcall(vfp_init);