blob: 3554500657f1bc357fa8d4bf73a1e6f5e49c25cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
Russell King90b44192010-12-18 10:59:49 +000013#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Russell King90b44192010-12-18 10:59:49 +000015#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/signal.h>
17#include <linux/sched.h>
Russell King90b44192010-12-18 10:59:49 +000018#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
Russell Kingd6551e82006-06-21 13:31:52 +010020
Tony Lindgren5aaf2542010-07-01 13:41:05 +010021#include <asm/cputype.h>
Russell Kingd6551e82006-06-21 13:31:52 +010022#include <asm/thread_notify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/vfp.h>
24
25#include "vfpinstr.h"
26#include "vfp.h"
27
28/*
29 * Our undef handlers (in entry.S)
30 */
31void vfp_testing_entry(void);
32void vfp_support_entry(void);
Russell King5d4cae52007-06-10 12:22:20 +010033void vfp_null_entry(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Russell King5d4cae52007-06-10 12:22:20 +010035void (*vfp_vector)(void) = vfp_null_entry;
Catalin Marinasc6428462007-01-24 18:47:08 +010036union vfp_state *last_VFP_context[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * Dual-use variable.
40 * Used in startup: set to non-zero if VFP checks fail
41 * After startup, holds VFP architecture
42 */
43unsigned int VFP_arch;
44
Russell King0d782dc2009-12-12 14:47:40 +000045/*
46 * Per-thread VFP initialization.
47 */
48static 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 King797245f2009-12-18 14:34:43 +000070static void vfp_thread_exit(struct thread_info *thread)
Russell King0d782dc2009-12-12 14:47:40 +000071{
72 /* release case: Per-thread VFP cleanup. */
73 union vfp_state *vfp = &thread->vfpstate;
Russell King797245f2009-12-18 14:34:43 +000074 unsigned int cpu = get_cpu();
Russell King0d782dc2009-12-12 14:47:40 +000075
76 if (last_VFP_context[cpu] == vfp)
77 last_VFP_context[cpu] = NULL;
Russell King797245f2009-12-18 14:34:43 +000078 put_cpu();
Russell King0d782dc2009-12-12 14:47:40 +000079}
80
Catalin Marinasc98c0972011-04-06 16:17:17 +010081static void vfp_thread_copy(struct thread_info *thread)
82{
83 struct thread_info *parent = current_thread_info();
84
85 vfp_sync_hwstate(parent);
86 thread->vfpstate = parent->vfpstate;
87}
88
Russell King0d782dc2009-12-12 14:47:40 +000089/*
90 * When this function is called with the following 'cmd's, the following
91 * is true while this function is being run:
92 * THREAD_NOFTIFY_SWTICH:
93 * - the previously running thread will not be scheduled onto another CPU.
94 * - the next thread to be run (v) will not be running on another CPU.
95 * - thread->cpu is the local CPU number
96 * - not preemptible as we're called in the middle of a thread switch
97 * THREAD_NOTIFY_FLUSH:
98 * - the thread (v) will be running on the local CPU, so
99 * v === current_thread_info()
100 * - thread->cpu is the local CPU number at the time it is accessed,
101 * but may change at any time.
102 * - we could be preempted if tree preempt rcu is enabled, so
103 * it is unsafe to use thread->cpu.
Russell King797245f2009-12-18 14:34:43 +0000104 * THREAD_NOTIFY_EXIT
105 * - the thread (v) will be running on the local CPU, so
106 * v === current_thread_info()
107 * - thread->cpu is the local CPU number at the time it is accessed,
108 * but may change at any time.
109 * - we could be preempted if tree preempt rcu is enabled, so
110 * it is unsafe to use thread->cpu.
Russell King0d782dc2009-12-12 14:47:40 +0000111 */
Russell Kingd6551e82006-06-21 13:31:52 +0100112static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Russell Kingd6551e82006-06-21 13:31:52 +0100114 struct thread_info *thread = v;
Catalin Marinas2e826692011-04-06 16:16:29 +0100115 u32 fpexc;
116#ifdef CONFIG_SMP
117 unsigned int cpu;
118#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Catalin Marinas2e826692011-04-06 16:16:29 +0100120 switch (cmd) {
121 case THREAD_NOTIFY_SWITCH:
122 fpexc = fmrx(FPEXC);
Catalin Marinasc6428462007-01-24 18:47:08 +0100123
124#ifdef CONFIG_SMP
Catalin Marinas2e826692011-04-06 16:16:29 +0100125 cpu = thread->cpu;
Russell King0d782dc2009-12-12 14:47:40 +0000126
Catalin Marinasc6428462007-01-24 18:47:08 +0100127 /*
128 * On SMP, if VFP is enabled, save the old state in
129 * case the thread migrates to a different CPU. The
130 * restoring is done lazily.
131 */
Russell King228adef2007-07-18 09:37:10 +0100132 if ((fpexc & FPEXC_EN) && last_VFP_context[cpu]) {
Catalin Marinasc6428462007-01-24 18:47:08 +0100133 vfp_save_state(last_VFP_context[cpu], fpexc);
134 last_VFP_context[cpu]->hard.cpu = cpu;
135 }
136 /*
137 * Thread migration, just force the reloading of the
138 * state on the new CPU in case the VFP registers
139 * contain stale data.
140 */
141 if (thread->vfpstate.hard.cpu != cpu)
142 last_VFP_context[cpu] = NULL;
143#endif
144
Russell King681a4992006-08-27 12:38:34 +0100145 /*
146 * Always disable VFP so we can lazily save/restore the
147 * old state.
148 */
Russell King228adef2007-07-18 09:37:10 +0100149 fmxr(FPEXC, fpexc & ~FPEXC_EN);
Catalin Marinas2e826692011-04-06 16:16:29 +0100150 break;
Russell King681a4992006-08-27 12:38:34 +0100151
Catalin Marinas2e826692011-04-06 16:16:29 +0100152 case THREAD_NOTIFY_FLUSH:
Russell King0d782dc2009-12-12 14:47:40 +0000153 vfp_thread_flush(thread);
Catalin Marinas2e826692011-04-06 16:16:29 +0100154 break;
155
156 case THREAD_NOTIFY_EXIT:
Russell King797245f2009-12-18 14:34:43 +0000157 vfp_thread_exit(thread);
Catalin Marinas2e826692011-04-06 16:16:29 +0100158 break;
Catalin Marinasc98c0972011-04-06 16:17:17 +0100159
160 case THREAD_NOTIFY_COPY:
161 vfp_thread_copy(thread);
162 break;
Catalin Marinas2e826692011-04-06 16:16:29 +0100163 }
Russell King681a4992006-08-27 12:38:34 +0100164
Russell Kingd6551e82006-06-21 13:31:52 +0100165 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Russell Kingd6551e82006-06-21 13:31:52 +0100168static struct notifier_block vfp_notifier_block = {
169 .notifier_call = vfp_notifier,
170};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/*
173 * Raise a SIGFPE for the current process.
174 * sicode describes the signal being raised.
175 */
Russell King2bbd7e92011-01-08 12:05:09 +0000176static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 siginfo_t info;
179
180 memset(&info, 0, sizeof(info));
181
182 info.si_signo = SIGFPE;
183 info.si_code = sicode;
Al Viro35d59fc2006-10-11 17:22:44 +0100184 info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 /*
187 * This is the same as NWFPE, because it's not clear what
188 * this is used for
189 */
190 current->thread.error_code = 0;
191 current->thread.trap_no = 6;
192
Russell Kingda411192005-06-29 23:02:02 +0100193 send_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Catalin Marinasc98929c2007-11-22 18:32:01 +0100196static void vfp_panic(char *reason, u32 inst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 int i;
199
200 printk(KERN_ERR "VFP: Error: %s\n", reason);
201 printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
Catalin Marinasc98929c2007-11-22 18:32:01 +0100202 fmrx(FPEXC), fmrx(FPSCR), inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 for (i = 0; i < 32; i += 2)
204 printk(KERN_ERR "VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
205 i, vfp_get_float(i), i+1, vfp_get_float(i+1));
206}
207
208/*
209 * Process bitmask of exception conditions.
210 */
211static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
212{
213 int si_code = 0;
214
215 pr_debug("VFP: raising exceptions %08x\n", exceptions);
216
Daniel Jacobowitz7c6f2512006-08-27 12:42:08 +0100217 if (exceptions == VFP_EXCEPTION_ERROR) {
Catalin Marinasc98929c2007-11-22 18:32:01 +0100218 vfp_panic("unhandled bounce", inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 vfp_raise_sigfpe(0, regs);
220 return;
221 }
222
223 /*
Catalin Marinasdbead402010-02-01 18:50:40 +0100224 * If any of the status flags are set, update the FPSCR.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 * Comparison instructions always return at least one of
226 * these flags set.
227 */
Catalin Marinasdbead402010-02-01 18:50:40 +0100228 if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
229 fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 fpscr |= exceptions;
232
233 fmxr(FPSCR, fpscr);
234
235#define RAISE(stat,en,sig) \
236 if (exceptions & stat && fpscr & en) \
237 si_code = sig;
238
239 /*
240 * These are arranged in priority order, least to highest.
241 */
Takashi Ohmasae0f205d2006-10-23 11:19:40 +0100242 RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 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
248 if (si_code)
249 vfp_raise_sigfpe(si_code, regs);
250}
251
252/*
253 * Emulate a VFP instruction.
254 */
255static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
256{
Daniel Jacobowitz7c6f2512006-08-27 12:42:08 +0100257 u32 exceptions = VFP_EXCEPTION_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
260
261 if (INST_CPRTDO(inst)) {
262 if (!INST_CPRT(inst)) {
263 /*
264 * CPDO
265 */
266 if (vfp_single(inst)) {
267 exceptions = vfp_single_cpdo(inst, fpscr);
268 } else {
269 exceptions = vfp_double_cpdo(inst, fpscr);
270 }
271 } else {
272 /*
273 * A CPRT instruction can not appear in FPINST2, nor
274 * can it cause an exception. Therefore, we do not
275 * have to emulate it.
276 */
277 }
278 } else {
279 /*
280 * A CPDT instruction can not appear in FPINST2, nor can
281 * it cause an exception. Therefore, we do not have to
282 * emulate it.
283 */
284 }
Russell King928bd1b2006-04-25 20:41:27 +0100285 return exceptions & ~VFP_NAN_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
289 * Package up a bounce condition.
290 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100291void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Catalin Marinasc98929c2007-11-22 18:32:01 +0100293 u32 fpscr, orig_fpscr, fpsid, exceptions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
296
297 /*
Catalin Marinasc98929c2007-11-22 18:32:01 +0100298 * At this point, FPEXC can have the following configuration:
299 *
300 * EX DEX IXE
301 * 0 1 x - synchronous exception
302 * 1 x 0 - asynchronous exception
303 * 1 x 1 - sychronous on VFP subarch 1 and asynchronous on later
304 * 0 0 1 - synchronous on VFP9 (non-standard subarch 1
305 * implementation), undefined otherwise
306 *
307 * Clear various bits and enable access to the VFP so we can
308 * handle the bounce.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100310 fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Catalin Marinasc98929c2007-11-22 18:32:01 +0100312 fpsid = fmrx(FPSID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 orig_fpscr = fpscr = fmrx(FPSCR);
314
315 /*
Catalin Marinasc98929c2007-11-22 18:32:01 +0100316 * Check for the special VFP subarch 1 and FPSCR.IXE bit case
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100318 if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
319 && (fpscr & FPSCR_IXE)) {
320 /*
321 * Synchronous exception, emulate the trigger instruction
322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto emulate;
324 }
325
Catalin Marinasc98929c2007-11-22 18:32:01 +0100326 if (fpexc & FPEXC_EX) {
Catalin Marinas85d69432009-05-30 14:00:18 +0100327#ifndef CONFIG_CPU_FEROCEON
Catalin Marinasc98929c2007-11-22 18:32:01 +0100328 /*
329 * Asynchronous exception. The instruction is read from FPINST
330 * and the interrupted instruction has to be restarted.
331 */
332 trigger = fmrx(FPINST);
333 regs->ARM_pc -= 4;
Catalin Marinas85d69432009-05-30 14:00:18 +0100334#endif
Catalin Marinasc98929c2007-11-22 18:32:01 +0100335 } else if (!(fpexc & FPEXC_DEX)) {
336 /*
337 * Illegal combination of bits. It can be caused by an
338 * unallocated VFP instruction but with FPSCR.IXE set and not
339 * on VFP subarch 1.
340 */
341 vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
George G. Davisf2255be2009-04-01 20:27:18 +0100342 goto exit;
Catalin Marinasc98929c2007-11-22 18:32:01 +0100343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /*
Catalin Marinasc98929c2007-11-22 18:32:01 +0100346 * Modify fpscr to indicate the number of iterations remaining.
347 * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
348 * whether FPEXC.VECITR or FPSCR.LEN is used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100350 if (fpexc & (FPEXC_EX | FPEXC_VV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 u32 len;
352
353 len = fpexc + (1 << FPEXC_LENGTH_BIT);
354
355 fpscr &= ~FPSCR_LENGTH_MASK;
356 fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
357 }
358
359 /*
360 * Handle the first FP instruction. We used to take note of the
361 * FPEXC bounce reason, but this appears to be unreliable.
362 * Emulate the bounced instruction instead.
363 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100364 exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (exceptions)
Catalin Marinasc98929c2007-11-22 18:32:01 +0100366 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /*
Catalin Marinasc98929c2007-11-22 18:32:01 +0100369 * If there isn't a second FP instruction, exit now. Note that
370 * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100372 if (fpexc ^ (FPEXC_EX | FPEXC_FP2V))
George G. Davisf2255be2009-04-01 20:27:18 +0100373 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /*
376 * The barrier() here prevents fpinst2 being read
377 * before the condition above.
378 */
379 barrier();
380 trigger = fmrx(FPINST2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 emulate:
Catalin Marinasc98929c2007-11-22 18:32:01 +0100383 exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (exceptions)
385 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
George G. Davisf2255be2009-04-01 20:27:18 +0100386 exit:
387 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
Russell Kingefe90d22006-12-08 15:22:20 +0000389
Russell King8e140362007-01-02 23:40:30 +0000390static void vfp_enable(void *unused)
391{
392 u32 access = get_copro_access();
393
394 /*
395 * Enable full access to VFP (cp10 and cp11)
396 */
397 set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
398}
399
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400int vfp_flush_context(void)
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100401{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700402 unsigned long flags;
403 struct thread_info *ti;
404 u32 fpexc;
405 u32 cpu;
406 int saved = 0;
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100407
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408 local_irq_save(flags);
409
410 ti = current_thread_info();
411 fpexc = fmrx(FPEXC);
412 cpu = ti->cpu;
413
414#ifdef CONFIG_SMP
415 /* On SMP, if VFP is enabled, save the old state */
416 if ((fpexc & FPEXC_EN) && last_VFP_context[cpu]) {
417 last_VFP_context[cpu]->hard.cpu = cpu;
418#else
419 /* If there is a VFP context we must save it. */
420 if (last_VFP_context[cpu]) {
421 /* Enable VFP so we can save the old state. */
422 fmxr(FPEXC, fpexc | FPEXC_EN);
423 isb();
424#endif
425 vfp_save_state(last_VFP_context[cpu], fpexc);
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100426
427 /* disable, just in case */
428 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700429 saved = 1;
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100430 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 last_VFP_context[cpu] = NULL;
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100432
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700433 local_irq_restore(flags);
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100434
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 return saved;
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100436}
437
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438void vfp_reinit(void)
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100439{
440 /* ensure we have access to the vfp */
441 vfp_enable(NULL);
442
443 /* and disable it to ensure the next usage restores the state */
444 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100445}
446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700447#ifdef CONFIG_PM
448#include <linux/syscore_ops.h>
449
450static int vfp_pm_suspend(void)
451{
452 vfp_flush_context();
453
454 return 0;
455}
456
457static void vfp_pm_resume(void)
458{
459 vfp_reinit();
460}
461
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200462static struct syscore_ops vfp_pm_syscore_ops = {
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100463 .suspend = vfp_pm_suspend,
464 .resume = vfp_pm_resume,
465};
466
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100467static void vfp_pm_init(void)
468{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200469 register_syscore_ops(&vfp_pm_syscore_ops);
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100470}
471
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100472#else
473static inline void vfp_pm_init(void) { }
474#endif /* CONFIG_PM */
475
Russell Kingad187f92010-02-06 11:36:23 +0000476void vfp_sync_hwstate(struct thread_info *thread)
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100477{
478 unsigned int cpu = get_cpu();
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100479
480 /*
Russell King54cb3db2010-02-06 11:27:45 +0000481 * If the thread we're interested in is the current owner of the
482 * hardware VFP state, then we need to save its state.
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100483 */
Russell King54cb3db2010-02-06 11:27:45 +0000484 if (last_VFP_context[cpu] == &thread->vfpstate) {
485 u32 fpexc = fmrx(FPEXC);
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100486
Russell King54cb3db2010-02-06 11:27:45 +0000487 /*
488 * Save the last VFP state on this CPU.
489 */
490 fmxr(FPEXC, fpexc | FPEXC_EN);
491 vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN);
Russell Kingad187f92010-02-06 11:36:23 +0000492 fmxr(FPEXC, fpexc);
493 }
494
495 put_cpu();
496}
497
498void vfp_flush_hwstate(struct thread_info *thread)
499{
500 unsigned int cpu = get_cpu();
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100501
502 /*
Russell Kingad187f92010-02-06 11:36:23 +0000503 * If the thread we're interested in is the current owner of the
504 * hardware VFP state, then we need to save its state.
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100505 */
Russell Kingad187f92010-02-06 11:36:23 +0000506 if (last_VFP_context[cpu] == &thread->vfpstate) {
507 u32 fpexc = fmrx(FPEXC);
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100508
Russell King54cb3db2010-02-06 11:27:45 +0000509 fmxr(FPEXC, fpexc & ~FPEXC_EN);
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100510
Russell King54cb3db2010-02-06 11:27:45 +0000511 /*
512 * Set the context to NULL to force a reload the next time
513 * the thread uses the VFP.
514 */
515 last_VFP_context[cpu] = NULL;
516 }
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100517
Imre Deak5c5cac62010-04-11 15:57:07 +0100518#ifdef CONFIG_SMP
519 /*
520 * For SMP we still have to take care of the case where the thread
521 * migrates to another CPU and then back to the original CPU on which
522 * the last VFP user is still the same thread. Mark the thread VFP
523 * state as belonging to a non-existent CPU so that the saved one will
524 * be reloaded in the above case.
525 */
526 thread->vfpstate.hard.cpu = NR_CPUS;
527#endif
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100528 put_cpu();
529}
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100530
Russell King90b44192010-12-18 10:59:49 +0000531/*
532 * VFP hardware can lose all context when a CPU goes offline.
Russell King74c25be2011-01-31 14:43:25 +0000533 * As we will be running in SMP mode with CPU hotplug, we will save the
534 * hardware state at every thread switch. We clear our held state when
535 * a CPU has been killed, indicating that the VFP hardware doesn't contain
536 * a threads VFP state. When a CPU starts up, we re-enable access to the
537 * VFP hardware.
Russell King90b44192010-12-18 10:59:49 +0000538 *
539 * Both CPU_DYING and CPU_STARTING are called on the CPU which
540 * is being offlined/onlined.
541 */
542static int vfp_hotplug(struct notifier_block *b, unsigned long action,
543 void *hcpu)
544{
545 if (action == CPU_DYING || action == CPU_DYING_FROZEN) {
546 unsigned int cpu = (long)hcpu;
547 last_VFP_context[cpu] = NULL;
548 } else if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
549 vfp_enable(NULL);
550 return NOTIFY_OK;
551}
Russell King8e140362007-01-02 23:40:30 +0000552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/*
554 * VFP support code initialisation.
555 */
556static int __init vfp_init(void)
557{
558 unsigned int vfpsid;
Russell Kingefe90d22006-12-08 15:22:20 +0000559 unsigned int cpu_arch = cpu_architecture();
Russell Kingefe90d22006-12-08 15:22:20 +0000560
Catalin Marinasc98929c2007-11-22 18:32:01 +0100561 if (cpu_arch >= CPU_ARCH_ARMv6)
562 vfp_enable(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /*
565 * First check that there is a VFP that we can use.
566 * The handler is already setup to just log calls, so
567 * we just need to read the VFPSID register.
568 */
Russell King5d4cae52007-06-10 12:22:20 +0100569 vfp_vector = vfp_testing_entry;
Tzachi Perelsteinb9338a72007-09-09 14:24:59 +0100570 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 vfpsid = fmrx(FPSID);
Russell King8e140362007-01-02 23:40:30 +0000572 barrier();
Russell King5d4cae52007-06-10 12:22:20 +0100573 vfp_vector = vfp_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 printk(KERN_INFO "VFP support v0.3: ");
Catalin Marinasc98929c2007-11-22 18:32:01 +0100576 if (VFP_arch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 printk("not present\n");
Catalin Marinasc98929c2007-11-22 18:32:01 +0100578 else if (vfpsid & FPSID_NODOUBLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 printk("no double precision support\n");
580 } else {
Russell King90b44192010-12-18 10:59:49 +0000581 hotcpu_notifier(vfp_hotplug, 0);
582
Jens Axboe8691e5a2008-06-06 11:18:06 +0200583 smp_call_function(vfp_enable, NULL, 1);
Russell King8e140362007-01-02 23:40:30 +0000584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT; /* Extract the architecture version */
586 printk("implementor %02x architecture %d part %02x variant %x rev %x\n",
587 (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
588 (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT,
589 (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
590 (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
591 (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
Russell Kingefe90d22006-12-08 15:22:20 +0000592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 vfp_vector = vfp_support_entry;
Russell Kingd6551e82006-06-21 13:31:52 +0100594
595 thread_register_notifier(&vfp_notifier_block);
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100596 vfp_pm_init();
Russell Kingefe90d22006-12-08 15:22:20 +0000597
598 /*
599 * We detected VFP, and the support code is
600 * in place; report VFP support to userspace.
601 */
602 elf_hwcap |= HWCAP_VFP;
Catalin Marinas7279dc32009-02-11 13:13:56 +0100603#ifdef CONFIG_VFPv3
Catalin Marinas325ffc32010-03-26 15:44:57 +0100604 if (VFP_arch >= 2) {
Catalin Marinas7279dc32009-02-11 13:13:56 +0100605 elf_hwcap |= HWCAP_VFPv3;
606
607 /*
608 * Check for VFPv3 D16. CPUs in this configuration
609 * only have 16 x 64bit registers.
610 */
611 if (((fmrx(MVFR0) & MVFR0_A_SIMD_MASK)) == 1)
612 elf_hwcap |= HWCAP_VFPv3D16;
613 }
614#endif
Catalin Marinas2bedbdf2008-11-06 13:23:07 +0000615#ifdef CONFIG_NEON
616 /*
617 * Check for the presence of the Advanced SIMD
618 * load/store instructions, integer and single
Tony Lindgren5aaf2542010-07-01 13:41:05 +0100619 * precision floating point operations. Only check
620 * for NEON if the hardware has the MVFR registers.
Catalin Marinas2bedbdf2008-11-06 13:23:07 +0000621 */
Tony Lindgren5aaf2542010-07-01 13:41:05 +0100622 if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) {
623 if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100)
624 elf_hwcap |= HWCAP_NEON;
625 }
Catalin Marinas2bedbdf2008-11-06 13:23:07 +0000626#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 return 0;
629}
630
631late_initcall(vfp_init);